@kmamal/sdl 0.5.2 → 0.6.1
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 +1242 -590
- package/package.json +12 -4
- package/scripts/build.mjs +1 -4
- package/scripts/install.mjs +1 -0
- package/src/javascript/audio/audio-instance.js +23 -10
- package/src/javascript/audio/device.js +18 -0
- package/src/javascript/audio/format-helpers.js +25 -20
- package/src/javascript/audio/index.js +9 -1
- package/src/javascript/audio/prevent-exit.js +5 -2
- package/src/javascript/cleanup.js +3 -3
- package/src/javascript/controller/controller-instance.js +115 -0
- package/src/javascript/controller/device.js +15 -0
- package/src/javascript/controller/index.js +25 -0
- package/src/javascript/enums.js +1 -1
- package/src/javascript/events/index.js +293 -147
- package/src/javascript/globals.js +9 -7
- package/src/javascript/index.js +9 -2
- package/src/javascript/joystick/device.js +15 -0
- package/src/javascript/joystick/index.js +15 -0
- package/src/javascript/joystick/joystick-instance.js +120 -0
- package/src/javascript/keyboard/index.js +4 -4
- package/src/javascript/mouse/index.js +4 -4
- package/src/javascript/video/index.js +2 -2
- package/src/javascript/video/window.js +9 -7
- package/src/native/binding.cpp +415 -240
- package/src/native/sdl-helpers.cpp +868 -567
- package/src/native/sdl-helpers.h +50 -22
- package/src/types/index.d.ts +338 -100
- package/src/javascript/_controller/index.js +0 -47
- package/src/javascript/_joystick/index.js +0 -46
package/package.json
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.6.1",
|
|
3
3
|
"name": "@kmamal/sdl",
|
|
4
|
-
"description": "
|
|
4
|
+
"description": "SDL bindings for Node.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sdl",
|
|
7
7
|
"sdl2",
|
|
8
8
|
"gui",
|
|
9
|
+
"desktop",
|
|
9
10
|
"window",
|
|
10
11
|
"screen",
|
|
11
12
|
"video",
|
|
12
|
-
"audio",
|
|
13
|
-
"sound",
|
|
14
13
|
"input",
|
|
15
14
|
"mouse",
|
|
16
15
|
"keyboard",
|
|
16
|
+
"joystick",
|
|
17
|
+
"controller",
|
|
18
|
+
"gamepad",
|
|
19
|
+
"rumble",
|
|
20
|
+
"audio",
|
|
21
|
+
"sound",
|
|
22
|
+
"speaker",
|
|
23
|
+
"mic",
|
|
17
24
|
"clipboard",
|
|
18
25
|
"game"
|
|
19
26
|
],
|
|
@@ -29,6 +36,7 @@
|
|
|
29
36
|
"test": "cd scripts && node test.mjs",
|
|
30
37
|
"install": "cd scripts && node install.mjs",
|
|
31
38
|
"download-release": "cd scripts && node download-release.mjs",
|
|
39
|
+
"download-sdl": "cd scripts && node download-sdl.mjs",
|
|
32
40
|
"build": "cd scripts && node build.mjs",
|
|
33
41
|
"upload-release": "cd scripts && node upload-release.mjs",
|
|
34
42
|
"release": "cd scripts && node release.mjs",
|
package/scripts/build.mjs
CHANGED
|
@@ -11,10 +11,6 @@ await Promise.all([
|
|
|
11
11
|
await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
|
|
12
12
|
}))
|
|
13
13
|
|
|
14
|
-
console.log("download @kmamal/build-sdl to", C.dir.sdl)
|
|
15
|
-
process.chdir(C.dir.root)
|
|
16
|
-
await import('./download-sdl.mjs')
|
|
17
|
-
|
|
18
14
|
console.log("build in", C.dir.build)
|
|
19
15
|
process.env.SDL_INC = Path.join(C.dir.sdl, 'include')
|
|
20
16
|
process.env.SDL_LIB = Path.join(C.dir.sdl, 'lib')
|
|
@@ -24,6 +20,7 @@ if (process.env.CROSS_COMPILE_ARCH) {
|
|
|
24
20
|
archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
|
|
25
21
|
}
|
|
26
22
|
|
|
23
|
+
process.chdir(C.dir.root)
|
|
27
24
|
execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, { stdio: 'inherit' })
|
|
28
25
|
|
|
29
26
|
console.log("install to", C.dir.dist)
|
package/scripts/install.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
const Bindings = require('../bindings')
|
|
2
|
-
const
|
|
2
|
+
const Enums = require('../enums')
|
|
3
3
|
const Globals = require('../globals')
|
|
4
|
+
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
4
5
|
const { AudioFormatHelpers } = require('./format-helpers')
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
Globals.audioDevices.recording = Bindings.audio_getDevices(true)
|
|
7
|
+
const validEvents = [ 'close' ]
|
|
8
8
|
|
|
9
|
-
class AudioInstance {
|
|
9
|
+
class AudioInstance extends EventsViaPoll {
|
|
10
10
|
constructor (device, options) {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
super(validEvents)
|
|
12
|
+
|
|
13
|
+
const { name, type } = device
|
|
13
14
|
|
|
14
15
|
const {
|
|
15
16
|
channels = 1,
|
|
@@ -18,8 +19,11 @@ class AudioInstance {
|
|
|
18
19
|
buffered = 4096,
|
|
19
20
|
} = options
|
|
20
21
|
|
|
21
|
-
const _format =
|
|
22
|
+
const _format = Enums.audioFormat[format] ?? null
|
|
22
23
|
|
|
24
|
+
if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
|
|
25
|
+
if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
|
|
26
|
+
if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
|
|
23
27
|
if (!Number.isFinite(channels)) { throw Object.assign(new Error("channels must be a number"), { channels }) }
|
|
24
28
|
if (![ 1, 2, 4, 6 ].includes(channels)) { throw Object.assign(new Error("invalid channels"), { channels }) }
|
|
25
29
|
if (!Number.isFinite(frequency)) { throw Object.assign(new Error("frequency must be a number"), { frequency }) }
|
|
@@ -29,7 +33,7 @@ class AudioInstance {
|
|
|
29
33
|
if (!Number.isFinite(buffered)) { throw Object.assign(new Error("buffered must be a number"), { buffered }) }
|
|
30
34
|
if (buffered !== 2 ** (32 - Math.clz32(buffered) - 1)) { throw Object.assign(new Error("invalid buffered"), { buffered }) }
|
|
31
35
|
|
|
32
|
-
this._id = Bindings.audio_openDevice(name, recording, frequency, _format, channels, buffered)
|
|
36
|
+
this._id = Bindings.audio_openDevice(name ?? '', type === 'recording', frequency, _format, channels, buffered)
|
|
33
37
|
|
|
34
38
|
this._device = device
|
|
35
39
|
this._name = name
|
|
@@ -46,10 +50,12 @@ class AudioInstance {
|
|
|
46
50
|
this._minSampleValue = helper.minSampleValue
|
|
47
51
|
this._maxSampleValue = helper.maxSampleValue
|
|
48
52
|
this._zeroSampleValue = helper.zeroSampleValue
|
|
53
|
+
this._readerName = helper.readerName
|
|
54
|
+
this._writerName = helper.writerName
|
|
49
55
|
this._reader = helper.reader
|
|
50
56
|
this._writer = helper.writer
|
|
51
57
|
|
|
52
|
-
Globals.audioInstances.
|
|
58
|
+
Globals.audioInstances.set(this._id, this)
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
get id () { return this._id }
|
|
@@ -69,10 +75,14 @@ class AudioInstance {
|
|
|
69
75
|
return this._reader.call(buffer, offset)
|
|
70
76
|
}
|
|
71
77
|
|
|
78
|
+
get readerName () { return this._readerName }
|
|
79
|
+
|
|
72
80
|
writeSample (buffer, value, offset) {
|
|
73
81
|
return this._writer.call(buffer, value, offset)
|
|
74
82
|
}
|
|
75
83
|
|
|
84
|
+
get writerName () { return this._writerName }
|
|
85
|
+
|
|
76
86
|
get buffered () { return this._buffered }
|
|
77
87
|
|
|
78
88
|
get playing () { return this._playing }
|
|
@@ -105,9 +115,12 @@ class AudioInstance {
|
|
|
105
115
|
close () {
|
|
106
116
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
|
|
107
117
|
|
|
118
|
+
this.emit('close')
|
|
119
|
+
this.removeAllListeners()
|
|
108
120
|
this._closed = true
|
|
121
|
+
|
|
109
122
|
Bindings.audio_close(this._id)
|
|
110
|
-
Globals.audioInstances.
|
|
123
|
+
Globals.audioInstances.delete(this._id)
|
|
111
124
|
}
|
|
112
125
|
}
|
|
113
126
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
const make = (device) => {
|
|
3
|
+
const { isRecorder } = device
|
|
4
|
+
delete device.isRecorder
|
|
5
|
+
device.type = isRecorder ? 'recording' : 'playback'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const compare = (a, b) => {
|
|
9
|
+
const { name: aName } = a
|
|
10
|
+
const { name: bName } = b
|
|
11
|
+
if (aName === bName) { return 0 }
|
|
12
|
+
return a.name < b.name ? -1 : 1
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
make,
|
|
17
|
+
compare,
|
|
18
|
+
}
|
|
@@ -23,53 +23,53 @@ const floatLimits = {
|
|
|
23
23
|
|
|
24
24
|
const AudioFormatHelpers = {
|
|
25
25
|
s8: {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
readerName: 'readInt8',
|
|
27
|
+
writerName: 'writeInt8',
|
|
28
28
|
...signedLimits(8),
|
|
29
29
|
},
|
|
30
30
|
u8: {
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
readerName: 'readUInt8',
|
|
32
|
+
writerName: 'writeUInt8',
|
|
33
33
|
...unsignedLimits(8),
|
|
34
34
|
},
|
|
35
35
|
s16lsb: {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
readerName: 'readInt16LE',
|
|
37
|
+
writerName: 'writeInt16LE',
|
|
38
38
|
...signedLimits(16),
|
|
39
39
|
},
|
|
40
40
|
s16msb: {
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
readerName: 'readInt16BE',
|
|
42
|
+
writerName: 'writeInt16BE',
|
|
43
43
|
...signedLimits(16),
|
|
44
44
|
},
|
|
45
45
|
u16lsb: {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
readerName: 'readUInt16LE',
|
|
47
|
+
writerName: 'writeUInt16LE',
|
|
48
48
|
...unsignedLimits(16),
|
|
49
49
|
},
|
|
50
50
|
u16msb: {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
readerName: 'readUInt16BE',
|
|
52
|
+
writerName: 'writeUInt16BE',
|
|
53
53
|
...unsignedLimits(16),
|
|
54
54
|
},
|
|
55
55
|
s32lsb: {
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
readerName: 'readInt32LE',
|
|
57
|
+
writerName: 'writeInt32LE',
|
|
58
58
|
...signedLimits(32),
|
|
59
59
|
},
|
|
60
60
|
s32msb: {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
readerName: 'readInt32BE',
|
|
62
|
+
writerName: 'writeInt32BE',
|
|
63
63
|
...signedLimits(32),
|
|
64
64
|
},
|
|
65
65
|
f32lsb: {
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
readerName: 'readFloatLE',
|
|
67
|
+
writerName: 'writeFloatLE',
|
|
68
68
|
...floatLimits,
|
|
69
69
|
},
|
|
70
70
|
f32msb: {
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
readerName: 'readFloatBE',
|
|
72
|
+
writerName: 'writeFloatBE',
|
|
73
73
|
...floatLimits,
|
|
74
74
|
},
|
|
75
75
|
}
|
|
@@ -91,4 +91,9 @@ if (Os.endianness() === 'LE') {
|
|
|
91
91
|
AudioFormatHelpers.f32sys = AudioFormatHelpers.f32msb
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
for (const helper of Object.values(AudioFormatHelpers)) {
|
|
95
|
+
helper.reader = Buffer.prototype[helper.readerName]
|
|
96
|
+
helper.writer = Buffer.prototype[helper.writerName]
|
|
97
|
+
}
|
|
98
|
+
|
|
94
99
|
module.exports = { AudioFormatHelpers }
|
|
@@ -17,7 +17,7 @@ const audio = new class extends EventsViaPoll {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
openDevice (device, options = {}) {
|
|
20
|
-
return device.recording
|
|
20
|
+
return device.type === 'recording'
|
|
21
21
|
? new AudioRecordingInstance(device, options)
|
|
22
22
|
: new AudioPlaybackInstance(device, options)
|
|
23
23
|
}
|
|
@@ -31,9 +31,17 @@ const audio = new class extends EventsViaPoll {
|
|
|
31
31
|
return AudioFormatHelpers[format].reader.call(buffer, offset)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
readerName (format) {
|
|
35
|
+
return AudioFormatHelpers[format].readerName
|
|
36
|
+
}
|
|
37
|
+
|
|
34
38
|
writeSample (format, buffer, value, offset) {
|
|
35
39
|
return AudioFormatHelpers[format].writer.call(buffer, value, offset)
|
|
36
40
|
}
|
|
41
|
+
|
|
42
|
+
writerName (format) {
|
|
43
|
+
return AudioFormatHelpers[format].writerName
|
|
44
|
+
}
|
|
37
45
|
}()
|
|
38
46
|
|
|
39
47
|
module.exports = { audio }
|
|
@@ -4,7 +4,10 @@ const { AudioPlaybackInstance } = require('./audio-playback-instance')
|
|
|
4
4
|
let timeout
|
|
5
5
|
|
|
6
6
|
const resetTimeout = () => {
|
|
7
|
-
if (timeout) {
|
|
7
|
+
if (timeout) {
|
|
8
|
+
clearTimeout(timeout)
|
|
9
|
+
timeout = null
|
|
10
|
+
}
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
process.on('beforeExit', (code) => {
|
|
@@ -12,7 +15,7 @@ process.on('beforeExit', (code) => {
|
|
|
12
15
|
|
|
13
16
|
let duration = 0
|
|
14
17
|
|
|
15
|
-
for (const instance of Globals.audioInstances.
|
|
18
|
+
for (const instance of Globals.audioInstances.values()) {
|
|
16
19
|
if (!(instance instanceof AudioPlaybackInstance)) { continue }
|
|
17
20
|
const { queued, playing, buffered } = instance
|
|
18
21
|
if (!queued || !playing) { continue }
|
|
@@ -12,17 +12,17 @@ process.on('exit', (code) => {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// Close all audio instances
|
|
15
|
-
for (const instance of Globals.audioInstances.
|
|
15
|
+
for (const instance of Globals.audioInstances.values()) {
|
|
16
16
|
instance.close()
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// Close all joysticks
|
|
20
|
-
for (const joystick of Globals.
|
|
20
|
+
for (const joystick of Globals.joystickInstances.all.values()) {
|
|
21
21
|
joystick.close()
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Close all controllers
|
|
25
|
-
for (const controller of Globals.
|
|
25
|
+
for (const controller of Globals.controllerInstances.all.values()) {
|
|
26
26
|
controller.close()
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const Bindings = require('../bindings')
|
|
2
|
+
const Enums = require('../enums')
|
|
3
|
+
const Globals = require('../globals')
|
|
4
|
+
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
5
|
+
|
|
6
|
+
const validEvents = [
|
|
7
|
+
'axisMotion',
|
|
8
|
+
'buttonDown',
|
|
9
|
+
'buttonUp',
|
|
10
|
+
'remap',
|
|
11
|
+
'close',
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
class ControllerInstance extends EventsViaPoll {
|
|
15
|
+
constructor (device) {
|
|
16
|
+
super(validEvents)
|
|
17
|
+
|
|
18
|
+
const result = Bindings.controller_open(device._index)
|
|
19
|
+
|
|
20
|
+
this._firmwareVersion = result.firmwareVersion
|
|
21
|
+
this._serialNumber = result.serialNumber
|
|
22
|
+
this._hasLed = result.hasLed
|
|
23
|
+
this._hasRumble = result.hasRumble
|
|
24
|
+
this._hasRumbleTriggers = result.hasRumbleTriggers
|
|
25
|
+
this._axes = result.axes
|
|
26
|
+
this._buttons = result.buttons
|
|
27
|
+
|
|
28
|
+
this._device = device
|
|
29
|
+
|
|
30
|
+
this._closed = false
|
|
31
|
+
|
|
32
|
+
Globals.controllerInstances.all.add(this)
|
|
33
|
+
let collection = Globals.controllerInstances.byId.get(this._device.id)
|
|
34
|
+
if (!collection) {
|
|
35
|
+
collection = new Set()
|
|
36
|
+
Globals.controllerInstances.byId.set(this._device.id, collection)
|
|
37
|
+
}
|
|
38
|
+
collection.add(this)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get device () { return this._device }
|
|
42
|
+
get firmwareVersion () { return this._firmwareVersion }
|
|
43
|
+
get serialNumber () { return this._serialNumber }
|
|
44
|
+
|
|
45
|
+
get axes () { return this._axes }
|
|
46
|
+
get buttons () { return this._buttons }
|
|
47
|
+
|
|
48
|
+
get power () {
|
|
49
|
+
const power = Bindings.joystick_getPower(this._device.id)
|
|
50
|
+
return Enums.powerLevelNames[power]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get hasLed () { return this._hasLed }
|
|
54
|
+
setLed (red, green, blue) {
|
|
55
|
+
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
56
|
+
|
|
57
|
+
if (!Number.isFinite(red)) { throw Object.assign(new Error("red must be a number"), { red }) }
|
|
58
|
+
if (red < 0 || red > 1) { throw Object.assign(new Error("red must be between 0 and 1"), { red }) }
|
|
59
|
+
if (!Number.isFinite(green)) { throw Object.assign(new Error("green must be a number"), { green }) }
|
|
60
|
+
if (green < 0 || green > 1) { throw Object.assign(new Error("green must be between 0 and 1"), { green }) }
|
|
61
|
+
if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
|
|
62
|
+
if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
|
|
63
|
+
|
|
64
|
+
Bindings.joystick_rumble(this._device.id, red, green, blue)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get hasRumble () { return this._hasRumble }
|
|
68
|
+
rumble (lowFreqRumble = 1, highFreqRumble = 1, duration = 1e3) {
|
|
69
|
+
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
70
|
+
|
|
71
|
+
if (!Number.isFinite(lowFreqRumble)) { throw Object.assign(new Error("lowFreqRumble must be a number"), { lowFreqRumble }) }
|
|
72
|
+
if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
|
|
73
|
+
if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
|
|
74
|
+
if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
|
|
75
|
+
if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
|
|
76
|
+
|
|
77
|
+
Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
stopRumble () { this.rumble(0, 0) }
|
|
81
|
+
|
|
82
|
+
get hasRumbleTriggers () { return this._hasRumbleTriggers }
|
|
83
|
+
rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
|
|
84
|
+
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
85
|
+
|
|
86
|
+
if (!Number.isFinite(leftRumble)) { throw Object.assign(new Error("leftRumble must be a number"), { leftRumble }) }
|
|
87
|
+
if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
|
|
88
|
+
if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
|
|
89
|
+
if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
|
|
90
|
+
if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
|
|
91
|
+
|
|
92
|
+
Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
stopRumbleTriggers () { this.rumbleTriggers(0, 0) }
|
|
96
|
+
|
|
97
|
+
get closed () { return this._closed }
|
|
98
|
+
close () {
|
|
99
|
+
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
100
|
+
|
|
101
|
+
this.emit('close')
|
|
102
|
+
this.removeAllListeners()
|
|
103
|
+
this._closed = true
|
|
104
|
+
|
|
105
|
+
Globals.controllerInstances.all.delete(this)
|
|
106
|
+
const collection = Globals.controllerInstances.byId.get(this._device.id)
|
|
107
|
+
collection.delete(this)
|
|
108
|
+
if (collection.size === 0) {
|
|
109
|
+
Bindings.controller_close(this._device.id)
|
|
110
|
+
Globals.controllerInstances.byId.delete(this._device.id)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = { ControllerInstance }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const Bindings = require('../bindings')
|
|
2
|
+
const Globals = require('../globals')
|
|
3
|
+
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
4
|
+
const { ControllerInstance } = require('./controller-instance')
|
|
5
|
+
|
|
6
|
+
const validEvents = [ 'deviceAdd', 'deviceRemove', 'deviceRemap' ]
|
|
7
|
+
|
|
8
|
+
const controller = new class extends EventsViaPoll {
|
|
9
|
+
constructor () { super(validEvents) }
|
|
10
|
+
|
|
11
|
+
get devices () { return Globals.controllerDevices }
|
|
12
|
+
|
|
13
|
+
openDevice (device) { return new ControllerInstance(device) }
|
|
14
|
+
|
|
15
|
+
addMappings (mappings) {
|
|
16
|
+
if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
|
|
17
|
+
for (const mapping of mappings) {
|
|
18
|
+
if (typeof mapping !== 'string') { throw Object.assign(new Error("mapping must be a string"), { mappings }) }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Bindings.controller_addMappings(mappings)
|
|
22
|
+
}
|
|
23
|
+
}()
|
|
24
|
+
|
|
25
|
+
module.exports = { controller }
|
package/src/javascript/enums.js
CHANGED