@kmamal/sdl 1.0.0-5 → 1.0.0-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -0,0 +1,301 @@
1
+ #include "threads.h"
2
+ #include "global.h"
3
+ #include "events.h"
4
+ #include <map>
5
+ #include <set>
6
+ #include <deque>
7
+ #include <mutex>
8
+ #include <atomic>
9
+
10
+
11
+ struct Thread {
12
+ napi_threadsafe_function wake = nullptr;
13
+ std::deque<threads::Event> queue;
14
+ std::multiset<Uint32> opens[3];
15
+ bool is_main = false;
16
+ bool demand = false;
17
+ bool wake_pending = false;
18
+ };
19
+
20
+ // Lock order must always be global::lock() before this mutex.
21
+ static std::mutex mutex;
22
+ static std::map<napi_env, Thread> all_threads;
23
+ static std::atomic<int> num_demanding { 0 };
24
+ static std::atomic<int> num_workers { 0 };
25
+
26
+
27
+ static void
28
+ _wake (Thread &thread)
29
+ {
30
+ if (thread.wake_pending || thread.wake == nullptr) { return; }
31
+ thread.wake_pending = true;
32
+ if (napi_call_threadsafe_function(thread.wake, nullptr, napi_tsfn_nonblocking) != napi_ok) {
33
+ thread.wake_pending = false;
34
+ }
35
+ }
36
+
37
+ static bool
38
+ _wants (const Thread &thread, const threads::Event &event)
39
+ {
40
+ if (thread.is_main) { return true; }
41
+
42
+ const packed::Str *target = event.target;
43
+ if (target == nullptr) { return true; }
44
+ if (false
45
+ || target == &events::targets::JOYSTICK_DEVICE
46
+ || target == &events::targets::GAMEPAD_DEVICE
47
+ || target == &events::targets::AUDIO_DEVICE
48
+ ) { return true; }
49
+ if (target == &events::targets::JOYSTICK || target == &events::targets::GAMEPAD) {
50
+ return thread.opens[threads::JOYSTICK].count(event.target_id) > 0
51
+ || thread.opens[threads::GAMEPAD].count(event.target_id) > 0;
52
+ }
53
+ if (target == &events::targets::SENSOR) {
54
+ return thread.opens[threads::SENSOR].count(event.target_id) > 0;
55
+ }
56
+ return false;
57
+ }
58
+
59
+ static void
60
+ _closeOpens (Thread &thread)
61
+ {
62
+ for (Uint32 id : thread.opens[threads::JOYSTICK]) {
63
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(id);
64
+ if (joystick != nullptr) { SDL_CloseJoystick(joystick); }
65
+ }
66
+ for (Uint32 id : thread.opens[threads::GAMEPAD]) {
67
+ SDL_Gamepad *gamepad = SDL_GetGamepadFromID(id);
68
+ if (gamepad != nullptr) { SDL_CloseGamepad(gamepad); }
69
+ }
70
+ for (Uint32 id : thread.opens[threads::SENSOR]) {
71
+ SDL_Sensor *sensor = SDL_GetSensorFromID(id);
72
+ if (sensor != nullptr) { SDL_CloseSensor(sensor); }
73
+ }
74
+ SDL_ClearError();
75
+ }
76
+
77
+ static void _envCleanup (void *arg);
78
+
79
+ static void
80
+ _leaveEnv (napi_env env, bool from_hook)
81
+ {
82
+ Thread thread;
83
+ {
84
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
85
+
86
+ {
87
+ std::lock_guard<std::mutex> registry_guard(mutex);
88
+ auto it = all_threads.find(env);
89
+ if (it == all_threads.end()) { return; }
90
+ thread = std::move(it->second);
91
+ all_threads.erase(it);
92
+ if (thread.demand) { num_demanding.fetch_sub(1, std::memory_order_acq_rel); }
93
+ if (!thread.is_main) { num_workers.fetch_sub(1, std::memory_order_acq_rel); }
94
+ }
95
+
96
+ if (global::isReady()) { _closeOpens(thread); }
97
+ }
98
+
99
+ // Not needed: the loop never runs again after this, and the abort release finalizes the function whether or not it is ref'd.
100
+ // if (thread.demand) { napi_unref_threadsafe_function(env, thread.wake); }
101
+ napi_release_threadsafe_function(thread.wake, napi_tsfn_abort);
102
+ if (!from_hook) { napi_remove_env_cleanup_hook(env, _envCleanup, (void *) env); }
103
+
104
+ if (thread.is_main) { global::teardown(); }
105
+ }
106
+
107
+ static void
108
+ _envCleanup (void *arg)
109
+ {
110
+ _leaveEnv((napi_env) arg, true);
111
+ }
112
+
113
+ void
114
+ threads::leave (napi_env env)
115
+ {
116
+ _leaveEnv(env, false);
117
+ }
118
+
119
+ void
120
+ threads::trackOpen (Napi::Env &env, Kind kind, Uint32 id)
121
+ {
122
+ std::lock_guard<std::mutex> guard(mutex);
123
+
124
+ auto it = all_threads.find(env);
125
+ if (it == all_threads.end()) { return; }
126
+
127
+ it->second.opens[kind].insert(id);
128
+ }
129
+
130
+ void
131
+ threads::trackClose (Napi::Env &env, Kind kind, Uint32 id)
132
+ {
133
+ std::lock_guard<std::mutex> guard(mutex);
134
+
135
+ auto it = all_threads.find(env);
136
+ if (it == all_threads.end()) { return; }
137
+
138
+ auto &ids = it->second.opens[kind];
139
+ auto entry = ids.find(id);
140
+ if (entry != ids.end()) { ids.erase(entry); }
141
+ }
142
+
143
+ void
144
+ threads::distribute (const Event &event, napi_env except)
145
+ {
146
+ std::lock_guard<std::mutex> guard(mutex);
147
+
148
+ for (auto &entry : all_threads) {
149
+ if (entry.first == except) { continue; }
150
+ Thread &thread = entry.second;
151
+ if (!_wants(thread, event)) { continue; }
152
+ // Unbounded and uncoalesced on purpose: a blocked worker can't observe the queue anyway, and dropping or merging events would make poll() on a worker differ from the main thread.
153
+ thread.queue.push_back(event);
154
+ _wake(thread);
155
+ }
156
+ }
157
+
158
+ void
159
+ threads::drain (Napi::Env &env, Napi::Function &callback)
160
+ {
161
+ std::deque<Event> pending;
162
+ {
163
+ std::lock_guard<std::mutex> guard(mutex);
164
+ auto it = all_threads.find(env);
165
+ if (it == all_threads.end()) { return; }
166
+ it->second.wake_pending = false;
167
+ pending.swap(it->second.queue);
168
+ }
169
+
170
+ while (!pending.empty()) {
171
+ Napi::HandleScope scope(env);
172
+ Event event = std::move(pending.front());
173
+ pending.pop_front();
174
+ try {
175
+ callback.Call(env.Global(), { event.value->toNapi(env) });
176
+ }
177
+ catch (...) {
178
+ std::lock_guard<std::mutex> guard(mutex);
179
+ auto it = all_threads.find(env);
180
+ if (it != all_threads.end()) {
181
+ auto &queue = it->second.queue;
182
+ queue.insert(queue.begin(), pending.begin(), pending.end());
183
+ if (!queue.empty()) { _wake(it->second); }
184
+ }
185
+ throw;
186
+ }
187
+ }
188
+ }
189
+
190
+ bool
191
+ threads::isPollingFast ()
192
+ {
193
+ return num_demanding.load(std::memory_order_acquire) > 0
194
+ || num_workers.load(std::memory_order_acquire) > 0;
195
+ }
196
+
197
+ bool
198
+ threads::hasWorkers ()
199
+ {
200
+ return num_workers.load(std::memory_order_acquire) > 0;
201
+ }
202
+
203
+ void
204
+ threads::wakeMain ()
205
+ {
206
+ std::lock_guard<std::mutex> guard(mutex);
207
+
208
+ for (auto &entry : all_threads) {
209
+ if (entry.second.is_main) {
210
+ _wake(entry.second);
211
+ return;
212
+ }
213
+ }
214
+ }
215
+
216
+ void
217
+ threads::shutdown ()
218
+ {
219
+ std::lock_guard<std::mutex> guard(mutex);
220
+
221
+ for (auto &entry : all_threads) {
222
+ napi_release_threadsafe_function(entry.second.wake, napi_tsfn_abort);
223
+ }
224
+ all_threads.clear();
225
+ num_demanding.store(0, std::memory_order_release);
226
+ num_workers.store(0, std::memory_order_release);
227
+ }
228
+
229
+ Napi::Value
230
+ threads::registerThread (const Napi::CallbackInfo &info)
231
+ {
232
+ Napi::Env env = info.Env();
233
+
234
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
235
+
236
+ Napi::Function poll = info[0].As<Napi::Function>();
237
+ bool is_main = info[1].As<Napi::Boolean>().Value();
238
+
239
+ napi_threadsafe_function wake;
240
+ napi_value resource_name;
241
+ NAPI_THROW_IF_FAILED(env, napi_create_string_utf8(env, "sdlPoll", NAPI_AUTO_LENGTH, &resource_name), Napi::Value());
242
+ NAPI_THROW_IF_FAILED(env, napi_create_threadsafe_function(env, poll, nullptr, resource_name, 0, 1, nullptr, nullptr, nullptr, nullptr, &wake), Napi::Value());
243
+ napi_status status = napi_unref_threadsafe_function(env, wake);
244
+ if (status != napi_ok) {
245
+ napi_release_threadsafe_function(wake, napi_tsfn_abort);
246
+ NAPI_THROW_IF_FAILED(env, status, Napi::Value());
247
+ }
248
+
249
+ {
250
+ std::lock_guard<std::mutex> registry_guard(mutex);
251
+
252
+ auto it = all_threads.find(env);
253
+ if (it != all_threads.end()) {
254
+ napi_release_threadsafe_function(wake, napi_tsfn_abort);
255
+ throw Napi::Error::New(env, "@kmamal/sdl is already loaded in this thread");
256
+ }
257
+
258
+ // Registered right after the wake function so that on worker termination it runs before Node finalizes it (cleanup hooks run last-registered-first).
259
+ status = napi_add_env_cleanup_hook(env, _envCleanup, (void *) (napi_env) env);
260
+ if (status != napi_ok) {
261
+ napi_release_threadsafe_function(wake, napi_tsfn_abort);
262
+ NAPI_THROW_IF_FAILED(env, status, Napi::Value());
263
+ }
264
+
265
+ Thread &thread = all_threads[env];
266
+ thread.wake = wake;
267
+ thread.is_main = is_main;
268
+ if (!is_main) { num_workers.fetch_add(1, std::memory_order_acq_rel); }
269
+ }
270
+
271
+ if (!is_main) { threads::wakeMain(); }
272
+
273
+ return env.Undefined();
274
+ }
275
+
276
+ Napi::Value
277
+ threads::setDemand (const Napi::CallbackInfo &info)
278
+ {
279
+ Napi::Env env = info.Env();
280
+
281
+ bool demand = info[0].As<Napi::Boolean>().Value();
282
+
283
+ {
284
+ std::lock_guard<std::mutex> guard(mutex);
285
+
286
+ auto it = all_threads.find(env);
287
+ if (it == all_threads.end()) { return Napi::Boolean::New(env, false); }
288
+
289
+ Thread &thread = it->second;
290
+ if (thread.demand != demand) {
291
+ napi_status status = demand
292
+ ? napi_ref_threadsafe_function(env, thread.wake)
293
+ : napi_unref_threadsafe_function(env, thread.wake);
294
+ NAPI_THROW_IF_FAILED(env, status, Napi::Value());
295
+ thread.demand = demand;
296
+ num_demanding.fetch_add(demand ? 1 : -1, std::memory_order_acq_rel);
297
+ }
298
+ }
299
+
300
+ return Napi::Boolean::New(env, threads::isPollingFast());
301
+ }
@@ -0,0 +1,36 @@
1
+ #ifndef _THREADS_H_
2
+ #define _THREADS_H_
3
+
4
+ #include <napi.h>
5
+ #include <SDL3/SDL.h>
6
+ #include <memory>
7
+ #include <string>
8
+ #include "packed.h"
9
+
10
+ namespace threads {
11
+
12
+ enum Kind { JOYSTICK, GAMEPAD, SENSOR };
13
+
14
+ struct Event {
15
+ const packed::Str *target;
16
+ Uint32 target_id;
17
+ std::shared_ptr<const packed::Value> value;
18
+ };
19
+
20
+ void trackOpen (Napi::Env &env, Kind kind, Uint32 id);
21
+ void trackClose (Napi::Env &env, Kind kind, Uint32 id);
22
+
23
+ void leave (napi_env env);
24
+ void distribute (const Event &event, napi_env except);
25
+ void drain (Napi::Env &env, Napi::Function &callback);
26
+ bool isPollingFast ();
27
+ bool hasWorkers ();
28
+ void wakeMain ();
29
+ void shutdown ();
30
+
31
+ Napi::Value registerThread (const Napi::CallbackInfo &info);
32
+ Napi::Value setDemand (const Napi::CallbackInfo &info);
33
+
34
+ }; // namespace threads
35
+
36
+ #endif // _THREADS_H_
@@ -2,66 +2,60 @@
2
2
  #include "global.h"
3
3
  #include <SDL3/SDL.h>
4
4
  #include <string>
5
- #include <sstream>
6
5
 
7
- std::map<SDL_TouchDeviceType, std::string> touch::device_types;
6
+ const std::map<SDL_TouchDeviceType, packed::Str> touch::device_types {
7
+ // { SDL_TOUCH_DEVICE_INVALID, nullptr },
8
+ { SDL_TOUCH_DEVICE_DIRECT, "direct" },
9
+ { SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE, "indirectAbsolute" },
10
+ { SDL_TOUCH_DEVICE_INDIRECT_RELATIVE, "indirectRelative" },
11
+ };
8
12
 
9
13
  Napi::Value
10
- touch::getDevices(const Napi::CallbackInfo &info)
14
+ touch::getDevices (const Napi::CallbackInfo &info)
11
15
  {
12
16
  Napi::Env env = info.Env();
13
17
 
14
18
  int num_devices;
15
- SDL_TouchID *touch_ids = SDL_GetTouchDevices(&num_devices);
16
- if (touch_ids == nullptr) {
17
- std::ostringstream message;
18
- message << "SDL_GetTouchDevices() error: " << SDL_GetError();
19
- SDL_ClearError();
20
- throw Napi::Error::New(env, message.str());
19
+ global::SdlArray<SDL_TouchID> touch_ids(SDL_GetTouchDevices(&num_devices));
20
+ if (!touch_ids) {
21
+ throw global::sdlError(env, "SDL_GetTouchDevices");
21
22
  }
22
23
 
23
24
  Napi::Array devices = Napi::Array::New(env);
24
25
 
25
- try {
26
- int num_returned = 0;
27
- for (int i = 0; i < num_devices; i++) {
28
- SDL_TouchID id = touch_ids[i];
29
- if (id == SDL_MOUSE_TOUCHID || id == SDL_PEN_TOUCHID) { continue; }
26
+ int num_returned = 0;
27
+ for (int i = 0; i < num_devices; i++) {
28
+ SDL_TouchID id = touch_ids[i];
29
+ if (id == SDL_MOUSE_TOUCHID || id == SDL_PEN_TOUCHID) { continue; }
30
30
 
31
- const char *_name = SDL_GetTouchDeviceName(id);
32
- Napi::Value name;
33
- if (_name != nullptr) {
34
- name = Napi::String::New(env, _name);
35
- }
36
- else {
37
- SDL_ClearError();
38
- name = env.Null();
39
- }
31
+ const char *_name = SDL_GetTouchDeviceName(id);
32
+ Napi::Value name;
33
+ if (_name != nullptr) {
34
+ name = Napi::String::New(env, _name);
35
+ }
36
+ else {
37
+ SDL_ClearError();
38
+ name = env.Null();
39
+ }
40
40
 
41
- SDL_TouchDeviceType _type = SDL_GetTouchDeviceType(id);
42
- auto type_entry = device_types.find(_type);
43
- Napi::Value type;
44
- if (type_entry != device_types.end()) {
45
- type = Napi::String::New(env, type_entry->second);
46
- }
47
- else {
48
- SDL_ClearError();
49
- type = env.Null();
50
- }
41
+ SDL_TouchDeviceType _type = SDL_GetTouchDeviceType(id);
42
+ auto type_entry = device_types.find(_type);
43
+ Napi::Value type;
44
+ if (type_entry != device_types.end()) {
45
+ type = Napi::String::New(env, type_entry->second);
46
+ }
47
+ else {
48
+ SDL_ClearError();
49
+ type = env.Null();
50
+ }
51
51
 
52
- Napi::Object device = Napi::Object::New(env);
53
- device.Set("id", Napi::BigInt::New(env, id));
54
- device.Set("name", name);
55
- device.Set("type", type);
52
+ Napi::Object device = Napi::Object::New(env);
53
+ device.Set("id", Napi::BigInt::New(env, id));
54
+ device.Set("name", name);
55
+ device.Set("type", type);
56
56
 
57
- devices.Set(num_returned++, device);
58
- }
59
- }
60
- catch (...) {
61
- SDL_free(touch_ids);
62
- throw;
57
+ devices.Set(num_returned++, device);
63
58
  }
64
- SDL_free(touch_ids);
65
59
 
66
60
  return devices;
67
61
  }
@@ -2,14 +2,15 @@
2
2
  #define _TOUCH_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include "packed.h"
5
6
  #include <SDL3/SDL.h>
6
7
  #include <map>
7
8
 
8
9
  namespace touch {
9
10
 
10
- extern std::map<SDL_TouchDeviceType, std::string> device_types;
11
+ extern const std::map<SDL_TouchDeviceType, packed::Str> device_types;
11
12
 
12
- Napi::Value getDevices(const Napi::CallbackInfo &info);
13
+ Napi::Value getDevices (const Napi::CallbackInfo &info);
13
14
 
14
15
  }; // namespace touch
15
16