@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,5 +1,6 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
+ const { INT32_MAX } = require('../validate')
3
4
 
4
5
  let timeout
5
6
 
@@ -12,6 +13,7 @@ const resetTimeout = () => {
12
13
 
13
14
  process.on('beforeExit', (code) => {
14
15
  if (code !== 0) { return }
16
+ if (Globals.audioStreams.size === 0) { return }
15
17
 
16
18
  let duration = 0
17
19
 
@@ -21,23 +23,24 @@ process.on('beforeExit', (code) => {
21
23
  if (!stream.playing) { continue }
22
24
 
23
25
  let queued = null
24
- let device = null
25
26
  try {
27
+ Bindings.audio_flush(stream.id)
26
28
  queued = Bindings.audio_getQueued(stream.id)
27
- device = Bindings.audio_getDeviceFormat(stream.id)
28
29
  }
29
- catch (_) { }
30
- if (!queued || !device) { continue }
31
-
32
- const { channels, frequency, bytesPerSample } = stream
33
- const bytesPerSecond = channels * frequency * bytesPerSample
34
- duration = Math.max(duration, queued / bytesPerSecond + device.buffered / device.frequency)
30
+ catch { }
31
+ // Skipping empty queues misses the device buffer's tail, so if something else kept the loop alive while the queue drained, up to one device buffer gets cut off at exit. Accepted.
32
+ if (!queued) { continue }
33
+
34
+ const { device, channels, frequency, bytesPerSample, frequencyRatio } = stream
35
+ const bytesPerSecond = channels * frequency * bytesPerSample * frequencyRatio
36
+ const tail = device.frequency ? device.buffered / device.frequency : 0
37
+ duration = Math.max(duration, queued / bytesPerSecond + tail)
35
38
  }
36
39
 
37
40
  if (duration) {
38
41
  // Keeps Node.js alive while audio is playing
39
42
  resetTimeout()
40
- timeout = setTimeout(() => { timeout = null }, Math.min(duration * 1e3, 2 ** 31 - 1))
43
+ timeout = setTimeout(() => { timeout = null }, Math.min(duration * 1e3, INT32_MAX))
41
44
  }
42
45
  })
43
46
 
@@ -0,0 +1,9 @@
1
+ const { validateNonNegativeInt32 } = require('../validate')
2
+
3
+ const validateBytes = (buffer, bytes) => {
4
+ validateNonNegativeInt32('bytes', bytes)
5
+ // Attaching the whole buffer is fine: Node's inspect only prints its first 50 bytes.
6
+ if (buffer.length < bytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, bytes }) }
7
+ }
8
+
9
+ module.exports = { validateBytes }
@@ -2,10 +2,13 @@ const Bindings = require('./bindings')
2
2
  const Globals = require('./globals')
3
3
 
4
4
  process.on('exit', (code) => {
5
- if (code !== 0) { return }
6
-
7
5
  Globals.events.stopPolling()
8
6
 
7
+ if (code !== 0) {
8
+ Bindings.global_cleanup()
9
+ return
10
+ }
11
+
9
12
  const errors = []
10
13
 
11
14
  for (const window of Globals.windows.all.values()) {
@@ -18,22 +21,22 @@ process.on('exit', (code) => {
18
21
  catch (error) { errors.push(error) }
19
22
  }
20
23
 
21
- for (const joystick of Globals.joystickInstances.all.values()) {
24
+ for (const joystick of Globals.joystickInstances.all) {
22
25
  try { joystick.close() }
23
26
  catch (error) { errors.push(error) }
24
27
  }
25
28
 
26
- for (const gamepad of Globals.gamepadInstances.all.values()) {
29
+ for (const gamepad of Globals.gamepadInstances.all) {
27
30
  try { gamepad.close() }
28
31
  catch (error) { errors.push(error) }
29
32
  }
30
33
 
31
- for (const sensor of Globals.sensorInstances.all.values()) {
34
+ for (const sensor of Globals.sensorInstances.all) {
32
35
  try { sensor.close() }
33
36
  catch (error) { errors.push(error) }
34
37
  }
35
38
 
36
39
  Bindings.global_cleanup()
37
40
 
38
- if (errors.length) { throw new AggregateError(errors, "errors occurred during process exit cleanup") }
41
+ if (errors.length) { throw new AggregateError(errors, "errors occurred during exit cleanup") }
39
42
  })
@@ -1,20 +1,25 @@
1
1
  const Globals = require('../globals')
2
+ const { assertInitialized, isAvailable } = require('../subsystems')
2
3
  const Bindings = require('../bindings')
3
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
+ const { validateString } = require('../validate')
4
6
 
5
7
  const validEvents = [ 'update' ]
6
8
 
7
9
  const clipboard = new class extends EventsViaPoll {
8
- constructor () { super(validEvents) }
10
+ constructor () { super(validEvents, () => { assertInitialized('video') }) }
11
+
12
+ isAvailable () { return isAvailable('video') }
9
13
 
10
14
  get text () {
15
+ assertInitialized('video')
11
16
  Globals.events.poll()
12
17
  return Bindings.clipboard_getText()
13
18
  }
14
19
 
15
20
  setText (text) {
16
- if (typeof text !== 'string') { throw Object.assign(new Error("text must be a string"), { text }) }
17
-
21
+ assertInitialized('video')
22
+ validateString('text', text)
18
23
  Bindings.clipboard_setText(text)
19
24
  }
20
25
  }()
@@ -0,0 +1,61 @@
1
+ const Globals = require('./globals')
2
+ const { EventsViaPoll } = require('./events/events-via-poll')
3
+ const { validateNotClosed, validateObject } = require('./validate')
4
+
5
+ const lookupDevice = (requested, name, getDevices) => {
6
+ validateObject('device', requested)
7
+
8
+ Globals.events.poll()
9
+
10
+ const device = getDevices().find(({ id }) => id === requested.id)
11
+ if (!device) { throw Object.assign(new Error(`device must be from sdl.${name}.devices`), { device: requested }) }
12
+ return device
13
+ }
14
+
15
+ class DeviceInstance extends EventsViaPoll {
16
+ constructor (validEvents, device, instances, closeBinding) {
17
+ super(validEvents)
18
+
19
+ this._device = device
20
+ this._instances = instances
21
+ this._closeBinding = closeBinding
22
+
23
+ this._closed = false
24
+
25
+ instances.all.add(this)
26
+ const collection = instances.byId.get(device.id) ?? new Set()
27
+ instances.byId.set(device.id, collection)
28
+ collection.add(this)
29
+ }
30
+
31
+ get device () { return this._device }
32
+
33
+ _beforeCloseBinding () {}
34
+
35
+ get closed () { return this._closed }
36
+ close () {
37
+ validateNotClosed(this)
38
+
39
+ this._closed = true
40
+
41
+ this._instances.all.delete(this)
42
+ const collection = this._instances.byId.get(this._device.id)
43
+ collection.delete(this)
44
+ if (collection.size === 0) { this._instances.byId.delete(this._device.id) }
45
+ this._beforeCloseBinding()
46
+
47
+ // This call could throw if the device is gone
48
+ try { this._closeBinding(this._device.id) }
49
+ catch {}
50
+
51
+ // We might be inside an event listener
52
+ this._retire(() => Object.assign(new Error("instance is closed"), { id: this._device.id }))
53
+
54
+ Globals.events.withoutPolling(() => {
55
+ try { this.emit('close', { type: 'close' }) }
56
+ catch (error) { this.emit('error', error) }
57
+ })
58
+ }
59
+ }
60
+
61
+ module.exports = { DeviceInstance, lookupDevice }
@@ -1,62 +1,68 @@
1
1
  const Globals = require('../globals')
2
2
  const { EventEmitter } = require('node:events')
3
3
 
4
- let _ID = 0
5
- const activeEmitters = new Set()
4
+ let numListeners = 0
6
5
 
7
6
  const commonEvents = [ 'newListener', 'removeListener', 'error', '*' ]
7
+ const metaEvents = [ 'newListener', 'removeListener' ]
8
+ const undemandingEvents = [ ...metaEvents, 'error' ]
8
9
 
9
10
  class EventsViaPoll extends EventEmitter {
10
- constructor (validEvents) {
11
+ constructor (validEvents, assertReady = null) {
11
12
  super()
12
13
 
13
14
  this._validEvents = validEvents
14
-
15
- const id = _ID++
16
- let count = 0
15
+ this._internalListeners = new Set()
17
16
 
18
17
  // NOTE: this needs to be first, otherwise it will emit for 'newListener'
19
- this.on('removeListener', (type) => {
20
- if (type === 'newListener' || type === 'removeListener') { return }
21
-
22
- count--
23
- if (count !== 0) { return }
24
-
25
- activeEmitters.delete(id)
26
- if (activeEmitters.size !== 0) { return }
18
+ this._addInternalListener('removeListener', (type) => {
19
+ if (undemandingEvents.includes(type)) { return }
27
20
 
28
- Globals.events.switchToPollingSlow()
21
+ if (--numListeners === 0) { Globals.events.setDemand(false) }
29
22
  })
30
23
 
31
- this.on('newListener', (type) => {
24
+ this._addInternalListener('newListener', (type) => {
32
25
  if (this._makeRetiredError) { throw this._makeRetiredError() }
33
26
 
34
27
  if (!commonEvents.includes(type) && !validEvents.includes(type)) {
35
28
  throw Object.assign(new Error("invalid event"), { type })
36
29
  }
37
30
 
38
- if (type === 'newListener' || type === 'removeListener') { return }
31
+ if (undemandingEvents.includes(type)) { return }
39
32
 
40
- count++
41
- if (count !== 1) { return }
33
+ if (assertReady) { assertReady() }
42
34
 
43
- activeEmitters.add(id)
44
- if (activeEmitters.size !== 1) { return }
45
-
46
- Globals.events.switchToPollingFast()
35
+ if (++numListeners === 1) { Globals.events.setDemand(true) }
47
36
  })
48
37
  }
49
38
 
39
+ _addInternalListener (type, listener) {
40
+ this._internalListeners.add(listener)
41
+ this.on(type, listener)
42
+ }
43
+
50
44
  _retire (makeError) {
51
45
  this._makeRetiredError = makeError
52
46
  process.nextTick(() => { this.removeAllListeners() })
53
47
  }
54
48
 
55
49
  removeAllListeners (type) {
56
- const types = type !== undefined ? [ type ] : this.eventNames()
57
- for (const eventType of types) {
58
- if (eventType === 'newListener' || eventType === 'removeListener') { continue }
59
- super.removeAllListeners(eventType)
50
+ if (type === undefined) {
51
+ const types = this.eventNames().filter((name) => name !== 'removeListener')
52
+ for (const eventType of [ ...types, 'removeListener' ]) {
53
+ this.removeAllListeners(eventType)
54
+ }
55
+ return this
56
+ }
57
+
58
+ if (!metaEvents.includes(type)) {
59
+ super.removeAllListeners(type)
60
+ return this
61
+ }
62
+
63
+ for (const listener of this.listeners(type)) {
64
+ if (this._internalListeners.has(listener)) { continue }
65
+ this.removeListener(type, listener)
60
66
  }
61
67
  return this
62
68
  }