@kmamal/sdl 0.11.13 → 1.0.0-3

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