@kmamal/sdl 0.11.5 → 0.11.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +49 -10
- package/binding.gyp +1 -0
- package/package.json +2 -2
- package/scripts/build-sdl.mjs +4 -1
- package/scripts/install.mjs +6 -3
- package/scripts/util/fetch.js +6 -3
- package/src/javascript/audio/audio-instance.js +2 -3
- package/src/javascript/audio/device.js +1 -10
- package/src/javascript/audio/index.js +5 -7
- package/src/javascript/controller/controller-instance.js +2 -0
- package/src/javascript/controller/device.js +14 -3
- package/src/javascript/controller/index.js +5 -11
- package/src/javascript/events/index.js +56 -126
- package/src/javascript/events/reconcile-audio-devices.js +15 -0
- package/src/javascript/events/reconcile-displays.js +16 -0
- package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
- package/src/javascript/events/reconcile.js +61 -0
- package/src/javascript/joystick/device.js +9 -3
- package/src/javascript/joystick/index.js +13 -5
- package/src/javascript/joystick/joystick-instance.js +2 -0
- package/src/javascript/sensor/sensor-instance.js +2 -0
- package/src/javascript/video/display.js +4 -0
- package/src/javascript/video/index.js +1 -0
- package/src/native/audio.cpp +10 -12
- package/src/native/audio.h +4 -0
- package/src/native/controller.cpp +72 -56
- package/src/native/controller.h +3 -0
- package/src/native/enums.cpp +315 -315
- package/src/native/events.cpp +156 -80
- package/src/native/events.h +1 -0
- package/src/native/global.cpp +46 -20
- package/src/native/joystick.cpp +87 -48
- package/src/native/joystick.h +3 -2
- package/src/native/keyboard.cpp +1 -1
- package/src/native/mouse.cpp +2 -2
- package/src/native/power.cpp +9 -3
- package/src/native/sensor.cpp +24 -13
- package/src/native/video.cpp +21 -13
- package/src/native/video.h +1 -0
- package/src/native/window.cpp +28 -22
- package/src/types/index.d.ts +57 -17
|
@@ -1,74 +1,11 @@
|
|
|
1
1
|
const Globals = require('../globals')
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const { video: videoModule } = require('../video')
|
|
4
|
+
const { reconcileDisplays } = require('./reconcile-displays')
|
|
4
5
|
const { mapping } = require('../keyboard/key-mapping')
|
|
5
|
-
const {
|
|
6
|
-
const {
|
|
7
|
-
const { audio: audioModule } = require('../audio')
|
|
6
|
+
const { reconcileAudioDevices } = require('./reconcile-audio-devices')
|
|
7
|
+
const { reconcileJoystickAndControllerDevices } = require('./reconcile-joystick-and-controller-devices')
|
|
8
8
|
const { clipboard: clipboardModule } = require('../clipboard')
|
|
9
|
-
const {
|
|
10
|
-
make: makeJoystickDevice,
|
|
11
|
-
compare: compareJoystickDevice,
|
|
12
|
-
} = require('../joystick/device')
|
|
13
|
-
const {
|
|
14
|
-
make: makeControllerDevice,
|
|
15
|
-
compare: compareControllerDevice,
|
|
16
|
-
filter: filterControllerDevice,
|
|
17
|
-
} = require('../controller/device')
|
|
18
|
-
const {
|
|
19
|
-
make: makeAudioDevice,
|
|
20
|
-
compare: compareAudioDevice,
|
|
21
|
-
} = require('../audio/device')
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const reconcileDevices = (emitter, mainList, currList, compare) => {
|
|
25
|
-
currList.sort(compare)
|
|
26
|
-
|
|
27
|
-
let mainIndex = 0
|
|
28
|
-
let currIndex = 0
|
|
29
|
-
let mainDevice = mainList[mainIndex]
|
|
30
|
-
let currDevice = currList[currIndex]
|
|
31
|
-
while (mainIndex < mainList.length && currIndex < currList.length) {
|
|
32
|
-
const cmp = compare(mainDevice, currDevice)
|
|
33
|
-
if (cmp === 0) {
|
|
34
|
-
mainDevice = mainList[++mainIndex]
|
|
35
|
-
currDevice = currList[++currIndex]
|
|
36
|
-
}
|
|
37
|
-
else if (cmp < 0) {
|
|
38
|
-
mainList.splice(mainIndex, 1)
|
|
39
|
-
const type = 'deviceRemove'
|
|
40
|
-
const event = { type, device: mainDevice }
|
|
41
|
-
emitter.emit(type, event)
|
|
42
|
-
mainDevice = mainList[mainIndex]
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
mainList.splice(mainIndex, 0, currDevice)
|
|
46
|
-
mainDevice = mainList[++mainIndex]
|
|
47
|
-
const type = 'deviceAdd'
|
|
48
|
-
const event = { type, device: currDevice }
|
|
49
|
-
emitter.emit(type, event)
|
|
50
|
-
currDevice = currList[++currIndex]
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (mainIndex < mainList.length) {
|
|
55
|
-
while (mainIndex < mainList.length) {
|
|
56
|
-
[ mainDevice ] = mainList.splice(mainIndex, 1)
|
|
57
|
-
const type = 'deviceRemove'
|
|
58
|
-
const event = { type, device: mainDevice }
|
|
59
|
-
emitter.emit(type, event)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
while (currIndex < currList.length) {
|
|
64
|
-
mainList.push(currDevice)
|
|
65
|
-
const type = 'deviceAdd'
|
|
66
|
-
const event = { type, device: currDevice }
|
|
67
|
-
emitter.emit(type, event)
|
|
68
|
-
currDevice = currList[++currIndex]
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
9
|
|
|
73
10
|
|
|
74
11
|
const handleEvent = (event) => {
|
|
@@ -78,7 +15,7 @@ const handleEvent = (event) => {
|
|
|
78
15
|
|
|
79
16
|
switch (family) {
|
|
80
17
|
case 'app': {
|
|
81
|
-
if (
|
|
18
|
+
if (type !== 'quit') { return }
|
|
82
19
|
|
|
83
20
|
for (const window of Globals.windows.all.values()) {
|
|
84
21
|
window.destroyGently()
|
|
@@ -90,15 +27,40 @@ const handleEvent = (event) => {
|
|
|
90
27
|
const { displayIndex } = event
|
|
91
28
|
delete event.displayIndex
|
|
92
29
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
30
|
+
switch (type) {
|
|
31
|
+
case 'displayAdd':
|
|
32
|
+
case 'displayRemove':
|
|
33
|
+
{
|
|
34
|
+
const { displays } = event
|
|
35
|
+
delete event.displays
|
|
36
|
+
|
|
37
|
+
reconcileDisplays(displays)
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
case 'displayOrient': {
|
|
42
|
+
const display = Globals.displays[displayIndex]
|
|
43
|
+
display.orientation = event.orientation
|
|
44
|
+
} break
|
|
45
|
+
|
|
46
|
+
case 'displayMove': {
|
|
47
|
+
const { geometryX, geometryY, usableX, usableY } = event
|
|
48
|
+
delete event.geometryX
|
|
49
|
+
delete event.geometryY
|
|
50
|
+
delete event.usableX
|
|
51
|
+
delete event.usableY
|
|
52
|
+
|
|
53
|
+
const display = Globals.displays[displayIndex]
|
|
54
|
+
display.geometry.x = geometryX
|
|
55
|
+
display.geometry.y = geometryY
|
|
56
|
+
display.usable.x = usableX
|
|
57
|
+
display.usable.y = usableY
|
|
58
|
+
} break
|
|
96
59
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
: newDisplays[displayIndex]
|
|
60
|
+
// No default
|
|
61
|
+
}
|
|
100
62
|
|
|
101
|
-
videoModule.emit(
|
|
63
|
+
videoModule.emit(type, event)
|
|
102
64
|
} break
|
|
103
65
|
|
|
104
66
|
case 'window': {
|
|
@@ -176,7 +138,7 @@ const handleEvent = (event) => {
|
|
|
176
138
|
default: return
|
|
177
139
|
}
|
|
178
140
|
|
|
179
|
-
window.emit(
|
|
141
|
+
window.emit(type, event)
|
|
180
142
|
} break
|
|
181
143
|
|
|
182
144
|
case 'keyboard': {
|
|
@@ -189,7 +151,7 @@ const handleEvent = (event) => {
|
|
|
189
151
|
const { key } = event
|
|
190
152
|
event.key = mapping[key] ?? (key?.length === 1 ? key : null)
|
|
191
153
|
|
|
192
|
-
window.emit(
|
|
154
|
+
window.emit(type, event)
|
|
193
155
|
} break
|
|
194
156
|
|
|
195
157
|
case 'mouse':
|
|
@@ -201,7 +163,7 @@ const handleEvent = (event) => {
|
|
|
201
163
|
const window = Globals.windows.all.get(windowId)
|
|
202
164
|
if (!window) { return }
|
|
203
165
|
|
|
204
|
-
window.emit(
|
|
166
|
+
window.emit(type, event)
|
|
205
167
|
} break
|
|
206
168
|
|
|
207
169
|
case 'joystickDevice': {
|
|
@@ -226,33 +188,10 @@ const handleEvent = (event) => {
|
|
|
226
188
|
}
|
|
227
189
|
}
|
|
228
190
|
|
|
229
|
-
const { devices
|
|
191
|
+
const { devices } = event
|
|
230
192
|
delete event.devices
|
|
231
193
|
|
|
232
|
-
|
|
233
|
-
.filter(filterControllerDevice)))
|
|
234
|
-
|
|
235
|
-
for (const joystickDevice of joystickDevices) {
|
|
236
|
-
makeJoystickDevice(joystickDevice)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
for (const controllerDevice of controllerDevices) {
|
|
240
|
-
makeControllerDevice(controllerDevice)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
reconcileDevices(
|
|
244
|
-
joystickModule,
|
|
245
|
-
Globals.joystickDevices,
|
|
246
|
-
joystickDevices,
|
|
247
|
-
compareJoystickDevice,
|
|
248
|
-
)
|
|
249
|
-
|
|
250
|
-
reconcileDevices(
|
|
251
|
-
controllerModule,
|
|
252
|
-
Globals.controllerDevices,
|
|
253
|
-
controllerDevices,
|
|
254
|
-
compareControllerDevice,
|
|
255
|
-
)
|
|
194
|
+
reconcileJoystickAndControllerDevices(devices)
|
|
256
195
|
} break
|
|
257
196
|
|
|
258
197
|
case 'joystick': {
|
|
@@ -266,7 +205,7 @@ const handleEvent = (event) => {
|
|
|
266
205
|
case 'axisMotion': {
|
|
267
206
|
for (const joystickInstance of collection) {
|
|
268
207
|
joystickInstance._axes[event.axis] = event.value
|
|
269
|
-
joystickInstance.emit(
|
|
208
|
+
joystickInstance.emit(type, event)
|
|
270
209
|
}
|
|
271
210
|
} break
|
|
272
211
|
|
|
@@ -275,27 +214,27 @@ const handleEvent = (event) => {
|
|
|
275
214
|
const ball = joystickInstance._balls[event.ball]
|
|
276
215
|
ball.x = event.x
|
|
277
216
|
ball.y = event.y
|
|
278
|
-
joystickInstance.emit(
|
|
217
|
+
joystickInstance.emit(type, event)
|
|
279
218
|
}
|
|
280
219
|
} break
|
|
281
220
|
|
|
282
221
|
case 'buttonDown': {
|
|
283
222
|
for (const joystickInstance of collection) {
|
|
284
223
|
joystickInstance._buttons[event.button] = true
|
|
285
|
-
joystickInstance.emit(
|
|
224
|
+
joystickInstance.emit(type, event)
|
|
286
225
|
}
|
|
287
226
|
} break
|
|
288
227
|
case 'buttonUp': {
|
|
289
228
|
for (const joystickInstance of collection) {
|
|
290
229
|
joystickInstance._buttons[event.button] = false
|
|
291
|
-
joystickInstance.emit(
|
|
230
|
+
joystickInstance.emit(type, event)
|
|
292
231
|
}
|
|
293
232
|
} break
|
|
294
233
|
|
|
295
234
|
case 'hatMotion': {
|
|
296
235
|
for (const joystickInstance of collection) {
|
|
297
236
|
joystickInstance._hats[event.hat] = event.value
|
|
298
|
-
joystickInstance.emit(
|
|
237
|
+
joystickInstance.emit(type, event)
|
|
299
238
|
}
|
|
300
239
|
} break
|
|
301
240
|
|
|
@@ -314,21 +253,21 @@ const handleEvent = (event) => {
|
|
|
314
253
|
case 'axisMotion': {
|
|
315
254
|
for (const controllerInstance of collection.values()) {
|
|
316
255
|
controllerInstance._axes[event.axis] = event.value
|
|
317
|
-
controllerInstance.emit(
|
|
256
|
+
controllerInstance.emit(type, event)
|
|
318
257
|
}
|
|
319
258
|
} break
|
|
320
259
|
|
|
321
260
|
case 'buttonDown': {
|
|
322
261
|
for (const controllerInstance of collection.values()) {
|
|
323
262
|
controllerInstance._buttons[event.button] = true
|
|
324
|
-
controllerInstance.emit(
|
|
263
|
+
controllerInstance.emit(type, event)
|
|
325
264
|
}
|
|
326
265
|
} break
|
|
327
266
|
|
|
328
267
|
case 'buttonUp': {
|
|
329
268
|
for (const controllerInstance of collection.values()) {
|
|
330
269
|
controllerInstance._buttons[event.button] = false
|
|
331
|
-
controllerInstance.emit(
|
|
270
|
+
controllerInstance.emit(type, event)
|
|
332
271
|
}
|
|
333
272
|
} break
|
|
334
273
|
|
|
@@ -340,7 +279,7 @@ const handleEvent = (event) => {
|
|
|
340
279
|
for (const controllerInstance of collection.values()) {
|
|
341
280
|
Object.assign(controllerInstance._axes, axes)
|
|
342
281
|
Object.assign(controllerInstance._buttons, buttons)
|
|
343
|
-
controllerInstance.emit(
|
|
282
|
+
controllerInstance.emit(type, event)
|
|
344
283
|
}
|
|
345
284
|
} break
|
|
346
285
|
|
|
@@ -358,7 +297,7 @@ const handleEvent = (event) => {
|
|
|
358
297
|
switch (type) {
|
|
359
298
|
case 'update': {
|
|
360
299
|
for (const sensorInstance of collection.values()) {
|
|
361
|
-
sensorInstance.emit(
|
|
300
|
+
sensorInstance.emit(type, event)
|
|
362
301
|
}
|
|
363
302
|
} break
|
|
364
303
|
|
|
@@ -367,8 +306,8 @@ const handleEvent = (event) => {
|
|
|
367
306
|
} break
|
|
368
307
|
|
|
369
308
|
case 'audioDevice': {
|
|
370
|
-
const {
|
|
371
|
-
delete event.
|
|
309
|
+
const { audioDeviceType } = event
|
|
310
|
+
delete event.audioDeviceType
|
|
372
311
|
|
|
373
312
|
if (type === 'deviceRemove') {
|
|
374
313
|
const { audioId } = event
|
|
@@ -378,23 +317,14 @@ const handleEvent = (event) => {
|
|
|
378
317
|
if (audioInstance) { audioInstance.close() }
|
|
379
318
|
}
|
|
380
319
|
|
|
381
|
-
const { devices
|
|
320
|
+
const { devices } = event
|
|
382
321
|
delete event.devices
|
|
383
322
|
|
|
384
|
-
|
|
385
|
-
makeAudioDevice(audioDevice)
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
reconcileDevices(
|
|
389
|
-
audioModule,
|
|
390
|
-
Globals.audioDevices[isRecorder ? 'recording' : 'playback'],
|
|
391
|
-
audioDevices,
|
|
392
|
-
compareAudioDevice,
|
|
393
|
-
)
|
|
323
|
+
reconcileAudioDevices(devices, audioDeviceType)
|
|
394
324
|
} break
|
|
395
325
|
|
|
396
326
|
case 'clipboard': {
|
|
397
|
-
clipboardModule.emit(
|
|
327
|
+
clipboardModule.emit(type, event)
|
|
398
328
|
} break
|
|
399
329
|
|
|
400
330
|
// No default
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const Globals = require('../globals')
|
|
2
|
+
const { reconcileDevices } = require('./reconcile')
|
|
3
|
+
|
|
4
|
+
const { compare: compareAudioDevice } = require('../audio/device')
|
|
5
|
+
|
|
6
|
+
const reconcileAudioDevices = (audioDevices, audioDeviceType) => {
|
|
7
|
+
reconcileDevices(
|
|
8
|
+
require('../audio').audio,
|
|
9
|
+
Globals.audioDevices[audioDeviceType],
|
|
10
|
+
audioDevices,
|
|
11
|
+
compareAudioDevice,
|
|
12
|
+
)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = { reconcileAudioDevices }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const Globals = require('../globals')
|
|
2
|
+
const { reconcileDevices } = require('./reconcile')
|
|
3
|
+
|
|
4
|
+
const { compare: compareDisplays } = require('../video/display')
|
|
5
|
+
|
|
6
|
+
const reconcileDisplays = (displays) => {
|
|
7
|
+
reconcileDevices(
|
|
8
|
+
require('../video').video,
|
|
9
|
+
Globals.displays,
|
|
10
|
+
displays,
|
|
11
|
+
compareDisplays,
|
|
12
|
+
'display',
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { reconcileDisplays }
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const Globals = require('../globals')
|
|
2
|
+
const { reconcileDevices } = require('./reconcile')
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
make: makeJoystickDevice,
|
|
6
|
+
compare: compareJoystickDevice,
|
|
7
|
+
} = require('../joystick/device')
|
|
8
|
+
const {
|
|
9
|
+
make: makeControllerDevice,
|
|
10
|
+
compare: compareControllerDevice,
|
|
11
|
+
filter: filterControllerDevice,
|
|
12
|
+
} = require('../controller/device')
|
|
13
|
+
|
|
14
|
+
const reconcileJoystickAndControllerDevices = (devices) => {
|
|
15
|
+
const joystickDevices = devices
|
|
16
|
+
.map(makeJoystickDevice)
|
|
17
|
+
|
|
18
|
+
const controllerDevices = devices
|
|
19
|
+
.filter(filterControllerDevice)
|
|
20
|
+
.map(makeControllerDevice)
|
|
21
|
+
|
|
22
|
+
reconcileDevices(
|
|
23
|
+
require('../joystick').joystick,
|
|
24
|
+
Globals.joystickDevices,
|
|
25
|
+
joystickDevices,
|
|
26
|
+
compareJoystickDevice,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
reconcileDevices(
|
|
30
|
+
require('../controller').controller,
|
|
31
|
+
Globals.controllerDevices,
|
|
32
|
+
controllerDevices,
|
|
33
|
+
compareControllerDevice,
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = { reconcileJoystickAndControllerDevices }
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
|
|
2
|
+
const reconcileDevices = (
|
|
3
|
+
emitter,
|
|
4
|
+
mainList,
|
|
5
|
+
currList,
|
|
6
|
+
compare,
|
|
7
|
+
prefix = 'device',
|
|
8
|
+
) => {
|
|
9
|
+
const addEventType = `${prefix}Add`
|
|
10
|
+
const removeEventType = `${prefix}Remove`
|
|
11
|
+
|
|
12
|
+
currList.sort(compare)
|
|
13
|
+
|
|
14
|
+
let mainIndex = 0
|
|
15
|
+
let currIndex = 0
|
|
16
|
+
let mainDevice = mainList[mainIndex]
|
|
17
|
+
let currDevice = currList[currIndex]
|
|
18
|
+
while (mainIndex < mainList.length && currIndex < currList.length) {
|
|
19
|
+
const cmp = compare(mainDevice, currDevice)
|
|
20
|
+
if (cmp === 0) {
|
|
21
|
+
Object.assign(mainList[mainIndex], currList[currIndex])
|
|
22
|
+
mainDevice = mainList[++mainIndex]
|
|
23
|
+
currDevice = currList[++currIndex]
|
|
24
|
+
}
|
|
25
|
+
else if (cmp < 0) {
|
|
26
|
+
mainList.splice(mainIndex, 1)
|
|
27
|
+
const type = removeEventType
|
|
28
|
+
const event = { type, device: mainDevice }
|
|
29
|
+
emitter.emit(type, event)
|
|
30
|
+
mainDevice = mainList[mainIndex]
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
mainList.splice(mainIndex, 0, currDevice)
|
|
34
|
+
mainDevice = mainList[++mainIndex]
|
|
35
|
+
const type = addEventType
|
|
36
|
+
const event = { type, device: currDevice }
|
|
37
|
+
emitter.emit(type, event)
|
|
38
|
+
currDevice = currList[++currIndex]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (mainIndex < mainList.length) {
|
|
43
|
+
while (mainIndex < mainList.length) {
|
|
44
|
+
[ mainDevice ] = mainList.splice(mainIndex, 1)
|
|
45
|
+
const type = removeEventType
|
|
46
|
+
const event = { type, device: mainDevice }
|
|
47
|
+
emitter.emit(type, event)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
while (currIndex < currList.length) {
|
|
52
|
+
mainList.push(currDevice)
|
|
53
|
+
const type = addEventType
|
|
54
|
+
const event = { type, device: currDevice }
|
|
55
|
+
emitter.emit(type, event)
|
|
56
|
+
currDevice = currList[++currIndex]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { reconcileDevices }
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
|
|
2
2
|
const make = (device) => {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const {
|
|
4
|
+
isController,
|
|
5
|
+
controllerMapping,
|
|
6
|
+
controllerName,
|
|
7
|
+
controllerType,
|
|
8
|
+
...rest
|
|
9
|
+
} = device
|
|
10
|
+
return rest
|
|
5
11
|
}
|
|
6
12
|
|
|
7
|
-
const compare = (a, b) => a.
|
|
13
|
+
const compare = (a, b) => a._index - b._index
|
|
8
14
|
|
|
9
15
|
module.exports = {
|
|
10
16
|
make,
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
const Globals = require('../globals')
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
|
-
const { make: makeJoystickDevice } = require('./device')
|
|
4
3
|
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
5
4
|
const { JoystickInstance } = require('./joystick-instance')
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
|
|
7
|
+
const { make: makeJoystickDevice } = require('./device')
|
|
8
|
+
const {
|
|
9
|
+
make: makeControllerDevice,
|
|
10
|
+
filter: filterControllerDevice,
|
|
11
|
+
} = require('../controller/device')
|
|
12
|
+
|
|
13
|
+
const devices = Bindings.joystick_getDevices()
|
|
14
|
+
Globals.joystickDevices = devices
|
|
15
|
+
.map(makeJoystickDevice)
|
|
16
|
+
Globals.controllerDevices = devices
|
|
17
|
+
.filter(filterControllerDevice)
|
|
18
|
+
.map(makeControllerDevice)
|
|
11
19
|
|
|
12
20
|
|
|
13
21
|
const validEvents = [ 'deviceAdd', 'deviceRemove' ]
|
|
@@ -15,6 +15,8 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
15
15
|
constructor (device) {
|
|
16
16
|
super(validEvents)
|
|
17
17
|
|
|
18
|
+
if (!Globals.joystickDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
|
|
19
|
+
|
|
18
20
|
const result = Bindings.joystick_open(device._index)
|
|
19
21
|
|
|
20
22
|
this._firmwareVersion = result.firmwareVersion
|
|
@@ -11,6 +11,8 @@ class SensorInstance extends EventsViaPoll {
|
|
|
11
11
|
constructor (device) {
|
|
12
12
|
super(validEvents)
|
|
13
13
|
|
|
14
|
+
if (!Globals.sensorDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
|
|
15
|
+
|
|
14
16
|
Bindings.sensor_open(device._index)
|
|
15
17
|
|
|
16
18
|
this._device = device
|
package/src/native/audio.cpp
CHANGED
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
#include <sstream>
|
|
6
6
|
|
|
7
7
|
|
|
8
|
+
std::map<bool, std::string> audio::device_types;
|
|
9
|
+
|
|
10
|
+
|
|
8
11
|
Napi::Array
|
|
9
12
|
audio::_getDevices(Napi::Env &env, bool is_capture)
|
|
10
13
|
{
|
|
11
|
-
const char *error = SDL_GetError();
|
|
12
|
-
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
13
|
-
SDL_ClearError();
|
|
14
|
-
|
|
15
14
|
int num_devices = SDL_GetNumAudioDevices(is_capture ? 1 : 0);
|
|
16
15
|
if (num_devices == -1) {
|
|
17
|
-
|
|
16
|
+
// SDL_GetNumAudioDevices can return -1 even if there is no error
|
|
17
|
+
const char *error = SDL_GetError();
|
|
18
18
|
if (error != global::no_error) {
|
|
19
19
|
std::ostringstream message;
|
|
20
20
|
message << "SDL_GetNumAudioDevices(" << is_capture << ") error: " << error;
|
|
@@ -26,6 +26,8 @@ audio::_getDevices(Napi::Env &env, bool is_capture)
|
|
|
26
26
|
|
|
27
27
|
Napi::Array devices = Napi::Array::New(env, num_devices);
|
|
28
28
|
|
|
29
|
+
std::string device_type = audio::device_types[is_capture];
|
|
30
|
+
|
|
29
31
|
for (int i = 0; i < num_devices; i++) {
|
|
30
32
|
const char *name = SDL_GetAudioDeviceName(i, is_capture);
|
|
31
33
|
if (name == nullptr) {
|
|
@@ -36,8 +38,8 @@ audio::_getDevices(Napi::Env &env, bool is_capture)
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
Napi::Object device = Napi::Object::New(env);
|
|
39
|
-
device.Set("
|
|
40
|
-
device.Set("
|
|
41
|
+
device.Set("name", name);
|
|
42
|
+
device.Set("type", device_type);
|
|
41
43
|
|
|
42
44
|
devices.Set(i, device);
|
|
43
45
|
}
|
|
@@ -155,12 +157,8 @@ audio::dequeue (const Napi::CallbackInfo &info)
|
|
|
155
157
|
void *dst = info[1].As<Napi::Buffer<char>>().Data();
|
|
156
158
|
int size = info[2].As<Napi::Number>().Int32Value();
|
|
157
159
|
|
|
158
|
-
const char *error = SDL_GetError();
|
|
159
|
-
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
160
|
-
SDL_ClearError();
|
|
161
|
-
|
|
162
160
|
int num = SDL_DequeueAudio(audio_id, dst, size);
|
|
163
|
-
error = SDL_GetError();
|
|
161
|
+
const char *error = SDL_GetError();
|
|
164
162
|
if (error != global::no_error) {
|
|
165
163
|
std::ostringstream message;
|
|
166
164
|
message << "SDL_DequeueAudio(" << audio_id << ") error: " << error;
|
package/src/native/audio.h
CHANGED
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
#define _AUDIO_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include <map>
|
|
6
|
+
#include <string>
|
|
5
7
|
|
|
6
8
|
namespace audio {
|
|
7
9
|
|
|
10
|
+
extern std::map<bool, std::string> device_types;
|
|
11
|
+
|
|
8
12
|
Napi::Array _getDevices(Napi::Env &env, bool is_capture);
|
|
9
13
|
|
|
10
14
|
Napi::Value getDevices(const Napi::CallbackInfo &info);
|