@kmamal/sdl 0.11.12 → 1.0.0-2

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 +257 -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} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  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 +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -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 +79 -42
  31. package/src/javascript/joystick/shared.js +88 -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 +20 -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 +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  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 +415 -305
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  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,88 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+
4
+ const effects = {
5
+ rumble: {
6
+ call: Bindings.joystick_rumble,
7
+ timeouts: new Map(),
8
+ },
9
+ rumbleTriggers: {
10
+ call: Bindings.joystick_rumbleTriggers,
11
+ timeouts: new Map(),
12
+ },
13
+ }
14
+
15
+ const clearEffectTimeout = (effect, id) => {
16
+ for (const timer of effect.timeouts.get(id) ?? []) { clearTimeout(timer) }
17
+ effect.timeouts.delete(id)
18
+ }
19
+
20
+ const MAX_DURATION = 0xFFFF
21
+
22
+ const startEffect = (kind, id, a, b, duration) => {
23
+ const effect = effects[kind]
24
+ a = Math.round(a * 0xFFFF)
25
+ b = Math.round(b * 0xFFFF)
26
+ effect.call(id, a, b, MAX_DURATION)
27
+
28
+ clearEffectTimeout(effect, id)
29
+
30
+ // Zero intensity stops the effect, so there is nothing to wait for
31
+ if (a === 0 && b === 0) { return }
32
+
33
+ // Keeps Node.js alive while rumbling
34
+ const timers = []
35
+ if (duration === null || duration > MAX_DURATION) {
36
+ timers.push(setInterval(() => {
37
+ try { effect.call(id, a, b, MAX_DURATION) }
38
+ catch (_) { clearEffectTimeout(effect, id) }
39
+ }, 30e3))
40
+ }
41
+ if (duration !== null) {
42
+ timers.push(setTimeout(() => {
43
+ clearEffectTimeout(effect, id)
44
+ try {
45
+ effect.call(id, 0, 0, 0)
46
+ Globals.events.poll()
47
+ }
48
+ catch (_) {}
49
+ }, duration))
50
+ }
51
+ effect.timeouts.set(id, timers)
52
+ }
53
+
54
+ const stopEffect = (kind, id) => {
55
+ const effect = effects[kind]
56
+ clearEffectTimeout(effect, id)
57
+ effect.call(id, 0, 0, 0)
58
+ Globals.events.poll()
59
+ }
60
+
61
+ const releaseDevice = (id) => {
62
+ if (Globals.joystickInstances.byId.has(id) || Globals.gamepadInstances.byId.has(id)) { return }
63
+
64
+ for (const effect of Object.values(effects)) {
65
+ if (!effect.timeouts.has(id)) { continue }
66
+
67
+ // This call will throw if the device is gone
68
+ try { effect.call(id, 0, 0, 0) }
69
+ catch (_) {}
70
+ clearEffectTimeout(effect, id)
71
+ }
72
+ }
73
+
74
+ const setPlayer = (id, player) => {
75
+ Bindings.joystick_setPlayer(id, player)
76
+ const devices = Bindings.joystick_getDevices()
77
+ for (const device of [ ...Globals.joystickDevices, ...Globals.gamepadDevices ]) {
78
+ const updated = devices.find(({ id: _id }) => _id === device.id)
79
+ if (updated) { device.player = updated.player }
80
+ }
81
+ }
82
+
83
+ module.exports = {
84
+ startEffect,
85
+ stopEffect,
86
+ releaseDevice,
87
+ setPlayer,
88
+ }
@@ -2,7 +2,7 @@ const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const Enums = require('../enums')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
- const { mapping, reverseMapping } = require('./key-mapping')
5
+ const { mapping, reverseMapping, isCharacter } = require('./key-mapping')
6
6
 
7
7
  const validEvents = [ 'keymapChange' ]
8
8
 
@@ -13,18 +13,20 @@ const keyboard = new class extends EventsViaPoll {
13
13
 
14
14
  getKey (scancode) {
15
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("invalid scancode"), { scancode }) }
16
+ if (scancode < 0 || scancode >= 512) { throw Object.assign(new Error("scancode must be between 0 and 511"), { scancode }) }
17
17
 
18
+ Globals.events.poll()
18
19
  const _key = Bindings.keyboard_getKey(scancode)
19
- return mapping[_key] ?? (_key?.length === 1 ? _key : null)
20
+ return mapping[_key] ?? (isCharacter(_key) ? _key : null)
20
21
  }
21
22
 
22
23
  getScancode (key) {
23
24
  if (typeof key !== 'string') { throw Object.assign(new Error("key must be a string"), { key }) }
24
25
 
25
- const _key = reverseMapping[key] ?? (key.length === 1 ? key : null)
26
+ const _key = reverseMapping[key] ?? (isCharacter(key) ? key : null)
26
27
  if (_key === null) { throw Object.assign(new Error("invalid key"), { key }) }
27
28
 
29
+ Globals.events.poll()
28
30
  return Bindings.keyboard_getScancode(_key)
29
31
  }
30
32
 
@@ -1,6 +1,7 @@
1
1
  const Bindings = require('../bindings')
2
2
 
3
- const mapping = {
3
+ const mapping = Object.assign(Object.create(null), {
4
+ '±': '+/-',
4
5
  'A': 'a',
5
6
  'AC Back': 'back',
6
7
  'AC Bookmarks': 'bookmarks',
@@ -9,29 +10,26 @@ const mapping = {
9
10
  'AC Refresh': 'refresh',
10
11
  'AC Search': 'search',
11
12
  'AC Stop': 'stop',
13
+ 'AC New': 'new',
14
+ 'AC Open': 'open',
15
+ 'AC Close': 'close',
16
+ 'AC Exit': 'exit',
17
+ 'AC Save': 'save',
18
+ 'AC Print': 'print',
19
+ 'AC Properties': 'properties',
12
20
  'Again': 'again',
21
+ 'Application': 'menu',
13
22
  'AltErase': 'altErase',
14
- 'App1': 'app1',
15
- 'App2': 'app2',
16
- 'Application': 'application',
17
- 'AudioFastForward': 'audioFastForward',
18
- 'AudioMute': 'audioMute',
19
- 'AudioNext': 'audioNext',
20
- 'AudioPlay': 'audioPlay',
21
- 'AudioPrev': 'audioPrev',
22
- 'AudioRewind': 'audioRewind',
23
- 'AudioStop': 'audioStop',
24
23
  'B': 'b',
25
24
  'Backspace': 'backspace',
26
- 'BrightnessDown': 'brightnessDown',
27
- 'BrightnessUp': 'brightnessUp',
28
25
  'C': 'c',
29
- 'Calculator': 'calculator',
26
+ 'Call': 'call',
30
27
  'Cancel': 'cancel',
28
+ 'ChannelDown': 'channelDown',
29
+ 'ChannelUp': 'channelUp',
31
30
  'CapsLock': 'capsLock',
32
31
  'Clear / Again': 'clear/again',
33
32
  'Clear': 'clear',
34
- 'Computer': 'computer',
35
33
  'Copy': 'copy',
36
34
  'CrSel': 'crSel',
37
35
  'CurrencySubUnit': 'currencySubUnit',
@@ -40,11 +38,11 @@ const mapping = {
40
38
  'D': 'd',
41
39
  'DecimalSeparator': 'decimalSeparator',
42
40
  'Delete': 'delete',
43
- 'DisplaySwitch': 'displaySwitch',
44
41
  'Down': 'down',
45
42
  'E': 'e',
46
43
  'Eject': 'eject',
47
44
  'End': 'end',
45
+ 'EndCall': 'endCall',
48
46
  'Escape': 'escape',
49
47
  'Execute': 'execute',
50
48
  'ExSel': 'exSel',
@@ -80,18 +78,16 @@ const mapping = {
80
78
  'Home': 'home',
81
79
  'I': 'i',
82
80
  'Insert': 'insert',
81
+ 'International 2': 'international2',
82
+ 'International 4': 'international4',
83
+ 'International 5': 'international5',
83
84
  'J': 'j',
84
85
  'K': 'k',
85
- 'KBDIllumDown': 'illumDown',
86
- 'KBDIllumToggle': 'illumToggle',
87
- 'KBDIllumUp': 'illumUp',
88
86
  'Keypad -': '-',
89
87
  'Keypad ,': ',',
90
88
  'Keypad :': ':',
91
89
  'Keypad !': '!',
92
90
  'Keypad .': '.',
93
- 'Keypad (': '(',
94
- 'Keypad )': ')',
95
91
  'Keypad {': '{',
96
92
  'Keypad }': '}',
97
93
  'Keypad @': '@',
@@ -103,13 +99,15 @@ const mapping = {
103
99
  'Keypad %': '%',
104
100
  'Keypad ^': '^',
105
101
  'Keypad +': '+',
106
- 'Keypad +/-': '+/-',
107
102
  'Keypad <': '<',
108
103
  'Keypad = (AS400)': '=',
109
104
  'Keypad =': '=',
110
105
  'Keypad >': '>',
111
106
  'Keypad |': '|',
112
107
  'Keypad ||': '||',
108
+ 'Keypad (': '(',
109
+ 'Keypad )': ')',
110
+ 'Keypad +/-': '+/-',
113
111
  'Keypad 0': '0',
114
112
  'Keypad 00': '00',
115
113
  'Keypad 000': '000',
@@ -147,16 +145,36 @@ const mapping = {
147
145
  'Keypad Tab': 'tab',
148
146
  'Keypad XOR': 'xor',
149
147
  'L': 'l',
148
+ 'Language 1': 'language1',
149
+ 'Language 2': 'language2',
150
+ 'Language 3': 'language3',
151
+ 'Language 4': 'language4',
150
152
  'Left Alt': 'alt',
153
+ 'Left Command': 'command',
151
154
  'Left Ctrl': 'ctrl',
152
155
  'Left GUI': 'gui',
156
+ 'Left Hyper': 'hyper',
157
+ 'Left Meta': 'meta',
158
+ 'Left Option': 'option',
153
159
  'Left Shift': 'shift',
160
+ 'Left Windows': 'windows',
161
+ 'LeftTab': 'leftTab',
154
162
  'Left': 'left',
163
+ 'Level5Shift': 'modeSwitch',
155
164
  'M': 'm',
156
- 'Mail': 'mail',
165
+ 'MediaFastForward': 'mediaFastForward',
166
+ 'MediaPause': 'mediaPause',
167
+ 'MediaPlay': 'mediaPlay',
168
+ 'MediaPlayPause': 'mediaPlayPause',
169
+ 'MediaRecord': 'mediaRecord',
170
+ 'MediaRewind': 'mediaRewind',
157
171
  'MediaSelect': 'mediaSelect',
172
+ 'MediaStop': 'mediaStop',
173
+ 'MediaTrackNext': 'mediaTrackNext',
174
+ 'MediaTrackPrevious': 'mediaTrackPrevious',
158
175
  'Menu': 'menu',
159
176
  'ModeSwitch': 'modeSwitch',
177
+ 'MultiKeyCompose': 'compose',
160
178
  'Mute': 'mute',
161
179
  'N': 'n',
162
180
  'Numlock': 'numlock',
@@ -175,16 +193,23 @@ const mapping = {
175
193
  'R': 'r',
176
194
  'Return': 'return',
177
195
  'Right Alt': 'alt',
196
+ 'Right Command': 'command',
178
197
  'Right Ctrl': 'ctrl',
179
- 'Right GUI': 'gUI',
198
+ 'Right GUI': 'gui',
199
+ 'Right Hyper': 'hyper',
200
+ 'Right Meta': 'meta',
201
+ 'Right Option': 'option',
180
202
  'Right Shift': 'shift',
203
+ 'Right Windows': 'windows',
181
204
  'Right': 'right',
182
205
  'S': 's',
183
206
  'ScrollLock': 'scrollLock',
184
207
  'Select': 'select',
185
208
  'Separator': 'separator',
186
209
  'Sleep': 'sleep',
187
- 'Space': 'space',
210
+ 'SoftLeft': 'softLeft',
211
+ 'SoftRight': 'softRight',
212
+ 'Space': ' ',
188
213
  'Stop': 'stop',
189
214
  'SysReq': 'sysReq',
190
215
  'T': 't',
@@ -196,14 +221,16 @@ const mapping = {
196
221
  'V': 'v',
197
222
  'VolumeDown': 'volumeDown',
198
223
  'VolumeUp': 'volumeUp',
224
+ 'Wake': 'wake',
199
225
  'W': 'w',
200
- 'WWW': 'www',
201
226
  'X': 'x',
202
227
  'Y': 'y',
203
228
  'Z': 'z',
204
- }
229
+ })
205
230
 
206
- const reverseMapping = {}
231
+ const reverseMapping = Object.create(null)
232
+
233
+ const isCharacter = (key) => key !== null && [ ...key ].length === 1
207
234
 
208
235
  for (const [ key, value ] of Object.entries(mapping)) {
209
236
  maybeSkip: {
@@ -222,7 +249,22 @@ for (const [ key, value ] of Object.entries(mapping)) {
222
249
  reverseMapping[value] = key
223
250
  }
224
251
 
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 }
256
+
257
+ const valueScancode = Bindings.keyboard_getScancode(value)
258
+ if (valueScancode === null) { continue }
259
+
260
+ const keyScancode = Bindings.keyboard_getScancode(key)
261
+ if (keyScancode === null || valueScancode < keyScancode) {
262
+ delete reverseMapping[value]
263
+ }
264
+ }
265
+
225
266
  module.exports = {
226
267
  mapping,
227
268
  reverseMapping,
269
+ isCharacter,
228
270
  }
@@ -1,21 +1,26 @@
1
+ const Globals = require('../globals')
1
2
  const Bindings = require('../bindings')
2
3
  const Enums = require('../enums')
4
+ const { VideoFormatHelpers } = require('../video/format-helpers')
5
+
6
+ let captured = false
3
7
 
4
8
  const mouse = {
5
9
  get BUTTON () { return Enums.mouseButtons },
6
10
 
7
11
  getButton (button) {
8
12
  if (!Number.isInteger(button)) { throw Object.assign(new Error("button must be an integer"), { button }) }
9
- if (button < 0 || button >= 32) { throw Object.assign(new Error("invalid button"), { button }) }
13
+ if (button < 1 || button > 32) { throw Object.assign(new Error("button must be between 1 and 32"), { button }) }
10
14
 
15
+ Globals.events.poll()
11
16
  return Bindings.mouse_getButton(button)
12
17
  },
13
18
 
14
19
  get position () { return Bindings.mouse_getPosition() },
15
20
 
16
21
  setPosition (x, y) {
17
- if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
18
- if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
22
+ if (!Number.isFinite(x)) { throw Object.assign(new Error("x must be a number"), { x }) }
23
+ if (!Number.isFinite(y)) { throw Object.assign(new Error("y must be a number"), { y }) }
19
24
 
20
25
  Bindings.mouse_setPosition(x, y)
21
26
  },
@@ -33,21 +38,24 @@ const mouse = {
33
38
 
34
39
  setCursorImage (width, height, stride, format, buffer, x, y) {
35
40
  if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
36
- if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
41
+ if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
37
42
  if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
38
- if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
43
+ if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
39
44
  if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
40
- if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
41
45
  if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
46
+ const helpers = VideoFormatHelpers[format]
47
+ if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
48
+ if (helpers.isYuv) { throw Object.assign(new Error("format must be an RGB format"), { format }) }
49
+ if (stride < helpers.minStride(width)) { throw Object.assign(new Error("stride is smaller than expected"), { stride, width, format }) }
50
+ if (stride > 2 ** 31 - 1) { throw Object.assign(new Error("stride must be a 32-bit integer"), { stride }) }
42
51
  if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
43
- if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
52
+ if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
44
53
  if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
45
- if (x < 0 || x >= width) { throw Object.assign(new Error("invalid x"), { x }) }
54
+ if (x < 0 || x >= width) { throw Object.assign(new Error("x must be between 0 and width - 1"), { x, width }) }
46
55
  if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
47
- if (y < 0 || y >= height) { throw Object.assign(new Error("invalid y"), { y }) }
56
+ if (y < 0 || y >= height) { throw Object.assign(new Error("y must be between 0 and height - 1"), { y, height }) }
48
57
 
49
58
  const _format = Enums.pixelFormat[format]
50
- if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
51
59
 
52
60
  Bindings.mouse_setCursorImage(width, height, stride, _format, buffer, x, y)
53
61
  },
@@ -62,10 +70,13 @@ const mouse = {
62
70
 
63
71
  redrawCursor () { Bindings.mouse_redrawCursor() },
64
72
 
73
+ get captured () { return captured },
74
+
65
75
  capture (capture = true) {
66
76
  if (typeof capture !== 'boolean') { throw Object.assign(new Error("capture must be a boolean"), { capture }) }
67
77
 
68
78
  Bindings.mouse_capture(capture)
79
+ captured = capture
69
80
  },
70
81
 
71
82
  uncapture () { mouse.capture(false) },
@@ -0,0 +1,17 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+
4
+ const refreshDevices = () => {
5
+ const devices = Bindings.sensor_getDevices()
6
+
7
+ const oldDevices = Globals.sensorDevices
8
+ Globals.sensorDevices = devices.map((sensorDevice) => {
9
+ const oldDevice = oldDevices.find(({ id }) => id === sensorDevice.id)
10
+ return oldDevice
11
+ ? Object.assign(oldDevice, sensorDevice)
12
+ : sensorDevice
13
+ })
14
+ return Globals.sensorDevices
15
+ }
16
+
17
+ module.exports = { refreshDevices }
@@ -1,25 +1,11 @@
1
- const Bindings = require('../bindings')
1
+ const { refreshDevices } = require('./devices')
2
2
  const { SensorInstance } = require('./sensor-instance')
3
3
 
4
4
 
5
5
  const sensor = {
6
6
  STANDARD_GRAVITY: 9.80665,
7
7
 
8
- get devices () {
9
- const devices = Bindings.sensor_getDevices()
10
- for (const sensorDevice of devices) {
11
- const { type } = sensorDevice
12
- sensorDevice.type
13
- = type.startsWith('accelerometer') ? 'accelerometer'
14
- : type.startsWith('gyroscope') ? 'gyroscope'
15
- : 'unknown'
16
- sensorDevice.side
17
- = type.endsWith('Left') ? 'left'
18
- : type.endsWith('Right') ? 'right'
19
- : null
20
- }
21
- return devices
22
- },
8
+ get devices () { return [ ...refreshDevices() ] },
23
9
 
24
10
  openDevice (device) { return new SensorInstance(device) },
25
11
  }
@@ -1,5 +1,6 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
+ const { refreshDevices } = require('./devices')
3
4
  const { EventsViaPoll } = require('../events/events-via-poll')
4
5
 
5
6
  const validEvents = [
@@ -11,9 +12,10 @@ class SensorInstance extends EventsViaPoll {
11
12
  constructor (device) {
12
13
  super(validEvents)
13
14
 
14
- if (!Globals.sensorDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
15
+ refreshDevices()
16
+ if (!Globals.sensorDevices.includes(device)) { throw Object.assign(new Error("device must be from sdl.sensor.devices"), { device }) }
15
17
 
16
- Bindings.sensor_open(device._index)
18
+ Bindings.sensor_open(device.id)
17
19
 
18
20
  this._device = device
19
21
 
@@ -30,23 +32,35 @@ class SensorInstance extends EventsViaPoll {
30
32
 
31
33
  get device () { return this._device }
32
34
 
33
- get data () { return Bindings.sensor_getData(this._device.id) }
35
+ get data () {
36
+ Globals.events.poll()
37
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
38
+
39
+ return Bindings.sensor_getData(this._device.id)
40
+ }
34
41
 
35
42
  get closed () { return this._closed }
36
43
  close () {
37
44
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
38
45
 
39
- this.emit('close')
40
- this.removeAllListeners()
41
46
  this._closed = true
42
47
 
43
48
  Globals.sensorInstances.all.delete(this)
44
49
  const collection = Globals.sensorInstances.byId.get(this._device.id)
45
50
  collection.delete(this)
46
51
  if (collection.size === 0) {
47
- Bindings.sensor_close(this._device.id)
48
52
  Globals.sensorInstances.byId.delete(this._device.id)
49
53
  }
54
+
55
+ // This call could throw if the device is gone
56
+ try { Bindings.sensor_close(this._device.id) }
57
+ catch (_) {}
58
+
59
+ // We might be inside an event listener
60
+ this._retire(() => Object.assign(new Error("instance is closed"), { id: this._device.id }))
61
+
62
+ try { this.emit('close', { type: 'close' }) }
63
+ catch (error) { this.emit('error', error) }
50
64
  }
51
65
  }
52
66
 
@@ -0,0 +1,17 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+
4
+ const refreshDevices = () => {
5
+ const devices = Bindings.touch_getDevices()
6
+
7
+ const oldDevices = Globals.touchDevices
8
+ Globals.touchDevices = devices.map((touchDevice) => {
9
+ const oldDevice = oldDevices.find(({ id }) => id === touchDevice.id)
10
+ return oldDevice
11
+ ? Object.assign(oldDevice, touchDevice)
12
+ : touchDevice
13
+ })
14
+ return Globals.touchDevices
15
+ }
16
+
17
+ module.exports = { refreshDevices }
@@ -1,15 +1,11 @@
1
- const Globals = require('../globals')
2
- const Bindings = require('../bindings')
1
+ const { refreshDevices } = require('./devices')
3
2
 
4
3
 
5
- Globals.touchDevices = Bindings.touch_getDevices()
4
+ refreshDevices()
6
5
 
7
6
 
8
7
  const touch = {
9
- get devices () {
10
- Globals.events.poll()
11
- return Globals.touchDevices
12
- },
8
+ get devices () { return [ ...refreshDevices() ] },
13
9
  }
14
10
 
15
11
  module.exports = { touch }
@@ -0,0 +1,99 @@
1
+
2
+ const minStride = (bytesPerPixel) => (width) => width * bytesPerPixel
3
+ const minStridePacked = (width) => 4 * Math.ceil(width / 2)
4
+ const minBufferSize = (stride, height) => stride * height
5
+ const minBufferSizePlanar = (stride, height) => stride * height + 2 * Math.ceil(stride / 2) * Math.ceil(height / 2)
6
+
7
+
8
+ const rgb = (bytesPerPixel) => ({
9
+ bytesPerPixel,
10
+ isYuv: false,
11
+ isPlanarYuv: false,
12
+ isRenderable: true,
13
+ minStride: minStride(bytesPerPixel),
14
+ minBufferSize,
15
+ })
16
+
17
+ const yuv = (bytesPerPixel, isPlanarYuv, isRenderable = true) => ({
18
+ bytesPerPixel,
19
+ isYuv: true,
20
+ isPlanarYuv,
21
+ isRenderable,
22
+ minStride: isPlanarYuv ? minStride(bytesPerPixel) : minStridePacked,
23
+ minBufferSize: isPlanarYuv ? minBufferSizePlanar : minBufferSize,
24
+ })
25
+
26
+ const VideoFormatHelpers = Object.assign(Object.create(null), {
27
+ rgb332: rgb(1),
28
+ xrgb4444: rgb(2),
29
+ xbgr4444: rgb(2),
30
+ xrgb1555: rgb(2),
31
+ xbgr1555: rgb(2),
32
+ argb4444: rgb(2),
33
+ rgba4444: rgb(2),
34
+ abgr4444: rgb(2),
35
+ bgra4444: rgb(2),
36
+ argb1555: rgb(2),
37
+ rgba5551: rgb(2),
38
+ abgr1555: rgb(2),
39
+ bgra5551: rgb(2),
40
+ rgb565: rgb(2),
41
+ bgr565: rgb(2),
42
+ rgb24: rgb(3),
43
+ bgr24: rgb(3),
44
+ xrgb8888: rgb(4),
45
+ rgbx8888: rgb(4),
46
+ xbgr8888: rgb(4),
47
+ bgrx8888: rgb(4),
48
+ argb8888: rgb(4),
49
+ rgba8888: rgb(4),
50
+ abgr8888: rgb(4),
51
+ bgra8888: rgb(4),
52
+ argb2101010: rgb(4),
53
+ xrgb2101010: rgb(4),
54
+ xbgr2101010: rgb(4),
55
+ abgr2101010: rgb(4),
56
+ rgb48: rgb(6),
57
+ bgr48: rgb(6),
58
+ rgba64: rgb(8),
59
+ argb64: rgb(8),
60
+ bgra64: rgb(8),
61
+ abgr64: rgb(8),
62
+ rgb48f: rgb(6),
63
+ bgr48f: rgb(6),
64
+ rgba64f: rgb(8),
65
+ argb64f: rgb(8),
66
+ bgra64f: rgb(8),
67
+ abgr64f: rgb(8),
68
+ rgb96f: rgb(12),
69
+ bgr96f: rgb(12),
70
+ rgba128f: rgb(16),
71
+ argb128f: rgb(16),
72
+ bgra128f: rgb(16),
73
+ abgr128f: rgb(16),
74
+ rgba32: rgb(4),
75
+ argb32: rgb(4),
76
+ bgra32: rgb(4),
77
+ abgr32: rgb(4),
78
+ rgbx32: rgb(4),
79
+ xrgb32: rgb(4),
80
+ bgrx32: rgb(4),
81
+ xbgr32: rgb(4),
82
+ yv12: yuv(1, true),
83
+ iyuv: yuv(1, true),
84
+ yuy2: yuv(2, false),
85
+ uyvy: yuv(2, false),
86
+ yvyu: yuv(2, false),
87
+ nv12: yuv(1, true),
88
+ nv21: yuv(1, true),
89
+ p010: { ...yuv(2, true, false), minStride: minStridePacked },
90
+ })
91
+
92
+ const getFormatHelpers = (format) => {
93
+ if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
94
+ const helpers = VideoFormatHelpers[format]
95
+ if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
96
+ return helpers
97
+ }
98
+
99
+ module.exports = { VideoFormatHelpers, getFormatHelpers }