@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/global.cpp
CHANGED
|
@@ -8,59 +8,91 @@
|
|
|
8
8
|
#include "sensor.h"
|
|
9
9
|
#include "audio.h"
|
|
10
10
|
#include "power.h"
|
|
11
|
+
#include "threads.h"
|
|
11
12
|
#include <SDL3/SDL.h>
|
|
12
13
|
#include <string>
|
|
13
|
-
#include <sstream>
|
|
14
14
|
#include <vector>
|
|
15
15
|
#include <atomic>
|
|
16
|
+
#include <condition_variable>
|
|
16
17
|
|
|
17
18
|
#if defined(SDL_PLATFORM_MACOS)
|
|
18
19
|
#include "cocoa-global.h"
|
|
19
20
|
#endif
|
|
20
21
|
|
|
21
22
|
|
|
22
|
-
static SDL_ThreadID
|
|
23
|
+
static std::atomic<SDL_ThreadID> main_thread_id { 0 };
|
|
23
24
|
static std::atomic<bool> ready { false };
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
static std::atomic<bool> started { false };
|
|
26
|
+
static bool tables_set = false;
|
|
27
|
+
|
|
28
|
+
struct InitRequest {
|
|
29
|
+
SDL_InitFlags flags;
|
|
30
|
+
bool done = false;
|
|
31
|
+
bool result = false;
|
|
32
|
+
std::string error;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
static std::mutex request_mutex;
|
|
36
|
+
static std::condition_variable request_cv;
|
|
37
|
+
static std::vector<InitRequest *> pending_requests;
|
|
38
|
+
|
|
39
|
+
// Recursive because watchEvents holds this lock while dispatching window events to JS, and the handlers re-enter it through the wrappers.
|
|
40
|
+
std::recursive_mutex &
|
|
26
41
|
global::lock()
|
|
27
42
|
{
|
|
28
|
-
static std::
|
|
43
|
+
static std::recursive_mutex mutex;
|
|
29
44
|
return mutex;
|
|
30
45
|
}
|
|
31
46
|
|
|
32
|
-
|
|
33
|
-
global::
|
|
47
|
+
bool
|
|
48
|
+
global::isReady()
|
|
34
49
|
{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
return ready.load(std::memory_order_acquire);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
bool
|
|
54
|
+
global::isMainThread()
|
|
55
|
+
{
|
|
56
|
+
return ready.load(std::memory_order_acquire) && SDL_GetCurrentThreadID() == main_thread_id.load(std::memory_order_acquire);
|
|
38
57
|
}
|
|
39
58
|
|
|
40
59
|
void
|
|
41
|
-
global::
|
|
60
|
+
global::assertReady(Napi::Env &env)
|
|
42
61
|
{
|
|
43
|
-
|
|
62
|
+
if (ready.load(std::memory_order_acquire)) { return; }
|
|
63
|
+
|
|
64
|
+
throw Napi::Error::New(env, started.load(std::memory_order_acquire)
|
|
65
|
+
? "SDL has been shut down"
|
|
66
|
+
: "@kmamal/sdl must be imported on the main thread before it can be used in a worker");
|
|
44
67
|
}
|
|
45
68
|
|
|
46
|
-
|
|
69
|
+
// Delivers window move/resize/expose events synchronously so handlers run during the OS's modal move/resize loop.
|
|
70
|
+
static bool SDLCALL
|
|
71
|
+
watchEvents (void *, SDL_Event *event)
|
|
72
|
+
{
|
|
47
73
|
if (true
|
|
48
|
-
&& SDL_GetCurrentThreadID() ==
|
|
74
|
+
&& SDL_GetCurrentThreadID() == main_thread_id.load(std::memory_order_acquire)
|
|
49
75
|
&& (false
|
|
50
76
|
|| event->type == SDL_EVENT_WINDOW_MOVED
|
|
51
77
|
|| event->type == SDL_EVENT_WINDOW_RESIZED
|
|
78
|
+
|| (event->type == SDL_EVENT_WINDOW_EXPOSED && event->window.data1 == 1)
|
|
52
79
|
)
|
|
53
80
|
) {
|
|
81
|
+
// SDL runs watchers with its watcher-list mutex held, and a worker holding global::lock() may be inside an SDL call that pushes an event and so waits for that same mutex.
|
|
82
|
+
// Waiting here would deadlock; try-lock instead and leave the event for the normal poll when a worker has the lock.
|
|
83
|
+
std::unique_lock<std::recursive_mutex> guard(global::lock(), std::try_to_lock);
|
|
84
|
+
if (!guard.owns_lock()) { return true; }
|
|
54
85
|
events::dispatchEventFromWatch(*event);
|
|
55
86
|
}
|
|
56
87
|
return true;
|
|
57
88
|
}
|
|
58
89
|
|
|
59
90
|
|
|
60
|
-
|
|
61
|
-
|
|
91
|
+
static void
|
|
92
|
+
_setTables ()
|
|
62
93
|
{
|
|
63
|
-
|
|
94
|
+
if (tables_set) { return; }
|
|
95
|
+
tables_set = true;
|
|
64
96
|
|
|
65
97
|
events::targets::APP = "app";
|
|
66
98
|
events::targets::VIDEO = "video";
|
|
@@ -73,6 +105,7 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
73
105
|
events::targets::JOYSTICK_DEVICE = "joystickDevice";
|
|
74
106
|
events::targets::GAMEPAD_DEVICE = "gamepadDevice";
|
|
75
107
|
events::targets::AUDIO_DEVICE = "audioDevice";
|
|
108
|
+
events::targets::CHANGE = "change";
|
|
76
109
|
|
|
77
110
|
events::types::QUIT = "quit";
|
|
78
111
|
events::types::DISPLAY_ADD = "displayAdd";
|
|
@@ -228,6 +261,7 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
228
261
|
gamepad::types[SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT] = "nintendoSwitchJoyconRight";
|
|
229
262
|
gamepad::types[SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR] = "nintendoSwitchJoyconPair";
|
|
230
263
|
gamepad::types[SDL_GAMEPAD_TYPE_GAMECUBE] = "gamecube";
|
|
264
|
+
gamepad::types[SDL_GAMEPAD_TYPE_STEAM] = "steam";
|
|
231
265
|
|
|
232
266
|
gamepad::axes[SDL_GAMEPAD_AXIS_LEFTX] = "leftStickX";
|
|
233
267
|
gamepad::axes[SDL_GAMEPAD_AXIS_LEFTY] = "leftStickY";
|
|
@@ -306,20 +340,51 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
306
340
|
power::states[SDL_POWERSTATE_ON_BATTERY] = "battery";
|
|
307
341
|
power::states[SDL_POWERSTATE_CHARGING] = "charging";
|
|
308
342
|
power::states[SDL_POWERSTATE_CHARGED] = "charged";
|
|
343
|
+
}
|
|
309
344
|
|
|
310
|
-
|
|
311
|
-
|
|
345
|
+
static void
|
|
346
|
+
_start (Napi::Env &env)
|
|
347
|
+
{
|
|
348
|
+
_setTables();
|
|
312
349
|
|
|
313
350
|
SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
|
|
314
351
|
SDL_SetHint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "0");
|
|
315
352
|
|
|
316
353
|
if (!SDL_InitSubSystem(SDL_INIT_EVENTS)) {
|
|
317
|
-
|
|
318
|
-
message << "SDL_Init() error: " << SDL_GetError();
|
|
319
|
-
SDL_ClearError();
|
|
320
|
-
throw Napi::Error::New(env, message.str());
|
|
354
|
+
throw global::sdlError(env, "SDL_InitSubSystem", "SDL_INIT_EVENTS");
|
|
321
355
|
}
|
|
322
356
|
|
|
357
|
+
main_thread_id.store(SDL_GetCurrentThreadID(), std::memory_order_release);
|
|
358
|
+
|
|
359
|
+
if (!SDL_AddEventWatch(watchEvents, nullptr)) {
|
|
360
|
+
Napi::Error error = global::sdlError(env, "SDL_AddEventWatch");
|
|
361
|
+
SDL_Quit();
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
keyboard::keys = SDL_GetKeyboardState(&keyboard::num_keys);
|
|
366
|
+
|
|
367
|
+
#if defined(SDL_PLATFORM_MACOS)
|
|
368
|
+
reenableInertialScrolling();
|
|
369
|
+
#endif
|
|
370
|
+
|
|
371
|
+
started.store(true, std::memory_order_release);
|
|
372
|
+
ready.store(true, std::memory_order_release);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
Napi::Value
|
|
377
|
+
global::initialize(const Napi::CallbackInfo &info)
|
|
378
|
+
{
|
|
379
|
+
Napi::Env env = info.Env();
|
|
380
|
+
|
|
381
|
+
bool is_main = info[0].As<Napi::Boolean>().Value();
|
|
382
|
+
|
|
383
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
384
|
+
|
|
385
|
+
if (is_main && !ready.load(std::memory_order_acquire)) { _start(env); }
|
|
386
|
+
else { _setTables(); }
|
|
387
|
+
|
|
323
388
|
int sdl_compile_version = SDL_VERSION;
|
|
324
389
|
|
|
325
390
|
Napi::Object compile_version = Napi::Object::New(env);
|
|
@@ -347,99 +412,139 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
347
412
|
platform = Napi::String::New(env, platform_name);
|
|
348
413
|
}
|
|
349
414
|
|
|
350
|
-
Napi::
|
|
415
|
+
Napi::Object result = Napi::Object::New(env);
|
|
416
|
+
result.Set("version", versions);
|
|
417
|
+
result.Set("platform", platform);
|
|
351
418
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
all_video_drivers.Set(i, SDL_GetVideoDriver(i));
|
|
355
|
-
}
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
356
421
|
|
|
357
|
-
Napi::Value current_video_driver;
|
|
358
|
-
if (SDL_InitSubSystem(SDL_INIT_VIDEO)) {
|
|
359
|
-
current_video_driver = Napi::String::New(env, SDL_GetCurrentVideoDriver());
|
|
360
|
-
initialized.Set("video", true);
|
|
361
|
-
}
|
|
362
|
-
else {
|
|
363
|
-
current_video_driver = env.Null();
|
|
364
|
-
initialized.Set("video", false);
|
|
365
|
-
}
|
|
366
422
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
423
|
+
static bool
|
|
424
|
+
_initSubsystem (SDL_InitFlags flags, std::string &error)
|
|
425
|
+
{
|
|
426
|
+
if ((SDL_WasInit(flags) & flags) == flags) { return true; }
|
|
427
|
+
|
|
428
|
+
bool success = SDL_InitSubSystem(flags);
|
|
429
|
+
if (!success) { error = SDL_GetError(); }
|
|
430
|
+
SDL_ClearError();
|
|
431
|
+
return success;
|
|
432
|
+
}
|
|
370
433
|
|
|
371
|
-
|
|
434
|
+
static Napi::Value
|
|
435
|
+
_initResult (Napi::Env env, bool success, const std::string &error)
|
|
436
|
+
{
|
|
437
|
+
if (success) { return Napi::Boolean::New(env, true); }
|
|
438
|
+
return Napi::String::New(env, error);
|
|
439
|
+
}
|
|
372
440
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
441
|
+
static void
|
|
442
|
+
_failInitRequests ()
|
|
443
|
+
{
|
|
444
|
+
std::lock_guard<std::mutex> guard(request_mutex);
|
|
445
|
+
for (InitRequest *request : pending_requests) {
|
|
446
|
+
request->done = true;
|
|
447
|
+
request->result = false;
|
|
448
|
+
request->error = "SDL has been shut down";
|
|
376
449
|
}
|
|
450
|
+
pending_requests.clear();
|
|
451
|
+
request_cv.notify_all();
|
|
452
|
+
}
|
|
377
453
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
454
|
+
void
|
|
455
|
+
global::processInitRequests()
|
|
456
|
+
{
|
|
457
|
+
std::vector<InitRequest *> batch;
|
|
458
|
+
{
|
|
459
|
+
std::lock_guard<std::mutex> guard(request_mutex);
|
|
460
|
+
if (pending_requests.empty()) { return; }
|
|
461
|
+
batch.swap(pending_requests);
|
|
382
462
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
463
|
+
|
|
464
|
+
{
|
|
465
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
466
|
+
for (InitRequest *request : batch) {
|
|
467
|
+
if (!ready.load(std::memory_order_acquire)) {
|
|
468
|
+
request->result = false;
|
|
469
|
+
request->error = "SDL has been shut down";
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
request->result = _initSubsystem(request->flags, request->error);
|
|
473
|
+
}
|
|
386
474
|
}
|
|
387
475
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
476
|
+
{
|
|
477
|
+
std::lock_guard<std::mutex> guard(request_mutex);
|
|
478
|
+
for (InitRequest *request : batch) { request->done = true; }
|
|
479
|
+
}
|
|
480
|
+
request_cv.notify_all();
|
|
481
|
+
}
|
|
391
482
|
|
|
392
|
-
Napi::Object drivers = Napi::Object::New(env);
|
|
393
|
-
drivers.Set("video", video_drivers);
|
|
394
|
-
drivers.Set("audio", audio_drivers);
|
|
395
483
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
484
|
+
Napi::Value
|
|
485
|
+
global::initSubsystem(const Napi::CallbackInfo &info)
|
|
486
|
+
{
|
|
487
|
+
Napi::Env env = info.Env();
|
|
400
488
|
|
|
401
|
-
|
|
402
|
-
SDL_ClearError();
|
|
489
|
+
std::string name = info[0].As<Napi::String>().Utf8Value();
|
|
403
490
|
|
|
404
|
-
|
|
405
|
-
if (
|
|
491
|
+
SDL_InitFlags flags;
|
|
492
|
+
if (name == "video") { flags = SDL_INIT_VIDEO; }
|
|
493
|
+
else if (name == "audio") { flags = SDL_INIT_AUDIO; }
|
|
494
|
+
else if (name == "joystick") { flags = SDL_INIT_JOYSTICK; }
|
|
495
|
+
else if (name == "gamepad") { flags = SDL_INIT_GAMEPAD; }
|
|
496
|
+
else if (name == "sensor") { flags = SDL_INIT_SENSOR; }
|
|
497
|
+
else {
|
|
406
498
|
std::ostringstream message;
|
|
407
|
-
message << "
|
|
408
|
-
SDL_ClearError();
|
|
499
|
+
message << "initSubsystem(" << name << ") error: invalid subsystem";
|
|
409
500
|
throw Napi::Error::New(env, message.str());
|
|
410
501
|
}
|
|
411
502
|
|
|
412
|
-
|
|
503
|
+
if (global::isMainThread()) {
|
|
504
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
505
|
+
global::assertReady(env);
|
|
506
|
+
std::string error;
|
|
507
|
+
bool success = _initSubsystem(flags, error);
|
|
508
|
+
return _initResult(env, success, error);
|
|
509
|
+
}
|
|
413
510
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
511
|
+
InitRequest request = { flags, false, false, std::string() };
|
|
512
|
+
{
|
|
513
|
+
std::lock_guard<std::mutex> guard(request_mutex);
|
|
514
|
+
global::assertReady(env);
|
|
515
|
+
pending_requests.push_back(&request);
|
|
516
|
+
}
|
|
517
|
+
threads::wakeMain();
|
|
417
518
|
|
|
519
|
+
{
|
|
520
|
+
std::unique_lock<std::mutex> guard(request_mutex);
|
|
521
|
+
request_cv.wait(guard, [&] { return request.done; });
|
|
522
|
+
}
|
|
418
523
|
|
|
419
|
-
|
|
524
|
+
return _initResult(env, request.result, request.error);
|
|
525
|
+
}
|
|
420
526
|
|
|
421
|
-
ready.store(true, std::memory_order_release);
|
|
422
527
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
result.Set("initialized", initialized);
|
|
528
|
+
void
|
|
529
|
+
global::teardown()
|
|
530
|
+
{
|
|
531
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
428
532
|
|
|
429
|
-
return
|
|
430
|
-
|
|
533
|
+
if (!ready.load(std::memory_order_acquire)) { return; }
|
|
534
|
+
ready.store(false, std::memory_order_release);
|
|
431
535
|
|
|
536
|
+
audio::closeAllStreams();
|
|
537
|
+
threads::shutdown();
|
|
538
|
+
_failInitRequests();
|
|
539
|
+
SDL_Quit();
|
|
540
|
+
}
|
|
432
541
|
|
|
433
542
|
Napi::Value
|
|
434
543
|
global::cleanup(const Napi::CallbackInfo &info)
|
|
435
544
|
{
|
|
436
545
|
Napi::Env env = info.Env();
|
|
437
546
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
audio::closeAllStreams();
|
|
441
|
-
ready.store(false, std::memory_order_release);
|
|
442
|
-
SDL_Quit();
|
|
547
|
+
threads::leave(env);
|
|
443
548
|
|
|
444
549
|
return env.Undefined();
|
|
445
550
|
}
|
package/src/native/global.h
CHANGED
|
@@ -2,15 +2,61 @@
|
|
|
2
2
|
#define _GLOBAL_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include <SDL3/SDL.h>
|
|
5
6
|
#include <mutex>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <sstream>
|
|
9
|
+
#include <string>
|
|
6
10
|
|
|
7
11
|
namespace global {
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
namespace detail {
|
|
14
|
+
|
|
15
|
+
inline void streamArgs (std::ostream &) {}
|
|
16
|
+
|
|
17
|
+
template <typename T, typename... Rest>
|
|
18
|
+
void streamArgs (std::ostream &out, const T &first, const Rest &...rest)
|
|
19
|
+
{
|
|
20
|
+
out << first;
|
|
21
|
+
((out << ", " << rest), ...);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}; // namespace detail
|
|
25
|
+
|
|
26
|
+
template <typename... Args>
|
|
27
|
+
Napi::Error sdlError (Napi::Env &env, const char *call, const Args &...args)
|
|
28
|
+
{
|
|
29
|
+
std::ostringstream message;
|
|
30
|
+
message << call << '(';
|
|
31
|
+
detail::streamArgs(message, args...);
|
|
32
|
+
message << ") error: " << SDL_GetError();
|
|
33
|
+
SDL_ClearError();
|
|
34
|
+
return Napi::Error::New(env, message.str());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
template <typename T>
|
|
38
|
+
T *fromIdOrThrow (Napi::Env &env, T *ptr, const char *call, const char *what, Uint32 id)
|
|
39
|
+
{
|
|
40
|
+
if (ptr != nullptr) { return ptr; }
|
|
41
|
+
std::ostringstream message;
|
|
42
|
+
message << call << '(' << id << ") error: invalid " << what << " id";
|
|
43
|
+
SDL_ClearError();
|
|
44
|
+
throw Napi::Error::New(env, message.str());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
struct SdlFree { void operator() (void *ptr) const { SDL_free(ptr); } };
|
|
48
|
+
template <typename T> using SdlPtr = std::unique_ptr<T, SdlFree>;
|
|
49
|
+
template <typename T> using SdlArray = std::unique_ptr<T[], SdlFree>;
|
|
50
|
+
|
|
51
|
+
std::recursive_mutex &lock();
|
|
10
52
|
void assertReady(Napi::Env &env);
|
|
11
|
-
|
|
53
|
+
bool isReady();
|
|
54
|
+
bool isMainThread();
|
|
55
|
+
void processInitRequests();
|
|
56
|
+
void teardown();
|
|
12
57
|
|
|
13
58
|
Napi::Value initialize(const Napi::CallbackInfo &info);
|
|
59
|
+
Napi::Value initSubsystem(const Napi::CallbackInfo &info);
|
|
14
60
|
Napi::Value cleanup(const Napi::CallbackInfo &info);
|
|
15
61
|
|
|
16
62
|
}; // namespace global
|