-
Notifications
You must be signed in to change notification settings - Fork 151
/
parsec-vdd.h
359 lines (302 loc) · 12.2 KB
/
parsec-vdd.h
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2023, Nguyen Duy <[email protected]> All rights reserved.
* GitHub repo: https://github.com/nomi-san/parsec-vdd/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __PARSEC_VDD_H
#define __PARSEC_VDD_H
#include <windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#ifdef _MSC_VER
#pragma comment(lib, "cfgmgr32.lib")
#pragma comment(lib, "setupapi.lib")
#endif
#ifdef __cplusplus
namespace parsec_vdd
{
#endif
// Device helper.
//////////////////////////////////////////////////
typedef enum {
DEVICE_OK = 0, // Ready to use
DEVICE_INACCESSIBLE, // Inaccessible
DEVICE_UNKNOWN, // Unknown status
DEVICE_UNKNOWN_PROBLEM, // Unknown problem
DEVICE_DISABLED, // Device is disabled
DEVICE_DRIVER_ERROR, // Device encountered error
DEVICE_RESTART_REQUIRED, // Must restart PC to use (could ignore but would have issue)
DEVICE_DISABLED_SERVICE, // Service is disabled
DEVICE_NOT_INSTALLED // Driver is not installed
} DeviceStatus;
/**
* Query the driver status.
*
* @param classGuid The GUID of the class.
* @param deviceId The device/hardware ID of the driver.
* @return DeviceStatus
*/
static DeviceStatus QueryDeviceStatus(const GUID *classGuid, const char *deviceId)
{
DeviceStatus status = DEVICE_INACCESSIBLE;
SP_DEVINFO_DATA devInfoData;
ZeroMemory(&devInfoData, sizeof(SP_DEVINFO_DATA));
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
HDEVINFO devInfo = SetupDiGetClassDevsA(classGuid, NULL, NULL, DIGCF_PRESENT);
if (devInfo != INVALID_HANDLE_VALUE)
{
BOOL foundProp = FALSE;
UINT deviceIndex = 0;
do
{
if (!SetupDiEnumDeviceInfo(devInfo, deviceIndex, &devInfoData))
break;
DWORD requiredSize = 0;
SetupDiGetDeviceRegistryPropertyA(devInfo, &devInfoData,
SPDRP_HARDWAREID, NULL, NULL, 0, &requiredSize);
if (requiredSize > 0)
{
DWORD regDataType = 0;
LPBYTE propBuffer = (LPBYTE)calloc(1, requiredSize);
if (SetupDiGetDeviceRegistryPropertyA(
devInfo,
&devInfoData,
SPDRP_HARDWAREID,
®DataType,
propBuffer,
requiredSize,
&requiredSize))
{
if (regDataType == REG_SZ || regDataType == REG_MULTI_SZ)
{
for (LPCSTR cp = (LPCSTR)propBuffer; ; cp += lstrlenA(cp) + 1)
{
if (!cp || *cp == 0 || cp >= (LPCSTR)(propBuffer + requiredSize))
{
status = DEVICE_NOT_INSTALLED;
goto except;
}
if (lstrcmpA(deviceId, cp) == 0)
break;
}
foundProp = TRUE;
ULONG devStatus, devProblemNum;
if (CM_Get_DevNode_Status(&devStatus, &devProblemNum, devInfoData.DevInst, 0) != CR_SUCCESS)
{
status = DEVICE_NOT_INSTALLED;
goto except;
}
if ((devStatus & (DN_DRIVER_LOADED | DN_STARTED)) != 0)
{
status = DEVICE_OK;
}
else if ((devStatus & DN_HAS_PROBLEM) != 0)
{
switch (devProblemNum)
{
case CM_PROB_NEED_RESTART:
status = DEVICE_RESTART_REQUIRED;
break;
case CM_PROB_DISABLED:
case CM_PROB_HARDWARE_DISABLED:
status = DEVICE_DISABLED;
break;
case CM_PROB_DISABLED_SERVICE:
status = DEVICE_DISABLED_SERVICE;
break;
default:
if (devProblemNum == CM_PROB_FAILED_POST_START)
status = DEVICE_DRIVER_ERROR;
else
status = DEVICE_UNKNOWN_PROBLEM;
break;
}
}
else
{
status = DEVICE_UNKNOWN;
}
}
}
except:
free(propBuffer);
}
++deviceIndex;
} while (!foundProp);
if (!foundProp && GetLastError() != 0)
status = DEVICE_NOT_INSTALLED;
SetupDiDestroyDeviceInfoList(devInfo);
}
return status;
}
/**
* Obtain the device handle.
* Returns NULL or INVALID_HANDLE_VALUE if fails, otherwise a valid handle.
* Should call CloseDeviceHandle to close this handle after use.
*
* @param interfaceGuid The adapter/interface GUID of the target device.
* @return HANDLE
*/
static HANDLE OpenDeviceHandle(const GUID *interfaceGuid)
{
HANDLE handle = INVALID_HANDLE_VALUE;
HDEVINFO devInfo = SetupDiGetClassDevsA(interfaceGuid,
NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (devInfo != INVALID_HANDLE_VALUE)
{
SP_DEVICE_INTERFACE_DATA devInterface;
ZeroMemory(&devInterface, sizeof(SP_DEVICE_INTERFACE_DATA));
devInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(devInfo, NULL, interfaceGuid, i, &devInterface); ++i)
{
DWORD detailSize = 0;
SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, NULL, 0, &detailSize, NULL);
SP_DEVICE_INTERFACE_DETAIL_DATA_A *detail = (SP_DEVICE_INTERFACE_DETAIL_DATA_A *)calloc(1, detailSize);
detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
if (SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, detail, detailSize, &detailSize, NULL))
{
handle = CreateFileA(detail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH,
NULL);
if (handle != NULL && handle != INVALID_HANDLE_VALUE)
break;
}
free(detail);
}
SetupDiDestroyDeviceInfoList(devInfo);
}
return handle;
}
/* Release the device handle */
static void CloseDeviceHandle(HANDLE handle)
{
if (handle != NULL && handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
// Parsec VDD core.
//////////////////////////////////////////////////
// Display name info.
static const char *VDD_DISPLAY_ID = "PSCCDD0"; // You will see it in registry (HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY)
static const char *VDD_DISPLAY_NAME = "ParsecVDA"; // You will see it in the [Advanced display settings] tab.
// Apdater GUID to obtain the device handle.
// {00b41627-04c4-429e-a26e-0265cf50c8fa}
static const GUID VDD_ADAPTER_GUID = { 0x00b41627, 0x04c4, 0x429e, { 0xa2, 0x6e, 0x02, 0x65, 0xcf, 0x50, 0xc8, 0xfa } };
static const char *VDD_ADAPTER_NAME = "Parsec Virtual Display Adapter";
// Class and hwid to query device status.
// {4d36e968-e325-11ce-bfc1-08002be10318}
static const GUID VDD_CLASS_GUID = { 0x4d36e968, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
static const char *VDD_HARDWARE_ID = "Root\\Parsec\\VDA";
// Actually up to 16 devices could be created per adapter
// so just use a half to avoid plugging lag.
static const int VDD_MAX_DISPLAYS = 8;
// Core IoControl codes, see usage below.
typedef enum {
VDD_IOCTL_ADD = 0x0022e004, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 1, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
VDD_IOCTL_REMOVE = 0x0022a008, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 2, METHOD_BUFFERED, FILE_WRITE_ACCESS)
VDD_IOCTL_UPDATE = 0x0022a00c, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 3, METHOD_BUFFERED, FILE_WRITE_ACCESS)
VDD_IOCTL_VERSION = 0x0022e010, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 4, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
// new code in driver v0.45
// relates to IOCTL_UPDATE and per display state
// but unused in Parsec app
VDD_IOCTL_UNKONWN = 0x0022a00c, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 5, METHOD_BUFFERED, FILE_WRITE_ACCESS)
} VddCtlCode;
// Generic DeviceIoControl for all IoControl codes.
static DWORD VddIoControl(HANDLE vdd, VddCtlCode code, const void *data, size_t size)
{
if (vdd == NULL || vdd == INVALID_HANDLE_VALUE)
return -1;
BYTE InBuffer[32];
ZeroMemory(InBuffer, sizeof(InBuffer));
OVERLAPPED Overlapped;
ZeroMemory(&Overlapped, sizeof(OVERLAPPED));
DWORD OutBuffer = 0;
DWORD NumberOfBytesTransferred;
if (data != NULL && size > 0)
memcpy(InBuffer, data, (size < sizeof(InBuffer)) ? size : sizeof(InBuffer));
Overlapped.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
DeviceIoControl(vdd, (DWORD)code, InBuffer, sizeof(InBuffer), &OutBuffer, sizeof(DWORD), NULL, &Overlapped);
if (!GetOverlappedResultEx(vdd, &Overlapped, &NumberOfBytesTransferred, 5000, FALSE))
{
CloseHandle(Overlapped.hEvent);
return -1;
}
if (Overlapped.hEvent != NULL)
CloseHandle(Overlapped.hEvent);
return OutBuffer;
}
/**
* Query VDD minor version.
*
* @param vdd The device handle of VDD.
* @return The number of minor version.
*/
static int VddVersion(HANDLE vdd)
{
int minor = VddIoControl(vdd, VDD_IOCTL_VERSION, NULL, 0);
return minor;
}
/**
* Update/ping to VDD.
* Should call this function in a side thread for each
* less than 100ms to keep all added virtual displays alive.
*
* @param vdd The device handle of VDD.
*/
static void VddUpdate(HANDLE vdd)
{
VddIoControl(vdd, VDD_IOCTL_UPDATE, NULL, 0);
}
/**
* Add/plug a virtual display.
*
* @param vdd The device handle of VDD.
* @return The index of the added display.
*/
static int VddAddDisplay(HANDLE vdd)
{
int idx = VddIoControl(vdd, VDD_IOCTL_ADD, NULL, 0);
VddUpdate(vdd);
return idx;
}
/**
* Remove/unplug a virtual display.
*
* @param vdd The device handle of VDD.
* @param index The index of the display will be removed.
*/
static void VddRemoveDisplay(HANDLE vdd, int index)
{
// 16-bit BE index
UINT16 indexData = ((index & 0xFF) << 8) | ((index >> 8) & 0xFF);
VddIoControl(vdd, VDD_IOCTL_REMOVE, &indexData, sizeof(indexData));
VddUpdate(vdd);
}
#ifdef __cplusplus
}
#endif
#endif