@kmamal/sdl 0.11.5 → 0.11.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 (42) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +49 -10
  3. package/binding.gyp +1 -0
  4. package/package.json +2 -2
  5. package/scripts/build-sdl.mjs +4 -1
  6. package/scripts/install.mjs +6 -3
  7. package/scripts/util/fetch.js +6 -3
  8. package/src/javascript/audio/audio-instance.js +2 -3
  9. package/src/javascript/audio/device.js +1 -10
  10. package/src/javascript/audio/index.js +5 -7
  11. package/src/javascript/controller/controller-instance.js +2 -0
  12. package/src/javascript/controller/device.js +14 -3
  13. package/src/javascript/controller/index.js +5 -11
  14. package/src/javascript/events/index.js +56 -126
  15. package/src/javascript/events/reconcile-audio-devices.js +15 -0
  16. package/src/javascript/events/reconcile-displays.js +16 -0
  17. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
  18. package/src/javascript/events/reconcile.js +61 -0
  19. package/src/javascript/joystick/device.js +9 -3
  20. package/src/javascript/joystick/index.js +13 -5
  21. package/src/javascript/joystick/joystick-instance.js +2 -0
  22. package/src/javascript/sensor/sensor-instance.js +2 -0
  23. package/src/javascript/video/display.js +4 -0
  24. package/src/javascript/video/index.js +1 -0
  25. package/src/native/audio.cpp +10 -12
  26. package/src/native/audio.h +4 -0
  27. package/src/native/controller.cpp +72 -56
  28. package/src/native/controller.h +3 -0
  29. package/src/native/enums.cpp +315 -315
  30. package/src/native/events.cpp +156 -80
  31. package/src/native/events.h +1 -0
  32. package/src/native/global.cpp +46 -20
  33. package/src/native/joystick.cpp +87 -48
  34. package/src/native/joystick.h +3 -2
  35. package/src/native/keyboard.cpp +1 -1
  36. package/src/native/mouse.cpp +2 -2
  37. package/src/native/power.cpp +9 -3
  38. package/src/native/sensor.cpp +24 -13
  39. package/src/native/video.cpp +21 -13
  40. package/src/native/video.h +1 -0
  41. package/src/native/window.cpp +28 -22
  42. package/src/types/index.d.ts +57 -17
package/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [v0.11.8] - 2025-06-26
9
+
10
+ ### Changed
11
+
12
+ - Upgraded SDL to v2.32.8.
13
+
14
+ ## [v0.11.7] - 2025-03-14
15
+
16
+ ### Added
17
+
18
+ - Started keeping this changelog.
19
+
20
+ [unreleased]: https://github.com/kmamal/node-sdl/compare/v0.11.8...HEAD
21
+ [v0.11.8]: https://github.com/kmamal/node-sdl/compare/v0.11.7...v0.11.8
22
+ [v0.11.7]: https://github.com/kmamal/node-sdl/releases/tag/v0.11.7
package/README.md CHANGED
@@ -134,6 +134,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
134
134
  * [Event: 'displayAdd'](#event-displayadd)
135
135
  * [Event: 'displayRemove'](#event-displayremove)
136
136
  * [Event: 'displayOrient'](#event-displayorient)
137
+ * [Event: 'displayMove'](#event-displaymove)
137
138
  * [sdl.video.displays](#sdlvideodisplays)
138
139
  * [sdl.video.windows](#sdlvideowindows)
139
140
  * [sdl.video.focused](#sdlvideofocused)
@@ -210,7 +211,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
210
211
  * [window.utility](#windowutility)
211
212
  * [window.render(width, height, stride, format, buffer[, options])](#windowrenderwidth-height-stride-format-buffer-options)
212
213
  * [window.setIcon(width, height, stride, format, buffer)](#windowseticonwidth-height-stride-format-buffer)
213
- * [window.flash(untilFocused)](#windowflashuntilfocused)
214
+ * [window.flash([untilFocused])](#windowflashuntilfocused)
214
215
  * [window.stopFlashing()](#windowstopflashing)
215
216
  * [window.destroyed](#windowdestroyed)
216
217
  * [window.destroy()](#windowdestroy)
@@ -511,9 +512,15 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
511
512
  ### Event: 'displayOrient'
512
513
 
513
514
  * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
515
+ * `orientation: <string>`: The display's new orientation.
514
516
 
515
517
  Fired when a display changes orientation.
516
- Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
518
+
519
+ ### Event: 'displayMove'
520
+
521
+ * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
522
+
523
+ Fired when a display changes position.
517
524
 
518
525
 
519
526
  ### sdl.video.displays
@@ -530,8 +537,19 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
530
537
  * `horizontal: <number>` The horizontal density.
531
538
  * `vertical: <number>` The vertical density.
532
539
  * `diagonal: <number>` The diagonal density.
540
+ * `orientation: <string>|<null>` The orientation of the display.
533
541
 
534
542
  A list of all detected displays.
543
+
544
+ Possible values for `orientation` are `null` if it is unknown, or one of:
545
+
546
+ | Value | Corresponding `SDL_DisplayOrientation` |
547
+ | --- | --- |
548
+ | `'landscape'` | `SDL_ORIENTATION_LANDSCAPE` |
549
+ | `'landscapeFlipped'` | `SDL_ORIENTATION_LANDSCAPE_FLIPPED` |
550
+ | `'portrait'` | `SDL_ORIENTATION_PORTRAIT` |
551
+ | `'portraitFlipped'` | `SDL_ORIENTATION_PORTRAIT_FLIPPED` |
552
+
535
553
  Sample output for two side-to-side monitors is below.
536
554
  Notice how the geometries don't overlap:
537
555
 
@@ -1082,7 +1100,7 @@ Possible values are:
1082
1100
  | `'best'` | `SDL_ScaleModeBest` | anisotropic filtering |
1083
1101
 
1084
1102
  If the window was created with either of the `opengl` or `webgpu` options, then you can only render to the window with OpenGL/WebGPU calls.
1085
- Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-options) will fail.
1103
+ Calls to `render()` will fail.
1086
1104
 
1087
1105
  ### window.setIcon(width, height, stride, format, buffer)
1088
1106
 
@@ -1090,7 +1108,7 @@ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-options) wi
1090
1108
 
1091
1109
  Set's the window's icon, usually displayed in the title bar and the taskbar.
1092
1110
 
1093
- ### window.flash(untilFocused)
1111
+ ### window.flash([untilFocused])
1094
1112
 
1095
1113
  * `untilFocused: <boolean>` Whether to keep flashing the window until the user focuses it. Default: `false`
1096
1114
 
@@ -1585,10 +1603,10 @@ When this event is emitted, all instances that were opened from the removed devi
1585
1603
 
1586
1604
  * `<object>[]`
1587
1605
  * `id: <number>` The unique id for the device.
1588
- * `type: <JoystickType>` The type of the device.
1589
1606
  * `name: <string>` The name of the device.
1590
1607
  * `path: <string>` The implementation dependent path of the device.
1591
1608
  * `guid: <string>` The GUID of the device.
1609
+ * `type: <JoystickType>|<null>` The type of the device.
1592
1610
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1593
1611
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1594
1612
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1596,7 +1614,7 @@ When this event is emitted, all instances that were opened from the removed devi
1596
1614
 
1597
1615
  A list of all the detected joystick devices.
1598
1616
 
1599
- Possible values for `type` are:
1617
+ Possible values for `type` are `null` if it is unknown, or one of:
1600
1618
 
1601
1619
  | Value | Corresponding `SDL_JoystickType` |
1602
1620
  | --- | --- |
@@ -1616,10 +1634,10 @@ Sample output:
1616
1634
  [
1617
1635
  {
1618
1636
  id: 0,
1619
- type: 'gamecontroller',
1620
1637
  name: 'DragonRise Inc. Generic USB Joystick',
1621
1638
  path: '/dev/input/event21',
1622
1639
  guid: '03000000790000000600000010010000',
1640
+ type: 'gamecontroller',
1623
1641
  vendor: 121,
1624
1642
  product: 6,
1625
1643
  version: 272,
@@ -1819,13 +1837,13 @@ Closes the instance.
1819
1837
  ## sdl.controller
1820
1838
 
1821
1839
  An SDL controller is an abstraction over [`joysticks`](#sdljoystick) based on the xbox360 controller: They have a dpad, two analog sticks, 4 buttons on the right (called A, B, X, Y), shoulder buttons (two of which might be axes) and 3 buttons in the middle ("Start", "Back" and usually some kind of logo-button called "Guide").
1822
- The SDL controller abstraction uses the naming-conventions of xbox360/XInput for all supported devices (for example devices that have a similar layout, like the Playstation DualShock Controller, but different button names), so you'll know that for example `controllerInstance.axes.leftStickX` is always the x-axis of the left analog stick, or `controllerInstance.buttons.b` is always the rightmost buttons of the 4 buttons on the right.
1840
+ The SDL controller abstraction uses the naming-conventions of xbox360/XInput for all supported devices (for example devices that have a similar layout, like the Playstation DualShock Controller, but different button names), so you'll know that for example `controllerInstance.axes.leftStickX` is always the x-axis of the left analog stick, or `controllerInstance.buttons.b` is always the rightmost button of the 4 buttons on the right.
1823
1841
  This makes it easy to provide consistent input bindings, like "press B to jump, move around with the left analog stick".
1824
1842
  With a pure joystick instance it's impossible to know which axis or button corresponds to which physical axis/button on the device.
1825
1843
 
1826
1844
  Because controllers are an abstraction over joysticks, they operate on the same set of devices (if a joystick device and a controller device have the same id, then they refer to the same underlying physical device).
1827
1845
  For a joystick device to also be available as a controller device it needs a "mapping".
1828
- A mapping is a string that consists of the device's name, it's GUID, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name.
1846
+ A mapping is a string that consists of the device's GUID, it's name, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name.
1829
1847
  See the sample output [`here`](#sdlcontrollerdevices) for an example.
1830
1848
  SDL has pretty good default controller mappings, but if you need more, there's a community sourced database available on https://github.com/gabomdq/SDL_GameControllerDB.
1831
1849
  You can add them via:
@@ -1834,7 +1852,7 @@ You can add them via:
1834
1852
  const url = 'https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt'
1835
1853
  const result = await fetch(url)
1836
1854
  const text = await result.text()
1837
- const mappings = text.split('\n')
1855
+ const mappings = text.split('\n').filter((line) => line && !line.startsWith('#'))
1838
1856
  sdl.controller.addMappings(mappings)
1839
1857
  ```
1840
1858
 
@@ -1871,6 +1889,7 @@ This can cause already opened controller instances to be [remapped](#event-remap
1871
1889
  * `name: <string>` The name of the device.
1872
1890
  * `path: <string>` The implementation dependent path of the device.
1873
1891
  * `guid: <string>` The GUID of the device.
1892
+ * `type: <string>|null` The type of the device.
1874
1893
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1875
1894
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1876
1895
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1878,6 +1897,25 @@ This can cause already opened controller instances to be [remapped](#event-remap
1878
1897
  * `mapping: <string>` The axis and button mapping for this device.
1879
1898
 
1880
1899
  A list of all the detected controller devices.
1900
+
1901
+ Possible values for `type` are `null` if it is unknown, or one of:
1902
+
1903
+ | Value | Corresponding `SDL_GameControllerType` |
1904
+ | --- | --- |
1905
+ | `'xbox360'` | SDL_CONTROLLER_TYPE_XBOX360 |
1906
+ | `'xboxOne'` | SDL_CONTROLLER_TYPE_XBOXONE |
1907
+ | `'ps3'` | SDL_CONTROLLER_TYPE_PS3 |
1908
+ | `'ps4'` | SDL_CONTROLLER_TYPE_PS4 |
1909
+ | `'nintendoSwitchPro'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO |
1910
+ | `'virtual'` | SDL_CONTROLLER_TYPE_VIRTUAL |
1911
+ | `'ps5'` | SDL_CONTROLLER_TYPE_PS5 |
1912
+ | `'amazonLuna'` | SDL_CONTROLLER_TYPE_AMAZON_LUNA |
1913
+ | `'googleStadia'` | SDL_CONTROLLER_TYPE_GOOGLE_STADIA |
1914
+ | `'nvidiaShield'` | SDL_CONTROLLER_TYPE_NVIDIA_SHIELD |
1915
+ | `'nintendoSwitchJoyconLeft'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT |
1916
+ | `'nintendoSwitchJoyconRight'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT |
1917
+ | `'nintendoSwitchJoyconPair'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR |
1918
+
1881
1919
  Sample output:
1882
1920
 
1883
1921
  ```js
@@ -1887,6 +1925,7 @@ Sample output:
1887
1925
  name: 'DragonRise Inc. Generic USB Joystick',
1888
1926
  path: '/dev/input/event21',
1889
1927
  guid: '03000000790000000600000010010000',
1928
+ type: null,
1890
1929
  vendor: 121,
1891
1930
  product: 6,
1892
1931
  version: 272,
package/binding.gyp CHANGED
@@ -24,6 +24,7 @@
24
24
  'NAPI_VERSION=<(napi_build_version)',
25
25
  'NODE_ADDON_API_DISABLE_DEPRECATED',
26
26
  ],
27
+ 'cflags': [ '-Werror', '-Wall', '-Wextra' ],
27
28
  'conditions': [
28
29
  ['OS == "linux"', {
29
30
  'cflags': [ '-D_REENTRANT' ],
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.5",
2
+ "version": "0.11.8",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -67,7 +67,7 @@
67
67
  "sdl": {
68
68
  "owner": "kmamal",
69
69
  "repo": "build-sdl",
70
- "version": "2.32.2"
70
+ "version": "2.32.8"
71
71
  },
72
72
  "devDependencies": {
73
73
  "@kmamal/evdev": "^0.0.2",
@@ -29,6 +29,9 @@ await Promise.all([
29
29
  Fs.promises.cp(
30
30
  Path.join(sdlDist, 'lib'),
31
31
  Path.join(C.dir.sdl, 'lib'),
32
- { recursive: true },
32
+ {
33
+ recursive: true,
34
+ verbatimSymlinks: true,
35
+ },
33
36
  ),
34
37
  ])
@@ -3,16 +3,19 @@ if (!process.env.NODE_SDL_FROM_SOURCE) {
3
3
  try {
4
4
  await import('./download-release.mjs')
5
5
  process.exit(0)
6
- } catch (_) {
6
+ }
7
+ catch (_) {
7
8
  console.log("failed to download release")
8
9
  }
9
- } else {
10
+ }
11
+ else {
10
12
  console.log("skip download and build from source")
11
13
  }
12
14
 
13
15
  try {
14
16
  await import('./download-sdl.mjs')
15
- } catch (_) {
17
+ }
18
+ catch (_) {
16
19
  console.log("failed to download sdl")
17
20
  await import('./build-sdl.mjs')
18
21
  }
@@ -30,7 +30,8 @@ const fetch = async (_url, options = {}) => {
30
30
  if (body?.length) {
31
31
  if (!Buffer.isBuffer(body)) { body = Buffer.from(body) }
32
32
  body = Promise.resolve(body)
33
- } else {
33
+ }
34
+ else {
34
35
  body = _consume(body)
35
36
  }
36
37
 
@@ -54,8 +55,10 @@ const fetch = async (_url, options = {}) => {
54
55
 
55
56
  if (!(200 <= statusCode && statusCode < 300)) {
56
57
  let responseBody
57
- try { responseBody = (await _consume(response)).toString() } catch (_) {}
58
- try { responseBody = JSON.parse(responseBody) } catch (_) {}
58
+ try { responseBody = (await _consume(response)).toString() }
59
+ catch (_) {}
60
+ try { responseBody = JSON.parse(responseBody) }
61
+ catch (_) {}
59
62
  throw Object.assign(new Error(`bad status code ${statusCode}`), {
60
63
  statusCode,
61
64
  responseBody,
@@ -19,9 +19,8 @@ class AudioInstance extends EventsViaPoll {
19
19
  buffered = 4096,
20
20
  } = options
21
21
 
22
- if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
23
- if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
24
- if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
22
+ if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("device.name must be a string"), { name }) }
23
+ // We already tested device.type in sdl.audio.openDevice()
25
24
  if (!Number.isInteger(channels)) { throw Object.assign(new Error("channels must be an integer"), { channels }) }
26
25
  if (![ 1, 2, 4, 6 ].includes(channels)) { throw Object.assign(new Error("invalid channels"), { channels }) }
27
26
  if (!Number.isInteger(frequency)) { throw Object.assign(new Error("frequency must be an integer"), { frequency }) }
@@ -1,10 +1,4 @@
1
1
 
2
- const make = (device) => {
3
- const { isRecorder } = device
4
- delete device.isRecorder
5
- device.type = isRecorder ? 'recording' : 'playback'
6
- }
7
-
8
2
  const compare = (a, b) => {
9
3
  const { name: aName } = a
10
4
  const { name: bName } = b
@@ -12,7 +6,4 @@ const compare = (a, b) => {
12
6
  return a.name < b.name ? -1 : 1
13
7
  }
14
8
 
15
- module.exports = {
16
- make,
17
- compare,
18
- }
9
+ module.exports = { compare }
@@ -1,19 +1,13 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
- const { make: makeAudioDevice } = require('./device')
4
3
  const { EventsViaPoll } = require('../events/events-via-poll')
5
4
  const { AudioPlaybackInstance } = require('./audio-playback-instance')
6
5
  const { AudioRecordingInstance } = require('./audio-recording-instance')
7
6
  const { AudioFormatHelpers } = require('./format-helpers')
8
7
 
8
+
9
9
  Globals.audioDevices.playback = Bindings.audio_getDevices(false)
10
- for (const audioDevice of Globals.audioDevices.playback) {
11
- makeAudioDevice(audioDevice)
12
- }
13
10
  Globals.audioDevices.recording = Bindings.audio_getDevices(true)
14
- for (const audioDevice of Globals.audioDevices.recording) {
15
- makeAudioDevice(audioDevice)
16
- }
17
11
 
18
12
 
19
13
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
@@ -30,6 +24,10 @@ const audio = new class extends EventsViaPoll {
30
24
  }
31
25
 
32
26
  openDevice (device, options = {}) {
27
+ if (typeof device !== 'object') { throw Object.assign(new Error("device must be an object"), { device }) }
28
+ if (typeof device.type !== 'string') { throw Object.assign(new Error("device.type must be a string"), { device }) }
29
+ if (![ 'playback', 'recording' ].includes(device.type)) { throw Object.assign(new Error("invalid device.type"), { device }) }
30
+
33
31
  return device.type === 'recording'
34
32
  ? new AudioRecordingInstance(device, options)
35
33
  : new AudioPlaybackInstance(device, options)
@@ -14,6 +14,8 @@ class ControllerInstance extends EventsViaPoll {
14
14
  constructor (device) {
15
15
  super(validEvents)
16
16
 
17
+ if (!Globals.controllerDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
18
+
17
19
  const result = Bindings.controller_open(device._index)
18
20
 
19
21
  this._firmwareVersion = result.firmwareVersion
@@ -1,10 +1,21 @@
1
1
 
2
2
  const make = (device) => {
3
- delete device.type
4
- delete device.isController
3
+ const {
4
+ isController,
5
+ controllerMapping,
6
+ controllerName,
7
+ controllerType,
8
+ ...rest
9
+ } = device
10
+ return {
11
+ ...rest,
12
+ mapping: controllerMapping,
13
+ name: controllerName,
14
+ type: controllerType,
15
+ }
5
16
  }
6
17
 
7
- const compare = (a, b) => a.id - b.id
18
+ const compare = (a, b) => a._index - b._index
8
19
 
9
20
  const filter = (device) => device.isController
10
21
 
@@ -1,18 +1,8 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
- const {
4
- make: makeControllerDevice,
5
- filter: filterControllerDevice,
6
- } = require('./device')
7
3
  const { EventsViaPoll } = require('../events/events-via-poll')
8
4
  const { ControllerInstance } = require('./controller-instance')
9
-
10
- Globals.controllerDevices = Bindings.joystick_getDevices()
11
- .filter(filterControllerDevice)
12
- for (const controllerDevice of Globals.controllerDevices) {
13
- makeControllerDevice(controllerDevice)
14
- }
15
-
5
+ const { reconcileJoystickAndControllerDevices } = require('../events/reconcile-joystick-and-controller-devices')
16
6
 
17
7
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
18
8
 
@@ -33,6 +23,10 @@ const controller = new class extends EventsViaPoll {
33
23
  }
34
24
 
35
25
  Bindings.controller_addMappings(mappings)
26
+ Globals.events.poll()
27
+
28
+ const devices = Bindings.joystick_getDevices()
29
+ reconcileJoystickAndControllerDevices(devices)
36
30
  }
37
31
  }()
38
32