forked from nomi-san/parsec-vdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsecVDD.cs
323 lines (277 loc) · 10.9 KB
/
ParsecVDD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace ParsecVDisplay
{
internal static class ParsecVDD
{
public const string NAME = "Parsec Virtual Display";
public const string DISPLAY_ID = "PSCCDD0";
public const string DISPLAY_NAME = "ParsecVDA";
public const string ADAPTER = "Parsec Virtual Display Adapter";
public const string ADAPTER_GUID = "{00b41627-04c4-429e-a26e-0265cf50c8fa}";
public const string HARDWARE_ID = @"Root\Parsec\VDA";
public const string CLASS_GUID = "{4d36e968-e325-11ce-bfc1-08002be10318}";
static IntPtr VddHandle;
// actually 16 devices could be created per adapter
// so just use a half to avoid plugging lag
public static int MAX_DISPLAYS => 8;
public static bool Init()
{
if (Device.OpenHandle(ADAPTER_GUID, out VddHandle))
{
Ping();
return true;
}
return false;
}
public static void Uninit()
{
Device.CloseHandle(VddHandle);
}
public static List<Display> GetDisplays(out bool noMonitors)
{
var displays = Display.GetAllDisplays();
noMonitors = displays.Count == 0;
displays = displays.FindAll(d => d.DisplayName
.Equals(DISPLAY_ID, StringComparison.OrdinalIgnoreCase));
noMonitors = displays.Count == 0 && noMonitors;
return displays;
}
public static List<Display> GetDisplays()
{
return GetDisplays(out var _);
}
public static Device.Status QueryStatus()
{
return Device.QueryStatus(CLASS_GUID, HARDWARE_ID);
}
public static bool QueryVersion(out string version)
{
if (Core.IoControl(VddHandle, Core.IOCTL_VERSION, null, out int vernum, 100))
{
int major = 0;
int minor = vernum & 0xFFFF;
version = $"{major}.{minor}";
return true;
}
else
{
version = "0.???";
return false;
}
}
public static bool AddDisplay(out int index)
{
if (Core.IoControl(VddHandle, Core.IOCTL_ADD, null, out index, 5000))
{
Ping();
return true;
}
return false;
}
public static bool RemoveDisplay(int index)
{
var input = new byte[2];
input[1] = (byte)(index & 0xFF);
if (Core.IoControl(VddHandle, Core.IOCTL_REMOVE, input, 1000))
{
Ping();
return true;
}
return false;
}
public static void Ping()
{
Core.IoControl(VddHandle, Core.IOCTL_UPDATE, null, 1000);
}
static unsafe class Core
{
public const uint IOCTL_ADD = 0x22E004;
public const uint IOCTL_REMOVE = 0x22A008;
public const uint IOCTL_UPDATE = 0x22A00C;
public const uint IOCTL_VERSION = 0x22E010;
// new code in driver v0.45
// relates to IOCTL_UPDATE and per display state
// but unused in Parsec app
public const uint IOCTL_UNKNOWN1 = 0x22A014;
static bool IoControl(IntPtr handle, uint code, byte[] input, int* result, int timeout)
{
var InBuffer = new byte[32];
var Overlapped = new Native.OVERLAPPED();
if (input != null && input.Length > 0)
{
Array.Copy(input, InBuffer, Math.Min(input.Length, InBuffer.Length));
}
fixed (byte* buffer = InBuffer)
{
int outputLength = result != null ? sizeof(int) : 0;
Overlapped.hEvent = Native.CreateEvent(null, false, false, null);
Native.DeviceIoControl(handle, code,
buffer, InBuffer.Length,
result, outputLength,
null, ref Overlapped);
bool success = Native.GetOverlappedResultEx(handle, ref Overlapped,
out var NumberOfBytesTransferred, timeout, false);
if (Overlapped.hEvent != IntPtr.Zero)
Native.CloseHandle(Overlapped.hEvent);
return success;
}
}
public static bool IoControl(IntPtr handle, uint code, byte[] input, int timeout)
{
return IoControl(handle, code, input, null, timeout);
}
public static bool IoControl(IntPtr handle, uint code, byte[] input, out int result, int timeout)
{
int output;
bool success = IoControl(handle, code, input, &output, timeout);
result = output;
return success;
}
}
public static IList<Display.Mode> GetCustomDisplayModes()
{
var list = new List<Display.Mode>();
using (var vdd = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Parsec\\vdd", RegistryKeyPermissionCheck.ReadSubTree))
{
if (vdd != null)
{
for (int i = 0; i < 5; i++)
{
using (var index = vdd.OpenSubKey($"{i}", RegistryKeyPermissionCheck.ReadSubTree))
{
if (index != null)
{
var width = index.GetValue("width");
var height = index.GetValue("height");
var hz = index.GetValue("hz");
if (width != null && height != null && hz != null)
{
list.Add(new Display.Mode
{
Width = Convert.ToUInt16(width),
Height = Convert.ToUInt16(height),
Hz = Convert.ToUInt16(hz),
});
}
}
}
}
}
}
return list;
}
// Requires admin perm
public static void SetCustomDisplayModes(List<Display.Mode> modes)
{
using (var vdd = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Parsec\\vdd", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (vdd != null)
{
for (int i = 0; i < 5; i++)
{
using (var index = vdd.CreateSubKey($"{i}", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (i >= modes.Count && index != null)
{
index.Dispose();
vdd.DeleteSubKey($"{i}");
}
else if (index != null)
{
index.SetValue("width", modes[i].Width, RegistryValueKind.DWord);
index.SetValue("height", modes[i].Height, RegistryValueKind.DWord);
index.SetValue("hz", modes[i].Hz, RegistryValueKind.DWord);
}
}
}
}
}
}
public enum ParentGPU
{
Auto = 0,
NVIDIA = 0x10DE,
AMD = 0x1002,
}
public static ParentGPU GetParentGPU()
{
using (var parameters = Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WUDF\\Services\\ParsecVDA\\Parameters",
RegistryKeyPermissionCheck.ReadSubTree))
{
if (parameters != null)
{
object value = parameters.GetValue("PreferredRenderAdapterVendorId");
if (value != null)
{
return (ParentGPU)Convert.ToInt32(value);
}
}
}
return ParentGPU.Auto;
}
// Requires admin perm
public static void SetParentGPU(ParentGPU kind)
{
using (var parameters = Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WUDF\\Services\\ParsecVDA\\Parameters",
RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (parameters != null)
{
if (kind == ParentGPU.Auto)
{
parameters.DeleteValue("PreferredRenderAdapterVendorId", false);
}
else
{
parameters.SetValue("PreferredRenderAdapterVendorId",
(uint)kind, RegistryValueKind.DWord);
}
}
}
}
static unsafe class Native
{
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeviceIoControl(
IntPtr device, uint code,
void* lpInBuffer, int nInBufferSize,
void* lpOutBuffer, int nOutBufferSize,
void* lpBytesReturned,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetOverlappedResultEx(
IntPtr handle,
ref OVERLAPPED lpOverlapped,
out uint lpNumberOfBytesTransferred,
int dwMilliseconds,
[MarshalAs(UnmanagedType.Bool)] bool bAlertable
);
[StructLayout(LayoutKind.Sequential)]
public struct OVERLAPPED
{
public IntPtr Internal;
public IntPtr InternalHigh;
public IntPtr Pointer;
public IntPtr hEvent;
}
[DllImport("kernel32.dll", EntryPoint = "CreateEventW", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateEvent(
void* lpEventAttributes,
[MarshalAs(UnmanagedType.Bool)] bool bManualReset,
[MarshalAs(UnmanagedType.Bool)] bool bInitialState,
string lpName
);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr handle);
}
}
}