This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (276 loc) · 8.13 KB
/
index.js
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
const ffi = require('ffi');
const ref = require('ref');
const StructType = require('ref-struct');
const ArrayType = require('ref-array');
const {
getError,
WootingAnalogResult_Enum,
DeviceEventType_Enum,
KeycodeType_Enum,
VirtualKey_Enum,
ScanCodes_Enum,
} = require(__dirname + '/util');
const platform = (require('os')).platform();
if (platform === 'win32'){
woot_loc = './wrapper/wooting_analog_wrapper.dll';
}else if(platform === 'linux'){
woot_loc = './wrapper/libwooting_analog_wrapper.so';
}else if(platform === 'darwin'){
woot_loc = './wrapper/libwooting_analog_wrapper.dylib';
}else{
throw new Error('unsupported platform for the wooting_analog_wrapper')
}
const ushort = ref.types.ushort;
const ushort_Ptr = ref.refType(ushort);
const ushortArray = ArrayType(ushort);
const ushortArray_Ptr = ref.refType(ushortArray);
const float = ref.types.float;
const float_Ptr = ref.refType(float);
const floatArray = ArrayType(float);
const floatArray_Ptr = ref.refType(floatArray);
const WootingAnalogResult = ref.types.int;
const WootingAnalog_DeviceID = ref.types.uint64;
const WootingAnalog_DeviceInfo = StructType({
vendor_id: ref.types.uint16,
product_id: ref.types.uint16,
manufacturer_name: ref.types.CString,
device_name: ref.types.CString,
device_id: WootingAnalog_DeviceID,
});
const WootingAnalog_DeviceInfo_Ptr = ref.refType(WootingAnalog_DeviceInfo);
const WootingAnalog_DeviceEventType = ref.types.int;
const WootingAnalogCallback = ffi.Function(ref.types.void, [
WootingAnalog_DeviceEventType,
WootingAnalog_DeviceInfo_Ptr,
]);
const WootingAnalog_KeycodeType = ref.types.int;
const wooting_analog_wrapper = ffi.Library(__dirname + woot_loc, {
'wooting_analog_clear_device_event_cb': [
WootingAnalogResult,
[]
],
'wooting_analog_get_connected_devices_info': [
ref.types.int,
[
WootingAnalog_DeviceInfo_Ptr, ref.types.uint,
]
],
'wooting_analog_initialise': [
WootingAnalogResult,
[]
],
'wooting_analog_is_initialised': [
ref.types.bool,
[]
],
'wooting_analog_read_analog': [
ref.types.float,
[
ref.types.ushort,
]
],
'wooting_analog_read_analog_device': [
ref.types.float,
[
ref.types.ushort,
WootingAnalog_DeviceID,
]
],
'wooting_analog_read_full_buffer': [
ref.types.int,
[
ushortArray_Ptr,
floatArray_Ptr,
ref.types.uint,
]
],
'wooting_analog_read_full_buffer_device': [
ref.types.int,
[
ushortArray_Ptr,
floatArray_Ptr,
ref.types.uint,
WootingAnalog_DeviceID,
]
],
'wooting_analog_set_device_event_cb': [
WootingAnalogResult,
[
WootingAnalogCallback,
]
],
'wooting_analog_set_keycode_mode': [
WootingAnalogResult,
[
WootingAnalog_KeycodeType,
]
],
'wooting_analog_uninitialise': [
WootingAnalogResult,
[]
],
});
class WootingWrapper {
/**
* Clears the device callback function
*
* @returns { number } The result of the function
* @memberof WootingWrapper
*/
clear_device_event_cb() {
const result = wooting_analog_wrapper.wooting_analog_clear_device_event_cb();
if (result !== WootingAnalogResult_Enum.Ok) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Fetches the connected devices infos
*
* @param { Buffer } device_info Device info buffer
* @param { number } length Length of the buffer to fill
* @returns { number } If bigger than 0 the number of devices that are connected
* @memberof WootingWrapper
*/
get_connected_devices_info(device_info, length) {
const result = wooting_analog_wrapper.wooting_analog_get_connected_devices_info(device_info, length);
if (result < 0) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Initilizes the Wooting Analog Wrapper
*
* @returns { number } The result of the function
* @memberof WootingWrapper
*/
initialise() {
const result = wooting_analog_wrapper.wooting_analog_initialise();
if (result !== WootingAnalogResult_Enum.Ok) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Checks if the Wooting Analog Wrapper is initilized
*
* @returns { boolean } True if initilized, false otherwise
* @memberof WootingWrapper
*/
is_initialised() {
return wooting_analog_wrapper.wooting_analog_is_initialised();
}
/**
* Read the Analog value of a specified key
*
* @param { number } keyCode keyCode of the key you want to read
* @returns { number } Analog value of the specified key
* @memberof WootingWrapper
*/
read_analog(keyCode) {
const result = wooting_analog_wrapper.wooting_analog_read_analog(keyCode);
if (result < 0 || result > 1) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Reads the analog value of a specified key from the specified device
*
* @param { number } keyCode The keyCode you want to fetch
* @param { number } device_id The device id that will be fetched from
* @returns { number } Analog value of the specified key
* @memberof WootingWrapper
*/
read_analog_device(keyCode, device_id) {
const result = wooting_analog_wrapper.wooting_analog_read_analog_device(keyCode, device_id);
if (result < 0 || result > 1) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Read the full analog buffer of a specified key
*
* @param { Buffer } code_buffer The keyCode buffer
* @param { Buffer } analog_buffer The analog buffer
* @param { number } length The length of the code and analog buffer
* @returns { number }
* @memberof WootingWrapper
*/
read_full_buffer(code_buffer, analog_buffer, length) {
const result = wooting_analog_wrapper.wooting_analog_read_full_buffer(code_buffer, analog_buffer, length);
if (result < 0) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Read the full analog buffer of a specified key from a specified device
*
* @param { Buffer } code_buffer The keyCode buffer
* @param { Buffer } analog_buffer The analog buffer
* @param { number } length The length of the code and analog buffer
* @param { number } device_id The device id that will be fetched from
* @returns { number }
* @memberof WootingWrapper
*/
read_full_buffer_device(code_buffer, analog_buffer, length, device_id) {
const result = wooting_analog_wrapper.wooting_analog_read_full_buffer_device(code_buffer, analog_buffer, length, device_id);
if (result < 0) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Set the analog device callback function
*
* @param { Object } callback The callback function to call on device events
* @returns { number }
* @memberof WootingWrapper
*/
set_device_event_cb(callback) {
this.ffi_callback = ffi.Callback(ref.types.void, [
WootingAnalog_DeviceEventType,
WootingAnalog_DeviceInfo_Ptr,
], (eventType, deviceInfo) => {
setImmediate(callback, eventType, deviceInfo.deref());
});
const result = wooting_analog_wrapper.wooting_analog_set_device_event_cb(this.ffi_callback);
if (result !== WootingAnalogResult_Enum.Ok) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Set the key mode of the device
*
* @param { number } mode The mode you want to set the device to
* @returns { number }
* @memberof WootingWrapper
*/
set_keycode_mode(mode) {
const result = wooting_analog_wrapper.wooting_analog_set_keycode_mode(mode);
if (result !== WootingAnalogResult_Enum.Ok) throw Error(getError(WootingAnalogResult_Enum, result));
return result;
}
/**
* Uninitilize the Wooting Analog Wrapper
*
* @returns { number }
* @memberof WootingWrapper
*/
uninitialise() {
return wooting_analog_wrapper.wooting_analog_uninitialise();
}
}
module.exports = {
WootingWrapper,
WootingAnalogResult: WootingAnalogResult_Enum,
DeviceEventType: DeviceEventType_Enum,
KeycodeType: KeycodeType_Enum,
types: {
WootingAnalogResult,
WootingAnalog_DeviceID,
WootingAnalog_DeviceInfo,
WootingAnalog_DeviceInfo_Ptr,
WootingAnalog_DeviceEventType,
WootingAnalogCallback,
WootingAnalog_KeycodeType,
ushort,
ushort_Ptr,
ushortArray,
ushortArray_Ptr,
float,
float_Ptr,
floatArray,
floatArray_Ptr,
},
VirtualKey: VirtualKey_Enum,
ScanCodes_Enum,
}