@kmamal/sdl 1.0.0-5 → 1.0.0-8

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