@kmamal/sdl 0.11.4 → 0.11.7

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 (39) hide show
  1. package/README.md +70 -16
  2. package/binding.gyp +1 -0
  3. package/package.json +1 -1
  4. package/src/javascript/audio/audio-instance.js +2 -3
  5. package/src/javascript/audio/device.js +1 -10
  6. package/src/javascript/audio/index.js +5 -7
  7. package/src/javascript/controller/controller-instance.js +2 -0
  8. package/src/javascript/controller/device.js +14 -3
  9. package/src/javascript/controller/index.js +5 -11
  10. package/src/javascript/events/index.js +61 -123
  11. package/src/javascript/events/reconcile-audio-devices.js +15 -0
  12. package/src/javascript/events/reconcile-displays.js +16 -0
  13. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
  14. package/src/javascript/events/reconcile.js +61 -0
  15. package/src/javascript/joystick/device.js +9 -3
  16. package/src/javascript/joystick/index.js +13 -5
  17. package/src/javascript/joystick/joystick-instance.js +2 -0
  18. package/src/javascript/sensor/sensor-instance.js +2 -0
  19. package/src/javascript/video/display.js +4 -0
  20. package/src/javascript/video/index.js +1 -0
  21. package/src/javascript/video/window.js +6 -0
  22. package/src/native/audio.cpp +10 -12
  23. package/src/native/audio.h +4 -0
  24. package/src/native/controller.cpp +72 -56
  25. package/src/native/controller.h +3 -0
  26. package/src/native/enums.cpp +315 -315
  27. package/src/native/events.cpp +161 -79
  28. package/src/native/events.h +2 -0
  29. package/src/native/global.cpp +47 -20
  30. package/src/native/joystick.cpp +87 -48
  31. package/src/native/joystick.h +3 -2
  32. package/src/native/keyboard.cpp +1 -1
  33. package/src/native/mouse.cpp +2 -2
  34. package/src/native/power.cpp +9 -3
  35. package/src/native/sensor.cpp +24 -13
  36. package/src/native/video.cpp +21 -13
  37. package/src/native/video.h +1 -0
  38. package/src/native/window.cpp +41 -29
  39. package/src/types/index.d.ts +65 -17
package/README.md CHANGED
@@ -10,13 +10,13 @@ Provides window management, input events (keyboard, mouse, joysticks, controller
10
10
  It should work on Linux, Mac, and Windows.
11
11
  Prebuilt binaries are available for x64 architectures, arm-based Macs, and Raspberry Pi.
12
12
 
13
- #### Canvas, WebGL, and WebGPU
13
+ #### Canvas2D, WebGL, and WebGPU
14
14
 
15
- One goal of this project is to allow using Canvas, WebGL, and WebGPU without a browser.
16
- You can use the [canvas](https://www.npmjs.com/package/canvas) package to render using the canvas API.
15
+ One goal of this project is to allow using Canvas2D, WebGL, and WebGPU without a browser.
16
+ You can use the [@napi-rs/canvas](https://www.npmjs.com/package/@napi-rs/canvas) package to render using the Canvas2D API.
17
17
  For WebGL you can use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) and for WebGPU you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme).
18
18
  Both WebGL and WebGPU support rendering directly to the window (without any intermediate buffer copying).
19
- Canvas still uses an intermediate buffer, but that might change in the future.
19
+ Canvas2D still uses an intermediate buffer, but that might change in the future.
20
20
 
21
21
 
22
22
  ## Installation
@@ -43,14 +43,14 @@ const window = sdl.video.createWindow({ title: "Hello, World!" })
43
43
  window.on('*', console.log)
44
44
  ```
45
45
 
46
- ### Canvas
46
+ ### Canvas2D
47
47
 
48
48
  ```js
49
49
  import sdl from '@kmamal/sdl'
50
- import { createCanvas } from 'canvas'
50
+ import { createCanvas } from '@napi-rs/canvas'
51
51
 
52
52
  // Setup
53
- const window = sdl.video.createWindow({ title: "Canvas" })
53
+ const window = sdl.video.createWindow({ title: "Canvas2D" })
54
54
  const { pixelWidth: width, pixelHeight: height } = window
55
55
  const canvas = createCanvas(width, height)
56
56
  const ctx = canvas.getContext('2d')
@@ -60,8 +60,8 @@ ctx.fillStyle = 'red'
60
60
  ctx.fillRect(0, 0, width, height)
61
61
 
62
62
  // Render to window
63
- const buffer = canvas.toBuffer('raw')
64
- window.render(width, height, width * 4, 'bgra32', buffer)
63
+ const buffer = Buffer.from(ctx.getImageData(0, 0, width, height).data)
64
+ window.render(width, height, width * 4, 'rgba32', buffer)
65
65
  ```
66
66
 
67
67
  ### WebGL
@@ -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)
@@ -148,6 +149,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
148
149
  * [Event: 'restore'](#event-restore)
149
150
  * [Event: 'move'](#event-move)
150
151
  * [Event: 'resize'](#event-resize)
152
+ * [Event: 'displayChange'](#event-displaychange)
151
153
  * [Event: 'focus'](#event-focus)
152
154
  * [Event: 'blur'](#event-blur)
153
155
  * [Event: 'hover'](#event-hover)
@@ -177,6 +179,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
177
179
  * [window.pixelHeight](#windowpixelheight)
178
180
  * [window.setSize(width, height)](#windowsetsizewidth-height)
179
181
  * [window.setSizeInPixels(pixelWidth, pixelHeight)](#windowsetsizeinpixelspixelwidth-pixelheight)
182
+ * [window.display](#windowdisplay)
180
183
  * [window.visible](#windowvisible)
181
184
  * [window.show([show])](#windowshowshow)
182
185
  * [window.hide()](#windowhide)
@@ -509,9 +512,15 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
509
512
  ### Event: 'displayOrient'
510
513
 
511
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.
512
516
 
513
517
  Fired when a display changes orientation.
514
- 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.
515
524
 
516
525
 
517
526
  ### sdl.video.displays
@@ -528,8 +537,19 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
528
537
  * `horizontal: <number>` The horizontal density.
529
538
  * `vertical: <number>` The vertical density.
530
539
  * `diagonal: <number>` The diagonal density.
540
+ * `orientation: <string>|<null>` The orientation of the display.
531
541
 
532
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
+
533
553
  Sample output for two side-to-side monitors is below.
534
554
  Notice how the geometries don't overlap:
535
555
 
@@ -654,6 +674,12 @@ Fired when the window changes position.
654
674
 
655
675
  Fired when the window changes size.
656
676
 
677
+ ### Event: 'displayChange'
678
+
679
+ * `display: <object>` An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the window's new display.
680
+
681
+ Fired when a window moves from one display to another.
682
+
657
683
  ### Event: 'focus'
658
684
 
659
685
  Fired when a window gains the keyboard focus.
@@ -851,6 +877,13 @@ Changes the size of the window.
851
877
  Changes the size of the window.
852
878
  This function only behaves differently from [`window.setSize()`](#windowsetsizewidth-height) for [high-dpi](#high-dpi) displays.
853
879
 
880
+ ### window.display
881
+
882
+ * `<object>`
883
+
884
+ An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display this window belongs to.
885
+ If the window spans multiple displays, then the display containing the center of the window is returned.
886
+
854
887
  ### window.visible
855
888
 
856
889
  * `<boolean>`
@@ -1570,10 +1603,10 @@ When this event is emitted, all instances that were opened from the removed devi
1570
1603
 
1571
1604
  * `<object>[]`
1572
1605
  * `id: <number>` The unique id for the device.
1573
- * `type: <JoystickType>` The type of the device.
1574
1606
  * `name: <string>` The name of the device.
1575
1607
  * `path: <string>` The implementation dependent path of the device.
1576
1608
  * `guid: <string>` The GUID of the device.
1609
+ * `type: <JoystickType>|<null>` The type of the device.
1577
1610
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1578
1611
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1579
1612
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1581,7 +1614,7 @@ When this event is emitted, all instances that were opened from the removed devi
1581
1614
 
1582
1615
  A list of all the detected joystick devices.
1583
1616
 
1584
- Possible values for `type` are:
1617
+ Possible values for `type` are `null` if it is unknown, or one of:
1585
1618
 
1586
1619
  | Value | Corresponding `SDL_JoystickType` |
1587
1620
  | --- | --- |
@@ -1601,10 +1634,10 @@ Sample output:
1601
1634
  [
1602
1635
  {
1603
1636
  id: 0,
1604
- type: 'gamecontroller',
1605
1637
  name: 'DragonRise Inc. Generic USB Joystick',
1606
1638
  path: '/dev/input/event21',
1607
1639
  guid: '03000000790000000600000010010000',
1640
+ type: 'gamecontroller',
1608
1641
  vendor: 121,
1609
1642
  product: 6,
1610
1643
  version: 272,
@@ -1804,13 +1837,13 @@ Closes the instance.
1804
1837
  ## sdl.controller
1805
1838
 
1806
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").
1807
- 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.
1808
1841
  This makes it easy to provide consistent input bindings, like "press B to jump, move around with the left analog stick".
1809
1842
  With a pure joystick instance it's impossible to know which axis or button corresponds to which physical axis/button on the device.
1810
1843
 
1811
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).
1812
1845
  For a joystick device to also be available as a controller device it needs a "mapping".
1813
- 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.
1814
1847
  See the sample output [`here`](#sdlcontrollerdevices) for an example.
1815
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.
1816
1849
  You can add them via:
@@ -1819,7 +1852,7 @@ You can add them via:
1819
1852
  const url = 'https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt'
1820
1853
  const result = await fetch(url)
1821
1854
  const text = await result.text()
1822
- const mappings = text.split('\n')
1855
+ const mappings = text.split('\n').filter((line) => line && !line.startsWith('#'))
1823
1856
  sdl.controller.addMappings(mappings)
1824
1857
  ```
1825
1858
 
@@ -1856,6 +1889,7 @@ This can cause already opened controller instances to be [remapped](#event-remap
1856
1889
  * `name: <string>` The name of the device.
1857
1890
  * `path: <string>` The implementation dependent path of the device.
1858
1891
  * `guid: <string>` The GUID of the device.
1892
+ * `type: <string>|null` The type of the device.
1859
1893
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1860
1894
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1861
1895
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1863,6 +1897,25 @@ This can cause already opened controller instances to be [remapped](#event-remap
1863
1897
  * `mapping: <string>` The axis and button mapping for this device.
1864
1898
 
1865
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
+
1866
1919
  Sample output:
1867
1920
 
1868
1921
  ```js
@@ -1872,6 +1925,7 @@ Sample output:
1872
1925
  name: 'DragonRise Inc. Generic USB Joystick',
1873
1926
  path: '/dev/input/event21',
1874
1927
  guid: '03000000790000000600000010010000',
1928
+ type: null,
1875
1929
  vendor: 121,
1876
1930
  product: 6,
1877
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.4",
2
+ "version": "0.11.7",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -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