@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.
Files changed (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -0,0 +1,176 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { DeviceInstance } = require('../device-instance')
4
+ const { validateNotClosed, validateNumberRange, validateNonNegativeInt32 } = require('../validate')
5
+ const { refreshAndBroadcast } = require('./devices')
6
+
7
+ const MAX_DURATION = 0xFFFF
8
+
9
+ // Timers are kept per device id at module level, not per instance, since every joystick and gamepad instance of a device drives the same motors.
10
+ const effects = {
11
+ rumble: {
12
+ call: Bindings.joystick_rumble,
13
+ capability: 'hasRumble',
14
+ missing: "device has no rumble",
15
+ timers: new Map(),
16
+ },
17
+ rumbleTriggers: {
18
+ call: Bindings.joystick_rumbleTriggers,
19
+ capability: 'hasRumbleTriggers',
20
+ missing: "device has no trigger rumble",
21
+ timers: new Map(),
22
+ },
23
+ }
24
+
25
+ const validateUnit = (name, value) => { validateNumberRange(name, value, 0, 1) }
26
+
27
+ const clearTimers = (effect, id) => {
28
+ for (const timer of effect.timers.get(id) ?? []) { clearTimeout(timer) }
29
+ effect.timers.delete(id)
30
+ }
31
+
32
+ const stopEffect = (effect, id) => {
33
+ clearTimers(effect, id)
34
+ effect.call(id, 0, 0, 0)
35
+ }
36
+
37
+ const startEffect = (effect, id, _a, _b, duration) => {
38
+ const a = Math.round(_a * 0xFFFF)
39
+ const b = Math.round(_b * 0xFFFF)
40
+ if (duration === 0 || (a === 0 && b === 0)) {
41
+ stopEffect(effect, id)
42
+ Globals.events.poll()
43
+ return
44
+ }
45
+
46
+ clearTimers(effect, id)
47
+ effect.call(id, a, b, duration === null ? MAX_DURATION : Math.min(duration, MAX_DURATION))
48
+
49
+ // Keeps Node.js alive while rumbling
50
+ // Not unref'd on purpose, so that the process stays alive until the effect finishes
51
+ const timers = []
52
+ if (duration === null || duration > MAX_DURATION) {
53
+ timers.push(setInterval(() => {
54
+ try { effect.call(id, a, b, MAX_DURATION) }
55
+ catch { clearTimers(effect, id) }
56
+ }, 30e3))
57
+ }
58
+ if (duration !== null) {
59
+ timers.push(setTimeout(() => {
60
+ try {
61
+ stopEffect(effect, id)
62
+ Globals.events.poll()
63
+ }
64
+ catch {}
65
+ }, duration))
66
+ }
67
+ effect.timers.set(id, timers)
68
+ }
69
+
70
+ const releaseDevice = (id) => {
71
+ if (Globals.joystickInstances.byId.has(id) || Globals.gamepadInstances.byId.has(id)) { return }
72
+
73
+ for (const effect of Object.values(effects)) {
74
+ if (!effect.timers.has(id)) { continue }
75
+
76
+ // This call will throw if the device is gone
77
+ try { stopEffect(effect, id) }
78
+ catch {}
79
+ }
80
+ }
81
+
82
+ class JoystickDeviceInstance extends DeviceInstance {
83
+ get firmwareVersion () { return this._firmwareVersion }
84
+ get serialNumber () { return this._serialNumber }
85
+
86
+ get power () {
87
+ Globals.events.poll()
88
+ validateNotClosed(this)
89
+
90
+ return this._power
91
+ }
92
+
93
+ setPlayer (player) {
94
+ validateNotClosed(this)
95
+
96
+ // SDL sizes its player array to player + 1 and zero-fills it (overflowing at the max); that's an SDL issue, not ours to cap.
97
+ validateNonNegativeInt32('player', player)
98
+
99
+ this._setPlayer(player)
100
+ }
101
+
102
+ resetPlayer () {
103
+ validateNotClosed(this)
104
+
105
+ this._setPlayer(-1)
106
+ }
107
+
108
+ _setPlayer (player) {
109
+ Bindings.joystick_setPlayer(this._device.id, player)
110
+ refreshAndBroadcast()
111
+ }
112
+
113
+ _hasCapability (name) {
114
+ Globals.events.poll()
115
+ validateNotClosed(this)
116
+
117
+ return Bindings.joystick_getCapabilities(this._device.id)[name]
118
+ }
119
+
120
+ get hasLed () { return this._hasCapability('hasLed') }
121
+ get hasRumble () { return this._hasCapability('hasRumble') }
122
+ get hasRumbleTriggers () { return this._hasCapability('hasRumbleTriggers') }
123
+
124
+ setLed (red, green, blue) {
125
+ validateNotClosed(this)
126
+
127
+ validateUnit('red', red)
128
+ validateUnit('green', green)
129
+ validateUnit('blue', blue)
130
+
131
+ if (!this.hasLed) { throw Object.assign(new Error("device has no led"), { id: this._device.id }) }
132
+
133
+ Bindings.joystick_setLed(this._device.id, red, green, blue)
134
+ }
135
+
136
+ _getEffect (kind) {
137
+ const effect = effects[kind]
138
+ if (!this[effect.capability]) { throw Object.assign(new Error(effect.missing), { id: this._device.id }) }
139
+ return effect
140
+ }
141
+
142
+ _startEffect (kind, nameA, _a, nameB, _b, duration) {
143
+ validateNotClosed(this)
144
+
145
+ const a = _a === undefined ? 1 : _a
146
+ const b = _b === undefined ? 1 : _b
147
+ validateUnit(nameA, a)
148
+ validateUnit(nameB, b)
149
+ if (duration !== undefined) { validateNonNegativeInt32('duration', duration) }
150
+
151
+ startEffect(this._getEffect(kind), this._device.id, a, b, duration ?? null)
152
+ }
153
+
154
+ _stopEffect (kind) {
155
+ validateNotClosed(this)
156
+
157
+ stopEffect(this._getEffect(kind), this._device.id)
158
+ Globals.events.poll()
159
+ }
160
+
161
+ rumble (lowFreqRumble, highFreqRumble, duration) {
162
+ this._startEffect('rumble', 'lowFreqRumble', lowFreqRumble, 'highFreqRumble', highFreqRumble, duration)
163
+ }
164
+
165
+ stopRumble () { this._stopEffect('rumble') }
166
+
167
+ rumbleTriggers (leftRumble, rightRumble, duration) {
168
+ this._startEffect('rumbleTriggers', 'leftRumble', leftRumble, 'rightRumble', rightRumble, duration)
169
+ }
170
+
171
+ stopRumbleTriggers () { this._stopEffect('rumbleTriggers') }
172
+
173
+ _beforeCloseBinding () { releaseDevice(this._device.id) }
174
+ }
175
+
176
+ module.exports = { JoystickDeviceInstance }
@@ -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('./shared')
3
+ const { validateNotClosed } = require('../validate')
4
+ const { lookupDevice } = require('../device-instance')
5
+ const { JoystickDeviceInstance } = require('./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 JoystickInstance extends EventsViaPoll {
17
- constructor (device) {
18
- super(validEvents)
19
-
20
- Globals.events.poll()
21
- if (!Globals.joystickDevices.includes(device)) { throw Object.assign(new Error("device must be from sdl.joystick.devices"), { device }) }
17
+ class JoystickInstance extends JoystickDeviceInstance {
18
+ constructor (requested) {
19
+ const device = lookupDevice(requested, 'joystick', () => Globals.joystickDevices)
22
20
 
23
21
  const result = Bindings.joystick_open(device.id)
24
22
 
23
+ super(validEvents, device, Globals.joystickInstances, Bindings.joystick_close)
24
+
25
25
  this._firmwareVersion = result.firmwareVersion
26
26
  this._serialNumber = result.serialNumber
27
27
  this._axes = result.axes
@@ -29,181 +29,35 @@ class JoystickInstance extends EventsViaPoll {
29
29
  this._buttons = result.buttons
30
30
  this._hats = result.hats
31
31
  this._power = result.power
32
-
33
- this._device = device
34
-
35
- this._closed = false
36
-
37
- Globals.joystickInstances.all.add(this)
38
- let collection = Globals.joystickInstances.byId.get(this._device.id)
39
- if (!collection) {
40
- collection = new Set()
41
- Globals.joystickInstances.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 axes () {
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._axes
55
39
  }
56
40
 
57
41
  get balls () {
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._balls
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 hats () {
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._hats
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.joystickInstances.all.delete(this)
190
- const collection = Globals.joystickInstances.byId.get(this._device.id)
191
- collection.delete(this)
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
- })
206
- }
207
61
  }
208
62
 
209
63
  module.exports = { JoystickInstance }
@@ -1,36 +1,46 @@
1
1
  const Globals = require('../globals')
2
+ const { assertInitialized, isAvailable } = require('../subsystems')
2
3
  const Bindings = require('../bindings')
3
4
  const Enums = require('../enums')
4
5
  const { EventsViaPoll } = require('../events/events-via-poll')
5
- const { mapping, reverseMapping, isCharacter } = require('./key-mapping')
6
+ const { mapKey, getReverseMapping, isCharacter } = require('./key-mapping')
7
+ const { validateIntegerRange, validateString } = require('../validate')
6
8
 
7
9
  const validEvents = [ 'keymapChange' ]
8
10
 
9
11
  const keyboard = new class extends EventsViaPoll {
10
- constructor () { super(validEvents) }
12
+ constructor () { super(validEvents, () => { assertInitialized('video') }) }
13
+
14
+ isAvailable () { return isAvailable('video') }
11
15
 
12
16
  get SCANCODE () { return Enums.scancodes }
13
17
 
14
18
  getKey (scancode) {
15
- if (!Number.isInteger(scancode)) { throw Object.assign(new Error("scancode must be an integer"), { scancode }) }
16
- if (scancode < 0 || scancode >= 512) { throw Object.assign(new Error("scancode must be between 0 and 511"), { scancode }) }
19
+ assertInitialized('video')
20
+ validateIntegerRange('scancode', scancode, 0, 511)
17
21
 
18
22
  Globals.events.poll()
19
- const _key = Bindings.keyboard_getKey(scancode)
20
- return mapping[_key] ?? (isCharacter(_key) ? _key : null)
23
+ return mapKey(Bindings.keyboard_getKey(scancode))
21
24
  }
22
25
 
23
26
  getScancode (key) {
24
- if (typeof key !== 'string') { throw Object.assign(new Error("key must be a string"), { key }) }
27
+ assertInitialized('video')
28
+ validateString('key', key)
25
29
 
26
- const _key = reverseMapping[key] ?? (isCharacter(key) ? key : null)
30
+ Globals.events.poll()
31
+ const isChar = isCharacter(key)
32
+ const _key = isChar ? key : getReverseMapping()[key] ?? null
27
33
  if (_key === null) { throw Object.assign(new Error("invalid key"), { key }) }
28
34
 
29
- Globals.events.poll()
30
- return Bindings.keyboard_getScancode(_key)
35
+ const scancode = Bindings.keyboard_getScancode(_key)
36
+ if (scancode === null) { return null }
37
+ if (!isChar && mapKey(Bindings.keyboard_getKey(scancode)) !== key) { return null }
38
+
39
+ return scancode
31
40
  }
32
41
 
33
42
  getState () {
43
+ assertInitialized('video')
34
44
  Globals.events.poll()
35
45
  return Bindings.keyboard_getState()
36
46
  }
@@ -1,5 +1,6 @@
1
1
  const Bindings = require('../bindings')
2
2
 
3
+ // Some entries look unreachable but aren't: X11 and Wayland fall back to scancode names like 'A' and 'Space' for keys without a character, and macOS and Windows rename modifiers to 'Left Command', 'Left Option' and 'Left Windows'.
3
4
  const mapping = Object.assign(Object.create(null), {
4
5
  '±': '+/-',
5
6
  'A': 'a',
@@ -72,15 +73,22 @@ const mapping = Object.assign(Object.create(null), {
72
73
  'F8': 'f8',
73
74
  'F9': 'f9',
74
75
  'Find': 'find',
76
+ 'Front': 'front',
75
77
  'G': 'g',
76
78
  'H': 'h',
77
79
  'Help': 'help',
78
80
  'Home': 'home',
79
81
  'I': 'i',
80
82
  'Insert': 'insert',
83
+ 'International 1': 'international1',
81
84
  'International 2': 'international2',
85
+ 'International 3': 'international3',
82
86
  'International 4': 'international4',
83
87
  'International 5': 'international5',
88
+ 'International 6': 'international6',
89
+ 'International 7': 'international7',
90
+ 'International 8': 'international8',
91
+ 'International 9': 'international9',
84
92
  'J': 'j',
85
93
  'K': 'k',
86
94
  'Keypad -': '-',
@@ -130,7 +138,7 @@ const mapping = Object.assign(Object.create(null), {
130
138
  'Keypad D': 'd',
131
139
  'Keypad Decimal': 'decimal',
132
140
  'Keypad E': 'e',
133
- 'Keypad Enter': 'enter',
141
+ 'Keypad Enter': 'return',
134
142
  'Keypad F': 'f',
135
143
  'Keypad Hexadecimal': 'hexadecimal',
136
144
  'Keypad MemAdd': 'memAdd',
@@ -149,6 +157,11 @@ const mapping = Object.assign(Object.create(null), {
149
157
  'Language 2': 'language2',
150
158
  'Language 3': 'language3',
151
159
  'Language 4': 'language4',
160
+ 'Language 5': 'language5',
161
+ 'Language 6': 'language6',
162
+ 'Language 7': 'language7',
163
+ 'Language 8': 'language8',
164
+ 'Language 9': 'language9',
152
165
  'Left Alt': 'alt',
153
166
  'Left Command': 'command',
154
167
  'Left Ctrl': 'ctrl',
@@ -160,7 +173,7 @@ const mapping = Object.assign(Object.create(null), {
160
173
  'Left Windows': 'windows',
161
174
  'LeftTab': 'leftTab',
162
175
  'Left': 'left',
163
- 'Level5Shift': 'modeSwitch',
176
+ 'Level5Shift': 'level5Shift',
164
177
  'M': 'm',
165
178
  'MediaFastForward': 'mediaFastForward',
166
179
  'MediaPause': 'mediaPause',
@@ -177,6 +190,7 @@ const mapping = Object.assign(Object.create(null), {
177
190
  'MultiKeyCompose': 'compose',
178
191
  'Mute': 'mute',
179
192
  'N': 'n',
193
+ 'NonUSBackslash': 'nonUsBackslash',
180
194
  'Numlock': 'numlock',
181
195
  'O': 'o',
182
196
  'Oper': 'oper',
@@ -188,7 +202,7 @@ const mapping = Object.assign(Object.create(null), {
188
202
  'Pause': 'pause',
189
203
  'Power': 'power',
190
204
  'PrintScreen': 'printScreen',
191
- 'Prior': 'prior',
205
+ 'Prior': 'pageUp',
192
206
  'Q': 'q',
193
207
  'R': 'r',
194
208
  'Return': 'return',
@@ -228,43 +242,48 @@ const mapping = Object.assign(Object.create(null), {
228
242
  'Z': 'z',
229
243
  })
230
244
 
231
- const reverseMapping = Object.create(null)
232
-
233
245
  const isCharacter = (key) => key !== null && [ ...key ].length === 1
234
246
 
235
- for (const [ key, value ] of Object.entries(mapping)) {
236
- maybeSkip: {
237
- const existing = reverseMapping[value]
238
- if (existing === undefined) { break maybeSkip }
247
+ const mapKey = (key) => mapping[key] ?? (isCharacter(key) ? key : null)
239
248
 
240
- const keyScancode = Bindings.keyboard_getScancode(key)
241
- if (keyScancode === null) { continue }
249
+ const buildReverseMapping = () => {
250
+ const reverseMapping = Object.create(null)
242
251
 
243
- const existingScancode = Bindings.keyboard_getScancode(existing)
244
- if (existingScancode === null) { break maybeSkip }
252
+ for (const [ key, value ] of Object.entries(mapping)) {
253
+ if (isCharacter(value)) { continue }
245
254
 
246
- if (existingScancode < keyScancode) { continue }
247
- }
255
+ maybeSkip: {
256
+ const existing = reverseMapping[value]
257
+ if (existing === undefined) { break maybeSkip }
248
258
 
249
- reverseMapping[value] = key
250
- }
259
+ const keyScancode = Bindings.keyboard_getScancode(key)
260
+ if (keyScancode === null) { continue }
251
261
 
252
- // Single-character keys are valid SDL key names.
253
- // Prefer bare key when it has a lower scancode.
254
- for (const [ value, key ] of Object.entries(reverseMapping)) {
255
- if (!isCharacter(value)) { continue }
262
+ const existingScancode = Bindings.keyboard_getScancode(existing)
263
+ if (existingScancode === null) { break maybeSkip }
256
264
 
257
- const valueScancode = Bindings.keyboard_getScancode(value)
258
- if (valueScancode === null) { continue }
265
+ if (existingScancode < keyScancode) { continue }
266
+ }
259
267
 
260
- const keyScancode = Bindings.keyboard_getScancode(key)
261
- if (keyScancode === null || valueScancode < keyScancode) {
262
- delete reverseMapping[value]
268
+ reverseMapping[value] = key
263
269
  }
270
+
271
+ return reverseMapping
264
272
  }
265
273
 
274
+ let reverseMapping = null
275
+
276
+ const getReverseMapping = () => {
277
+ reverseMapping ??= buildReverseMapping()
278
+ return reverseMapping
279
+ }
280
+
281
+ const invalidateReverseMapping = () => { reverseMapping = null }
282
+
266
283
  module.exports = {
267
284
  mapping,
268
- reverseMapping,
285
+ mapKey,
286
+ getReverseMapping,
287
+ invalidateReverseMapping,
269
288
  isCharacter,
270
289
  }