-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHidDevices.ahk
219 lines (183 loc) · 5.1 KB
/
HidDevices.ahk
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
#Include HidDeviceInfo.ahk
#Include Helpers.ahk
#Include Errors.ahk
#Include WinApi.ahk
class HidDevices {
; TODO: add docs
static Find(vendorId, productId, usagePage, usageId, &err) {
err := ""
devicePaths := this._ListDevicePaths(&err)
if err {
return ""
}
if devicePaths.Length == 0 {
err := DeviceNotFoundError()
return ""
}
hModule := DllCall("kernel32\LoadLibraryW", "Str", "Hid.dll", "Ptr")
if not hModule {
errorCode := A_LastError
err := OSErrorC("Failed to load Hid.dll library: " _GetErrorMessage(), errorCode)
return ""
}
try {
for devicePath in devicePaths {
secAttributes := _SECURITY_ATTRIBUTES(0, true)
hDevice := DllCall("CreateFileW",
"Ptr", StrPtr(devicePath),
"UInt", ACCESS_NONE,
"UInt", FILE_SHARE_READ | FILE_SHARE_WRITE,
"Ptr", secAttributes,
"UInt", OPEN_EXISTING,
"UInt", 0,
"Ptr", 0,
"Ptr")
if hDevice == INVALID_HANDLE_VALUE {
; TODO: to log
continue
}
try {
hidAttributes := _HIDD_ATTRIBUTES()
succeeded := DllCall("hid\HidD_GetAttributes", "Ptr", hDevice, "Ptr", hidAttributes)
if not succeeded {
; TODO: to log
continue
}
if vendorId != hidAttributes.GetVendorID() || productId != hidAttributes.GetProductID() {
continue
}
succeeded := DllCall("hid\HidD_GetPreparsedData", "Ptr", hDevice, "Ptr*", &preparsedData:=0)
if not succeeded {
; TODO: to log
continue
}
try {
caps := _HIDP_CAPS()
succeeded := DllCall("hid\HidP_GetCaps", "Ptr", preparsedData, "Ptr", caps)
if not succeeded {
; TODO: to log
continue
}
if caps.GetUsagePage() != usagePage || caps.GetUsageID() != usageId {
continue
}
stringBuff := Buffer(254, 0)
succeeded := DllCall("hid\HidD_GetManufacturerString",
"Ptr", hDevice,
"Ptr", stringBuff,
"UInt", stringBuff.Size)
if succeeded {
manufacturerString := StrGet(stringBuff)
} else {
manufacturerString := "Unknown"
; TODO: to log
}
succeeded := DllCall("hid\HidD_GetProductString",
"Ptr", hDevice,
"Ptr", stringBuff,
"UInt", stringBuff.Size)
if succeeded {
productString := StrGet(stringBuff)
} else {
productString := "Unknown"
; TODO: to log
}
return HidDeviceInfo(
devicePath,
manufacturerString,
productString,
caps.GetInputReportByteLength(),
caps.GetOutputReportByteLength(),
vendorId,
productId,
usagePage,
usageId)
} finally {
if not DllCall("hid\HidD_FreePreparsedData", "Ptr", preparsedData) {
; TODO: to log
}
}
} finally {
if not DllCall("kernel32\CloseHandle", "Ptr", hDevice) {
; TODO: to log
}
}
}
err := DeviceNotFoundError()
return ""
} finally {
if not DllCall("kernel32\FreeLibrary", "Ptr", hModule) {
; TODO: to log
}
}
}
; TODO: add docs
static _ListDevicePaths(&err) {
hidGuid := Buffer(16)
DllCall("hid\HidD_GetHidGuid", "Ptr", hidGuid)
hModule := DllCall("kernel32\LoadLibraryW", "Str", "SetupApi.dll", "Ptr")
if not hModule {
errorCode := A_LastError
err := OSErrorC("Failed to load SetupApi.dll library: " _GetErrorMessage(), errorCode)
return ""
}
try {
hDevInfoSet := DllCall("setupapi\SetupDiGetClassDevsW",
"Ptr", hidGuid,
"Ptr", 0,
"Ptr", 0,
"UInt", DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, "Ptr")
if hDevInfoSet == INVALID_HANDLE_VALUE {
errorCode := A_LastError
err := OSErrorC("Failed to get Device Information Set: " _GetErrorMessage(), errorCode)
return ""
}
try {
devInterfaceData := _SP_DEVICE_INTERFACE_DATA()
devicePathList := []
mIndex := 0
while DllCall("setupapi\SetupDiEnumDeviceInterfaces",
"Ptr", hDevInfoSet,
"Ptr", 0,
"Ptr", hidGuid,
"UInt", mIndex++,
"Ptr", devInterfaceData)
{
_ := DllCall("setupapi\SetupDiGetDeviceInterfaceDetailW",
"Ptr", hDevInfoSet,
"Ptr", devInterfaceData,
"Ptr", 0,
"UInt", 0,
"UInt*", &requiredSize:=0,
"Ptr", 0)
if A_LastError != ERROR_INSUFFICIENT_BUFFER {
; TODO: to log
continue
}
devInterfaceDetailData := _SP_DEVICE_INTERFACE_DETAIL_DATA(requiredSize)
succeeded := DllCall("setupapi\SetupDiGetDeviceInterfaceDetailW",
"Ptr", hDevInfoSet,
"Ptr", devInterfaceData,
"Ptr", devInterfaceDetailData,
"UInt", requiredSize,
"Ptr", 0,
"Ptr", 0)
if succeeded {
devicePathList.Push(devInterfaceDetailData.GetDevicePath())
} else {
; TODO: to log
}
}
return devicePathList
} finally {
if not DllCall("setupapi\SetupDiDestroyDeviceInfoList", "Ptr", hDevInfoSet) {
; TODO: to log
}
}
} finally {
if not DllCall("kernel32\FreeLibrary", "Ptr", hModule) {
; TODO: to log
}
}
}
}