@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
@@ -1,4 +1,5 @@
1
1
  const Os = require('node:os')
2
+ const { validateString } = require('../validate')
2
3
 
3
4
  const minStride = (bytesPerPixel) => (width) => width * bytesPerPixel
4
5
  const minStridePacked = (width) => 4 * Math.ceil(width / 2)
@@ -13,15 +14,17 @@ const rgb = (bytesPerPixel) => ({
13
14
  minStride: minStride(bytesPerPixel),
14
15
  minBufferSize,
15
16
  isRenderable: true,
17
+ isConvertible: true,
16
18
  })
17
19
 
18
- const yuv = (bytesPerPixel, isPlanarYuv, isRenderable = true) => ({
20
+ const yuv = (bytesPerPixel, isPlanarYuv, isSupported = true) => ({
19
21
  bytesPerPixel,
20
22
  isYuv: true,
21
23
  isPlanarYuv,
22
24
  minStride: isPlanarYuv ? minStride(bytesPerPixel) : minStridePacked,
23
25
  minBufferSize: isPlanarYuv ? minBufferSizePlanar : minBufferSize,
24
- isRenderable,
26
+ isRenderable: isSupported,
27
+ isConvertible: isSupported,
25
28
  })
26
29
 
27
30
  const VideoFormatHelpers = Object.assign(Object.create(null), {
@@ -82,10 +85,6 @@ const VideoFormatHelpers = Object.assign(Object.create(null), {
82
85
  p010: { ...yuv(2, true, false), minStride: minStridePacked },
83
86
  })
84
87
 
85
- for (const [ key, value ] of Object.entries(VideoFormatHelpers)) {
86
- value.name = key
87
- }
88
-
89
88
  if (Os.endianness() === 'LE') {
90
89
  VideoFormatHelpers.rgba32 = VideoFormatHelpers.abgr8888
91
90
  VideoFormatHelpers.argb32 = VideoFormatHelpers.bgra8888
@@ -108,10 +107,13 @@ else {
108
107
  }
109
108
 
110
109
  const getFormatHelpers = (format) => {
111
- if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
110
+ validateString('format', format)
112
111
  const helpers = VideoFormatHelpers[format]
113
112
  if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
114
113
  return helpers
115
114
  }
116
115
 
117
- module.exports = { VideoFormatHelpers, getFormatHelpers }
116
+ module.exports = {
117
+ VideoFormatHelpers,
118
+ getFormatHelpers,
119
+ }
@@ -1,13 +1,11 @@
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')
4
5
  const { Window } = require('./window')
5
6
  const { getFormatHelpers } = require('./format-helpers')
6
-
7
-
8
- Globals.displays = Globals.info.initialized.video
9
- ? Bindings.video_getDisplays()
10
- : []
7
+ const { convert } = require('./conversion')
8
+ const { validateNonNegativeInteger } = require('../validate')
11
9
 
12
10
 
13
11
  const validEvents = [
@@ -21,26 +19,45 @@ const validEvents = [
21
19
  ]
22
20
 
23
21
  const video = new class extends EventsViaPoll {
24
- constructor () { super(validEvents) }
22
+ constructor () { super(validEvents, () => { assertInitialized('video') }) }
23
+
24
+ isAvailable () { return isAvailable('video') }
25
+
26
+ get drivers () {
27
+ assertInitialized('video')
28
+ return Bindings.video_getDrivers()
29
+ }
25
30
 
26
31
  get displays () {
32
+ assertInitialized('video')
27
33
  Globals.events.poll()
28
34
  return [ ...Globals.displays ]
29
35
  }
30
36
 
31
- get windows () { return [ ...Globals.windows.all.values() ] }
37
+ get windows () {
38
+ assertInitialized('video')
39
+ Globals.events.poll()
40
+ return [ ...Globals.windows.all.values() ]
41
+ }
32
42
 
33
43
  get focused () {
44
+ assertInitialized('video')
34
45
  Globals.events.poll()
35
46
  return Globals.windows.focused
36
47
  }
37
48
 
38
49
  get hovered () {
50
+ assertInitialized('video')
39
51
  Globals.events.poll()
40
52
  return Globals.windows.hovered
41
53
  }
42
54
 
43
- createWindow (options) { return new Window(options) }
55
+ createWindow (options) {
56
+ assertInitialized('video')
57
+ return new Window(options)
58
+ }
59
+
60
+ convert (...args) { return convert(...args) }
44
61
 
45
62
  bytesPerPixel (format) { return getFormatHelpers(format).bytesPerPixel }
46
63
  isYuv (format) { return getFormatHelpers(format).isYuv }
@@ -48,17 +65,14 @@ const video = new class extends EventsViaPoll {
48
65
 
49
66
  minStride (format, width) {
50
67
  const helpers = getFormatHelpers(format)
51
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
52
- if (width < 0) { throw Object.assign(new Error("width must be non-negative"), { width }) }
68
+ validateNonNegativeInteger('width', width)
53
69
  return helpers.minStride(width)
54
70
  }
55
71
 
56
72
  minBufferSize (format, stride, height) {
57
73
  const helpers = getFormatHelpers(format)
58
- if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
59
- if (stride < 0) { throw Object.assign(new Error("stride must be non-negative"), { stride }) }
60
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
61
- if (height < 0) { throw Object.assign(new Error("height must be non-negative"), { height }) }
74
+ validateNonNegativeInteger('stride', stride)
75
+ validateNonNegativeInteger('height', height)
62
76
  return helpers.minBufferSize(stride, height)
63
77
  }
64
78
  }()
@@ -0,0 +1,45 @@
1
+ const { VideoFormatHelpers } = require('./format-helpers')
2
+ const { INT32_MAX, validateString, validateBuffer, validateInteger, validateIntegerRange, validatePositiveInt32 } = require('../validate')
3
+
4
+ const validateVsync = (vsync) => {
5
+ validateIntegerRange('vsync', vsync, -1, INT32_MAX, "must be -1 or a non-negative 32-bit integer")
6
+ }
7
+
8
+ const validateStride = (name, stride, minStride, details) => {
9
+ validateInteger(name, stride)
10
+ if (stride > INT32_MAX) { throw Object.assign(new Error(`${name} must be a 32-bit integer`), details) }
11
+ if (stride < minStride) { throw Object.assign(new Error(`${name} is smaller than expected`), details) }
12
+ }
13
+
14
+ const validateImageBuffer = (name, buffer, minSize, details) => {
15
+ validateBuffer(name, buffer)
16
+ if (buffer.length < minSize) { throw Object.assign(new Error(`${name} is smaller than expected`), details) }
17
+ }
18
+
19
+ const validateImage = (width, height, stride, format, buffer, validateHelpers) => {
20
+ validatePositiveInt32('width', width)
21
+ validatePositiveInt32('height', height)
22
+ validateString('format', format)
23
+ const helpers = VideoFormatHelpers[format]
24
+ if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
25
+ validateHelpers(helpers, format)
26
+ validateStride('stride', stride, helpers.minStride(width), { stride, width, format })
27
+ validateImageBuffer('buffer', buffer, helpers.minBufferSize(stride, height), { buffer, stride, height, format })
28
+ }
29
+
30
+ const validateRenderableFormat = (helpers, format) => {
31
+ if (!helpers.isRenderable) { throw Object.assign(new Error("format can't be rendered"), { format }) }
32
+ }
33
+
34
+ const validateRgbFormat = (helpers, format) => {
35
+ if (helpers.isYuv) { throw Object.assign(new Error("format must be an RGB format"), { format }) }
36
+ }
37
+
38
+ module.exports = {
39
+ validateVsync,
40
+ validateStride,
41
+ validateImageBuffer,
42
+ validateImage,
43
+ validateRenderableFormat,
44
+ validateRgbFormat,
45
+ }