@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.
- package/CHANGELOG.md +12 -6
- package/binding.gyp +3 -0
- package/package.json +5 -12
- package/scripts/util/common.js +2 -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 +100 -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 +12 -9
- 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 +33 -27
- package/src/javascript/events/index.js +75 -482
- package/src/javascript/events/try.js +22 -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 +175 -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 +114 -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 +144 -161
- package/src/native/audio.cpp +457 -151
- package/src/native/audio.h +12 -6
- package/src/native/clipboard.cpp +8 -37
- package/src/native/enums.cpp +2 -0
- package/src/native/events.cpp +416 -354
- package/src/native/events.h +67 -65
- package/src/native/gamepad.cpp +86 -103
- package/src/native/gamepad.h +7 -8
- package/src/native/global.cpp +190 -85
- package/src/native/global.h +48 -2
- package/src/native/joystick.cpp +180 -274
- package/src/native/joystick.h +12 -6
- package/src/native/keyboard.cpp +60 -22
- package/src/native/keyboard.h +2 -1
- package/src/native/module.cpp +20 -3
- package/src/native/mouse.cpp +27 -58
- package/src/native/packed.cpp +106 -0
- package/src/native/packed.h +91 -0
- package/src/native/power.cpp +7 -1
- package/src/native/power.h +2 -2
- package/src/native/sensor.cpp +61 -69
- package/src/native/sensor.h +3 -3
- package/src/native/testing.cpp +131 -0
- package/src/native/testing.h +16 -0
- package/src/native/threads.cpp +300 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +32 -43
- package/src/native/touch.h +2 -1
- package/src/native/video.cpp +145 -95
- package/src/native/video.h +6 -3
- package/src/native/window.cpp +155 -218
- package/src/native/window.h +21 -20
- package/src/types/index.d.ts +141 -69
- 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,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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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,
|
|
153
|
+
_packWindowEvent (const SDL_Event &event, Packing &packed)
|
|
112
154
|
{
|
|
113
|
-
const
|
|
155
|
+
const packed::Str *type = _windowEventType(event.type);
|
|
114
156
|
if (type == nullptr) { return false; }
|
|
115
157
|
|
|
116
|
-
packed.
|
|
117
|
-
packed.
|
|
118
|
-
packed.
|
|
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.
|
|
123
|
-
packed.
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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.
|
|
141
|
-
packed.
|
|
142
|
-
packed.
|
|
143
|
-
packed.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
206
|
+
return _packWindowEvent(event, packed);
|
|
166
207
|
}
|
|
167
|
-
|
|
208
|
+
|
|
209
|
+
switch (event.type) {
|
|
168
210
|
case SDL_EVENT_QUIT: {
|
|
169
|
-
packed.
|
|
170
|
-
packed.
|
|
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
|
-
|
|
176
|
-
if (display.
|
|
217
|
+
packed::Value display = video::_getDisplay(event.display.displayID);
|
|
218
|
+
if (display.isNull()) { return false; }
|
|
177
219
|
|
|
178
|
-
packed.
|
|
179
|
-
packed.
|
|
180
|
-
packed.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
230
|
-
if (display.
|
|
236
|
+
packed::Value display = video::_getDisplay(event.display.displayID);
|
|
237
|
+
if (display.isNull()) { return false; }
|
|
231
238
|
|
|
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);
|
|
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.
|
|
245
|
-
packed.
|
|
246
|
-
packed.
|
|
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.
|
|
253
|
-
packed.
|
|
254
|
-
packed.
|
|
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.
|
|
260
|
-
packed.
|
|
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.
|
|
264
|
-
packed.
|
|
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.
|
|
271
|
-
packed.
|
|
272
|
-
packed.
|
|
273
|
-
|
|
274
|
-
|
|
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.
|
|
289
|
+
packed.set("scancode", (int) event.key.scancode);
|
|
280
290
|
|
|
281
|
-
packed.
|
|
291
|
+
packed.set("key", keyboard::packKey(event.key.key));
|
|
282
292
|
|
|
283
293
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
|
284
|
-
packed.
|
|
294
|
+
packed.set("repeat", event.key.repeat);
|
|
285
295
|
}
|
|
286
296
|
|
|
287
|
-
packed.
|
|
288
|
-
packed.
|
|
289
|
-
packed.
|
|
290
|
-
packed.
|
|
291
|
-
packed.
|
|
292
|
-
packed.
|
|
293
|
-
packed.
|
|
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.
|
|
299
|
-
packed.
|
|
300
|
-
packed.
|
|
301
|
-
packed.
|
|
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.
|
|
307
|
-
packed.
|
|
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.
|
|
313
|
-
packed.
|
|
314
|
-
packed.
|
|
315
|
-
packed.
|
|
316
|
-
packed.
|
|
317
|
-
packed.
|
|
318
|
-
packed.
|
|
319
|
-
packed.
|
|
320
|
-
packed.
|
|
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.
|
|
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);
|
|
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.
|
|
342
|
-
packed.
|
|
343
|
-
packed.
|
|
344
|
-
packed.
|
|
345
|
-
packed.
|
|
346
|
-
packed.
|
|
347
|
-
packed.
|
|
348
|
-
packed.
|
|
349
|
-
packed.
|
|
350
|
-
packed.
|
|
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.
|
|
358
|
-
packed.
|
|
359
|
-
packed.
|
|
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.
|
|
365
|
-
packed.
|
|
366
|
-
packed.
|
|
367
|
-
packed.
|
|
368
|
-
packed.
|
|
369
|
-
packed.
|
|
370
|
-
packed.
|
|
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.
|
|
375
|
-
packed.
|
|
376
|
-
packed.
|
|
377
|
-
packed.
|
|
378
|
-
packed.
|
|
379
|
-
packed.
|
|
380
|
-
packed.
|
|
381
|
-
packed.
|
|
382
|
-
packed.
|
|
383
|
-
packed.
|
|
384
|
-
packed.
|
|
385
|
-
packed.
|
|
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
|
-
|
|
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.
|
|
395
|
-
packed.
|
|
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.
|
|
408
|
+
packed.setTarget(event.type == SDL_EVENT_JOYSTICK_REMOVED
|
|
401
409
|
? events::targets::JOYSTICK_DEVICE
|
|
402
410
|
: events::targets::GAMEPAD_DEVICE);
|
|
403
|
-
packed.
|
|
404
|
-
packed.
|
|
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.
|
|
410
|
-
packed.
|
|
411
|
-
packed.
|
|
412
|
-
packed.
|
|
413
|
-
packed.
|
|
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.
|
|
418
|
-
packed.
|
|
419
|
-
packed.
|
|
420
|
-
packed.
|
|
421
|
-
packed.
|
|
422
|
-
packed.
|
|
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.
|
|
428
|
-
packed.
|
|
429
|
-
packed.
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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.
|
|
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);
|
|
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.
|
|
453
|
-
packed.
|
|
454
|
-
packed.
|
|
455
|
-
packed.
|
|
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.
|
|
461
|
-
packed.
|
|
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.
|
|
471
|
-
packed.
|
|
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.
|
|
477
|
-
packed.
|
|
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.
|
|
487
|
-
packed.
|
|
488
|
-
gamepad::getState(
|
|
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.
|
|
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);
|
|
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.
|
|
509
|
-
packed.
|
|
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.
|
|
515
|
-
packed.
|
|
516
|
-
packed.
|
|
517
|
-
|
|
518
|
-
|
|
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.
|
|
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.
|
|
531
|
-
packed.
|
|
532
|
-
packed.
|
|
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
|
-
|
|
540
|
-
if (device.
|
|
535
|
+
packed::Value device = audio::_getDevice(event.adevice.which);
|
|
536
|
+
if (device.isNull()) { return false; }
|
|
541
537
|
|
|
542
|
-
packed.
|
|
543
|
-
packed.
|
|
544
|
-
packed.
|
|
545
|
-
packed.
|
|
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.
|
|
550
|
-
packed.
|
|
551
|
-
packed.
|
|
552
|
-
packed.
|
|
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.
|
|
557
|
-
packed.
|
|
558
|
-
packed.
|
|
559
|
-
packed.
|
|
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.
|
|
565
|
-
packed.
|
|
564
|
+
packed.setTarget(events::targets::CLIPBOARD);
|
|
565
|
+
packed.setType(events::types::UPDATE);
|
|
566
566
|
break;
|
|
567
567
|
}
|
|
568
568
|
}
|
|
569
569
|
|
|
570
|
-
|
|
570
|
+
return packed.value.has("type");
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
static bool in_callback = false;
|
|
571
574
|
|
|
572
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
649
|
-
poll_callback = &callback;
|
|
718
|
+
threads::drain(env, callback);
|
|
650
719
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
while (SDL_PollEvent(&event)) {
|
|
654
|
-
if (wasDispatchedFromWatch(event)) { continue; }
|
|
655
|
-
events::dispatchEvent(event);
|
|
656
|
-
}
|
|
720
|
+
return env.Undefined();
|
|
721
|
+
}
|
|
657
722
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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
|
}
|