@kmamal/sdl 0.11.13 → 1.0.0-3

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 (86) hide show
  1. package/CHANGELOG.md +113 -1
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +40 -34
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +29 -34
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +280 -174
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +211 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +41 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +81 -42
  31. package/src/javascript/joystick/shared.js +90 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +22 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +117 -0
  41. package/src/javascript/video/index.js +44 -5
  42. package/src/javascript/video/window.js +289 -133
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +409 -321
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +45 -19
  73. package/src/types/index.d.ts +366 -273
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
@@ -1,85 +0,0 @@
1
-
2
- const _consume = async (stream) => {
3
- const chunks = []
4
- for await (const chunk of stream) {
5
- chunks.push(chunk)
6
- }
7
- return Buffer.concat(chunks)
8
- }
9
-
10
- const fetch = async (_url, options = {}) => {
11
- let url = _url
12
- let {
13
- maxRedirect = 5,
14
- body = null,
15
- } = options
16
-
17
- const { protocol } = new URL(url)
18
- const lib = require(protocol.slice(0, -1))
19
-
20
- const evalPromise = (resolve, reject) => {
21
- const request = lib.request(url, options)
22
- .on('error', reject)
23
- .on('response', resolve)
24
-
25
- if (!body) {
26
- request.end()
27
- return
28
- }
29
-
30
- if (body?.length) {
31
- if (!Buffer.isBuffer(body)) { body = Buffer.from(body) }
32
- body = Promise.resolve(body)
33
- }
34
- else {
35
- body = _consume(body)
36
- }
37
-
38
- body.then((buffer) => {
39
- request.setHeader('Content-Length', buffer.length)
40
- request.end(buffer)
41
- })
42
- }
43
-
44
- for (;;) {
45
- const response = await new Promise(evalPromise)
46
-
47
- const { statusCode } = response
48
-
49
- if (300 <= statusCode && statusCode < 400) {
50
- if (maxRedirect-- > 0) {
51
- url = response.headers.location
52
- continue
53
- }
54
- }
55
-
56
- if (!(200 <= statusCode && statusCode < 300)) {
57
- let responseBody
58
- try { responseBody = (await _consume(response)).toString() }
59
- catch (_) {}
60
- try { responseBody = JSON.parse(responseBody) }
61
- catch (_) {}
62
- throw Object.assign(new Error(`bad status code ${statusCode}`), {
63
- statusCode,
64
- responseBody,
65
- })
66
- }
67
-
68
- return {
69
- stream () { return response },
70
- async buffer () {
71
- return await _consume(response)
72
- },
73
- async text () {
74
- const buffer = await this.buffer()
75
- return buffer.toString()
76
- },
77
- async json () {
78
- const text = await this.text()
79
- return JSON.parse(text)
80
- },
81
- }
82
- }
83
- }
84
-
85
- module.exports = { fetch }
@@ -1,23 +0,0 @@
1
- const Bindings = require('../bindings')
2
- const { AudioInstance } = require('./audio-instance')
3
- const { resetTimeout } = require('./prevent-exit')
4
-
5
- class AudioPlaybackInstance extends AudioInstance {
6
- enqueue (buffer, numBytes = buffer.length) {
7
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
8
-
9
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
10
- if (!Number.isInteger(numBytes)) { throw Object.assign(new Error("numBytes must be an integer"), { numBytes }) }
11
- if (numBytes <= 0) { throw Object.assign(new Error("invalid numBytes"), { numBytes }) }
12
- if (buffer.length < numBytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, numBytes }) }
13
-
14
- Bindings.audio_enqueue(this._id, buffer, numBytes)
15
- }
16
-
17
- clearQueue () {
18
- super.clearQueue()
19
- resetTimeout()
20
- }
21
- }
22
-
23
- module.exports = { AudioPlaybackInstance }
@@ -1,17 +0,0 @@
1
- const Bindings = require('../bindings')
2
- const { AudioInstance } = require('./audio-instance')
3
-
4
- class AudioRecordingInstance extends AudioInstance {
5
- dequeue (buffer, numBytes = buffer.length) {
6
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
7
-
8
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
9
- if (!Number.isInteger(numBytes)) { throw Object.assign(new Error("numBytes must be an integer"), { numBytes }) }
10
- if (numBytes <= 0) { throw Object.assign(new Error("invalid numBytes"), { numBytes }) }
11
- if (buffer.length < numBytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, numBytes }) }
12
-
13
- return Bindings.audio_dequeue(this._id, buffer, numBytes)
14
- }
15
- }
16
-
17
- module.exports = { AudioRecordingInstance }
@@ -1,160 +0,0 @@
1
- const Globals = require('../globals')
2
- const Bindings = require('../bindings')
3
- const { EventsViaPoll } = require('../events/events-via-poll')
4
-
5
- const validEvents = [
6
- 'axisMotion',
7
- 'buttonDown',
8
- 'buttonUp',
9
- 'powerUpdate',
10
- 'steamHandleUpdate',
11
- 'remap',
12
- 'close',
13
- ]
14
-
15
- class ControllerInstance extends EventsViaPoll {
16
- constructor (device) {
17
- super(validEvents)
18
-
19
- if (!Globals.controllerDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }
20
-
21
- const result = Bindings.controller_open(device._index)
22
-
23
- this._firmwareVersion = result.firmwareVersion
24
- this._serialNumber = result.serialNumber
25
- this._hasLed = result.hasLed
26
- this._hasRumble = result.hasRumble
27
- this._hasRumbleTriggers = result.hasRumbleTriggers
28
- this._steamHandle = result.steamHandle
29
- this._power = result.power
30
- this._axes = result.axes
31
- this._buttons = result.buttons
32
-
33
- this._device = device
34
-
35
- this._rumbleTimeout = null
36
- this._rumbleTriggersTimeout = null
37
- this._closed = false
38
-
39
- Globals.controllerInstances.all.add(this)
40
- let collection = Globals.controllerInstances.byId.get(this._device.id)
41
- if (!collection) {
42
- collection = new Set()
43
- Globals.controllerInstances.byId.set(this._device.id, collection)
44
- }
45
- collection.add(this)
46
- }
47
-
48
- get device () { return this._device }
49
- get firmwareVersion () { return this._firmwareVersion }
50
- get serialNumber () { return this._serialNumber }
51
- get steamHandle () { return this._steamHandle }
52
-
53
- get axes () {
54
- Globals.events.poll()
55
- return this._axes
56
- }
57
-
58
- get buttons () {
59
- Globals.events.poll()
60
- return this._buttons
61
- }
62
-
63
- get power () { return this._power }
64
-
65
- setPlayer (player) {
66
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
67
-
68
- if (!Number.isInteger(player)) { throw Object.assign(new Error("player must be an integer"), { player }) }
69
- if (player < 0) { throw Object.assign(new Error("invalid player"), { player }) }
70
-
71
- Bindings.joystick_setPlayer(this._device.id, player)
72
- }
73
-
74
- resetPlayer () {
75
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
76
-
77
- Bindings.joystick_setPlayer(this._device.id, -1)
78
- }
79
-
80
- get hasLed () { return this._hasLed }
81
- setLed (red, green, blue) {
82
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
83
-
84
- if (!Number.isFinite(red)) { throw Object.assign(new Error("red must be a number"), { red }) }
85
- if (red < 0 || red > 1) { throw Object.assign(new Error("red must be between 0 and 1"), { red }) }
86
- if (!Number.isFinite(green)) { throw Object.assign(new Error("green must be a number"), { green }) }
87
- if (green < 0 || green > 1) { throw Object.assign(new Error("green must be between 0 and 1"), { green }) }
88
- if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
89
- if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
90
-
91
- Bindings.joystick_setLed(this._device.id, red, green, blue)
92
- }
93
-
94
- get hasRumble () { return this._hasRumble }
95
- rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = 1e3) {
96
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
97
-
98
- if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
99
- if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
100
- if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
101
- if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
102
- if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
103
- if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
104
-
105
- // Globals.events.poll() // Errors if it hasn't been called at least once
106
- Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
107
- clearTimeout(this._rumbleTimeout)
108
- this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
109
- }
110
-
111
- stopRumble () {
112
- clearTimeout(this._rumbleTimeout)
113
- this._rumbleTimeout = null
114
- Bindings.joystick_rumble(this._device.id, 0, 0, 0)
115
- Globals.events.poll()
116
- }
117
-
118
- get hasRumbleTriggers () { return this._hasRumbleTriggers }
119
- rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
120
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
121
-
122
- if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
123
- if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
124
- if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
125
- if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
126
- if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
127
- if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
128
-
129
- // Globals.events.poll() // Errors if it hasn't been called at least once
130
- Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
131
- clearTimeout(this._rumbleTriggersTimeout)
132
- this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
133
- }
134
-
135
- stopRumbleTriggers () {
136
- clearTimeout(this._rumbleTriggersTimeout)
137
- this._rumbleTriggersTimeout = null
138
- Bindings.joystick_rumbleTriggers(this._device.id, 0, 0, 0)
139
- Globals.events.poll()
140
- }
141
-
142
- get closed () { return this._closed }
143
- close () {
144
- if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
145
-
146
- this.emit('close')
147
- this.removeAllListeners()
148
- this._closed = true
149
-
150
- Globals.controllerInstances.all.delete(this)
151
- const collection = Globals.controllerInstances.byId.get(this._device.id)
152
- collection.delete(this)
153
- if (collection.size === 0) {
154
- Bindings.controller_close(this._device.id)
155
- Globals.controllerInstances.byId.delete(this._device.id)
156
- }
157
- }
158
- }
159
-
160
- module.exports = { ControllerInstance }
@@ -1,26 +0,0 @@
1
-
2
- const make = (device) => {
3
- const {
4
- isController,
5
- controllerMapping,
6
- controllerName,
7
- controllerType,
8
- ...rest
9
- } = device
10
- return {
11
- ...rest,
12
- mapping: controllerMapping,
13
- name: controllerName ?? rest.name,
14
- type: controllerType,
15
- }
16
- }
17
-
18
- const compare = (a, b) => a._index - b._index
19
-
20
- const filter = (device) => device.isController
21
-
22
- module.exports = {
23
- make,
24
- compare,
25
- filter,
26
- }
@@ -1,33 +0,0 @@
1
- const Globals = require('../globals')
2
- const Bindings = require('../bindings')
3
- const { EventsViaPoll } = require('../events/events-via-poll')
4
- const { ControllerInstance } = require('./controller-instance')
5
-
6
- const validEvents = [ 'deviceAdd', 'deviceRemove' ]
7
-
8
- const controller = new class extends EventsViaPoll {
9
- constructor () { super(validEvents) }
10
-
11
- get devices () {
12
- Globals.events.poll()
13
- return Globals.controllerDevices
14
- }
15
-
16
- openDevice (device) { return new ControllerInstance(device) }
17
-
18
- addMappings (mappings) {
19
- if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
20
- for (const mapping of mappings) {
21
- if (typeof mapping !== 'string') { throw Object.assign(new Error("mapping must be a string"), { mappings }) }
22
- }
23
-
24
- Bindings.controller_addMappings(mappings)
25
- Globals.events.poll()
26
-
27
- const devices = Bindings.joystick_getDevices()
28
- require('../events/reconcile-joystick-and-controller-devices')
29
- .reconcileJoystickAndControllerDevices(devices)
30
- }
31
- }()
32
-
33
- module.exports = { controller }
@@ -1,16 +0,0 @@
1
- const Globals = require('../globals')
2
- const { reconcileDevices } = require('./reconcile')
3
-
4
- const { audio: audioModule } = require('../audio')
5
- const { compare: compareAudioDevice } = require('../audio/device')
6
-
7
- const reconcileAudioDevices = (audioDevices, audioDeviceType) => {
8
- reconcileDevices(
9
- audioModule,
10
- Globals.audioDevices[audioDeviceType],
11
- audioDevices,
12
- compareAudioDevice,
13
- )
14
- }
15
-
16
- module.exports = { reconcileAudioDevices }
@@ -1,17 +0,0 @@
1
- const Globals = require('../globals')
2
- const { reconcileDevices } = require('./reconcile')
3
-
4
- const { video: videoModule } = require('../video')
5
- const { compare: compareDisplays } = require('../video/display')
6
-
7
- const reconcileDisplays = (displays) => {
8
- reconcileDevices(
9
- videoModule,
10
- Globals.displays,
11
- displays,
12
- compareDisplays,
13
- 'display',
14
- )
15
- }
16
-
17
- module.exports = { reconcileDisplays }
@@ -1,40 +0,0 @@
1
- const Globals = require('../globals')
2
- const { reconcileDevices } = require('./reconcile')
3
-
4
- const { joystick: joystickModule } = require('../joystick')
5
- const {
6
- make: makeJoystickDevice,
7
- compare: compareJoystickDevice,
8
- } = require('../joystick/device')
9
-
10
- const { controller: controllerModule } = require('../controller')
11
- const {
12
- make: makeControllerDevice,
13
- compare: compareControllerDevice,
14
- filter: filterControllerDevice,
15
- } = require('../controller/device')
16
-
17
- const reconcileJoystickAndControllerDevices = (devices) => {
18
- const joystickDevices = devices
19
- .map(makeJoystickDevice)
20
-
21
- const controllerDevices = devices
22
- .filter(filterControllerDevice)
23
- .map(makeControllerDevice)
24
-
25
- reconcileDevices(
26
- joystickModule,
27
- Globals.joystickDevices,
28
- joystickDevices,
29
- compareJoystickDevice,
30
- )
31
-
32
- reconcileDevices(
33
- controllerModule,
34
- Globals.controllerDevices,
35
- controllerDevices,
36
- compareControllerDevice,
37
- )
38
- }
39
-
40
- module.exports = { reconcileJoystickAndControllerDevices }
@@ -1,61 +0,0 @@
1
-
2
- const reconcileDevices = (
3
- emitter,
4
- mainList,
5
- currList,
6
- compare,
7
- prefix = 'device',
8
- ) => {
9
- const addEventType = `${prefix}Add`
10
- const removeEventType = `${prefix}Remove`
11
-
12
- currList.sort(compare)
13
-
14
- let mainIndex = 0
15
- let currIndex = 0
16
- let mainDevice = mainList[mainIndex]
17
- let currDevice = currList[currIndex]
18
- while (mainIndex < mainList.length && currIndex < currList.length) {
19
- const cmp = compare(mainDevice, currDevice)
20
- if (cmp === 0) {
21
- Object.assign(mainList[mainIndex], currList[currIndex])
22
- mainDevice = mainList[++mainIndex]
23
- currDevice = currList[++currIndex]
24
- }
25
- else if (cmp < 0) {
26
- mainList.splice(mainIndex, 1)
27
- const type = removeEventType
28
- const event = { type, device: mainDevice }
29
- emitter.emit(type, event)
30
- mainDevice = mainList[mainIndex]
31
- }
32
- else {
33
- mainList.splice(mainIndex, 0, currDevice)
34
- mainDevice = mainList[++mainIndex]
35
- const type = addEventType
36
- const event = { type, device: currDevice }
37
- emitter.emit(type, event)
38
- currDevice = currList[++currIndex]
39
- }
40
- }
41
-
42
- if (mainIndex < mainList.length) {
43
- while (mainIndex < mainList.length) {
44
- [ mainDevice ] = mainList.splice(mainIndex, 1)
45
- const type = removeEventType
46
- const event = { type, device: mainDevice }
47
- emitter.emit(type, event)
48
- }
49
- }
50
- else {
51
- while (currIndex < currList.length) {
52
- mainList.push(currDevice)
53
- const type = addEventType
54
- const event = { type, device: currDevice }
55
- emitter.emit(type, event)
56
- currDevice = currList[++currIndex]
57
- }
58
- }
59
- }
60
-
61
- module.exports = { reconcileDevices }
@@ -1,4 +0,0 @@
1
-
2
- const compare = (a, b) => a._index - b._index
3
-
4
- module.exports = { compare }
@@ -1,192 +0,0 @@
1
- #include "controller.h"
2
- #include "joystick.h"
3
- #include "global.h"
4
- #include <SDL.h>
5
- #include <map>
6
- #include <string>
7
- #include <sstream>
8
-
9
-
10
- std::map<SDL_GameControllerType, std::string> controller::types;
11
- std::map<SDL_GameControllerAxis, std::string> controller::axes;
12
- std::map<SDL_GameControllerButton, std::string> controller::buttons;
13
-
14
-
15
- double
16
- controller::mapAxis (SDL_GameController *controller, SDL_GameControllerAxis axis) {
17
- return mapAxisValue(controller, axis, SDL_GameControllerGetAxis(controller, axis));
18
- }
19
-
20
- double
21
- controller::mapAxisValue (SDL_GameController *controller, SDL_GameControllerAxis axis, int value) {
22
- SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForAxis(controller, axis);
23
- if (bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS) {
24
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
25
- return joystick::mapAxisValue(joystick, bind.value.axis, value);
26
- }
27
-
28
- double range = value < 0 ? -SDL_JOYSTICK_AXIS_MIN : SDL_JOYSTICK_AXIS_MAX;
29
- return value / range;
30
- }
31
-
32
- Napi::Value
33
- controller::getSteamHandle (Napi::Env &env, SDL_GameController *controller)
34
- {
35
- Uint64 _steam_handle = SDL_GameControllerGetSteamHandle(controller);
36
- return _steam_handle != 0
37
- ? Napi::Buffer<Uint64>::Copy(env, &_steam_handle, 1)
38
- : env.Null();
39
- }
40
-
41
- void
42
- controller::getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst)
43
- {
44
- Napi::Object axes = Napi::Object::New(env);
45
- axes.Set("leftStickX", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_LEFTX));
46
- axes.Set("leftStickY", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_LEFTY));
47
- axes.Set("rightStickX", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX));
48
- axes.Set("rightStickY", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY));
49
- axes.Set("leftTrigger", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT));
50
- axes.Set("rightTrigger", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT));
51
-
52
- const char *error;
53
-
54
- error = SDL_GetError();
55
- if (error != global::no_error) {
56
- std::ostringstream message;
57
- message << "SDL_GameControllerGetAxis() error: " << error;
58
- SDL_ClearError();
59
- throw Napi::Error::New(env, message.str());
60
- }
61
-
62
- Napi::Object buttons = Napi::Object::New(env);
63
- buttons.Set("dpadLeft", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
64
- buttons.Set("dpadRight", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
65
- buttons.Set("dpadUp", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP));
66
- buttons.Set("dpadDown", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
67
- buttons.Set("a", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A));
68
- buttons.Set("b", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_B));
69
- buttons.Set("x", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X));
70
- buttons.Set("y", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_Y));
71
- buttons.Set("guide", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_GUIDE));
72
- buttons.Set("back", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_BACK));
73
- buttons.Set("start", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_START));
74
- buttons.Set("leftStick", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSTICK));
75
- buttons.Set("rightStick", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK));
76
- buttons.Set("leftShoulder", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
77
- buttons.Set("rightShoulder", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
78
- buttons.Set("paddle1", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE1));
79
- buttons.Set("paddle2", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE2));
80
- buttons.Set("paddle3", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE3));
81
- buttons.Set("paddle4", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE4));
82
-
83
- error = SDL_GetError();
84
- if (error != global::no_error) {
85
- std::ostringstream message;
86
- message << "SDL_GameControllerGetButton() error: " << error;
87
- SDL_ClearError();
88
- throw Napi::Error::New(env, message.str());
89
- }
90
-
91
- dst.Set("axes", axes);
92
- dst.Set("buttons", buttons);
93
- }
94
-
95
-
96
- Napi::Value
97
- controller::addMappings (const Napi::CallbackInfo &info)
98
- {
99
- Napi::Env env = info.Env();
100
-
101
- Napi::Array mappings = info[0].As<Napi::Array>();
102
-
103
- for (int i = 0; i < (int) mappings.Length(); i++) {
104
- std::string mapping = mappings.Get(i).As<Napi::String>().Utf8Value();
105
- if (SDL_GameControllerAddMapping(mapping.c_str()) == -1) {
106
- std::ostringstream message;
107
- message << "SDL_GameControllerAddMapping(" << mapping << ") error: " << SDL_GetError();
108
- SDL_ClearError();
109
- throw Napi::Error::New(env, message.str());
110
- }
111
-
112
- // SDL_GameControllerAddMapping produces errors even though it succeeds
113
- const char *error = SDL_GetError();
114
- if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
115
- SDL_ClearError();
116
- }
117
-
118
- return env.Undefined();
119
- }
120
-
121
- Napi::Value
122
- controller::open (const Napi::CallbackInfo &info)
123
- {
124
- Napi::Env env = info.Env();
125
-
126
- int index = info[0].As<Napi::Number>().Int32Value();
127
-
128
- SDL_GameController *controller = SDL_GameControllerOpen(index);
129
- if (controller == nullptr) {
130
- std::ostringstream message;
131
- message << "SDL_GameControllerOpen(" << index << ") error: " << SDL_GetError();
132
- SDL_ClearError();
133
- throw Napi::Error::New(env, message.str());
134
- }
135
-
136
- // SDL_GameControllerOpen produces errors even though it succeeds
137
- const char *error = SDL_GetError();
138
- if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
139
- SDL_ClearError();
140
-
141
- int _firmware_version = SDL_GameControllerGetFirmwareVersion(controller);
142
- Napi::Value firmware_version = _firmware_version != 0
143
- ? Napi::Number::New(env, _firmware_version)
144
- : env.Null();
145
-
146
- const char *_serial_number = SDL_GameControllerGetSerial(controller);
147
- Napi::Value serial_number = _serial_number != nullptr
148
- ? Napi::String::New(env, _serial_number)
149
- : env.Null();
150
-
151
- bool has_led = SDL_GameControllerHasLED(controller);
152
- bool has_rumble = SDL_GameControllerHasRumble(controller);
153
- bool has_rumble_triggers = SDL_GameControllerHasRumbleTriggers(controller);
154
-
155
- Napi::Value steam_handle = getSteamHandle(env, controller);
156
-
157
- SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
158
- Napi::Value power = joystick::getPowerLevel(env, joystick);
159
-
160
- Napi::Object result = Napi::Object::New(env);
161
- result.Set("firmwareVersion", firmware_version);
162
- result.Set("serialNumber", serial_number);
163
- result.Set("hasLed", has_led);
164
- result.Set("hasRumble", has_rumble);
165
- result.Set("hasRumbleTriggers", has_rumble_triggers);
166
- result.Set("steamHandle", steam_handle);
167
- result.Set("power", power);
168
-
169
- controller::getState(env, controller, result);
170
-
171
- return result;
172
- }
173
-
174
- Napi::Value
175
- controller::close (const Napi::CallbackInfo &info)
176
- {
177
- Napi::Env env = info.Env();
178
-
179
- int controller_id = info[0].As<Napi::Number>().Int32Value();
180
-
181
- SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
182
- if (controller == nullptr) {
183
- std::ostringstream message;
184
- message << "SDL_GameControllerFromInstanceID(" << controller_id << ") error: " << SDL_GetError();
185
- SDL_ClearError();
186
- throw Napi::Error::New(env, message.str());
187
- }
188
-
189
- SDL_GameControllerClose(controller);
190
-
191
- return env.Undefined();
192
- }