@kmamal/sdl 1.0.0-5 → 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.
Files changed (84) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/binding.gyp +3 -0
  3. package/package.json +5 -12
  4. package/scripts/util/common.js +2 -2
  5. package/src/javascript/audio/audio-playback-stream.js +9 -10
  6. package/src/javascript/audio/audio-recording-stream.js +9 -8
  7. package/src/javascript/audio/audio-stream.js +100 -44
  8. package/src/javascript/audio/conversion.js +41 -0
  9. package/src/javascript/audio/devices.js +11 -0
  10. package/src/javascript/audio/events.js +65 -0
  11. package/src/javascript/audio/format-helpers.js +8 -4
  12. package/src/javascript/audio/index.js +25 -20
  13. package/src/javascript/audio/prevent-exit.js +12 -9
  14. package/src/javascript/audio/validate.js +9 -0
  15. package/src/javascript/cleanup.js +9 -6
  16. package/src/javascript/clipboard/index.js +8 -3
  17. package/src/javascript/device-instance.js +61 -0
  18. package/src/javascript/events/events-via-poll.js +33 -27
  19. package/src/javascript/events/index.js +75 -482
  20. package/src/javascript/events/try.js +22 -0
  21. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  22. package/src/javascript/gamepad/events.js +66 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  24. package/src/javascript/gamepad/index.js +15 -16
  25. package/src/javascript/globals.js +10 -1
  26. package/src/javascript/index.js +2 -5
  27. package/src/javascript/joystick/devices.js +43 -0
  28. package/src/javascript/joystick/events.js +129 -0
  29. package/src/javascript/joystick/index.js +9 -18
  30. package/src/javascript/joystick/joystick-device-instance.js +175 -0
  31. package/src/javascript/joystick/joystick-instance.js +12 -158
  32. package/src/javascript/keyboard/index.js +20 -10
  33. package/src/javascript/keyboard/key-mapping.js +46 -27
  34. package/src/javascript/mouse/index.js +36 -29
  35. package/src/javascript/sensor/events.js +21 -0
  36. package/src/javascript/sensor/index.js +13 -2
  37. package/src/javascript/sensor/sensor-instance.js +7 -47
  38. package/src/javascript/subsystems.js +56 -0
  39. package/src/javascript/touch/index.js +9 -4
  40. package/src/javascript/validate.js +114 -0
  41. package/src/javascript/video/conversion.js +63 -0
  42. package/src/javascript/video/events.js +179 -0
  43. package/src/javascript/video/format-helpers.js +10 -8
  44. package/src/javascript/video/index.js +28 -14
  45. package/src/javascript/video/validate.js +45 -0
  46. package/src/javascript/video/window.js +144 -161
  47. package/src/native/audio.cpp +457 -151
  48. package/src/native/audio.h +12 -6
  49. package/src/native/clipboard.cpp +8 -37
  50. package/src/native/enums.cpp +2 -0
  51. package/src/native/events.cpp +416 -354
  52. package/src/native/events.h +67 -65
  53. package/src/native/gamepad.cpp +86 -103
  54. package/src/native/gamepad.h +7 -8
  55. package/src/native/global.cpp +190 -85
  56. package/src/native/global.h +48 -2
  57. package/src/native/joystick.cpp +180 -274
  58. package/src/native/joystick.h +12 -6
  59. package/src/native/keyboard.cpp +60 -22
  60. package/src/native/keyboard.h +2 -1
  61. package/src/native/module.cpp +20 -3
  62. package/src/native/mouse.cpp +27 -58
  63. package/src/native/packed.cpp +106 -0
  64. package/src/native/packed.h +91 -0
  65. package/src/native/power.cpp +7 -1
  66. package/src/native/power.h +2 -2
  67. package/src/native/sensor.cpp +61 -69
  68. package/src/native/sensor.h +3 -3
  69. package/src/native/testing.cpp +131 -0
  70. package/src/native/testing.h +16 -0
  71. package/src/native/threads.cpp +300 -0
  72. package/src/native/threads.h +36 -0
  73. package/src/native/touch.cpp +32 -43
  74. package/src/native/touch.h +2 -1
  75. package/src/native/video.cpp +145 -95
  76. package/src/native/video.h +6 -3
  77. package/src/native/window.cpp +155 -218
  78. package/src/native/window.h +21 -20
  79. package/src/types/index.d.ts +141 -69
  80. package/src/javascript/audio/device.js +0 -37
  81. package/src/javascript/joystick/device.js +0 -13
  82. package/src/javascript/joystick/shared.js +0 -90
  83. package/src/javascript/worker.js +0 -88
  84. package/src/types/worker.d.ts +0 -71
@@ -1,7 +1,8 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
- const { EventsViaPoll } = require('../events/events-via-poll')
4
- const { startEffect, stopEffect, releaseDevice, setPlayer } = require('../joystick/shared')
3
+ const { validateNotClosed } = require('../validate')
4
+ const { lookupDevice } = require('../device-instance')
5
+ const { JoystickDeviceInstance } = require('../joystick/joystick-device-instance')
5
6
 
6
7
  const validEvents = [
7
8
  'axisMotion',
@@ -13,15 +14,14 @@ const validEvents = [
13
14
  'close',
14
15
  ]
15
16
 
16
- class GamepadInstance extends EventsViaPoll {
17
- constructor (device) {
18
- super(validEvents)
19
-
20
- Globals.events.poll()
21
- if (!Globals.gamepadDevices.includes(device)) { throw Object.assign(new Error("device must be from sdl.gamepad.devices"), { device }) }
17
+ class GamepadInstance extends JoystickDeviceInstance {
18
+ constructor (requested) {
19
+ const device = lookupDevice(requested, 'gamepad', () => Globals.gamepadDevices)
22
20
 
23
21
  const result = Bindings.gamepad_open(device.id)
24
22
 
23
+ super(validEvents, device, Globals.gamepadInstances, Bindings.gamepad_close)
24
+
25
25
  this._firmwareVersion = result.firmwareVersion
26
26
  this._serialNumber = result.serialNumber
27
27
  this._steamHandle = result.steamHandle
@@ -29,183 +29,35 @@ class GamepadInstance extends EventsViaPoll {
29
29
  this._axes = result.axes
30
30
  this._buttons = result.buttons
31
31
  this._buttonLabels = result.buttonLabels
32
-
33
- this._device = device
34
-
35
- this._closed = false
36
-
37
- Globals.gamepadInstances.all.add(this)
38
- let collection = Globals.gamepadInstances.byId.get(this._device.id)
39
- if (!collection) {
40
- collection = new Set()
41
- Globals.gamepadInstances.byId.set(this._device.id, collection)
42
- }
43
- collection.add(this)
44
32
  }
45
33
 
46
- get device () { return this._device }
47
- get firmwareVersion () { return this._firmwareVersion }
48
- get serialNumber () { return this._serialNumber }
49
-
50
34
  get steamHandle () {
51
35
  Globals.events.poll()
52
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
36
+ validateNotClosed(this)
53
37
 
54
38
  return this._steamHandle
55
39
  }
56
40
 
57
41
  get axes () {
58
42
  Globals.events.poll()
59
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
43
+ validateNotClosed(this)
60
44
 
61
45
  return this._axes
62
46
  }
63
47
 
64
48
  get buttons () {
65
49
  Globals.events.poll()
66
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
50
+ validateNotClosed(this)
67
51
 
68
52
  return this._buttons
69
53
  }
70
54
 
71
55
  get buttonLabels () {
72
56
  Globals.events.poll()
73
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
57
+ validateNotClosed(this)
74
58
 
75
59
  return this._buttonLabels
76
60
  }
77
-
78
- get power () {
79
- Globals.events.poll()
80
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
81
-
82
- return this._power
83
- }
84
-
85
- setPlayer (player) {
86
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
87
-
88
- if (!Number.isInteger(player)) { throw Object.assign(new Error("player must be an integer"), { player }) }
89
- if (player < 0 || player > 2 ** 31 - 1) { throw Object.assign(new Error("player must be a non-negative 32-bit integer"), { player }) }
90
-
91
- setPlayer(this._device.id, player)
92
- }
93
-
94
- resetPlayer () {
95
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
96
-
97
- setPlayer(this._device.id, -1)
98
- }
99
-
100
- get hasLed () {
101
- Globals.events.poll()
102
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
103
-
104
- return Bindings.joystick_getCapabilities(this._device.id).hasLed
105
- }
106
- setLed (red, green, blue) {
107
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
108
-
109
- if (!Number.isFinite(red)) { throw Object.assign(new Error("red must be a number"), { red }) }
110
- if (red < 0 || red > 1) { throw Object.assign(new Error("red must be between 0 and 1"), { red }) }
111
- if (!Number.isFinite(green)) { throw Object.assign(new Error("green must be a number"), { green }) }
112
- if (green < 0 || green > 1) { throw Object.assign(new Error("green must be between 0 and 1"), { green }) }
113
- if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
114
- if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
115
-
116
- if (!this.hasLed) { throw Object.assign(new Error("device has no led"), { id: this._device.id }) }
117
-
118
- Bindings.joystick_setLed(this._device.id, red, green, blue)
119
- }
120
-
121
- get hasRumble () {
122
- Globals.events.poll()
123
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
124
-
125
- return Bindings.joystick_getCapabilities(this._device.id).hasRumble
126
- }
127
- rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = null) {
128
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
129
-
130
- if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
131
- if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
132
- if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
133
- if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
134
- if (duration !== null) {
135
- if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
136
- if (duration < 0 || duration > 2 ** 31 - 1) { throw Object.assign(new Error("duration must be a non-negative 32-bit integer"), { duration }) }
137
- }
138
-
139
- if (!this.hasRumble) { throw Object.assign(new Error("device has no rumble"), { id: this._device.id }) }
140
-
141
- startEffect('rumble', this._device.id, lowFreqRumble, highFreqRumble, duration)
142
- }
143
-
144
- stopRumble () {
145
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
146
-
147
- if (!this.hasRumble) { throw Object.assign(new Error("device has no rumble"), { id: this._device.id }) }
148
-
149
- stopEffect('rumble', this._device.id)
150
- }
151
-
152
- get hasRumbleTriggers () {
153
- Globals.events.poll()
154
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
155
-
156
- return Bindings.joystick_getCapabilities(this._device.id).hasRumbleTriggers
157
- }
158
- rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = null) {
159
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
160
-
161
- if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
162
- if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
163
- if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
164
- if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
165
- if (duration !== null) {
166
- if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
167
- if (duration < 0 || duration > 2 ** 31 - 1) { throw Object.assign(new Error("duration must be a non-negative 32-bit integer"), { duration }) }
168
- }
169
-
170
- if (!this.hasRumbleTriggers) { throw Object.assign(new Error("device has no trigger rumble"), { id: this._device.id }) }
171
-
172
- startEffect('rumbleTriggers', this._device.id, leftRumble, rightRumble, duration)
173
- }
174
-
175
- stopRumbleTriggers () {
176
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
177
-
178
- if (!this.hasRumbleTriggers) { throw Object.assign(new Error("device has no trigger rumble"), { id: this._device.id }) }
179
-
180
- stopEffect('rumbleTriggers', this._device.id)
181
- }
182
-
183
- get closed () { return this._closed }
184
- close () {
185
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
186
-
187
- this._closed = true
188
-
189
- Globals.gamepadInstances.all.delete(this)
190
- const collection = Globals.gamepadInstances.byId.get(this._device.id)
191
- collection.delete(this)
192
- if (collection.size === 0) {
193
- Globals.gamepadInstances.byId.delete(this._device.id)
194
- }
195
- releaseDevice(this._device.id)
196
-
197
- // This call could throw if the device is gone
198
- try { Bindings.gamepad_close(this._device.id) }
199
- catch (_) {}
200
-
201
- // We might be inside an event listener
202
- this._retire(() => Object.assign(new Error("instance is closed"), { id: this._device.id }))
203
-
204
- Globals.events.withoutPolling(() => {
205
- try { this.emit('close', { type: 'close' }) }
206
- catch (error) { this.emit('error', error) }
207
- })
208
- }
209
61
  }
210
62
 
211
63
  module.exports = { GamepadInstance }
@@ -1,47 +1,46 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { assertInitialized, isAvailable } = require('../subsystems')
4
5
  const { GamepadInstance } = require('./gamepad-instance')
5
- const { make: makeGamepadDevice } = require('./device')
6
- const { make: makeJoystickDevice } = require('../joystick/device')
6
+ const { refreshAndBroadcast } = require('../joystick/devices')
7
+ const { validateString } = require('../validate')
7
8
 
8
9
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
9
10
 
10
11
  // Updated mappings change existing joystick and gamepad devices without an event
11
12
  const refreshDevices = () => {
12
- const devices = Bindings.joystick_getDevices()
13
- for (const joystickDevice of Globals.joystickDevices) {
14
- const device = devices.find(({ id }) => id === joystickDevice.id)
15
- if (device) { Object.assign(joystickDevice, makeJoystickDevice(device)) }
16
- }
17
- for (const gamepadDevice of Globals.gamepadDevices) {
18
- const device = devices.find(({ id }) => id === gamepadDevice.id)
19
- if (device?.isGamepad) { Object.assign(gamepadDevice, makeGamepadDevice(device)) }
20
- }
21
-
13
+ refreshAndBroadcast()
22
14
  Globals.events.poll()
23
15
  }
24
16
 
25
17
  const gamepad = new class extends EventsViaPoll {
26
- constructor () { super(validEvents) }
18
+ constructor () { super(validEvents, () => { assertInitialized('gamepad') }) }
19
+
20
+ isAvailable () { return isAvailable('gamepad') }
27
21
 
28
22
  get devices () {
23
+ assertInitialized('gamepad')
29
24
  Globals.events.poll()
30
25
  return [ ...Globals.gamepadDevices ]
31
26
  }
32
27
 
33
- openDevice (device) { return new GamepadInstance(device) }
28
+ openDevice (device) {
29
+ assertInitialized('gamepad')
30
+ return new GamepadInstance(device)
31
+ }
34
32
 
35
33
  addMappings (mappings) {
34
+ assertInitialized('gamepad')
36
35
  if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
37
36
  for (const mapping of mappings) {
38
- if (typeof mapping !== 'string') { throw Object.assign(new Error("mapping must be a string"), { mapping }) }
37
+ validateString('mapping', mapping)
39
38
  }
40
39
 
41
40
  try { Bindings.gamepad_addMappings(mappings) }
42
41
  catch (error) {
43
42
  try { refreshDevices() }
44
- catch (_) { }
43
+ catch { }
45
44
  throw error
46
45
  }
47
46
  refreshDevices()
@@ -1,6 +1,15 @@
1
1
 
2
2
  module.exports = {
3
- info: null,
3
+ info: {},
4
+
5
+ initialized: {
6
+ video: null,
7
+ audio: null,
8
+ joystick: null,
9
+ gamepad: null,
10
+ sensor: null,
11
+ },
12
+ initErrors: {},
4
13
 
5
14
  displays: [],
6
15
  windows: {
@@ -1,11 +1,8 @@
1
1
  const { isMainThread } = require('node:worker_threads')
2
- if (!isMainThread) { throw new Error("@kmamal/sdl can only be used in the main thread, import @kmamal/sdl/worker in workers") }
3
-
4
2
  const Bindings = require('./bindings')
5
3
  const Globals = require('./globals')
6
4
 
7
- const info = Bindings.global_initialize()
8
- Globals.info = info
5
+ Globals.info = Bindings.global_initialize(isMainThread)
9
6
 
10
7
  const { video } = require('./video')
11
8
  const { keyboard } = require('./keyboard')
@@ -22,7 +19,7 @@ require('./events')
22
19
  require('./cleanup')
23
20
 
24
21
  module.exports = {
25
- info,
22
+ info: Globals.info,
26
23
  video,
27
24
  keyboard,
28
25
  mouse,
@@ -0,0 +1,43 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { makeDevice: makeGamepadDevice } = require('../gamepad/devices')
4
+
5
+ const makeDevice = (device) => {
6
+ const {
7
+ isGamepad,
8
+ gamepadMapping,
9
+ gamepadName,
10
+ gamepadType,
11
+ ...rest
12
+ } = device
13
+ return rest
14
+ }
15
+
16
+ const refreshDevices = () => {
17
+ const devices = Bindings.joystick_getDevices()
18
+ for (const joystickDevice of Globals.joystickDevices) {
19
+ const device = devices.find(({ id }) => id === joystickDevice.id)
20
+ if (device) { Object.assign(joystickDevice, makeDevice(device)) }
21
+ }
22
+ for (const gamepadDevice of Globals.gamepadDevices) {
23
+ const device = devices.find(({ id }) => id === gamepadDevice.id)
24
+ if (device?.isGamepad) { Object.assign(gamepadDevice, makeGamepadDevice(device)) }
25
+ }
26
+ }
27
+
28
+ const handleDevicesChange = () => {
29
+ // Thread might not have initialized this subsystem
30
+ if (!Globals.initialized.joystick) { return }
31
+ refreshDevices()
32
+ }
33
+
34
+ const refreshAndBroadcast = () => {
35
+ refreshDevices()
36
+ Globals.events.broadcastChange('joystickDevices')
37
+ }
38
+
39
+ module.exports = {
40
+ makeDevice,
41
+ handleDevicesChange,
42
+ refreshAndBroadcast,
43
+ }
@@ -0,0 +1,129 @@
1
+ const Globals = require('../globals')
2
+ const { joystick: joystickModule } = require('.')
3
+ const { gamepad: gamepadModule } = require('../gamepad')
4
+ const { makeDevice: makeJoystickDevice } = require('./devices')
5
+ const { makeDevice: makeGamepadDevice, isGamepadDevice } = require('../gamepad/devices')
6
+ const { tryEmit, tryCall } = require('../events/try')
7
+
8
+ const handleEvent = (event, targetId) => {
9
+ const { type } = event
10
+
11
+ const collection = Globals.joystickInstances.byId.get(targetId)
12
+
13
+ // Battery updates arrive as joystick events but also concern gamepads
14
+ if (type === 'powerUpdate') {
15
+ if (collection) {
16
+ for (const joystickInstance of collection) {
17
+ joystickInstance._power = event.power
18
+ tryEmit(joystickInstance, type, event)
19
+ }
20
+ }
21
+
22
+ const otherCollection = Globals.gamepadInstances.byId.get(targetId)
23
+ if (!otherCollection) { return }
24
+
25
+ for (const gamepadInstance of otherCollection) {
26
+ gamepadInstance._power = event.power
27
+ tryEmit(gamepadInstance, type, event)
28
+ }
29
+ return
30
+ }
31
+
32
+ if (!collection) { return }
33
+
34
+ switch (type) {
35
+ case 'axisMotion': {
36
+ for (const joystickInstance of collection) {
37
+ joystickInstance._axes[event.axis] = event.value
38
+ tryEmit(joystickInstance, type, event)
39
+ }
40
+ } break
41
+
42
+ case 'ballMotion': {
43
+ for (const joystickInstance of collection) {
44
+ const ball = joystickInstance._balls[event.ball]
45
+ ball.x += event.dx
46
+ ball.y += event.dy
47
+ // Each instance accumulates its own x and y, so each gets its own copy of the event
48
+ tryEmit(joystickInstance, type, { ...event, x: ball.x, y: ball.y })
49
+ }
50
+ } break
51
+
52
+ case 'buttonDown': {
53
+ for (const joystickInstance of collection) {
54
+ joystickInstance._buttons[event.button] = true
55
+ tryEmit(joystickInstance, type, event)
56
+ }
57
+ } break
58
+ case 'buttonUp': {
59
+ for (const joystickInstance of collection) {
60
+ joystickInstance._buttons[event.button] = false
61
+ tryEmit(joystickInstance, type, event)
62
+ }
63
+ } break
64
+
65
+ case 'hatMotion': {
66
+ for (const joystickInstance of collection) {
67
+ joystickInstance._hats[event.hat] = event.value
68
+ tryEmit(joystickInstance, type, event)
69
+ }
70
+ } break
71
+
72
+ // No default
73
+ }
74
+ }
75
+
76
+ const handleDeviceEvent = (isGamepad, event) => {
77
+ // Thread might not have initialized this subsystem
78
+ if (!Globals.initialized.joystick) { return }
79
+
80
+ const { type } = event
81
+
82
+ const emitter = isGamepad ? gamepadModule : joystickModule
83
+ const list = isGamepad ? Globals.gamepadDevices : Globals.joystickDevices
84
+
85
+ switch (type) {
86
+ case 'deviceAdd': {
87
+ const { device } = event
88
+ delete event.device
89
+
90
+ if (isGamepad && !isGamepadDevice(device)) { return }
91
+ if (list.some((a) => a.id === device.id)) { return }
92
+
93
+ const made = isGamepad ? makeGamepadDevice(device) : makeJoystickDevice(device)
94
+ list.push(made)
95
+ event.device = made
96
+ } break
97
+
98
+ case 'deviceRemove': {
99
+ const { deviceId } = event
100
+ delete event.deviceId
101
+
102
+ const instances = isGamepad ? Globals.gamepadInstances : Globals.joystickInstances
103
+ const collection = instances.byId.get(deviceId)
104
+ if (collection) {
105
+ for (const instance of collection.values()) {
106
+ tryCall(() => { instance.close() })
107
+ }
108
+ }
109
+
110
+ const index = list.findIndex((a) => a.id === deviceId)
111
+ if (index === -1) { return }
112
+ event.device = list[index]
113
+ list.splice(index, 1)
114
+ } break
115
+
116
+ // No default
117
+ }
118
+
119
+ tryEmit(emitter, type, event)
120
+ }
121
+
122
+ const handleJoystickDeviceEvent = (event) => { handleDeviceEvent(false, event) }
123
+ const handleGamepadDeviceEvent = (event) => { handleDeviceEvent(true, event) }
124
+
125
+ module.exports = {
126
+ handleEvent,
127
+ handleJoystickDeviceEvent,
128
+ handleGamepadDeviceEvent,
129
+ }
@@ -1,34 +1,25 @@
1
1
  const Globals = require('../globals')
2
- const Bindings = require('../bindings')
3
2
  const { EventsViaPoll } = require('../events/events-via-poll')
3
+ const { assertInitialized, isAvailable } = require('../subsystems')
4
4
  const { JoystickInstance } = require('./joystick-instance')
5
5
 
6
-
7
- const { make: makeJoystickDevice } = require('./device')
8
- const {
9
- make: makeGamepadDevice,
10
- filter: filterGamepadDevice,
11
- } = require('../gamepad/device')
12
-
13
- const devices = Bindings.joystick_getDevices()
14
- Globals.joystickDevices = devices
15
- .map(makeJoystickDevice)
16
- Globals.gamepadDevices = devices
17
- .filter(filterGamepadDevice)
18
- .map(makeGamepadDevice)
19
-
20
-
21
6
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
22
7
 
23
8
  const joystick = new class extends EventsViaPoll {
24
- constructor () { super(validEvents) }
9
+ constructor () { super(validEvents, () => { assertInitialized('joystick') }) }
10
+
11
+ isAvailable () { return isAvailable('joystick') }
25
12
 
26
13
  get devices () {
14
+ assertInitialized('joystick')
27
15
  Globals.events.poll()
28
16
  return [ ...Globals.joystickDevices ]
29
17
  }
30
18
 
31
- openDevice (device) { return new JoystickInstance(device) }
19
+ openDevice (device) {
20
+ assertInitialized('joystick')
21
+ return new JoystickInstance(device)
22
+ }
32
23
  }()
33
24
 
34
25
  module.exports = { joystick }