@kmamal/sdl 0.9.5 → 0.9.6-0

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 (55) hide show
  1. package/README.md +34 -15
  2. package/binding.gyp +25 -5
  3. package/package.json +7 -4
  4. package/src/javascript/audio/audio-instance.js +9 -6
  5. package/src/javascript/cleanup.js +1 -1
  6. package/src/javascript/clipboard/index.js +2 -1
  7. package/src/javascript/controller/controller-instance.js +6 -0
  8. package/src/javascript/enums.js +1 -1
  9. package/src/javascript/events/index.js +302 -305
  10. package/src/javascript/index.js +1 -1
  11. package/src/javascript/joystick/joystick-instance.js +7 -0
  12. package/src/javascript/keyboard/index.js +3 -3
  13. package/src/javascript/keyboard/key-mapping.js +14 -6
  14. package/src/javascript/mouse/index.js +13 -12
  15. package/src/javascript/power/index.js +0 -2
  16. package/src/javascript/sensor/sensor-instance.js +1 -5
  17. package/src/javascript/video/window.js +17 -16
  18. package/src/native/audio.cpp +178 -0
  19. package/src/native/audio.h +21 -0
  20. package/src/native/clipboard.cpp +45 -0
  21. package/src/native/clipboard.h +13 -0
  22. package/src/native/cocoa-global.h +8 -0
  23. package/src/native/cocoa-global.mm +8 -0
  24. package/src/native/cocoa-window.h +9 -4
  25. package/src/native/cocoa-window.mm +6 -9
  26. package/src/native/controller.cpp +154 -0
  27. package/src/native/controller.h +17 -0
  28. package/src/native/enums.cpp +477 -0
  29. package/src/native/enums.h +67 -0
  30. package/src/native/events.cpp +339 -0
  31. package/src/native/events.h +15 -0
  32. package/src/native/global.cpp +187 -0
  33. package/src/native/global.h +15 -0
  34. package/src/native/joystick.cpp +388 -0
  35. package/src/native/joystick.h +22 -0
  36. package/src/native/keyboard.cpp +55 -0
  37. package/src/native/keyboard.h +18 -0
  38. package/src/native/module.cpp +98 -0
  39. package/src/native/mouse.cpp +186 -0
  40. package/src/native/mouse.h +21 -0
  41. package/src/native/power.cpp +18 -0
  42. package/src/native/power.h +12 -0
  43. package/src/native/sensor.cpp +117 -0
  44. package/src/native/sensor.h +15 -0
  45. package/src/native/video.cpp +90 -0
  46. package/src/native/video.h +12 -0
  47. package/src/native/window.cpp +755 -0
  48. package/src/native/window.h +29 -0
  49. package/src/types/index.d.ts +7 -4
  50. package/scripts/test.mjs +0 -12
  51. package/src/native/asprintf.c +0 -47
  52. package/src/native/asprintf.h +0 -37
  53. package/src/native/binding.cpp +0 -1680
  54. package/src/native/sdl-helpers.cpp +0 -2617
  55. package/src/native/sdl-helpers.h +0 -182
package/README.md CHANGED
@@ -213,9 +213,11 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
213
213
  * [sdl.mouse.position](#sdlmouseposition)
214
214
  * [sdl.mouse.setPosition(x, y)](#sdlmousesetpositionx-y)
215
215
  * [sdl.mouse.setCursor(cursor)](#sdlmousesetcursorcursor)
216
+ * [sdl.mouse.resetCursor()](#sdlmouseresetcursor)
216
217
  * [sdl.mouse.setCursorImage(width, height, stride, format, buffer, x, y)](#sdlmousesetcursorimagewidth-height-stride-format-buffer-x-y)
217
218
  * [sdl.mouse.showCursor([show])](#sdlmouseshowcursorshow)
218
219
  * [sdl.mouse.hideCursor()](#sdlmousehidecursor)
220
+ * [sdl.mouse.redrawCursor()](#sdlmouseredrawcursor)
219
221
  * [sdl.mouse.capture([capture])](#sdlmousecapturecapture)
220
222
  * [sdl.mouse.uncapture()](#sdlmouseuncapture)
221
223
  * [sdl.joystick](#sdljoystick)
@@ -242,6 +244,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
242
244
  * [joystickInstance.hats](#joystickinstancehats)
243
245
  * [joystickInstance.power](#joystickinstancepower)
244
246
  * [joystickInstance.setPlayer(index)](#joystickinstancesetplayerindex)
247
+ * [joystickInstance.resetPlayer()](#joystickinstanceresetplayer)
245
248
  * [joystickInstance.hasLed](#joystickinstancehasled)
246
249
  * [joystickInstance.setLed(red, green, blue)](#joystickinstancesetledred-green-blue)
247
250
  * [joystickInstance.hasRumble](#joystickinstancehasrumble)
@@ -271,6 +274,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
271
274
  * [controllerInstance.buttons](#controllerinstancebuttons)
272
275
  * [controllerInstance.power](#controllerinstancepower)
273
276
  * [controllerInstance.setPlayer(index)](#controllerinstancesetplayerindex)
277
+ * [controllerInstance.resetPlayer()](#controllerinstanceresetplayer)
274
278
  * [controllerInstance.hasLed](#controllerinstancehasled)
275
279
  * [controllerInstance.setLed(red, green, blue)](#controllerinstancesetledred-green-blue)
276
280
  * [controllerInstance.hasRumble](#controllerinstancehasrumble)
@@ -282,7 +286,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
282
286
  * [controllerInstance.closed](#controllerinstanceclosed)
283
287
  * [controllerInstance.close()](#controllerinstanceclose)
284
288
  * [sdl.sensor](#sdlsensor)
285
- * [sdl.sensor.STANDARD_GRAVITY](#sdlsensorstandardgravity)
289
+ * [sdl.sensor.STANDARD_GRAVITY](#sdlsensorstandard_gravity)
286
290
  * [sdl.sensor.devices](#sdlsensordevices)
287
291
  * [sdl.sensor.openDevice(device)](#sdlsensoropendevicedevice)
288
292
  * [class SensorInstance](#class-sensorinstance)
@@ -1092,7 +1096,6 @@ Values are based on the [USB usage page standard](https://www.usb.org/sites/defa
1092
1096
 
1093
1097
  | Value | Corresponding `SDL_Scancode` | Comment |
1094
1098
  | --- | --- | --- |
1095
- | `sdl.keyboard.SCANCODE.UNKNOWN` | `SDL_SCANCODE_UNKNOWN` | value `0` |
1096
1099
  | `sdl.keyboard.SCANCODE.A` | `SDL_SCANCODE_A` | |
1097
1100
  | `sdl.keyboard.SCANCODE.B` | `SDL_SCANCODE_B` | |
1098
1101
  | `sdl.keyboard.SCANCODE.C` | `SDL_SCANCODE_C` | |
@@ -1353,13 +1356,15 @@ All others are represented by one of these values:
1353
1356
  * Returns: [`<Key>`](#virtual-keys)`|<null>`
1354
1357
 
1355
1358
  Maps a scancode to the corresponding key based on the current keyboard mapping.
1359
+ Retuns `null` if the scancode does not currespond to a key in the current mapping.
1356
1360
 
1357
1361
  ### sdl.keyboard.getScancode(key)
1358
1362
 
1359
1363
  * `key: `[`<Key>`](#virtual-keys)
1360
- * Returns: [`<Scancode>`](#enum-scancode)
1364
+ * Returns: [`<Scancode>`](#enum-scancode)`|<null>`
1361
1365
 
1362
1366
  Maps a key to the corresponding scancode based on the current keyboard mapping.
1367
+ Retuns `null` if the key does not currespond to a scancode in the current mapping.
1363
1368
  If multiple physical keys produce the same virtual key, then only the first one will be returned.
1364
1369
 
1365
1370
  ### sdl.keyboard.getState()
@@ -1430,6 +1435,10 @@ Moves the mouse to the specified position.
1430
1435
 
1431
1436
  Changes the icon that is displayed for the mouse cursor.
1432
1437
 
1438
+ ### sdl.mouse.resetCursor()
1439
+
1440
+ Switched back to the default cursor.
1441
+
1433
1442
  ### sdl.mouse.setCursorImage(width, height, stride, format, buffer, x, y)
1434
1443
 
1435
1444
  * `width, height, stride, format, buffer: `[`<Image>`](#image-data) The image to use as a cursor.
@@ -1447,7 +1456,11 @@ Changes the visibility of the mouse cursor.
1447
1456
 
1448
1457
  ### sdl.mouse.hideCursor()
1449
1458
 
1450
- Equivalent to [`sdl.mouse.showCursor(false)`](#sdlmouseshowcursorshow).
1459
+ Equivalent to [`sdl.mouse.showCursor
1460
+
1461
+ ### sdl.mouse.redrawCursor()
1462
+
1463
+ Forces a cursor redraw.
1451
1464
 
1452
1465
  ### sdl.mouse.capture([capture])
1453
1466
 
@@ -1468,7 +1481,6 @@ String values used to represent the type of a joystick device.
1468
1481
 
1469
1482
  | Value | Corresponding `SDL_JoystickType` |
1470
1483
  | --- | --- |
1471
- | `'unknown'` | `SDL_JOYSTICK_TYPE_UNKNOWN` |
1472
1484
  | `'gamecontroller'` | `SDL_JOYSTICK_TYPE_GAMECONTROLLER` |
1473
1485
  | `'wheel'` | `SDL_JOYSTICK_TYPE_WHEEL` |
1474
1486
  | `'arcadestick'` | `SDL_JOYSTICK_TYPE_ARCADE_STICK` |
@@ -1501,7 +1513,6 @@ String values used to represent the power level of a joystick device.
1501
1513
 
1502
1514
  | Value | Corresponding `SDL_JoystickPowerLevel` |
1503
1515
  | --- | --- |
1504
- | `'unknown'` | `SDL_JOYSTICK_POWER_UNKNOWN` |
1505
1516
  | `'empty'` | `SDL_JOYSTICK_POWER_EMPTY` |
1506
1517
  | `'low'` | `SDL_JOYSTICK_POWER_LOW` |
1507
1518
  | `'medium'` | `SDL_JOYSTICK_POWER_MEDIUM` |
@@ -1627,9 +1638,9 @@ The [device](#sdljoystickdevices) from which this instance was opened.
1627
1638
 
1628
1639
  ### joystickInstance.firmwareVersion
1629
1640
 
1630
- * `<number>`
1641
+ * `<number>|<null>`
1631
1642
 
1632
- The joystick's firmware version.
1643
+ The joystick's firmware version, or `null` if it's not available..
1633
1644
 
1634
1645
  ### joystickInstance.serialNumber
1635
1646
 
@@ -1668,9 +1679,9 @@ An array of values, each corresponding to the position of one of the joystick's
1668
1679
 
1669
1680
  ### joystickInstance.power
1670
1681
 
1671
- * [`<PowerLevel>`](#power-levels)
1682
+ * [`<PowerLevel>`](#power-levels)`|<null>`
1672
1683
 
1673
- The current power level of the joystick device.
1684
+ The current power level of the joystick device, or `null` if it is unknown;
1674
1685
 
1675
1686
  ### joystickInstance.setPlayer(index)
1676
1687
 
@@ -1678,6 +1689,10 @@ The current power level of the joystick device.
1678
1689
 
1679
1690
  Sets the player index of this joystick.
1680
1691
 
1692
+ ### joystickInstance.resetPlayer()
1693
+
1694
+ Clears player assignment and player led.
1695
+
1681
1696
  ### joystickInstance.hasLed
1682
1697
 
1683
1698
  * `<boolean>`
@@ -1942,6 +1957,10 @@ The current power level of the controller device.
1942
1957
 
1943
1958
  Sets the player index of this controller.
1944
1959
 
1960
+ ### controllerInstance.resetPlayer()
1961
+
1962
+ Clears player assignment and player led.
1963
+
1945
1964
  ### controllerInstance.hasLed
1946
1965
 
1947
1966
  * `<boolean>`
@@ -2014,7 +2033,7 @@ Closes the instance.
2014
2033
 
2015
2034
  * `<number>`
2016
2035
 
2017
- The readings from accelerometers will include the force of gravity, so a device at rest will have an value of `STANDARD_GRAVITY` away from the center of the earth.
2036
+ The readings from accelerometers will include the force of gravity, so a device at rest will have a value of `STANDARD_GRAVITY` away from the center of the earth.
2018
2037
  Use this constant to correct for that if your application requires it.
2019
2038
 
2020
2039
  ### sdl.sensor.devices
@@ -2023,7 +2042,7 @@ Use this constant to correct for that if your application requires it.
2023
2042
  * `id: <number>` The unique id of the device.
2024
2043
  * `name: <string>` The name of the device.
2025
2044
  * `type: <string>` Will be either `'accelerometer'`, `'gyroscope'`, or `'unknown'`.
2026
- * `side: <string | null>` Some sensors (such as those on the Joy-Con controller) have a `'left'` or `'right'` side, otherwise this value will be `null`.
2045
+ * `side: <string>|<null>` Some sensors (such as those on the Joy-Con controller) have a `'left'` or `'right'` side, otherwise this value will be `null`.
2027
2046
 
2028
2047
  A list of all the detected sensor devices.
2029
2048
  Sample output:
@@ -2065,7 +2084,7 @@ The [device](#sdlsensordevices) from which this instance was opened.
2065
2084
  ### sensorInstance.data
2066
2085
 
2067
2086
  * `<object>`
2068
- * `timestamp: <number | null>` The time this measurement was taken **in microseconds**, or `null` if that's not available.
2087
+ * `timestamp: <number>|<null>` The time this measurement was taken **in microseconds**, or `null` if that's not available.
2069
2088
  * `x: <number>` X axis value.
2070
2089
  * `y: <number>` Y axis value.
2071
2090
  * `z: <number>` Z axis value.
@@ -2518,8 +2537,8 @@ Changes the text contents of the clipboard.
2518
2537
 
2519
2538
  * `<object>`
2520
2539
  * `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'charged'`.
2521
- * `seconds: <number | null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2522
- * `percent: <number | null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2540
+ * `seconds: <number>|<null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2541
+ * `percent: <number>|<null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2523
2542
 
2524
2543
  The curent power information of the device.
2525
2544
 
package/binding.gyp CHANGED
@@ -2,10 +2,28 @@
2
2
  'targets': [{
3
3
  'target_name': 'sdl',
4
4
  'sources': [
5
- 'src/native/binding.cpp',
6
- 'src/native/sdl-helpers.cpp',
5
+ 'src/native/module.cpp',
6
+ 'src/native/enums.cpp',
7
+ 'src/native/global.cpp',
8
+ 'src/native/events.cpp',
9
+ 'src/native/video.cpp',
10
+ 'src/native/window.cpp',
11
+ 'src/native/keyboard.cpp',
12
+ 'src/native/mouse.cpp',
13
+ 'src/native/joystick.cpp',
14
+ 'src/native/controller.cpp',
15
+ 'src/native/sensor.cpp',
16
+ 'src/native/audio.cpp',
17
+ 'src/native/clipboard.cpp',
18
+ 'src/native/power.cpp',
19
+ ],
20
+ 'dependencies': [
21
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
22
+ ],
23
+ 'defines': [
24
+ 'NAPI_VERSION=<(napi_build_version)',
25
+ 'NODE_ADDON_API_DISABLE_DEPRECATED',
7
26
  ],
8
- 'defines': [ 'NAPI_VERSION=<(napi_build_version)' ],
9
27
  'conditions': [
10
28
  ['OS == "linux"', {
11
29
  'cflags': [ '-D_REENTRANT' ],
@@ -17,7 +35,10 @@
17
35
  },
18
36
  }],
19
37
  ['OS == "mac"', {
20
- 'sources': [ 'src/native/cocoa-window.mm' ],
38
+ 'sources': [
39
+ 'src/native/cocoa-global.mm',
40
+ 'src/native/cocoa-window.mm',
41
+ ],
21
42
  'cflags': [ '-D_THREAD_SAFE' ],
22
43
  'xcode_settings': { 'OTHER_CFLAGS': [ '-std=c++17' ] },
23
44
  'include_dirs': [
@@ -30,7 +51,6 @@
30
51
  },
31
52
  }],
32
53
  ['OS == "win"', {
33
- 'sources': [ 'src/native/asprintf.c' ],
34
54
  'cflags': [ '-D_REENTRANT' ],
35
55
  'msvs_settings': {
36
56
  'VCCLCompilerTool': {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.5",
2
+ "version": "0.9.6-0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "gypfile": true,
48
48
  "scripts": {
49
- "test": "cd scripts && node test.mjs",
49
+ "test": "npx @kmamal/testing",
50
50
  "install": "cd scripts && node install.mjs",
51
51
  "download-release": "cd scripts && node download-release.mjs",
52
52
  "download-sdl": "cd scripts && node download-sdl.mjs",
@@ -56,7 +56,7 @@
56
56
  "clean": "cd scripts && node clean.mjs"
57
57
  },
58
58
  "dependencies": {
59
- "tar": "^6.1.15"
59
+ "tar": "^6.2.0"
60
60
  },
61
61
  "sdl": {
62
62
  "owner": "kmamal",
@@ -64,6 +64,9 @@
64
64
  "version": "2.26.4"
65
65
  },
66
66
  "devDependencies": {
67
- "@types/node": "^20.2.3"
67
+ "@kmamal/testing": "^0.0.27",
68
+ "@types/node": "^20.11.5",
69
+ "node-addon-api": "^7.1.0",
70
+ "node-gyp": "^10.0.1"
68
71
  }
69
72
  }
@@ -19,8 +19,6 @@ class AudioInstance extends EventsViaPoll {
19
19
  buffered = 4096,
20
20
  } = options
21
21
 
22
- const _format = Enums.audioFormat[format] ?? null
23
-
24
22
  if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
25
23
  if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
26
24
  if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
@@ -29,11 +27,13 @@ class AudioInstance extends EventsViaPoll {
29
27
  if (!Number.isFinite(frequency)) { throw Object.assign(new Error("frequency must be a number"), { frequency }) }
30
28
  if (frequency <= 0) { throw Object.assign(new Error("invalid frequency"), { frequency }) }
31
29
  if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
32
- if (_format === null) { throw Object.assign(new Error("invalid format"), { format }) }
33
30
  if (!Number.isFinite(buffered)) { throw Object.assign(new Error("buffered must be a number"), { buffered }) }
34
31
  if (buffered !== 2 ** (32 - Math.clz32(buffered) - 1)) { throw Object.assign(new Error("invalid buffered"), { buffered }) }
35
32
 
36
- this._id = Bindings.audio_openDevice(name ?? '', type === 'recording', frequency, _format, channels, buffered)
33
+ const _format = Enums.audioFormat[format]
34
+ if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
35
+
36
+ this._id = Bindings.audio_open(name ?? null, type === 'recording', frequency, _format, channels, buffered)
37
37
 
38
38
  this._device = device
39
39
  this._name = name
@@ -42,7 +42,7 @@ class AudioInstance extends EventsViaPoll {
42
42
  this._format = format
43
43
  this._frequency = frequency
44
44
 
45
- this._playing = true
45
+ this._playing = false
46
46
  this._closed = false
47
47
 
48
48
  const helper = AudioFormatHelpers[this._format]
@@ -90,8 +90,10 @@ class AudioInstance extends EventsViaPoll {
90
90
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
91
91
 
92
92
  if (typeof play !== 'boolean') { throw Object.assign(new Error("play must be a boolean"), { play }) }
93
- this._playing = play
93
+
94
94
  Bindings.audio_play(this._id, play)
95
+
96
+ this._playing = play
95
97
  }
96
98
 
97
99
  pause () {
@@ -119,6 +121,7 @@ class AudioInstance extends EventsViaPoll {
119
121
  this._closed = true
120
122
 
121
123
  Bindings.audio_close(this._id)
124
+
122
125
  Globals.audioInstances.delete(this._id)
123
126
  }
124
127
  }
@@ -26,5 +26,5 @@ process.on('exit', (code) => {
26
26
  controller.close()
27
27
  }
28
28
 
29
- Bindings.cleanup()
29
+ Bindings.global_cleanup()
30
30
  })
@@ -6,9 +6,10 @@ const validEvents = [ 'update' ]
6
6
  const clipboard = new class extends EventsViaPoll {
7
7
  constructor () { super(validEvents) }
8
8
 
9
- get text () { return Bindings.clipboard_getText() ?? '' }
9
+ get text () { return Bindings.clipboard_getText() }
10
10
  setText (text) {
11
11
  if (typeof text !== 'string') { throw Object.assign(new Error("text must be a string"), { text }) }
12
+
12
13
  Bindings.clipboard_setText(text)
13
14
  }
14
15
  }()
@@ -61,6 +61,12 @@ class ControllerInstance extends EventsViaPoll {
61
61
  Bindings.joystick_setPlayer(this._device.id, player)
62
62
  }
63
63
 
64
+ resetPlayer () {
65
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
66
+
67
+ Bindings.joystick_setPlayer(this._device.id, -1)
68
+ }
69
+
64
70
  get hasLed () { return this._hasLed }
65
71
  setLed (red, green, blue) {
66
72
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
@@ -1,6 +1,6 @@
1
1
  const Bindings = require('./bindings')
2
2
 
3
- const enums = Bindings.getEnums()
3
+ const enums = Bindings.enums_get()
4
4
 
5
5
  for (const [ key, obj ] of Object.entries(enums)) {
6
6
  const names = {}