@kmamal/sdl 1.0.0-5 → 1.0.0-9
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/README.md +2 -2
- package/binding.gyp +21 -4
- package/package.json +5 -12
- package/scripts/build.mjs +3 -1
- package/scripts/install.mjs +2 -0
- package/scripts/util/common.js +3 -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 +101 -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 +13 -10
- 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 +32 -28
- package/src/javascript/events/index.js +85 -479
- package/src/javascript/events/try.js +25 -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 +176 -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 +119 -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 +196 -167
- package/src/native/audio.cpp +457 -153
- package/src/native/audio.h +25 -19
- package/src/native/clipboard.cpp +8 -37
- package/src/native/clipboard.h +2 -2
- package/src/native/enums.cpp +16 -1
- package/src/native/enums.h +1 -1
- package/src/native/events.cpp +351 -357
- package/src/native/events.h +70 -68
- package/src/native/gamepad.cpp +147 -103
- package/src/native/gamepad.h +10 -11
- package/src/native/global.cpp +183 -340
- package/src/native/global.h +51 -5
- package/src/native/joystick.cpp +204 -275
- package/src/native/joystick.h +21 -15
- package/src/native/keyboard.cpp +63 -25
- package/src/native/keyboard.h +5 -4
- package/src/native/module.cpp +23 -3
- package/src/native/mouse.cpp +28 -59
- package/src/native/mouse.h +9 -9
- package/src/native/packed.cpp +99 -0
- package/src/native/packed.h +86 -0
- package/src/native/power.cpp +13 -1
- package/src/native/power.h +3 -3
- package/src/native/sensor.cpp +78 -69
- package/src/native/sensor.h +7 -7
- package/src/native/testing.cpp +148 -0
- package/src/native/testing.h +17 -0
- package/src/native/threads.cpp +301 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +38 -44
- package/src/native/touch.h +3 -2
- package/src/native/video.cpp +211 -98
- package/src/native/video.h +7 -4
- package/src/native/window.cpp +201 -219
- package/src/native/window.h +24 -20
- package/src/types/index.d.ts +157 -70
- 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,491 +1,91 @@
|
|
|
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
|
-
case 'app': {
|
|
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
|
|
467
|
-
|
|
468
|
-
// No default
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
tryEmit(emitter, type, event)
|
|
472
|
-
} break
|
|
473
|
-
|
|
474
|
-
case 'clipboard': {
|
|
475
|
-
tryEmit(clipboardModule, type, event)
|
|
476
|
-
} break
|
|
477
|
-
|
|
478
|
-
// No default
|
|
479
|
-
}
|
|
64
|
+
handlers[target]?.(event, targetId)
|
|
480
65
|
}
|
|
481
66
|
|
|
482
67
|
|
|
483
68
|
let polling = false
|
|
484
69
|
|
|
70
|
+
let pollInterval = null
|
|
71
|
+
let pollingFast = null
|
|
72
|
+
|
|
73
|
+
// I would like to move this to native code eventually.
|
|
74
|
+
// Sadly a native uv_timer poll loop would needs direct libuv calls, making the binary non-portable across Node.js versions.
|
|
75
|
+
const setPollingFast = (fast) => {
|
|
76
|
+
if (!isMainThread || fast === pollingFast) { return }
|
|
77
|
+
pollingFast = fast
|
|
78
|
+
Timers.clearInterval(pollInterval)
|
|
79
|
+
pollInterval = Timers.setInterval(poll, fast ? 0 : 1e3)
|
|
80
|
+
pollInterval.unref()
|
|
81
|
+
}
|
|
82
|
+
|
|
485
83
|
const poll = () => {
|
|
486
84
|
if (polling) { return }
|
|
487
85
|
polling = true
|
|
488
|
-
try {
|
|
86
|
+
try {
|
|
87
|
+
setPollingFast(Bindings.events_poll(handleEvent))
|
|
88
|
+
}
|
|
489
89
|
finally { polling = false }
|
|
490
90
|
}
|
|
491
91
|
|
|
@@ -496,31 +96,37 @@ const withoutPolling = (fn) => {
|
|
|
496
96
|
finally { polling = prevValue }
|
|
497
97
|
}
|
|
498
98
|
|
|
499
|
-
|
|
99
|
+
const setDemand = (demand) => { setPollingFast(Bindings.events_setDemand(demand)) }
|
|
100
|
+
|
|
101
|
+
let refCount = 0
|
|
500
102
|
|
|
501
|
-
const
|
|
502
|
-
|
|
503
|
-
pollInterval = setInterval(poll, 0)
|
|
103
|
+
const ref = () => {
|
|
104
|
+
if (++refCount === 1) { setDemand(true) }
|
|
504
105
|
}
|
|
505
106
|
|
|
506
|
-
const
|
|
507
|
-
|
|
508
|
-
pollInterval = setInterval(poll, 1e3)
|
|
509
|
-
pollInterval.unref()
|
|
107
|
+
const unref = () => {
|
|
108
|
+
if (--refCount === 0) { setDemand(false) }
|
|
510
109
|
}
|
|
511
110
|
|
|
512
111
|
const stopPolling = () => {
|
|
513
|
-
clearInterval(pollInterval)
|
|
112
|
+
Timers.clearInterval(pollInterval)
|
|
514
113
|
pollInterval = null
|
|
515
114
|
}
|
|
516
115
|
|
|
116
|
+
const broadcastChange = (name) => { Bindings.events_broadcastChange(name) }
|
|
117
|
+
|
|
517
118
|
Globals.events = {
|
|
518
119
|
poll,
|
|
519
120
|
withoutPolling,
|
|
520
|
-
|
|
521
|
-
|
|
121
|
+
ref,
|
|
122
|
+
unref,
|
|
522
123
|
stopPolling,
|
|
124
|
+
broadcastChange,
|
|
523
125
|
}
|
|
524
126
|
|
|
525
|
-
|
|
526
|
-
|
|
127
|
+
Bindings.events_register(poll, isMainThread)
|
|
128
|
+
|
|
129
|
+
if (isMainThread) {
|
|
130
|
+
setPollingFast(false)
|
|
131
|
+
poll()
|
|
132
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Only for code that runs inside a NAPI callback.
|
|
2
|
+
// This is to prevent the exception from bubbling up through C++ code.
|
|
3
|
+
// Everywhere else, emit synchronously with `try { this.emit(...) } catch (error) { this.emit('error', error) }`
|
|
4
|
+
const rethrow = (error) => {
|
|
5
|
+
process.nextTick(() => { throw error })
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const tryEmit = (emitter, type, event) => {
|
|
9
|
+
try { emitter.emit(type, event) }
|
|
10
|
+
catch (error) {
|
|
11
|
+
if (type === 'error') {
|
|
12
|
+
rethrow(error)
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
try { emitter.emit('error', error) }
|
|
16
|
+
catch (error2) { rethrow(error2) }
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const tryCall = (fn) => {
|
|
21
|
+
try { fn() }
|
|
22
|
+
catch (error) { rethrow(error) }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
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 }
|