@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.
- package/CHANGELOG.md +12 -6
- package/README.md +2 -2
- package/binding.gyp +21 -4
- package/package.json +5 -12
- package/scripts/build.mjs +3 -1
- package/scripts/install.mjs +2 -0
- package/scripts/util/common.js +3 -2
- package/src/javascript/audio/audio-playback-stream.js +9 -10
- package/src/javascript/audio/audio-recording-stream.js +9 -8
- package/src/javascript/audio/audio-stream.js +101 -44
- package/src/javascript/audio/conversion.js +41 -0
- package/src/javascript/audio/devices.js +11 -0
- package/src/javascript/audio/events.js +65 -0
- package/src/javascript/audio/format-helpers.js +8 -4
- package/src/javascript/audio/index.js +25 -20
- package/src/javascript/audio/prevent-exit.js +13 -10
- package/src/javascript/audio/validate.js +9 -0
- package/src/javascript/cleanup.js +9 -6
- package/src/javascript/clipboard/index.js +8 -3
- package/src/javascript/device-instance.js +61 -0
- package/src/javascript/events/events-via-poll.js +32 -28
- package/src/javascript/events/index.js +85 -479
- package/src/javascript/events/try.js +25 -0
- package/src/javascript/gamepad/{device.js → devices.js} +4 -5
- package/src/javascript/gamepad/events.js +66 -0
- package/src/javascript/gamepad/gamepad-instance.js +12 -160
- package/src/javascript/gamepad/index.js +15 -16
- package/src/javascript/globals.js +10 -1
- package/src/javascript/index.js +2 -5
- package/src/javascript/joystick/devices.js +43 -0
- package/src/javascript/joystick/events.js +129 -0
- package/src/javascript/joystick/index.js +9 -18
- package/src/javascript/joystick/joystick-device-instance.js +176 -0
- package/src/javascript/joystick/joystick-instance.js +12 -158
- package/src/javascript/keyboard/index.js +20 -10
- package/src/javascript/keyboard/key-mapping.js +46 -27
- package/src/javascript/mouse/index.js +36 -29
- package/src/javascript/sensor/events.js +21 -0
- package/src/javascript/sensor/index.js +13 -2
- package/src/javascript/sensor/sensor-instance.js +7 -47
- package/src/javascript/subsystems.js +56 -0
- package/src/javascript/touch/index.js +9 -4
- package/src/javascript/validate.js +119 -0
- package/src/javascript/video/conversion.js +63 -0
- package/src/javascript/video/events.js +179 -0
- package/src/javascript/video/format-helpers.js +10 -8
- package/src/javascript/video/index.js +28 -14
- package/src/javascript/video/validate.js +45 -0
- package/src/javascript/video/window.js +196 -167
- package/src/native/audio.cpp +457 -153
- package/src/native/audio.h +25 -19
- package/src/native/clipboard.cpp +8 -37
- package/src/native/clipboard.h +2 -2
- package/src/native/enums.cpp +16 -1
- package/src/native/enums.h +1 -1
- package/src/native/events.cpp +351 -357
- package/src/native/events.h +70 -68
- package/src/native/gamepad.cpp +147 -103
- package/src/native/gamepad.h +10 -11
- package/src/native/global.cpp +183 -340
- package/src/native/global.h +51 -5
- package/src/native/joystick.cpp +204 -275
- package/src/native/joystick.h +21 -15
- package/src/native/keyboard.cpp +63 -25
- package/src/native/keyboard.h +5 -4
- package/src/native/module.cpp +23 -3
- package/src/native/mouse.cpp +28 -59
- package/src/native/mouse.h +9 -9
- package/src/native/packed.cpp +99 -0
- package/src/native/packed.h +86 -0
- package/src/native/power.cpp +13 -1
- package/src/native/power.h +3 -3
- package/src/native/sensor.cpp +78 -69
- package/src/native/sensor.h +7 -7
- package/src/native/testing.cpp +148 -0
- package/src/native/testing.h +17 -0
- package/src/native/threads.cpp +301 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +38 -44
- package/src/native/touch.h +3 -2
- package/src/native/video.cpp +211 -98
- package/src/native/video.h +7 -4
- package/src/native/window.cpp +201 -219
- package/src/native/window.h +24 -20
- package/src/types/index.d.ts +157 -70
- package/src/javascript/audio/device.js +0 -37
- package/src/javascript/joystick/device.js +0 -13
- package/src/javascript/joystick/shared.js +0 -90
- package/src/javascript/worker.js +0 -88
- package/src/types/worker.d.ts +0 -71
package/src/native/events.cpp
CHANGED
|
@@ -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
|
-
|
|
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,
|
|
85
|
+
_packWindowEvent (const SDL_Event &event, Packing &packed)
|
|
112
86
|
{
|
|
113
|
-
const
|
|
87
|
+
const packed::Str *type = _windowEventType(event.type);
|
|
114
88
|
if (type == nullptr) { return false; }
|
|
115
89
|
|
|
116
|
-
packed.
|
|
117
|
-
packed.
|
|
118
|
-
packed.
|
|
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.
|
|
123
|
-
packed.
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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.
|
|
141
|
-
packed.
|
|
142
|
-
packed.
|
|
143
|
-
packed.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
138
|
+
return _packWindowEvent(event, packed);
|
|
166
139
|
}
|
|
167
|
-
|
|
140
|
+
|
|
141
|
+
switch (event.type) {
|
|
168
142
|
case SDL_EVENT_QUIT: {
|
|
169
|
-
packed.
|
|
170
|
-
packed.
|
|
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
|
-
|
|
176
|
-
if (display.
|
|
149
|
+
packed::Value display = video::_getDisplay(event.display.displayID);
|
|
150
|
+
if (display.isNull()) { return false; }
|
|
177
151
|
|
|
178
|
-
packed.
|
|
179
|
-
packed.
|
|
180
|
-
packed.
|
|
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.
|
|
185
|
-
packed.
|
|
186
|
-
packed.
|
|
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
|
-
|
|
230
|
-
if (display.
|
|
168
|
+
packed::Value display = video::_getDisplay(event.display.displayID);
|
|
169
|
+
if (display.isNull()) { return false; }
|
|
231
170
|
|
|
232
|
-
packed.
|
|
233
|
-
packed.
|
|
234
|
-
|
|
235
|
-
|
|
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.
|
|
245
|
-
packed.
|
|
246
|
-
packed.
|
|
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.
|
|
253
|
-
packed.
|
|
254
|
-
packed.
|
|
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.
|
|
260
|
-
packed.
|
|
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.
|
|
264
|
-
packed.
|
|
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.
|
|
271
|
-
packed.
|
|
272
|
-
packed.
|
|
273
|
-
|
|
274
|
-
|
|
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.
|
|
221
|
+
packed.set("scancode", (int) event.key.scancode);
|
|
280
222
|
|
|
281
|
-
packed.
|
|
223
|
+
packed.set("key", keyboard::packKey(event.key.key));
|
|
282
224
|
|
|
283
225
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
|
284
|
-
packed.
|
|
226
|
+
packed.set("repeat", event.key.repeat);
|
|
285
227
|
}
|
|
286
228
|
|
|
287
|
-
packed.
|
|
288
|
-
packed.
|
|
289
|
-
packed.
|
|
290
|
-
packed.
|
|
291
|
-
packed.
|
|
292
|
-
packed.
|
|
293
|
-
packed.
|
|
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.
|
|
299
|
-
packed.
|
|
300
|
-
packed.
|
|
301
|
-
packed.
|
|
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.
|
|
307
|
-
packed.
|
|
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.
|
|
313
|
-
packed.
|
|
314
|
-
packed.
|
|
315
|
-
packed.
|
|
316
|
-
packed.
|
|
317
|
-
packed.
|
|
318
|
-
packed.
|
|
319
|
-
packed.
|
|
320
|
-
packed.
|
|
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.
|
|
326
|
-
packed.
|
|
327
|
-
packed.
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
);
|
|
333
|
-
packed.
|
|
334
|
-
packed.
|
|
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.
|
|
342
|
-
packed.
|
|
343
|
-
packed.
|
|
344
|
-
packed.
|
|
345
|
-
packed.
|
|
346
|
-
packed.
|
|
347
|
-
packed.
|
|
348
|
-
packed.
|
|
349
|
-
packed.
|
|
350
|
-
packed.
|
|
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.
|
|
358
|
-
packed.
|
|
359
|
-
packed.
|
|
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.
|
|
365
|
-
packed.
|
|
366
|
-
packed.
|
|
367
|
-
packed.
|
|
368
|
-
packed.
|
|
369
|
-
packed.
|
|
370
|
-
packed.
|
|
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.
|
|
375
|
-
packed.
|
|
376
|
-
packed.
|
|
377
|
-
packed.
|
|
378
|
-
packed.
|
|
379
|
-
packed.
|
|
380
|
-
packed.
|
|
381
|
-
packed.
|
|
382
|
-
packed.
|
|
383
|
-
packed.
|
|
384
|
-
packed.
|
|
385
|
-
packed.
|
|
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
|
-
|
|
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.
|
|
395
|
-
packed.
|
|
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.
|
|
340
|
+
packed.setTarget(event.type == SDL_EVENT_JOYSTICK_REMOVED
|
|
401
341
|
? events::targets::JOYSTICK_DEVICE
|
|
402
342
|
: events::targets::GAMEPAD_DEVICE);
|
|
403
|
-
packed.
|
|
404
|
-
packed.
|
|
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.
|
|
410
|
-
packed.
|
|
411
|
-
packed.
|
|
412
|
-
packed.
|
|
413
|
-
packed.
|
|
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.
|
|
418
|
-
packed.
|
|
419
|
-
packed.
|
|
420
|
-
packed.
|
|
421
|
-
packed.
|
|
422
|
-
packed.
|
|
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.
|
|
428
|
-
packed.
|
|
429
|
-
packed.
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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.
|
|
440
|
-
packed.
|
|
441
|
-
packed.
|
|
442
|
-
packed.
|
|
443
|
-
|
|
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.
|
|
453
|
-
packed.
|
|
454
|
-
packed.
|
|
455
|
-
packed.
|
|
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.
|
|
461
|
-
packed.
|
|
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.
|
|
471
|
-
packed.
|
|
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.
|
|
477
|
-
packed.
|
|
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.
|
|
487
|
-
packed.
|
|
488
|
-
gamepad::getState(
|
|
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.
|
|
494
|
-
packed.
|
|
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.
|
|
509
|
-
packed.
|
|
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.
|
|
515
|
-
packed.
|
|
516
|
-
packed.
|
|
517
|
-
|
|
518
|
-
|
|
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.
|
|
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.
|
|
531
|
-
packed.
|
|
532
|
-
packed.
|
|
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
|
-
|
|
540
|
-
if (device.
|
|
467
|
+
packed::Value device = audio::_getDevice(event.adevice.which);
|
|
468
|
+
if (device.isNull()) { return false; }
|
|
541
469
|
|
|
542
|
-
packed.
|
|
543
|
-
packed.
|
|
544
|
-
packed.
|
|
545
|
-
packed.
|
|
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.
|
|
550
|
-
packed.
|
|
551
|
-
packed.
|
|
552
|
-
packed.
|
|
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.
|
|
557
|
-
packed.
|
|
558
|
-
packed.
|
|
559
|
-
packed.
|
|
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.
|
|
565
|
-
packed.
|
|
496
|
+
packed.setTarget(events::targets::CLIPBOARD);
|
|
497
|
+
packed.setType(events::types::UPDATE);
|
|
566
498
|
break;
|
|
567
499
|
}
|
|
568
500
|
}
|
|
569
501
|
|
|
570
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
649
|
-
poll_callback = &callback;
|
|
650
|
+
threads::drain(env, callback);
|
|
650
651
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
while (SDL_PollEvent(&event)) {
|
|
654
|
-
if (wasDispatchedFromWatch(event)) { continue; }
|
|
655
|
-
events::dispatchEvent(event);
|
|
656
|
-
}
|
|
652
|
+
return env.Undefined();
|
|
653
|
+
}
|
|
657
654
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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
|
}
|