@kmamal/sdl 0.5.2 → 0.6.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.
@@ -1,227 +1,373 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
+ const Enums = require('../enums')
3
4
  const { maybeTriggerQuit } = require('./quit')
4
- const { audio } = require('../audio')
5
- const { clipboard } = require('../clipboard')
6
5
  const { mapping } = require('../keyboard/key-mapping')
6
+ const { joystick: joystickModule } = require('../joystick')
7
+ const { controller: controllerModule } = require('../controller')
8
+ const { audio: audioModule } = require('../audio')
9
+ const { clipboard: clipboardModule } = require('../clipboard')
10
+ const {
11
+ make: makeJoystickDevice,
12
+ compare: compareJoystickDevice,
13
+ } = require('../joystick/device')
14
+ const {
15
+ make: makeControllerDevice,
16
+ compare: compareControllerDevice,
17
+ filter: filterControllerDevice,
18
+ } = require('../controller/device')
19
+ const {
20
+ make: makeAudioDevice,
21
+ compare: compareAudioDevice,
22
+ } = require('../audio/device')
23
+
24
+
25
+ const reconcileDevices = (emitter, mainList, currList, compare) => {
26
+ currList.sort(compare)
27
+
28
+ let mainIndex = 0
29
+ let currIndex = 0
30
+ let mainDevice = mainList[mainIndex]
31
+ let currDevice = currList[currIndex]
32
+ while (mainIndex < mainList.length && currIndex < currList.length) {
33
+ const cmp = compare(mainDevice, currDevice)
34
+ if (cmp === 0) {
35
+ mainDevice = mainList[++mainIndex]
36
+ currDevice = currList[++currIndex]
37
+ } else if (cmp < 0) {
38
+ mainList.splice(mainIndex, 1)
39
+ emitter.emit('deviceRemove', { device: mainDevice })
40
+ mainDevice = mainList[mainIndex]
41
+ } else {
42
+ mainList.splice(mainIndex, 0, currDevice)
43
+ mainDevice = mainList[++mainIndex]
44
+ emitter.emit('deviceAdd', { device: currDevice })
45
+ currDevice = currList[++currIndex]
46
+ }
47
+ }
48
+
49
+ if (mainIndex < mainList.length) {
50
+ while (mainIndex < mainList.length) {
51
+ [ mainDevice ] = mainList.splice(mainIndex, 1)
52
+ emitter.emit('deviceRemove', { device: mainDevice })
53
+ }
54
+ } else {
55
+ while (currIndex < currList.length) {
56
+ mainList.push(currDevice)
57
+ emitter.emit('deviceAdd', { device: currDevice })
58
+ currDevice = currList[++currIndex]
59
+ }
60
+ }
61
+ }
62
+
7
63
 
8
64
  const handleEvents = () => {
9
65
  let event
10
66
  while ((event = Bindings.events_poll())) {
11
67
  const { family, type } = event
12
- if (!family || !type) { continue }
68
+ if (family === undefined || type === undefined) { continue }
13
69
  delete event.family
14
70
  delete event.type
15
71
 
16
72
  switch (family) {
17
- case 'app': {
18
- maybeTriggerQuit()
19
- break
20
- }
21
-
22
- case 'window': {
23
- const window = Globals.windows.all.get(event.window)
24
- if (!window) { return }
25
- delete event.window
73
+ case Enums.eventFamily.app: {
74
+ if (type === Enums.eventType.quit) {
75
+ maybeTriggerQuit()
76
+ }
77
+ } break
78
+
79
+ case Enums.eventFamily.window: {
80
+ const { windowId } = event
81
+ delete event.windowId
82
+
83
+ const window = Globals.windows.all.get(windowId)
84
+ if (!window) { continue }
26
85
 
27
86
  switch (type) {
28
- case 'move': {
87
+ case Enums.eventType.move: {
29
88
  window._x = event.x
30
89
  window._y = event.y
31
- break
32
- }
33
- case 'resize': {
90
+ } break
91
+ case Enums.eventType.resize: {
34
92
  window._width = event.width
35
93
  window._height = event.height
36
- break
37
- }
94
+ } break
38
95
 
39
- case 'show': {
96
+ case Enums.eventType.show: {
40
97
  window._visible = true
41
- break
42
- }
43
- case 'hide': {
98
+ } break
99
+ case Enums.eventType.hide: {
44
100
  window._visible = false
45
- break
46
- }
101
+ } break
47
102
 
48
- case 'minimize': {
103
+ case Enums.eventType.minimize: {
49
104
  window._visible = false
50
105
  window._minimized = true
51
106
  window._maximized = false
52
107
  window._fullscreen = false
53
- break
54
- }
55
- case 'maximize': {
108
+ } break
109
+ case Enums.eventType.maximize: {
56
110
  window._visible = true
57
111
  window._minimized = false
58
112
  window._maximized = true
59
113
  window._fullscreen = false
60
- break
61
- }
62
- case 'restore': {
114
+ } break
115
+ case Enums.eventType.restore: {
63
116
  window._visible = true
64
117
  window._minimized = false
65
118
  window._maximized = false
66
119
  window._fullscreen = false
67
- break
68
- }
120
+ } break
69
121
 
70
- case 'focus': {
122
+ case Enums.eventType.focus: {
71
123
  window._focused = true
72
124
  Globals.windows.focused = window
73
- break
74
- }
75
- case 'blur': {
125
+ } break
126
+ case Enums.eventType.blur: {
76
127
  window._focused = false
77
128
  Globals.windows.focused = null
78
- break
79
- }
129
+ } break
80
130
 
81
- case 'hover': {
131
+ case Enums.eventType.hover: {
82
132
  window._hovered = true
83
133
  Globals.windows.hovered = window
84
- break
85
- }
86
- case 'leave': {
134
+ } break
135
+ case Enums.eventType.leave: {
87
136
  window._hovered = false
88
137
  Globals.windows.hovered = null
89
- break
90
- }
138
+ } break
91
139
 
92
- case 'close': {
140
+ case Enums.eventType.close: {
93
141
  let shouldPrevent = false
94
142
  const prevent = () => { shouldPrevent = true }
143
+ window.emit('beforeClose', { prevent })
144
+ if (shouldPrevent) { continue }
145
+ } break
95
146
 
96
- const type2 = 'beforeClose'
97
- const event2 = { prevent }
98
- window.emit(type2, event2)
99
-
100
- if (shouldPrevent) { return }
101
- break
102
- }
103
-
104
- // No default
147
+ default: continue
105
148
  }
106
149
 
107
150
  if (Object.keys(event).length > 0) {
108
- window.emit(type, event)
151
+ window.emit(Enums.eventTypeNames[type], event)
109
152
  } else {
110
- window.emit(type)
153
+ window.emit(Enums.eventTypeNames[type])
111
154
  }
112
155
 
113
- if (type === 'close') { window.destroy() }
114
- break
115
- }
156
+ if (type === Enums.eventType.close) { window.destroy() }
157
+ } break
116
158
 
117
- case 'keyboard': {
118
- const window = Globals.windows.all.get(event.window)
119
- if (!window) { return }
159
+ case Enums.eventFamily.keyboard: {
160
+ const { windowId } = event
120
161
  delete event.window
121
162
 
163
+ const window = Globals.windows.all.get(windowId)
164
+ if (!window) { continue }
165
+
122
166
  const { key } = event
123
- event.key = mapping[key] ?? (key && Array.from(key).length === 1 ? key : null)
167
+ event.key = mapping[key] ?? key?.length === 1 ? key : null
124
168
 
125
169
  if (Object.keys(event).length > 0) {
126
- window.emit(type, event)
170
+ window.emit(Enums.eventTypeNames[type], event)
127
171
  } else {
128
- window.emit(type)
172
+ window.emit(Enums.eventTypeNames[type])
129
173
  }
174
+ } break
130
175
 
131
- break
132
- }
176
+ case Enums.eventFamily.mouse:
177
+ case Enums.eventFamily.text:
178
+ case Enums.eventFamily.drop: {
179
+ const { windowId } = event
180
+ delete event.windowId
133
181
 
134
- case 'mouse':
135
- case 'text':
136
- case 'drop':
137
- {
138
- const window = Globals.windows.all.get(event.window)
139
- if (!window) { return }
140
- delete event.window
182
+ const window = Globals.windows.all.get(windowId)
183
+ if (!window) { continue }
141
184
 
142
185
  if (Object.keys(event).length > 0) {
143
- window.emit(type, event)
186
+ window.emit(Enums.eventTypeNames[type], event)
144
187
  } else {
145
- window.emit(type)
188
+ window.emit(Enums.eventTypeNames[type])
146
189
  }
190
+ } break
191
+
192
+ case Enums.eventFamily.joystickDevice: {
193
+ if (type === Enums.eventType.deviceRemove) {
194
+ const { joystickId } = event
195
+ delete event.joystickId
196
+
197
+ {
198
+ const collection = Globals.joystickInstances.byId.get(joystickId)
199
+ for (const joystickInstance of collection.values()) {
200
+ joystickInstance.close()
201
+ }
202
+ }
147
203
 
148
- break
149
- }
150
-
151
- case 'audioDevice': {
152
- const { index, recording } = event
153
- const outdated = recording
154
- ? Globals.audioDevices.recording
155
- : Globals.audioDevices.playback
156
- const updated = Bindings.audio_getDevices(recording)
157
- if (recording) {
158
- Globals.audioDevices.recording = updated
159
- } else {
160
- Globals.audioDevices.playback = updated
204
+ {
205
+ const collection = Globals.controllerInstances.byId.get(joystickId)
206
+ for (const controllerInstance of collection.values()) {
207
+ controllerInstance.close()
208
+ }
209
+ }
161
210
  }
162
211
 
163
- if (type.endsWith('remove')) {
164
- const device = outdated[index]
165
- for (const instance of Globals.audioInstances.all.values()) {
166
- if (instance.device !== device) { continue }
167
- instance.close()
168
- }
169
- event.device = device
170
- } else {
171
- event.device = updated[index]
212
+ const { devices: joystickDevices } = event
213
+ delete event.devices
214
+
215
+ const controllerDevices = JSON.parse(JSON.stringify(joystickDevices
216
+ .filter(filterControllerDevice)))
217
+
218
+ for (const joystickDevice of joystickDevices) {
219
+ makeJoystickDevice(joystickDevice)
220
+ }
221
+
222
+ for (const controllerDevice of controllerDevices) {
223
+ makeControllerDevice(controllerDevice)
172
224
  }
173
225
 
174
- delete event.index
175
- delete event.recording
176
- audio.emit(type, event)
177
- break
178
- }
226
+ reconcileDevices(
227
+ joystickModule,
228
+ Globals.joystickDevices,
229
+ joystickDevices,
230
+ compareJoystickDevice,
231
+ )
179
232
 
180
- case 'clipboard': {
181
- clipboard.emit(type)
182
- }
233
+ reconcileDevices(
234
+ controllerModule,
235
+ Globals.controllerDevices,
236
+ controllerDevices,
237
+ compareControllerDevice,
238
+ )
239
+ } break
183
240
 
184
- // No default
185
- }
241
+ case Enums.eventFamily.joystick: {
242
+ const { joystickId } = event
243
+ delete event.joystickId
244
+
245
+ const collection = Globals.joystickInstances.byId.get(joystickId)
246
+ if (!collection) { continue }
247
+
248
+ switch (type) {
249
+ case Enums.eventType.axisMotion: {
250
+ for (const joystickInstance of collection) {
251
+ joystickInstance._axes[event.axis] = event.value
252
+ joystickInstance.emit('axisMotion', event)
253
+ }
254
+ } break
255
+
256
+ case Enums.eventType.ballMotion: {
257
+ for (const joystickInstance of collection) {
258
+ const ball = joystickInstance._balls[event.ball]
259
+ ball.x = event.x
260
+ ball.y = event.y
261
+ joystickInstance.emit('ballMotion', event)
262
+ }
263
+ } break
264
+
265
+ case Enums.eventType.buttonDown: {
266
+ for (const joystickInstance of collection) {
267
+ joystickInstance._buttons[event.button] = true
268
+ joystickInstance.emit('buttonDown', event)
269
+ }
270
+ } break
271
+ case Enums.eventType.buttonUp: {
272
+ for (const joystickInstance of collection) {
273
+ joystickInstance._buttons[event.button] = false
274
+ joystickInstance.emit('buttonUp', event)
275
+ }
276
+ } break
277
+
278
+ case Enums.eventType.hatMotion: {
279
+ event.value = Enums.hatPositionNames[event.value]
280
+ for (const joystickInstance of collection) {
281
+ joystickInstance._hats[event.hat] = event.value
282
+ joystickInstance.emit('hatMotion', event)
283
+ }
284
+ } break
285
+
286
+ default: continue
287
+ }
288
+ } break
289
+
290
+ case Enums.eventFamily.controller: {
291
+ const { controllerId } = event
292
+ delete event.controllerId
186
293
 
187
- // if (event.joystick) {
188
- // const joystick = Globals.joysticks.all.get(event.joystick)
189
- // if (!joystick) { return }
190
-
191
- // switch (type) {
192
- // case 'joystickAxisMotion': {
193
- // joystick.axes[event.axis] = event.value
194
- // break
195
- // }
196
- // case 'joystickBallMotion': {
197
- // const ball = joystick.balls[event.ball]
198
- // ball.x = event.x
199
- // ball.x = event.y
200
- // break
201
- // }
202
- // case 'joystickHatMotion': {
203
- // joystick.hats[event.hat] = event.value
204
- // break
205
- // }
206
- // case 'joystickButtonUp': {
207
- // joystick.buttons[event.button] = 0
208
- // break
209
- // }
210
- // case 'joystickButtonDown': {
211
- // joystick.buttons[event.button] = 0
212
- // break
213
- // }
214
- // case 'joystickDeviceRemoved': {
215
- // joystick.close()
216
- // break
217
- // }
218
- // // No default
219
- // }
220
-
221
- // event.joystick = joystick
222
- // joystick.emit(type, event)
223
- // return
224
- // }
294
+ const collection = Globals.controllerInstances.byId.get(controllerId)
295
+ if (!collection) { continue }
296
+
297
+ switch (type) {
298
+ case Enums.eventType.axisMotion: {
299
+ event.axis = Enums.controllerAxisNames[event.axis]
300
+ for (const controllerInstance of collection.values()) {
301
+ controllerInstance._axes[event.axis] = event.value
302
+ controllerInstance.emit('axisMotion', event)
303
+ }
304
+ } break
305
+
306
+ case Enums.eventType.buttonDown: {
307
+ event.button = Enums.controllerButtonNames[event.button]
308
+ for (const controllerInstance of collection.values()) {
309
+ controllerInstance._axes[event.button] = true
310
+ controllerInstance.emit('buttonDown', event)
311
+ }
312
+ } break
313
+
314
+ case Enums.eventType.buttonUp: {
315
+ event.button = Enums.controllerButtonNames[event.button]
316
+ for (const controllerInstance of collection.values()) {
317
+ controllerInstance._axes[event.button] = true
318
+ controllerInstance.emit('buttonUp', event)
319
+ }
320
+ } break
321
+
322
+ case Enums.eventType.remap: {
323
+ const { axes, buttons } = event
324
+ delete event.axes
325
+ delete event.buttons
326
+
327
+ for (const controllerInstance of collection.values()) {
328
+ Object.assign(controllerInstance._axes, axes)
329
+ Object.assign(controllerInstance._buttons, buttons)
330
+ controllerInstance.emit('remap', event)
331
+ }
332
+ } break
333
+
334
+ default: continue
335
+ }
336
+ } break
337
+
338
+ case Enums.eventFamily.audioDevice: {
339
+ const { isRecorder } = event
340
+ delete event.isRecorder
341
+
342
+ if (type === Enums.eventType.deviceRemove) {
343
+ const { audioId } = event
344
+ delete event.audioId
345
+
346
+ const audioInstance = Globals.audioInstances.get(audioId)
347
+ if (audioInstance) { audioInstance.close() }
348
+ }
349
+
350
+ const { devices: audioDevices } = event
351
+ delete event.devices
352
+
353
+ for (const audioDevice of audioDevices) {
354
+ makeAudioDevice(audioDevice)
355
+ }
356
+
357
+ reconcileDevices(
358
+ audioModule,
359
+ Globals.audioDevices[isRecorder ? 'recording' : 'playback'],
360
+ audioDevices,
361
+ compareAudioDevice,
362
+ )
363
+ } break
364
+
365
+ case Enums.eventFamily.clipboard: {
366
+ clipboardModule.emit(Enums.eventTypeNames[type])
367
+ } break
368
+
369
+ default: continue
370
+ }
225
371
  }
226
372
  }
227
373
 
@@ -9,14 +9,16 @@ module.exports = {
9
9
  playback: [],
10
10
  recording: [],
11
11
  },
12
- audioInstances: {
13
- all: new Map(),
14
- },
15
- joysticks: {
16
- all: new Map(),
12
+ audioInstances: new Map(),
13
+ joystickDevices: [],
14
+ joystickInstances: {
15
+ all: new Set(),
16
+ byId: new Map(),
17
17
  },
18
- controllers: {
19
- all: new Map(),
18
+ controllerDevices: [],
19
+ controllerInstances: {
20
+ all: new Set(),
21
+ byId: new Map(),
20
22
  },
21
23
 
22
24
  events: null, // Set later to { startPolling, stopPolling }
@@ -1,19 +1,26 @@
1
+ const { isMainThread } = require('worker_threads')
2
+ if (!isMainThread) { throw new Error('@kmamal/sdl can only be used in the main thread') }
3
+
1
4
  const Bindings = require('./bindings')
2
5
 
3
6
  const info = Bindings.initialize()
4
7
 
5
8
  const { sdl } = require('./sdl')
6
9
  const { video } = require('./video')
7
- const { audio } = require('./audio')
8
10
  const { keyboard } = require('./keyboard')
9
11
  const { mouse } = require('./mouse')
12
+ const { joystick } = require('./joystick')
13
+ const { controller } = require('./controller')
14
+ const { audio } = require('./audio')
10
15
  const { clipboard } = require('./clipboard')
11
16
 
12
17
  sdl.info = info
13
18
  sdl.video = video
14
- sdl.audio = audio
15
19
  sdl.keyboard = keyboard
16
20
  sdl.mouse = mouse
21
+ sdl.joystick = joystick
22
+ sdl.controller = controller
23
+ sdl.audio = audio
17
24
  sdl.clipboard = clipboard
18
25
 
19
26
  require('./events')
@@ -0,0 +1,15 @@
1
+ const Enums = require('../enums')
2
+
3
+ const make = (device) => {
4
+ const { type } = device
5
+ device.type = Enums.joystickTypeNames[type]
6
+ delete device.isController
7
+ delete device.mapping
8
+ }
9
+
10
+ const compare = (a, b) => a.id - b.id
11
+
12
+ module.exports = {
13
+ make,
14
+ compare,
15
+ }
@@ -0,0 +1,15 @@
1
+ const Globals = require('../globals')
2
+ const { EventsViaPoll } = require('../events/events-via-poll')
3
+ const { JoystickInstance } = require('./joystick-instance')
4
+
5
+ const validEvents = [ 'deviceAdd', 'deviceRemove' ]
6
+
7
+ const joystick = new class extends EventsViaPoll {
8
+ constructor () { super(validEvents) }
9
+
10
+ get devices () { return Globals.joystickDevices }
11
+
12
+ openDevice (device) { return new JoystickInstance(device) }
13
+ }()
14
+
15
+ module.exports = { joystick }