@kmamal/sdl 0.6.6 → 0.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,11 +7,18 @@
7
7
 
8
8
  SDL bindings for Node.js.
9
9
  Provides window management, input events (keyboard, mouse, joystick, controller), audio playback and recording, and clipboard manipulation.
10
- You can also use it together with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to get native WebGL drawing without having to use a browser.
11
10
 
12
11
  It should work on Linux, Mac, and Windows.
13
12
  Prebuilt binaries are available for x64 architectures, and arm-based Macs.
14
13
 
14
+ #### Canvas, WebGL, and WebGPU
15
+
16
+ One goal of this project is to allow using Canvas (the 2D rendering context API), WebGL, and WebGPU without a browser.
17
+ You can already use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to draw on windows using WebGL.
18
+ WebGPU is also working, but it's not been fully integrated: you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme) for compute and render, but it can't yet render directly to windows; that will come in a future version.
19
+ Right now you have to go through an intermediate buffer.
20
+ Same with the Canvas API, where you can use the [canvas](https://www.npmjs.com/package/canvas) package to draw to a buffer and then draw that to a window.
21
+
15
22
 
16
23
  ## Installation
17
24
 
@@ -26,7 +33,9 @@ npm install @kmamal/sdl
26
33
  (But if things go wrong do look over [here](#building-from-source))
27
34
 
28
35
 
29
- ## "Hello, World!" example
36
+ ## Examples
37
+
38
+ ### "Hello, World!"
30
39
 
31
40
  ```js
32
41
  const sdl = require('@kmamal/sdl')
@@ -35,25 +44,39 @@ const window = sdl.video.createWindow({ title: "Hello, World!" })
35
44
  window.on('*', console.log)
36
45
  ```
37
46
 
38
- ## WebGL example
47
+ ### Canvas
39
48
  ```js
40
- const sdl = require('@kmamal/sdl')
41
- const createContext = require('@kmamal/gl')
49
+ import sdl from '@kmamal/sdl'
50
+ import canvas from 'canvas'
42
51
 
43
- const window = sdl.video.createWindow({
44
- title: "WebGL!",
45
- opengl: true,
46
- })
52
+ const window = sdl.video.createWindow({ title: "Canvas" })
53
+ const { width, height } = window
54
+ const canvas = canvas.createCanvas(width, height)
55
+ const ctx = canvas.getContext('2d')
47
56
 
48
57
  // Clear screen to red
58
+ ctx.fillStyle = 'red'
59
+ ctx.fillRect(0, 0, width, height)
60
+ const buffer = canvas.toBuffer('raw')
61
+ window.render(width, height, width * 4, 'bgra32', buffer)
62
+ ```
63
+
64
+ ### WebGL
65
+ ```js
66
+ const sdl = require('@kmamal/sdl')
67
+ const createContext = require('@kmamal/gl')
68
+
69
+ const window = sdl.video.createWindow({title: "WebGL", opengl: true })
49
70
  const { width, height, native } = window
50
71
  const gl = createContext(width, height, { window: native })
72
+
73
+ // Clear screen to red
51
74
  gl.clearColor(1, 0, 0, 1)
52
75
  gl.clear(gl.COLOR_BUFFER_BIT)
53
76
  gl.swap()
54
77
  ```
55
78
 
56
- There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples), covering several common use cases, including how to draw using Canvas and WebGL, as well as how to package the application for distribution.
79
+ There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples).
57
80
 
58
81
 
59
82
  # API Reference
@@ -146,12 +169,12 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
146
169
  * [sdl.mouse.getButton(button)](#sdlmousegetbuttonbutton)
147
170
  * [sdl.mouse.position](#sdlmouseposition)
148
171
  * [sdl.mouse.setPosition(x, y)](#sdlmousesetpositionx-y)
149
- * [sdl.mouse.setCursor(cursor)](#sdlmousesetcursor)
172
+ * [sdl.mouse.setCursor(cursor)](#sdlmousesetcursorcursor)
150
173
  * [sdl.mouse.setCursorImage(width, height, stride, format, buffer, x, y)](#sdlmousesetcursorimagewidth-height-stride-format-buffer-x-y)
151
174
  * [sdl.mouse.showCursor([show])](#sdlmouseshowcursorshow)
152
175
  * [sdl.mouse.hideCursor()](#sdlmousehidecursor)
153
176
  * [sdl.mouse.capture([capture])](#sdlmousecapturecapture)
154
- * [sdl.mouse.uncapture()](#sdlmousecapture)
177
+ * [sdl.mouse.uncapture()](#sdlmouseuncapture)
155
178
  * [sdl.joystick](#sdljoystick)
156
179
  * [Joystick types](#joystick-types)
157
180
  * [Hat positions](#hat-positions)
@@ -176,12 +199,12 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
176
199
  * [joystickInstance.hats](#joystickinstancehats)
177
200
  * [joystickInstance.power](#joystickinstancepower)
178
201
  * [joystickInstance.hasLed](#joystickinstancehasled)
179
- * [joystickInstance.setLed(red, green, blue)](#joystickinstancesetledredgreenblue)
202
+ * [joystickInstance.setLed(red, green, blue)](#joystickinstancesetledred-green-blue)
180
203
  * [joystickInstance.hasRumble](#joystickinstancehasrumble)
181
- * [joystickInstance.rumble([low[, high[, duration]]])](#joystickinstancerumblelowhighduration)
204
+ * [joystickInstance.rumble([low[, high[, duration]]])](#joystickinstancerumblelow-high-duration)
182
205
  * [joystickInstance.stopRumble()](#joystickinstancestoprumble)
183
206
  * [joystickInstance.hasRumbleTriggers](#joystickinstancehasrumbletriggers)
184
- * [joystickInstance.rumbleTriggers([left[, right[, duration]]])](#joystickinstancerumbletriggersleftrightduration)
207
+ * [joystickInstance.rumbleTriggers([left[, right[, duration]]])](#joystickinstancerumbletriggersleft-right-duration)
185
208
  * [joystickInstance.stopRumbleTriggers()](#joystickinstancestoprumbletriggers)
186
209
  * [joystickInstance.closed](#joystickinstanceclosed)
187
210
  * [joystickInstance.close()](#joystickinstanceclose)
@@ -204,12 +227,12 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
204
227
  * [controllerInstance.buttons](#controllerinstancebuttons)
205
228
  * [controllerInstance.power](#controllerinstancepower)
206
229
  * [controllerInstance.hasLed](#controllerinstancehasled)
207
- * [controllerInstance.setLed(red, green, blue)](#controllerinstancesetledredgreenblue)
230
+ * [controllerInstance.setLed(red, green, blue)](#controllerinstancesetledred-green-blue)
208
231
  * [controllerInstance.hasRumble](#controllerinstancehasrumble)
209
- * [controllerInstance.rumble([low[, high[, duration]]])](#controllerinstancerumblelowhighduration)
232
+ * [controllerInstance.rumble([low[, high[, duration]]])](#controllerinstancerumblelow-high-duration)
210
233
  * [controllerInstance.stopRumble()](#controllerinstancestoprumble)
211
234
  * [controllerInstance.hasRumbleTriggers](#controllerinstancehasrumbletriggers)
212
- * [controllerInstance.rumbleTriggers([left[, right[, duration]]])](#controllerinstancerumbletriggersleftrightduration)
235
+ * [controllerInstance.rumbleTriggers([left[, right[, duration]]])](#controllerinstancerumbletriggersleft-right-duration)
213
236
  * [controllerInstance.stopRumbleTriggers()](#controllerinstancestoprumbletriggers)
214
237
  * [controllerInstance.closed](#controllerinstanceclosed)
215
238
  * [controllerInstance.close()](#controllerinstanceclose)
@@ -231,7 +254,6 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
231
254
  * [class AudioInstance](#class-audioinstance)
232
255
  * [Event: 'close'](#audio-instance-event-close)
233
256
  * [audioInstance.id](#audioinstanceid)
234
- * [audioInstance.name](#audioinstancename)
235
257
  * [audioInstance.channels](#audioinstancechannels)
236
258
  * [audioInstance.frequency](#audioinstancefrequency)
237
259
  * [audioInstance.format](#audioinstanceformat)
@@ -636,7 +658,7 @@ Fired when the mouse wheel is scrolled.
636
658
 
637
659
  ### Event: 'dropBegin'
638
660
 
639
- When dropping a set of items onto a window, first the `'drop-begin'` event will be fired, then a number of `'drop-text'` and/or `'drop-file'` events will be fired corresponding to the contents of the drop, then finally the `'drop-complete'` event will be fired.
661
+ When dropping a set of items onto a window, first the [`'dropBegin'`](#event-dropbegin) event will be fired, then a number of [`'dropText'`](#event-droptext) and/or [`'dropFile'`](#event-dropfile) events will be fired, corresponding to the contents of the drop, then finally the [`'dropComplete'`](#event-dropcomplete) event will be fired.
640
662
 
641
663
  ### Event: 'dropText'
642
664
 
@@ -1378,7 +1400,7 @@ Sample output:
1378
1400
  ]
1379
1401
  ```
1380
1402
 
1381
- ### sdl.joystick.openDevice(device[, options])
1403
+ ### sdl.joystick.openDevice(device)
1382
1404
 
1383
1405
  * `device: <object>` An object from [`sdl.joystick.devices`](#sdljoystickdevices) that should be opened.
1384
1406
  * Returns: [`<joystickInstance>`](#class-joystickinstance) an object representing the opened joystick device instance.
@@ -1565,7 +1587,8 @@ With a pure joystick instance it's impossible to know which axis or button corre
1565
1587
 
1566
1588
  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).
1567
1589
  For a joystick device to also be available as a controller device it needs a "mapping".
1568
- 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. See the sample output [`here`](#sdlcontrollerdevices) for an example.
1590
+ 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.
1591
+ See the sample output [`here`](#sdlcontrollerdevices) for an example.
1569
1592
  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.
1570
1593
  You can add them via:
1571
1594
  ```js
@@ -1576,6 +1599,8 @@ const mappings = text.split('\n')
1576
1599
  sdl.controller.addMappings(mappings)
1577
1600
  ```
1578
1601
 
1602
+ <a id="controller-event-device-add"></a>
1603
+
1579
1604
  ### Event: 'deviceAdd'
1580
1605
 
1581
1606
  * `device: <object>`: An object from [`sdl.controller.devices`](#sdlcontrollerdevices) indicating the device that caused the event.
@@ -1632,7 +1657,7 @@ Sample output:
1632
1657
  ]
1633
1658
  ```
1634
1659
 
1635
- ### sdl.controller.openDevice(device[, options])
1660
+ ### sdl.controller.openDevice(device)
1636
1661
 
1637
1662
  * `device: <object>` An object from [`sdl.controller.devices`](#sdlcontrollerdevices) that should be opened.
1638
1663
  * Returns: [`<ControllerInstance>`](#class-controllerinstance) an object representing the opened controller device instance.
@@ -2215,7 +2240,8 @@ You will need to have `cmake` installed for that to work.
2215
2240
 
2216
2241
  ---
2217
2242
 
2218
- You could also have found your way to the "Building from source" section because you are trying to contribute to this package. There are some npm scripts in `package.json` that could be of use to you:
2243
+ You could also have found your way to the "Building from source" section because you are trying to contribute to this package.
2244
+ There are some npm scripts in `package.json` that could be of use to you:
2219
2245
  * `npm run clean` deletes all folders that are created during the build, as well as `node_modules`.
2220
2246
  * `npm run download-release` downloads the prebuilt binaries. This is the first thing the install script tries to do.
2221
2247
  * `npm run download-sdl` downloads the SDL headers and libraries from `@kmamal/build-sdl` so you can compile against them in later steps. This is the second step in the install script, after `download-release` has failed.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.6.6",
2
+ "version": "0.6.8",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -7,6 +7,10 @@
7
7
  "sdl2",
8
8
  "gui",
9
9
  "desktop",
10
+ "canvas",
11
+ "webgl",
12
+ "opengl",
13
+ "webgpu",
10
14
  "window",
11
15
  "screen",
12
16
  "video",
@@ -20,6 +24,7 @@
20
24
  "audio",
21
25
  "sound",
22
26
  "speaker",
27
+ "microphone",
23
28
  "mic",
24
29
  "clipboard",
25
30
  "game"
package/scripts/build.mjs CHANGED
@@ -12,8 +12,8 @@ await Promise.all([
12
12
  }))
13
13
 
14
14
  console.log("build in", C.dir.build)
15
- process.env.SDL_INC = Path.join(C.dir.sdl, 'include')
16
- process.env.SDL_LIB = Path.join(C.dir.sdl, 'lib')
15
+ const SDL_INC = Path.join(C.dir.sdl, 'include')
16
+ const SDL_LIB = Path.join(C.dir.sdl, 'lib')
17
17
 
18
18
  let archFlag = ''
19
19
  if (process.env.CROSS_COMPILE_ARCH) {
@@ -21,7 +21,14 @@ if (process.env.CROSS_COMPILE_ARCH) {
21
21
  }
22
22
 
23
23
  process.chdir(C.dir.root)
24
- execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, { stdio: 'inherit' })
24
+ execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, {
25
+ stdio: 'inherit',
26
+ env: {
27
+ ...process.env,
28
+ SDL_INC,
29
+ SDL_LIB,
30
+ },
31
+ })
25
32
 
26
33
  console.log("install to", C.dir.dist)
27
34
  await Fs.promises.rm(C.dir.dist, { recursive: true }).catch(() => {})
@@ -32,11 +39,11 @@ await Promise.all([
32
39
  Path.join(C.dir.dist, 'sdl.node'),
33
40
  ),
34
41
  (async () => {
35
- const libs = await Fs.promises.readdir(process.env.SDL_LIB)
42
+ const libs = await Fs.promises.readdir(SDL_LIB)
36
43
  await Promise.all(libs.map(async (name) => {
37
44
  if (C.platform === 'win32' && name !== 'SDL2.dll') { return }
38
45
  await Fs.promises.cp(
39
- Path.join(process.env.SDL_LIB, name),
46
+ Path.join(SDL_LIB, name),
40
47
  Path.join(C.dir.dist, name),
41
48
  { verbatimSymlinks: true },
42
49
  )
@@ -3,7 +3,9 @@ if (!process.env.NODE_SDL_FROM_SOURCE) {
3
3
  try {
4
4
  await import('./download-release.mjs')
5
5
  process.exit(0)
6
- } catch (error) {}
6
+ } catch (error) {
7
+ console.log("failed to download release")
8
+ }
7
9
  } else {
8
10
  console.log("skip download and build from source")
9
11
  }
@@ -2,9 +2,12 @@ import { execSync } from 'node:child_process'
2
2
 
3
3
  await import('./clean.mjs')
4
4
 
5
- process.env.NODE_SDL_FROM_SOURCE = 1
6
5
  execSync('npm install', {
7
6
  stdio: 'inherit',
7
+ env: {
8
+ ...process.env,
9
+ NODE_SDL_FROM_SOURCE: 1,
10
+ },
8
11
  })
9
12
 
10
13
  await import('./upload-release.mjs')
@@ -36,12 +36,16 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
36
36
  currDevice = currList[++currIndex]
37
37
  } else if (cmp < 0) {
38
38
  mainList.splice(mainIndex, 1)
39
- emitter.emit('deviceRemove', { device: mainDevice })
39
+ const type = 'deviceRemove'
40
+ const event = { type, device: mainDevice }
41
+ emitter.emit(type, event)
40
42
  mainDevice = mainList[mainIndex]
41
43
  } else {
42
44
  mainList.splice(mainIndex, 0, currDevice)
43
45
  mainDevice = mainList[++mainIndex]
44
- emitter.emit('deviceAdd', { device: currDevice })
46
+ const type = 'deviceAdd'
47
+ const event = { type, device: currDevice }
48
+ emitter.emit(type, event)
45
49
  currDevice = currList[++currIndex]
46
50
  }
47
51
  }
@@ -49,12 +53,16 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
49
53
  if (mainIndex < mainList.length) {
50
54
  while (mainIndex < mainList.length) {
51
55
  [ mainDevice ] = mainList.splice(mainIndex, 1)
52
- emitter.emit('deviceRemove', { device: mainDevice })
56
+ const type = 'deviceRemove'
57
+ const event = { type, device: mainDevice }
58
+ emitter.emit(type, event)
53
59
  }
54
60
  } else {
55
61
  while (currIndex < currList.length) {
56
62
  mainList.push(currDevice)
57
- emitter.emit('deviceAdd', { device: currDevice })
63
+ const type = 'deviceAdd'
64
+ const event = { type, device: currDevice }
65
+ emitter.emit(type, event)
58
66
  currDevice = currList[++currIndex]
59
67
  }
60
68
  }
@@ -67,7 +75,7 @@ const handleEvents = () => {
67
75
  const { family, type } = event
68
76
  if (family === undefined || type === undefined) { continue }
69
77
  delete event.family
70
- delete event.type
78
+ event.type = Enums.eventTypeNames[type]
71
79
 
72
80
  switch (family) {
73
81
  case Enums.eventFamily.app: {
@@ -140,25 +148,23 @@ const handleEvents = () => {
140
148
  case Enums.eventType.close: {
141
149
  let shouldPrevent = false
142
150
  const prevent = () => { shouldPrevent = true }
143
- window.emit('beforeClose', { prevent })
151
+ const type2 = 'beforeClose'
152
+ const event2 = { type: type2, prevent }
153
+ window.emit(type2, event2)
144
154
  if (shouldPrevent) { continue }
145
155
  } break
146
156
 
147
157
  default: continue
148
158
  }
149
159
 
150
- if (Object.keys(event).length > 0) {
151
- window.emit(Enums.eventTypeNames[type], event)
152
- } else {
153
- window.emit(Enums.eventTypeNames[type])
154
- }
160
+ window.emit(event.type, event)
155
161
 
156
162
  if (type === Enums.eventType.close) { window.destroy() }
157
163
  } break
158
164
 
159
165
  case Enums.eventFamily.keyboard: {
160
166
  const { windowId } = event
161
- delete event.window
167
+ delete event.windowId
162
168
 
163
169
  const window = Globals.windows.all.get(windowId)
164
170
  if (!window) { continue }
@@ -166,11 +172,7 @@ const handleEvents = () => {
166
172
  const { key } = event
167
173
  event.key = mapping[key] ?? (key?.length === 1 ? key : null)
168
174
 
169
- if (Object.keys(event).length > 0) {
170
- window.emit(Enums.eventTypeNames[type], event)
171
- } else {
172
- window.emit(Enums.eventTypeNames[type])
173
- }
175
+ window.emit(event.type, event)
174
176
  } break
175
177
 
176
178
  case Enums.eventFamily.mouse:
@@ -182,11 +184,7 @@ const handleEvents = () => {
182
184
  const window = Globals.windows.all.get(windowId)
183
185
  if (!window) { continue }
184
186
 
185
- if (Object.keys(event).length > 0) {
186
- window.emit(Enums.eventTypeNames[type], event)
187
- } else {
188
- window.emit(Enums.eventTypeNames[type])
189
- }
187
+ window.emit(event.type, event)
190
188
  } break
191
189
 
192
190
  case Enums.eventFamily.joystickDevice: {
@@ -249,7 +247,7 @@ const handleEvents = () => {
249
247
  case Enums.eventType.axisMotion: {
250
248
  for (const joystickInstance of collection) {
251
249
  joystickInstance._axes[event.axis] = event.value
252
- joystickInstance.emit('axisMotion', event)
250
+ joystickInstance.emit(event.type, event)
253
251
  }
254
252
  } break
255
253
 
@@ -258,20 +256,20 @@ const handleEvents = () => {
258
256
  const ball = joystickInstance._balls[event.ball]
259
257
  ball.x = event.x
260
258
  ball.y = event.y
261
- joystickInstance.emit('ballMotion', event)
259
+ joystickInstance.emit(event.type, event)
262
260
  }
263
261
  } break
264
262
 
265
263
  case Enums.eventType.buttonDown: {
266
264
  for (const joystickInstance of collection) {
267
265
  joystickInstance._buttons[event.button] = true
268
- joystickInstance.emit('buttonDown', event)
266
+ joystickInstance.emit(event.type, event)
269
267
  }
270
268
  } break
271
269
  case Enums.eventType.buttonUp: {
272
270
  for (const joystickInstance of collection) {
273
271
  joystickInstance._buttons[event.button] = false
274
- joystickInstance.emit('buttonUp', event)
272
+ joystickInstance.emit(event.type, event)
275
273
  }
276
274
  } break
277
275
 
@@ -279,7 +277,7 @@ const handleEvents = () => {
279
277
  event.value = Enums.hatPositionNames[event.value]
280
278
  for (const joystickInstance of collection) {
281
279
  joystickInstance._hats[event.hat] = event.value
282
- joystickInstance.emit('hatMotion', event)
280
+ joystickInstance.emit(event.type, event)
283
281
  }
284
282
  } break
285
283
 
@@ -299,7 +297,7 @@ const handleEvents = () => {
299
297
  event.axis = Enums.controllerAxisNames[event.axis]
300
298
  for (const controllerInstance of collection.values()) {
301
299
  controllerInstance._axes[event.axis] = event.value
302
- controllerInstance.emit('axisMotion', event)
300
+ controllerInstance.emit(event.type, event)
303
301
  }
304
302
  } break
305
303
 
@@ -307,7 +305,7 @@ const handleEvents = () => {
307
305
  event.button = Enums.controllerButtonNames[event.button]
308
306
  for (const controllerInstance of collection.values()) {
309
307
  controllerInstance._axes[event.button] = true
310
- controllerInstance.emit('buttonDown', event)
308
+ controllerInstance.emit(event.type, event)
311
309
  }
312
310
  } break
313
311
 
@@ -315,7 +313,7 @@ const handleEvents = () => {
315
313
  event.button = Enums.controllerButtonNames[event.button]
316
314
  for (const controllerInstance of collection.values()) {
317
315
  controllerInstance._axes[event.button] = true
318
- controllerInstance.emit('buttonUp', event)
316
+ controllerInstance.emit(event.type, event)
319
317
  }
320
318
  } break
321
319
 
@@ -327,7 +325,7 @@ const handleEvents = () => {
327
325
  for (const controllerInstance of collection.values()) {
328
326
  Object.assign(controllerInstance._axes, axes)
329
327
  Object.assign(controllerInstance._buttons, buttons)
330
- controllerInstance.emit('remap', event)
328
+ controllerInstance.emit(event.type, event)
331
329
  }
332
330
  } break
333
331
 
@@ -363,7 +361,7 @@ const handleEvents = () => {
363
361
  } break
364
362
 
365
363
  case Enums.eventFamily.clipboard: {
366
- clipboardModule.emit(Enums.eventTypeNames[type])
364
+ clipboardModule.emit(event.type, event)
367
365
  } break
368
366
 
369
367
  default: continue
@@ -1,19 +1,22 @@
1
1
  const Globals = require('../globals')
2
2
  const { sdl } = require('../sdl')
3
3
 
4
+
4
5
  const maybeTriggerQuit = () => {
5
6
  if (Globals.windows.all.size > 0) { return }
6
7
 
7
8
  let shouldPrevent = false
8
9
  const prevent = () => { shouldPrevent = true }
9
10
 
10
- const type2 = 'beforeQuit'
11
- const event2 = { prevent }
12
- sdl.emit(type2, event2)
11
+ const type1 = 'beforeQuit'
12
+ const event1 = { type: type1, prevent }
13
+ sdl.emit(type1, event1)
13
14
 
14
15
  if (shouldPrevent) { return }
15
16
 
16
- sdl.emit('quit')
17
+ const type2 = 'quit'
18
+ const event2 = { type: type2 }
19
+ sdl.emit(type2, event2)
17
20
  process.exit()
18
21
  }
19
22
 
@@ -122,7 +122,7 @@ class Window extends EventsViaPoll {
122
122
 
123
123
  // This also keeps Node.js alive while windows are open
124
124
  this.on('close', () => {
125
- this.removeAllListeners()
125
+ process.nextTick(() => { this.removeAllListeners() })
126
126
  })
127
127
 
128
128
  // Manually emit an initial resize event for convenience
@@ -1,17 +1,18 @@
1
1
 
2
2
  export namespace Events {
3
3
 
4
- interface BaseEvent {}
4
+ interface BaseEvent { readonly type: string }
5
5
 
6
6
  type PreventCallback = () => void
7
7
 
8
8
  namespace App {
9
9
 
10
10
  interface BeforeQuit extends BaseEvent {
11
+ readonly type: 'beforeQuit'
11
12
  readonly prevent: PreventCallback
12
13
  }
13
14
 
14
- // interface Quit extends BaseEvent {}
15
+ interface Quit extends BaseEvent { readonly type: 'quit' }
15
16
 
16
17
  }
17
18
 
@@ -31,65 +32,75 @@ export namespace Events {
31
32
  readonly numlock: number
32
33
  }
33
34
 
35
+ interface KeyDownEvent extends KeyEvent {
36
+ readonly type: 'keyDown'
37
+ readonly repeat: number
38
+ }
39
+ interface KeyUpEvent extends KeyEvent { readonly type: 'keyUp' }
40
+
41
+ interface TextInput extends WindowEvent {
42
+ readonly type: 'textInput'
43
+ readonly text: string
44
+ }
45
+
34
46
  interface MouseEvent extends WindowEvent {
35
47
  readonly x: number
36
48
  readonly y: number
37
49
  readonly touch: boolean
38
50
  }
39
51
 
52
+ interface MouseMoveEvent extends MouseEvent { readonly type: 'mouseMove' }
53
+
40
54
  interface MouseButtonEvent extends MouseEvent {
41
55
  readonly button: number
42
56
  }
43
57
 
44
- // interface Show extends WindowEvent {}
45
- // interface Hide extends WindowEvent {}
46
- // interface Expose extends WindowEvent {}
47
- // interface Minimize extends WindowEvent {}
48
- // interface Maximize extends WindowEvent {}
49
- // interface Restore extends WindowEvent {}
58
+ interface MouseButtonDownEvent extends MouseButtonEvent { readonly type: 'mouseButtonDown' }
59
+ interface MouseButtonUpEvent extends MouseButtonEvent { readonly type: 'mouseButtonUp' }
60
+
61
+ interface MouseWheelEvent extends MouseEvent {
62
+ readonly type: 'flipped'
63
+ readonly dx: number
64
+ readonly dy: number
65
+ readonly flipped: boolean
66
+ }
67
+
68
+ interface Show extends WindowEvent { readonly type: 'show' }
69
+ interface Hide extends WindowEvent { readonly type: 'hide' }
70
+ interface Expose extends WindowEvent { readonly type: 'expose' }
71
+ interface Minimize extends WindowEvent { readonly type: 'minimize' }
72
+ interface Maximize extends WindowEvent { readonly type: 'maximize' }
73
+ interface Restore extends WindowEvent { readonly type: 'restore' }
50
74
  interface Move extends WindowEvent {
75
+ readonly type: 'move'
51
76
  readonly x: number,
52
77
  readonly y: number
53
78
  }
54
79
  interface Resize extends WindowEvent {
80
+ readonly type: 'resize'
55
81
  readonly width: number,
56
82
  readonly height: number
57
83
  }
58
- // interface Focus extends WindowEvent {}
59
- // interface Blur extends WindowEvent {}
60
- // interface Hover extends WindowEvent {}
61
- // interface Leave extends WindowEvent {}
84
+ interface Focus extends WindowEvent { readonly type: 'focus' }
85
+ interface Blur extends WindowEvent { readonly type: 'blur' }
86
+ interface Hover extends WindowEvent { readonly type: 'hover' }
87
+ interface Leave extends WindowEvent { readonly type: 'leave' }
62
88
  interface BeforeClose extends WindowEvent {
89
+ readonly type: 'beforeClose'
63
90
  readonly prevent: PreventCallback
64
91
  }
65
- // interface Close extends WindowEvent {}
66
-
67
- interface KeyDown extends KeyEvent {
68
- readonly repeat: number
69
- }
70
- interface KeyUp extends KeyEvent {}
92
+ interface Close extends WindowEvent { readonly type: 'close' }
71
93
 
72
- interface TextInput extends WindowEvent {
73
- readonly text: string
74
- }
75
-
76
- interface MouseButtonDown extends MouseButtonEvent {}
77
- interface MouseButtonUp extends MouseButtonEvent {}
78
- interface MouseMove extends MouseEvent {}
79
- interface MouseWheel extends MouseEvent {
80
- readonly dx: number
81
- readonly dy: number
82
- readonly flipped: boolean
83
- }
84
-
85
- // interface DropBegin extends WindowEvent {}
94
+ interface DropBegin extends WindowEvent { readonly type: 'dropBegin' }
86
95
  interface DropText extends WindowEvent {
96
+ readonly type: 'dropText'
87
97
  readonly text: string
88
98
  }
89
99
  interface DropFile extends WindowEvent {
100
+ readonly type: 'dropFile'
90
101
  readonly file: string
91
102
  }
92
- // interface DropComplete extends WindowEvent {}
103
+ interface DropComplete extends WindowEvent { readonly type: 'dropComplete' }
93
104
 
94
105
  }
95
106
 
@@ -99,34 +110,37 @@ export namespace Events {
99
110
  readonly device: Sdl.Joystick.Device
100
111
  }
101
112
 
102
- interface DeviceAdd extends DeviceEvent {}
103
- interface DeviceRemove extends DeviceEvent {}
113
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
114
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
104
115
 
105
116
  interface JoystickEvent extends BaseEvent {}
106
117
 
107
118
  interface AxisMotion extends JoystickEvent {
108
- axis: number,
109
- value: number
119
+ readonly type: 'axisMotion'
120
+ readonly axis: number,
121
+ readonly value: number
110
122
  }
111
123
 
112
124
  interface BallMotion extends JoystickEvent {
113
- ball: number,
114
- x: number
115
- y: number
125
+ readonly type: 'ballMotion'
126
+ readonly ball: number,
127
+ readonly x: number
128
+ readonly y: number
116
129
  }
117
130
 
118
131
  interface ButtonEvent extends JoystickEvent {
119
- button: number
132
+ readonly button: number
120
133
  }
121
- interface ButtonDown extends ButtonEvent {}
122
- interface ButtonUp extends ButtonEvent {}
134
+ interface ButtonDown extends ButtonEvent { readonly type: 'buttonDown' }
135
+ interface ButtonUp extends ButtonEvent { readonly type: 'buttonUp' }
123
136
 
124
137
  interface HatMotion extends JoystickEvent {
125
- hat: number,
126
- value: Sdl.Joystick.HatPosition
138
+ readonly type: 'hatMotion'
139
+ readonly hat: number,
140
+ readonly value: Sdl.Joystick.HatPosition
127
141
  }
128
142
 
129
- // interface Close extends JoystickEvent {}
143
+ interface Close extends JoystickEvent { readonly type: 'close' }
130
144
 
131
145
  }
132
146
 
@@ -136,25 +150,26 @@ export namespace Events {
136
150
  readonly device: Sdl.Controller.Device
137
151
  }
138
152
 
139
- interface DeviceAdd extends DeviceEvent {}
140
- interface DeviceRemove extends DeviceEvent {}
153
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
154
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
141
155
 
142
156
  interface ControllerEvent extends BaseEvent {}
143
157
 
144
158
  interface AxisMotion extends ControllerEvent {
145
- axis: number,
146
- value: number
159
+ readonly type: 'axisMotion'
160
+ readonly axis: number,
161
+ readonly value: number
147
162
  }
148
163
 
149
164
  interface ButtonEvent extends ControllerEvent {
150
- button: number
165
+ readonly button: number
151
166
  }
152
- interface ButtonDown extends ButtonEvent {}
153
- interface ButtonUp extends ButtonEvent {}
167
+ interface ButtonDown extends ButtonEvent { readonly type: 'buttonDown' }
168
+ interface ButtonUp extends ButtonEvent { readonly type: 'buttonUp' }
154
169
 
155
- interface Remap extends ControllerEvent {}
170
+ interface Remap extends ControllerEvent { readonly type: 'remap' }
156
171
 
157
- // interface Close extends ControllerEvent {}
172
+ interface Close extends ControllerEvent { readonly type: 'close' }
158
173
 
159
174
  }
160
175
 
@@ -164,16 +179,17 @@ export namespace Events {
164
179
  readonly device: Sdl.Audio.Device
165
180
  }
166
181
 
167
- interface DeviceAdd extends DeviceEvent {}
168
- interface DeviceRemove extends DeviceEvent {}
182
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
183
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
184
+
185
+ interface AudioEvent extends BaseEvent {}
169
186
 
170
- // interface AudioEvent extends BaseEvent {}
171
- // interface Close extends AudioEvent {}
187
+ interface Close extends AudioEvent { readonly type: 'close' }
172
188
  }
173
189
 
174
190
  namespace Clipboard {
175
191
 
176
- interface Update extends BaseEvent {}
192
+ interface Update extends BaseEvent { readonly type: 'update' }
177
193
 
178
194
  }
179
195
  }
@@ -271,20 +287,20 @@ export namespace Sdl {
271
287
  }
272
288
 
273
289
  class Window {
274
- on (event: 'show', listener: () => void): this
275
- on (event: 'hide', listener: () => void): this
276
- on (event: 'expose', listener: () => void): this
277
- on (event: 'minimize', listener: () => void): this
278
- on (event: 'maximize', listener: () => void): this
279
- on (event: 'restore', listener: () => void): this
290
+ on (event: 'show', listener: (event: Events.Window.Show) => void): this
291
+ on (event: 'hide', listener: (event: Events.Window.Hide) => void): this
292
+ on (event: 'expose', listener: (event: Events.Window.Expose) => void): this
293
+ on (event: 'minimize', listener: (event: Events.Window.Minimize) => void): this
294
+ on (event: 'maximize', listener: (event: Events.Window.Maximize) => void): this
295
+ on (event: 'restore', listener: (event: Events.Window.Restore) => void): this
280
296
  on (event: 'move', listener: (event: Events.Window.Move) => void): this
281
297
  on (event: 'resize', listener: (event: Events.Window.Resize) => void): this
282
- on (event: 'focus', listener: () => void): this
283
- on (event: 'blur', listener: () => void): this
284
- on (event: 'hover', listener: () => void): this
285
- on (event: 'leave', listener: () => void): this
298
+ on (event: 'focus', listener: (event: Event.Window.Focus) => void): this
299
+ on (event: 'blur', listener: (event: Event.Window.Blur) => void): this
300
+ on (event: 'hover', listener: (event: Event.Window.Hover) => void): this
301
+ on (event: 'leave', listener: (event: Event.Window.Leave) => void): this
286
302
  on (event: 'beforeClose', listener: (event: Events.Window.BeforeClose) => void): this
287
- on (event: 'close', listener: () => void): this
303
+ on (event: 'close', listener: (event: Event.Window.Close) => void): this
288
304
  on (event: 'keyDown', listener: (event: Events.Window.KeyDown) => void): this
289
305
  on (event: 'keyUp', listener: (event: Events.Window.KeyUp) => void): this
290
306
  on (event: 'textInput', listener: (event: Events.Window.TextInput) => void): this
@@ -292,10 +308,10 @@ export namespace Sdl {
292
308
  on (event: 'mouseButtonUp', listener: (event: Events.Window.MouseButtonUp) => void): this
293
309
  on (event: 'mouseMove', listener: (event: Events.Window.MouseMove) => void): this
294
310
  on (event: 'mouseWheel', listener: (event: Events.Window.MouseWheel) => void): this
295
- on (event: 'dropBegin', listener: () => void): this
311
+ on (event: 'dropBegin', listener: (event: Event.Window.DropBegin) => void): this
296
312
  on (event: 'dropText', listener: (event: Events.Window.DropText) => void): this
297
313
  on (event: 'dropFile', listener: (event: Events.Window.DropFile) => void): this
298
- on (event: 'dropComplete', listener: () => void): this
314
+ on (event: 'dropComplete', listener: (event: Event.Window.DropComplete) => void): this
299
315
 
300
316
  readonly id: number
301
317
 
@@ -729,7 +745,7 @@ export namespace Sdl {
729
745
  on (event: 'buttonDown', listener: (event: Events.Joystick.ButtonDown) => void): this
730
746
  on (event: 'buttonUp', listener: (event: Events.Joystick.ButtonUp) => void): this
731
747
  on (event: 'hatMotion', listener: (event: Events.Joystick.HatMotion) => void): this
732
- on (event: 'close', listener: () => void): this
748
+ on (event: 'close', listener: (event: Events.Joystick.Close) => void): this
733
749
 
734
750
  readonly device: Device
735
751
  readonly firmwareVersion: number
@@ -787,7 +803,7 @@ export namespace Sdl {
787
803
  on (event: 'buttonDown', listener: (event: Events.Controller.ButtonDown) => void): this
788
804
  on (event: 'buttonUp', listener: (event: Events.Controller.ButtonUp) => void): this
789
805
  on (event: 'remap', listener: (event: Events.Controller.Remap) => void): this
790
- on (event: 'close', listener: () => void): this
806
+ on (event: 'close', listener: (event: Events.Controller.Close) => void): this
791
807
 
792
808
  readonly device: Device
793
809
  readonly firmwareVersion: number
@@ -890,7 +906,7 @@ export namespace Sdl {
890
906
  }
891
907
 
892
908
  class AudioInstance {
893
- on (event: 'close', listener: () => void): this
909
+ on (event: 'close', listener: (event: Events.Audio.Close) => void): this
894
910
 
895
911
  readonly id: number
896
912
  readonly device: Device
@@ -954,7 +970,7 @@ export namespace Sdl {
954
970
  namespace Clipboard {
955
971
 
956
972
  interface Module {
957
- on (event: 'update', listener: () => void): this
973
+ on (event: 'update', listener: (event: Events.Clipboard.Update) => void): this
958
974
 
959
975
  readonly text: string
960
976
  setText (text: string): void
@@ -964,7 +980,7 @@ export namespace Sdl {
964
980
 
965
981
  interface Module {
966
982
  on (event: 'beforeQuit', listener: (event: Events.App.BeforeQuit) => void): this
967
- on (event: 'quit', listener: () => void): this
983
+ on (event: 'quit', listener: (event: Events.App.Quit) => void): this
968
984
 
969
985
  readonly info: Info
970
986
  readonly video: Video.Module