@kmamal/sdl 0.8.3 → 0.9.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.
package/README.md CHANGED
@@ -5,18 +5,18 @@
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, clipboard manipulation, and battery status.
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.
12
12
 
13
13
  #### Canvas, WebGL, and WebGPU
14
14
 
15
- One goal of this project is to allow using Canvas (the 2D rendering context API), WebGL, and WebGPU without a browser.
16
- You can already use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to draw on windows using WebGL.
17
- WebGPU is also working, but it's not been fully integrated: you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme) for compute and render, but it can't yet render directly to windows; that will come in a future version.
18
- Right now you have to go through an intermediate buffer.
19
- Same with the Canvas API, where you can use the [canvas](https://www.npmjs.com/package/canvas) package to draw to a buffer and then draw that to a window.
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.
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
+ 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.
20
20
 
21
21
 
22
22
  ## Installation
@@ -65,7 +65,7 @@ window.render(width, height, width * 4, 'bgra32', buffer)
65
65
  import sdl from '@kmamal/sdl'
66
66
  import createContext from '@kmamal/gl'
67
67
 
68
- const window = sdl.video.createWindow({title: "WebGL", opengl: true })
68
+ const window = sdl.video.createWindow({ title: "WebGL", opengl: true })
69
69
  const { pixelWidth: width, pixelHeight: height, native } = window
70
70
  const gl = createContext(width, height, { window: native })
71
71
 
@@ -75,6 +75,38 @@ gl.clear(gl.COLOR_BUFFER_BIT)
75
75
  gl.swap()
76
76
  ```
77
77
 
78
+ ### WebGPU
79
+ ```js
80
+ import sdl from '@kmamal/sdl'
81
+ import gpu from '@kmamal/gpu'
82
+
83
+ const window = sdl.video.createWindow({ title: "WebGPU", webgpu: true })
84
+
85
+ const instance = gpu.create([])
86
+ const adapter = await instance.requestAdapter()
87
+ const device = await adapter.requestDevice()
88
+
89
+ const renderer = gpu.renderGPUDeviceToWindow({ device, window })
90
+
91
+ // Clear screen to red
92
+ const commandEncoder = device.createCommandEncoder()
93
+ const renderPass = commandEncoder.beginRenderPass({
94
+ colorAttachments: [
95
+ {
96
+ view: renderer.getCurrentTextureView(),
97
+ clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 },
98
+ loadOp: 'clear',
99
+ storeOp: 'store',
100
+ },
101
+ ],
102
+ })
103
+ renderPass.end()
104
+ device.queue.submit([ commandEncoder.finish() ])
105
+
106
+ renderer.swap()
107
+
108
+ ```
109
+
78
110
  There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples).
79
111
 
80
112
 
@@ -148,6 +180,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
148
180
  * [window.vsync](#windowvsync)
149
181
  * [window.setVsync(vsync)](#windowsetvsyncvsync)
150
182
  * [window.opengl](#windowopengl)
183
+ * [window.webgpu](#windowwebgpu)
151
184
  * [window.native](#windownative)
152
185
  * [window.maximized](#windowmaximized)
153
186
  * [window.maximize()](#windowmaximize)
@@ -246,6 +279,16 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
246
279
  * [controllerInstance.stopRumbleTriggers()](#controllerinstancestoprumbletriggers)
247
280
  * [controllerInstance.closed](#controllerinstanceclosed)
248
281
  * [controllerInstance.close()](#controllerinstanceclose)
282
+ * [sdl.sensor](#sdlsensor)
283
+ * [sdl.sensor.STANDARD_GRAVITY](#sdlsensorstandardgravity)
284
+ * [sdl.sensor.devices](#sdlsensordevices)
285
+ * [sdl.sensor.openDevice(device)](#sdlsensoropendevicedevice)
286
+ * [class SensorInstance](#class-sensorinstance)
287
+ * [Event: 'update'](#sensor-instance-event-update)
288
+ * [sensorInstance.device](#sensorinstancedevice)
289
+ * [sensorInstance.data](#sensorinstancedata)
290
+ * [sensorInstance.closed](#sensorinstanceclosed)
291
+ * [sensorInstance.close()](#sensorinstanceclose)
249
292
  * [sdl.audio](#sdlaudio)
250
293
  * [Audio data](#audio-data)
251
294
  * [Sample formats](#sample-formats)
@@ -532,6 +575,7 @@ The window that the mouse is hovered over, or `null` if the mouse is not over a
532
575
  * `accelerated: <boolean>` Set to `false` to disable hardware accelerated rendering. Default: `true`
533
576
  * `vsync: <boolean>` Set to `false` to disable frame rate synchronization. Default: `true`
534
577
  * `opengl: <boolean>` Set to `true` to create an OpenGL-compatible window (for use with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme)). Default: `false`
578
+ * `webgpu: <boolean>` Set to `true` to create an WebGPU-compatible window (for use with [@kmamal/gpu](https://github.com/kmamal/gpu#readme)). Default: `false`
535
579
  * `skipTaskbar: <boolean>` X11 only. Set to `true` to not add this window to the taskbar. Default: `false`
536
580
  * `popupMenu: <boolean>` X11 only. Set to `true` to treat this window like a popup menu. Default: `false`
537
581
  * `tooltip: <boolean>` X11 only. Set to `true` to treat this window like a tooltip. Default: `false`
@@ -543,9 +587,11 @@ Creates a new window.
543
587
  The following restrictions apply:
544
588
  * If you specify the `display` option, you can't also specify the `x` or `y` options, and vice-versa.
545
589
  * The `resizable` and `borderless` options are mutually exclusive.
590
+ * The `opengl` and `webgpu` options are mutually exclusive.
546
591
  * The `vsync` option only applies to windows that are also `accelerated`.
592
+ * The `accelerated` and `vsync` options have no effect if either `opengl` or `webgpu` is also specified.
547
593
 
548
- If you set the `opengl` option, then you can only render to the window with OpenGL calls.
594
+ If you set the `opengl` or `webgpu` options, then you can only render to the window with OpenGL/WebGPU calls.
549
595
  Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
550
596
 
551
597
  ## class Window
@@ -857,6 +903,8 @@ Will be `true` if the window is using hardware accelerated rendering.
857
903
 
858
904
  Changes the window's accelerated property.
859
905
 
906
+ If you have set the `opengl` or `webgpu` options, then calls to this function will fail.
907
+
860
908
  ### window.vsync
861
909
 
862
910
  * `<boolean>`
@@ -871,6 +919,8 @@ Note that vsync can only be used if that window is also [`accelerated`](#windowa
871
919
 
872
920
  Changes the window's vsync property.
873
921
 
922
+ If you have set the `opengl` or `webgpu` options, then calls to this function will fail.
923
+
874
924
  ### window.opengl
875
925
 
876
926
  * `<boolean>`
@@ -879,12 +929,20 @@ Will be `true` if the window was created in OpenGl mode.
879
929
  In OpenGL mode, you can only render to the window with OpenGL calls.
880
930
  Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
881
931
 
932
+ ### window.webgpu
933
+
934
+ * `<boolean>`
935
+
936
+ Will be `true` if the window was created in WebGPU mode.
937
+ In WebGPU mode, you can only render to the window with WebGPU calls.
938
+ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
939
+
882
940
  ### window.native
883
941
 
884
942
  * `<any>`
885
943
 
886
944
  Holds a copy of the native (platform-specific) representation of the window.
887
- Only used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme).
945
+ Only used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) or [@kmamal/gpu](https://github.com/kmamal/gpu#readme).
888
946
 
889
947
  ### window.maximized
890
948
 
@@ -965,6 +1023,9 @@ Such a window will always be treated as a utility window.
965
1023
  Displays an image in the window.
966
1024
  The provided image will be stretched over the entire window.
967
1025
 
1026
+ If you set the `opengl` or `webgpu` options, then you can only render to the window with OpenGL/WebGPU calls.
1027
+ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
1028
+
968
1029
  ### window.setIcon(width, height, stride, format, buffer)
969
1030
 
970
1031
  * `width, height, stride, format, buffer: `[`<Image>`](#image-data) The image to display as the icon of the window.
@@ -1936,6 +1997,95 @@ A closed instance object should not be used any further.
1936
1997
 
1937
1998
  Closes the instance.
1938
1999
 
2000
+
2001
+ ## sdl.sensor
2002
+
2003
+ ### sdl.sensor.STANDARD_GRAVITY
2004
+
2005
+ * `<number>`
2006
+
2007
+ 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.
2008
+ Use this constant to correct for that if your application requires it.
2009
+
2010
+ ### sdl.sensor.devices
2011
+
2012
+ * `<object>[]`
2013
+ * `id: <number>` The unique id of the device.
2014
+ * `name: <string>` The name of the device.
2015
+ * `type: <string>` Will be either `'accelerometer'`, `'gyroscope'`, or `'unknown'`.
2016
+ * `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`.
2017
+
2018
+ A list of all the detected sensor devices.
2019
+ Sample output:
2020
+
2021
+ // TODO
2022
+
2023
+ ### sdl.sensor.openDevice(device)
2024
+
2025
+ * `device: <object>` An object from [`sdl.sensor.devices`](#sdlsensordevices) that should be opened.
2026
+ * Returns: [`<SensorInstance>`](#class-sensorinstance) an object representing the opened sensor device instance.
2027
+
2028
+ Initializes a sensor device and returns a corresponding instance.
2029
+
2030
+ ## class SensorInstance
2031
+
2032
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
2033
+ Instead, objects returned by [`sdl.sensor.openDevice()`](#sdlsensoropendevicedevice) are of this type.
2034
+
2035
+ <a id="sensor-instance-event-update"></a>
2036
+
2037
+ ### Event: 'update'
2038
+
2039
+ Fired when the sensor's data changes.
2040
+ Get the new data by accessing [`sensorInstance.data`](#sensorInstancedata)
2041
+
2042
+ <a id="sensor-instance-event-close"></a>
2043
+
2044
+ ### Event: 'close'
2045
+
2046
+ Fired when the instance is about to close.
2047
+ Handle cleanup here.
2048
+
2049
+ ### sensorInstance.device
2050
+
2051
+ * `<object>`
2052
+
2053
+ The [device](#sdlsensordevices) from which this instance was opened.
2054
+
2055
+ ### sensorInstance.data
2056
+
2057
+ * `<object>`
2058
+ * `timestamp: <number | null>` The time this measurement was taken **in microseconds**, or `null` if that's not available.
2059
+ * `x: <number>` X axis value.
2060
+ * `y: <number>` Y axis value.
2061
+ * `z: <number>` Z axis value.
2062
+
2063
+ An object reporting the latest measurement from the sensor.
2064
+
2065
+ For accelerometers, the values correspond to the current acceleration in meters per second squared.
2066
+ 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.
2067
+
2068
+ For gyroscopes, the values correspond to the current rate of rotation in radians per second.
2069
+ The rotation is positive in the counter-clockwise direction.
2070
+ 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.
2071
+
2072
+ For phones held in portrait mode and game controllers held in front of you, the axes are defined as follows:
2073
+ * -X ... +X : left ... right
2074
+ * -Y ... +Y : bottom ... top
2075
+ * -Z ... +Z : farther ... closer
2076
+
2077
+ ### sensorInstance.closed
2078
+
2079
+ * `<boolean>`
2080
+
2081
+ Will be `true` if the instance is closed.
2082
+ A closed instance object should not be used any further.
2083
+
2084
+ ### sensorInstance.close()
2085
+
2086
+ Closes the instance.
2087
+
2088
+
1939
2089
  ## sdl.audio
1940
2090
 
1941
2091
  ### Audio data
@@ -2357,11 +2507,11 @@ Changes the text contents of the clipboard.
2357
2507
  ### sdl.power.info
2358
2508
 
2359
2509
  * `<object>`
2360
- * `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'changed'`.
2510
+ * `state: <string>` One of `'unknown'`, `'noBattery'`, `'battery'`, `'charging'`, `'charged'`.
2361
2511
  * `seconds: <number | null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2362
2512
  * `percent: <number | null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
2363
2513
 
2364
- The curent power state of the device.
2514
+ The curent power information of the device.
2365
2515
 
2366
2516
 
2367
2517
  ## Building from source
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.3",
2
+ "version": "0.9.0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -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
  }
@@ -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 }
@@ -48,6 +48,7 @@ class Window extends EventsViaPoll {
48
48
  accelerated = true,
49
49
  vsync = true,
50
50
  opengl = false,
51
+ webgpu = false,
51
52
  skipTaskbar = false,
52
53
  popupMenu = false,
53
54
  tooltip = false,
@@ -74,12 +75,14 @@ class Window extends EventsViaPoll {
74
75
  if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
75
76
  if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
76
77
  if (typeof opengl !== 'boolean') { throw Object.assign(new Error("opengl must be a boolean"), { opengl }) }
78
+ if (typeof webgpu !== 'boolean') { throw Object.assign(new Error("webgpu must be a boolean"), { webgpu }) }
77
79
  if (typeof skipTaskbar !== 'boolean') { throw Object.assign(new Error("skipTaskbar must be a boolean"), { skipTaskbar }) }
78
80
  if (typeof popupMenu !== 'boolean') { throw Object.assign(new Error("popupMenu must be a boolean"), { popupMenu }) }
79
81
  if (typeof tooltip !== 'boolean') { throw Object.assign(new Error("tooltip must be a boolean"), { tooltip }) }
80
82
  if (typeof utility !== 'boolean') { throw Object.assign(new Error("utility must be a boolean"), { utility }) }
81
83
  if (display && (Number.isFinite(x) || Number.isFinite(y))) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
82
84
  if (resizable && borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless }) }
85
+ if (opengl && webgpu) { throw Object.assign(new Error("opengl and webgpu are mutually exclusive"), { opengl, webgpu }) }
83
86
 
84
87
  let displayIndex = 0
85
88
  if (display) {
@@ -104,6 +107,7 @@ class Window extends EventsViaPoll {
104
107
  accelerated,
105
108
  vsync,
106
109
  opengl,
110
+ webgpu,
107
111
  skipTaskbar,
108
112
  popupMenu,
109
113
  tooltip,
@@ -132,6 +136,7 @@ class Window extends EventsViaPoll {
132
136
  this._title = title
133
137
  this._visible = visible
134
138
  this._opengl = opengl
139
+ this._webgpu = webgpu
135
140
 
136
141
  this._focused = false
137
142
  this._hovered = false
@@ -256,6 +261,9 @@ class Window extends EventsViaPoll {
256
261
  setAccelerated (accelerated) {
257
262
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
258
263
 
264
+ if (this._opengl) { throw new Error("can't call setAccelerated in opengl mode") }
265
+ if (this._webgpu) { throw new Error("can't call setAccelerated in webgpu mode") }
266
+
259
267
  if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
260
268
 
261
269
  [ this._accelerated, this._vsync ] = Bindings.window_setAcceleratedAndVsync(this._id, accelerated, this._vsync)
@@ -265,12 +273,16 @@ class Window extends EventsViaPoll {
265
273
  setVsync (vsync) {
266
274
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
267
275
 
276
+ if (this._opengl) { throw new Error("can't call setVsync in opengl mode") }
277
+ if (this._webgpu) { throw new Error("can't call setVsync in webgpu mode") }
278
+
268
279
  if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
269
280
 
270
281
  [ this._accelerated, this._vsync ] = Bindings.window_setAcceleratedAndVsync(this._id, this._accelerated, vsync)
271
282
  }
272
283
 
273
284
  get opengl () { return this._opengl }
285
+ get webgpu () { return this._webgpu }
274
286
  get native () { return this._native }
275
287
 
276
288
  get minimized () { return this._minimized }
@@ -325,6 +337,7 @@ class Window extends EventsViaPoll {
325
337
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
326
338
 
327
339
  if (this._opengl) { throw new Error("can't call render in opengl mode") }
340
+ if (this._webgpu) { throw new Error("can't call render in webgpu mode") }
328
341
 
329
342
  const _format = Enums.pixelFormat[format] ?? null
330
343
 
@@ -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);
@@ -312,7 +321,7 @@ window_create(napi_env env, napi_callback_info info)
312
321
  int * height = nullptr;
313
322
  void * native_pointer = nullptr;
314
323
 
315
- napi_value argv[18];
324
+ napi_value argv[19];
316
325
  size_t argc = sizeof(argv) / sizeof(napi_value);
317
326
  CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
318
327
 
@@ -351,7 +360,7 @@ window_create(napi_env env, napi_callback_info info)
351
360
  }
352
361
 
353
362
  bool visible, fullscreen, resizable, borderless, always_on_top,
354
- accelerated, vsync, opengl,
363
+ accelerated, vsync, opengl, webgpu,
355
364
  skip_taskbar, popup_menu, tooltip, utility;
356
365
  CALL_NAPI(napi_get_value_bool, argv[6], &visible);
357
366
  CALL_NAPI(napi_get_value_bool, argv[7], &fullscreen);
@@ -361,21 +370,22 @@ window_create(napi_env env, napi_callback_info info)
361
370
  CALL_NAPI(napi_get_value_bool, argv[11], &accelerated);
362
371
  CALL_NAPI(napi_get_value_bool, argv[12], &vsync);
363
372
  CALL_NAPI(napi_get_value_bool, argv[13], &opengl);
364
- CALL_NAPI(napi_get_value_bool, argv[14], &skip_taskbar);
365
- CALL_NAPI(napi_get_value_bool, argv[15], &popup_menu);
366
- CALL_NAPI(napi_get_value_bool, argv[16], &tooltip);
367
- CALL_NAPI(napi_get_value_bool, argv[17], &utility);
373
+ CALL_NAPI(napi_get_value_bool, argv[14], &webgpu);
374
+ CALL_NAPI(napi_get_value_bool, argv[15], &skip_taskbar);
375
+ CALL_NAPI(napi_get_value_bool, argv[16], &popup_menu);
376
+ CALL_NAPI(napi_get_value_bool, argv[17], &tooltip);
377
+ CALL_NAPI(napi_get_value_bool, argv[18], &utility);
368
378
 
369
- int pixel_width, pixel_height, window_id, native_pointer_size;
379
+ int pixel_width, pixel_height, window_id, native_size;
370
380
 
371
381
  CALL_SDL_HELPER(window_create,
372
382
  title, display,
373
383
  &x, &y, &width, &height,
374
384
  &pixel_width, &pixel_height,
375
385
  visible, &fullscreen, &resizable, &borderless, &always_on_top,
376
- &accelerated, &vsync, opengl,
386
+ &accelerated, &vsync, opengl, webgpu,
377
387
  &skip_taskbar, &popup_menu, &tooltip, &utility,
378
- &window_id, &native_pointer, &native_pointer_size
388
+ &window_id, &native_pointer, &native_size
379
389
  );
380
390
 
381
391
  napi_value key, value;
@@ -461,7 +471,7 @@ window_create(napi_env env, napi_callback_info info)
461
471
 
462
472
  if (native_pointer) {
463
473
  CALL_NAPI(napi_create_external_buffer,
464
- native_pointer_size, native_pointer, freePointer, native_pointer,
474
+ native_size, native_pointer, freePointer, native_pointer,
465
475
  &value);
466
476
  CALL_NAPI(napi_create_string_latin1, "native", 6, &key);
467
477
  CALL_NAPI(napi_set_property, result, key, value);
@@ -1260,6 +1270,79 @@ controller_addMappings(napi_env env, napi_callback_info info)
1260
1270
  }
1261
1271
 
1262
1272
 
1273
+ napi_value
1274
+ sensor_getDevices(napi_env env, napi_callback_info info)
1275
+ {
1276
+ napi_value result = nullptr;
1277
+
1278
+ SdlHelpers::Variant devices;
1279
+ CALL_SDL_HELPER(sensor_getDevices, devices);
1280
+
1281
+ CALL_NAPI(fromVariant, devices, &result);
1282
+
1283
+ cleanup:
1284
+ return result;
1285
+ }
1286
+
1287
+ napi_value
1288
+ sensor_open(napi_env env, napi_callback_info info)
1289
+ {
1290
+ SdlHelpers::Variant object;
1291
+
1292
+ napi_value argv[1];
1293
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1294
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1295
+
1296
+ int index;
1297
+ CALL_NAPI(napi_get_value_int32, argv[0], &index);
1298
+
1299
+ CALL_SDL_HELPER(sensor_open, index);
1300
+
1301
+ cleanup:
1302
+ return nullptr;
1303
+ }
1304
+
1305
+ napi_value
1306
+ sensor_getData(napi_env env, napi_callback_info info)
1307
+ {
1308
+ napi_value result = nullptr;
1309
+
1310
+ SdlHelpers::Variant object;
1311
+
1312
+ napi_value argv[1];
1313
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1314
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1315
+
1316
+ int sensor_id;
1317
+ CALL_NAPI(napi_get_value_int32, argv[0], &sensor_id);
1318
+
1319
+ CALL_SDL_HELPER(sensor_getData, sensor_id, object);
1320
+
1321
+ CALL_NAPI(fromVariant, object, &result);
1322
+
1323
+ cleanup:
1324
+ return result;
1325
+ }
1326
+
1327
+ napi_value
1328
+ sensor_close(napi_env env, napi_callback_info info)
1329
+ {
1330
+ SdlHelpers::Variant object;
1331
+
1332
+ napi_value argv[1];
1333
+ size_t argc = sizeof(argv) / sizeof(napi_value);
1334
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
1335
+
1336
+ int sensor_id;
1337
+ CALL_NAPI(napi_get_value_int32, argv[0], &sensor_id);
1338
+
1339
+ CALL_SDL_HELPER(sensor_close, sensor_id);
1340
+
1341
+ cleanup:
1342
+ return nullptr;
1343
+ }
1344
+
1345
+
1263
1346
  napi_value
1264
1347
  audio_getDevices(napi_env env, napi_callback_info info)
1265
1348
  {
@@ -1550,6 +1633,10 @@ init (napi_env env, napi_value exports)
1550
1633
  { "controller_open", NULL, controller_open, NULL, NULL, NULL, napi_enumerable, NULL },
1551
1634
  { "controller_close", NULL, controller_close, NULL, NULL, NULL, napi_enumerable, NULL },
1552
1635
  { "controller_addMappings", NULL, controller_addMappings, NULL, NULL, NULL, napi_enumerable, NULL },
1636
+ { "sensor_getDevices", NULL, sensor_getDevices, NULL, NULL, NULL, napi_enumerable, NULL },
1637
+ { "sensor_open", NULL, sensor_open, NULL, NULL, NULL, napi_enumerable, NULL },
1638
+ { "sensor_getData", NULL, sensor_getData, NULL, NULL, NULL, napi_enumerable, NULL },
1639
+ { "sensor_close", NULL, sensor_close, NULL, NULL, NULL, napi_enumerable, NULL },
1553
1640
  { "audio_getDevices", NULL, audio_getDevices, NULL, NULL, NULL, napi_enumerable, NULL },
1554
1641
  { "audio_openDevice", NULL, audio_openDevice, NULL, NULL, NULL, napi_enumerable, NULL },
1555
1642
  { "audio_close", NULL, audio_close, NULL, NULL, NULL, napi_enumerable, NULL },
@@ -1564,7 +1651,7 @@ init (napi_env env, napi_value exports)
1564
1651
  { "cleanup", NULL, cleanup, NULL, NULL, NULL, napi_enumerable, NULL }
1565
1652
  };
1566
1653
 
1567
- CALL_NAPI(napi_define_properties, exports, 55, desc);
1654
+ CALL_NAPI(napi_define_properties, exports, 59, desc);
1568
1655
 
1569
1656
  cleanup:
1570
1657
  return exports;
@@ -3,6 +3,8 @@
3
3
  class CALayer;
4
4
  #endif
5
5
 
6
- extern "C" CALayer * getView(NSWindow *);
6
+ extern "C" CALayer * getCocoaGlView(NSWindow *);
7
+
8
+ extern "C" CALayer * getCocoaGpuView(NSWindow *);
7
9
 
8
10
  extern "C" void reenableInertialScrolling();
@@ -1,10 +1,20 @@
1
1
  #include <Cocoa/Cocoa.h>
2
+ #import <QuartzCore/CAMetalLayer.h>
2
3
 
3
4
  #include "cocoa-window.h"
4
5
 
5
- extern "C" CALayer * getView(NSWindow * window) {
6
+ extern "C" CALayer * getCocoaGlView(NSWindow * window) {
6
7
  NSView * view = window.contentView;
7
8
  [view setWantsLayer:YES];
9
+ [[view layer] setContentsScale:[window backingScaleFactor]];
10
+ return view.layer;
11
+ }
12
+
13
+ extern "C" CALayer * getCocoaGpuView(NSWindow * window) {
14
+ NSView * view = window.contentView;
15
+ [view setWantsLayer:YES];
16
+ [view setLayer:[CAMetalLayer layer]];
17
+ [[view layer] setContentsScale:[window backingScaleFactor]];
8
18
  return view.layer;
9
19
  }
10
20
 
@@ -11,19 +11,28 @@
11
11
 
12
12
 
13
13
  #if defined(__LINUX__)
14
- #define __WINDOW_TYPE__ Window
15
- #define GET_WINDOW(x) x.info.x11.window
14
+ #define GL_NativeWindow Window
15
+ struct GPU_NativeData {
16
+ Display *display;
17
+ Window window;
18
+ };
19
+ #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
16
20
  #elif defined(__WIN32__)
17
- #define __WINDOW_TYPE__ HWND
18
- #define GET_WINDOW(x) x.info.win.window
21
+ #define GL_NativeWindow HWND
22
+ struct GPU_NativeData {
23
+ HWND hwnd;
24
+ HINSTANCE hinstance;
25
+ };
26
+ #define GPU_WINDOW_FLAG 0
19
27
  #elif defined(__MACOSX__)
20
28
  #include "cocoa-window.h"
21
- #define __WINDOW_TYPE__ CALayer *
22
- #define GET_WINDOW(x) getView(x.info.cocoa.window)
29
+ #define GL_NativeWindow CALayer *
30
+ struct GPU_NativeData {
31
+ CALayer *layer;
32
+ };
33
+ #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
23
34
  #endif
24
35
 
25
- typedef __WINDOW_TYPE__ NativeWindow;
26
-
27
36
 
28
37
  #define RETURN_ERROR(...) do { \
29
38
  char * error; \
@@ -129,6 +138,7 @@ enum class EventFamily {
129
138
  JOYSTICK_DEVICE,
130
139
  JOYSTICK,
131
140
  CONTROLLER,
141
+ SENSOR,
132
142
  AUDIO_DEVICE,
133
143
  CLIPBOARD,
134
144
  };
@@ -694,6 +704,22 @@ enum_getControllerButtons (Variant & buttons)
694
704
  return nullptr;
695
705
  }
696
706
 
707
+ ErrorMessage *
708
+ enum_getSensorTypes (Variant & types)
709
+ {
710
+ MAKE_MAP(types);
711
+
712
+ SET_NUM(types, "unknown", SDL_SENSOR_UNKNOWN);
713
+ SET_NUM(types, "accelerometer", SDL_SENSOR_ACCEL);
714
+ SET_NUM(types, "gyroscope", SDL_SENSOR_GYRO);
715
+ SET_NUM(types, "accelerometerLeft", SDL_SENSOR_ACCEL_L);
716
+ SET_NUM(types, "gyroscopeLeft", SDL_SENSOR_GYRO_L);
717
+ SET_NUM(types, "accelerometerRight", SDL_SENSOR_ACCEL_R);
718
+ SET_NUM(types, "gyroscopeRight", SDL_SENSOR_GYRO_R);
719
+
720
+ return nullptr;
721
+ }
722
+
697
723
  ErrorMessage *
698
724
  enum_getPowerStates (Variant & states)
699
725
  {
@@ -726,7 +752,8 @@ initialize (Variant & object)
726
752
  | SDL_INIT_EVENTS
727
753
  | SDL_INIT_JOYSTICK
728
754
  | SDL_INIT_GAMECONTROLLER
729
- | SDL_INIT_HAPTIC;
755
+ | SDL_INIT_HAPTIC
756
+ | SDL_INIT_SENSOR;
730
757
  if (SDL_Init(subsystems) != 0) {
731
758
  RETURN_ERROR("SDL_Init() error: %s\n", SDL_GetError());
732
759
  }
@@ -862,21 +889,6 @@ initialize (Variant & object)
862
889
  }
863
890
 
864
891
 
865
- ErrorMessage *
866
- _windowGetPixelSize(SDL_Window * window, int * pixel_width, int * pixel_height)
867
- {
868
- SDL_Renderer * renderer = SDL_GetRenderer(window);
869
- if (renderer == nullptr) {
870
- RETURN_ERROR("SDL_GetRenderer() error: %s\n", SDL_GetError());
871
- }
872
-
873
- if (SDL_GetRendererOutputSize(renderer, pixel_width, pixel_height) != 0) {
874
- RETURN_ERROR("SDL_GetRendererOutputSize() error: %s\n", SDL_GetError());
875
- }
876
-
877
- return nullptr;
878
- }
879
-
880
892
  ErrorMessage *
881
893
  _windowUpdateTexture(
882
894
  SDL_Window * window,
@@ -933,8 +945,7 @@ _windowUpdateRenderer(
933
945
  ErrorMessage * error;
934
946
 
935
947
  int pixel_width, pixel_height;
936
- error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
937
- if (error != nullptr) { return error; }
948
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
938
949
 
939
950
  error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, nullptr);
940
951
  if (error != nullptr) { return error; }
@@ -1046,8 +1057,7 @@ packageEvent (const SDL_Event & event, Variant & object)
1046
1057
  }
1047
1058
 
1048
1059
  int pixel_width, pixel_height;
1049
- ErrorMessage * error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
1050
- if (error != nullptr) { return error; }
1060
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
1051
1061
 
1052
1062
  SET_NUM(object, "type", (int) EventType::RESIZE);
1053
1063
  SET_NUM(object, "width", event.window.data1);
@@ -1263,6 +1273,13 @@ packageEvent (const SDL_Event & event, Variant & object)
1263
1273
  break;
1264
1274
  }
1265
1275
 
1276
+ case SDL_SENSORUPDATE: {
1277
+ SET_NUM(object, "family", (int) EventFamily::SENSOR);
1278
+ SET_NUM(object, "type", (int) EventType::UPDATE);
1279
+ SET_NUM(object, "sensorId", event.sensor.which);
1280
+ break;
1281
+ }
1282
+
1266
1283
  case SDL_AUDIODEVICEADDED: {
1267
1284
  SET_NUM(object, "family", (int) EventFamily::AUDIO_DEVICE);
1268
1285
  SET_NUM(object, "type", (int) EventType::DEVICE_ADD);
@@ -1450,11 +1467,12 @@ window_create (
1450
1467
  bool * accelerated,
1451
1468
  bool * vsync,
1452
1469
  bool opengl,
1470
+ bool webgpu,
1453
1471
  bool * skip_taskbar,
1454
1472
  bool * popup_menu,
1455
1473
  bool * tooltip,
1456
1474
  bool * utility,
1457
- int * window_id, void ** native_pointer, int * native_pointer_size
1475
+ int * window_id, void ** native_pointer, int * native_size
1458
1476
  ) {
1459
1477
  if (*x == nullptr) {
1460
1478
  *x = (int *) malloc(sizeof(int));
@@ -1479,17 +1497,22 @@ window_create (
1479
1497
  | (*borderless ? SDL_WINDOW_BORDERLESS : 0)
1480
1498
  | (*always_on_top ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
1481
1499
  | (opengl ? SDL_WINDOW_OPENGL : 0)
1500
+ | (webgpu ? GPU_WINDOW_FLAG : 0)
1482
1501
  | (*skip_taskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
1483
1502
  | (*popup_menu ? SDL_WINDOW_POPUP_MENU : 0)
1484
1503
  | (*tooltip ? SDL_WINDOW_TOOLTIP : 0)
1485
- | (*utility ? SDL_WINDOW_UTILITY : 0);
1504
+ | (*utility ? SDL_WINDOW_UTILITY : 0)
1505
+ ;
1486
1506
 
1487
1507
  SDL_Window * window = SDL_CreateWindow(title, **x, **y, **width, **height, desired_flags);
1488
1508
  if (window == nullptr) {
1489
1509
  RETURN_ERROR("SDL_CreateWindow() error: %s\n", SDL_GetError());
1490
1510
  }
1491
1511
 
1492
- SDL_GetWindowPosition(window, *x, *y);
1512
+ *window_id = SDL_GetWindowID(window);
1513
+ if (*window_id == 0) {
1514
+ RETURN_ERROR("SDL_GetWindowID() error: %s\n", SDL_GetError());
1515
+ }
1493
1516
 
1494
1517
  int actual_flags = SDL_GetWindowFlags(window);
1495
1518
  *fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
@@ -1501,12 +1524,10 @@ window_create (
1501
1524
  *tooltip = actual_flags & SDL_WINDOW_TOOLTIP;
1502
1525
  *utility = actual_flags & SDL_WINDOW_UTILITY;
1503
1526
 
1504
- *window_id = SDL_GetWindowID(window);
1505
- if (*window_id == 0) {
1506
- RETURN_ERROR("SDL_GetWindowID() error: %s\n", SDL_GetError());
1507
- }
1527
+ SDL_GetWindowPosition(window, *x, *y);
1528
+ SDL_GetWindowSizeInPixels(window, pixel_width, pixel_height);
1508
1529
 
1509
- if (opengl) {
1530
+ if (opengl || webgpu) {
1510
1531
  SDL_SysWMinfo info;
1511
1532
  SDL_VERSION(&(info.version));
1512
1533
  bool got_info = SDL_GetWindowWMInfo(window, &info);
@@ -1514,20 +1535,41 @@ window_create (
1514
1535
  RETURN_ERROR("SDL_GetWindowWMInfo(%d) error: %s\n", *window_id, SDL_GetError());
1515
1536
  }
1516
1537
 
1517
- int size = sizeof(NativeWindow);
1518
- NativeWindow * pointer = (NativeWindow *) malloc(size);
1519
- *pointer = GET_WINDOW(info);
1520
- *native_pointer = pointer;
1521
- *native_pointer_size = size;
1538
+ if (opengl) {
1539
+ int size = sizeof(GL_NativeWindow);
1540
+ GL_NativeWindow *pointer = (GL_NativeWindow *) malloc(size);
1541
+ #if defined(__LINUX__)
1542
+ *pointer = info.info.x11.window;
1543
+ #elif defined(__WIN32__)
1544
+ *pointer = info.info.win.window;
1545
+ #elif defined(__MACOSX__)
1546
+ *pointer = getCocoaGlView(info.info.cocoa.window);
1547
+ #endif
1548
+
1549
+ *native_pointer = pointer;
1550
+ *native_size = size;
1551
+ } else if (webgpu) {
1552
+ int size = sizeof(GPU_NativeData);
1553
+ GPU_NativeData *pointer = (GPU_NativeData *) malloc(size);
1554
+
1555
+ #if defined(__LINUX__)
1556
+ pointer->display = info.info.x11.display;
1557
+ pointer->window = info.info.x11.window;
1558
+ #elif defined(__WIN32__)
1559
+ pointer->hwnd = info.info.win.window;
1560
+ pointer->hinstance = info.info.win.hinstance;
1561
+ #elif defined(__MACOSX__)
1562
+ pointer->layer = getCocoaGpuView(info.info.cocoa.window);
1563
+ #endif
1564
+
1565
+ *native_pointer = pointer;
1566
+ *native_size = size;
1567
+ }
1568
+ }
1569
+ else {
1570
+ ErrorMessage * error = _windowUpdateRenderer(window, accelerated, vsync);
1571
+ if (error != nullptr) { return error; }
1522
1572
  }
1523
-
1524
- ErrorMessage * error;
1525
-
1526
- error = _windowUpdateRenderer(window, accelerated, vsync);
1527
- if (error != nullptr) { return error; }
1528
-
1529
- error = _windowGetPixelSize(window, pixel_width, pixel_height);
1530
- if (error != nullptr) { return error; }
1531
1573
 
1532
1574
  if (visible) { SDL_ShowWindow(window); }
1533
1575
 
@@ -1570,8 +1612,7 @@ window_setSize (int window_id, int width, int height, int * pixel_width, int * p
1570
1612
 
1571
1613
  SDL_SetWindowSize(window, width, height);
1572
1614
 
1573
- ErrorMessage * error = _windowGetPixelSize(window, pixel_width, pixel_height);
1574
- if (error != nullptr) { return error; }
1615
+ SDL_GetWindowSizeInPixels(window, pixel_width, pixel_height);
1575
1616
 
1576
1617
  return nullptr;
1577
1618
  }
@@ -1771,8 +1812,7 @@ window_render (
1771
1812
  }
1772
1813
 
1773
1814
  int pixel_width, pixel_height;
1774
- ErrorMessage * error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
1775
- if (error != nullptr) { return error; }
1815
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
1776
1816
 
1777
1817
  if (pixel_width != texture_width || pixel_height != texture_height) {
1778
1818
  ErrorMessage * error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, &texture);
@@ -1826,14 +1866,13 @@ window_destroy (int window_id)
1826
1866
  }
1827
1867
 
1828
1868
  SDL_Renderer * renderer = SDL_GetRenderer(window);
1829
- if (renderer == nullptr) {
1830
- RETURN_ERROR("SDL_GetRenderer(%d) error: %s\n", window_id, SDL_GetError());
1831
- }
1869
+ if (renderer != nullptr) {
1870
+ SDL_Texture * texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
1832
1871
 
1833
- SDL_Texture * texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
1872
+ SDL_DestroyTexture(texture);
1873
+ SDL_DestroyRenderer(renderer);
1874
+ }
1834
1875
 
1835
- SDL_DestroyTexture(texture);
1836
- SDL_DestroyRenderer(renderer);
1837
1876
  SDL_DestroyWindow(window);
1838
1877
 
1839
1878
  return nullptr;
@@ -2309,6 +2348,89 @@ controller_addMappings (char ** mappings, int length)
2309
2348
  }
2310
2349
 
2311
2350
 
2351
+ ErrorMessage *
2352
+ sensor_getDevices (Variant & list)
2353
+ {
2354
+ int num_devices = SDL_NumSensors();
2355
+ if (num_devices < 0) {
2356
+ RETURN_ERROR("SDL_NumSensors() error: %s\n", SDL_GetError());
2357
+ }
2358
+
2359
+ MAKE_LIST(list);
2360
+
2361
+ for (int i = 0; i < num_devices; i++) {
2362
+ Variant device;
2363
+ MAKE_MAP(device);
2364
+
2365
+ SET_NUM(device, "_index", i);
2366
+
2367
+ int id = SDL_SensorGetDeviceInstanceID(i);
2368
+ SET_NUM(device, "id", id);
2369
+
2370
+ int type = SDL_SensorGetDeviceType(i);
2371
+ SET_NUM(device, "type", type);
2372
+
2373
+ const char * name = SDL_SensorGetDeviceName(i);
2374
+ if (name == nullptr) {
2375
+ RETURN_ERROR("SDL_SensorGetDeviceName(%d) error: %s\n", i, SDL_GetError());
2376
+ }
2377
+ SET_STRING(device, "name", name);
2378
+
2379
+ APPEND(list, device);
2380
+ }
2381
+
2382
+ return nullptr;
2383
+ }
2384
+
2385
+ ErrorMessage *
2386
+ sensor_open (int index)
2387
+ {
2388
+ SDL_Sensor * sensor = SDL_SensorOpen(index);
2389
+ if (sensor == nullptr) {
2390
+ RETURN_ERROR("SDL_SensorOpen(%d) error: %s\n", index, SDL_GetError());
2391
+ }
2392
+
2393
+ return nullptr;
2394
+ }
2395
+
2396
+ ErrorMessage *
2397
+ sensor_close (int sensor_id)
2398
+ {
2399
+ SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2400
+ if (sensor == nullptr) {
2401
+ RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2402
+ }
2403
+
2404
+ SDL_SensorClose(sensor);
2405
+
2406
+ return nullptr;
2407
+ }
2408
+
2409
+ ErrorMessage *
2410
+ sensor_getData (int sensor_id, Variant & object)
2411
+ {
2412
+ SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2413
+ if (sensor == nullptr) {
2414
+ RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2415
+ }
2416
+
2417
+ Uint64 timestamp;
2418
+ float data[3];
2419
+ if (SDL_SensorGetDataWithTimestamp(sensor, &timestamp, data, 3) == -1) {
2420
+ RETURN_ERROR("SDL_SensorGetDataWithTimestamp(%d) error: %s\n", sensor_id, SDL_GetError());
2421
+ }
2422
+
2423
+ MAKE_MAP(object);
2424
+ SET_NUM(object, "timestamp", timestamp);
2425
+ SET_NUM(object, "x", data[0]);
2426
+ SET_NUM(object, "y", data[1]);
2427
+ SET_NUM(object, "z", data[2]);
2428
+
2429
+ return nullptr;
2430
+ }
2431
+
2432
+
2433
+
2312
2434
  ErrorMessage *
2313
2435
  audio_getDevices(bool capture, Variant & list)
2314
2436
  {
@@ -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);
@@ -66,11 +67,12 @@ ErrorMessage * window_create (
66
67
  bool * accelerated,
67
68
  bool * vsync,
68
69
  bool opengl,
70
+ bool webgpu,
69
71
  bool * skipTaskbar,
70
72
  bool * popupMenu,
71
73
  bool * tooltip,
72
74
  bool * utility,
73
- int * window_id, void ** native_pointer, int * native_pointer_size
75
+ int * window_id, void ** native_pointer, int * native_size
74
76
  );
75
77
  ErrorMessage * window_setTitle (int window_id, const char * title);
76
78
  ErrorMessage * window_setPosition (int window_id, int x, int y);
@@ -151,6 +153,11 @@ ErrorMessage * controller_open (int index, Variant & object);
151
153
  ErrorMessage * controller_close (int controller_id);
152
154
  ErrorMessage * controller_addMappings (char ** mappings, int length);
153
155
 
156
+ ErrorMessage * sensor_getDevices (Variant & list);
157
+ ErrorMessage * sensor_open (int index);
158
+ ErrorMessage * sensor_close (int sensor_id);
159
+ ErrorMessage * sensor_getData (int sensor_id, Variant & object);
160
+
154
161
  ErrorMessage * audio_getDevices (bool capture, Variant & list);
155
162
  ErrorMessage * audio_openDevice (
156
163
  const char * name, bool capture,
@@ -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 {
@@ -421,6 +437,8 @@ export namespace Sdl {
421
437
  readonly vsync: boolean
422
438
  setVsync (vsync: boolean): void
423
439
 
440
+ readonly opengl: boolean
441
+ readonly webgpu: boolean
424
442
  readonly native: unknown
425
443
 
426
444
  readonly maximized: boolean
@@ -476,6 +494,7 @@ export namespace Sdl {
476
494
  accelerated?: boolean
477
495
  vsync?: boolean
478
496
  opengl?: boolean
497
+ webgpu?: boolean
479
498
  skipTaskbar?: boolean
480
499
  popupMenu?: boolean
481
500
  tooltip?: boolean
@@ -969,6 +988,54 @@ export namespace Sdl {
969
988
 
970
989
  }
971
990
 
991
+ namespace Sensor {
992
+
993
+ type Type
994
+ = 'unknown'
995
+ | 'accelerometer'
996
+ | 'gyroscope'
997
+
998
+ type Side
999
+ = 'left'
1000
+ | 'right'
1001
+
1002
+ interface Device {
1003
+ readonly id: number
1004
+ readonly name: string
1005
+ readonly type: Type
1006
+ readonly side: Side | null
1007
+ }
1008
+
1009
+ interface Data {
1010
+ timestamp: number | null,
1011
+ x: number,
1012
+ y: number,
1013
+ z: number,
1014
+ }
1015
+
1016
+ class SensorInstance {
1017
+ on (event: 'update', listener: (event: Events.Sensor.Update) => void): this
1018
+ on (event: 'close', listener: (event: Events.Sensor.Close) => void): this
1019
+ on (event: '*', listener: (event: Events.Sensor.Any) => void): this
1020
+
1021
+ readonly device: Device
1022
+
1023
+ readonly data: Data
1024
+
1025
+ readonly closed: boolean
1026
+ close (): void
1027
+ }
1028
+
1029
+ interface Module {
1030
+ STANDARD_GRAVITY: 9.80665,
1031
+
1032
+ readonly devices: Device[]
1033
+
1034
+ openDevice (device: Device): SensorInstance
1035
+ }
1036
+
1037
+ }
1038
+
972
1039
  namespace Audio {
973
1040
 
974
1041
  type Format
@@ -1108,6 +1175,7 @@ export namespace Sdl {
1108
1175
  readonly mouse: Mouse.Module
1109
1176
  readonly joystick: Joystick.Module
1110
1177
  readonly controller: Controller.Module
1178
+ readonly sensor: Sensor.Module
1111
1179
  readonly audio: Audio.Module
1112
1180
  readonly clipboard: Clipboard.Module
1113
1181
  readonly power: Power.Module