@kmamal/sdl 0.8.3 → 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 +102 -3
- package/package.json +1 -1
- package/src/javascript/events/index.js +18 -0
- package/src/javascript/globals.js +9 -5
- package/src/javascript/index.js +2 -0
- package/src/javascript/sensor/index.js +27 -0
- package/src/javascript/sensor/sensor-instance.js +55 -0
- package/src/native/binding.cpp +87 -1
- package/src/native/sdl-helpers.cpp +109 -1
- package/src/native/sdl-helpers.h +6 -0
- package/src/types/index.d.ts +65 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
7
|
SDL bindings for Node.js.
|
|
8
|
-
Provides window management, input events (keyboard, mouse,
|
|
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.
|
|
@@ -246,6 +246,16 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
|
|
|
246
246
|
* [controllerInstance.stopRumbleTriggers()](#controllerinstancestoprumbletriggers)
|
|
247
247
|
* [controllerInstance.closed](#controllerinstanceclosed)
|
|
248
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)
|
|
249
259
|
* [sdl.audio](#sdlaudio)
|
|
250
260
|
* [Audio data](#audio-data)
|
|
251
261
|
* [Sample formats](#sample-formats)
|
|
@@ -1936,6 +1946,95 @@ A closed instance object should not be used any further.
|
|
|
1936
1946
|
|
|
1937
1947
|
Closes the instance.
|
|
1938
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
|
+
|
|
1939
2038
|
## sdl.audio
|
|
1940
2039
|
|
|
1941
2040
|
### Audio data
|
|
@@ -2357,11 +2456,11 @@ Changes the text contents of the clipboard.
|
|
|
2357
2456
|
### sdl.power.info
|
|
2358
2457
|
|
|
2359
2458
|
* `<object>`
|
|
2360
|
-
* `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'
|
|
2459
|
+
* `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'charged'`.
|
|
2361
2460
|
* `seconds: <number | null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
|
|
2362
2461
|
* `percent: <number | null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
|
|
2363
2462
|
|
|
2364
|
-
The curent power
|
|
2463
|
+
The curent power information of the device.
|
|
2365
2464
|
|
|
2366
2465
|
|
|
2367
2466
|
## Building from source
|
package/package.json
CHANGED
|
@@ -350,6 +350,24 @@ const poll = () => {
|
|
|
350
350
|
}
|
|
351
351
|
} break
|
|
352
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
|
+
|
|
353
371
|
case Enums.eventFamily.audioDevice: {
|
|
354
372
|
const { isRecorder } = event
|
|
355
373
|
delete event.isRecorder
|
|
@@ -6,11 +6,6 @@ module.exports = {
|
|
|
6
6
|
focused: null,
|
|
7
7
|
hovered: null,
|
|
8
8
|
},
|
|
9
|
-
audioDevices: {
|
|
10
|
-
playback: [],
|
|
11
|
-
recording: [],
|
|
12
|
-
},
|
|
13
|
-
audioInstances: new Map(),
|
|
14
9
|
joystickDevices: [],
|
|
15
10
|
joystickInstances: {
|
|
16
11
|
all: new Set(),
|
|
@@ -21,6 +16,15 @@ module.exports = {
|
|
|
21
16
|
all: new Set(),
|
|
22
17
|
byId: new Map(),
|
|
23
18
|
},
|
|
19
|
+
sensorInstances: {
|
|
20
|
+
all: new Set(),
|
|
21
|
+
byId: new Map(),
|
|
22
|
+
},
|
|
23
|
+
audioDevices: {
|
|
24
|
+
playback: [],
|
|
25
|
+
recording: [],
|
|
26
|
+
},
|
|
27
|
+
audioInstances: new Map(),
|
|
24
28
|
|
|
25
29
|
events: null, // Set later to { startPolling, stopPolling, poll }
|
|
26
30
|
}
|
package/src/javascript/index.js
CHANGED
|
@@ -10,6 +10,7 @@ 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')
|
|
15
16
|
const { power } = require('./power')
|
|
@@ -24,6 +25,7 @@ module.exports = {
|
|
|
24
25
|
mouse,
|
|
25
26
|
joystick,
|
|
26
27
|
controller,
|
|
28
|
+
sensor,
|
|
27
29
|
audio,
|
|
28
30
|
clipboard,
|
|
29
31
|
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 }
|
package/src/native/binding.cpp
CHANGED
|
@@ -221,6 +221,15 @@ 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
|
+
|
|
224
233
|
{
|
|
225
234
|
SdlHelpers::Variant power_states;
|
|
226
235
|
CALL_SDL_HELPER(enum_getPowerStates, power_states);
|
|
@@ -1260,6 +1269,79 @@ controller_addMappings(napi_env env, napi_callback_info info)
|
|
|
1260
1269
|
}
|
|
1261
1270
|
|
|
1262
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
|
+
|
|
1263
1345
|
napi_value
|
|
1264
1346
|
audio_getDevices(napi_env env, napi_callback_info info)
|
|
1265
1347
|
{
|
|
@@ -1550,6 +1632,10 @@ init (napi_env env, napi_value exports)
|
|
|
1550
1632
|
{ "controller_open", NULL, controller_open, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
1551
1633
|
{ "controller_close", NULL, controller_close, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
1552
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 },
|
|
1553
1639
|
{ "audio_getDevices", NULL, audio_getDevices, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
1554
1640
|
{ "audio_openDevice", NULL, audio_openDevice, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
1555
1641
|
{ "audio_close", NULL, audio_close, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
@@ -1564,7 +1650,7 @@ init (napi_env env, napi_value exports)
|
|
|
1564
1650
|
{ "cleanup", NULL, cleanup, NULL, NULL, NULL, napi_enumerable, NULL }
|
|
1565
1651
|
};
|
|
1566
1652
|
|
|
1567
|
-
CALL_NAPI(napi_define_properties, exports,
|
|
1653
|
+
CALL_NAPI(napi_define_properties, exports, 59, desc);
|
|
1568
1654
|
|
|
1569
1655
|
cleanup:
|
|
1570
1656
|
return exports;
|
|
@@ -129,6 +129,7 @@ enum class EventFamily {
|
|
|
129
129
|
JOYSTICK_DEVICE,
|
|
130
130
|
JOYSTICK,
|
|
131
131
|
CONTROLLER,
|
|
132
|
+
SENSOR,
|
|
132
133
|
AUDIO_DEVICE,
|
|
133
134
|
CLIPBOARD,
|
|
134
135
|
};
|
|
@@ -694,6 +695,22 @@ enum_getControllerButtons (Variant & buttons)
|
|
|
694
695
|
return nullptr;
|
|
695
696
|
}
|
|
696
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
|
+
|
|
697
714
|
ErrorMessage *
|
|
698
715
|
enum_getPowerStates (Variant & states)
|
|
699
716
|
{
|
|
@@ -726,7 +743,8 @@ initialize (Variant & object)
|
|
|
726
743
|
| SDL_INIT_EVENTS
|
|
727
744
|
| SDL_INIT_JOYSTICK
|
|
728
745
|
| SDL_INIT_GAMECONTROLLER
|
|
729
|
-
| SDL_INIT_HAPTIC
|
|
746
|
+
| SDL_INIT_HAPTIC
|
|
747
|
+
| SDL_INIT_SENSOR;
|
|
730
748
|
if (SDL_Init(subsystems) != 0) {
|
|
731
749
|
RETURN_ERROR("SDL_Init() error: %s\n", SDL_GetError());
|
|
732
750
|
}
|
|
@@ -1263,6 +1281,13 @@ packageEvent (const SDL_Event & event, Variant & object)
|
|
|
1263
1281
|
break;
|
|
1264
1282
|
}
|
|
1265
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
|
+
|
|
1266
1291
|
case SDL_AUDIODEVICEADDED: {
|
|
1267
1292
|
SET_NUM(object, "family", (int) EventFamily::AUDIO_DEVICE);
|
|
1268
1293
|
SET_NUM(object, "type", (int) EventType::DEVICE_ADD);
|
|
@@ -2309,6 +2334,89 @@ controller_addMappings (char ** mappings, int length)
|
|
|
2309
2334
|
}
|
|
2310
2335
|
|
|
2311
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, ×tamp, 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
|
+
|
|
2312
2420
|
ErrorMessage *
|
|
2313
2421
|
audio_getDevices(bool capture, Variant & list)
|
|
2314
2422
|
{
|
package/src/native/sdl-helpers.h
CHANGED
|
@@ -44,6 +44,7 @@ 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);
|
|
47
48
|
ErrorMessage * enum_getPowerStates (Variant & object);
|
|
48
49
|
|
|
49
50
|
ErrorMessage * initialize (Variant & object);
|
|
@@ -151,6 +152,11 @@ ErrorMessage * controller_open (int index, Variant & object);
|
|
|
151
152
|
ErrorMessage * controller_close (int controller_id);
|
|
152
153
|
ErrorMessage * controller_addMappings (char ** mappings, int length);
|
|
153
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
|
+
|
|
154
160
|
ErrorMessage * audio_getDevices (bool capture, Variant & list);
|
|
155
161
|
ErrorMessage * audio_openDevice (
|
|
156
162
|
const char * name, bool capture,
|
package/src/types/index.d.ts
CHANGED
|
@@ -219,6 +219,22 @@ export namespace Events {
|
|
|
219
219
|
|
|
220
220
|
}
|
|
221
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
|
+
|
|
222
238
|
namespace ControllerDevice {
|
|
223
239
|
|
|
224
240
|
interface DeviceEvent extends BaseEvent {
|
|
@@ -969,6 +985,54 @@ export namespace Sdl {
|
|
|
969
985
|
|
|
970
986
|
}
|
|
971
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
|
+
|
|
972
1036
|
namespace Audio {
|
|
973
1037
|
|
|
974
1038
|
type Format
|
|
@@ -1108,6 +1172,7 @@ export namespace Sdl {
|
|
|
1108
1172
|
readonly mouse: Mouse.Module
|
|
1109
1173
|
readonly joystick: Joystick.Module
|
|
1110
1174
|
readonly controller: Controller.Module
|
|
1175
|
+
readonly sensor: Sensor.Module
|
|
1111
1176
|
readonly audio: Audio.Module
|
|
1112
1177
|
readonly clipboard: Clipboard.Module
|
|
1113
1178
|
readonly power: Power.Module
|