-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsteamcontroller_win32.c
200 lines (153 loc) · 5.48 KB
/
steamcontroller_win32.c
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
#if _WIN32
#include "steamcontroller.h"
#include "common.h"
#include <windows.h>
#include <hidsdi.h>
#include <setupapi.h>
#include <guiddef.h>
#include <stdio.h>
struct SteamControllerDevice {
HANDLE devHandle;
HANDLE reportEvent;
OVERLAPPED overlapped;
bool isWireless;
};
struct SteamControllerDeviceEnum {
struct SteamControllerDeviceEnum *next;
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->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
);
pDevice->reportEvent = CreateEvent(NULL, true, false, NULL);
pDevice->overlapped.hEvent = pDevice->reportEvent;
pDevice->overlapped.Offset = 0;
pDevice->overlapped.OffsetHigh = 0;
SteamController_Initialize(pDevice);
return pDevice;
}
SCAPI void SteamController_Close(SteamControllerDevice *pDevice) {
if (!pDevice)
return;
CloseHandle(pDevice->reportEvent);
CloseHandle(pDevice->devHandle);
free(pDevice);
}
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;
}
uint8_t SteamController_ReadRaw(const SteamControllerDevice *pDevice, uint8_t *buffer, uint8_t maxLen) {
if (!pDevice)
return 0;
DWORD bytesRead = 0;
ReadFile(pDevice->devHandle, buffer, maxLen, &bytesRead, (OVERLAPPED*)&pDevice->overlapped);
WaitForSingleObject(pDevice->reportEvent, 0);
return bytesRead & 0xff;
}
#endif