@kmamal/sdl 0.11.5 → 0.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +46 -7
  2. package/binding.gyp +1 -0
  3. package/package.json +1 -1
  4. package/src/javascript/audio/audio-instance.js +2 -3
  5. package/src/javascript/audio/device.js +1 -10
  6. package/src/javascript/audio/index.js +5 -7
  7. package/src/javascript/controller/controller-instance.js +2 -0
  8. package/src/javascript/controller/device.js +14 -3
  9. package/src/javascript/controller/index.js +5 -11
  10. package/src/javascript/events/index.js +56 -126
  11. package/src/javascript/events/reconcile-audio-devices.js +15 -0
  12. package/src/javascript/events/reconcile-displays.js +16 -0
  13. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
  14. package/src/javascript/events/reconcile.js +61 -0
  15. package/src/javascript/joystick/device.js +9 -3
  16. package/src/javascript/joystick/index.js +13 -5
  17. package/src/javascript/joystick/joystick-instance.js +2 -0
  18. package/src/javascript/sensor/sensor-instance.js +2 -0
  19. package/src/javascript/video/display.js +4 -0
  20. package/src/javascript/video/index.js +1 -0
  21. package/src/native/audio.cpp +10 -12
  22. package/src/native/audio.h +4 -0
  23. package/src/native/controller.cpp +72 -56
  24. package/src/native/controller.h +3 -0
  25. package/src/native/enums.cpp +315 -315
  26. package/src/native/events.cpp +156 -80
  27. package/src/native/events.h +1 -0
  28. package/src/native/global.cpp +46 -20
  29. package/src/native/joystick.cpp +87 -48
  30. package/src/native/joystick.h +3 -2
  31. package/src/native/keyboard.cpp +1 -1
  32. package/src/native/mouse.cpp +2 -2
  33. package/src/native/power.cpp +9 -3
  34. package/src/native/sensor.cpp +24 -13
  35. package/src/native/video.cpp +21 -13
  36. package/src/native/video.h +1 -0
  37. package/src/native/window.cpp +28 -22
  38. package/src/types/index.d.ts +57 -17
package/README.md CHANGED
@@ -134,6 +134,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
134
134
  * [Event: 'displayAdd'](#event-displayadd)
135
135
  * [Event: 'displayRemove'](#event-displayremove)
136
136
  * [Event: 'displayOrient'](#event-displayorient)
137
+ * [Event: 'displayMove'](#event-displaymove)
137
138
  * [sdl.video.displays](#sdlvideodisplays)
138
139
  * [sdl.video.windows](#sdlvideowindows)
139
140
  * [sdl.video.focused](#sdlvideofocused)
@@ -511,9 +512,15 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
511
512
  ### Event: 'displayOrient'
512
513
 
513
514
  * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
515
+ * `orientation: <string>`: The display's new orientation.
514
516
 
515
517
  Fired when a display changes orientation.
516
- Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
518
+
519
+ ### Event: 'displayMove'
520
+
521
+ * `device: <object>`: An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display that caused the event.
522
+
523
+ Fired when a display changes position.
517
524
 
518
525
 
519
526
  ### sdl.video.displays
@@ -530,8 +537,19 @@ Check [`sdl.video.displays`](#sdlvideodisplays) to get the new list of displays.
530
537
  * `horizontal: <number>` The horizontal density.
531
538
  * `vertical: <number>` The vertical density.
532
539
  * `diagonal: <number>` The diagonal density.
540
+ * `orientation: <string>|<null>` The orientation of the display.
533
541
 
534
542
  A list of all detected displays.
543
+
544
+ Possible values for `orientation` are `null` if it is unknown, or one of:
545
+
546
+ | Value | Corresponding `SDL_DisplayOrientation` |
547
+ | --- | --- |
548
+ | `'landscape'` | `SDL_ORIENTATION_LANDSCAPE` |
549
+ | `'landscapeFlipped'` | `SDL_ORIENTATION_LANDSCAPE_FLIPPED` |
550
+ | `'portrait'` | `SDL_ORIENTATION_PORTRAIT` |
551
+ | `'portraitFlipped'` | `SDL_ORIENTATION_PORTRAIT_FLIPPED` |
552
+
535
553
  Sample output for two side-to-side monitors is below.
536
554
  Notice how the geometries don't overlap:
537
555
 
@@ -1585,10 +1603,10 @@ When this event is emitted, all instances that were opened from the removed devi
1585
1603
 
1586
1604
  * `<object>[]`
1587
1605
  * `id: <number>` The unique id for the device.
1588
- * `type: <JoystickType>` The type of the device.
1589
1606
  * `name: <string>` The name of the device.
1590
1607
  * `path: <string>` The implementation dependent path of the device.
1591
1608
  * `guid: <string>` The GUID of the device.
1609
+ * `type: <JoystickType>|<null>` The type of the device.
1592
1610
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1593
1611
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1594
1612
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1596,7 +1614,7 @@ When this event is emitted, all instances that were opened from the removed devi
1596
1614
 
1597
1615
  A list of all the detected joystick devices.
1598
1616
 
1599
- Possible values for `type` are:
1617
+ Possible values for `type` are `null` if it is unknown, or one of:
1600
1618
 
1601
1619
  | Value | Corresponding `SDL_JoystickType` |
1602
1620
  | --- | --- |
@@ -1616,10 +1634,10 @@ Sample output:
1616
1634
  [
1617
1635
  {
1618
1636
  id: 0,
1619
- type: 'gamecontroller',
1620
1637
  name: 'DragonRise Inc. Generic USB Joystick',
1621
1638
  path: '/dev/input/event21',
1622
1639
  guid: '03000000790000000600000010010000',
1640
+ type: 'gamecontroller',
1623
1641
  vendor: 121,
1624
1642
  product: 6,
1625
1643
  version: 272,
@@ -1819,13 +1837,13 @@ Closes the instance.
1819
1837
  ## sdl.controller
1820
1838
 
1821
1839
  An SDL controller is an abstraction over [`joysticks`](#sdljoystick) based on the xbox360 controller: They have a dpad, two analog sticks, 4 buttons on the right (called A, B, X, Y), shoulder buttons (two of which might be axes) and 3 buttons in the middle ("Start", "Back" and usually some kind of logo-button called "Guide").
1822
- The SDL controller abstraction uses the naming-conventions of xbox360/XInput for all supported devices (for example devices that have a similar layout, like the Playstation DualShock Controller, but different button names), so you'll know that for example `controllerInstance.axes.leftStickX` is always the x-axis of the left analog stick, or `controllerInstance.buttons.b` is always the rightmost buttons of the 4 buttons on the right.
1840
+ The SDL controller abstraction uses the naming-conventions of xbox360/XInput for all supported devices (for example devices that have a similar layout, like the Playstation DualShock Controller, but different button names), so you'll know that for example `controllerInstance.axes.leftStickX` is always the x-axis of the left analog stick, or `controllerInstance.buttons.b` is always the rightmost button of the 4 buttons on the right.
1823
1841
  This makes it easy to provide consistent input bindings, like "press B to jump, move around with the left analog stick".
1824
1842
  With a pure joystick instance it's impossible to know which axis or button corresponds to which physical axis/button on the device.
1825
1843
 
1826
1844
  Because controllers are an abstraction over joysticks, they operate on the same set of devices (if a joystick device and a controller device have the same id, then they refer to the same underlying physical device).
1827
1845
  For a joystick device to also be available as a controller device it needs a "mapping".
1828
- A mapping is a string that consists of the device's name, it's GUID, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name.
1846
+ A mapping is a string that consists of the device's GUID, it's name, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name.
1829
1847
  See the sample output [`here`](#sdlcontrollerdevices) for an example.
1830
1848
  SDL has pretty good default controller mappings, but if you need more, there's a community sourced database available on https://github.com/gabomdq/SDL_GameControllerDB.
1831
1849
  You can add them via:
@@ -1834,7 +1852,7 @@ You can add them via:
1834
1852
  const url = 'https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt'
1835
1853
  const result = await fetch(url)
1836
1854
  const text = await result.text()
1837
- const mappings = text.split('\n')
1855
+ const mappings = text.split('\n').filter((line) => line && !line.startsWith('#'))
1838
1856
  sdl.controller.addMappings(mappings)
1839
1857
  ```
1840
1858
 
@@ -1871,6 +1889,7 @@ This can cause already opened controller instances to be [remapped](#event-remap
1871
1889
  * `name: <string>` The name of the device.
1872
1890
  * `path: <string>` The implementation dependent path of the device.
1873
1891
  * `guid: <string>` The GUID of the device.
1892
+ * `type: <string>|null` The type of the device.
1874
1893
  * `vendor: <number>|<null>` The USB vendor ID of the device. Will be `null` if it can't be determined.
1875
1894
  * `product: <number>|<null>` The USB product ID of the device. Will be `null` if it can't be determined.
1876
1895
  * `version: <number>|<null>` The USB product version of the device. Will be `null` if it can't be determined.
@@ -1878,6 +1897,25 @@ This can cause already opened controller instances to be [remapped](#event-remap
1878
1897
  * `mapping: <string>` The axis and button mapping for this device.
1879
1898
 
1880
1899
  A list of all the detected controller devices.
1900
+
1901
+ Possible values for `type` are `null` if it is unknown, or one of:
1902
+
1903
+ | Value | Corresponding `SDL_GameControllerType` |
1904
+ | --- | --- |
1905
+ | `'xbox360'` | SDL_CONTROLLER_TYPE_XBOX360 |
1906
+ | `'xboxOne'` | SDL_CONTROLLER_TYPE_XBOXONE |
1907
+ | `'ps3'` | SDL_CONTROLLER_TYPE_PS3 |
1908
+ | `'ps4'` | SDL_CONTROLLER_TYPE_PS4 |
1909
+ | `'nintendoSwitchPro'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO |
1910
+ | `'virtual'` | SDL_CONTROLLER_TYPE_VIRTUAL |
1911
+ | `'ps5'` | SDL_CONTROLLER_TYPE_PS5 |
1912
+ | `'amazonLuna'` | SDL_CONTROLLER_TYPE_AMAZON_LUNA |
1913
+ | `'googleStadia'` | SDL_CONTROLLER_TYPE_GOOGLE_STADIA |
1914
+ | `'nvidiaShield'` | SDL_CONTROLLER_TYPE_NVIDIA_SHIELD |
1915
+ | `'nintendoSwitchJoyconLeft'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT |
1916
+ | `'nintendoSwitchJoyconRight'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT |
1917
+ | `'nintendoSwitchJoyconPair'` | SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR |
1918
+
1881
1919
  Sample output:
1882
1920
 
1883
1921
  ```js
@@ -1887,6 +1925,7 @@ Sample output:
1887
1925
  name: 'DragonRise Inc. Generic USB Joystick',
1888
1926
  path: '/dev/input/event21',
1889
1927
  guid: '03000000790000000600000010010000',
1928
+ type: null,
1890
1929
  vendor: 121,
1891
1930
  product: 6,
1892
1931
  version: 272,
package/binding.gyp CHANGED
@@ -24,6 +24,7 @@
24
24
  'NAPI_VERSION=<(napi_build_version)',
25
25
  'NODE_ADDON_API_DISABLE_DEPRECATED',
26
26
  ],
27
+ 'cflags': [ '-Werror', '-Wall', '-Wextra' ],
27
28
  'conditions': [
28
29
  ['OS == "linux"', {
29
30
  'cflags': [ '-D_REENTRANT' ],
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.5",
2
+ "version": "0.11.7",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -19,9 +19,8 @@ class AudioInstance extends EventsViaPoll {
19
19
  buffered = 4096,
20
20
  } = options
21
21
 
22
- if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
23
- if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
24
- if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
22
+ if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("device.name must be a string"), { name }) }
23
+ // We already tested device.type in sdl.audio.openDevice()
25
24
  if (!Number.isInteger(channels)) { throw Object.assign(new Error("channels must be an integer"), { channels }) }
26
25
  if (![ 1, 2, 4, 6 ].includes(channels)) { throw Object.assign(new Error("invalid channels"), { channels }) }
27
26
  if (!Number.isInteger(frequency)) { throw Object.assign(new Error("frequency must be an integer"), { frequency }) }
@@ -1,10 +1,4 @@
1
1
 
2
- const make = (device) => {
3
- const { isRecorder } = device
4
- delete device.isRecorder
5
- device.type = isRecorder ? 'recording' : 'playback'
6
- }
7
-
8
2
  const compare = (a, b) => {
9
3
  const { name: aName } = a
10
4
  const { name: bName } = b
@@ -12,7 +6,4 @@ const compare = (a, b) => {
12
6
  return a.name < b.name ? -1 : 1
13
7
  }
14
8
 
15
- module.exports = {
16
- make,
17
- compare,
18
- }
9
+ module.exports = { compare }
@@ -1,19 +1,13 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
- const { make: makeAudioDevice } = require('./device')
4
3
  const { EventsViaPoll } = require('../events/events-via-poll')
5
4
  const { AudioPlaybackInstance } = require('./audio-playback-instance')
6
5
  const { AudioRecordingInstance } = require('./audio-recording-instance')
7
6
  const { AudioFormatHelpers } = require('./format-helpers')
8
7
 
8
+
9
9
  Globals.audioDevices.playback = Bindings.audio_getDevices(false)
10
- for (const audioDevice of Globals.audioDevices.playback) {
11
- makeAudioDevice(audioDevice)
12
- }
13
10
  Globals.audioDevices.recording = Bindings.audio_getDevices(true)
14
- for (const audioDevice of Globals.audioDevices.recording) {
15
- makeAudioDevice(audioDevice)
16
- }
17
11
 
18
12
 
19
13
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
@@ -30,6 +24,10 @@ const audio = new class extends EventsViaPoll {
30
24
  }
31
25
 
32
26
  openDevice (device, options = {}) {
27
+ if (typeof device !== 'object') { throw Object.assign(new Error("device must be an object"), { device }) }
28
+ if (typeof device.type !== 'string') { throw Object.assign(new Error("device.type must be a string"), { device }) }
29
+ if (![ 'playback', 'recording' ].includes(device.type)) { throw Object.assign(new Error("invalid device.type"), { device }) }
30
+
33
31
  return device.type === 'recording'
34
32
  ? new AudioRecordingInstance(device, options)
35
33
  : new AudioPlaybackInstance(device, options)
@@ -14,6 +14,8 @@ class ControllerInstance extends EventsViaPoll {
14
14
  constructor (device) {
15
15
  super(validEvents)
16
16
 
17
+ if (!Globals.controllerDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
18
+
17
19
  const result = Bindings.controller_open(device._index)
18
20
 
19
21
  this._firmwareVersion = result.firmwareVersion
@@ -1,10 +1,21 @@
1
1
 
2
2
  const make = (device) => {
3
- delete device.type
4
- delete device.isController
3
+ const {
4
+ isController,
5
+ controllerMapping,
6
+ controllerName,
7
+ controllerType,
8
+ ...rest
9
+ } = device
10
+ return {
11
+ ...rest,
12
+ mapping: controllerMapping,
13
+ name: controllerName,
14
+ type: controllerType,
15
+ }
5
16
  }
6
17
 
7
- const compare = (a, b) => a.id - b.id
18
+ const compare = (a, b) => a._index - b._index
8
19
 
9
20
  const filter = (device) => device.isController
10
21
 
@@ -1,18 +1,8 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
- const {
4
- make: makeControllerDevice,
5
- filter: filterControllerDevice,
6
- } = require('./device')
7
3
  const { EventsViaPoll } = require('../events/events-via-poll')
8
4
  const { ControllerInstance } = require('./controller-instance')
9
-
10
- Globals.controllerDevices = Bindings.joystick_getDevices()
11
- .filter(filterControllerDevice)
12
- for (const controllerDevice of Globals.controllerDevices) {
13
- makeControllerDevice(controllerDevice)
14
- }
15
-
5
+ const { reconcileJoystickAndControllerDevices } = require('../events/reconcile-joystick-and-controller-devices')
16
6
 
17
7
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
18
8
 
@@ -33,6 +23,10 @@ const controller = new class extends EventsViaPoll {
33
23
  }
34
24
 
35
25
  Bindings.controller_addMappings(mappings)
26
+ Globals.events.poll()
27
+
28
+ const devices = Bindings.joystick_getDevices()
29
+ reconcileJoystickAndControllerDevices(devices)
36
30
  }
37
31
  }()
38
32
 
@@ -1,74 +1,11 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const { video: videoModule } = require('../video')
4
+ const { reconcileDisplays } = require('./reconcile-displays')
4
5
  const { mapping } = require('../keyboard/key-mapping')
5
- const { joystick: joystickModule } = require('../joystick')
6
- const { controller: controllerModule } = require('../controller')
7
- const { audio: audioModule } = require('../audio')
6
+ const { reconcileAudioDevices } = require('./reconcile-audio-devices')
7
+ const { reconcileJoystickAndControllerDevices } = require('./reconcile-joystick-and-controller-devices')
8
8
  const { clipboard: clipboardModule } = require('../clipboard')
9
- const {
10
- make: makeJoystickDevice,
11
- compare: compareJoystickDevice,
12
- } = require('../joystick/device')
13
- const {
14
- make: makeControllerDevice,
15
- compare: compareControllerDevice,
16
- filter: filterControllerDevice,
17
- } = require('../controller/device')
18
- const {
19
- make: makeAudioDevice,
20
- compare: compareAudioDevice,
21
- } = require('../audio/device')
22
-
23
-
24
- const reconcileDevices = (emitter, mainList, currList, compare) => {
25
- currList.sort(compare)
26
-
27
- let mainIndex = 0
28
- let currIndex = 0
29
- let mainDevice = mainList[mainIndex]
30
- let currDevice = currList[currIndex]
31
- while (mainIndex < mainList.length && currIndex < currList.length) {
32
- const cmp = compare(mainDevice, currDevice)
33
- if (cmp === 0) {
34
- mainDevice = mainList[++mainIndex]
35
- currDevice = currList[++currIndex]
36
- }
37
- else if (cmp < 0) {
38
- mainList.splice(mainIndex, 1)
39
- const type = 'deviceRemove'
40
- const event = { type, device: mainDevice }
41
- emitter.emit(type, event)
42
- mainDevice = mainList[mainIndex]
43
- }
44
- else {
45
- mainList.splice(mainIndex, 0, currDevice)
46
- mainDevice = mainList[++mainIndex]
47
- const type = 'deviceAdd'
48
- const event = { type, device: currDevice }
49
- emitter.emit(type, event)
50
- currDevice = currList[++currIndex]
51
- }
52
- }
53
-
54
- if (mainIndex < mainList.length) {
55
- while (mainIndex < mainList.length) {
56
- [ mainDevice ] = mainList.splice(mainIndex, 1)
57
- const type = 'deviceRemove'
58
- const event = { type, device: mainDevice }
59
- emitter.emit(type, event)
60
- }
61
- }
62
- else {
63
- while (currIndex < currList.length) {
64
- mainList.push(currDevice)
65
- const type = 'deviceAdd'
66
- const event = { type, device: currDevice }
67
- emitter.emit(type, event)
68
- currDevice = currList[++currIndex]
69
- }
70
- }
71
- }
72
9
 
73
10
 
74
11
  const handleEvent = (event) => {
@@ -78,7 +15,7 @@ const handleEvent = (event) => {
78
15
 
79
16
  switch (family) {
80
17
  case 'app': {
81
- if (event.type !== 'quit') { return }
18
+ if (type !== 'quit') { return }
82
19
 
83
20
  for (const window of Globals.windows.all.values()) {
84
21
  window.destroyGently()
@@ -90,15 +27,40 @@ const handleEvent = (event) => {
90
27
  const { displayIndex } = event
91
28
  delete event.displayIndex
92
29
 
93
- const oldDisplays = Globals.displays
94
- Globals.displays = Bindings.video_getDisplays()
95
- const newDisplays = Globals.displays
30
+ switch (type) {
31
+ case 'displayAdd':
32
+ case 'displayRemove':
33
+ {
34
+ const { displays } = event
35
+ delete event.displays
36
+
37
+ reconcileDisplays(displays)
38
+ return
39
+ }
40
+
41
+ case 'displayOrient': {
42
+ const display = Globals.displays[displayIndex]
43
+ display.orientation = event.orientation
44
+ } break
45
+
46
+ case 'displayMove': {
47
+ const { geometryX, geometryY, usableX, usableY } = event
48
+ delete event.geometryX
49
+ delete event.geometryY
50
+ delete event.usableX
51
+ delete event.usableY
52
+
53
+ const display = Globals.displays[displayIndex]
54
+ display.geometry.x = geometryX
55
+ display.geometry.y = geometryY
56
+ display.usable.x = usableX
57
+ display.usable.y = usableY
58
+ } break
96
59
 
97
- event.display = event.type === 'displayRemove'
98
- ? oldDisplays[displayIndex]
99
- : newDisplays[displayIndex]
60
+ // No default
61
+ }
100
62
 
101
- videoModule.emit(event.type, event)
63
+ videoModule.emit(type, event)
102
64
  } break
103
65
 
104
66
  case 'window': {
@@ -176,7 +138,7 @@ const handleEvent = (event) => {
176
138
  default: return
177
139
  }
178
140
 
179
- window.emit(event.type, event)
141
+ window.emit(type, event)
180
142
  } break
181
143
 
182
144
  case 'keyboard': {
@@ -189,7 +151,7 @@ const handleEvent = (event) => {
189
151
  const { key } = event
190
152
  event.key = mapping[key] ?? (key?.length === 1 ? key : null)
191
153
 
192
- window.emit(event.type, event)
154
+ window.emit(type, event)
193
155
  } break
194
156
 
195
157
  case 'mouse':
@@ -201,7 +163,7 @@ const handleEvent = (event) => {
201
163
  const window = Globals.windows.all.get(windowId)
202
164
  if (!window) { return }
203
165
 
204
- window.emit(event.type, event)
166
+ window.emit(type, event)
205
167
  } break
206
168
 
207
169
  case 'joystickDevice': {
@@ -226,33 +188,10 @@ const handleEvent = (event) => {
226
188
  }
227
189
  }
228
190
 
229
- const { devices: joystickDevices } = event
191
+ const { devices } = event
230
192
  delete event.devices
231
193
 
232
- const controllerDevices = JSON.parse(JSON.stringify(joystickDevices
233
- .filter(filterControllerDevice)))
234
-
235
- for (const joystickDevice of joystickDevices) {
236
- makeJoystickDevice(joystickDevice)
237
- }
238
-
239
- for (const controllerDevice of controllerDevices) {
240
- makeControllerDevice(controllerDevice)
241
- }
242
-
243
- reconcileDevices(
244
- joystickModule,
245
- Globals.joystickDevices,
246
- joystickDevices,
247
- compareJoystickDevice,
248
- )
249
-
250
- reconcileDevices(
251
- controllerModule,
252
- Globals.controllerDevices,
253
- controllerDevices,
254
- compareControllerDevice,
255
- )
194
+ reconcileJoystickAndControllerDevices(devices)
256
195
  } break
257
196
 
258
197
  case 'joystick': {
@@ -266,7 +205,7 @@ const handleEvent = (event) => {
266
205
  case 'axisMotion': {
267
206
  for (const joystickInstance of collection) {
268
207
  joystickInstance._axes[event.axis] = event.value
269
- joystickInstance.emit(event.type, event)
208
+ joystickInstance.emit(type, event)
270
209
  }
271
210
  } break
272
211
 
@@ -275,27 +214,27 @@ const handleEvent = (event) => {
275
214
  const ball = joystickInstance._balls[event.ball]
276
215
  ball.x = event.x
277
216
  ball.y = event.y
278
- joystickInstance.emit(event.type, event)
217
+ joystickInstance.emit(type, event)
279
218
  }
280
219
  } break
281
220
 
282
221
  case 'buttonDown': {
283
222
  for (const joystickInstance of collection) {
284
223
  joystickInstance._buttons[event.button] = true
285
- joystickInstance.emit(event.type, event)
224
+ joystickInstance.emit(type, event)
286
225
  }
287
226
  } break
288
227
  case 'buttonUp': {
289
228
  for (const joystickInstance of collection) {
290
229
  joystickInstance._buttons[event.button] = false
291
- joystickInstance.emit(event.type, event)
230
+ joystickInstance.emit(type, event)
292
231
  }
293
232
  } break
294
233
 
295
234
  case 'hatMotion': {
296
235
  for (const joystickInstance of collection) {
297
236
  joystickInstance._hats[event.hat] = event.value
298
- joystickInstance.emit(event.type, event)
237
+ joystickInstance.emit(type, event)
299
238
  }
300
239
  } break
301
240
 
@@ -314,21 +253,21 @@ const handleEvent = (event) => {
314
253
  case 'axisMotion': {
315
254
  for (const controllerInstance of collection.values()) {
316
255
  controllerInstance._axes[event.axis] = event.value
317
- controllerInstance.emit(event.type, event)
256
+ controllerInstance.emit(type, event)
318
257
  }
319
258
  } break
320
259
 
321
260
  case 'buttonDown': {
322
261
  for (const controllerInstance of collection.values()) {
323
262
  controllerInstance._buttons[event.button] = true
324
- controllerInstance.emit(event.type, event)
263
+ controllerInstance.emit(type, event)
325
264
  }
326
265
  } break
327
266
 
328
267
  case 'buttonUp': {
329
268
  for (const controllerInstance of collection.values()) {
330
269
  controllerInstance._buttons[event.button] = false
331
- controllerInstance.emit(event.type, event)
270
+ controllerInstance.emit(type, event)
332
271
  }
333
272
  } break
334
273
 
@@ -340,7 +279,7 @@ const handleEvent = (event) => {
340
279
  for (const controllerInstance of collection.values()) {
341
280
  Object.assign(controllerInstance._axes, axes)
342
281
  Object.assign(controllerInstance._buttons, buttons)
343
- controllerInstance.emit(event.type, event)
282
+ controllerInstance.emit(type, event)
344
283
  }
345
284
  } break
346
285
 
@@ -358,7 +297,7 @@ const handleEvent = (event) => {
358
297
  switch (type) {
359
298
  case 'update': {
360
299
  for (const sensorInstance of collection.values()) {
361
- sensorInstance.emit(event.type, event)
300
+ sensorInstance.emit(type, event)
362
301
  }
363
302
  } break
364
303
 
@@ -367,8 +306,8 @@ const handleEvent = (event) => {
367
306
  } break
368
307
 
369
308
  case 'audioDevice': {
370
- const { isRecorder } = event
371
- delete event.isRecorder
309
+ const { audioDeviceType } = event
310
+ delete event.audioDeviceType
372
311
 
373
312
  if (type === 'deviceRemove') {
374
313
  const { audioId } = event
@@ -378,23 +317,14 @@ const handleEvent = (event) => {
378
317
  if (audioInstance) { audioInstance.close() }
379
318
  }
380
319
 
381
- const { devices: audioDevices } = event
320
+ const { devices } = event
382
321
  delete event.devices
383
322
 
384
- for (const audioDevice of audioDevices) {
385
- makeAudioDevice(audioDevice)
386
- }
387
-
388
- reconcileDevices(
389
- audioModule,
390
- Globals.audioDevices[isRecorder ? 'recording' : 'playback'],
391
- audioDevices,
392
- compareAudioDevice,
393
- )
323
+ reconcileAudioDevices(devices, audioDeviceType)
394
324
  } break
395
325
 
396
326
  case 'clipboard': {
397
- clipboardModule.emit(event.type, event)
327
+ clipboardModule.emit(type, event)
398
328
  } break
399
329
 
400
330
  // No default
@@ -0,0 +1,15 @@
1
+ const Globals = require('../globals')
2
+ const { reconcileDevices } = require('./reconcile')
3
+
4
+ const { compare: compareAudioDevice } = require('../audio/device')
5
+
6
+ const reconcileAudioDevices = (audioDevices, audioDeviceType) => {
7
+ reconcileDevices(
8
+ require('../audio').audio,
9
+ Globals.audioDevices[audioDeviceType],
10
+ audioDevices,
11
+ compareAudioDevice,
12
+ )
13
+ }
14
+
15
+ module.exports = { reconcileAudioDevices }
@@ -0,0 +1,16 @@
1
+ const Globals = require('../globals')
2
+ const { reconcileDevices } = require('./reconcile')
3
+
4
+ const { compare: compareDisplays } = require('../video/display')
5
+
6
+ const reconcileDisplays = (displays) => {
7
+ reconcileDevices(
8
+ require('../video').video,
9
+ Globals.displays,
10
+ displays,
11
+ compareDisplays,
12
+ 'display',
13
+ )
14
+ }
15
+
16
+ module.exports = { reconcileDisplays }
@@ -0,0 +1,37 @@
1
+ const Globals = require('../globals')
2
+ const { reconcileDevices } = require('./reconcile')
3
+
4
+ const {
5
+ make: makeJoystickDevice,
6
+ compare: compareJoystickDevice,
7
+ } = require('../joystick/device')
8
+ const {
9
+ make: makeControllerDevice,
10
+ compare: compareControllerDevice,
11
+ filter: filterControllerDevice,
12
+ } = require('../controller/device')
13
+
14
+ const reconcileJoystickAndControllerDevices = (devices) => {
15
+ const joystickDevices = devices
16
+ .map(makeJoystickDevice)
17
+
18
+ const controllerDevices = devices
19
+ .filter(filterControllerDevice)
20
+ .map(makeControllerDevice)
21
+
22
+ reconcileDevices(
23
+ require('../joystick').joystick,
24
+ Globals.joystickDevices,
25
+ joystickDevices,
26
+ compareJoystickDevice,
27
+ )
28
+
29
+ reconcileDevices(
30
+ require('../controller').controller,
31
+ Globals.controllerDevices,
32
+ controllerDevices,
33
+ compareControllerDevice,
34
+ )
35
+ }
36
+
37
+ module.exports = { reconcileJoystickAndControllerDevices }