@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,32 +1,41 @@
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
- const { VideoFormatHelpers } = require('../video/format-helpers')
5
+ const { validateImage, validateRgbFormat } = require('../video/validate')
6
+ const { validateBoolean, validateIntegerRange, validateFloat, validateString } = require('../validate')
5
7
 
6
8
  let captured = false
7
9
 
8
10
  const mouse = {
11
+ isAvailable () { return isAvailable('video') },
12
+
9
13
  get BUTTON () { return Enums.mouseButtons },
10
14
 
11
15
  getButton (button) {
12
- if (!Number.isInteger(button)) { throw Object.assign(new Error("button must be an integer"), { button }) }
13
- if (button < 1 || button > 32) { throw Object.assign(new Error("button must be between 1 and 32"), { button }) }
16
+ assertInitialized('video')
17
+ validateIntegerRange('button', button, 1, 32)
14
18
 
15
19
  Globals.events.poll()
16
20
  return Bindings.mouse_getButton(button)
17
21
  },
18
22
 
19
- get position () { return Bindings.mouse_getPosition() },
23
+ get position () {
24
+ assertInitialized('video')
25
+ return Bindings.mouse_getPosition()
26
+ },
20
27
 
21
28
  setPosition (x, 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 }) }
29
+ assertInitialized('video')
30
+ validateFloat('x', x)
31
+ validateFloat('y', y)
24
32
 
25
33
  Bindings.mouse_setPosition(x, y)
26
34
  },
27
35
 
28
36
  setCursor (cursor) {
29
- if (typeof cursor !== 'string') { throw Object.assign(new Error("cursor must be a string"), { cursor }) }
37
+ assertInitialized('video')
38
+ validateString('cursor', cursor)
30
39
 
31
40
  const _cursor = Enums.cursors[cursor]
32
41
  if (_cursor === undefined) { throw Object.assign(new Error("invalid cursor"), { cursor }) }
@@ -34,26 +43,16 @@ const mouse = {
34
43
  Bindings.mouse_setCursor(_cursor)
35
44
  },
36
45
 
37
- resetCursor () { Bindings.mouse_resetCursor() },
46
+ resetCursor () {
47
+ assertInitialized('video')
48
+ Bindings.mouse_resetCursor()
49
+ },
38
50
 
39
51
  setCursorImage (width, height, stride, format, buffer, x, y) {
40
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
41
- if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
42
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
43
- if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
44
- if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
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 }) }
51
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
52
- if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
53
- if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
54
- if (x < 0 || x >= width) { throw Object.assign(new Error("x must be between 0 and width - 1"), { x, width }) }
55
- if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
56
- if (y < 0 || y >= height) { throw Object.assign(new Error("y must be between 0 and height - 1"), { y, height }) }
52
+ assertInitialized('video')
53
+ validateImage(width, height, stride, format, buffer, validateRgbFormat)
54
+ validateIntegerRange('x', x, 0, width - 1, "must be between 0 and width - 1")
55
+ validateIntegerRange('y', y, 0, height - 1, "must be between 0 and height - 1")
57
56
 
58
57
  const _format = Enums.pixelFormat[format]
59
58
 
@@ -61,19 +60,27 @@ const mouse = {
61
60
  },
62
61
 
63
62
  showCursor (show = true) {
64
- if (typeof show !== 'boolean') { throw Object.assign(new Error("show must be a boolean"), { show }) }
63
+ assertInitialized('video')
64
+ validateBoolean('show', show)
65
65
 
66
66
  Bindings.mouse_showCursor(show)
67
67
  },
68
68
 
69
69
  hideCursor () { mouse.showCursor(false) },
70
70
 
71
- redrawCursor () { Bindings.mouse_redrawCursor() },
71
+ redrawCursor () {
72
+ assertInitialized('video')
73
+ Bindings.mouse_redrawCursor()
74
+ },
72
75
 
73
- get captured () { return captured },
76
+ get captured () {
77
+ assertInitialized('video')
78
+ return captured
79
+ },
74
80
 
75
81
  capture (capture = true) {
76
- if (typeof capture !== 'boolean') { throw Object.assign(new Error("capture must be a boolean"), { capture }) }
82
+ assertInitialized('video')
83
+ validateBoolean('capture', capture)
77
84
 
78
85
  Bindings.mouse_capture(capture)
79
86
  captured = capture
@@ -0,0 +1,21 @@
1
+ const Globals = require('../globals')
2
+ const { tryEmit } = require('../events/try')
3
+
4
+ const handleEvent = (event, targetId) => {
5
+ const { type } = event
6
+
7
+ const collection = Globals.sensorInstances.byId.get(targetId)
8
+ if (!collection) { return }
9
+
10
+ switch (type) {
11
+ case 'update': {
12
+ for (const sensorInstance of collection) {
13
+ tryEmit(sensorInstance, type, event)
14
+ }
15
+ } break
16
+
17
+ // No default
18
+ }
19
+ }
20
+
21
+ module.exports = { handleEvent }
@@ -1,3 +1,5 @@
1
+ const Globals = require('../globals')
2
+ const { assertInitialized, isAvailable } = require('../subsystems')
1
3
  const { refreshDevices } = require('./devices')
2
4
  const { SensorInstance } = require('./sensor-instance')
3
5
 
@@ -5,9 +7,18 @@ const { SensorInstance } = require('./sensor-instance')
5
7
  const sensor = {
6
8
  STANDARD_GRAVITY: 9.80665,
7
9
 
8
- get devices () { return [ ...refreshDevices() ] },
10
+ isAvailable () { return isAvailable('sensor') },
9
11
 
10
- openDevice (device) { return new SensorInstance(device) },
12
+ get devices () {
13
+ assertInitialized('sensor')
14
+ Globals.events.poll()
15
+ return [ ...refreshDevices() ]
16
+ },
17
+
18
+ openDevice (device) {
19
+ assertInitialized('sensor')
20
+ return new SensorInstance(device)
21
+ },
11
22
  }
12
23
 
13
24
  module.exports = { sensor }
@@ -1,69 +1,29 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const { refreshDevices } = require('./devices')
4
- const { EventsViaPoll } = require('../events/events-via-poll')
4
+ const { DeviceInstance, lookupDevice } = require('../device-instance')
5
+ const { validateNotClosed } = require('../validate')
5
6
 
6
7
  const validEvents = [
7
8
  'update',
8
9
  'close',
9
10
  ]
10
11
 
11
- class SensorInstance extends EventsViaPoll {
12
- constructor (device) {
13
- super(validEvents)
14
-
15
- refreshDevices()
16
- if (!Globals.sensorDevices.includes(device)) { throw Object.assign(new Error("device must be from sdl.sensor.devices"), { device }) }
12
+ class SensorInstance extends DeviceInstance {
13
+ constructor (requested) {
14
+ const device = lookupDevice(requested, 'sensor', refreshDevices)
17
15
 
18
16
  Bindings.sensor_open(device.id)
19
17
 
20
- this._device = device
21
-
22
- this._closed = false
23
-
24
- Globals.sensorInstances.all.add(this)
25
- let collection = Globals.sensorInstances.byId.get(this._device.id)
26
- if (!collection) {
27
- collection = new Set()
28
- Globals.sensorInstances.byId.set(this._device.id, collection)
29
- }
30
- collection.add(this)
18
+ super(validEvents, device, Globals.sensorInstances, Bindings.sensor_close)
31
19
  }
32
20
 
33
- get device () { return this._device }
34
-
35
21
  get data () {
36
22
  Globals.events.poll()
37
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
23
+ validateNotClosed(this)
38
24
 
39
25
  return Bindings.sensor_getData(this._device.id)
40
26
  }
41
-
42
- get closed () { return this._closed }
43
- close () {
44
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
45
-
46
- this._closed = true
47
-
48
- Globals.sensorInstances.all.delete(this)
49
- const collection = Globals.sensorInstances.byId.get(this._device.id)
50
- collection.delete(this)
51
- if (collection.size === 0) {
52
- Globals.sensorInstances.byId.delete(this._device.id)
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
- Globals.events.withoutPolling(() => {
63
- try { this.emit('close', { type: 'close' }) }
64
- catch (error) { this.emit('error', error) }
65
- })
66
- }
67
27
  }
68
28
 
69
29
  module.exports = { SensorInstance }
@@ -0,0 +1,56 @@
1
+ const { isMainThread } = require('node:worker_threads')
2
+ const Bindings = require('./bindings')
3
+ const Globals = require('./globals')
4
+ const { makeDevice: makeJoystickDevice } = require('./joystick/devices')
5
+ const { makeDevice: makeGamepadDevice, isGamepadDevice } = require('./gamepad/devices')
6
+ const { refreshDevices: refreshAudioDevices } = require('./audio/devices')
7
+
8
+ // The flag is set before the initializers fetch devices on purpose: it tracks whether SDL initialized the subsystem, which stays true even if the fetch fails.
9
+ const initSubsystem = (name) => {
10
+ const result = Bindings.global_initSubsystem(name)
11
+ Globals.initialized[name] = result === true
12
+ if (result !== true) { Globals.initErrors[name] = result }
13
+ return Globals.initialized[name]
14
+ }
15
+
16
+ const initJoysticks = () => {
17
+ initSubsystem('joystick')
18
+ initSubsystem('gamepad')
19
+ if (!Globals.initialized.joystick) { return }
20
+ const devices = Bindings.joystick_getDevices()
21
+ Globals.joystickDevices = devices.map(makeJoystickDevice)
22
+ Globals.gamepadDevices = devices.filter(isGamepadDevice).map(makeGamepadDevice)
23
+ }
24
+
25
+ const initializers = {
26
+ video: () => {
27
+ if (!isMainThread) { throw new Error("the video subsystem is only available on the main thread") }
28
+ if (initSubsystem('video')) { Globals.displays = Bindings.video_getDisplays() }
29
+ },
30
+ audio: () => { if (initSubsystem('audio')) { refreshAudioDevices() } },
31
+ joystick: initJoysticks,
32
+ gamepad: initJoysticks,
33
+ sensor: () => { initSubsystem('sensor') },
34
+ }
35
+
36
+ const ensure = (name) => {
37
+ if (Globals.initialized[name] === null) { initializers[name]() }
38
+ return Globals.initialized[name]
39
+ }
40
+
41
+ const isAvailable = (name) => {
42
+ try { return ensure(name) }
43
+ catch { return false }
44
+ }
45
+
46
+ const assertInitialized = (name) => {
47
+ if (!ensure(name)) {
48
+ const error = Globals.initErrors[name]
49
+ throw new Error(`SDL ${name} subsystem failed to initialize${error ? `: ${error}` : ''}`)
50
+ }
51
+ }
52
+
53
+ module.exports = {
54
+ assertInitialized,
55
+ isAvailable,
56
+ }
@@ -1,11 +1,16 @@
1
+ const Globals = require('../globals')
2
+ const { assertInitialized, isAvailable } = require('../subsystems')
1
3
  const { refreshDevices } = require('./devices')
2
4
 
3
5
 
4
- refreshDevices()
5
-
6
-
7
6
  const touch = {
8
- get devices () { return [ ...refreshDevices() ] },
7
+ isAvailable () { return isAvailable('video') },
8
+
9
+ get devices () {
10
+ assertInitialized('video')
11
+ Globals.events.poll()
12
+ return [ ...refreshDevices() ]
13
+ },
9
14
  }
10
15
 
11
16
  module.exports = { touch }
@@ -0,0 +1,114 @@
1
+ const INT32_MIN = -(2 ** 31)
2
+ const INT32_MAX = 2 ** 31 - 1
3
+ const FLOAT32_MAX = 3.4028234663852886e38
4
+
5
+ const fail = (message, name, value) => {
6
+ throw Object.assign(new Error(message), { [name.slice(name.lastIndexOf('.') + 1)]: value })
7
+ }
8
+
9
+ const validateBoolean = (name, value) => {
10
+ if (typeof value !== 'boolean') { fail(`${name} must be a boolean`, name, value) }
11
+ }
12
+
13
+ const validateString = (name, value) => {
14
+ if (typeof value !== 'string') { fail(`${name} must be a string`, name, value) }
15
+ }
16
+
17
+ const validateObject = (name, value) => {
18
+ if (typeof value !== 'object' || value === null) { fail(`${name} must be an object`, name, value) }
19
+ }
20
+
21
+ const validateBuffer = (name, value) => {
22
+ if (!(value instanceof Buffer)) { fail(`${name} must be a Buffer`, name, value) }
23
+ }
24
+
25
+ const validateNumber = (name, value) => {
26
+ if (!Number.isFinite(value)) { fail(`${name} must be a finite number`, name, value) }
27
+ }
28
+
29
+ const validateNonNegativeNumber = (name, value) => {
30
+ validateNumber(name, value)
31
+ if (value < 0) { fail(`${name} must be non-negative`, name, value) }
32
+ }
33
+
34
+ const validateFloat = (name, value) => {
35
+ validateNumber(name, value)
36
+ if (Math.abs(value) > FLOAT32_MAX) { fail(`${name} must fit in a 32-bit float`, name, value) }
37
+ }
38
+
39
+ const validateNonNegativeFloat = (name, value) => {
40
+ validateNonNegativeNumber(name, value)
41
+ validateFloat(name, value)
42
+ }
43
+
44
+ const validatePositiveFloat = (name, value) => {
45
+ validateFloat(name, value)
46
+ if (value <= 0) { fail(`${name} must be positive`, name, value) }
47
+ }
48
+
49
+ const validateNumberRange = (name, value, min, max) => {
50
+ validateNumber(name, value)
51
+ if (value < min || value > max) { fail(`${name} must be between ${min} and ${max}`, name, value) }
52
+ }
53
+
54
+ const validateInteger = (name, value) => {
55
+ if (!Number.isInteger(value)) { fail(`${name} must be an integer`, name, value) }
56
+ }
57
+
58
+ const validateNonNegativeInteger = (name, value) => {
59
+ validateInteger(name, value)
60
+ if (value < 0) { fail(`${name} must be non-negative`, name, value) }
61
+ }
62
+
63
+ const validatePositiveInteger = (name, value) => {
64
+ validateInteger(name, value)
65
+ if (value <= 0) { fail(`${name} must be positive`, name, value) }
66
+ }
67
+
68
+ const validateIntegerRange = (name, value, min, max, constraint = `must be between ${min} and ${max}`) => {
69
+ validateInteger(name, value)
70
+ if (value < min || value > max) { fail(`${name} ${constraint}`, name, value) }
71
+ }
72
+
73
+ const validateInt32 = (name, value) => {
74
+ validateIntegerRange(name, value, INT32_MIN, INT32_MAX, "must be a 32-bit integer")
75
+ }
76
+
77
+ const validateNonNegativeInt32 = (name, value) => {
78
+ validateIntegerRange(name, value, 0, INT32_MAX, "must be a non-negative 32-bit integer")
79
+ }
80
+
81
+ const validatePositiveInt32 = (name, value) => {
82
+ validateIntegerRange(name, value, 1, INT32_MAX, "must be a positive 32-bit integer")
83
+ }
84
+
85
+ const validateNotClosed = (instance) => {
86
+ if (instance._closed) { throw Object.assign(new Error("instance is closed"), { id: instance._device.id }) }
87
+ }
88
+
89
+ const validateNotDestroyed = (window) => {
90
+ if (window._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: window._id }) }
91
+ }
92
+
93
+ module.exports = {
94
+ INT32_MAX,
95
+ validateBoolean,
96
+ validateString,
97
+ validateObject,
98
+ validateBuffer,
99
+ validateNumber,
100
+ validateNonNegativeNumber,
101
+ validateFloat,
102
+ validateNonNegativeFloat,
103
+ validatePositiveFloat,
104
+ validateNumberRange,
105
+ validateInteger,
106
+ validateNonNegativeInteger,
107
+ validatePositiveInteger,
108
+ validateIntegerRange,
109
+ validateInt32,
110
+ validateNonNegativeInt32,
111
+ validatePositiveInt32,
112
+ validateNotClosed,
113
+ validateNotDestroyed,
114
+ }
@@ -0,0 +1,63 @@
1
+ const Bindings = require('../bindings')
2
+ const Enums = require('../enums')
3
+ const { VideoFormatHelpers } = require('./format-helpers')
4
+ const { validateObject, validateString, validatePositiveInt32 } = require('../validate')
5
+ const { validateStride, validateImageBuffer } = require('./validate')
6
+
7
+ const overlaps = (a, b) => {
8
+ if (a.buffer !== b.buffer) { return false }
9
+ return a.byteOffset < b.byteOffset + b.length && b.byteOffset < a.byteOffset + a.length
10
+ }
11
+
12
+ const convert = (srcSpec, srcData, dstSpec, dstData) => {
13
+ validateObject('srcSpec', srcSpec)
14
+ validateObject('dstSpec', dstSpec)
15
+ const { width: srcWidth, height: srcHeight, format: srcFormat } = srcSpec
16
+ const { width: dstWidth = srcWidth, height: dstHeight = srcHeight, format: dstFormat, scaling = 'nearest' } = dstSpec
17
+ let { stride: srcStride } = srcSpec
18
+ let { stride: dstStride } = dstSpec
19
+
20
+ validatePositiveInt32('srcSpec.width', srcWidth)
21
+ validatePositiveInt32('srcSpec.height', srcHeight)
22
+ validateString('srcSpec.format', srcFormat)
23
+ const srcHelpers = VideoFormatHelpers[srcFormat]
24
+ if (srcHelpers === undefined) { throw Object.assign(new Error("invalid srcSpec.format"), { srcFormat }) }
25
+ if (!srcHelpers.isConvertible) { throw Object.assign(new Error("srcSpec.format can't be converted"), { srcFormat }) }
26
+
27
+ const minSrcStride = srcHelpers.minStride(srcWidth)
28
+ if (srcStride === undefined) { srcStride = minSrcStride }
29
+ validateStride('srcSpec.stride', srcStride, minSrcStride, { srcStride, srcWidth, srcFormat })
30
+ validateImageBuffer('srcData', srcData, srcHelpers.minBufferSize(srcStride, srcHeight), { srcData, srcStride, srcHeight, srcFormat })
31
+
32
+ validatePositiveInt32('dstSpec.width', dstWidth)
33
+ validatePositiveInt32('dstSpec.height', dstHeight)
34
+ validateString('dstSpec.scaling', scaling)
35
+ const _scaling = Enums.scaleMode[scaling]
36
+ if (_scaling === undefined) { throw Object.assign(new Error("dstSpec.scaling must be 'nearest' or 'linear'"), { scaling }) }
37
+
38
+ validateString('dstSpec.format', dstFormat)
39
+ const dstHelpers = VideoFormatHelpers[dstFormat]
40
+ if (dstHelpers === undefined) { throw Object.assign(new Error("invalid dstSpec.format"), { dstFormat }) }
41
+ if (!dstHelpers.isConvertible) { throw Object.assign(new Error("dstSpec.format can't be converted"), { dstFormat }) }
42
+
43
+ const minStride = dstHelpers.minStride(dstWidth)
44
+ if (dstStride === undefined) { dstStride = minStride }
45
+ validateStride('dstSpec.stride', dstStride, minStride, { dstStride, dstWidth, dstFormat })
46
+
47
+ const size = dstHelpers.minBufferSize(dstStride, dstHeight)
48
+ const _srcFormat = Enums.pixelFormat[srcFormat]
49
+ const _dstFormat = Enums.pixelFormat[dstFormat]
50
+
51
+ if (dstData === undefined) {
52
+ const buffer = Buffer.alloc(size)
53
+ Bindings.video_convert(srcWidth, srcHeight, _srcFormat, srcData, srcStride, dstWidth, dstHeight, _dstFormat, buffer, dstStride, _scaling)
54
+ return buffer
55
+ }
56
+
57
+ validateImageBuffer('dstData', dstData, size, { dstData, dstStride, dstHeight, dstFormat })
58
+ if (overlaps(srcData, dstData)) { throw Object.assign(new Error("srcData and dstData must not overlap"), { srcData, dstData }) }
59
+ Bindings.video_convert(srcWidth, srcHeight, _srcFormat, srcData, srcStride, dstWidth, dstHeight, _dstFormat, dstData, dstStride, _scaling)
60
+ return size
61
+ }
62
+
63
+ module.exports = { convert }
@@ -0,0 +1,179 @@
1
+ const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { video: videoModule } = require('.')
4
+ const { mapKey } = require('../keyboard/key-mapping')
5
+ const { refreshDevices: refreshTouchDevices } = require('../touch/devices')
6
+ const { tryEmit, tryCall } = require('../events/try')
7
+
8
+ const handleDisplayEvent = (event) => {
9
+ const { type } = event
10
+
11
+ switch (type) {
12
+ case 'displayAdd': {
13
+ const { display } = event
14
+ delete event.display
15
+
16
+ if (Globals.displays.some((a) => a.id === display.id)) { return }
17
+ Globals.displays.push(display)
18
+ event.device = display
19
+ } break
20
+
21
+ case 'displayRemove': {
22
+ const { displayId } = event
23
+ delete event.displayId
24
+
25
+ const index = Globals.displays.findIndex((a) => a.id === displayId)
26
+ if (index === -1) { return }
27
+ event.device = Globals.displays[index]
28
+ Globals.displays.splice(index, 1)
29
+ } break
30
+
31
+ case 'displayOrient':
32
+ case 'displayMove':
33
+ case 'displayScaleChange':
34
+ case 'displayModeChange':
35
+ case 'displayUsableChange': {
36
+ const { displayId, display: fresh } = event
37
+ delete event.displayId
38
+ delete event.display
39
+
40
+ const display = Globals.displays.find((a) => a.id === displayId)
41
+ if (!display) { return }
42
+
43
+ Object.assign(display, fresh)
44
+ event.device = display
45
+ } break
46
+
47
+ // No default
48
+ }
49
+
50
+ tryEmit(videoModule, type, event)
51
+ }
52
+
53
+ const handleWindowEvent = (event, targetId) => {
54
+ const { type } = event
55
+
56
+ const window = Globals.windows.all.get(targetId)
57
+ if (!window) { return }
58
+
59
+ switch (type) {
60
+ case 'move': {
61
+ if (window._x === event.x && window._y === event.y) { return }
62
+ window._x = event.x
63
+ window._y = event.y
64
+ } break
65
+ case 'resize': {
66
+ if (
67
+ window._width === event.width
68
+ && window._height === event.height
69
+ && window._pixelWidth === event.pixelWidth
70
+ && window._pixelHeight === event.pixelHeight
71
+ ) { return }
72
+ window._width = event.width
73
+ window._height = event.height
74
+ window._pixelWidth = event.pixelWidth
75
+ window._pixelHeight = event.pixelHeight
76
+ } break
77
+ case 'displayChange': {
78
+ window._displayId = event.displayId
79
+ delete event.displayId
80
+ event.display = Globals.displays.find((a) => a.id === window._displayId) ?? null
81
+ } break
82
+
83
+ case 'show': {
84
+ window._visible = true
85
+ } break
86
+ case 'hide': {
87
+ window._visible = false
88
+ } break
89
+ case 'expose': {
90
+ // Nothing
91
+ } break
92
+
93
+ case 'minimize': {
94
+ window._minimized = true
95
+ window._maximized = false
96
+ } break
97
+ case 'maximize': {
98
+ window._minimized = false
99
+ window._maximized = true
100
+ } break
101
+ case 'restore': {
102
+ window._minimized = false
103
+ window._maximized = false
104
+ } break
105
+
106
+ case 'enterFullscreen': {
107
+ window._fullscreen = true
108
+ } break
109
+ case 'leaveFullscreen': {
110
+ window._fullscreen = false
111
+ } break
112
+
113
+ case 'focus': {
114
+ Globals.windows.focused = window
115
+ } break
116
+ case 'blur': {
117
+ if (Globals.windows.focused === window) { Globals.windows.focused = null }
118
+ } break
119
+
120
+ case 'hover': {
121
+ Globals.windows.hovered = window
122
+ } break
123
+ case 'leave': {
124
+ if (Globals.windows.hovered === window) { Globals.windows.hovered = null }
125
+ } break
126
+
127
+ case 'close': {
128
+ tryCall(() => { window.destroyGently() })
129
+ return
130
+ }
131
+
132
+ case 'renderDeviceLost': {
133
+ // Only SDL renderers send this event, so it never arrives for opengl/webgpu windows, whose _accelerated and _vsync are null.
134
+ try {
135
+ const result = Bindings.window_setAcceleratedAndVsync(window._id, window._accelerated, window._vsync)
136
+ window._accelerated = result.accelerated
137
+ window._vsync = result.vsync
138
+ }
139
+ catch (error) { tryEmit(window, 'error', error) }
140
+ return
141
+ }
142
+
143
+ case 'keyDown':
144
+ case 'keyUp': {
145
+ event.key = mapKey(event.key)
146
+ } break
147
+
148
+ case 'fingerDown':
149
+ case 'fingerUp':
150
+ case 'fingerMove':
151
+ case 'fingerCancel': {
152
+ const { touchId } = event
153
+ delete event.touchId
154
+
155
+ // Events synthesized from the mouse or pen have null touch device
156
+ let device = null
157
+ if (!event.mouse && !event.pen) {
158
+ device = Globals.touchDevices.find(({ id }) => id === touchId)
159
+ if (!device) {
160
+ try { refreshTouchDevices() }
161
+ catch { return }
162
+ device = Globals.touchDevices.find(({ id }) => id === touchId)
163
+ if (!device) { return }
164
+ }
165
+ }
166
+
167
+ event.device = device
168
+ } break
169
+
170
+ // No default
171
+ }
172
+
173
+ tryEmit(window, type, event)
174
+ }
175
+
176
+ module.exports = {
177
+ handleDisplayEvent,
178
+ handleWindowEvent,
179
+ }