forked from kolrabi/steamcontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteamcontroller_win32.c
More file actions
278 lines (220 loc) · 6.74 KB
/
steamcontroller_win32.c
File metadata and controls
278 lines (220 loc) · 6.74 KB
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
#if _WIN32
#include "include/sc.h"
#include "common.h"
#include <windows.h>
#include <hidsdi.h>
#include <setupapi.h>
#include <guiddef.h>
#include <stdio.h>
struct SteamControllerDevice {
HANDLE devHandle;
char* guidStr;
bool isWireless;
};
struct SteamControllerDeviceEnum {
struct SteamControllerDeviceEnum *next;
GUID guid;
SP_DEVICE_INTERFACE_DETAIL_DATA *pDevIntfDetailData;
HIDD_ATTRIBUTES hidAttribs;
};
SCAPI SteamControllerDeviceEnum * SteamController_EnumControllerDevices() {
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
HDEVINFO devInfo;
devInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT);
SP_DEVICE_INTERFACE_DATA devIntfData = { .cbSize = sizeof(SP_DEVICE_INTERFACE_DATA) };
DWORD devIndex = 0;
SteamControllerDeviceEnum *pEnum = NULL;
while (SetupDiEnumDeviceInterfaces(devInfo, NULL, &hidGuid, devIndex++, &devIntfData)) {
DWORD reqSize;
SetupDiGetDeviceInterfaceDetail(devInfo, &devIntfData, NULL, 0, &reqSize, NULL);
SP_DEVICE_INTERFACE_DETAIL_DATA *pDevIntfDetailData;
pDevIntfDetailData = (SP_DEVICE_INTERFACE_DETAIL_DATA*)malloc(reqSize);
pDevIntfDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(devInfo, &devIntfData, pDevIntfDetailData, reqSize, &reqSize, NULL)) {
fprintf(stderr, "SetupDiGetDeviceInterfaceDetail failed. Last error: %08lx\n", GetLastError());
free(pDevIntfDetailData);
continue;
}
HANDLE devFile;
devFile = CreateFile(
pDevIntfDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
if (devFile == INVALID_HANDLE_VALUE) {
free(pDevIntfDetailData);
continue;
}
HIDD_ATTRIBUTES hidAttribs = { .Size = sizeof(HIDD_ATTRIBUTES) };
HidD_GetAttributes(devFile, &hidAttribs);
CloseHandle(devFile);
if (hidAttribs.VendorID != USB_VID_VALVE) {
free(pDevIntfDetailData);
continue;
}
if (hidAttribs.ProductID != USB_PID_STEAMCONTROLLER_WIRED && hidAttribs.ProductID != USB_PID_STEAMCONTROLLER_WIRELESS) {
free(pDevIntfDetailData);
continue;
}
SteamControllerDeviceEnum *pNewEnum = malloc(sizeof(SteamControllerDeviceEnum));
pNewEnum->next = pEnum;
pNewEnum->pDevIntfDetailData = pDevIntfDetailData;
pNewEnum->guid = devIntfData.InterfaceClassGuid;
pNewEnum->hidAttribs = hidAttribs;
pEnum = pNewEnum;
}
SetupDiDestroyDeviceInfoList(devInfo);
return pEnum;
}
SCAPI SteamControllerDeviceEnum * SteamController_NextControllerDevice(SteamControllerDeviceEnum *pCurrent) {
if (!pCurrent)
return NULL;
SteamControllerDeviceEnum *pNext = pCurrent->next;
free(pCurrent->pDevIntfDetailData);
free(pCurrent);
return pNext;
}
SCAPI SteamControllerDevice * SteamController_Open(const SteamControllerDeviceEnum *pEnum) {
if (!pEnum)
return NULL;
SteamControllerDevice *pDevice = malloc(sizeof(SteamControllerDevice));
pDevice->isWireless = pEnum->hidAttribs.ProductID == USB_PID_STEAMCONTROLLER_WIRELESS;
pDevice->devHandle = CreateFile(
pEnum->pDevIntfDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
0,
NULL
);
char guidStr[256];
GUID* guid = &pEnum->guid;
sprintf(guidStr, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
(unsigned int)guid->Data1, guid->Data2, guid->Data3,
guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
pDevice->guidStr = strdup(guidStr);
SteamController_Initialize(pDevice);
return pDevice;
}
SCAPI void SteamController_Close(SteamControllerDevice *pDevice) {
if (!pDevice) {
return;
}
HANDLE handle = pDevice->devHandle;
free(pDevice->guidStr);
pDevice->guidStr = NULL;
pDevice->devHandle = NULL;
free(pDevice);
CloseHandle(handle);
}
bool SteamController_HIDSetFeatureReport(const SteamControllerDevice *pDevice, SteamController_HIDFeatureReport *pReport) {
if (!pDevice || !pReport || !pDevice->devHandle)
return false;
fprintf(stderr, "SteamController_HIDSetFeatureReport %02x\n", pReport->featureId);
for (int i = 0; i < 50; i++) {
bool ok = HidD_SetFeature(pDevice->devHandle, pReport, sizeof(SteamController_HIDFeatureReport));
if (ok)
return true;
fprintf(stderr, "HidD_SetFeature failed. Last error: %08lx\n", GetLastError());
Sleep(1);
}
return false;
}
bool SteamController_HIDGetFeatureReport(const SteamControllerDevice *pDevice, SteamController_HIDFeatureReport *pReport) {
if (!pDevice || !pReport || !pDevice->devHandle)
return false;
uint8_t featureId = pReport->featureId;
SteamController_HIDSetFeatureReport(pDevice, pReport);
fprintf(stderr, "SteamController_HIDGetFeatureReport %02x\n", pReport->featureId);
for (int i = 0; i < 50; i++) {
bool ok = HidD_GetFeature(pDevice->devHandle, pReport, sizeof(SteamController_HIDFeatureReport));
if (ok) {
if (featureId == pReport->featureId)
return true;
continue;
}
fprintf(stderr, "HidD_SetFeature failed. Last error: %08lx\n", GetLastError());
Sleep(1);
}
return false;
}
bool SCAPI SteamController_IsWirelessDongle(const SteamControllerDevice *pDevice) {
if (!pDevice)
return false;
return pDevice->isWireless;
}
/**
* Read the range of eventnts from the device.
*
* @param pController Device to use.
* @param pEvent Where to store event data.
*
* @return The total number of successfully read events
*/
int SCAPI SteamController_ReadEvents(const SteamControllerDevice *pDevice, SteamControllerEvent **ppEvents, int total) {
if (!pDevice) {
return 0;
}
if (!ppEvents) {
return 0;
}
size_t size = 65 * total;
uint8_t* eventDataBuf = malloc(size);
size_t len = SteamController_ReadRaw(pDevice, eventDataBuf, size);
if (!len) {
free(eventDataBuf);
return 0;
}
uint8_t *eventData = eventDataBuf;
int totalRead = 0;
int i = 0;
while (len > 0) {
if (!*eventData) {
eventData++;
}
else
{
break;
}
SteamControllerEvent event;
uint8_t eventType = processEvent(&event, eventData, 64);
if (eventType != 0) {
*ppEvents[i] = event;
i++;
totalRead = i;
}
eventData += 64;
len -= 65;
}
free(eventDataBuf);
return totalRead;
}
SCAPI size_t SteamController_ReadRaw(const SteamControllerDevice *pDevice, uint8_t *buffer, size_t maxLen) {
if (!pDevice)
return 0;
DWORD bytesRead = 0;
BOOL isOk = ReadFile(pDevice->devHandle, buffer, maxLen, &bytesRead, NULL);
if (!isOk) {
DWORD dwLastError = GetLastError();
if (ERROR_IO_PENDING != dwLastError) {
return 0;
}
}
return bytesRead & 0xff;
}
SCAPI const char* SteamController_GetId(const struct SteamControllerDevice *pDevice) {
return pDevice->guidStr;
}
SCAPI int SteamController_Init() {
return 0;
}
SCAPI void SteamController_Exit() {
}
#endif