@kmamal/sdl 1.0.0-5 → 1.0.0-9

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 (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -5,84 +5,37 @@
5
5
  #include "joystick.h"
6
6
  #include "gamepad.h"
7
7
  #include "audio.h"
8
+ #include "window.h"
9
+ #include "threads.h"
10
+ #include "global.h"
11
+ #include <memory>
8
12
  #include <SDL3/SDL.h>
9
13
  #include <string>
10
- #include <sstream>
11
14
  #include <vector>
12
15
  #include <algorithm>
13
16
 
14
17
 
15
- std::string events::targets::APP;
16
- std::string events::targets::VIDEO;
17
- std::string events::targets::WINDOW;
18
- std::string events::targets::KEYBOARD;
19
- std::string events::targets::JOYSTICK;
20
- std::string events::targets::GAMEPAD;
21
- std::string events::targets::SENSOR;
22
- std::string events::targets::CLIPBOARD;
23
- std::string events::targets::JOYSTICK_DEVICE;
24
- std::string events::targets::GAMEPAD_DEVICE;
25
- std::string events::targets::AUDIO_DEVICE;
26
-
27
- std::string events::types::QUIT;
28
- std::string events::types::DISPLAY_ADD;
29
- std::string events::types::DISPLAY_REMOVE;
30
- std::string events::types::DISPLAY_ORIENT;
31
- std::string events::types::DISPLAY_MOVE;
32
- std::string events::types::DISPLAY_SCALE;
33
- std::string events::types::DISPLAY_MODE;
34
- std::string events::types::DISPLAY_USABLE;
35
- std::string events::types::DISPLAY_CHANGE;
36
- std::string events::types::SHOW;
37
- std::string events::types::HIDE;
38
- std::string events::types::EXPOSE;
39
- std::string events::types::MOVE;
40
- std::string events::types::RESIZE;
41
- std::string events::types::MINIMIZE;
42
- std::string events::types::MAXIMIZE;
43
- std::string events::types::RESTORE;
44
- std::string events::types::ENTER_FULLSCREEN;
45
- std::string events::types::LEAVE_FULLSCREEN;
46
- std::string events::types::FOCUS;
47
- std::string events::types::BLUR;
48
- std::string events::types::HOVER;
49
- std::string events::types::LEAVE;
50
- std::string events::types::KEY_DOWN;
51
- std::string events::types::KEY_UP;
52
- std::string events::types::TEXT_INPUT;
53
- std::string events::types::MOUSE_MOVE;
54
- std::string events::types::MOUSE_BUTTON_DOWN;
55
- std::string events::types::MOUSE_BUTTON_UP;
56
- std::string events::types::MOUSE_WHEEL;
57
- std::string events::types::DROP_BEGIN;
58
- std::string events::types::DROP_COMPLETE;
59
- std::string events::types::DROP_FILE;
60
- std::string events::types::DROP_TEXT;
61
- std::string events::types::CLOSE;
62
- std::string events::types::RENDER_DEVICE_LOST;
63
- std::string events::types::KEYMAP_CHANGE;
64
- std::string events::types::FINGER_DOWN;
65
- std::string events::types::FINGER_UP;
66
- std::string events::types::FINGER_MOVE;
67
- std::string events::types::FINGER_CANCEL;
68
- std::string events::types::DEVICE_ADD;
69
- std::string events::types::DEVICE_REMOVE;
70
- std::string events::types::DEVICE_FORMAT_CHANGE;
71
- std::string events::types::AXIS_MOTION;
72
- std::string events::types::BUTTON_DOWN;
73
- std::string events::types::BUTTON_UP;
74
- std::string events::types::BALL_MOTION;
75
- std::string events::types::HAT_MOTION;
76
- std::string events::types::POWER_UPDATE;
77
- std::string events::types::STEAM_HANDLE_UPDATE;
78
- std::string events::types::REMAP;
79
- std::string events::types::UPDATE;
80
-
81
-
82
18
  static Napi::Env *poll_env = nullptr;
83
19
  static Napi::Function *poll_callback = nullptr;
84
20
 
85
- static const std::string *
21
+ struct Packing {
22
+ packed::Value value = packed::Value::object();
23
+ const packed::Str *target = nullptr;
24
+ Uint32 target_id = 0;
25
+
26
+ void setTarget (const packed::Str &name) {
27
+ target = &name;
28
+ value.set("target", packed::Value::ref(name));
29
+ }
30
+ void setTargetId (Uint32 id) {
31
+ target_id = id;
32
+ value.set("targetId", id);
33
+ }
34
+ void setType (const packed::Str &name) { value.set("type", packed::Value::ref(name)); }
35
+ void set (const char *key, packed::Value item) { value.set(key, std::move(item)); }
36
+ };
37
+
38
+ static const packed::Str *
86
39
  _windowEventType (Uint32 type)
87
40
  {
88
41
  switch (type) {
@@ -107,20 +60,41 @@ _windowEventType (Uint32 type)
107
60
  }
108
61
  }
109
62
 
63
+ static const packed::Str *
64
+ _displayEventType (Uint32 type)
65
+ {
66
+ switch (type) {
67
+ case SDL_EVENT_DISPLAY_ORIENTATION: return &events::types::DISPLAY_ORIENT;
68
+ case SDL_EVENT_DISPLAY_MOVED: return &events::types::DISPLAY_MOVE;
69
+ case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED: return &events::types::DISPLAY_SCALE;
70
+ case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED: return &events::types::DISPLAY_MODE;
71
+ case SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED: return &events::types::DISPLAY_USABLE;
72
+ default: return nullptr;
73
+ }
74
+ }
75
+
76
+ template <typename Map, typename Key>
77
+ static packed::Value
78
+ _lookup (const Map &map, Key key)
79
+ {
80
+ auto entry = map.find(key);
81
+ return entry != map.end() ? packed::Value::ref(entry->second) : packed::Value();
82
+ }
83
+
110
84
  static bool
111
- _packWindowEvent (const SDL_Event &event, Napi::Object &packed)
85
+ _packWindowEvent (const SDL_Event &event, Packing &packed)
112
86
  {
113
- const std::string *type = _windowEventType(event.type);
87
+ const packed::Str *type = _windowEventType(event.type);
114
88
  if (type == nullptr) { return false; }
115
89
 
116
- packed.Set("target", events::targets::WINDOW);
117
- packed.Set("targetId", event.window.windowID);
118
- packed.Set("type", *type);
90
+ packed.setTarget(events::targets::WINDOW);
91
+ packed.setTargetId(event.window.windowID);
92
+ packed.setType(*type);
119
93
 
120
94
  switch (event.type) {
121
95
  case SDL_EVENT_WINDOW_MOVED: {
122
- packed.Set("x", event.window.data1);
123
- packed.Set("y", event.window.data2);
96
+ packed.set("x", event.window.data1);
97
+ packed.set("y", event.window.data2);
124
98
  break;
125
99
  }
126
100
  case SDL_EVENT_WINDOW_RESIZED:
@@ -132,19 +106,23 @@ _packWindowEvent (const SDL_Event &event, Napi::Object &packed)
132
106
  return false;
133
107
  }
134
108
 
135
- int width, height;
136
- SDL_GetWindowSize(window, &width, &height);
137
- int pixel_width, pixel_height;
138
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
109
+ int width, height, pixel_width, pixel_height;
110
+ if (
111
+ !SDL_GetWindowSize(window, &width, &height)
112
+ || !SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height)
113
+ ) {
114
+ SDL_ClearError();
115
+ return false;
116
+ }
139
117
 
140
- packed.Set("width", width);
141
- packed.Set("height", height);
142
- packed.Set("pixelWidth", pixel_width);
143
- packed.Set("pixelHeight", pixel_height);
118
+ packed.set("width", width);
119
+ packed.set("height", height);
120
+ packed.set("pixelWidth", pixel_width);
121
+ packed.set("pixelHeight", pixel_height);
144
122
  break;
145
123
  }
146
124
  case SDL_EVENT_WINDOW_DISPLAY_CHANGED: {
147
- packed.Set("displayId", event.window.data1);
125
+ packed.set("displayId", (Uint32) event.window.data1);
148
126
  break;
149
127
  }
150
128
  default: break;
@@ -153,313 +131,269 @@ _packWindowEvent (const SDL_Event &event, Napi::Object &packed)
153
131
  return true;
154
132
  }
155
133
 
156
- bool
157
- events::dispatchEvent(const SDL_Event &event)
134
+ static bool
135
+ _packEvent (const SDL_Event &event, Packing &packed)
158
136
  {
159
- if (poll_env == nullptr) { return false; }
160
- Napi::Env &env = *poll_env;
161
-
162
- Napi::Object packed = Napi::Object::New(env);
163
-
164
137
  if (event.type >= SDL_EVENT_WINDOW_FIRST && event.type <= SDL_EVENT_WINDOW_LAST) {
165
- if (!_packWindowEvent(event, packed)) { return false; }
138
+ return _packWindowEvent(event, packed);
166
139
  }
167
- else switch (event.type) {
140
+
141
+ switch (event.type) {
168
142
  case SDL_EVENT_QUIT: {
169
- packed.Set("target", events::targets::APP);
170
- packed.Set("type", events::types::QUIT);
143
+ packed.setTarget(events::targets::APP);
144
+ packed.setType(events::types::QUIT);
171
145
  break;
172
146
  }
173
147
 
174
148
  case SDL_EVENT_DISPLAY_ADDED: {
175
- Napi::Value display = video::_getDisplay(env, event.display.displayID);
176
- if (display.IsNull()) { return false; }
149
+ packed::Value display = video::_getDisplay(event.display.displayID);
150
+ if (display.isNull()) { return false; }
177
151
 
178
- packed.Set("target", events::targets::VIDEO);
179
- packed.Set("type", events::types::DISPLAY_ADD);
180
- packed.Set("display", display);
152
+ packed.setTarget(events::targets::VIDEO);
153
+ packed.setType(events::types::DISPLAY_ADD);
154
+ packed.set("display", std::move(display));
181
155
  break;
182
156
  }
183
157
  case SDL_EVENT_DISPLAY_REMOVED: {
184
- packed.Set("target", events::targets::VIDEO);
185
- packed.Set("type", events::types::DISPLAY_REMOVE);
186
- packed.Set("displayId", event.display.displayID);
187
- break;
188
- }
189
- case SDL_EVENT_DISPLAY_ORIENTATION: {
190
- auto orientation_entry = video::orientations.find((SDL_DisplayOrientation) event.display.data1);
191
- Napi::Value orientation = orientation_entry != video::orientations.end()
192
- ? Napi::String::New(env, orientation_entry->second)
193
- : env.Null();
194
-
195
- packed.Set("target", events::targets::VIDEO);
196
- packed.Set("displayId", event.display.displayID);
197
- packed.Set("type", events::types::DISPLAY_ORIENT);
198
- packed.Set("orientation", orientation);
199
- break;
200
- }
201
- case SDL_EVENT_DISPLAY_MOVED: {
202
- packed.Set("target", events::targets::VIDEO);
203
- packed.Set("type", events::types::DISPLAY_MOVE);
204
-
205
- SDL_DisplayID display_id = event.display.displayID;
206
- packed.Set("displayId", display_id);
207
-
208
- SDL_Rect rect;
209
-
210
- if(!SDL_GetDisplayBounds(display_id, &rect)) {
211
- SDL_ClearError();
212
- return false;
213
- }
214
- packed.Set("geometryX", rect.x);
215
- packed.Set("geometryY", rect.y);
216
-
217
- if(!SDL_GetDisplayUsableBounds(display_id, &rect)) {
218
- SDL_ClearError();
219
- return false;
220
- }
221
- packed.Set("usableX", rect.x);
222
- packed.Set("usableY", rect.y);
223
-
158
+ packed.setTarget(events::targets::VIDEO);
159
+ packed.setType(events::types::DISPLAY_REMOVE);
160
+ packed.set("displayId", event.display.displayID);
224
161
  break;
225
162
  }
163
+ case SDL_EVENT_DISPLAY_ORIENTATION:
164
+ case SDL_EVENT_DISPLAY_MOVED:
226
165
  case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
227
166
  case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
228
167
  case SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED: {
229
- Napi::Value display = video::_getDisplay(env, event.display.displayID);
230
- if (display.IsNull()) { return false; }
168
+ packed::Value display = video::_getDisplay(event.display.displayID);
169
+ if (display.isNull()) { return false; }
231
170
 
232
- packed.Set("target", events::targets::VIDEO);
233
- packed.Set("type", event.type == SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED
234
- ? events::types::DISPLAY_SCALE
235
- : event.type == SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED
236
- ? events::types::DISPLAY_MODE
237
- : events::types::DISPLAY_USABLE);
238
- packed.Set("displayId", event.display.displayID);
239
- packed.Set("display", display);
171
+ packed.setTarget(events::targets::VIDEO);
172
+ packed.setType(*_displayEventType(event.type));
173
+ packed.set("displayId", event.display.displayID);
174
+ packed.set("display", std::move(display));
240
175
  break;
241
176
  }
242
177
 
243
178
  case SDL_EVENT_RENDER_DEVICE_LOST: {
244
- packed.Set("target", events::targets::WINDOW);
245
- packed.Set("targetId", event.render.windowID);
246
- packed.Set("type", events::types::RENDER_DEVICE_LOST);
179
+ packed.setTarget(events::targets::WINDOW);
180
+ packed.setTargetId(event.render.windowID);
181
+ packed.setType(events::types::RENDER_DEVICE_LOST);
247
182
  break;
248
183
  }
249
184
 
185
+ case SDL_EVENT_RENDER_DEVICE_RESET: {
186
+ SDL_Window *window = SDL_GetWindowFromID(event.render.windowID);
187
+ if (window == nullptr) {
188
+ SDL_ClearError();
189
+ return false;
190
+ }
191
+ window::resetTexture(window);
192
+ return false;
193
+ }
194
+
250
195
  case SDL_EVENT_DROP_BEGIN:
251
196
  case SDL_EVENT_DROP_COMPLETE: {
252
- packed.Set("target", events::targets::WINDOW);
253
- packed.Set("targetId", event.drop.windowID);
254
- packed.Set("type", event.type == SDL_EVENT_DROP_BEGIN ? events::types::DROP_BEGIN : events::types::DROP_COMPLETE);
197
+ packed.setTarget(events::targets::WINDOW);
198
+ packed.setTargetId(event.drop.windowID);
199
+ packed.setType(event.type == SDL_EVENT_DROP_BEGIN ? events::types::DROP_BEGIN : events::types::DROP_COMPLETE);
255
200
  break;
256
201
  }
257
202
  case SDL_EVENT_DROP_FILE:
258
203
  case SDL_EVENT_DROP_TEXT: {
259
- packed.Set("target", events::targets::WINDOW);
260
- packed.Set("targetId", event.drop.windowID);
204
+ packed.setTarget(events::targets::WINDOW);
205
+ packed.setTargetId(event.drop.windowID);
261
206
 
262
207
  bool is_file = event.type == SDL_EVENT_DROP_FILE;
263
- packed.Set("type", is_file ? events::types::DROP_FILE : events::types::DROP_TEXT);
264
- packed.Set(is_file ? "file" : "text", event.drop.data);
208
+ packed.setType(is_file ? events::types::DROP_FILE : events::types::DROP_TEXT);
209
+ packed.set(is_file ? "file" : "text", event.drop.data);
265
210
  break;
266
211
  }
267
212
 
268
213
  case SDL_EVENT_KEY_DOWN:
269
214
  case SDL_EVENT_KEY_UP: {
270
- packed.Set("target", events::targets::WINDOW);
271
- packed.Set("targetId", event.key.windowID);
272
- packed.Set(
273
- "type",
274
- event.type == SDL_EVENT_KEY_DOWN
275
- ? events::types::KEY_DOWN
276
- : events::types::KEY_UP
277
- );
215
+ packed.setTarget(events::targets::WINDOW);
216
+ packed.setTargetId(event.key.windowID);
217
+ packed.setType(event.type == SDL_EVENT_KEY_DOWN
218
+ ? events::types::KEY_DOWN
219
+ : events::types::KEY_UP);
278
220
 
279
- packed.Set("scancode", (int) event.key.scancode);
221
+ packed.set("scancode", (int) event.key.scancode);
280
222
 
281
- packed.Set("key", keyboard::packKey(env, event.key.key));
223
+ packed.set("key", keyboard::packKey(event.key.key));
282
224
 
283
225
  if (event.type == SDL_EVENT_KEY_DOWN) {
284
- packed.Set("repeat", event.key.repeat);
226
+ packed.set("repeat", event.key.repeat);
285
227
  }
286
228
 
287
- packed.Set("alt", !!(event.key.mod & SDL_KMOD_ALT));
288
- packed.Set("ctrl", !!(event.key.mod & SDL_KMOD_CTRL));
289
- packed.Set("shift", !!(event.key.mod & SDL_KMOD_SHIFT));
290
- packed.Set("super", !!(event.key.mod & SDL_KMOD_GUI));
291
- packed.Set("altgr", !!(event.key.mod & SDL_KMOD_MODE));
292
- packed.Set("numlock", !!(event.key.mod & SDL_KMOD_NUM));
293
- packed.Set("capslock", !!(event.key.mod & SDL_KMOD_CAPS));
229
+ packed.set("alt", !!(event.key.mod & SDL_KMOD_ALT));
230
+ packed.set("ctrl", !!(event.key.mod & SDL_KMOD_CTRL));
231
+ packed.set("shift", !!(event.key.mod & SDL_KMOD_SHIFT));
232
+ packed.set("super", !!(event.key.mod & SDL_KMOD_GUI));
233
+ packed.set("altgr", !!(event.key.mod & SDL_KMOD_MODE));
234
+ packed.set("numlock", !!(event.key.mod & SDL_KMOD_NUM));
235
+ packed.set("capslock", !!(event.key.mod & SDL_KMOD_CAPS));
294
236
  break;
295
237
  }
296
238
 
297
239
  case SDL_EVENT_TEXT_INPUT: {
298
- packed.Set("target", events::targets::WINDOW);
299
- packed.Set("targetId", event.text.windowID);
300
- packed.Set("type", events::types::TEXT_INPUT);
301
- packed.Set("text", event.text.text);
240
+ packed.setTarget(events::targets::WINDOW);
241
+ packed.setTargetId(event.text.windowID);
242
+ packed.setType(events::types::TEXT_INPUT);
243
+ packed.set("text", event.text.text);
302
244
  break;
303
245
  }
304
246
 
305
247
  case SDL_EVENT_KEYMAP_CHANGED: {
306
- packed.Set("target", events::targets::KEYBOARD);
307
- packed.Set("type", events::types::KEYMAP_CHANGE);
248
+ packed.setTarget(events::targets::KEYBOARD);
249
+ packed.setType(events::types::KEYMAP_CHANGE);
308
250
  break;
309
251
  }
310
252
 
311
253
  case SDL_EVENT_MOUSE_MOTION: {
312
- packed.Set("target", events::targets::WINDOW);
313
- packed.Set("targetId", event.motion.windowID);
314
- packed.Set("type", events::types::MOUSE_MOVE);
315
- packed.Set("touch", event.motion.which == SDL_TOUCH_MOUSEID);
316
- packed.Set("pen", event.motion.which == SDL_PEN_MOUSEID);
317
- packed.Set("x", event.motion.x);
318
- packed.Set("y", event.motion.y);
319
- packed.Set("dx", event.motion.xrel);
320
- packed.Set("dy", event.motion.yrel);
254
+ packed.setTarget(events::targets::WINDOW);
255
+ packed.setTargetId(event.motion.windowID);
256
+ packed.setType(events::types::MOUSE_MOVE);
257
+ packed.set("touch", event.motion.which == SDL_TOUCH_MOUSEID);
258
+ packed.set("pen", event.motion.which == SDL_PEN_MOUSEID);
259
+ packed.set("x", event.motion.x);
260
+ packed.set("y", event.motion.y);
261
+ packed.set("dx", event.motion.xrel);
262
+ packed.set("dy", event.motion.yrel);
321
263
  break;
322
264
  }
323
265
  case SDL_EVENT_MOUSE_BUTTON_DOWN:
324
266
  case SDL_EVENT_MOUSE_BUTTON_UP: {
325
- packed.Set("target", events::targets::WINDOW);
326
- packed.Set("targetId", event.button.windowID);
327
- packed.Set(
328
- "type",
329
- event.type == SDL_EVENT_MOUSE_BUTTON_DOWN
330
- ? events::types::MOUSE_BUTTON_DOWN
331
- : events::types::MOUSE_BUTTON_UP
332
- );
333
- packed.Set("touch", event.button.which == SDL_TOUCH_MOUSEID);
334
- packed.Set("pen", event.button.which == SDL_PEN_MOUSEID);
335
- packed.Set("button", event.button.button);
336
- packed.Set("x", event.button.x);
337
- packed.Set("y", event.button.y);
267
+ packed.setTarget(events::targets::WINDOW);
268
+ packed.setTargetId(event.button.windowID);
269
+ packed.setType(event.type == SDL_EVENT_MOUSE_BUTTON_DOWN
270
+ ? events::types::MOUSE_BUTTON_DOWN
271
+ : events::types::MOUSE_BUTTON_UP);
272
+ packed.set("touch", event.button.which == SDL_TOUCH_MOUSEID);
273
+ packed.set("pen", event.button.which == SDL_PEN_MOUSEID);
274
+ packed.set("button", event.button.button);
275
+ packed.set("x", event.button.x);
276
+ packed.set("y", event.button.y);
338
277
  break;
339
278
  }
340
279
  case SDL_EVENT_MOUSE_WHEEL: {
341
- packed.Set("target", events::targets::WINDOW);
342
- packed.Set("targetId", event.wheel.windowID);
343
- packed.Set("type", events::types::MOUSE_WHEEL);
344
- packed.Set("touch", event.wheel.which == SDL_TOUCH_MOUSEID);
345
- packed.Set("pen", event.wheel.which == SDL_PEN_MOUSEID);
346
- packed.Set("x", event.wheel.mouse_x);
347
- packed.Set("y", event.wheel.mouse_y);
348
- packed.Set("dx", event.wheel.x);
349
- packed.Set("dy", event.wheel.y);
350
- packed.Set("flipped", event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED);
280
+ packed.setTarget(events::targets::WINDOW);
281
+ packed.setTargetId(event.wheel.windowID);
282
+ packed.setType(events::types::MOUSE_WHEEL);
283
+ packed.set("touch", event.wheel.which == SDL_TOUCH_MOUSEID);
284
+ packed.set("pen", event.wheel.which == SDL_PEN_MOUSEID);
285
+ packed.set("x", event.wheel.mouse_x);
286
+ packed.set("y", event.wheel.mouse_y);
287
+ packed.set("dx", event.wheel.x);
288
+ packed.set("dy", event.wheel.y);
289
+ packed.set("flipped", event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED);
351
290
  break;
352
291
  }
353
292
 
354
293
  case SDL_EVENT_FINGER_UP:
355
294
  case SDL_EVENT_FINGER_DOWN:
356
295
  case SDL_EVENT_FINGER_CANCELED: {
357
- packed.Set("target", events::targets::WINDOW);
358
- packed.Set("targetId", event.tfinger.windowID);
359
- packed.Set("type", event.type == SDL_EVENT_FINGER_UP
296
+ packed.setTarget(events::targets::WINDOW);
297
+ packed.setTargetId(event.tfinger.windowID);
298
+ packed.setType(event.type == SDL_EVENT_FINGER_UP
360
299
  ? events::types::FINGER_UP
361
300
  : event.type == SDL_EVENT_FINGER_DOWN
362
301
  ? events::types::FINGER_DOWN
363
302
  : events::types::FINGER_CANCEL);
364
- packed.Set("mouse", event.tfinger.touchID == SDL_MOUSE_TOUCHID);
365
- packed.Set("pen", event.tfinger.touchID == SDL_PEN_TOUCHID);
366
- packed.Set("touchId", Napi::BigInt::New(env, event.tfinger.touchID));
367
- packed.Set("fingerId", Napi::BigInt::New(env, event.tfinger.fingerID));
368
- packed.Set("x", event.tfinger.x);
369
- packed.Set("y", event.tfinger.y);
370
- packed.Set("pressure", event.tfinger.pressure);
303
+ packed.set("mouse", event.tfinger.touchID == SDL_MOUSE_TOUCHID);
304
+ packed.set("pen", event.tfinger.touchID == SDL_PEN_TOUCHID);
305
+ packed.set("touchId", (uint64_t) event.tfinger.touchID);
306
+ packed.set("fingerId", (uint64_t) event.tfinger.fingerID);
307
+ packed.set("x", event.tfinger.x);
308
+ packed.set("y", event.tfinger.y);
309
+ packed.set("pressure", event.tfinger.pressure);
371
310
  break;
372
311
  }
373
312
  case SDL_EVENT_FINGER_MOTION: {
374
- packed.Set("target", events::targets::WINDOW);
375
- packed.Set("type", events::types::FINGER_MOVE);
376
- packed.Set("targetId", event.tfinger.windowID);
377
- packed.Set("mouse", event.tfinger.touchID == SDL_MOUSE_TOUCHID);
378
- packed.Set("pen", event.tfinger.touchID == SDL_PEN_TOUCHID);
379
- packed.Set("touchId", Napi::BigInt::New(env, event.tfinger.touchID));
380
- packed.Set("fingerId", Napi::BigInt::New(env, event.tfinger.fingerID));
381
- packed.Set("x", event.tfinger.x);
382
- packed.Set("y", event.tfinger.y);
383
- packed.Set("dx", event.tfinger.dx);
384
- packed.Set("dy", event.tfinger.dy);
385
- packed.Set("pressure", event.tfinger.pressure);
313
+ packed.setTarget(events::targets::WINDOW);
314
+ packed.setType(events::types::FINGER_MOVE);
315
+ packed.setTargetId(event.tfinger.windowID);
316
+ packed.set("mouse", event.tfinger.touchID == SDL_MOUSE_TOUCHID);
317
+ packed.set("pen", event.tfinger.touchID == SDL_PEN_TOUCHID);
318
+ packed.set("touchId", (uint64_t) event.tfinger.touchID);
319
+ packed.set("fingerId", (uint64_t) event.tfinger.fingerID);
320
+ packed.set("x", event.tfinger.x);
321
+ packed.set("y", event.tfinger.y);
322
+ packed.set("dx", event.tfinger.dx);
323
+ packed.set("dy", event.tfinger.dy);
324
+ packed.set("pressure", event.tfinger.pressure);
386
325
  break;
387
326
  }
388
327
 
389
328
  case SDL_EVENT_JOYSTICK_ADDED:
390
329
  case SDL_EVENT_GAMEPAD_ADDED: {
391
- packed.Set("target", event.type == SDL_EVENT_JOYSTICK_ADDED
330
+ // Unlike displays and audio devices, a joystick that vanished before the poll isn't dropped: its REMOVED event follows in the same poll, and SDL's name cache makes SDL_GetJoystickNameForID() useless for detecting it.
331
+ packed.setTarget(event.type == SDL_EVENT_JOYSTICK_ADDED
392
332
  ? events::targets::JOYSTICK_DEVICE
393
333
  : events::targets::GAMEPAD_DEVICE);
394
- packed.Set("type", events::types::DEVICE_ADD);
395
- packed.Set("device", joystick::_getDevice(env, event.jdevice.which));
334
+ packed.setType(events::types::DEVICE_ADD);
335
+ packed.set("device", joystick::_getDevice(event.jdevice.which));
396
336
  break;
397
337
  }
398
338
  case SDL_EVENT_JOYSTICK_REMOVED:
399
339
  case SDL_EVENT_GAMEPAD_REMOVED: {
400
- packed.Set("target", event.type == SDL_EVENT_JOYSTICK_REMOVED
340
+ packed.setTarget(event.type == SDL_EVENT_JOYSTICK_REMOVED
401
341
  ? events::targets::JOYSTICK_DEVICE
402
342
  : events::targets::GAMEPAD_DEVICE);
403
- packed.Set("type", events::types::DEVICE_REMOVE);
404
- packed.Set("deviceId", event.jdevice.which);
343
+ packed.setType(events::types::DEVICE_REMOVE);
344
+ packed.set("deviceId", event.jdevice.which);
405
345
  break;
406
346
  }
407
347
 
408
348
  case SDL_EVENT_JOYSTICK_AXIS_MOTION: {
409
- packed.Set("target", events::targets::JOYSTICK);
410
- packed.Set("targetId", event.jaxis.which);
411
- packed.Set("type", events::types::AXIS_MOTION);
412
- packed.Set("axis", event.jaxis.axis);
413
- packed.Set("value", joystick::mapAxisValue(event.jaxis.value));
349
+ packed.setTarget(events::targets::JOYSTICK);
350
+ packed.setTargetId(event.jaxis.which);
351
+ packed.setType(events::types::AXIS_MOTION);
352
+ packed.set("axis", event.jaxis.axis);
353
+ packed.set("value", joystick::mapAxisValue(event.jaxis.value));
414
354
  break;
415
355
  }
416
356
  case SDL_EVENT_JOYSTICK_BALL_MOTION: {
417
- packed.Set("target", events::targets::JOYSTICK);
418
- packed.Set("targetId", event.jball.which);
419
- packed.Set("type", events::types::BALL_MOTION);
420
- packed.Set("ball", event.jball.ball);
421
- packed.Set("dx", event.jball.xrel);
422
- packed.Set("dy", event.jball.yrel);
357
+ packed.setTarget(events::targets::JOYSTICK);
358
+ packed.setTargetId(event.jball.which);
359
+ packed.setType(events::types::BALL_MOTION);
360
+ packed.set("ball", event.jball.ball);
361
+ packed.set("dx", event.jball.xrel);
362
+ packed.set("dy", event.jball.yrel);
423
363
  break;
424
364
  }
425
365
  case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
426
366
  case SDL_EVENT_JOYSTICK_BUTTON_UP: {
427
- packed.Set("target", events::targets::JOYSTICK);
428
- packed.Set("targetId", event.jbutton.which);
429
- packed.Set(
430
- "type",
431
- event.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN
432
- ? events::types::BUTTON_DOWN
433
- : events::types::BUTTON_UP
434
- );
435
- packed.Set("button", event.jbutton.button);
367
+ packed.setTarget(events::targets::JOYSTICK);
368
+ packed.setTargetId(event.jbutton.which);
369
+ packed.setType(event.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN
370
+ ? events::types::BUTTON_DOWN
371
+ : events::types::BUTTON_UP);
372
+ packed.set("button", event.jbutton.button);
436
373
  break;
437
374
  }
438
375
  case SDL_EVENT_JOYSTICK_HAT_MOTION: {
439
- packed.Set("target", events::targets::JOYSTICK);
440
- packed.Set("targetId", event.jhat.which);
441
- packed.Set("type", events::types::HAT_MOTION);
442
- packed.Set("hat", event.jhat.hat);
443
- auto hat_position_entry = joystick::hat_positions.find(event.jhat.value);
444
- Napi::Value hat_position = hat_position_entry != joystick::hat_positions.end()
445
- ? Napi::String::New(env, hat_position_entry->second)
446
- : env.Null();
447
- packed.Set("value", hat_position);
376
+ packed.setTarget(events::targets::JOYSTICK);
377
+ packed.setTargetId(event.jhat.which);
378
+ packed.setType(events::types::HAT_MOTION);
379
+ packed.set("hat", event.jhat.hat);
380
+ packed.set("value", _lookup(joystick::hat_positions, event.jhat.value));
448
381
  break;
449
382
  }
450
383
 
451
384
  case SDL_EVENT_JOYSTICK_BATTERY_UPDATED: {
452
- packed.Set("target", events::targets::JOYSTICK);
453
- packed.Set("targetId", event.jbattery.which);
454
- packed.Set("type", events::types::POWER_UPDATE);
455
- packed.Set("power", joystick::mapPowerInfo(env, event.jbattery.state, event.jbattery.percent));
385
+ packed.setTarget(events::targets::JOYSTICK);
386
+ packed.setTargetId(event.jbattery.which);
387
+ packed.setType(events::types::POWER_UPDATE);
388
+ packed.set("power", joystick::mapPowerInfo(event.jbattery.state, event.jbattery.percent));
456
389
  break;
457
390
  }
458
391
 
459
392
  case SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED: {
460
- packed.Set("target", events::targets::GAMEPAD);
461
- packed.Set("type", events::types::STEAM_HANDLE_UPDATE);
393
+ packed.setTarget(events::targets::GAMEPAD);
394
+ packed.setType(events::types::STEAM_HANDLE_UPDATE);
462
395
 
396
+ joystick::JoysticksLock joysticks_lock;
463
397
  SDL_JoystickID gamepad_id = event.gdevice.which;
464
398
  SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
465
399
  if (gamepad == nullptr) {
@@ -467,15 +401,16 @@ events::dispatchEvent(const SDL_Event &event)
467
401
  return false;
468
402
  }
469
403
 
470
- packed.Set("targetId", gamepad_id);
471
- packed.Set("steamHandle", gamepad::getSteamHandle(env, gamepad));
404
+ packed.setTargetId(gamepad_id);
405
+ packed.set("steamHandle", gamepad::getSteamHandle(gamepad));
472
406
  break;
473
407
  }
474
408
 
475
409
  case SDL_EVENT_GAMEPAD_REMAPPED: {
476
- packed.Set("target", events::targets::GAMEPAD);
477
- packed.Set("type", events::types::REMAP);
410
+ packed.setTarget(events::targets::GAMEPAD);
411
+ packed.setType(events::types::REMAP);
478
412
 
413
+ joystick::JoysticksLock joysticks_lock;
479
414
  SDL_JoystickID gamepad_id = event.gdevice.which;
480
415
  SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
481
416
  if (gamepad == nullptr) {
@@ -483,93 +418,123 @@ events::dispatchEvent(const SDL_Event &event)
483
418
  return false;
484
419
  }
485
420
 
486
- packed.Set("targetId", gamepad_id);
487
- packed.Set("device", joystick::_getDevice(env, gamepad_id));
488
- gamepad::getState(env, gamepad, packed);
421
+ packed.setTargetId(gamepad_id);
422
+ packed.set("device", joystick::_getDevice(gamepad_id));
423
+ gamepad::getState(gamepad, packed.value);
489
424
  break;
490
425
  }
491
426
 
492
427
  case SDL_EVENT_GAMEPAD_AXIS_MOTION: {
493
- packed.Set("target", events::targets::GAMEPAD);
494
- packed.Set("type", events::types::AXIS_MOTION);
495
-
496
- int gamepad_id = event.gaxis.which;
497
- SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
498
- if (gamepad == nullptr) {
499
- SDL_ClearError();
500
- return false;
501
- }
502
-
503
- packed.Set("targetId", gamepad_id);
428
+ packed.setTarget(events::targets::GAMEPAD);
429
+ packed.setTargetId(event.gaxis.which);
430
+ packed.setType(events::types::AXIS_MOTION);
504
431
 
505
432
  SDL_GamepadAxis axis = (SDL_GamepadAxis) event.gaxis.axis;
506
433
  auto axis_entry = gamepad::axes.find(axis);
507
434
  if (axis_entry == gamepad::axes.end()) { return false; }
508
- packed.Set("axis", axis_entry->second);
509
- packed.Set("value", gamepad::mapAxisValue(event.gaxis.value));
435
+ packed.set("axis", packed::Value::ref(axis_entry->second));
436
+ packed.set("value", joystick::mapAxisValue(event.gaxis.value));
510
437
  break;
511
438
  }
512
439
  case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
513
440
  case SDL_EVENT_GAMEPAD_BUTTON_UP: {
514
- packed.Set("target", events::targets::GAMEPAD);
515
- packed.Set("targetId", event.gbutton.which);
516
- packed.Set(
517
- "type",
518
- event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN
519
- ? events::types::BUTTON_DOWN
520
- : events::types::BUTTON_UP
521
- );
441
+ packed.setTarget(events::targets::GAMEPAD);
442
+ packed.setTargetId(event.gbutton.which);
443
+ packed.setType(event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN
444
+ ? events::types::BUTTON_DOWN
445
+ : events::types::BUTTON_UP);
522
446
 
523
447
  auto button_entry = gamepad::buttons.find((SDL_GamepadButton) event.gbutton.button);
524
448
  if (button_entry == gamepad::buttons.end()) { return false; }
525
- packed.Set("button", button_entry->second);
449
+ packed.set("button", packed::Value::ref(button_entry->second));
526
450
  break;
527
451
  }
528
452
 
529
453
  case SDL_EVENT_SENSOR_UPDATE: {
530
- packed.Set("target", events::targets::SENSOR);
531
- packed.Set("targetId", event.sensor.which);
532
- packed.Set("type", events::types::UPDATE);
454
+ packed.setTarget(events::targets::SENSOR);
455
+ packed.setTargetId(event.sensor.which);
456
+ packed.setType(events::types::UPDATE);
457
+ packed.set("x", event.sensor.data[0]);
458
+ packed.set("y", event.sensor.data[1]);
459
+ packed.set("z", event.sensor.data[2]);
460
+ packed.set("timestamp", (uint64_t) event.sensor.sensor_timestamp);
533
461
  break;
534
462
  }
535
463
 
536
464
  case SDL_EVENT_AUDIO_DEVICE_ADDED: {
537
465
  bool is_recording = event.adevice.recording;
538
466
 
539
- Napi::Value device = audio::_getDevice(env, event.adevice.which);
540
- if (device.IsNull()) { return false; }
467
+ packed::Value device = audio::_getDevice(event.adevice.which);
468
+ if (device.isNull()) { return false; }
541
469
 
542
- packed.Set("target", events::targets::AUDIO_DEVICE);
543
- packed.Set("type", events::types::DEVICE_ADD);
544
- packed.Set("audioDeviceType", audio::device_types[is_recording]);
545
- packed.Set("device", device);
470
+ packed.setTarget(events::targets::AUDIO_DEVICE);
471
+ packed.setType(events::types::DEVICE_ADD);
472
+ packed.set("audioDeviceType", packed::Value::ref(audio::device_types.at(is_recording)));
473
+ packed.set("device", std::move(device));
474
+ packed.set("defaultDevice", audio::_getDefaultDevice(is_recording));
546
475
  break;
547
476
  }
548
477
  case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
549
- packed.Set("target", events::targets::AUDIO_DEVICE);
550
- packed.Set("type", events::types::DEVICE_REMOVE);
551
- packed.Set("audioDeviceType", audio::device_types[event.adevice.recording]);
552
- packed.Set("deviceId", event.adevice.which);
478
+ packed.setTarget(events::targets::AUDIO_DEVICE);
479
+ packed.setType(events::types::DEVICE_REMOVE);
480
+ packed.set("audioDeviceType", packed::Value::ref(audio::device_types.at(event.adevice.recording)));
481
+ packed.set("deviceId", event.adevice.which);
482
+ packed.set("defaultDevice", audio::_getDefaultDevice(event.adevice.recording));
553
483
  break;
554
484
  }
555
485
  case SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED: {
556
- packed.Set("target", events::targets::AUDIO_DEVICE);
557
- packed.Set("type", events::types::DEVICE_FORMAT_CHANGE);
558
- packed.Set("audioDeviceType", audio::device_types[event.adevice.recording]);
559
- packed.Set("deviceId", event.adevice.which);
486
+ packed.setTarget(events::targets::AUDIO_DEVICE);
487
+ packed.setType(events::types::DEVICE_FORMAT_CHANGE);
488
+ packed.set("audioDeviceType", packed::Value::ref(audio::device_types.at(event.adevice.recording)));
489
+ packed.set("deviceId", event.adevice.which);
490
+ packed.set("format", audio::_getFormat(event.adevice.which));
491
+ packed.set("defaultDevice", audio::_getDefaultDevice(event.adevice.recording));
560
492
  break;
561
493
  }
562
494
 
563
495
  case SDL_EVENT_CLIPBOARD_UPDATE: {
564
- packed.Set("target", events::targets::CLIPBOARD);
565
- packed.Set("type", events::types::UPDATE);
496
+ packed.setTarget(events::targets::CLIPBOARD);
497
+ packed.setType(events::types::UPDATE);
566
498
  break;
567
499
  }
568
500
  }
569
501
 
570
- if (!packed.Has("type")) { return false; }
502
+ return packed.value.has("type");
503
+ }
504
+
505
+ static bool in_callback = false;
506
+
507
+ struct CallbackGuard {
508
+ CallbackGuard () { in_callback = true; }
509
+ ~CallbackGuard () { in_callback = false; }
510
+ };
511
+
512
+ static bool
513
+ _dispatchEvent (const SDL_Event &event)
514
+ {
515
+ if (poll_env == nullptr) { return false; }
516
+ Napi::Env &env = *poll_env;
517
+ Napi::HandleScope scope(env);
518
+
519
+ Packing packing;
520
+ if (!_packEvent(event, packing)) { return false; }
521
+
522
+ if (!threads::hasWorkers()) {
523
+ CallbackGuard guard;
524
+ poll_callback->Call(env.Global(), { packing.value.toNapi(env) });
525
+ return true;
526
+ }
527
+
528
+ threads::Event shared = {
529
+ packing.target,
530
+ packing.target_id,
531
+ std::make_shared<const packed::Value>(std::move(packing.value)),
532
+ };
533
+
534
+ threads::distribute(shared, env);
571
535
 
572
- poll_callback->Call(poll_env->Global(), { packed });
536
+ CallbackGuard guard;
537
+ poll_callback->Call(env.Global(), { shared.value->toNapi(env) });
573
538
 
574
539
  return true;
575
540
  }
@@ -617,10 +582,14 @@ events::isDispatchingFromWatch (const Napi::CallbackInfo &info)
617
582
  }
618
583
 
619
584
  bool
620
- events::dispatchEventFromWatch(const SDL_Event &event)
585
+ events::dispatchEventFromWatch (const SDL_Event &event)
621
586
  {
587
+ // Only dispatch while no JS callback is running; otherwise the event arrives through the normal poll.
588
+ if (in_callback) { return false; }
589
+
590
+ // No try/catch: the JS handler only assigns fields before routing user code through tryEmit, so only an out-of-memory failure inside Node-API could throw here, which isn't worth guarding against.
622
591
  dispatching_from_watch++;
623
- bool dispatched = events::dispatchEvent(event);
592
+ bool dispatched = _dispatchEvent(event);
624
593
  dispatching_from_watch--;
625
594
 
626
595
  if (dispatched) {
@@ -636,6 +605,34 @@ events::dispatchEventFromWatch(const SDL_Event &event)
636
605
  return dispatched;
637
606
  }
638
607
 
608
+ struct PollGuard {
609
+ PollGuard (Napi::Env &env, Napi::Function &callback) { poll_env = &env; poll_callback = &callback; }
610
+ ~PollGuard () { poll_env = nullptr; poll_callback = nullptr; }
611
+ };
612
+
613
+ static void
614
+ _pump (Napi::Env &env, Napi::Function &callback)
615
+ {
616
+ Uint64 poll_start = SDL_GetTicksNS();
617
+
618
+ PollGuard guard(env, callback);
619
+
620
+ SDL_Event event;
621
+ while (SDL_PollEvent(&event)) {
622
+ if (wasDispatchedFromWatch(event)) { continue; }
623
+ _dispatchEvent(event);
624
+ }
625
+
626
+ watch_dispatched_events.erase(
627
+ std::remove_if(
628
+ watch_dispatched_events.begin(),
629
+ watch_dispatched_events.end(),
630
+ [poll_start](const WatchDispatchedEvent &it) { return it.timestamp < poll_start; }
631
+ ),
632
+ watch_dispatched_events.end()
633
+ );
634
+ }
635
+
639
636
  Napi::Value
640
637
  events::poll (const Napi::CallbackInfo &info)
641
638
  {
@@ -643,34 +640,31 @@ events::poll (const Napi::CallbackInfo &info)
643
640
 
644
641
  Napi::Function callback = info[0].As<Napi::Function>();
645
642
 
646
- Uint64 poll_start = SDL_GetTicksNS();
643
+ if (global::isMainThread()) {
644
+ threads::drain(env, callback);
645
+ global::processInitRequests();
646
+ _pump(env, callback);
647
+ return Napi::Boolean::New(env, threads::isPollingFast());
648
+ }
647
649
 
648
- poll_env = &env;
649
- poll_callback = &callback;
650
+ threads::drain(env, callback);
650
651
 
651
- try {
652
- SDL_Event event;
653
- while (SDL_PollEvent(&event)) {
654
- if (wasDispatchedFromWatch(event)) { continue; }
655
- events::dispatchEvent(event);
656
- }
652
+ return env.Undefined();
653
+ }
657
654
 
658
- watch_dispatched_events.erase(
659
- std::remove_if(
660
- watch_dispatched_events.begin(),
661
- watch_dispatched_events.end(),
662
- [poll_start](const WatchDispatchedEvent &it) { return it.timestamp < poll_start; }
663
- ),
664
- watch_dispatched_events.end()
665
- );
666
- }
667
- catch (...) {
668
- poll_env = nullptr;
669
- poll_callback = nullptr;
670
- throw;
671
- }
672
- poll_env = nullptr;
673
- poll_callback = nullptr;
655
+ Napi::Value
656
+ events::broadcastChange (const Napi::CallbackInfo &info)
657
+ {
658
+ Napi::Env env = info.Env();
659
+
660
+ std::string name = info[0].As<Napi::String>().Utf8Value();
661
+
662
+ packed::Value value = packed::Value::object();
663
+ value.set("target", packed::Value::ref(events::targets::CHANGE));
664
+ value.set("type", name);
665
+
666
+ threads::Event shared = { nullptr, 0, std::make_shared<const packed::Value>(std::move(value)) };
667
+ threads::distribute(shared, env);
674
668
 
675
669
  return env.Undefined();
676
670
  }