@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,36 +1,37 @@
1
1
  #include "events.h"
2
2
  #include "enums.h"
3
- #include "global.h"
4
3
  #include "video.h"
4
+ #include "keyboard.h"
5
5
  #include "joystick.h"
6
- #include "controller.h"
6
+ #include "gamepad.h"
7
7
  #include "audio.h"
8
- #include <SDL.h>
8
+ #include <SDL3/SDL.h>
9
9
  #include <string>
10
10
  #include <sstream>
11
-
12
-
13
- std::string events::families::APP;
14
- std::string events::families::DISPLAY;
15
- std::string events::families::WINDOW;
16
- std::string events::families::DROP;
17
- std::string events::families::KEYMAP;
18
- std::string events::families::KEYBOARD;
19
- std::string events::families::TEXT;
20
- std::string events::families::MOUSE;
21
- std::string events::families::TOUCH;
22
- std::string events::families::JOYSTICK_DEVICE;
23
- std::string events::families::JOYSTICK;
24
- std::string events::families::CONTROLLER;
25
- std::string events::families::SENSOR;
26
- std::string events::families::AUDIO_DEVICE;
27
- std::string events::families::CLIPBOARD;
11
+ #include <vector>
12
+ #include <algorithm>
13
+
14
+
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;
28
26
 
29
27
  std::string events::types::QUIT;
30
28
  std::string events::types::DISPLAY_ADD;
31
29
  std::string events::types::DISPLAY_REMOVE;
32
30
  std::string events::types::DISPLAY_ORIENT;
33
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;
34
35
  std::string events::types::DISPLAY_CHANGE;
35
36
  std::string events::types::SHOW;
36
37
  std::string events::types::HIDE;
@@ -40,6 +41,8 @@ std::string events::types::RESIZE;
40
41
  std::string events::types::MINIMIZE;
41
42
  std::string events::types::MAXIMIZE;
42
43
  std::string events::types::RESTORE;
44
+ std::string events::types::ENTER_FULLSCREEN;
45
+ std::string events::types::LEAVE_FULLSCREEN;
43
46
  std::string events::types::FOCUS;
44
47
  std::string events::types::BLUR;
45
48
  std::string events::types::HOVER;
@@ -56,12 +59,15 @@ std::string events::types::DROP_COMPLETE;
56
59
  std::string events::types::DROP_FILE;
57
60
  std::string events::types::DROP_TEXT;
58
61
  std::string events::types::CLOSE;
62
+ std::string events::types::RENDER_DEVICE_LOST;
59
63
  std::string events::types::KEYMAP_CHANGE;
60
64
  std::string events::types::FINGER_DOWN;
61
65
  std::string events::types::FINGER_UP;
62
66
  std::string events::types::FINGER_MOVE;
67
+ std::string events::types::FINGER_CANCEL;
63
68
  std::string events::types::DEVICE_ADD;
64
69
  std::string events::types::DEVICE_REMOVE;
70
+ std::string events::types::DEVICE_FORMAT_CHANGE;
65
71
  std::string events::types::AXIS_MOTION;
66
72
  std::string events::types::BUTTON_DOWN;
67
73
  std::string events::types::BUTTON_UP;
@@ -76,248 +82,302 @@ std::string events::types::UPDATE;
76
82
  static Napi::Env *poll_env = nullptr;
77
83
  static Napi::Function *poll_callback = nullptr;
78
84
 
79
- void
85
+ static const std::string *
86
+ _windowEventType (Uint32 type)
87
+ {
88
+ switch (type) {
89
+ case SDL_EVENT_WINDOW_SHOWN: return &events::types::SHOW;
90
+ case SDL_EVENT_WINDOW_HIDDEN: return &events::types::HIDE;
91
+ case SDL_EVENT_WINDOW_EXPOSED: return &events::types::EXPOSE;
92
+ case SDL_EVENT_WINDOW_MOVED: return &events::types::MOVE;
93
+ case SDL_EVENT_WINDOW_RESIZED:
94
+ case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: return &events::types::RESIZE;
95
+ case SDL_EVENT_WINDOW_DISPLAY_CHANGED: return &events::types::DISPLAY_CHANGE;
96
+ case SDL_EVENT_WINDOW_MINIMIZED: return &events::types::MINIMIZE;
97
+ case SDL_EVENT_WINDOW_MAXIMIZED: return &events::types::MAXIMIZE;
98
+ case SDL_EVENT_WINDOW_RESTORED: return &events::types::RESTORE;
99
+ case SDL_EVENT_WINDOW_ENTER_FULLSCREEN: return &events::types::ENTER_FULLSCREEN;
100
+ case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN: return &events::types::LEAVE_FULLSCREEN;
101
+ case SDL_EVENT_WINDOW_FOCUS_GAINED: return &events::types::FOCUS;
102
+ case SDL_EVENT_WINDOW_FOCUS_LOST: return &events::types::BLUR;
103
+ case SDL_EVENT_WINDOW_MOUSE_ENTER: return &events::types::HOVER;
104
+ case SDL_EVENT_WINDOW_MOUSE_LEAVE: return &events::types::LEAVE;
105
+ case SDL_EVENT_WINDOW_CLOSE_REQUESTED: return &events::types::CLOSE;
106
+ default: return nullptr;
107
+ }
108
+ }
109
+
110
+ static bool
111
+ _packWindowEvent (const SDL_Event &event, Napi::Object &packed)
112
+ {
113
+ const std::string *type = _windowEventType(event.type);
114
+ if (type == nullptr) { return false; }
115
+
116
+ packed.Set("target", events::targets::WINDOW);
117
+ packed.Set("targetId", event.window.windowID);
118
+ packed.Set("type", *type);
119
+
120
+ switch (event.type) {
121
+ case SDL_EVENT_WINDOW_MOVED: {
122
+ packed.Set("x", event.window.data1);
123
+ packed.Set("y", event.window.data2);
124
+ break;
125
+ }
126
+ case SDL_EVENT_WINDOW_RESIZED:
127
+ case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: {
128
+ // The window was destroyed with events for it still in the queue
129
+ SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
130
+ if (window == nullptr) {
131
+ SDL_ClearError();
132
+ return false;
133
+ }
134
+
135
+ int width, height;
136
+ SDL_GetWindowSize(window, &width, &height);
137
+ int pixel_width, pixel_height;
138
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
139
+
140
+ packed.Set("width", width);
141
+ packed.Set("height", height);
142
+ packed.Set("pixelWidth", pixel_width);
143
+ packed.Set("pixelHeight", pixel_height);
144
+ break;
145
+ }
146
+ case SDL_EVENT_WINDOW_DISPLAY_CHANGED: {
147
+ packed.Set("displayId", event.window.data1);
148
+ break;
149
+ }
150
+ default: break;
151
+ }
152
+
153
+ return true;
154
+ }
155
+
156
+ bool
80
157
  events::dispatchEvent(const SDL_Event &event)
81
158
  {
82
- if (poll_env == nullptr) { return; }
159
+ if (poll_env == nullptr) { return false; }
83
160
  Napi::Env &env = *poll_env;
84
161
 
85
162
  Napi::Object packed = Napi::Object::New(env);
86
163
 
87
- switch (event.type) {
88
- case SDL_QUIT: {
89
- packed.Set("family", events::families::APP);
164
+ if (event.type >= SDL_EVENT_WINDOW_FIRST && event.type <= SDL_EVENT_WINDOW_LAST) {
165
+ if (!_packWindowEvent(event, packed)) { return false; }
166
+ }
167
+ else switch (event.type) {
168
+ case SDL_EVENT_QUIT: {
169
+ packed.Set("target", events::targets::APP);
90
170
  packed.Set("type", events::types::QUIT);
91
171
  break;
92
172
  }
93
173
 
94
- case SDL_DISPLAYEVENT: {
95
- packed.Set("family", events::families::DISPLAY);
96
-
97
- int display_index = event.display.display;
98
- packed.Set("displayIndex", display_index);
99
-
100
- switch (event.display.event) {
101
- case SDL_DISPLAYEVENT_CONNECTED: { packed.Set("type", events::types::DISPLAY_ADD); break; }
102
- case SDL_DISPLAYEVENT_DISCONNECTED: { packed.Set("type", events::types::DISPLAY_REMOVE); break; }
103
- case SDL_DISPLAYEVENT_ORIENTATION: {
104
- packed.Set("type", events::types::DISPLAY_ORIENT);
105
- packed.Set("orientation", video::orientations[(SDL_DisplayOrientation) event.display.data1]);
106
- break;
107
- }
108
- case SDL_DISPLAYEVENT_MOVED: {
109
- packed.Set("type", events::types::DISPLAY_MOVE);
110
-
111
- SDL_Rect rect;
112
-
113
- if(SDL_GetDisplayBounds(display_index, &rect) < 0) {
114
- std::ostringstream message;
115
- message << "SDL_GetDisplayBounds(" << display_index << ") error: " << SDL_GetError();
116
- SDL_ClearError();
117
- throw Napi::Error::New(env, message.str());
118
- }
119
- packed.Set("geometryX", rect.x);
120
- packed.Set("geometryY", rect.y);
121
-
122
- if(SDL_GetDisplayUsableBounds(display_index, &rect) < 0) {
123
- std::ostringstream message;
124
- message << "SDL_GetDisplayUsableBounds(" << display_index << ") error: " << SDL_GetError();
125
- SDL_ClearError();
126
- throw Napi::Error::New(env, message.str());
127
- }
128
- packed.Set("usableX", rect.x);
129
- packed.Set("usableY", rect.y);
130
-
131
- break;
132
- }
133
- }
174
+ case SDL_EVENT_DISPLAY_ADDED: {
175
+ Napi::Value display = video::_getDisplay(env, event.display.displayID);
176
+ if (display.IsNull()) { return false; }
177
+
178
+ packed.Set("target", events::targets::VIDEO);
179
+ packed.Set("type", events::types::DISPLAY_ADD);
180
+ packed.Set("display", display);
134
181
  break;
135
182
  }
183
+ 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);
136
207
 
137
- case SDL_WINDOWEVENT: {
138
- packed.Set("family", events::families::WINDOW);
139
- packed.Set("windowId", event.window.windowID);
140
-
141
- switch (event.window.event) {
142
- case SDL_WINDOWEVENT_SHOWN: { packed.Set("type", events::types::SHOW); break; }
143
- case SDL_WINDOWEVENT_HIDDEN: { packed.Set("type", events::types::HIDE); break; }
144
- case SDL_WINDOWEVENT_EXPOSED: { packed.Set("type", events::types::EXPOSE); break; }
145
- case SDL_WINDOWEVENT_MOVED: {
146
- packed.Set("type", events::types::MOVE);
147
- packed.Set("x", event.window.data1);
148
- packed.Set("y", event.window.data2);
149
- break;
150
- }
151
- case SDL_WINDOWEVENT_SIZE_CHANGED: {
152
- SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
153
- if (window == nullptr) {
154
- const char *error = SDL_GetError();
155
- if (error == global::no_error) { return; }
156
-
157
- std::ostringstream message;
158
- message << "SDL_GetWindowFromID() error: " << SDL_GetError();
159
- SDL_ClearError();
160
- throw Napi::Error::New(env, message.str());
161
- }
162
-
163
- int pixel_width, pixel_height;
164
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
165
-
166
- packed.Set("type", events::types::RESIZE);
167
- packed.Set("width", event.window.data1);
168
- packed.Set("height", event.window.data2);
169
- packed.Set("pixelWidth", pixel_width);
170
- packed.Set("pixelHeight", pixel_height);
171
- break;
172
- }
173
- case SDL_WINDOWEVENT_DISPLAY_CHANGED: {
174
- packed.Set("type", events::types::DISPLAY_CHANGE);
175
- packed.Set("displayIndex", event.window.data1);
176
- break;
177
- }
178
- case SDL_WINDOWEVENT_MINIMIZED: { packed.Set("type", events::types::MINIMIZE); break; }
179
- case SDL_WINDOWEVENT_MAXIMIZED: { packed.Set("type", events::types::MAXIMIZE); break; }
180
- case SDL_WINDOWEVENT_RESTORED: { packed.Set("type", events::types::RESTORE); break; }
181
- case SDL_WINDOWEVENT_FOCUS_GAINED: { packed.Set("type", events::types::FOCUS); break; }
182
- case SDL_WINDOWEVENT_FOCUS_LOST: { packed.Set("type", events::types::BLUR); break; }
183
- case SDL_WINDOWEVENT_ENTER: { packed.Set("type", events::types::HOVER); break; }
184
- case SDL_WINDOWEVENT_LEAVE: { packed.Set("type", events::types::LEAVE); break; }
185
- case SDL_WINDOWEVENT_CLOSE: { packed.Set("type", events::types::CLOSE); break; }
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;
186
220
  }
221
+ packed.Set("usableX", rect.x);
222
+ packed.Set("usableY", rect.y);
223
+
224
+ break;
225
+ }
226
+ case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
227
+ case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
228
+ case SDL_EVENT_DISPLAY_USABLE_BOUNDS_CHANGED: {
229
+ Napi::Value display = video::_getDisplay(env, event.display.displayID);
230
+ if (display.IsNull()) { return false; }
231
+
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);
240
+ break;
241
+ }
242
+
243
+ 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);
187
247
  break;
188
248
  }
189
249
 
190
- case SDL_DROPBEGIN:
191
- case SDL_DROPCOMPLETE: {
192
- packed.Set("family", events::families::DROP);
193
- packed.Set("type", event.type == SDL_DROPBEGIN ? events::types::DROP_BEGIN : events::types::DROP_COMPLETE);
194
- packed.Set("windowId", event.drop.windowID);
250
+ case SDL_EVENT_DROP_BEGIN:
251
+ 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);
195
255
  break;
196
256
  }
197
- case SDL_DROPFILE:
198
- case SDL_DROPTEXT: {
199
- packed.Set("family", events::families::DROP);
200
- bool is_file = event.type == SDL_DROPFILE;
257
+ case SDL_EVENT_DROP_FILE:
258
+ case SDL_EVENT_DROP_TEXT: {
259
+ packed.Set("target", events::targets::WINDOW);
260
+ packed.Set("targetId", event.drop.windowID);
261
+
262
+ bool is_file = event.type == SDL_EVENT_DROP_FILE;
201
263
  packed.Set("type", is_file ? events::types::DROP_FILE : events::types::DROP_TEXT);
202
- packed.Set("windowId", event.drop.windowID);
203
- packed.Set(is_file ? "file" : "text", event.drop.file);
264
+ packed.Set(is_file ? "file" : "text", event.drop.data);
204
265
  break;
205
266
  }
206
267
 
207
- case SDL_KEYDOWN:
208
- case SDL_KEYUP: {
209
- packed.Set("family", events::families::KEYBOARD);
268
+ case SDL_EVENT_KEY_DOWN:
269
+ case SDL_EVENT_KEY_UP: {
270
+ packed.Set("target", events::targets::WINDOW);
271
+ packed.Set("targetId", event.key.windowID);
210
272
  packed.Set(
211
273
  "type",
212
- event.type == SDL_KEYDOWN
274
+ event.type == SDL_EVENT_KEY_DOWN
213
275
  ? events::types::KEY_DOWN
214
276
  : events::types::KEY_UP
215
277
  );
216
- packed.Set("windowId", event.key.windowID);
217
278
 
218
- SDL_Keysym symbol = event.key.keysym;
219
- packed.Set("scancode", (int) symbol.scancode);
279
+ packed.Set("scancode", (int) event.key.scancode);
220
280
 
221
- const char *name = SDL_GetKeyName(symbol.sym);
222
- if (name[0] != '\0') {
223
- packed.Set("key", name);
224
- }
225
- else {
226
- packed.Set("key", env.Null());
227
- }
281
+ packed.Set("key", keyboard::packKey(env, event.key.key));
228
282
 
229
- if (event.type == SDL_KEYDOWN) {
230
- packed.Set("repeat", !!event.key.repeat);
283
+ if (event.type == SDL_EVENT_KEY_DOWN) {
284
+ packed.Set("repeat", event.key.repeat);
231
285
  }
232
286
 
233
- packed.Set("alt", !!(symbol.mod & KMOD_ALT));
234
- packed.Set("ctrl", !!(symbol.mod & KMOD_CTRL));
235
- packed.Set("shift", !!(symbol.mod & KMOD_SHIFT));
236
- packed.Set("super", !!(symbol.mod & KMOD_GUI));
237
- packed.Set("altgr", !!(symbol.mod & KMOD_MODE));
238
- packed.Set("numlock", !!(symbol.mod & KMOD_NUM));
239
- packed.Set("capslock", !!(symbol.mod & KMOD_CAPS));
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));
240
294
  break;
241
295
  }
242
296
 
243
- case SDL_TEXTINPUT: {
244
- packed.Set("family", events::families::TEXT);
297
+ case SDL_EVENT_TEXT_INPUT: {
298
+ packed.Set("target", events::targets::WINDOW);
299
+ packed.Set("targetId", event.text.windowID);
245
300
  packed.Set("type", events::types::TEXT_INPUT);
246
- packed.Set("windowId", event.text.windowID);
247
301
  packed.Set("text", event.text.text);
248
302
  break;
249
303
  }
250
304
 
251
- case SDL_KEYMAPCHANGED: {
252
- packed.Set("family", events::families::KEYMAP);
305
+ case SDL_EVENT_KEYMAP_CHANGED: {
306
+ packed.Set("target", events::targets::KEYBOARD);
253
307
  packed.Set("type", events::types::KEYMAP_CHANGE);
254
308
  break;
255
309
  }
256
310
 
257
- case SDL_MOUSEMOTION: {
258
- packed.Set("family", events::families::MOUSE);
311
+ case SDL_EVENT_MOUSE_MOTION: {
312
+ packed.Set("target", events::targets::WINDOW);
313
+ packed.Set("targetId", event.motion.windowID);
259
314
  packed.Set("type", events::types::MOUSE_MOVE);
260
- packed.Set("windowId", event.motion.windowID);
261
315
  packed.Set("touch", event.motion.which == SDL_TOUCH_MOUSEID);
316
+ packed.Set("pen", event.motion.which == SDL_PEN_MOUSEID);
262
317
  packed.Set("x", event.motion.x);
263
318
  packed.Set("y", event.motion.y);
319
+ packed.Set("dx", event.motion.xrel);
320
+ packed.Set("dy", event.motion.yrel);
264
321
  break;
265
322
  }
266
- case SDL_MOUSEBUTTONDOWN:
267
- case SDL_MOUSEBUTTONUP: {
268
- packed.Set("family", events::families::MOUSE);
323
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
324
+ case SDL_EVENT_MOUSE_BUTTON_UP: {
325
+ packed.Set("target", events::targets::WINDOW);
326
+ packed.Set("targetId", event.button.windowID);
269
327
  packed.Set(
270
328
  "type",
271
- event.type == SDL_MOUSEBUTTONDOWN
329
+ event.type == SDL_EVENT_MOUSE_BUTTON_DOWN
272
330
  ? events::types::MOUSE_BUTTON_DOWN
273
331
  : events::types::MOUSE_BUTTON_UP
274
332
  );
275
- packed.Set("windowId", event.button.windowID);
276
333
  packed.Set("touch", event.button.which == SDL_TOUCH_MOUSEID);
334
+ packed.Set("pen", event.button.which == SDL_PEN_MOUSEID);
277
335
  packed.Set("button", event.button.button);
278
336
  packed.Set("x", event.button.x);
279
337
  packed.Set("y", event.button.y);
280
338
  break;
281
339
  }
282
- case SDL_MOUSEWHEEL: {
283
- packed.Set("family", events::families::MOUSE);
340
+ case SDL_EVENT_MOUSE_WHEEL: {
341
+ packed.Set("target", events::targets::WINDOW);
342
+ packed.Set("targetId", event.wheel.windowID);
284
343
  packed.Set("type", events::types::MOUSE_WHEEL);
285
- packed.Set("windowId", event.wheel.windowID);
286
344
  packed.Set("touch", event.wheel.which == SDL_TOUCH_MOUSEID);
287
-
288
- int x, y;
289
- SDL_GetMouseState(&x, &y);
290
- packed.Set("x", x);
291
- packed.Set("y", y);
292
-
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);
293
348
  packed.Set("dx", event.wheel.x);
294
349
  packed.Set("dy", event.wheel.y);
295
- packed.Set("flipped", event.wheel.direction);
350
+ packed.Set("flipped", event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED);
296
351
  break;
297
352
  }
298
353
 
299
- case SDL_FINGERUP:
300
- case SDL_FINGERDOWN: {
301
- packed.Set("family", events::families::TOUCH);
302
- packed.Set("type", event.type == SDL_FINGERUP
354
+ case SDL_EVENT_FINGER_UP:
355
+ case SDL_EVENT_FINGER_DOWN:
356
+ 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
303
360
  ? events::types::FINGER_UP
304
- : events::types::FINGER_DOWN);
305
- packed.Set("windowId", event.tfinger.windowID);
306
- packed.Set("mouse", event.tfinger.touchId == SDL_MOUSE_TOUCHID);
307
- packed.Set("touchId", event.tfinger.touchId);
308
- packed.Set("fingerId", event.tfinger.fingerId);
361
+ : event.type == SDL_EVENT_FINGER_DOWN
362
+ ? events::types::FINGER_DOWN
363
+ : 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));
309
368
  packed.Set("x", event.tfinger.x);
310
369
  packed.Set("y", event.tfinger.y);
311
370
  packed.Set("pressure", event.tfinger.pressure);
312
371
  break;
313
372
  }
314
- case SDL_FINGERMOTION: {
315
- packed.Set("family", events::families::TOUCH);
373
+ case SDL_EVENT_FINGER_MOTION: {
374
+ packed.Set("target", events::targets::WINDOW);
316
375
  packed.Set("type", events::types::FINGER_MOVE);
317
- packed.Set("windowId", event.tfinger.windowID);
318
- packed.Set("mouse", event.tfinger.touchId == SDL_MOUSE_TOUCHID);
319
- packed.Set("touchId", event.tfinger.touchId);
320
- packed.Set("fingerId", event.tfinger.fingerId);
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));
321
381
  packed.Set("x", event.tfinger.x);
322
382
  packed.Set("y", event.tfinger.y);
323
383
  packed.Set("dx", event.tfinger.dx);
@@ -326,181 +386,254 @@ events::dispatchEvent(const SDL_Event &event)
326
386
  break;
327
387
  }
328
388
 
329
- case SDL_JOYDEVICEADDED: {
330
- packed.Set("family", events::families::JOYSTICK_DEVICE);
389
+ case SDL_EVENT_JOYSTICK_ADDED:
390
+ case SDL_EVENT_GAMEPAD_ADDED: {
391
+ packed.Set("target", event.type == SDL_EVENT_JOYSTICK_ADDED
392
+ ? events::targets::JOYSTICK_DEVICE
393
+ : events::targets::GAMEPAD_DEVICE);
331
394
  packed.Set("type", events::types::DEVICE_ADD);
332
- packed.Set("devices", joystick::_getDevices(env));
395
+ packed.Set("device", joystick::_getDevice(env, event.jdevice.which));
333
396
  break;
334
397
  }
335
- case SDL_JOYDEVICEREMOVED: {
336
- packed.Set("family", events::families::JOYSTICK_DEVICE);
398
+ case SDL_EVENT_JOYSTICK_REMOVED:
399
+ case SDL_EVENT_GAMEPAD_REMOVED: {
400
+ packed.Set("target", event.type == SDL_EVENT_JOYSTICK_REMOVED
401
+ ? events::targets::JOYSTICK_DEVICE
402
+ : events::targets::GAMEPAD_DEVICE);
337
403
  packed.Set("type", events::types::DEVICE_REMOVE);
338
- packed.Set("joystickId", event.jdevice.which);
339
- packed.Set("devices", joystick::_getDevices(env));
404
+ packed.Set("deviceId", event.jdevice.which);
340
405
  break;
341
406
  }
342
407
 
343
- case SDL_JOYAXISMOTION: {
344
- packed.Set("family", events::families::JOYSTICK);
408
+ case SDL_EVENT_JOYSTICK_AXIS_MOTION: {
409
+ packed.Set("target", events::targets::JOYSTICK);
410
+ packed.Set("targetId", event.jaxis.which);
345
411
  packed.Set("type", events::types::AXIS_MOTION);
346
-
347
- int joystick_id = event.jaxis.which;
348
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
349
- if (joystick == nullptr) {
350
- std::ostringstream message;
351
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
352
- SDL_ClearError();
353
- throw Napi::Error::New(env, message.str());
354
- }
355
-
356
- packed.Set("joystickId", joystick_id);
357
412
  packed.Set("axis", event.jaxis.axis);
358
- packed.Set("value", joystick::mapAxisValue(joystick, event.jaxis.axis, event.jaxis.value));
413
+ packed.Set("value", joystick::mapAxisValue(event.jaxis.value));
359
414
  break;
360
415
  }
361
- case SDL_JOYBALLMOTION: {
362
- packed.Set("family", events::families::JOYSTICK);
416
+ case SDL_EVENT_JOYSTICK_BALL_MOTION: {
417
+ packed.Set("target", events::targets::JOYSTICK);
418
+ packed.Set("targetId", event.jball.which);
363
419
  packed.Set("type", events::types::BALL_MOTION);
364
- packed.Set("joystickId", event.jball.which);
365
420
  packed.Set("ball", event.jball.ball);
366
- packed.Set("x", event.jball.xrel);
367
- packed.Set("y", event.jball.yrel);
421
+ packed.Set("dx", event.jball.xrel);
422
+ packed.Set("dy", event.jball.yrel);
368
423
  break;
369
424
  }
370
- case SDL_JOYBUTTONDOWN:
371
- case SDL_JOYBUTTONUP: {
372
- packed.Set("family", events::families::JOYSTICK);
425
+ case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
426
+ case SDL_EVENT_JOYSTICK_BUTTON_UP: {
427
+ packed.Set("target", events::targets::JOYSTICK);
428
+ packed.Set("targetId", event.jbutton.which);
373
429
  packed.Set(
374
430
  "type",
375
- event.type == SDL_JOYBUTTONDOWN
431
+ event.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN
376
432
  ? events::types::BUTTON_DOWN
377
433
  : events::types::BUTTON_UP
378
434
  );
379
- packed.Set("joystickId", event.jbutton.which);
380
435
  packed.Set("button", event.jbutton.button);
381
436
  break;
382
437
  }
383
- case SDL_JOYHATMOTION: {
384
- packed.Set("family", events::families::JOYSTICK);
438
+ case SDL_EVENT_JOYSTICK_HAT_MOTION: {
439
+ packed.Set("target", events::targets::JOYSTICK);
440
+ packed.Set("targetId", event.jhat.which);
385
441
  packed.Set("type", events::types::HAT_MOTION);
386
- packed.Set("joystickId", event.jhat.which);
387
442
  packed.Set("hat", event.jhat.hat);
388
- packed.Set("value", joystick::hat_positions[event.jhat.value]);
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);
389
448
  break;
390
449
  }
391
450
 
392
- case SDL_JOYBATTERYUPDATED: {
393
- packed.Set("family", events::families::JOYSTICK);
451
+ case SDL_EVENT_JOYSTICK_BATTERY_UPDATED: {
452
+ packed.Set("target", events::targets::JOYSTICK);
453
+ packed.Set("targetId", event.jbattery.which);
394
454
  packed.Set("type", events::types::POWER_UPDATE);
395
- packed.Set("joystickId", event.jbattery.which);
396
- packed.Set("power", joystick::mapPowerLevel(env, event.jbattery.level));
455
+ packed.Set("power", joystick::mapPowerInfo(env, event.jbattery.state, event.jbattery.percent));
397
456
  break;
398
457
  }
399
458
 
400
- case SDL_CONTROLLERSTEAMHANDLEUPDATED: {
401
- packed.Set("family", events::families::CONTROLLER);
402
- packed.Set("type", events::types::REMAP);
459
+ case SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED: {
460
+ packed.Set("target", events::targets::GAMEPAD);
461
+ packed.Set("type", events::types::STEAM_HANDLE_UPDATE);
403
462
 
404
- SDL_JoystickID controller_id = event.cdevice.which;
405
- SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
406
- if (controller == nullptr) {
407
- std::ostringstream message;
408
- message << "SDL_GameControllerFromInstanceID(" << controller_id << ") error: " << SDL_GetError();
463
+ SDL_JoystickID gamepad_id = event.gdevice.which;
464
+ SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
465
+ if (gamepad == nullptr) {
409
466
  SDL_ClearError();
410
- throw Napi::Error::New(env, message.str());
467
+ return false;
411
468
  }
412
469
 
413
- packed.Set("controllerId", controller_id);
414
- packed.Set("steamHandle", controller::getSteamHandle(env, controller));
470
+ packed.Set("targetId", gamepad_id);
471
+ packed.Set("steamHandle", gamepad::getSteamHandle(env, gamepad));
415
472
  break;
416
473
  }
417
474
 
418
- case SDL_CONTROLLERDEVICEREMAPPED: {
419
- packed.Set("family", events::families::CONTROLLER);
475
+ case SDL_EVENT_GAMEPAD_REMAPPED: {
476
+ packed.Set("target", events::targets::GAMEPAD);
420
477
  packed.Set("type", events::types::REMAP);
421
478
 
422
- SDL_JoystickID controller_id = event.cdevice.which;
423
- SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
424
- if (controller == nullptr) {
425
- std::ostringstream message;
426
- message << "SDL_GameControllerFromInstanceID(" << controller_id << ") error: " << SDL_GetError();
479
+ SDL_JoystickID gamepad_id = event.gdevice.which;
480
+ SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
481
+ if (gamepad == nullptr) {
427
482
  SDL_ClearError();
428
- throw Napi::Error::New(env, message.str());
483
+ return false;
429
484
  }
430
485
 
431
- packed.Set("controllerId", controller_id);
432
- controller::getState(env, controller, packed);
486
+ packed.Set("targetId", gamepad_id);
487
+ packed.Set("device", joystick::_getDevice(env, gamepad_id));
488
+ gamepad::getState(env, gamepad, packed);
433
489
  break;
434
490
  }
435
491
 
436
- case SDL_CONTROLLERAXISMOTION: {
437
- packed.Set("family", events::families::CONTROLLER);
492
+ case SDL_EVENT_GAMEPAD_AXIS_MOTION: {
493
+ packed.Set("target", events::targets::GAMEPAD);
438
494
  packed.Set("type", events::types::AXIS_MOTION);
439
495
 
440
- int controller_id = event.caxis.which;
441
- SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
442
- if (controller == nullptr) {
443
- std::ostringstream message;
444
- message << "SDL_GameControllerFromInstanceID(" << controller_id << ") error: " << SDL_GetError();
496
+ int gamepad_id = event.gaxis.which;
497
+ SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
498
+ if (gamepad == nullptr) {
445
499
  SDL_ClearError();
446
- throw Napi::Error::New(env, message.str());
500
+ return false;
447
501
  }
448
- SDL_GameControllerAxis axis = (SDL_GameControllerAxis) event.caxis.axis;
449
502
 
450
- packed.Set("controllerId", controller_id);
451
- packed.Set("axis", controller::axes[axis]);
452
- packed.Set("value", controller::mapAxisValue(controller, axis, event.caxis.value));
503
+ packed.Set("targetId", gamepad_id);
504
+
505
+ SDL_GamepadAxis axis = (SDL_GamepadAxis) event.gaxis.axis;
506
+ auto axis_entry = gamepad::axes.find(axis);
507
+ if (axis_entry == gamepad::axes.end()) { return false; }
508
+ packed.Set("axis", axis_entry->second);
509
+ packed.Set("value", gamepad::mapAxisValue(event.gaxis.value));
453
510
  break;
454
511
  }
455
- case SDL_CONTROLLERBUTTONDOWN:
456
- case SDL_CONTROLLERBUTTONUP: {
457
- packed.Set("family", events::families::CONTROLLER);
512
+ case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
513
+ case SDL_EVENT_GAMEPAD_BUTTON_UP: {
514
+ packed.Set("target", events::targets::GAMEPAD);
515
+ packed.Set("targetId", event.gbutton.which);
458
516
  packed.Set(
459
517
  "type",
460
- event.type == SDL_CONTROLLERBUTTONDOWN
518
+ event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN
461
519
  ? events::types::BUTTON_DOWN
462
520
  : events::types::BUTTON_UP
463
521
  );
464
- packed.Set("controllerId", event.cbutton.which);
465
- packed.Set("button", controller::buttons[(SDL_GameControllerButton) event.cbutton.button]);
522
+
523
+ auto button_entry = gamepad::buttons.find((SDL_GamepadButton) event.gbutton.button);
524
+ if (button_entry == gamepad::buttons.end()) { return false; }
525
+ packed.Set("button", button_entry->second);
466
526
  break;
467
527
  }
468
528
 
469
- case SDL_SENSORUPDATE: {
470
- packed.Set("family", events::families::SENSOR);
529
+ case SDL_EVENT_SENSOR_UPDATE: {
530
+ packed.Set("target", events::targets::SENSOR);
531
+ packed.Set("targetId", event.sensor.which);
471
532
  packed.Set("type", events::types::UPDATE);
472
- packed.Set("sensorId", event.sensor.which);
473
533
  break;
474
534
  }
475
535
 
476
- case SDL_AUDIODEVICEADDED: {
477
- packed.Set("family", events::families::AUDIO_DEVICE);
536
+ case SDL_EVENT_AUDIO_DEVICE_ADDED: {
537
+ bool is_recording = event.adevice.recording;
538
+
539
+ Napi::Value device = audio::_getDevice(env, event.adevice.which);
540
+ if (device.IsNull()) { return false; }
541
+
542
+ packed.Set("target", events::targets::AUDIO_DEVICE);
478
543
  packed.Set("type", events::types::DEVICE_ADD);
479
- bool is_capture = event.adevice.iscapture;
480
- packed.Set("audioDeviceType", audio::device_types[is_capture]);
481
- packed.Set("devices", audio::_getDevices(env, is_capture));
544
+ packed.Set("audioDeviceType", audio::device_types[is_recording]);
545
+ packed.Set("device", device);
482
546
  break;
483
547
  }
484
- case SDL_AUDIODEVICEREMOVED: {
485
- packed.Set("family", events::families::AUDIO_DEVICE);
548
+ case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
549
+ packed.Set("target", events::targets::AUDIO_DEVICE);
486
550
  packed.Set("type", events::types::DEVICE_REMOVE);
487
-
488
- bool is_capture = event.adevice.iscapture;
489
-
490
- packed.Set("audioId", event.adevice.which);
491
- packed.Set("audioDeviceType", audio::device_types[is_capture]);
492
- packed.Set("devices", audio::_getDevices(env, is_capture));
551
+ packed.Set("audioDeviceType", audio::device_types[event.adevice.recording]);
552
+ packed.Set("deviceId", event.adevice.which);
553
+ break;
554
+ }
555
+ 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);
493
560
  break;
494
561
  }
495
562
 
496
- case SDL_CLIPBOARDUPDATE: {
497
- packed.Set("family", events::families::CLIPBOARD);
563
+ case SDL_EVENT_CLIPBOARD_UPDATE: {
564
+ packed.Set("target", events::targets::CLIPBOARD);
498
565
  packed.Set("type", events::types::UPDATE);
499
566
  break;
500
567
  }
501
568
  }
502
569
 
570
+ if (!packed.Has("type")) { return false; }
571
+
503
572
  poll_callback->Call(poll_env->Global(), { packed });
573
+
574
+ return true;
575
+ }
576
+
577
+ // We need to track events dispatched from the watcher to avoid duplicates.
578
+ struct WatchDispatchedEvent {
579
+ Uint64 timestamp;
580
+ Uint32 window_id;
581
+ SDL_EventType window_event;
582
+ Sint32 data1;
583
+ Sint32 data2;
584
+ };
585
+
586
+ static std::vector<WatchDispatchedEvent> watch_dispatched_events;
587
+
588
+ static bool
589
+ wasDispatchedFromWatch (const SDL_Event &event)
590
+ {
591
+ if (event.type < SDL_EVENT_WINDOW_FIRST || event.type > SDL_EVENT_WINDOW_LAST) { return false; }
592
+
593
+ for (auto it = watch_dispatched_events.begin(); it != watch_dispatched_events.end(); ++it) {
594
+ if (true
595
+ && it->timestamp == event.common.timestamp
596
+ && it->window_id == event.window.windowID
597
+ && it->window_event == event.window.type
598
+ && it->data1 == event.window.data1
599
+ && it->data2 == event.window.data2
600
+ ) {
601
+ watch_dispatched_events.erase(it);
602
+ return true;
603
+ }
604
+ }
605
+
606
+ return false;
607
+ }
608
+
609
+ static int dispatching_from_watch = 0;
610
+
611
+ Napi::Value
612
+ events::isDispatchingFromWatch (const Napi::CallbackInfo &info)
613
+ {
614
+ Napi::Env env = info.Env();
615
+
616
+ return Napi::Boolean::New(env, dispatching_from_watch > 0);
617
+ }
618
+
619
+ bool
620
+ events::dispatchEventFromWatch(const SDL_Event &event)
621
+ {
622
+ dispatching_from_watch++;
623
+ bool dispatched = events::dispatchEvent(event);
624
+ dispatching_from_watch--;
625
+
626
+ if (dispatched) {
627
+ watch_dispatched_events.push_back({
628
+ event.common.timestamp,
629
+ event.window.windowID,
630
+ event.window.type,
631
+ event.window.data1,
632
+ event.window.data2,
633
+ });
634
+ }
635
+
636
+ return dispatched;
504
637
  }
505
638
 
506
639
  Napi::Value
@@ -510,12 +643,32 @@ events::poll (const Napi::CallbackInfo &info)
510
643
 
511
644
  Napi::Function callback = info[0].As<Napi::Function>();
512
645
 
646
+ Uint64 poll_start = SDL_GetTicksNS();
647
+
513
648
  poll_env = &env;
514
649
  poll_callback = &callback;
515
650
 
516
- SDL_Event event;
517
- while (SDL_PollEvent(&event)) { events::dispatchEvent(event); }
651
+ try {
652
+ SDL_Event event;
653
+ while (SDL_PollEvent(&event)) {
654
+ if (wasDispatchedFromWatch(event)) { continue; }
655
+ events::dispatchEvent(event);
656
+ }
518
657
 
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
+ }
519
672
  poll_env = nullptr;
520
673
  poll_callback = nullptr;
521
674