@kmamal/sdl 1.0.0-4 → 1.0.0-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 +12 -6
- package/binding.gyp +3 -0
- package/package.json +6 -13
- package/scripts/util/common.js +2 -2
- package/src/javascript/audio/audio-playback-stream.js +9 -10
- package/src/javascript/audio/audio-recording-stream.js +9 -8
- package/src/javascript/audio/audio-stream.js +100 -44
- package/src/javascript/audio/conversion.js +41 -0
- package/src/javascript/audio/devices.js +11 -0
- package/src/javascript/audio/events.js +65 -0
- package/src/javascript/audio/format-helpers.js +8 -4
- package/src/javascript/audio/index.js +25 -20
- package/src/javascript/audio/prevent-exit.js +12 -9
- package/src/javascript/audio/validate.js +9 -0
- package/src/javascript/cleanup.js +9 -6
- package/src/javascript/clipboard/index.js +8 -3
- package/src/javascript/device-instance.js +61 -0
- package/src/javascript/events/events-via-poll.js +33 -27
- package/src/javascript/events/index.js +76 -482
- package/src/javascript/events/try.js +22 -0
- package/src/javascript/gamepad/{device.js → devices.js} +4 -5
- package/src/javascript/gamepad/events.js +66 -0
- package/src/javascript/gamepad/gamepad-instance.js +12 -160
- package/src/javascript/gamepad/index.js +15 -16
- package/src/javascript/globals.js +10 -1
- package/src/javascript/index.js +2 -5
- package/src/javascript/joystick/devices.js +43 -0
- package/src/javascript/joystick/events.js +129 -0
- package/src/javascript/joystick/index.js +9 -18
- package/src/javascript/joystick/joystick-device-instance.js +175 -0
- package/src/javascript/joystick/joystick-instance.js +12 -158
- package/src/javascript/keyboard/index.js +20 -10
- package/src/javascript/keyboard/key-mapping.js +46 -27
- package/src/javascript/mouse/index.js +36 -29
- package/src/javascript/sensor/events.js +21 -0
- package/src/javascript/sensor/index.js +13 -2
- package/src/javascript/sensor/sensor-instance.js +7 -47
- package/src/javascript/subsystems.js +56 -0
- package/src/javascript/touch/index.js +9 -4
- package/src/javascript/validate.js +114 -0
- package/src/javascript/video/conversion.js +63 -0
- package/src/javascript/video/events.js +179 -0
- package/src/javascript/video/format-helpers.js +10 -8
- package/src/javascript/video/index.js +28 -14
- package/src/javascript/video/validate.js +45 -0
- package/src/javascript/video/window.js +144 -161
- package/src/native/audio.cpp +457 -151
- package/src/native/audio.h +12 -6
- package/src/native/clipboard.cpp +8 -37
- package/src/native/enums.cpp +2 -0
- package/src/native/events.cpp +416 -354
- package/src/native/events.h +67 -65
- package/src/native/gamepad.cpp +86 -103
- package/src/native/gamepad.h +7 -8
- package/src/native/global.cpp +190 -85
- package/src/native/global.h +48 -2
- package/src/native/joystick.cpp +180 -274
- package/src/native/joystick.h +12 -6
- package/src/native/keyboard.cpp +60 -22
- package/src/native/keyboard.h +2 -1
- package/src/native/module.cpp +20 -3
- package/src/native/mouse.cpp +27 -58
- package/src/native/packed.cpp +106 -0
- package/src/native/packed.h +91 -0
- package/src/native/power.cpp +7 -1
- package/src/native/power.h +2 -2
- package/src/native/sensor.cpp +61 -69
- package/src/native/sensor.h +3 -3
- package/src/native/testing.cpp +131 -0
- package/src/native/testing.h +16 -0
- package/src/native/threads.cpp +300 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +32 -43
- package/src/native/touch.h +2 -1
- package/src/native/video.cpp +145 -95
- package/src/native/video.h +6 -3
- package/src/native/window.cpp +155 -218
- package/src/native/window.h +21 -20
- package/src/types/index.d.ts +141 -69
- package/src/javascript/audio/device.js +0 -37
- package/src/javascript/joystick/device.js +0 -13
- package/src/javascript/joystick/shared.js +0 -90
- package/src/javascript/worker.js +0 -88
- package/src/types/worker.d.ts +0 -71
|
@@ -1,525 +1,119 @@
|
|
|
1
|
+
const { isMainThread } = require('node:worker_threads')
|
|
1
2
|
const Globals = require('../globals')
|
|
2
3
|
const Bindings = require('../bindings')
|
|
3
|
-
const { video: videoModule } = require('../video')
|
|
4
4
|
const { keyboard: keyboardModule } = require('../keyboard')
|
|
5
|
-
const { mapping, isCharacter } = require('../keyboard/key-mapping')
|
|
6
|
-
const { joystick: joystickModule } = require('../joystick')
|
|
7
|
-
const { make: makeJoystickDevice } = require('../joystick/device')
|
|
8
|
-
const { gamepad: gamepadModule } = require('../gamepad')
|
|
9
|
-
const { make: makeGamepadDevice, filter: filterGamepadDevice } = require('../gamepad/device')
|
|
10
|
-
const { audio: audioModule } = require('../audio')
|
|
11
|
-
const { make: makeAudioDevice, update: updateAudioDevice } = require('../audio/device')
|
|
12
5
|
const { clipboard: clipboardModule } = require('../clipboard')
|
|
13
|
-
const {
|
|
6
|
+
const { tryEmit, tryCall } = require('./try')
|
|
7
|
+
const { invalidateReverseMapping } = require('../keyboard/key-mapping')
|
|
8
|
+
const {
|
|
9
|
+
handleDisplayEvent,
|
|
10
|
+
handleWindowEvent,
|
|
11
|
+
} = require('../video/events')
|
|
12
|
+
const {
|
|
13
|
+
handleEvent: handleJoystickEvent,
|
|
14
|
+
handleJoystickDeviceEvent,
|
|
15
|
+
handleGamepadDeviceEvent,
|
|
16
|
+
} = require('../joystick/events')
|
|
17
|
+
const { handleEvent: handleGamepadEvent } = require('../gamepad/events')
|
|
18
|
+
const { handleEvent: handleSensorEvent } = require('../sensor/events')
|
|
19
|
+
const { handleEvent: handleAudioDeviceEvent } = require('../audio/events')
|
|
20
|
+
const { handleDevicesChange: handleJoystickDevicesChange } = require('../joystick/devices')
|
|
21
|
+
const Timers = require('node:timers')
|
|
22
|
+
|
|
23
|
+
const handleAppEvent = ({ type }) => {
|
|
24
|
+
if (type !== 'quit') { return }
|
|
25
|
+
|
|
26
|
+
for (const window of Globals.windows.all.values()) {
|
|
27
|
+
tryCall(() => { window.destroyGently() })
|
|
28
|
+
}
|
|
29
|
+
}
|
|
14
30
|
|
|
31
|
+
const handleChangeEvent = ({ type }) => {
|
|
32
|
+
if (type !== 'joystickDevices') { return }
|
|
15
33
|
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
try { emitter.emit(type, event) }
|
|
19
|
-
catch (error) { emitter.emit('error', error) }
|
|
20
|
-
}
|
|
21
|
-
catch (error) { process.nextTick(() => { throw error }) }
|
|
34
|
+
handleJoystickDevicesChange()
|
|
22
35
|
}
|
|
23
36
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
const emitTo = (emitter) => (event) => { tryEmit(emitter, event.type, event) }
|
|
38
|
+
|
|
39
|
+
const handleKeyboardEvent = (event) => {
|
|
40
|
+
if (event.type === 'keymapChange') { invalidateReverseMapping() }
|
|
41
|
+
tryEmit(keyboardModule, event.type, event)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const handlers = {
|
|
45
|
+
app: handleAppEvent,
|
|
46
|
+
change: handleChangeEvent,
|
|
47
|
+
video: handleDisplayEvent,
|
|
48
|
+
window: handleWindowEvent,
|
|
49
|
+
keyboard: handleKeyboardEvent,
|
|
50
|
+
joystickDevice: handleJoystickDeviceEvent,
|
|
51
|
+
joystick: handleJoystickEvent,
|
|
52
|
+
gamepadDevice: handleGamepadDeviceEvent,
|
|
53
|
+
gamepad: handleGamepadEvent,
|
|
54
|
+
sensor: handleSensorEvent,
|
|
55
|
+
audioDevice: handleAudioDeviceEvent,
|
|
56
|
+
clipboard: emitTo(clipboardModule),
|
|
27
57
|
}
|
|
28
58
|
|
|
29
59
|
const handleEvent = (event) => {
|
|
30
|
-
const { target, targetId
|
|
60
|
+
const { target, targetId } = event
|
|
31
61
|
delete event.target
|
|
32
62
|
delete event.targetId
|
|
33
63
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (type !== 'quit') { return }
|
|
37
|
-
|
|
38
|
-
for (const window of Globals.windows.all.values()) {
|
|
39
|
-
tryCall(() => { window.destroyGently() })
|
|
40
|
-
}
|
|
41
|
-
} break
|
|
42
|
-
|
|
43
|
-
case 'video': {
|
|
44
|
-
switch (type) {
|
|
45
|
-
case 'displayAdd': {
|
|
46
|
-
const { display } = event
|
|
47
|
-
delete event.display
|
|
48
|
-
|
|
49
|
-
if (Globals.displays.some((a) => a.id === display.id)) { return }
|
|
50
|
-
Globals.displays.push(display)
|
|
51
|
-
event.device = display
|
|
52
|
-
} break
|
|
53
|
-
|
|
54
|
-
case 'displayRemove': {
|
|
55
|
-
const { displayId } = event
|
|
56
|
-
delete event.displayId
|
|
57
|
-
|
|
58
|
-
const index = Globals.displays.findIndex((a) => a.id === displayId)
|
|
59
|
-
if (index === -1) { return }
|
|
60
|
-
event.device = Globals.displays[index]
|
|
61
|
-
Globals.displays.splice(index, 1)
|
|
62
|
-
} break
|
|
63
|
-
|
|
64
|
-
case 'displayOrient': {
|
|
65
|
-
const { displayId } = event
|
|
66
|
-
delete event.displayId
|
|
67
|
-
|
|
68
|
-
const display = Globals.displays.find((a) => a.id === displayId)
|
|
69
|
-
if (!display) { return }
|
|
70
|
-
|
|
71
|
-
display.orientation = event.orientation
|
|
72
|
-
event.device = display
|
|
73
|
-
} break
|
|
74
|
-
|
|
75
|
-
case 'displayMove': {
|
|
76
|
-
const { displayId, geometryX, geometryY, usableX, usableY } = event
|
|
77
|
-
delete event.displayId
|
|
78
|
-
delete event.geometryX
|
|
79
|
-
delete event.geometryY
|
|
80
|
-
delete event.usableX
|
|
81
|
-
delete event.usableY
|
|
82
|
-
|
|
83
|
-
const display = Globals.displays.find((a) => a.id === displayId)
|
|
84
|
-
if (!display) { return }
|
|
85
|
-
|
|
86
|
-
display.geometry.x = geometryX
|
|
87
|
-
display.geometry.y = geometryY
|
|
88
|
-
display.usable.x = usableX
|
|
89
|
-
display.usable.y = usableY
|
|
90
|
-
event.device = display
|
|
91
|
-
} break
|
|
92
|
-
|
|
93
|
-
case 'displayScaleChange':
|
|
94
|
-
case 'displayModeChange':
|
|
95
|
-
case 'displayUsableChange': {
|
|
96
|
-
const { displayId, display: fresh } = event
|
|
97
|
-
delete event.displayId
|
|
98
|
-
delete event.display
|
|
99
|
-
|
|
100
|
-
const display = Globals.displays.find((a) => a.id === displayId)
|
|
101
|
-
if (!display) { return }
|
|
102
|
-
|
|
103
|
-
Object.assign(display, fresh)
|
|
104
|
-
event.device = display
|
|
105
|
-
if (type === 'displayScaleChange') { event.scale = display.scale }
|
|
106
|
-
if (type === 'displayModeChange') {
|
|
107
|
-
event.format = display.format
|
|
108
|
-
event.frequency = display.frequency
|
|
109
|
-
event.geometry = display.geometry
|
|
110
|
-
}
|
|
111
|
-
} break
|
|
112
|
-
|
|
113
|
-
// No default
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
tryEmit(videoModule, type, event)
|
|
117
|
-
} break
|
|
118
|
-
|
|
119
|
-
case 'window': {
|
|
120
|
-
const window = Globals.windows.all.get(targetId)
|
|
121
|
-
if (!window) { return }
|
|
122
|
-
|
|
123
|
-
switch (type) {
|
|
124
|
-
case 'move': {
|
|
125
|
-
window._x = event.x
|
|
126
|
-
window._y = event.y
|
|
127
|
-
} break
|
|
128
|
-
case 'resize': {
|
|
129
|
-
if (
|
|
130
|
-
window._width === event.width
|
|
131
|
-
&& window._height === event.height
|
|
132
|
-
&& window._pixelWidth === event.pixelWidth
|
|
133
|
-
&& window._pixelHeight === event.pixelHeight
|
|
134
|
-
) { return }
|
|
135
|
-
window._width = event.width
|
|
136
|
-
window._height = event.height
|
|
137
|
-
window._pixelWidth = event.pixelWidth
|
|
138
|
-
window._pixelHeight = event.pixelHeight
|
|
139
|
-
} break
|
|
140
|
-
case 'displayChange': {
|
|
141
|
-
window._displayId = event.displayId
|
|
142
|
-
delete event.displayId
|
|
143
|
-
event.display = window.display
|
|
144
|
-
} break
|
|
145
|
-
|
|
146
|
-
case 'show': {
|
|
147
|
-
window._visible = true
|
|
148
|
-
} break
|
|
149
|
-
case 'hide': {
|
|
150
|
-
window._visible = false
|
|
151
|
-
} break
|
|
152
|
-
case 'expose': {
|
|
153
|
-
// Nothing
|
|
154
|
-
} break
|
|
155
|
-
|
|
156
|
-
case 'minimize': {
|
|
157
|
-
window._minimized = true
|
|
158
|
-
window._maximized = false
|
|
159
|
-
} break
|
|
160
|
-
case 'maximize': {
|
|
161
|
-
window._minimized = false
|
|
162
|
-
window._maximized = true
|
|
163
|
-
} break
|
|
164
|
-
case 'restore': {
|
|
165
|
-
window._minimized = false
|
|
166
|
-
window._maximized = false
|
|
167
|
-
} break
|
|
168
|
-
|
|
169
|
-
case 'enterFullscreen': {
|
|
170
|
-
window._fullscreen = true
|
|
171
|
-
return
|
|
172
|
-
}
|
|
173
|
-
case 'leaveFullscreen': {
|
|
174
|
-
window._fullscreen = false
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
case 'focus': {
|
|
179
|
-
Globals.windows.focused = window
|
|
180
|
-
} break
|
|
181
|
-
case 'blur': {
|
|
182
|
-
if (Globals.windows.focused === window) { Globals.windows.focused = null }
|
|
183
|
-
} break
|
|
184
|
-
|
|
185
|
-
case 'hover': {
|
|
186
|
-
Globals.windows.hovered = window
|
|
187
|
-
} break
|
|
188
|
-
case 'leave': {
|
|
189
|
-
if (Globals.windows.hovered === window) { Globals.windows.hovered = null }
|
|
190
|
-
} break
|
|
191
|
-
|
|
192
|
-
case 'close': {
|
|
193
|
-
tryCall(() => { window.destroyGently() })
|
|
194
|
-
return
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
case 'renderDeviceLost': {
|
|
198
|
-
tryCall(() => {
|
|
199
|
-
const result = Bindings.window_setAcceleratedAndVsync(window._id, window._accelerated, window._vsync)
|
|
200
|
-
window._accelerated = result.accelerated
|
|
201
|
-
window._vsync = result.vsync
|
|
202
|
-
})
|
|
203
|
-
return
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
case 'keyDown':
|
|
207
|
-
case 'keyUp': {
|
|
208
|
-
const { key } = event
|
|
209
|
-
event.key = mapping[key] ?? (isCharacter(key) ? key : null)
|
|
210
|
-
} break
|
|
211
|
-
|
|
212
|
-
case 'fingerDown':
|
|
213
|
-
case 'fingerUp':
|
|
214
|
-
case 'fingerMove':
|
|
215
|
-
case 'fingerCancel': {
|
|
216
|
-
const { touchId } = event
|
|
217
|
-
delete event.touchId
|
|
218
|
-
|
|
219
|
-
// Events synthesized from the mouse or pen have null touch device
|
|
220
|
-
let device = null
|
|
221
|
-
if (!event.mouse && !event.pen) {
|
|
222
|
-
device = Globals.touchDevices.find(({ id }) => id === touchId)
|
|
223
|
-
if (!device) {
|
|
224
|
-
try { refreshTouchDevices() }
|
|
225
|
-
catch (_) { return }
|
|
226
|
-
device = Globals.touchDevices.find(({ id }) => id === touchId)
|
|
227
|
-
if (!device) { return }
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
event.device = device
|
|
232
|
-
} break
|
|
233
|
-
|
|
234
|
-
// No default
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
tryEmit(window, type, event)
|
|
238
|
-
} break
|
|
239
|
-
|
|
240
|
-
case 'keyboard': {
|
|
241
|
-
tryEmit(keyboardModule, type, event)
|
|
242
|
-
} break
|
|
243
|
-
|
|
244
|
-
case 'joystick': {
|
|
245
|
-
const collection = Globals.joystickInstances.byId.get(targetId)
|
|
246
|
-
|
|
247
|
-
// Battery updates arrive as joystick events but also concern gamepads
|
|
248
|
-
if (type === 'powerUpdate') {
|
|
249
|
-
if (collection) {
|
|
250
|
-
for (const joystickInstance of collection) {
|
|
251
|
-
joystickInstance._power = event.power
|
|
252
|
-
tryEmit(joystickInstance, type, event)
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const otherCollection = Globals.gamepadInstances.byId.get(targetId)
|
|
257
|
-
if (!otherCollection) { return }
|
|
258
|
-
|
|
259
|
-
for (const gamepadInstance of otherCollection) {
|
|
260
|
-
gamepadInstance._power = event.power
|
|
261
|
-
tryEmit(gamepadInstance, type, event)
|
|
262
|
-
}
|
|
263
|
-
return
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (!collection) { return }
|
|
267
|
-
|
|
268
|
-
switch (type) {
|
|
269
|
-
case 'axisMotion': {
|
|
270
|
-
for (const joystickInstance of collection) {
|
|
271
|
-
joystickInstance._axes[event.axis] = event.value
|
|
272
|
-
tryEmit(joystickInstance, type, event)
|
|
273
|
-
}
|
|
274
|
-
} break
|
|
275
|
-
|
|
276
|
-
case 'ballMotion': {
|
|
277
|
-
for (const joystickInstance of collection) {
|
|
278
|
-
const ball = joystickInstance._balls[event.ball]
|
|
279
|
-
ball.x += event.dx
|
|
280
|
-
ball.y += event.dy
|
|
281
|
-
// Each instance accumulates its own x and y, so each gets its own copy of the event
|
|
282
|
-
tryEmit(joystickInstance, type, { ...event, x: ball.x, y: ball.y })
|
|
283
|
-
}
|
|
284
|
-
} break
|
|
285
|
-
|
|
286
|
-
case 'buttonDown': {
|
|
287
|
-
for (const joystickInstance of collection) {
|
|
288
|
-
joystickInstance._buttons[event.button] = true
|
|
289
|
-
tryEmit(joystickInstance, type, event)
|
|
290
|
-
}
|
|
291
|
-
} break
|
|
292
|
-
case 'buttonUp': {
|
|
293
|
-
for (const joystickInstance of collection) {
|
|
294
|
-
joystickInstance._buttons[event.button] = false
|
|
295
|
-
tryEmit(joystickInstance, type, event)
|
|
296
|
-
}
|
|
297
|
-
} break
|
|
298
|
-
|
|
299
|
-
case 'hatMotion': {
|
|
300
|
-
for (const joystickInstance of collection) {
|
|
301
|
-
joystickInstance._hats[event.hat] = event.value
|
|
302
|
-
tryEmit(joystickInstance, type, event)
|
|
303
|
-
}
|
|
304
|
-
} break
|
|
305
|
-
|
|
306
|
-
// No default
|
|
307
|
-
}
|
|
308
|
-
} break
|
|
309
|
-
|
|
310
|
-
case 'joystickDevice':
|
|
311
|
-
case 'gamepadDevice': {
|
|
312
|
-
const isGamepad = target === 'gamepadDevice'
|
|
313
|
-
const emitter = isGamepad ? gamepadModule : joystickModule
|
|
314
|
-
const list = isGamepad ? Globals.gamepadDevices : Globals.joystickDevices
|
|
315
|
-
|
|
316
|
-
switch (type) {
|
|
317
|
-
case 'deviceAdd': {
|
|
318
|
-
const { device } = event
|
|
319
|
-
delete event.device
|
|
320
|
-
|
|
321
|
-
if (isGamepad && !filterGamepadDevice(device)) { return }
|
|
322
|
-
if (list.some((a) => a.id === device.id)) { return }
|
|
323
|
-
|
|
324
|
-
const made = isGamepad ? makeGamepadDevice(device) : makeJoystickDevice(device)
|
|
325
|
-
list.push(made)
|
|
326
|
-
event.device = made
|
|
327
|
-
} break
|
|
328
|
-
|
|
329
|
-
case 'deviceRemove': {
|
|
330
|
-
const { deviceId } = event
|
|
331
|
-
delete event.deviceId
|
|
332
|
-
|
|
333
|
-
const instances = isGamepad ? Globals.gamepadInstances : Globals.joystickInstances
|
|
334
|
-
const collection = instances.byId.get(deviceId)
|
|
335
|
-
if (collection) {
|
|
336
|
-
for (const instance of collection.values()) {
|
|
337
|
-
tryCall(() => { instance.close() })
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
const index = list.findIndex((a) => a.id === deviceId)
|
|
342
|
-
if (index === -1) { return }
|
|
343
|
-
event.device = list[index]
|
|
344
|
-
list.splice(index, 1)
|
|
345
|
-
} break
|
|
346
|
-
|
|
347
|
-
// No default
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
tryEmit(emitter, type, event)
|
|
351
|
-
} break
|
|
352
|
-
|
|
353
|
-
case 'gamepad': {
|
|
354
|
-
const collection = Globals.gamepadInstances.byId.get(targetId)
|
|
355
|
-
if (!collection) { return }
|
|
356
|
-
|
|
357
|
-
switch (type) {
|
|
358
|
-
case 'axisMotion': {
|
|
359
|
-
for (const gamepadInstance of collection) {
|
|
360
|
-
gamepadInstance._axes[event.axis] = event.value
|
|
361
|
-
tryEmit(gamepadInstance, type, event)
|
|
362
|
-
}
|
|
363
|
-
} break
|
|
364
|
-
|
|
365
|
-
case 'buttonDown': {
|
|
366
|
-
for (const gamepadInstance of collection) {
|
|
367
|
-
gamepadInstance._buttons[event.button] = true
|
|
368
|
-
tryEmit(gamepadInstance, type, event)
|
|
369
|
-
}
|
|
370
|
-
} break
|
|
371
|
-
|
|
372
|
-
case 'buttonUp': {
|
|
373
|
-
for (const gamepadInstance of collection) {
|
|
374
|
-
gamepadInstance._buttons[event.button] = false
|
|
375
|
-
tryEmit(gamepadInstance, type, event)
|
|
376
|
-
}
|
|
377
|
-
} break
|
|
378
|
-
|
|
379
|
-
case 'steamHandleUpdate': {
|
|
380
|
-
for (const gamepadInstance of collection) {
|
|
381
|
-
gamepadInstance._steamHandle = event.steamHandle
|
|
382
|
-
tryEmit(gamepadInstance, type, event)
|
|
383
|
-
}
|
|
384
|
-
} break
|
|
385
|
-
|
|
386
|
-
case 'remap': {
|
|
387
|
-
const { device, axes, buttons, buttonLabels } = event
|
|
388
|
-
delete event.device
|
|
389
|
-
delete event.axes
|
|
390
|
-
delete event.buttons
|
|
391
|
-
delete event.buttonLabels
|
|
392
|
-
|
|
393
|
-
const gamepadDevice = Globals.gamepadDevices.find(({ id }) => id === targetId)
|
|
394
|
-
if (gamepadDevice) { Object.assign(gamepadDevice, makeGamepadDevice(device)) }
|
|
395
|
-
|
|
396
|
-
for (const gamepadInstance of collection) {
|
|
397
|
-
Object.assign(gamepadInstance._axes, axes)
|
|
398
|
-
Object.assign(gamepadInstance._buttons, buttons)
|
|
399
|
-
Object.assign(gamepadInstance._buttonLabels, buttonLabels)
|
|
400
|
-
tryEmit(gamepadInstance, type, event)
|
|
401
|
-
}
|
|
402
|
-
} break
|
|
403
|
-
|
|
404
|
-
// No default
|
|
405
|
-
}
|
|
406
|
-
} break
|
|
407
|
-
|
|
408
|
-
case 'sensor': {
|
|
409
|
-
const collection = Globals.sensorInstances.byId.get(targetId)
|
|
410
|
-
if (!collection) { return }
|
|
411
|
-
|
|
412
|
-
switch (type) {
|
|
413
|
-
case 'update': {
|
|
414
|
-
for (const sensorInstance of collection) {
|
|
415
|
-
tryEmit(sensorInstance, type, event)
|
|
416
|
-
}
|
|
417
|
-
} break
|
|
418
|
-
|
|
419
|
-
// No default
|
|
420
|
-
}
|
|
421
|
-
} break
|
|
422
|
-
|
|
423
|
-
case 'audioDevice': {
|
|
424
|
-
const { audioDeviceType } = event
|
|
425
|
-
delete event.audioDeviceType
|
|
426
|
-
|
|
427
|
-
const list = Globals.audioDevices[audioDeviceType]
|
|
428
|
-
const emitter = audioModule[audioDeviceType]
|
|
429
|
-
|
|
430
|
-
switch (type) {
|
|
431
|
-
case 'deviceAdd': {
|
|
432
|
-
const { device } = event
|
|
433
|
-
delete event.device
|
|
434
|
-
|
|
435
|
-
if (list.some((a) => a.id === device.id)) { return }
|
|
436
|
-
const made = makeAudioDevice(device)
|
|
437
|
-
list.push(made)
|
|
438
|
-
event.device = made
|
|
439
|
-
updateAudioDevice(list[0])
|
|
440
|
-
} break
|
|
441
|
-
|
|
442
|
-
case 'deviceRemove': {
|
|
443
|
-
const { deviceId } = event
|
|
444
|
-
delete event.deviceId
|
|
445
|
-
|
|
446
|
-
const index = list.findIndex((a) => a.id === deviceId)
|
|
447
|
-
if (index === -1) { return }
|
|
448
|
-
const device = list[index]
|
|
449
|
-
list.splice(index, 1)
|
|
450
|
-
|
|
451
|
-
for (const stream of [ ...Globals.audioStreams.values() ]) {
|
|
452
|
-
if (stream.device === device) {
|
|
453
|
-
tryCall(() => { stream.close() })
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
updateAudioDevice(list[0])
|
|
457
|
-
|
|
458
|
-
event.device = device
|
|
459
|
-
} break
|
|
460
|
-
|
|
461
|
-
case 'deviceFormatChange': {
|
|
462
|
-
const { deviceId } = event
|
|
463
|
-
const device = Globals.audioStreams.get(deviceId)?.device
|
|
464
|
-
?? list.find((a) => a.id === deviceId)
|
|
465
|
-
if (device) { updateAudioDevice(device) }
|
|
466
|
-
} return
|
|
64
|
+
handlers[target]?.(event, targetId)
|
|
65
|
+
}
|
|
467
66
|
|
|
468
|
-
// No default
|
|
469
|
-
}
|
|
470
67
|
|
|
471
|
-
|
|
472
|
-
} break
|
|
68
|
+
let polling = false
|
|
473
69
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
} break
|
|
70
|
+
let pollInterval = null
|
|
71
|
+
let pollingFast = null
|
|
477
72
|
|
|
478
|
-
|
|
479
|
-
}
|
|
73
|
+
const setPollingFast = (fast) => {
|
|
74
|
+
if (!isMainThread || fast === pollingFast) { return }
|
|
75
|
+
pollingFast = fast
|
|
76
|
+
Timers.clearInterval(pollInterval)
|
|
77
|
+
pollInterval = Timers.setInterval(poll, fast ? 0 : 1e3)
|
|
78
|
+
pollInterval.unref()
|
|
480
79
|
}
|
|
481
80
|
|
|
482
|
-
|
|
483
|
-
let polling = false
|
|
484
|
-
|
|
485
81
|
const poll = () => {
|
|
486
82
|
if (polling) { return }
|
|
487
83
|
polling = true
|
|
488
|
-
try {
|
|
84
|
+
try {
|
|
85
|
+
setPollingFast(Bindings.events_poll(handleEvent))
|
|
86
|
+
}
|
|
489
87
|
finally { polling = false }
|
|
490
88
|
}
|
|
491
89
|
|
|
492
90
|
const withoutPolling = (fn) => {
|
|
493
91
|
const prevValue = polling
|
|
92
|
+
polling = true
|
|
494
93
|
try { fn() }
|
|
495
94
|
finally { polling = prevValue }
|
|
496
95
|
}
|
|
497
96
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const switchToPollingFast = () => {
|
|
501
|
-
clearInterval(pollInterval)
|
|
502
|
-
pollInterval = setInterval(poll, 0)
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
const switchToPollingSlow = () => {
|
|
506
|
-
clearInterval(pollInterval)
|
|
507
|
-
pollInterval = setInterval(poll, 1e3)
|
|
508
|
-
pollInterval.unref()
|
|
509
|
-
}
|
|
97
|
+
const setDemand = (demand) => { setPollingFast(Bindings.events_setDemand(demand)) }
|
|
510
98
|
|
|
511
99
|
const stopPolling = () => {
|
|
512
|
-
clearInterval(pollInterval)
|
|
100
|
+
Timers.clearInterval(pollInterval)
|
|
513
101
|
pollInterval = null
|
|
514
102
|
}
|
|
515
103
|
|
|
104
|
+
const broadcastChange = (name) => { Bindings.events_broadcastChange(name) }
|
|
105
|
+
|
|
516
106
|
Globals.events = {
|
|
517
107
|
poll,
|
|
518
108
|
withoutPolling,
|
|
519
|
-
|
|
520
|
-
switchToPollingSlow,
|
|
109
|
+
setDemand,
|
|
521
110
|
stopPolling,
|
|
111
|
+
broadcastChange,
|
|
522
112
|
}
|
|
523
113
|
|
|
524
|
-
|
|
525
|
-
|
|
114
|
+
Bindings.events_register(poll, isMainThread)
|
|
115
|
+
|
|
116
|
+
if (isMainThread) {
|
|
117
|
+
setPollingFast(false)
|
|
118
|
+
poll()
|
|
119
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const rethrow = (error) => {
|
|
2
|
+
process.nextTick(() => { throw error })
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
const tryEmit = (emitter, type, event) => {
|
|
6
|
+
try { emitter.emit(type, event) }
|
|
7
|
+
catch (error) {
|
|
8
|
+
if (type === 'error') {
|
|
9
|
+
rethrow(error)
|
|
10
|
+
return
|
|
11
|
+
}
|
|
12
|
+
try { emitter.emit('error', error) }
|
|
13
|
+
catch (error2) { rethrow(error2) }
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const tryCall = (fn) => {
|
|
18
|
+
try { fn() }
|
|
19
|
+
catch (error) { rethrow(error) }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { tryEmit, tryCall }
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const make = (device) => {
|
|
1
|
+
const makeDevice = (device) => {
|
|
3
2
|
const {
|
|
4
3
|
isGamepad,
|
|
5
4
|
gamepadMapping,
|
|
@@ -15,9 +14,9 @@ const make = (device) => {
|
|
|
15
14
|
}
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
const
|
|
17
|
+
const isGamepadDevice = (device) => device.isGamepad
|
|
19
18
|
|
|
20
19
|
module.exports = {
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
makeDevice,
|
|
21
|
+
isGamepadDevice,
|
|
23
22
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const { isMainThread } = require('node:worker_threads')
|
|
2
|
+
const Globals = require('../globals')
|
|
3
|
+
const { makeDevice: makeGamepadDevice } = require('./devices')
|
|
4
|
+
const { tryEmit } = require('../events/try')
|
|
5
|
+
|
|
6
|
+
const handleEvent = (event, targetId) => {
|
|
7
|
+
const { type } = event
|
|
8
|
+
|
|
9
|
+
if (type === 'remap') {
|
|
10
|
+
const gamepadDevice = Globals.gamepadDevices.find(({ id }) => id === targetId)
|
|
11
|
+
if (gamepadDevice) { Object.assign(gamepadDevice, makeGamepadDevice(event.device)) }
|
|
12
|
+
if (isMainThread) { Globals.events.broadcastChange('joystickDevices') }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const collection = Globals.gamepadInstances.byId.get(targetId)
|
|
16
|
+
if (!collection) { return }
|
|
17
|
+
|
|
18
|
+
switch (type) {
|
|
19
|
+
case 'axisMotion': {
|
|
20
|
+
for (const gamepadInstance of collection) {
|
|
21
|
+
gamepadInstance._axes[event.axis] = event.value
|
|
22
|
+
tryEmit(gamepadInstance, type, event)
|
|
23
|
+
}
|
|
24
|
+
} break
|
|
25
|
+
|
|
26
|
+
case 'buttonDown': {
|
|
27
|
+
for (const gamepadInstance of collection) {
|
|
28
|
+
gamepadInstance._buttons[event.button] = true
|
|
29
|
+
tryEmit(gamepadInstance, type, event)
|
|
30
|
+
}
|
|
31
|
+
} break
|
|
32
|
+
|
|
33
|
+
case 'buttonUp': {
|
|
34
|
+
for (const gamepadInstance of collection) {
|
|
35
|
+
gamepadInstance._buttons[event.button] = false
|
|
36
|
+
tryEmit(gamepadInstance, type, event)
|
|
37
|
+
}
|
|
38
|
+
} break
|
|
39
|
+
|
|
40
|
+
case 'steamHandleUpdate': {
|
|
41
|
+
for (const gamepadInstance of collection) {
|
|
42
|
+
gamepadInstance._steamHandle = event.steamHandle
|
|
43
|
+
tryEmit(gamepadInstance, type, event)
|
|
44
|
+
}
|
|
45
|
+
} break
|
|
46
|
+
|
|
47
|
+
case 'remap': {
|
|
48
|
+
const { axes, buttons, buttonLabels } = event
|
|
49
|
+
delete event.device
|
|
50
|
+
delete event.axes
|
|
51
|
+
delete event.buttons
|
|
52
|
+
delete event.buttonLabels
|
|
53
|
+
|
|
54
|
+
for (const gamepadInstance of collection) {
|
|
55
|
+
Object.assign(gamepadInstance._axes, axes)
|
|
56
|
+
Object.assign(gamepadInstance._buttons, buttons)
|
|
57
|
+
Object.assign(gamepadInstance._buttonLabels, buttonLabels)
|
|
58
|
+
tryEmit(gamepadInstance, type, event)
|
|
59
|
+
}
|
|
60
|
+
} break
|
|
61
|
+
|
|
62
|
+
// No default
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = { handleEvent }
|