@kmamal/sdl 0.11.13 → 1.0.0-3

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 (86) hide show
  1. package/CHANGELOG.md +113 -1
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +40 -34
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +29 -34
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +280 -174
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +211 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +41 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +81 -42
  31. package/src/javascript/joystick/shared.js +90 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +22 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +117 -0
  41. package/src/javascript/video/index.js +44 -5
  42. package/src/javascript/video/window.js +289 -133
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +409 -321
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +45 -19
  73. package/src/types/index.d.ts +366 -273
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
@@ -0,0 +1,211 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { startEffect, stopEffect, releaseDevice, setPlayer } = require('../joystick/shared')
5
+
6
+ const validEvents = [
7
+ 'axisMotion',
8
+ 'buttonDown',
9
+ 'buttonUp',
10
+ 'powerUpdate',
11
+ 'steamHandleUpdate',
12
+ 'remap',
13
+ 'close',
14
+ ]
15
+
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 }) }
22
+
23
+ const result = Bindings.gamepad_open(device.id)
24
+
25
+ this._firmwareVersion = result.firmwareVersion
26
+ this._serialNumber = result.serialNumber
27
+ this._steamHandle = result.steamHandle
28
+ this._power = result.power
29
+ this._axes = result.axes
30
+ this._buttons = result.buttons
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
+ }
45
+
46
+ get device () { return this._device }
47
+ get firmwareVersion () { return this._firmwareVersion }
48
+ get serialNumber () { return this._serialNumber }
49
+
50
+ get steamHandle () {
51
+ Globals.events.poll()
52
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
53
+
54
+ return this._steamHandle
55
+ }
56
+
57
+ get axes () {
58
+ Globals.events.poll()
59
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
60
+
61
+ return this._axes
62
+ }
63
+
64
+ get buttons () {
65
+ Globals.events.poll()
66
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
67
+
68
+ return this._buttons
69
+ }
70
+
71
+ get buttonLabels () {
72
+ Globals.events.poll()
73
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
74
+
75
+ return this._buttonLabels
76
+ }
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
+ }
210
+
211
+ module.exports = { GamepadInstance }
@@ -0,0 +1,51 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { GamepadInstance } = require('./gamepad-instance')
5
+ const { make: makeGamepadDevice } = require('./device')
6
+ const { make: makeJoystickDevice } = require('../joystick/device')
7
+
8
+ const validEvents = [ 'deviceAdd', 'deviceRemove' ]
9
+
10
+ // Updated mappings change existing joystick and gamepad devices without an event
11
+ 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
+
22
+ Globals.events.poll()
23
+ }
24
+
25
+ const gamepad = new class extends EventsViaPoll {
26
+ constructor () { super(validEvents) }
27
+
28
+ get devices () {
29
+ Globals.events.poll()
30
+ return [ ...Globals.gamepadDevices ]
31
+ }
32
+
33
+ openDevice (device) { return new GamepadInstance(device) }
34
+
35
+ addMappings (mappings) {
36
+ if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
37
+ for (const mapping of mappings) {
38
+ if (typeof mapping !== 'string') { throw Object.assign(new Error("mapping must be a string"), { mapping }) }
39
+ }
40
+
41
+ try { Bindings.gamepad_addMappings(mappings) }
42
+ catch (error) {
43
+ try { refreshDevices() }
44
+ catch (_) { }
45
+ throw error
46
+ }
47
+ refreshDevices()
48
+ }
49
+ }()
50
+
51
+ module.exports = { gamepad }
@@ -1,22 +1,25 @@
1
1
 
2
2
  module.exports = {
3
+ info: null,
4
+
3
5
  displays: [],
4
6
  windows: {
5
7
  all: new Map(),
6
8
  focused: null,
7
9
  hovered: null,
8
10
  },
9
- touchDevices: null,
11
+ touchDevices: [],
10
12
  joystickDevices: [],
11
13
  joystickInstances: {
12
14
  all: new Set(),
13
15
  byId: new Map(),
14
16
  },
15
- controllerDevices: [],
16
- controllerInstances: {
17
+ gamepadDevices: [],
18
+ gamepadInstances: {
17
19
  all: new Set(),
18
20
  byId: new Map(),
19
21
  },
22
+ sensorDevices: [],
20
23
  sensorInstances: {
21
24
  all: new Set(),
22
25
  byId: new Map(),
@@ -25,7 +28,7 @@ module.exports = {
25
28
  playback: [],
26
29
  recording: [],
27
30
  },
28
- audioInstances: new Map(),
31
+ audioStreams: new Map(),
29
32
 
30
- events: null, // Set later from events/index.js
33
+ events: null,
31
34
  }
@@ -1,18 +1,52 @@
1
- const { AudioFormatHelpers } = require('./audio/format-helpers')
1
+ const Enums = require('./enums')
2
+ const { getFormatHelpers: getAudioFormatHelpers } = require('./audio/format-helpers')
3
+ const { getFormatHelpers: getVideoFormatHelpers } = require('./video/format-helpers')
2
4
 
3
5
  module.exports = {
6
+ video: {
7
+ bytesPerPixel (format) { return getVideoFormatHelpers(format).bytesPerPixel },
8
+ isYuv (format) { return getVideoFormatHelpers(format).isYuv },
9
+ isPlanarYuv (format) { return getVideoFormatHelpers(format).isPlanarYuv },
10
+
11
+ minStride (format, width) {
12
+ const helpers = getVideoFormatHelpers(format)
13
+ if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
14
+ if (width < 0) { throw Object.assign(new Error("width must be non-negative"), { width }) }
15
+ return helpers.minStride(width)
16
+ },
17
+
18
+ minBufferSize (format, stride, height) {
19
+ const helpers = getVideoFormatHelpers(format)
20
+ if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
21
+ if (stride < 0) { throw Object.assign(new Error("stride must be non-negative"), { stride }) }
22
+ if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
23
+ if (height < 0) { throw Object.assign(new Error("height must be non-negative"), { height }) }
24
+ return helpers.minBufferSize(stride, height)
25
+ },
26
+ },
27
+ keyboard: {
28
+ get SCANCODE () { return Enums.scancodes },
29
+ },
30
+ mouse: {
31
+ get BUTTON () { return Enums.mouseButtons },
32
+ },
33
+ sensor: {
34
+ STANDARD_GRAVITY: 9.80665,
35
+ },
4
36
  audio: {
5
- bytesPerSample (format) { return AudioFormatHelpers[format].bytesPerSample },
6
- minSampleValue (format) { return AudioFormatHelpers[format].minSampleValue },
7
- maxSampleValue (format) { return AudioFormatHelpers[format].maxSampleValue },
8
- zeroSampleValue (format) { return AudioFormatHelpers[format].zeroSampleValue },
37
+ bytesPerSample (format) { return getAudioFormatHelpers(format).bytesPerSample },
38
+ minSampleValue (format) { return getAudioFormatHelpers(format).minSampleValue },
39
+ maxSampleValue (format) { return getAudioFormatHelpers(format).maxSampleValue },
40
+ zeroSampleValue (format) { return getAudioFormatHelpers(format).zeroSampleValue },
9
41
 
10
42
  readSample (format, buffer, offset) {
11
- return AudioFormatHelpers[format].reader.call(buffer, offset)
43
+ if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
44
+ return getAudioFormatHelpers(format).reader.call(buffer, offset)
12
45
  },
13
46
 
14
47
  writeSample (format, buffer, value, offset) {
15
- return AudioFormatHelpers[format].writer.call(buffer, value, offset)
48
+ if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
49
+ return getAudioFormatHelpers(format).writer.call(buffer, value, offset)
16
50
  },
17
51
  },
18
52
  }
@@ -1,16 +1,18 @@
1
- const { isMainThread } = require('worker_threads')
1
+ const { isMainThread } = require('node:worker_threads')
2
2
  if (!isMainThread) { throw new Error('@kmamal/sdl can only be used in the main thread') }
3
3
 
4
4
  const Bindings = require('./bindings')
5
+ const Globals = require('./globals')
5
6
 
6
7
  const info = Bindings.global_initialize()
8
+ Globals.info = info
7
9
 
8
10
  const { video } = require('./video')
9
11
  const { keyboard } = require('./keyboard')
10
12
  const { mouse } = require('./mouse')
11
13
  const { touch } = require('./touch')
12
14
  const { joystick } = require('./joystick')
13
- const { controller } = require('./controller')
15
+ const { gamepad } = require('./gamepad')
14
16
  const { sensor } = require('./sensor')
15
17
  const { audio } = require('./audio')
16
18
  const { clipboard } = require('./clipboard')
@@ -26,7 +28,7 @@ module.exports = {
26
28
  mouse,
27
29
  touch,
28
30
  joystick,
29
- controller,
31
+ gamepad,
30
32
  sensor,
31
33
  audio,
32
34
  clipboard,
@@ -1,18 +1,13 @@
1
1
 
2
2
  const make = (device) => {
3
3
  const {
4
- isController,
5
- controllerMapping,
6
- controllerName,
7
- controllerType,
4
+ isGamepad,
5
+ gamepadMapping,
6
+ gamepadName,
7
+ gamepadType,
8
8
  ...rest
9
9
  } = device
10
10
  return rest
11
11
  }
12
12
 
13
- const compare = (a, b) => a._index - b._index
14
-
15
- module.exports = {
16
- make,
17
- compare,
18
- }
13
+ module.exports = { make }
@@ -6,16 +6,16 @@ const { JoystickInstance } = require('./joystick-instance')
6
6
 
7
7
  const { make: makeJoystickDevice } = require('./device')
8
8
  const {
9
- make: makeControllerDevice,
10
- filter: filterControllerDevice,
11
- } = require('../controller/device')
9
+ make: makeGamepadDevice,
10
+ filter: filterGamepadDevice,
11
+ } = require('../gamepad/device')
12
12
 
13
13
  const devices = Bindings.joystick_getDevices()
14
14
  Globals.joystickDevices = devices
15
15
  .map(makeJoystickDevice)
16
- Globals.controllerDevices = devices
17
- .filter(filterControllerDevice)
18
- .map(makeControllerDevice)
16
+ Globals.gamepadDevices = devices
17
+ .filter(filterGamepadDevice)
18
+ .map(makeGamepadDevice)
19
19
 
20
20
 
21
21
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
@@ -25,7 +25,7 @@ const joystick = new class extends EventsViaPoll {
25
25
 
26
26
  get devices () {
27
27
  Globals.events.poll()
28
- return Globals.joystickDevices
28
+ return [ ...Globals.joystickDevices ]
29
29
  }
30
30
 
31
31
  openDevice (device) { return new JoystickInstance(device) }
@@ -1,6 +1,7 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { startEffect, stopEffect, releaseDevice, setPlayer } = require('./shared')
4
5
 
5
6
  const validEvents = [
6
7
  'axisMotion',
@@ -16,15 +17,13 @@ class JoystickInstance extends EventsViaPoll {
16
17
  constructor (device) {
17
18
  super(validEvents)
18
19
 
19
- if (!Globals.joystickDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
20
+ Globals.events.poll()
21
+ if (!Globals.joystickDevices.includes(device)) { throw Object.assign(new Error("device must be from sdl.joystick.devices"), { device }) }
20
22
 
21
- const result = Bindings.joystick_open(device._index)
23
+ const result = Bindings.joystick_open(device.id)
22
24
 
23
25
  this._firmwareVersion = result.firmwareVersion
24
26
  this._serialNumber = result.serialNumber
25
- this._hasLed = result.hasLed
26
- this._hasRumble = result.hasRumble
27
- this._hasRumbleTriggers = result.hasRumbleTriggers
28
27
  this._axes = result.axes
29
28
  this._balls = result.balls
30
29
  this._buttons = result.buttons
@@ -33,8 +32,6 @@ class JoystickInstance extends EventsViaPoll {
33
32
 
34
33
  this._device = device
35
34
 
36
- this._rumbleTimeout = null
37
- this._rumbleTriggersTimeout = null
38
35
  this._closed = false
39
36
 
40
37
  Globals.joystickInstances.all.add(this)
@@ -52,42 +49,60 @@ class JoystickInstance extends EventsViaPoll {
52
49
 
53
50
  get axes () {
54
51
  Globals.events.poll()
52
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
53
+
55
54
  return this._axes
56
55
  }
57
56
 
58
57
  get balls () {
59
58
  Globals.events.poll()
59
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
60
+
60
61
  return this._balls
61
62
  }
62
63
 
63
64
  get buttons () {
64
65
  Globals.events.poll()
66
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
67
+
65
68
  return this._buttons
66
69
  }
67
70
 
68
71
  get hats () {
69
72
  Globals.events.poll()
73
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
74
+
70
75
  return this._hats
71
76
  }
72
77
 
73
- get power () { return this._power }
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
+ }
74
84
 
75
85
  setPlayer (player) {
76
86
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
77
87
 
78
88
  if (!Number.isInteger(player)) { throw Object.assign(new Error("player must be an integer"), { player }) }
79
- if (player < 0) { throw Object.assign(new Error("invalid player"), { player }) }
89
+ if (player < 0 || player > 2 ** 31 - 1) { throw Object.assign(new Error("player must be a non-negative 32-bit integer"), { player }) }
80
90
 
81
- Bindings.joystick_setPlayer(this._device.id, player)
91
+ setPlayer(this._device.id, player)
82
92
  }
83
93
 
84
94
  resetPlayer () {
85
95
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
86
96
 
87
- Bindings.joystick_setPlayer(this._device.id, -1)
97
+ setPlayer(this._device.id, -1)
88
98
  }
89
99
 
90
- get hasLed () { return this._hasLed }
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
+ }
91
106
  setLed (red, green, blue) {
92
107
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
93
108
 
@@ -98,72 +113,96 @@ class JoystickInstance extends EventsViaPoll {
98
113
  if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
99
114
  if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
100
115
 
116
+ if (!this.hasLed) { throw Object.assign(new Error("device has no led"), { id: this._device.id }) }
117
+
101
118
  Bindings.joystick_setLed(this._device.id, red, green, blue)
102
119
  }
103
120
 
104
- get hasRumble () { return this._hasRumble }
105
- rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = 1e3) {
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) {
106
128
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
107
129
 
108
130
  if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
109
131
  if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
110
132
  if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
111
133
  if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
112
- if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
113
- if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
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 }) }
114
140
 
115
- // Globals.events.poll() // Errors if it hasn't been called at least once
116
- Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
117
- clearTimeout(this._rumbleTimeout)
118
- this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
141
+ startEffect('rumble', this._device.id, lowFreqRumble, highFreqRumble, duration)
119
142
  }
120
143
 
121
144
  stopRumble () {
122
- clearTimeout(this._rumbleTimeout)
123
- this._rumbleTimeout = null
124
- Bindings.joystick_rumble(this._device.id, 0, 0, 0)
125
- Globals.events.poll()
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)
126
150
  }
127
151
 
128
- get hasRumbleTriggers () { return this._hasRumbleTriggers }
129
- rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
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) {
130
159
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
131
160
 
132
161
  if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
133
162
  if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
134
163
  if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
135
164
  if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
136
- if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
137
- if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
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 }) }
138
171
 
139
- // Globals.events.poll() // Errors if it hasn't been called at least once
140
- Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
141
- clearTimeout(this._rumbleTriggersTimeout)
142
- this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
172
+ startEffect('rumbleTriggers', this._device.id, leftRumble, rightRumble, duration)
143
173
  }
144
174
 
145
175
  stopRumbleTriggers () {
146
- clearTimeout(this._rumbleTriggersTimeout)
147
- this._rumbleTriggersTimeout = null
148
- Bindings.joystick_rumbleTriggers(this._device.id, 0, 0, 0)
149
- Globals.events.poll()
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)
150
181
  }
151
182
 
152
183
  get closed () { return this._closed }
153
184
  close () {
154
185
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
155
186
 
156
- this.emit('close')
157
- this.removeAllListeners()
158
187
  this._closed = true
159
188
 
160
189
  Globals.joystickInstances.all.delete(this)
161
190
  const collection = Globals.joystickInstances.byId.get(this._device.id)
162
191
  collection.delete(this)
163
- if (collection.size === 0) {
164
- Bindings.joystick_close(this._device.id)
165
- Globals.joystickInstances.byId.delete(this._device.id)
166
- }
192
+ if (collection.size === 0) { Globals.joystickInstances.byId.delete(this._device.id) }
193
+ releaseDevice(this._device.id)
194
+
195
+ // This call could throw if the device is gone
196
+ try { Bindings.joystick_close(this._device.id) }
197
+ catch (_) {}
198
+
199
+ // We might be inside an event listener
200
+ this._retire(() => Object.assign(new Error("instance is closed"), { id: this._device.id }))
201
+
202
+ Globals.events.withoutPolling(() => {
203
+ try { this.emit('close', { type: 'close' }) }
204
+ catch (error) { this.emit('error', error) }
205
+ })
167
206
  }
168
207
  }
169
208