@kmamal/sdl 0.8.0 → 0.8.4

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.
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
7
  SDL bindings for Node.js.
8
- Provides window management, input events (keyboard, mouse, joystick, controller), audio playback and recording, and clipboard manipulation.
8
+ Provides window management, input events (keyboard, mouse, joysticks, controllers, sensors), audio playback and recording, clipboard manipulation, and battery status.
9
9
 
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.
@@ -88,6 +88,9 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
88
88
  * [Image data](#image-data)
89
89
  * [Pixel formats](#pixel-formats)
90
90
  * [High-DPI](#highdpi)
91
+ * [Event: 'displayAdd'](#event-displayadd)
92
+ * [Event: 'displayRemove'](#event-displayremove)
93
+ * [Event: 'displayOrient'](#event-displayorient)
91
94
  * [sdl.video.displays](#sdlvideodisplays)
92
95
  * [sdl.video.windows](#sdlvideowindows)
93
96
  * [sdl.video.focused](#sdlvideofocused)
@@ -184,8 +187,8 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
184
187
  * [Joystick types](#joystick-types)
185
188
  * [Hat positions](#hat-positions)
186
189
  * [Power levels](#power-levels)
187
- * [Event: 'deviceAdd'](#joystick-event-device-add)
188
- * [Event: 'deviceRemove'](#joystick-event-device-remove)
190
+ * [Event: 'deviceAdd'](#joystick-event-deviceadd)
191
+ * [Event: 'deviceRemove'](#joystick-event-deviceremove)
189
192
  * [sdl.joystick.devices](#sdljoystickdevices)
190
193
  * [sdl.joystick.openDevice(device)](#sdljoystickopendevicedevice)
191
194
  * [class JoystickInstance](#class-joystickinstance)
@@ -215,8 +218,8 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
215
218
  * [joystickInstance.closed](#joystickinstanceclosed)
216
219
  * [joystickInstance.close()](#joystickinstanceclose)
217
220
  * [sdl.controller](#sdlcontroller)
218
- * [Event: 'deviceAdd'](#controller-event-device-add)
219
- * [Event: 'deviceRemove'](#controller-event-device-remove)
221
+ * [Event: 'deviceAdd'](#controller-event-deviceadd)
222
+ * [Event: 'deviceRemove'](#controller-event-deviceremove)
220
223
  * [sdl.controller.addMappings(mappings)](#sdlcontrolleraddmappingsmappings)
221
224
  * [sdl.controller.devices](#sdlcontrollerdevices)
222
225
  * [sdl.controller.openDevice(device)](#sdlcontrolleropendevicedevice)
@@ -243,11 +246,21 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
243
246
  * [controllerInstance.stopRumbleTriggers()](#controllerinstancestoprumbletriggers)
244
247
  * [controllerInstance.closed](#controllerinstanceclosed)
245
248
  * [controllerInstance.close()](#controllerinstanceclose)
249
+ * [sdl.sensor](#sdlsensor)
250
+ * [sdl.sensor.STANDARD_GRAVITY](#sdlsensorstandardgravity)
251
+ * [sdl.sensor.devices](#sdlsensordevices)
252
+ * [sdl.sensor.openDevice(device)](#sdlsensoropendevicedevice)
253
+ * [class SensorInstance](#class-sensorinstance)
254
+ * [Event: 'update'](#sensor-instance-event-update)
255
+ * [sensorInstance.device](#sensorinstancedevice)
256
+ * [sensorInstance.data](#sensorinstancedata)
257
+ * [sensorInstance.closed](#sensorinstanceclosed)
258
+ * [sensorInstance.close()](#sensorinstanceclose)
246
259
  * [sdl.audio](#sdlaudio)
247
260
  * [Audio data](#audio-data)
248
261
  * [Sample formats](#sample-formats)
249
- * [Event: 'deviceAdd'](#audio-event-device-add)
250
- * [Event: 'deviceRemove'](#audio-event-device-remove)
262
+ * [Event: 'deviceAdd'](#audio-event-deviceadd)
263
+ * [Event: 'deviceRemove'](#audio-event-deviceremove)
251
264
  * [sdl.audio.bytesPerSample(format)](#sdlaudiobytespersampleformat)
252
265
  * [sdl.audio.minSampleValue(format)](#sdlaudiominsamplevalueformat)
253
266
  * [sdl.audio.maxSampleValue(format)](#sdlaudiomaxsamplevalueformat)
@@ -288,6 +301,8 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
288
301
  * [Event: 'update'](#event-update)
289
302
  * [sdl.clipboard.text](#sdlclipboardtext)
290
303
  * [sdl.clipboard.setText(text)](#sdlclipboardsettexttext)
304
+ * [sdl.power](#sdlpower)
305
+ * [sdl.power.info](#sdlpowerinfo)
291
306
 
292
307
 
293
308
  ## sdl
@@ -428,6 +443,30 @@ If you need to know a window's width and height in pixels, you should use the `p
428
443
  You should be doing this always, since you don't know beforehand if your program will be running on a high-dpi system.
429
444
 
430
445
 
446
+ ### Event: 'displayAdd'
447
+
448
+ * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
449
+
450
+ Fired when a display is added to the system.
451
+ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
452
+
453
+
454
+ ### Event: 'displayRemove'
455
+
456
+ * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
457
+
458
+ Fired when a display is removed from the system.
459
+ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
460
+
461
+
462
+ ### Event: 'displayOrient'
463
+
464
+ * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
465
+
466
+ Fired when a display changes orientation.
467
+ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
468
+
469
+
431
470
  ### sdl.video.displays
432
471
 
433
472
  * `<object>[]`
@@ -1409,7 +1448,7 @@ String values used to represent the power level of a joystick device.
1409
1448
  | `'wired'` | `SDL_JOYSTICK_POWER_WIRED` |
1410
1449
  | `'max'` | `SDL_JOYSTICK_POWER_MAX` |
1411
1450
 
1412
- <a id="joystick-event-device-add"></a>
1451
+ <a id="joystick-event-deviceadd"></a>
1413
1452
 
1414
1453
  ### Event: 'deviceAdd'
1415
1454
 
@@ -1418,7 +1457,7 @@ String values used to represent the power level of a joystick device.
1418
1457
  Fired when a new joystick device becomes available.
1419
1458
  Check [`sdl.joystick.devices`](#sdljoystickdevices) to get the new list of joystick devices.
1420
1459
 
1421
- <a id="joystick-event-device-remove"></a>
1460
+ <a id="joystick-event-deviceremove"></a>
1422
1461
 
1423
1462
  ### Event: 'deviceRemove'
1424
1463
 
@@ -1665,7 +1704,7 @@ const mappings = text.split('\n')
1665
1704
  sdl.controller.addMappings(mappings)
1666
1705
  ```
1667
1706
 
1668
- <a id="controller-event-device-add"></a>
1707
+ <a id="controller-event-deviceadd"></a>
1669
1708
 
1670
1709
  ### Event: 'deviceAdd'
1671
1710
 
@@ -1674,7 +1713,7 @@ sdl.controller.addMappings(mappings)
1674
1713
  Fired when a new controller device becomes available.
1675
1714
  Check [`sdl.controller.devices`](#sdlcontrollerdevices) to get the new list of controller devices.
1676
1715
 
1677
- <a id="controller-event-device-remove"></a>
1716
+ <a id="controller-event-deviceremove"></a>
1678
1717
 
1679
1718
  ### Event: 'deviceRemove'
1680
1719
 
@@ -1907,6 +1946,95 @@ A closed instance object should not be used any further.
1907
1946
 
1908
1947
  Closes the instance.
1909
1948
 
1949
+
1950
+ ## sdl.sensor
1951
+
1952
+ ### sdl.sensor.STANDARD_GRAVITY
1953
+
1954
+ * `<number>`
1955
+
1956
+ 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.
1957
+ Use this constant to correct for that if your application requires it.
1958
+
1959
+ ### sdl.sensor.devices
1960
+
1961
+ * `<object>[]`
1962
+ * `id: <number>` The unique id of the device.
1963
+ * `name: <string>` The name of the device.
1964
+ * `type: <string>` Will be either `'accelerometer'`, `'gyroscope'`, or `'unknown'`.
1965
+ * `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`.
1966
+
1967
+ A list of all the detected sensor devices.
1968
+ Sample output:
1969
+
1970
+ // TODO
1971
+
1972
+ ### sdl.sensor.openDevice(device)
1973
+
1974
+ * `device: <object>` An object from [`sdl.sensor.devices`](#sdlsensordevices) that should be opened.
1975
+ * Returns: [`<SensorInstance>`](#class-sensorinstance) an object representing the opened sensor device instance.
1976
+
1977
+ Initializes a sensor device and returns a corresponding instance.
1978
+
1979
+ ## class SensorInstance
1980
+
1981
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
1982
+ Instead, objects returned by [`sdl.sensor.openDevice()`](#sdlsensoropendevicedevice) are of this type.
1983
+
1984
+ <a id="sensor-instance-event-update"></a>
1985
+
1986
+ ### Event: 'update'
1987
+
1988
+ Fired when the sensor's data changes.
1989
+ Get the new data by accessing [`sensorInstance.data`](#sensorInstancedata)
1990
+
1991
+ <a id="sensor-instance-event-close"></a>
1992
+
1993
+ ### Event: 'close'
1994
+
1995
+ Fired when the instance is about to close.
1996
+ Handle cleanup here.
1997
+
1998
+ ### sensorInstance.device
1999
+
2000
+ * `<object>`
2001
+
2002
+ The [device](#sdlsensordevices) from which this instance was opened.
2003
+
2004
+ ### sensorInstance.data
2005
+
2006
+ * `<object>`
2007
+ * `timestamp: <number | null>` The time this measurement was taken **in microseconds**, or `null` if that's not available.
2008
+ * `x: <number>` X axis value.
2009
+ * `y: <number>` Y axis value.
2010
+ * `z: <number>` Z axis value.
2011
+
2012
+ An object reporting the latest measurement from the sensor.
2013
+
2014
+ For accelerometers, the values correspond to the current acceleration in meters per second squared.
2015
+ This measurement includes the force of gravity, so a device at rest will have an value of [`sdl.sensor.STANDARD_GRAVITY`](#sdlsensorstandard_gravity) away from the center of the earth.
2016
+
2017
+ For gyroscopes, the values correspond to the current rate of rotation in radians per second.
2018
+ The rotation is positive in the counter-clockwise direction.
2019
+ That is, an observer looking from a positive location on one of the axes would see positive rotation on that axis when it appeared to be rotating counter-clockwise.
2020
+
2021
+ For phones held in portrait mode and game controllers held in front of you, the axes are defined as follows:
2022
+ * -X ... +X : left ... right
2023
+ * -Y ... +Y : bottom ... top
2024
+ * -Z ... +Z : farther ... closer
2025
+
2026
+ ### sensorInstance.closed
2027
+
2028
+ * `<boolean>`
2029
+
2030
+ Will be `true` if the instance is closed.
2031
+ A closed instance object should not be used any further.
2032
+
2033
+ ### sensorInstance.close()
2034
+
2035
+ Closes the instance.
2036
+
2037
+
1910
2038
  ## sdl.audio
1911
2039
 
1912
2040
  ### Audio data
@@ -1989,7 +2117,7 @@ String values used to represent how audio samples are stored in a Buffer.
1989
2117
  | `'f32sys'` | `AUDIO_F32SYS` | 32-bit floating point samples in native byte order |
1990
2118
  | `'f32'` | `AUDIO_F32` | alias for `'f32lsb'` |
1991
2119
 
1992
- <a id="audio-event-device-add"></a>
2120
+ <a id="audio-event-deviceadd"></a>
1993
2121
 
1994
2122
  ### Event: 'deviceAdd'
1995
2123
 
@@ -1998,7 +2126,7 @@ String values used to represent how audio samples are stored in a Buffer.
1998
2126
  Fired when a new audio device becomes available.
1999
2127
  Check [`sdl.audio.devices`](#sdlaudiodevices) to get the new list of audio devices.
2000
2128
 
2001
- <a id="audio-event-device-remove"></a>
2129
+ <a id="audio-event-deviceremove"></a>
2002
2130
 
2003
2131
  ### Event: 'deviceRemove'
2004
2132
 
@@ -2323,6 +2451,18 @@ The current text value on the clipboard.
2323
2451
  Changes the text contents of the clipboard.
2324
2452
 
2325
2453
 
2454
+ ## sdl.power
2455
+
2456
+ ### sdl.power.info
2457
+
2458
+ * `<object>`
2459
+ * `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'charged'`.
2460
+ * `seconds: <number | null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2461
+ * `percent: <number | null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2462
+
2463
+ The curent power information of the device.
2464
+
2465
+
2326
2466
  ## Building from source
2327
2467
 
2328
2468
  If there are no prebuilt binaries available for your platform, `@kmamal/sdl` will try to compile itself during installation.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.0",
2
+ "version": "0.8.4",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -27,6 +27,8 @@
27
27
  "microphone",
28
28
  "mic",
29
29
  "clipboard",
30
+ "power",
31
+ "battery",
30
32
  "game"
31
33
  ],
32
34
  "license": "MIT",
@@ -7,7 +7,7 @@ const {
7
7
  const { EventsViaPoll } = require('../events/events-via-poll')
8
8
  const { ControllerInstance } = require('./controller-instance')
9
9
 
10
- Globals.controllerDevices = Bindings.joystick_getDevices(false)
10
+ Globals.controllerDevices = Bindings.joystick_getDevices()
11
11
  .filter(filterControllerDevice)
12
12
  for (const controllerDevice of Globals.controllerDevices) {
13
13
  makeControllerDevice(controllerDevice)
@@ -1,6 +1,7 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
3
  const Enums = require('../enums')
4
+ const { video: videoModule } = require('../video')
4
5
  const { mapping } = require('../keyboard/key-mapping')
5
6
  const { joystick: joystickModule } = require('../joystick')
6
7
  const { controller: controllerModule } = require('../controller')
@@ -77,6 +78,24 @@ const poll = () => {
77
78
  event.type = Enums.eventTypeNames[type]
78
79
 
79
80
  switch (family) {
81
+ case Enums.eventFamily.display: {
82
+ const { displayIndex } = event
83
+ delete event.displayIndex
84
+
85
+ const oldDisplays = Globals.displays
86
+ Globals.displays = Bindings.video_getDisplays()
87
+ for (const display of Globals.displays) {
88
+ display.format = Enums.pixelFormatNames[display.format]
89
+ }
90
+ const newDisplays = Globals.displays
91
+
92
+ event.display = event.type === Enums.eventType.displayRemove
93
+ ? oldDisplays[displayIndex]
94
+ : newDisplays[displayIndex]
95
+
96
+ videoModule.emit(event.type, event)
97
+ } break
98
+
80
99
  case Enums.eventFamily.window: {
81
100
  const { windowId } = event
82
101
  delete event.windowId
@@ -331,6 +350,24 @@ const poll = () => {
331
350
  }
332
351
  } break
333
352
 
353
+ case Enums.eventFamily.sensor: {
354
+ const { sensorId } = event
355
+ delete event.sensorId
356
+
357
+ const collection = Globals.sensorInstances.byId.get(sensorId)
358
+ if (!collection) { continue }
359
+
360
+ switch (type) {
361
+ case Enums.eventType.update: {
362
+ for (const sensorInstance of collection.values()) {
363
+ sensorInstance.emit(event.type, event)
364
+ }
365
+ } break
366
+
367
+ default: continue
368
+ }
369
+ } break
370
+
334
371
  case Enums.eventFamily.audioDevice: {
335
372
  const { isRecorder } = event
336
373
  delete event.isRecorder
@@ -1,15 +1,11 @@
1
1
 
2
2
  module.exports = {
3
+ displays: [],
3
4
  windows: {
4
5
  all: new Map(),
5
6
  focused: null,
6
7
  hovered: null,
7
8
  },
8
- audioDevices: {
9
- playback: [],
10
- recording: [],
11
- },
12
- audioInstances: new Map(),
13
9
  joystickDevices: [],
14
10
  joystickInstances: {
15
11
  all: new Set(),
@@ -20,6 +16,15 @@ module.exports = {
20
16
  all: new Set(),
21
17
  byId: new Map(),
22
18
  },
19
+ sensorInstances: {
20
+ all: new Set(),
21
+ byId: new Map(),
22
+ },
23
+ audioDevices: {
24
+ playback: [],
25
+ recording: [],
26
+ },
27
+ audioInstances: new Map(),
23
28
 
24
29
  events: null, // Set later to { startPolling, stopPolling, poll }
25
30
  }
@@ -10,8 +10,10 @@ const { keyboard } = require('./keyboard')
10
10
  const { mouse } = require('./mouse')
11
11
  const { joystick } = require('./joystick')
12
12
  const { controller } = require('./controller')
13
+ const { sensor } = require('./sensor')
13
14
  const { audio } = require('./audio')
14
15
  const { clipboard } = require('./clipboard')
16
+ const { power } = require('./power')
15
17
 
16
18
  require('./events')
17
19
  require('./cleanup')
@@ -23,6 +25,8 @@ module.exports = {
23
25
  mouse,
24
26
  joystick,
25
27
  controller,
28
+ sensor,
26
29
  audio,
27
30
  clipboard,
31
+ power,
28
32
  }
@@ -4,7 +4,7 @@ const { make: makeJoystickDevice } = require('./device')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
5
  const { JoystickInstance } = require('./joystick-instance')
6
6
 
7
- Globals.joystickDevices = Bindings.joystick_getDevices(false)
7
+ Globals.joystickDevices = Bindings.joystick_getDevices()
8
8
  for (const joystickDevice of Globals.joystickDevices) {
9
9
  makeJoystickDevice(joystickDevice)
10
10
  }
@@ -0,0 +1,14 @@
1
+ const Bindings = require('../bindings')
2
+ const Enums = require('../enums')
3
+
4
+ const power = {
5
+ get info () {
6
+ const info = Bindings.power_getInfo()
7
+ if (info.seconds === -1) { info.seconds = null }
8
+ if (info.percent === -1) { info.percent = null }
9
+ info.state = Enums.powerStateNames[info.state]
10
+ return info
11
+ },
12
+ }
13
+
14
+ module.exports = { power }
@@ -0,0 +1,27 @@
1
+ const Bindings = require('../bindings')
2
+ const { SensorInstance } = require('./sensor-instance')
3
+
4
+
5
+ const sensor = {
6
+ STANDARD_GRAVITY: 9.80665,
7
+
8
+ get devices () {
9
+ const devices = Bindings.sensor_getDevices()
10
+ for (const sensorDevice of devices) {
11
+ const { type } = sensorDevice
12
+ sensorDevice.type
13
+ = type.startsWith('accelerometer') ? 'accelerometer'
14
+ : type.startsWith('gyroscope') ? 'gyroscope'
15
+ : 'unknown'
16
+ sensorDevice.side
17
+ = type.endsWith('Left') ? 'left'
18
+ : type.endsWith('Right') ? 'right'
19
+ : null
20
+ }
21
+ return devices
22
+ },
23
+
24
+ openDevice (device) { return new SensorInstance(device) },
25
+ }
26
+
27
+ module.exports = { sensor }
@@ -0,0 +1,55 @@
1
+ const Bindings = require('../bindings')
2
+ const Globals = require('../globals')
3
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
+
5
+ const validEvents = [
6
+ 'update',
7
+ 'close',
8
+ ]
9
+
10
+ class SensorInstance extends EventsViaPoll {
11
+ constructor (device) {
12
+ super(validEvents)
13
+
14
+ Bindings.sensor_open(device._index)
15
+
16
+ this._device = device
17
+
18
+ this._closed = false
19
+
20
+ Globals.sensorInstances.all.add(this)
21
+ let collection = Globals.sensorInstances.byId.get(this._device.id)
22
+ if (!collection) {
23
+ collection = new Set()
24
+ Globals.sensorInstances.byId.set(this._device.id, collection)
25
+ }
26
+ collection.add(this)
27
+ }
28
+
29
+ get device () { return this._device }
30
+
31
+ get data () {
32
+ const data = Bindings.sensor_getData(this._device.id)
33
+ if (data.timestamp === 0) { data.timestamp = null }
34
+ return data
35
+ }
36
+
37
+ get closed () { return this._closed }
38
+ close () {
39
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
40
+
41
+ this.emit('close')
42
+ this.removeAllListeners()
43
+ this._closed = true
44
+
45
+ Globals.sensorInstances.all.delete(this)
46
+ const collection = Globals.sensorInstances.byId.get(this._device.id)
47
+ collection.delete(this)
48
+ if (collection.size === 0) {
49
+ Bindings.sensor_close(this._device.id)
50
+ Globals.sensorInstances.byId.delete(this._device.id)
51
+ }
52
+ }
53
+ }
54
+
55
+ module.exports = { SensorInstance }
@@ -1,22 +1,27 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
3
  const Enums = require('../enums')
4
+ const { EventsViaPoll } = require('../events/events-via-poll')
4
5
  const { Window } = require('./window')
5
6
 
6
- const video = {
7
- get displays () {
8
- const displays = Bindings.video_getDisplays()
9
- for (const display of displays) {
10
- display.format = Enums.pixelFormatNames[display.format]
11
- }
12
- return displays
13
- },
7
+ Globals.displays = Bindings.video_getDisplays()
8
+ for (const display of Globals.displays) {
9
+ display.format = Enums.pixelFormatNames[display.format]
10
+ }
14
11
 
15
- get windows () { return [ ...Globals.windows.all.values() ] },
16
- get focused () { return Globals.windows.focused },
17
- get hovered () { return Globals.windows.hovered },
18
12
 
19
- createWindow: (options) => new Window(options),
20
- }
13
+ const validEvents = [ 'displayAdd', 'displayRemove', 'displayOrient' ]
14
+
15
+ const video = new class extends EventsViaPoll {
16
+ constructor () { super(validEvents) }
17
+
18
+ get displays () { return Globals.displays }
19
+
20
+ get windows () { return [ ...Globals.windows.all.values() ] }
21
+ get focused () { return Globals.windows.focused }
22
+ get hovered () { return Globals.windows.hovered }
23
+
24
+ createWindow (options) { return new Window(options) }
25
+ }()
21
26
 
22
27
  module.exports = { video }
@@ -221,6 +221,24 @@ getEnums(napi_env env, napi_callback_info info)
221
221
  CALL_NAPI(napi_set_property, result, key, value);
222
222
  }
223
223
 
224
+ {
225
+ SdlHelpers::Variant sensor_types;
226
+ CALL_SDL_HELPER(enum_getSensorTypes, sensor_types);
227
+
228
+ CALL_NAPI(fromVariant, sensor_types, &value);
229
+ CALL_NAPI(napi_create_string_latin1, "sensorType", 10, &key);
230
+ CALL_NAPI(napi_set_property, result, key, value);
231
+ }
232
+
233
+ {
234
+ SdlHelpers::Variant power_states;
235
+ CALL_SDL_HELPER(enum_getPowerStates, power_states);
236
+
237
+ CALL_NAPI(fromVariant, power_states, &value);
238
+ CALL_NAPI(napi_create_string_latin1, "powerState", 10, &key);
239
+ CALL_NAPI(napi_set_property, result, key, value);
240
+ }
241
+
224
242
  cleanup:
225
243
  return result;
226
244
  }
@@ -1251,6 +1269,79 @@ controller_addMappings(napi_env env, napi_callback_info info)
1251
1269
  }
1252
1270
 
1253
1271
 
1272
+ napi_value
1273
+ sensor_getDevices(napi_env env, napi_callback_info info)
1274
+ {
1275
+ napi_value result = nullptr;
1276
+
1277
+ SdlHelpers::Variant devices;
1278
+ CALL_SDL_HELPER(sensor_getDevices, devices);
1279
+
1280
+ CALL_NAPI(fromVariant, devices, &result);
1281
+
1282
+ cleanup:
1283
+ return result;
1284
+ }
1285
+
1286
+ napi_value
1287
+ sensor_open(napi_env env, napi_callback_info info)
1288
+ {
1289
+ SdlHelpers::Variant object;
1290
+
1291
+ napi_value argv[1];
1292
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1293
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1294
+
1295
+ int index;
1296
+ CALL_NAPI(napi_get_value_int32, argv[0], &index);
1297
+
1298
+ CALL_SDL_HELPER(sensor_open, index);
1299
+
1300
+ cleanup:
1301
+ return nullptr;
1302
+ }
1303
+
1304
+ napi_value
1305
+ sensor_getData(napi_env env, napi_callback_info info)
1306
+ {
1307
+ napi_value result = nullptr;
1308
+
1309
+ SdlHelpers::Variant object;
1310
+
1311
+ napi_value argv[1];
1312
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1313
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1314
+
1315
+ int sensor_id;
1316
+ CALL_NAPI(napi_get_value_int32, argv[0], &sensor_id);
1317
+
1318
+ CALL_SDL_HELPER(sensor_getData, sensor_id, object);
1319
+
1320
+ CALL_NAPI(fromVariant, object, &result);
1321
+
1322
+ cleanup:
1323
+ return result;
1324
+ }
1325
+
1326
+ napi_value
1327
+ sensor_close(napi_env env, napi_callback_info info)
1328
+ {
1329
+ SdlHelpers::Variant object;
1330
+
1331
+ napi_value argv[1];
1332
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1333
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1334
+
1335
+ int sensor_id;
1336
+ CALL_NAPI(napi_get_value_int32, argv[0], &sensor_id);
1337
+
1338
+ CALL_SDL_HELPER(sensor_close, sensor_id);
1339
+
1340
+ cleanup:
1341
+ return nullptr;
1342
+ }
1343
+
1344
+
1254
1345
  napi_value
1255
1346
  audio_getDevices(napi_env env, napi_callback_info info)
1256
1347
  {
@@ -1469,6 +1560,21 @@ clipboard_setText(napi_env env, napi_callback_info info)
1469
1560
  }
1470
1561
 
1471
1562
 
1563
+ napi_value
1564
+ power_getInfo(napi_env env, napi_callback_info info)
1565
+ {
1566
+ napi_value result = nullptr;
1567
+
1568
+ SdlHelpers::Variant power;
1569
+ CALL_SDL_HELPER(power_getInfo, power);
1570
+
1571
+ CALL_NAPI(fromVariant, power, &result);
1572
+
1573
+ cleanup:
1574
+ return result;
1575
+ }
1576
+
1577
+
1472
1578
  napi_value
1473
1579
  cleanup(napi_env env, napi_callback_info info)
1474
1580
  {
@@ -1526,6 +1632,10 @@ init (napi_env env, napi_value exports)
1526
1632
  { "controller_open", NULL, controller_open, NULL, NULL, NULL, napi_enumerable, NULL },
1527
1633
  { "controller_close", NULL, controller_close, NULL, NULL, NULL, napi_enumerable, NULL },
1528
1634
  { "controller_addMappings", NULL, controller_addMappings, NULL, NULL, NULL, napi_enumerable, NULL },
1635
+ { "sensor_getDevices", NULL, sensor_getDevices, NULL, NULL, NULL, napi_enumerable, NULL },
1636
+ { "sensor_open", NULL, sensor_open, NULL, NULL, NULL, napi_enumerable, NULL },
1637
+ { "sensor_getData", NULL, sensor_getData, NULL, NULL, NULL, napi_enumerable, NULL },
1638
+ { "sensor_close", NULL, sensor_close, NULL, NULL, NULL, napi_enumerable, NULL },
1529
1639
  { "audio_getDevices", NULL, audio_getDevices, NULL, NULL, NULL, napi_enumerable, NULL },
1530
1640
  { "audio_openDevice", NULL, audio_openDevice, NULL, NULL, NULL, napi_enumerable, NULL },
1531
1641
  { "audio_close", NULL, audio_close, NULL, NULL, NULL, napi_enumerable, NULL },
@@ -1536,10 +1646,11 @@ init (napi_env env, napi_value exports)
1536
1646
  { "audio_clearQueue", NULL, audio_clearQueue, NULL, NULL, NULL, napi_enumerable, NULL },
1537
1647
  { "clipboard_getText", NULL, clipboard_getText, NULL, NULL, NULL, napi_enumerable, NULL },
1538
1648
  { "clipboard_setText", NULL, clipboard_setText, NULL, NULL, NULL, napi_enumerable, NULL },
1649
+ { "power_getInfo", NULL, power_getInfo, NULL, NULL, NULL, napi_enumerable, NULL },
1539
1650
  { "cleanup", NULL, cleanup, NULL, NULL, NULL, napi_enumerable, NULL }
1540
1651
  };
1541
1652
 
1542
- CALL_NAPI(napi_define_properties, exports, 54, desc);
1653
+ CALL_NAPI(napi_define_properties, exports, 59, desc);
1543
1654
 
1544
1655
  cleanup:
1545
1656
  return exports;
@@ -120,6 +120,7 @@ ErrorMessage::
120
120
 
121
121
 
122
122
  enum class EventFamily {
123
+ DISPLAY,
123
124
  WINDOW,
124
125
  KEYBOARD,
125
126
  MOUSE,
@@ -128,6 +129,7 @@ enum class EventFamily {
128
129
  JOYSTICK_DEVICE,
129
130
  JOYSTICK,
130
131
  CONTROLLER,
132
+ SENSOR,
131
133
  AUDIO_DEVICE,
132
134
  CLIPBOARD,
133
135
  };
@@ -137,6 +139,7 @@ enum_getEventFamilies (Variant & families)
137
139
  {
138
140
  MAKE_MAP(families);
139
141
 
142
+ SET_NUM(families, "display", (int) EventFamily::DISPLAY);
140
143
  SET_NUM(families, "window", (int) EventFamily::WINDOW);
141
144
  SET_NUM(families, "keyboard", (int) EventFamily::KEYBOARD);
142
145
  SET_NUM(families, "mouse", (int) EventFamily::MOUSE);
@@ -154,6 +157,9 @@ enum_getEventFamilies (Variant & families)
154
157
 
155
158
  enum class EventType {
156
159
  QUIT,
160
+ DISPLAY_ADD,
161
+ DISPLAY_REMOVE,
162
+ DISPLAY_ORIENT,
157
163
  SHOW,
158
164
  HIDE,
159
165
  EXPOSE,
@@ -195,6 +201,9 @@ enum_getEventTypes (Variant & types)
195
201
  MAKE_MAP(types);
196
202
 
197
203
  SET_NUM(types, "quit", (int) EventType::QUIT);
204
+ SET_NUM(types, "displayAdd", (int) EventType::DISPLAY_ADD);
205
+ SET_NUM(types, "displayRemove", (int) EventType::DISPLAY_REMOVE);
206
+ SET_NUM(types, "displayOrient", (int) EventType::DISPLAY_ORIENT);
198
207
  SET_NUM(types, "show", (int) EventType::SHOW);
199
208
  SET_NUM(types, "hide", (int) EventType::HIDE);
200
209
  SET_NUM(types, "expose", (int) EventType::EXPOSE);
@@ -686,6 +695,37 @@ enum_getControllerButtons (Variant & buttons)
686
695
  return nullptr;
687
696
  }
688
697
 
698
+ ErrorMessage *
699
+ enum_getSensorTypes (Variant & types)
700
+ {
701
+ MAKE_MAP(types);
702
+
703
+ SET_NUM(types, "unknown", SDL_SENSOR_UNKNOWN);
704
+ SET_NUM(types, "accelerometer", SDL_SENSOR_ACCEL);
705
+ SET_NUM(types, "gyroscope", SDL_SENSOR_GYRO);
706
+ SET_NUM(types, "accelerometerLeft", SDL_SENSOR_ACCEL_L);
707
+ SET_NUM(types, "gyroscopeLeft", SDL_SENSOR_GYRO_L);
708
+ SET_NUM(types, "accelerometerRight", SDL_SENSOR_ACCEL_R);
709
+ SET_NUM(types, "gyroscopeRight", SDL_SENSOR_GYRO_R);
710
+
711
+ return nullptr;
712
+ }
713
+
714
+ ErrorMessage *
715
+ enum_getPowerStates (Variant & states)
716
+ {
717
+ MAKE_MAP(states);
718
+
719
+ SET_NUM(states, "unknown", SDL_POWERSTATE_UNKNOWN);
720
+ SET_NUM(states, "noBattery", SDL_POWERSTATE_NO_BATTERY);
721
+ SET_NUM(states, "battery", SDL_POWERSTATE_ON_BATTERY);
722
+ SET_NUM(states, "charging", SDL_POWERSTATE_CHARGING);
723
+ SET_NUM(states, "charged", SDL_POWERSTATE_CHARGED);
724
+
725
+ return nullptr;
726
+ }
727
+
728
+
689
729
 
690
730
  ErrorMessage *
691
731
  initialize (Variant & object)
@@ -703,7 +743,8 @@ initialize (Variant & object)
703
743
  | SDL_INIT_EVENTS
704
744
  | SDL_INIT_JOYSTICK
705
745
  | SDL_INIT_GAMECONTROLLER
706
- | SDL_INIT_HAPTIC;
746
+ | SDL_INIT_HAPTIC
747
+ | SDL_INIT_SENSOR;
707
748
  if (SDL_Init(subsystems) != 0) {
708
749
  RETURN_ERROR("SDL_Init() error: %s\n", SDL_GetError());
709
750
  }
@@ -990,6 +1031,18 @@ packageEvent (const SDL_Event & event, Variant & object)
990
1031
 
991
1032
  Variant value;
992
1033
  switch (event.type) {
1034
+ case SDL_DISPLAYEVENT: {
1035
+ SET_NUM(object, "family", (int) EventFamily::DISPLAY);
1036
+ SET_NUM(object, "displayIndex", event.display.display);
1037
+
1038
+ switch (event.display.event) {
1039
+ case SDL_DISPLAYEVENT_CONNECTED: { SET_NUM(object, "type", (int) EventType::DISPLAY_ADD); break; }
1040
+ case SDL_DISPLAYEVENT_DISCONNECTED: { SET_NUM(object, "type", (int) EventType::DISPLAY_REMOVE); break; }
1041
+ case SDL_DISPLAYEVENT_ORIENTATION: { SET_NUM(object, "type", (int) EventType::DISPLAY_ORIENT); break; }
1042
+ }
1043
+ break;
1044
+ }
1045
+
993
1046
  case SDL_WINDOWEVENT: {
994
1047
  SET_NUM(object, "family", (int) EventFamily::WINDOW);
995
1048
  SET_NUM(object, "windowId", event.window.windowID);
@@ -1228,6 +1281,13 @@ packageEvent (const SDL_Event & event, Variant & object)
1228
1281
  break;
1229
1282
  }
1230
1283
 
1284
+ case SDL_SENSORUPDATE: {
1285
+ SET_NUM(object, "family", (int) EventFamily::SENSOR);
1286
+ SET_NUM(object, "type", (int) EventType::UPDATE);
1287
+ SET_NUM(object, "sensorId", event.sensor.which);
1288
+ break;
1289
+ }
1290
+
1231
1291
  case SDL_AUDIODEVICEADDED: {
1232
1292
  SET_NUM(object, "family", (int) EventFamily::AUDIO_DEVICE);
1233
1293
  SET_NUM(object, "type", (int) EventType::DEVICE_ADD);
@@ -2274,6 +2334,89 @@ controller_addMappings (char ** mappings, int length)
2274
2334
  }
2275
2335
 
2276
2336
 
2337
+ ErrorMessage *
2338
+ sensor_getDevices (Variant & list)
2339
+ {
2340
+ int num_devices = SDL_NumSensors();
2341
+ if (num_devices < 0) {
2342
+ RETURN_ERROR("SDL_NumSensors() error: %s\n", SDL_GetError());
2343
+ }
2344
+
2345
+ MAKE_LIST(list);
2346
+
2347
+ for (int i = 0; i < num_devices; i++) {
2348
+ Variant device;
2349
+ MAKE_MAP(device);
2350
+
2351
+ SET_NUM(device, "_index", i);
2352
+
2353
+ int id = SDL_SensorGetDeviceInstanceID(i);
2354
+ SET_NUM(device, "id", id);
2355
+
2356
+ int type = SDL_SensorGetDeviceType(i);
2357
+ SET_NUM(device, "type", type);
2358
+
2359
+ const char * name = SDL_SensorGetDeviceName(i);
2360
+ if (name == nullptr) {
2361
+ RETURN_ERROR("SDL_SensorGetDeviceName(%d) error: %s\n", i, SDL_GetError());
2362
+ }
2363
+ SET_STRING(device, "name", name);
2364
+
2365
+ APPEND(list, device);
2366
+ }
2367
+
2368
+ return nullptr;
2369
+ }
2370
+
2371
+ ErrorMessage *
2372
+ sensor_open (int index)
2373
+ {
2374
+ SDL_Sensor * sensor = SDL_SensorOpen(index);
2375
+ if (sensor == nullptr) {
2376
+ RETURN_ERROR("SDL_SensorOpen(%d) error: %s\n", index, SDL_GetError());
2377
+ }
2378
+
2379
+ return nullptr;
2380
+ }
2381
+
2382
+ ErrorMessage *
2383
+ sensor_close (int sensor_id)
2384
+ {
2385
+ SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2386
+ if (sensor == nullptr) {
2387
+ RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2388
+ }
2389
+
2390
+ SDL_SensorClose(sensor);
2391
+
2392
+ return nullptr;
2393
+ }
2394
+
2395
+ ErrorMessage *
2396
+ sensor_getData (int sensor_id, Variant & object)
2397
+ {
2398
+ SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2399
+ if (sensor == nullptr) {
2400
+ RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2401
+ }
2402
+
2403
+ Uint64 timestamp;
2404
+ float data[3];
2405
+ if (SDL_SensorGetDataWithTimestamp(sensor, &timestamp, data, 3) == -1) {
2406
+ RETURN_ERROR("SDL_SensorGetDataWithTimestamp(%d) error: %s\n", sensor_id, SDL_GetError());
2407
+ }
2408
+
2409
+ MAKE_MAP(object);
2410
+ SET_NUM(object, "timestamp", timestamp);
2411
+ SET_NUM(object, "x", data[0]);
2412
+ SET_NUM(object, "y", data[1]);
2413
+ SET_NUM(object, "z", data[2]);
2414
+
2415
+ return nullptr;
2416
+ }
2417
+
2418
+
2419
+
2277
2420
  ErrorMessage *
2278
2421
  audio_getDevices(bool capture, Variant & list)
2279
2422
  {
@@ -2414,6 +2557,22 @@ clipboard_setText (const char * text)
2414
2557
  }
2415
2558
 
2416
2559
 
2560
+ ErrorMessage *
2561
+ power_getInfo (Variant & info)
2562
+ {
2563
+ int seconds, percent;
2564
+ SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
2565
+
2566
+ MAKE_MAP(info);
2567
+
2568
+ SET_NUM(info, "state", (int) state);
2569
+ SET_NUM(info, "seconds", seconds);
2570
+ SET_NUM(info, "percent", percent);
2571
+
2572
+ return nullptr;
2573
+ }
2574
+
2575
+
2417
2576
  ErrorMessage *
2418
2577
  cleanup ()
2419
2578
  {
@@ -44,6 +44,8 @@ ErrorMessage * enum_getPowerLevels (Variant & object);
44
44
  ErrorMessage * enum_getJoystickTypes (Variant & object);
45
45
  ErrorMessage * enum_getControllerAxes (Variant & object);
46
46
  ErrorMessage * enum_getControllerButtons (Variant & object);
47
+ ErrorMessage * enum_getSensorTypes (Variant & object);
48
+ ErrorMessage * enum_getPowerStates (Variant & object);
47
49
 
48
50
  ErrorMessage * initialize (Variant & object);
49
51
  ErrorMessage * cleanup ();
@@ -150,6 +152,11 @@ ErrorMessage * controller_open (int index, Variant & object);
150
152
  ErrorMessage * controller_close (int controller_id);
151
153
  ErrorMessage * controller_addMappings (char ** mappings, int length);
152
154
 
155
+ ErrorMessage * sensor_getDevices (Variant & list);
156
+ ErrorMessage * sensor_open (int index);
157
+ ErrorMessage * sensor_close (int sensor_id);
158
+ ErrorMessage * sensor_getData (int sensor_id, Variant & object);
159
+
153
160
  ErrorMessage * audio_getDevices (bool capture, Variant & list);
154
161
  ErrorMessage * audio_openDevice (
155
162
  const char * name, bool capture,
@@ -166,6 +173,8 @@ ErrorMessage * audio_dequeue (int audio_id, void * dst, int size, int * num);
166
173
  ErrorMessage * clipboard_getText (const char ** text);
167
174
  ErrorMessage * clipboard_setText (const char * text);
168
175
 
176
+ ErrorMessage * power_getInfo (Variant & info);
177
+
169
178
  } // namespace
170
179
 
171
180
  #endif // _SDL_H_
@@ -5,6 +5,20 @@ export namespace Events {
5
5
 
6
6
  type PreventCallback = () => void
7
7
 
8
+ namespace Display {
9
+
10
+ interface DisplayEvent extends BaseEvent {
11
+ readonly display: Sdl.Video.Display
12
+ }
13
+
14
+ interface Add extends DisplayEvent { readonly type: 'deviceAdd' }
15
+ interface Remove extends DisplayEvent { readonly type: 'deviceRemove' }
16
+ interface Orient extends DisplayEvent { readonly type: 'deviceOrient' }
17
+
18
+ type Any = Add | Remove | Orient
19
+
20
+ }
21
+
8
22
  namespace Window {
9
23
 
10
24
  interface WindowEvent extends BaseEvent {}
@@ -119,6 +133,7 @@ export namespace Events {
119
133
  | DropText
120
134
  | DropFile
121
135
  | DropComplete
136
+
122
137
  }
123
138
 
124
139
  namespace Joystick {
@@ -204,6 +219,22 @@ export namespace Events {
204
219
 
205
220
  }
206
221
 
222
+ namespace Sensor {
223
+
224
+ interface SensorEvent extends BaseEvent {}
225
+
226
+ interface Update extends SensorEvent {
227
+ readonly type: 'update'
228
+ }
229
+
230
+ interface Close extends SensorEvent { readonly type: 'close' }
231
+
232
+ type Any
233
+ = Update
234
+ | Close
235
+
236
+ }
237
+
207
238
  namespace ControllerDevice {
208
239
 
209
240
  interface DeviceEvent extends BaseEvent {
@@ -435,6 +466,11 @@ export namespace Sdl {
435
466
  }
436
467
 
437
468
  interface Module {
469
+ on (event: 'displayAdd', listener: (event: Events.Display.Add) => void): this
470
+ on (event: 'displayRemove', listener: (event: Events.Display.Remove) => void): this
471
+ on (event: 'displayOrient', listener: (event: Events.Display.Orient) => void): this
472
+ on (event: '*', listener: (event: Events.Display.Any) => void): this
473
+
438
474
  readonly displays: Display[]
439
475
 
440
476
  readonly windows: Window[]
@@ -949,6 +985,54 @@ export namespace Sdl {
949
985
 
950
986
  }
951
987
 
988
+ namespace Sensor {
989
+
990
+ type Type
991
+ = 'unknown'
992
+ | 'accelerometer'
993
+ | 'gyroscope'
994
+
995
+ type Side
996
+ = 'left'
997
+ | 'right'
998
+
999
+ interface Device {
1000
+ readonly id: number
1001
+ readonly name: string
1002
+ readonly type: Type
1003
+ readonly side: Side | null
1004
+ }
1005
+
1006
+ interface Data {
1007
+ timestamp: number | null,
1008
+ x: number,
1009
+ y: number,
1010
+ z: number,
1011
+ }
1012
+
1013
+ class SensorInstance {
1014
+ on (event: 'update', listener: (event: Events.Sensor.Update) => void): this
1015
+ on (event: 'close', listener: (event: Events.Sensor.Close) => void): this
1016
+ on (event: '*', listener: (event: Events.Sensor.Any) => void): this
1017
+
1018
+ readonly device: Device
1019
+
1020
+ readonly data: Data
1021
+
1022
+ readonly closed: boolean
1023
+ close (): void
1024
+ }
1025
+
1026
+ interface Module {
1027
+ STANDARD_GRAVITY: 9.80665,
1028
+
1029
+ readonly devices: Device[]
1030
+
1031
+ openDevice (device: Device): SensorInstance
1032
+ }
1033
+
1034
+ }
1035
+
952
1036
  namespace Audio {
953
1037
 
954
1038
  type Format
@@ -1060,6 +1144,27 @@ export namespace Sdl {
1060
1144
 
1061
1145
  }
1062
1146
 
1147
+ namespace Power {
1148
+
1149
+ type PowerState
1150
+ = 'unknown'
1151
+ | 'noBattery'
1152
+ | 'battery'
1153
+ | 'charging'
1154
+ | 'charged'
1155
+
1156
+ interface PowerInfo {
1157
+ state: PowerState
1158
+ seconds: number | null
1159
+ percent: number | null
1160
+ }
1161
+
1162
+ interface Module {
1163
+ readonly info: PowerInfo
1164
+ }
1165
+
1166
+ }
1167
+
1063
1168
  interface Module {
1064
1169
  readonly info: Info
1065
1170
  readonly video: Video.Module
@@ -1067,9 +1172,12 @@ export namespace Sdl {
1067
1172
  readonly mouse: Mouse.Module
1068
1173
  readonly joystick: Joystick.Module
1069
1174
  readonly controller: Controller.Module
1175
+ readonly sensor: Sensor.Module
1070
1176
  readonly audio: Audio.Module
1071
1177
  readonly clipboard: Clipboard.Module
1178
+ readonly power: Power.Module
1072
1179
  }
1180
+
1073
1181
  }
1074
1182
 
1075
1183
  declare const sdl: Sdl.Module