@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,90 +0,0 @@
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
- if (duration === 0) { return stopEffect(kind, id) }
24
-
25
- const effect = effects[kind]
26
- a = Math.round(a * 0xFFFF)
27
- b = Math.round(b * 0xFFFF)
28
- effect.call(id, a, b, MAX_DURATION)
29
-
30
- clearEffectTimeout(effect, id)
31
-
32
- // Zero intensity stops the effect, so there is nothing to wait for
33
- if (a === 0 && b === 0) { return }
34
-
35
- // Keeps Node.js alive while rumbling
36
- const timers = []
37
- if (duration === null || duration > MAX_DURATION) {
38
- timers.push(setInterval(() => {
39
- try { effect.call(id, a, b, MAX_DURATION) }
40
- catch (_) { clearEffectTimeout(effect, id) }
41
- }, 30e3))
42
- }
43
- if (duration !== null) {
44
- timers.push(setTimeout(() => {
45
- clearEffectTimeout(effect, id)
46
- try {
47
- effect.call(id, 0, 0, 0)
48
- Globals.events.poll()
49
- }
50
- catch (_) {}
51
- }, duration))
52
- }
53
- effect.timeouts.set(id, timers)
54
- }
55
-
56
- const stopEffect = (kind, id) => {
57
- const effect = effects[kind]
58
- clearEffectTimeout(effect, id)
59
- effect.call(id, 0, 0, 0)
60
- Globals.events.poll()
61
- }
62
-
63
- const releaseDevice = (id) => {
64
- if (Globals.joystickInstances.byId.has(id) || Globals.gamepadInstances.byId.has(id)) { return }
65
-
66
- for (const effect of Object.values(effects)) {
67
- if (!effect.timeouts.has(id)) { continue }
68
-
69
- // This call will throw if the device is gone
70
- try { effect.call(id, 0, 0, 0) }
71
- catch (_) {}
72
- clearEffectTimeout(effect, id)
73
- }
74
- }
75
-
76
- const setPlayer = (id, player) => {
77
- Bindings.joystick_setPlayer(id, player)
78
- const devices = Bindings.joystick_getDevices()
79
- for (const device of [ ...Globals.joystickDevices, ...Globals.gamepadDevices ]) {
80
- const updated = devices.find(({ id: _id }) => _id === device.id)
81
- if (updated) { device.player = updated.player }
82
- }
83
- }
84
-
85
- module.exports = {
86
- startEffect,
87
- stopEffect,
88
- releaseDevice,
89
- setPlayer,
90
- }
@@ -1,88 +0,0 @@
1
- const { isMainThread } = require('node:worker_threads')
2
- if (isMainThread) { throw new Error("@kmamal/sdl/worker can only be used in worker threads, import @kmamal/sdl in the main thread") }
3
-
4
- const Globals = require('./globals')
5
-
6
- Globals.events = {
7
- poll () {},
8
- withoutPolling (fn) { fn() },
9
- switchToPollingFast () {},
10
- switchToPollingSlow () {},
11
- }
12
-
13
- const Enums = require('./enums')
14
- const { getFormatHelpers: getAudioFormatHelpers } = require('./audio/format-helpers')
15
- const { getFormatHelpers: getVideoFormatHelpers } = require('./video/format-helpers')
16
- const { AudioPlaybackStream } = require('./audio/audio-playback-stream')
17
- const { AudioRecordingStream } = require('./audio/audio-recording-stream')
18
- const { makeDefault: makeDefaultDevice } = require('./audio/device')
19
-
20
- const makeModule = (type, Stream) => {
21
- const defaultDevice = makeDefaultDevice()
22
-
23
- return {
24
- openDevice (device = null, options = {}) {
25
- if (device === null) { device = defaultDevice }
26
- else {
27
- if (typeof device !== 'object') { throw Object.assign(new Error("device must be an object"), { device }) }
28
- const { id } = device
29
- if (id !== null && !Number.isInteger(id)) { throw Object.assign(new Error("device.id must be an integer or null"), { id }) }
30
- if (id !== null && (id < 0 || id > 2 ** 32 - 1)) { throw Object.assign(new Error("device.id must be an unsigned 32-bit integer"), { id }) }
31
- }
32
-
33
- return new Stream(device, options)
34
- },
35
- }
36
- }
37
-
38
- module.exports = {
39
- video: {
40
- bytesPerPixel (format) { return getVideoFormatHelpers(format).bytesPerPixel },
41
- isYuv (format) { return getVideoFormatHelpers(format).isYuv },
42
- isPlanarYuv (format) { return getVideoFormatHelpers(format).isPlanarYuv },
43
-
44
- minStride (format, width) {
45
- const helpers = getVideoFormatHelpers(format)
46
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
47
- if (width < 0) { throw Object.assign(new Error("width must be non-negative"), { width }) }
48
- return helpers.minStride(width)
49
- },
50
-
51
- minBufferSize (format, stride, height) {
52
- const helpers = getVideoFormatHelpers(format)
53
- if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
54
- if (stride < 0) { throw Object.assign(new Error("stride must be non-negative"), { stride }) }
55
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
56
- if (height < 0) { throw Object.assign(new Error("height must be non-negative"), { height }) }
57
- return helpers.minBufferSize(stride, height)
58
- },
59
- },
60
- keyboard: {
61
- get SCANCODE () { return Enums.scancodes },
62
- },
63
- mouse: {
64
- get BUTTON () { return Enums.mouseButtons },
65
- },
66
- sensor: {
67
- STANDARD_GRAVITY: 9.80665,
68
- },
69
- audio: {
70
- bytesPerSample (format) { return getAudioFormatHelpers(format).bytesPerSample },
71
- minSampleValue (format) { return getAudioFormatHelpers(format).minSampleValue },
72
- maxSampleValue (format) { return getAudioFormatHelpers(format).maxSampleValue },
73
- zeroSampleValue (format) { return getAudioFormatHelpers(format).zeroSampleValue },
74
-
75
- readSample (format, buffer, offset) {
76
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
77
- return getAudioFormatHelpers(format).reader.call(buffer, offset)
78
- },
79
-
80
- writeSample (format, buffer, value, offset) {
81
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
82
- return getAudioFormatHelpers(format).writer.call(buffer, value, offset)
83
- },
84
-
85
- playback: makeModule('playback', AudioPlaybackStream),
86
- recording: makeModule('recording', AudioRecordingStream),
87
- },
88
- }
@@ -1,71 +0,0 @@
1
- import type { Sdl } from './index'
2
-
3
- export namespace SdlWorker {
4
-
5
- export namespace Video {
6
-
7
- export type Format = Sdl.Video.Format | Sdl.Video.FormatAlias
8
-
9
- interface Module {
10
- bytesPerPixel (format: Format): number
11
- isYuv (format: Format): boolean
12
- isPlanarYuv (format: Format): boolean
13
- minStride (format: Format, width: number): number
14
- minBufferSize (format: Format, stride: number, height: number): number
15
- }
16
-
17
- }
18
-
19
- export namespace Keyboard {
20
-
21
- interface Module {
22
- readonly SCANCODE: { [name in Sdl.Keyboard.ScancodeNames]: Sdl.Keyboard.Scancode }
23
- }
24
-
25
- }
26
-
27
- export namespace Mouse {
28
-
29
- interface Module {
30
- readonly BUTTON: { [name in Sdl.Mouse.ButtonNames]: Sdl.Mouse.Button }
31
- }
32
-
33
- }
34
-
35
- export namespace Sensor {
36
-
37
- interface Module {
38
- readonly STANDARD_GRAVITY: 9.80665
39
- }
40
-
41
- }
42
-
43
- export namespace Audio {
44
-
45
- export type Format = Sdl.Audio.Format | Sdl.Audio.FormatAlias
46
-
47
- interface Module {
48
- bytesPerSample (format: Format): number
49
- minSampleValue (format: Format): number
50
- maxSampleValue (format: Format): number
51
- zeroSampleValue (format: Format): number
52
- readSample (format: Format, buffer: Buffer, offset?: number): number
53
- writeSample (format: Format, buffer: Buffer, value: number, offset?: number): number
54
-
55
- readonly playback: DeviceModule<Sdl.Audio.AudioPlaybackStream>
56
- readonly recording: DeviceModule<Sdl.Audio.AudioRecordingStream>
57
- }
58
-
59
- interface DeviceModule<Stream extends Sdl.Audio.AudioStream> {
60
- openDevice (device?: Sdl.Audio.Device | null, options?: Sdl.Audio.StreamOptions): Stream
61
- }
62
-
63
- }
64
-
65
- }
66
-
67
- export const video: SdlWorker.Video.Module
68
- export const keyboard: SdlWorker.Keyboard.Module
69
- export const mouse: SdlWorker.Mouse.Module
70
- export const sensor: SdlWorker.Sensor.Module
71
- export const audio: SdlWorker.Audio.Module