@kmamal/sdl 0.11.12 → 1.0.0-2

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 +257 -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} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  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 +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -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 +79 -42
  31. package/src/javascript/joystick/shared.js +88 -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 +20 -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 +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  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 +415 -305
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  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,6 +1,7 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const Enums = require('../enums')
4
+ const { VideoFormatHelpers } = require('./format-helpers')
4
5
  const { EventsViaPoll } = require('../events/events-via-poll')
5
6
 
6
7
  const validEvents = [
@@ -29,6 +30,7 @@ const validEvents = [
29
30
  'fingerDown',
30
31
  'fingerUp',
31
32
  'fingerMove',
33
+ 'fingerCancel',
32
34
  'dropBegin',
33
35
  'dropText',
34
36
  'dropFile',
@@ -39,6 +41,8 @@ class Window extends EventsViaPoll {
39
41
  constructor (options = {}) {
40
42
  super(validEvents)
41
43
 
44
+ if (typeof options !== 'object' || options === null) { throw Object.assign(new Error("options must be an object"), { options }) }
45
+
42
46
  const {
43
47
  title = "",
44
48
  display = null,
@@ -49,56 +53,54 @@ class Window extends EventsViaPoll {
49
53
  resizable = false,
50
54
  borderless = false,
51
55
  alwaysOnTop = false,
52
- accelerated = true,
53
- vsync = true,
56
+ accelerated,
57
+ vsync,
54
58
  opengl = false,
55
59
  webgpu = false,
56
- skipTaskbar = false,
57
- popupMenu = false,
58
- tooltip = false,
59
- utility = false,
60
60
  } = options
61
61
 
62
+ const hasAccelerated = accelerated !== undefined
63
+ const hasVsync = vsync !== undefined
64
+
62
65
  if (typeof title !== 'string') { throw Object.assign(new Error("title must be a string"), { title }) }
63
66
  if (display !== null && typeof display !== 'object') { throw Object.assign(new Error("display must be an object"), { display }) }
64
67
  if (x !== null && !Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
68
+ if (x !== null && (x < -(2 ** 31) || x > 2 ** 31 - 1)) { throw Object.assign(new Error("x must be a 32-bit integer"), { x }) }
65
69
  if (y !== null && !Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
70
+ if (y !== null && (y < -(2 ** 31) || y > 2 ** 31 - 1)) { throw Object.assign(new Error("y must be a 32-bit integer"), { y }) }
66
71
  if (width !== null) {
67
72
  if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
68
- if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
73
+ if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
69
74
  }
70
75
  if (height !== null) {
71
76
  if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
72
- if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
77
+ if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
73
78
  }
74
79
  if (typeof visible !== 'boolean') { throw Object.assign(new Error("visible must be a boolean"), { visible }) }
75
80
  if (typeof fullscreen !== 'boolean') { throw Object.assign(new Error("fullscreen must be a boolean"), { fullscreen }) }
76
81
  if (typeof resizable !== 'boolean') { throw Object.assign(new Error("resizable must be a boolean"), { resizable }) }
77
82
  if (typeof borderless !== 'boolean') { throw Object.assign(new Error("borderless must be a boolean"), { borderless }) }
78
83
  if (typeof alwaysOnTop !== 'boolean') { throw Object.assign(new Error("alwaysOnTop must be a boolean"), { alwaysOnTop }) }
79
- if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
80
- if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
84
+ if (hasAccelerated && typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
85
+ if (hasVsync && typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
81
86
  if (typeof opengl !== 'boolean') { throw Object.assign(new Error("opengl must be a boolean"), { opengl }) }
82
87
  if (typeof webgpu !== 'boolean') { throw Object.assign(new Error("webgpu must be a boolean"), { webgpu }) }
83
- if (typeof skipTaskbar !== 'boolean') { throw Object.assign(new Error("skipTaskbar must be a boolean"), { skipTaskbar }) }
84
- if (typeof popupMenu !== 'boolean') { throw Object.assign(new Error("popupMenu must be a boolean"), { popupMenu }) }
85
- if (typeof tooltip !== 'boolean') { throw Object.assign(new Error("tooltip must be a boolean"), { tooltip }) }
86
- if (typeof utility !== 'boolean') { throw Object.assign(new Error("utility must be a boolean"), { utility }) }
87
88
  if (display !== null && (x !== null || y !== null)) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
88
89
  if (resizable && borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless }) }
89
90
  if (opengl && webgpu) { throw Object.assign(new Error("opengl and webgpu are mutually exclusive"), { opengl, webgpu }) }
91
+ if (opengl && (hasAccelerated || hasVsync)) { throw Object.assign(new Error("opengl and accelerated/vsync are mutually exclusive"), { opengl, accelerated, vsync }) }
92
+ if (webgpu && (hasAccelerated || hasVsync)) { throw Object.assign(new Error("webgpu and accelerated/vsync are mutually exclusive"), { webgpu, accelerated, vsync }) }
90
93
 
91
- let displayIndex = 0
94
+ let displayId = 0
92
95
  if (display) {
93
- const { name } = display
94
- const displays = Bindings.video_getDisplays()
95
- const index = displays.findIndex((a) => a.name === name)
96
- displayIndex = Math.max(0, index)
96
+ Globals.events.poll()
97
+ if (!Globals.displays.includes(display)) { throw Object.assign(new Error("display must be from sdl.video.displays"), { display }) }
98
+ displayId = display.id
97
99
  }
98
100
 
99
101
  const result = Bindings.window_create(
100
102
  title,
101
- displayIndex,
103
+ displayId,
102
104
  x,
103
105
  y,
104
106
  width,
@@ -108,14 +110,10 @@ class Window extends EventsViaPoll {
108
110
  resizable,
109
111
  borderless,
110
112
  alwaysOnTop,
111
- accelerated,
112
- vsync,
113
+ accelerated ?? true,
114
+ vsync ?? true,
113
115
  opengl,
114
116
  webgpu,
115
- skipTaskbar,
116
- popupMenu,
117
- tooltip,
118
- utility,
119
117
  )
120
118
 
121
119
  this._id = result.id
@@ -125,7 +123,7 @@ class Window extends EventsViaPoll {
125
123
  this._height = result.height
126
124
  this._pixelWidth = result.pixelWidth
127
125
  this._pixelHeight = result.pixelHeight
128
- this._displayIndex = result.displayIndex
126
+ this._displayId = result.displayId
129
127
  this._fullscreen = result.fullscreen
130
128
  this._resizable = result.resizable
131
129
  this._borderless = result.borderless
@@ -133,10 +131,6 @@ class Window extends EventsViaPoll {
133
131
  this._accelerated = result.accelerated
134
132
  this._vsync = result.vsync
135
133
  this._native = result.native
136
- this._skipTaskbar = result.skipTaskbar
137
- this._popupMenu = result.popupMenu
138
- this._tooltip = result.tooltip
139
- this._utility = result.utility
140
134
 
141
135
  this._title = title
142
136
  this._visible = visible
@@ -145,32 +139,47 @@ class Window extends EventsViaPoll {
145
139
 
146
140
  this._minimized = false
147
141
  this._maximized = false
142
+ this._relativeMouseMode = false
148
143
  this._destroyed = false
149
144
 
150
145
  Globals.windows.all.set(this._id, this)
151
146
 
152
- // This also keeps Node.js alive while windows are open
153
- this.on('close', () => {
154
- if (Globals.windows.hovered === this) { Globals.windows.hovered = null }
155
- if (Globals.windows.focused === this) { Globals.windows.focused = null }
156
-
157
- Bindings.window_destroy(this._id)
158
- this._destroyed = true
159
-
160
- Globals.windows.all.delete(this._id)
161
-
162
- process.nextTick(() => { this.removeAllListeners() })
147
+ // Keeps Node.js alive while windows are open
148
+ const keepAlive = () => {}
149
+ this.on('close', keepAlive)
150
+ this.on('removeListener', (type, listener) => {
151
+ if (listener === keepAlive && !this._destroyed) { this.on('close', keepAlive) }
163
152
  })
164
153
 
165
- // Manually emit an initial resize event for convenience
154
+ // Manually emit some initial events for convenience
166
155
  process.nextTick(() => {
167
- this.emit('resize', {
168
- width: this._width,
169
- height: this._height,
170
- pixelWidth: this._pixelWidth,
171
- pixelHeight: this._pixelHeight,
172
- type: 'resize',
173
- })
156
+ if (this._destroyed) { return }
157
+ try {
158
+ this.emit('move', {
159
+ x: this._x,
160
+ y: this._y,
161
+ type: 'move',
162
+ })
163
+ }
164
+ catch (error) { this.emit('error', error) }
165
+ try {
166
+ this.emit('resize', {
167
+ width: this._width,
168
+ height: this._height,
169
+ pixelWidth: this._pixelWidth,
170
+ pixelHeight: this._pixelHeight,
171
+ type: 'resize',
172
+ })
173
+ }
174
+ catch (error) { this.emit('error', error) }
175
+ if (this._visible) {
176
+ try {
177
+ this.emit('expose', {
178
+ type: 'expose',
179
+ })
180
+ }
181
+ catch (error) { this.emit('error', error) }
182
+ }
174
183
  })
175
184
  }
176
185
 
@@ -186,67 +195,96 @@ class Window extends EventsViaPoll {
186
195
  this._title = title
187
196
  }
188
197
 
189
- get x () { return this._x }
190
- get y () { return this._y }
198
+ get x () {
199
+ Globals.events.poll()
200
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
201
+
202
+ return this._x
203
+ }
204
+
205
+ get y () {
206
+ Globals.events.poll()
207
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
208
+
209
+ return this._y
210
+ }
211
+
191
212
  setPosition (x, y) {
192
213
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
193
214
 
194
215
  if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
216
+ if (x < -(2 ** 31) || x > 2 ** 31 - 1) { throw Object.assign(new Error("x must be a 32-bit integer"), { x }) }
195
217
  if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
218
+ if (y < -(2 ** 31) || y > 2 ** 31 - 1) { throw Object.assign(new Error("y must be a 32-bit integer"), { y }) }
196
219
 
197
220
  Bindings.window_setPosition(this._id, x, y)
198
- this._x = x
199
- this._y = y
200
221
  }
201
222
 
202
- get width () { return this._width }
203
- get height () { return this._height }
223
+ get width () {
224
+ Globals.events.poll()
225
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
226
+
227
+ return this._width
228
+ }
229
+
230
+ get height () {
231
+ Globals.events.poll()
232
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
233
+
234
+ return this._height
235
+ }
236
+
204
237
  setSize (width, height) {
205
238
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
206
239
 
207
240
  if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
208
- if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
241
+ if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
209
242
  if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
210
- if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
243
+ if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
211
244
 
212
- const { pixelWidth, pixelHeight } = Bindings.window_setSize(this._id, width, height)
213
- this._width = Math.floor(width)
214
- this._height = Math.floor(height)
215
- this._pixelWidth = pixelWidth
216
- this._pixelHeight = pixelHeight
245
+ Bindings.window_setSize(this._id, width, height, false)
217
246
  }
218
247
 
219
248
  setSizeInPixels (pixelWidth, pixelHeight) {
249
+ Globals.events.poll()
220
250
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
221
251
 
222
252
  if (!Number.isInteger(pixelWidth)) { throw Object.assign(new Error("pixelWidth must be an integer"), { pixelWidth }) }
223
- if (pixelWidth <= 0) { throw Object.assign(new Error("invalid pixelWidth"), { pixelWidth }) }
253
+ if (pixelWidth <= 0 || pixelWidth > 2 ** 31 - 1) { throw Object.assign(new Error("pixelWidth must be a positive 32-bit integer"), { pixelWidth }) }
224
254
  if (!Number.isInteger(pixelHeight)) { throw Object.assign(new Error("pixelHeight must be an integer"), { pixelHeight }) }
225
- if (pixelHeight <= 0) { throw Object.assign(new Error("invalid pixelHeight"), { pixelHeight }) }
255
+ if (pixelHeight <= 0 || pixelHeight > 2 ** 31 - 1) { throw Object.assign(new Error("pixelHeight must be a positive 32-bit integer"), { pixelHeight }) }
226
256
 
227
- const xRatio = this._width / this._pixelWidth
228
- const yRatio = this._height / this._pixelHeight
229
- const width = pixelWidth * xRatio
230
- const height = pixelHeight * yRatio
257
+ Bindings.window_setSize(this._id, pixelWidth, pixelHeight, true)
258
+ }
231
259
 
232
- if (!Number.isInteger(width)) { throw Object.assign(new Error(`pixelWidth must be a multiple of ${xRatio}`), { pixelWidth }) }
233
- if (!Number.isInteger(height)) { throw Object.assign(new Error(`pixelWidth must be a multiple of ${yRatio}`), { pixelWidth }) }
260
+ get pixelWidth () {
261
+ Globals.events.poll()
262
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
234
263
 
235
- Bindings.window_setSize(this._id, width, height)
236
- this._width = width
237
- this._height = height
238
- this._pixelWidth = pixelWidth
239
- this._pixelHeight = pixelHeight
264
+ return this._pixelWidth
240
265
  }
241
266
 
242
- get pixelWidth () { return this._pixelWidth }
243
- get pixelHeight () { return this._pixelHeight }
267
+ get pixelHeight () {
268
+ Globals.events.poll()
269
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
270
+
271
+ return this._pixelHeight
272
+ }
244
273
 
245
274
  get display () {
246
- return Globals.displays[this._displayIndex]
275
+ Globals.events.poll()
276
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
277
+
278
+ return Globals.displays.find((a) => a.id === this._displayId) ?? null
279
+ }
280
+
281
+ get visible () {
282
+ Globals.events.poll()
283
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
284
+
285
+ return this._visible
247
286
  }
248
287
 
249
- get visible () { return this._visible }
250
288
  show (show = true) {
251
289
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
252
290
 
@@ -258,36 +296,63 @@ class Window extends EventsViaPoll {
258
296
 
259
297
  hide () { this.show(false) }
260
298
 
261
- get fullscreen () { return this._fullscreen }
299
+ get fullscreen () {
300
+ Globals.events.poll()
301
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
302
+
303
+ return this._fullscreen
304
+ }
305
+
262
306
  setFullscreen (fullscreen) {
263
307
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
264
308
 
265
309
  if (typeof fullscreen !== 'boolean') { throw Object.assign(new Error("fullscreen must be a boolean"), { fullscreen }) }
266
310
 
267
- this._fullscreen = Bindings.window_setFullscreen(this._id, fullscreen)
311
+ Bindings.window_setFullscreen(this._id, fullscreen)
312
+ }
313
+
314
+ get resizable () {
315
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
316
+
317
+ return this._resizable
268
318
  }
269
319
 
270
- get resizable () { return this._resizable }
271
320
  setResizable (resizable) {
272
321
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
273
322
 
274
323
  if (typeof resizable !== 'boolean') { throw Object.assign(new Error("resizable must be a boolean"), { resizable }) }
324
+ if (resizable && this._borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless: this._borderless }) }
275
325
 
276
326
  this._resizable = Bindings.window_setResizable(this._id, resizable)
277
327
  }
278
328
 
279
- get borderless () { return this._borderless }
329
+ get borderless () {
330
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
331
+
332
+ return this._borderless
333
+ }
334
+
280
335
  setBorderless (borderless) {
281
336
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
282
337
 
283
338
  if (typeof borderless !== 'boolean') { throw Object.assign(new Error("borderless must be a boolean"), { borderless }) }
339
+ if (borderless && this._resizable) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable: this._resizable, borderless }) }
284
340
 
285
341
  this._borderless = Bindings.window_setBorderless(this._id, borderless)
286
342
  }
287
343
 
288
- get alwaysOnTop () { return this._alwaysOnTop }
344
+ get alwaysOnTop () {
345
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
346
+
347
+ return this._alwaysOnTop
348
+ }
349
+
350
+ get accelerated () {
351
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
352
+
353
+ return this._accelerated
354
+ }
289
355
 
290
- get accelerated () { return this._accelerated }
291
356
  setAccelerated (accelerated) {
292
357
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
293
358
 
@@ -301,7 +366,12 @@ class Window extends EventsViaPoll {
301
366
  this._vsync = result.vsync
302
367
  }
303
368
 
304
- get vsync () { return this._vsync }
369
+ get vsync () {
370
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
371
+
372
+ return this._vsync
373
+ }
374
+
305
375
  setVsync (vsync) {
306
376
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
307
377
 
@@ -315,56 +385,100 @@ class Window extends EventsViaPoll {
315
385
  this._vsync = result.vsync
316
386
  }
317
387
 
318
- get opengl () { return this._opengl }
319
- get webgpu () { return this._webgpu }
320
- get native () { return this._native }
388
+ get opengl () {
389
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
390
+
391
+ return this._opengl
392
+ }
393
+
394
+ get webgpu () {
395
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
396
+
397
+ return this._webgpu
398
+ }
399
+
400
+ get native () {
401
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
402
+
403
+ return this._native
404
+ }
405
+
406
+ get minimized () {
407
+ Globals.events.poll()
408
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
409
+
410
+ return this._minimized
411
+ }
321
412
 
322
- get minimized () { return this._minimized }
323
413
  minimize () {
324
414
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
325
415
 
326
416
  Bindings.window_minimize(this._id)
327
- this._visible = false
328
- this._minimized = true
329
- this._maximized = false
330
- this._fullscreen = false
331
417
  }
332
418
 
333
- get maximized () { return this._maximized }
419
+ get maximized () {
420
+ Globals.events.poll()
421
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
422
+
423
+ return this._maximized
424
+ }
425
+
334
426
  maximize () {
335
427
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
336
428
 
429
+ if (!this._resizable) { throw new Error("can't maximize a non-resizable window") }
430
+
337
431
  Bindings.window_maximize(this._id)
338
- this._visible = true
339
- this._minimized = false
340
- this._maximized = true
341
- this._fullscreen = false
342
432
  }
343
433
 
344
434
  restore () {
345
435
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
346
436
 
347
437
  Bindings.window_restore(this._id)
348
- this._visible = true
349
- this._minimized = false
350
- this._maximized = false
351
- this._fullscreen = false
352
438
  }
353
439
 
354
- get focused () { return Globals.windows.focused === this }
440
+ get focused () {
441
+ Globals.events.poll()
442
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
443
+
444
+ return Globals.windows.focused === this
445
+ }
446
+
355
447
  focus () {
356
448
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
357
449
 
358
450
  Bindings.window_focus(this._id)
359
- Globals.windows.focused = this
360
451
  }
361
452
 
362
- get hovered () { return Globals.windows.hovered === this }
453
+ get hovered () {
454
+ Globals.events.poll()
455
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
456
+
457
+ return Globals.windows.hovered === this
458
+ }
459
+
460
+ get mouseCaptured () {
461
+ Globals.events.poll()
462
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
363
463
 
364
- get skipTaskbar () { return this._skipTaskbar }
365
- get popupMenu () { return this._popupMenu }
366
- get tooltip () { return this._tooltip }
367
- get utility () { return this._utility }
464
+ return Bindings.window_getMouseCaptured(this._id)
465
+ }
466
+
467
+ get relativeMouseMode () {
468
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
469
+
470
+ return this._relativeMouseMode
471
+ }
472
+
473
+ setRelativeMouseMode (relative = true) {
474
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
475
+
476
+ if (typeof relative !== 'boolean') { throw Object.assign(new Error("relative must be a boolean"), { relative }) }
477
+
478
+ this._relativeMouseMode = Bindings.window_setRelativeMouseMode(this._id, relative)
479
+ }
480
+
481
+ unsetRelativeMouseMode () { this.setRelativeMouseMode(false) }
368
482
 
369
483
  render (width, height, stride, format, buffer, options = {}) {
370
484
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
@@ -372,37 +486,42 @@ class Window extends EventsViaPoll {
372
486
  if (this._opengl) { throw new Error("can't call render in opengl mode") }
373
487
  if (this._webgpu) { throw new Error("can't call render in webgpu mode") }
374
488
 
489
+ if (typeof options !== 'object' || options === null) { throw Object.assign(new Error("options must be an object"), { options }) }
490
+
375
491
  const {
376
492
  scaling = 'nearest',
377
493
  dstRect = null,
378
494
  } = options
379
495
 
380
496
  if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
381
- if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
497
+ if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
382
498
  if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
383
- if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
499
+ if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
384
500
  if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
385
- if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
386
501
  if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
502
+ const helpers = VideoFormatHelpers[format]
503
+ if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
504
+ if (!helpers.isRenderable) { throw Object.assign(new Error("format can't be rendered"), { format }) }
505
+ if (stride < helpers.minStride(width)) { throw Object.assign(new Error("stride is smaller than expected"), { stride, width, format }) }
506
+ if (stride > 2 ** 31 - 1) { throw Object.assign(new Error("stride must be a 32-bit integer"), { stride }) }
387
507
  if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
388
- if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
389
- if (scaling !== undefined && typeof scaling !== 'string') { throw Object.assign(new Error("scaling must be a string"), { scaling }) }
508
+ if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
509
+ if (typeof scaling !== 'string') { throw Object.assign(new Error("scaling must be a string"), { scaling }) }
390
510
 
391
511
  if (dstRect !== null) {
392
512
  if (typeof dstRect !== 'object') { throw Object.assign(new Error("dstRect must be an object"), { dstRect }) }
393
- if (!Number.isInteger(dstRect.x)) { throw Object.assign(new Error("dstRect.x must be an integer"), { dstRect }) }
394
- if (!Number.isInteger(dstRect.y)) { throw Object.assign(new Error("dstRect.y must be an integer"), { dstRect }) }
395
- if (!Number.isInteger(dstRect.width)) { throw Object.assign(new Error("dstRect.width must be an integer"), { dstRect }) }
396
- if (dstRect.width <= 0) { throw Object.assign(new Error("invalid dstRect.width"), { dstRect }) }
397
- if (!Number.isInteger(dstRect.height)) { throw Object.assign(new Error("dstRect.height must be an integer"), { dstRect }) }
398
- if (dstRect.height <= 0) { throw Object.assign(new Error("invalid dstRect.height"), { dstRect }) }
513
+ if (!Number.isFinite(dstRect.x)) { throw Object.assign(new Error("dstRect.x must be a number"), { dstRect }) }
514
+ if (!Number.isFinite(dstRect.y)) { throw Object.assign(new Error("dstRect.y must be a number"), { dstRect }) }
515
+ if (!Number.isFinite(dstRect.width)) { throw Object.assign(new Error("dstRect.width must be a number"), { dstRect }) }
516
+ if (dstRect.width <= 0) { throw Object.assign(new Error("dstRect.width must be positive"), { dstRect }) }
517
+ if (!Number.isFinite(dstRect.height)) { throw Object.assign(new Error("dstRect.height must be a number"), { dstRect }) }
518
+ if (dstRect.height <= 0) { throw Object.assign(new Error("dstRect.height must be positive"), { dstRect }) }
399
519
  }
400
520
 
401
521
  const _format = Enums.pixelFormat[format]
402
- if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
403
522
 
404
523
  const _scaling = Enums.scaleMode[scaling]
405
- if (_scaling === undefined) { throw Object.assign(new Error("invalid scaling"), { scaling }) }
524
+ if (_scaling === undefined) { throw Object.assign(new Error("scaling must be 'nearest' or 'linear'"), { scaling }) }
406
525
 
407
526
  Bindings.window_render(this._id, width, height, stride, _format, buffer, _scaling, dstRect)
408
527
  }
@@ -411,17 +530,20 @@ class Window extends EventsViaPoll {
411
530
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
412
531
 
413
532
  if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
414
- if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
533
+ if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
415
534
  if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
416
- if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
535
+ if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
417
536
  if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
418
- if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
419
537
  if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
538
+ const helpers = VideoFormatHelpers[format]
539
+ if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
540
+ if (helpers.isYuv) { throw Object.assign(new Error("format must be an RGB format"), { format }) }
541
+ if (stride < helpers.minStride(width)) { throw Object.assign(new Error("stride is smaller than expected"), { stride, width, format }) }
542
+ if (stride > 2 ** 31 - 1) { throw Object.assign(new Error("stride must be a 32-bit integer"), { stride }) }
420
543
  if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
421
- if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
544
+ if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
422
545
 
423
546
  const _format = Enums.pixelFormat[format]
424
- if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
425
547
 
426
548
  Bindings.window_setIcon(this._id, width, height, stride, _format, buffer)
427
549
  }
@@ -444,19 +566,43 @@ class Window extends EventsViaPoll {
444
566
  destroy () {
445
567
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
446
568
 
447
- this.emit('close', { type: 'close' })
569
+ if (Globals.windows.hovered === this) { Globals.windows.hovered = null }
570
+ if (Globals.windows.focused === this) { Globals.windows.focused = null }
571
+
572
+ // Defer the native destruction until the pump has unwound.
573
+ if (Bindings.events_isDispatchingFromWatch()) {
574
+ process.nextTick(() => { Bindings.window_destroy(this._id) })
575
+ }
576
+ else {
577
+ Bindings.window_destroy(this._id)
578
+ }
579
+ this._destroyed = true
580
+
581
+ Globals.windows.all.delete(this._id)
582
+
583
+ // We might be inside an event listener
584
+ this._retire(() => Object.assign(new Error("window is destroyed"), { id: this._id }))
585
+
586
+ try { this.emit('close', { type: 'close' }) }
587
+ catch (error) { this.emit('error', error) }
448
588
  }
449
589
 
450
590
  destroyGently () {
451
591
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
452
592
 
453
593
  let shouldPrevent = false
454
- this.emit('beforeClose', {
455
- type: 'beforeClose',
456
- prevent: () => { shouldPrevent = true },
457
- })
594
+ try {
595
+ this.emit('beforeClose', {
596
+ type: 'beforeClose',
597
+ prevent: () => { shouldPrevent = true },
598
+ })
599
+ }
600
+ catch (error) { this.emit('error', error) }
458
601
  if (shouldPrevent) { return }
459
602
 
603
+ // A listener may have already destroyed the window
604
+ if (this._destroyed) { return }
605
+
460
606
  this.destroy()
461
607
  }
462
608
  }