@kmamal/sdl 1.0.0-4 → 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 +6 -13
- 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 +76 -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/keyboard.cpp
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
1
1
|
#include "keyboard.h"
|
|
2
2
|
#include <SDL3/SDL.h>
|
|
3
3
|
#include <string>
|
|
4
|
-
#include <sstream>
|
|
5
4
|
|
|
6
5
|
int keyboard::num_keys;
|
|
7
6
|
const bool *keyboard::keys;
|
|
8
7
|
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
keyboard::packKey(
|
|
9
|
+
packed::Value
|
|
10
|
+
keyboard::packKey(SDL_Keycode keycode)
|
|
12
11
|
{
|
|
13
|
-
bool
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
bool is_masked = (keycode & (SDLK_SCANCODE_MASK | SDLK_EXTENDED_MASK)) != 0;
|
|
13
|
+
|
|
14
|
+
if (!is_masked) {
|
|
15
|
+
switch (keycode) {
|
|
16
|
+
case SDLK_RETURN:
|
|
17
|
+
case SDLK_ESCAPE:
|
|
18
|
+
case SDLK_BACKSPACE:
|
|
19
|
+
case SDLK_TAB:
|
|
20
|
+
case SDLK_DELETE:
|
|
21
|
+
break;
|
|
22
|
+
default: {
|
|
23
|
+
bool is_control = keycode < 0x20 || (keycode >= 0x80 && keycode <= 0x9F);
|
|
24
|
+
if (is_control) { return packed::Value(); }
|
|
25
|
+
char utf8[5];
|
|
26
|
+
char *end = SDL_UCS4ToUTF8(keycode, utf8);
|
|
27
|
+
*end = '\0';
|
|
28
|
+
return packed::Value(utf8);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
22
31
|
}
|
|
23
32
|
|
|
24
33
|
const char *name = SDL_GetKeyName(keycode);
|
|
25
34
|
if (name[0] == '\0') {
|
|
26
35
|
SDL_ClearError();
|
|
27
|
-
return
|
|
36
|
+
return packed::Value();
|
|
28
37
|
}
|
|
29
|
-
|
|
38
|
+
|
|
39
|
+
return packed::Value(name);
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
Napi::Value
|
|
@@ -37,7 +47,27 @@ keyboard::getKey(const Napi::CallbackInfo &info)
|
|
|
37
47
|
int scancode = info[0].As<Napi::Number>().Int32Value();
|
|
38
48
|
|
|
39
49
|
SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode, SDL_KMOD_NONE, true);
|
|
40
|
-
return packKey(
|
|
50
|
+
return packKey(keycode).toNapi(env);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static bool
|
|
54
|
+
isKeypad(int scancode)
|
|
55
|
+
{
|
|
56
|
+
return (scancode >= SDL_SCANCODE_KP_DIVIDE && scancode <= SDL_SCANCODE_KP_PERIOD)
|
|
57
|
+
|| scancode == SDL_SCANCODE_KP_EQUALS
|
|
58
|
+
|| scancode == SDL_SCANCODE_KP_COMMA
|
|
59
|
+
|| scancode == SDL_SCANCODE_KP_EQUALSAS400
|
|
60
|
+
|| (scancode >= SDL_SCANCODE_KP_00 && scancode <= SDL_SCANCODE_KP_HEXADECIMAL);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static int
|
|
64
|
+
findScancode(SDL_Keycode keycode)
|
|
65
|
+
{
|
|
66
|
+
for (int i = 1; i < SDL_SCANCODE_COUNT; i++) {
|
|
67
|
+
if (isKeypad(i)) { continue; }
|
|
68
|
+
if (SDL_GetKeyFromScancode((SDL_Scancode) i, SDL_KMOD_NONE, true) == keycode) { return i; }
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
41
71
|
}
|
|
42
72
|
|
|
43
73
|
Napi::Value
|
|
@@ -53,10 +83,19 @@ keyboard::getScancode(const Napi::CallbackInfo &info)
|
|
|
53
83
|
return env.Null();
|
|
54
84
|
}
|
|
55
85
|
|
|
56
|
-
|
|
86
|
+
const char *cursor = keyname.c_str();
|
|
87
|
+
size_t remaining = keyname.size();
|
|
88
|
+
Uint32 codepoint = SDL_StepUTF8(&cursor, &remaining);
|
|
89
|
+
SDL_Keycode exact = (codepoint != 0 && remaining == 0) ? (SDL_Keycode) codepoint : keycode;
|
|
90
|
+
|
|
91
|
+
int scancode = findScancode(exact);
|
|
92
|
+
if (scancode == 0 && exact != keycode) { scancode = findScancode(keycode); }
|
|
57
93
|
if (scancode == 0) {
|
|
58
|
-
|
|
59
|
-
|
|
94
|
+
scancode = SDL_GetScancodeFromKey(keycode, nullptr);
|
|
95
|
+
if (scancode == 0) {
|
|
96
|
+
SDL_ClearError();
|
|
97
|
+
return env.Null();
|
|
98
|
+
}
|
|
60
99
|
}
|
|
61
100
|
|
|
62
101
|
return Napi::Number::New(env, scancode);
|
|
@@ -67,11 +106,10 @@ keyboard::getState(const Napi::CallbackInfo &info)
|
|
|
67
106
|
{
|
|
68
107
|
Napi::Env env = info.Env();
|
|
69
108
|
|
|
70
|
-
|
|
109
|
+
static_assert(sizeof(bool) == 1, "bool must be one byte");
|
|
71
110
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
111
|
+
Napi::Uint8Array result = Napi::Uint8Array::New(env, keyboard::num_keys);
|
|
112
|
+
SDL_memcpy(result.Data(), keyboard::keys, keyboard::num_keys);
|
|
75
113
|
|
|
76
114
|
return result;
|
|
77
115
|
}
|
package/src/native/keyboard.h
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#define _KEYBOARD_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include "packed.h"
|
|
5
6
|
#include <SDL3/SDL.h>
|
|
6
7
|
|
|
7
8
|
namespace keyboard {
|
|
@@ -9,7 +10,7 @@ namespace keyboard {
|
|
|
9
10
|
extern int num_keys;
|
|
10
11
|
extern const bool *keys;
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
packed::Value packKey (SDL_Keycode keycode);
|
|
13
14
|
|
|
14
15
|
Napi::Value getKey(const Napi::CallbackInfo &info);
|
|
15
16
|
Napi::Value getScancode(const Napi::CallbackInfo &info);
|
package/src/native/module.cpp
CHANGED
|
@@ -14,22 +14,28 @@
|
|
|
14
14
|
#include "audio.h"
|
|
15
15
|
#include "clipboard.h"
|
|
16
16
|
#include "power.h"
|
|
17
|
+
#include "threads.h"
|
|
18
|
+
#include "testing.h"
|
|
17
19
|
|
|
18
20
|
|
|
19
21
|
Napi::Object
|
|
20
22
|
init (Napi::Env env, Napi::Object exports)
|
|
21
23
|
{
|
|
22
|
-
global::registerEnv(env);
|
|
23
|
-
|
|
24
24
|
exports.Set("enums_get", Napi::Function::New<enums::get>(env));
|
|
25
25
|
|
|
26
26
|
exports.Set("global_initialize", Napi::Function::New<global::initialize>(env));
|
|
27
|
+
exports.Set("global_initSubsystem", Napi::Function::New<global::initSubsystem>(env));
|
|
27
28
|
exports.Set("global_cleanup", Napi::Function::New<global::cleanup>(env));
|
|
28
29
|
|
|
30
|
+
exports.Set("events_register", Napi::Function::New<threads::registerThread>(env));
|
|
31
|
+
exports.Set("events_setDemand", Napi::Function::New<threads::setDemand>(env));
|
|
29
32
|
exports.Set("events_poll", Napi::Function::New<events::poll>(env));
|
|
33
|
+
exports.Set("events_broadcastChange", Napi::Function::New<events::broadcastChange>(env));
|
|
30
34
|
exports.Set("events_isDispatchingFromWatch", Napi::Function::New<events::isDispatchingFromWatch>(env));
|
|
31
35
|
|
|
36
|
+
exports.Set("video_getDrivers", Napi::Function::New<video::getDrivers>(env));
|
|
32
37
|
exports.Set("video_getDisplays", Napi::Function::New<video::getDisplays>(env));
|
|
38
|
+
exports.Set("video_convert", Napi::Function::New<video::convert>(env));
|
|
33
39
|
|
|
34
40
|
exports.Set("window_create", Napi::Function::New<window::create>(env));
|
|
35
41
|
exports.Set("window_setTitle", Napi::Function::New<window::setTitle>(env));
|
|
@@ -86,20 +92,31 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
86
92
|
exports.Set("sensor_getData", Napi::Function::New<sensor::getData>(env));
|
|
87
93
|
exports.Set("sensor_close", Napi::Function::New<sensor::close>(env));
|
|
88
94
|
|
|
95
|
+
exports.Set("audio_getDrivers", Napi::Function::New<audio::getDrivers>(env));
|
|
89
96
|
exports.Set("audio_getDevices", Napi::Function::New<audio::getDevices>(env));
|
|
90
97
|
exports.Set("audio_open", Napi::Function::New<audio::open>(env));
|
|
91
|
-
exports.Set("audio_getDeviceFormat", Napi::Function::New<audio::getDeviceFormat>(env));
|
|
92
98
|
exports.Set("audio_play", Napi::Function::New<audio::play>(env));
|
|
93
99
|
exports.Set("audio_getQueued", Napi::Function::New<audio::getQueued>(env));
|
|
100
|
+
exports.Set("audio_flush", Napi::Function::New<audio::flush>(env));
|
|
94
101
|
exports.Set("audio_getAvailable", Napi::Function::New<audio::getAvailable>(env));
|
|
95
102
|
exports.Set("audio_putData", Napi::Function::New<audio::putData>(env));
|
|
96
103
|
exports.Set("audio_getData", Napi::Function::New<audio::getData>(env));
|
|
97
104
|
exports.Set("audio_clear", Napi::Function::New<audio::clear>(env));
|
|
98
105
|
exports.Set("audio_close", Napi::Function::New<audio::close>(env));
|
|
106
|
+
exports.Set("audio_setNotify", Napi::Function::New<audio::setNotify>(env));
|
|
107
|
+
exports.Set("audio_setGain", Napi::Function::New<audio::setGain>(env));
|
|
108
|
+
exports.Set("audio_setFrequencyRatio", Napi::Function::New<audio::setFrequencyRatio>(env));
|
|
109
|
+
exports.Set("audio_convert", Napi::Function::New<audio::convert>(env));
|
|
99
110
|
|
|
100
111
|
exports.Set("clipboard_getText", Napi::Function::New<clipboard::getText>(env));
|
|
101
112
|
exports.Set("clipboard_setText", Napi::Function::New<clipboard::setText>(env));
|
|
102
113
|
|
|
114
|
+
exports.Set("testing_attachVirtualJoystick", Napi::Function::New<testing::attachVirtualJoystick>(env));
|
|
115
|
+
exports.Set("testing_setVirtualJoystickAxis", Napi::Function::New<testing::setVirtualJoystickAxis>(env));
|
|
116
|
+
exports.Set("testing_setVirtualJoystickButton", Napi::Function::New<testing::setVirtualJoystickButton>(env));
|
|
117
|
+
exports.Set("testing_detachVirtualJoystick", Napi::Function::New<testing::detachVirtualJoystick>(env));
|
|
118
|
+
exports.Set("testing_pushAudioDeviceEvent", Napi::Function::New<testing::pushAudioDeviceEvent>(env));
|
|
119
|
+
|
|
103
120
|
exports.Set("power_getInfo", Napi::Function::New<power::getInfo>(env));
|
|
104
121
|
|
|
105
122
|
return exports;
|
package/src/native/mouse.cpp
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
#include "mouse.h"
|
|
2
|
+
#include "global.h"
|
|
2
3
|
#include <SDL3/SDL.h>
|
|
3
4
|
#include <string>
|
|
4
|
-
#include <sstream>
|
|
5
5
|
|
|
6
6
|
static SDL_Cursor *allocated_cursor = nullptr;
|
|
7
7
|
|
|
8
|
+
template <typename... Args>
|
|
9
|
+
static void
|
|
10
|
+
_adoptCursor (Napi::Env &env, SDL_Cursor *cursor, const Args &...args)
|
|
11
|
+
{
|
|
12
|
+
if (!SDL_SetCursor(cursor)) {
|
|
13
|
+
Napi::Error error = global::sdlError(env, "SDL_SetCursor", args...);
|
|
14
|
+
SDL_DestroyCursor(cursor);
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
|
|
19
|
+
allocated_cursor = cursor;
|
|
20
|
+
}
|
|
21
|
+
|
|
8
22
|
|
|
9
23
|
Napi::Value
|
|
10
24
|
mouse::getButton (const Napi::CallbackInfo &info)
|
|
@@ -43,10 +57,7 @@ mouse::setPosition (const Napi::CallbackInfo &info)
|
|
|
43
57
|
float y = info[1].As<Napi::Number>().FloatValue();
|
|
44
58
|
|
|
45
59
|
if (!SDL_WarpMouseGlobal(x, y)) {
|
|
46
|
-
|
|
47
|
-
message << "SDL_WarpMouseGlobal(" << x << ", " << y << ") error: " << SDL_GetError();
|
|
48
|
-
SDL_ClearError();
|
|
49
|
-
throw Napi::Error::New(env, message.str());
|
|
60
|
+
throw global::sdlError(env, "SDL_WarpMouseGlobal", x, y);
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
return env.Undefined();
|
|
@@ -61,22 +72,10 @@ mouse::setCursor (const Napi::CallbackInfo &info)
|
|
|
61
72
|
|
|
62
73
|
SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor) cursor_id);
|
|
63
74
|
if (cursor == nullptr) {
|
|
64
|
-
|
|
65
|
-
message << "SDL_CreateSystemCursor(" << cursor_id << ") error: " << SDL_GetError();
|
|
66
|
-
SDL_ClearError();
|
|
67
|
-
throw Napi::Error::New(env, message.str());
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (!SDL_SetCursor(cursor)) {
|
|
71
|
-
std::ostringstream message;
|
|
72
|
-
message << "SDL_SetCursor(" << cursor_id << ") error: " << SDL_GetError();
|
|
73
|
-
SDL_ClearError();
|
|
74
|
-
SDL_DestroyCursor(cursor);
|
|
75
|
-
throw Napi::Error::New(env, message.str());
|
|
75
|
+
throw global::sdlError(env, "SDL_CreateSystemCursor", cursor_id);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
allocated_cursor = cursor;
|
|
78
|
+
_adoptCursor(env, cursor, cursor_id);
|
|
80
79
|
|
|
81
80
|
return env.Undefined();
|
|
82
81
|
}
|
|
@@ -88,17 +87,11 @@ mouse::resetCursor(const Napi::CallbackInfo &info)
|
|
|
88
87
|
|
|
89
88
|
SDL_Cursor *cursor = SDL_GetDefaultCursor();
|
|
90
89
|
if (cursor == nullptr) {
|
|
91
|
-
|
|
92
|
-
message << "SDL_GetDefaultCursor() error: " << SDL_GetError();
|
|
93
|
-
SDL_ClearError();
|
|
94
|
-
throw Napi::Error::New(env, message.str());
|
|
90
|
+
throw global::sdlError(env, "SDL_GetDefaultCursor");
|
|
95
91
|
}
|
|
96
92
|
|
|
97
93
|
if (!SDL_SetCursor(cursor)) {
|
|
98
|
-
|
|
99
|
-
message << "SDL_SetCursor() error: " << SDL_GetError();
|
|
100
|
-
SDL_ClearError();
|
|
101
|
-
throw Napi::Error::New(env, message.str());
|
|
94
|
+
throw global::sdlError(env, "SDL_SetCursor");
|
|
102
95
|
}
|
|
103
96
|
|
|
104
97
|
if (allocated_cursor != nullptr) {
|
|
@@ -124,34 +117,19 @@ mouse::setCursorImage (const Napi::CallbackInfo &info)
|
|
|
124
117
|
|
|
125
118
|
SDL_Surface* surface = SDL_CreateSurfaceFrom(w, h, (SDL_PixelFormat) format, pixels, stride);
|
|
126
119
|
if (surface == nullptr) {
|
|
127
|
-
|
|
128
|
-
message << "SDL_CreateSurfaceFrom(" << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
|
|
129
|
-
SDL_ClearError();
|
|
130
|
-
throw Napi::Error::New(env, message.str());
|
|
120
|
+
throw global::sdlError(env, "SDL_CreateSurfaceFrom", w, h, format);
|
|
131
121
|
}
|
|
132
122
|
|
|
133
123
|
SDL_Cursor *cursor = SDL_CreateColorCursor(surface, x, y);
|
|
134
124
|
if (cursor == nullptr) {
|
|
125
|
+
Napi::Error error = global::sdlError(env, "SDL_CreateColorCursor", x, y);
|
|
135
126
|
SDL_DestroySurface(surface);
|
|
136
|
-
|
|
137
|
-
std::ostringstream message;
|
|
138
|
-
message << "SDL_CreateColorCursor(" << x << ", " << y << ") error: " << SDL_GetError();
|
|
139
|
-
SDL_ClearError();
|
|
140
|
-
throw Napi::Error::New(env, message.str());
|
|
127
|
+
throw error;
|
|
141
128
|
}
|
|
142
129
|
|
|
143
130
|
SDL_DestroySurface(surface);
|
|
144
131
|
|
|
145
|
-
|
|
146
|
-
std::ostringstream message;
|
|
147
|
-
message << "SDL_SetCursor() error: " << SDL_GetError();
|
|
148
|
-
SDL_ClearError();
|
|
149
|
-
SDL_DestroyCursor(cursor);
|
|
150
|
-
throw Napi::Error::New(env, message.str());
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
|
|
154
|
-
allocated_cursor = cursor;
|
|
132
|
+
_adoptCursor(env, cursor);
|
|
155
133
|
|
|
156
134
|
return env.Undefined();
|
|
157
135
|
}
|
|
@@ -165,10 +143,7 @@ mouse::showCursor (const Napi::CallbackInfo &info)
|
|
|
165
143
|
|
|
166
144
|
bool success = should_show ? SDL_ShowCursor() : SDL_HideCursor();
|
|
167
145
|
if (!success) {
|
|
168
|
-
|
|
169
|
-
message << (should_show ? "SDL_ShowCursor(" : "SDL_HideCursor(") << ") error: " << SDL_GetError();
|
|
170
|
-
SDL_ClearError();
|
|
171
|
-
throw Napi::Error::New(env, message.str());
|
|
146
|
+
throw global::sdlError(env, should_show ? "SDL_ShowCursor" : "SDL_HideCursor");
|
|
172
147
|
}
|
|
173
148
|
|
|
174
149
|
return env.Undefined();
|
|
@@ -180,10 +155,7 @@ mouse::redrawCursor (const Napi::CallbackInfo &info)
|
|
|
180
155
|
Napi::Env env = info.Env();
|
|
181
156
|
|
|
182
157
|
if (!SDL_SetCursor(nullptr)) {
|
|
183
|
-
|
|
184
|
-
message << "SDL_SetCursor(nullptr) error: " << SDL_GetError();
|
|
185
|
-
SDL_ClearError();
|
|
186
|
-
throw Napi::Error::New(env, message.str());
|
|
158
|
+
throw global::sdlError(env, "SDL_SetCursor", "nullptr");
|
|
187
159
|
}
|
|
188
160
|
|
|
189
161
|
return env.Undefined();
|
|
@@ -197,10 +169,7 @@ mouse::capture (const Napi::CallbackInfo &info)
|
|
|
197
169
|
bool should_capture = info[0].As<Napi::Boolean>().Value();
|
|
198
170
|
|
|
199
171
|
if (!SDL_CaptureMouse(should_capture)) {
|
|
200
|
-
|
|
201
|
-
message << "SDL_CaptureMouse(" << should_capture << ") error: " << SDL_GetError();
|
|
202
|
-
SDL_ClearError();
|
|
203
|
-
throw Napi::Error::New(env, message.str());
|
|
172
|
+
throw global::sdlError(env, "SDL_CaptureMouse", should_capture);
|
|
204
173
|
}
|
|
205
174
|
|
|
206
175
|
return env.Undefined();
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#include "packed.h"
|
|
2
|
+
#include <cstring>
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
template <typename... Fs>
|
|
6
|
+
struct Overload : Fs... { using Fs::operator()...; };
|
|
7
|
+
|
|
8
|
+
template <typename... Fs>
|
|
9
|
+
Overload(Fs...) -> Overload<Fs...>;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
packed::Value
|
|
13
|
+
packed::Value::ref (const Str &value)
|
|
14
|
+
{
|
|
15
|
+
Value result;
|
|
16
|
+
result.data.emplace<const std::string *>(&value.str());
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
packed::Value
|
|
21
|
+
packed::Value::object ()
|
|
22
|
+
{
|
|
23
|
+
Value value;
|
|
24
|
+
value.data.emplace<Object>();
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
packed::Value
|
|
29
|
+
packed::Value::array ()
|
|
30
|
+
{
|
|
31
|
+
Value value;
|
|
32
|
+
value.data.emplace<Array>();
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
bool
|
|
37
|
+
packed::Value::has (const char *key) const
|
|
38
|
+
{
|
|
39
|
+
return get(key) != nullptr;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const packed::Value *
|
|
43
|
+
packed::Value::get (const char *key) const
|
|
44
|
+
{
|
|
45
|
+
for (const Member &member : std::get<Object>(data)) {
|
|
46
|
+
if (std::strcmp(member.key, key) == 0) { return &member.value; }
|
|
47
|
+
}
|
|
48
|
+
return nullptr;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
packed::Value &
|
|
52
|
+
packed::Value::set (const char *key, Value value)
|
|
53
|
+
{
|
|
54
|
+
Object &members = std::get<Object>(data);
|
|
55
|
+
for (Member &member : members) {
|
|
56
|
+
if (std::strcmp(member.key, key) == 0) {
|
|
57
|
+
member.value = std::move(value);
|
|
58
|
+
return *this;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
members.push_back({ key, std::move(value) });
|
|
62
|
+
return *this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
packed::Value &
|
|
66
|
+
packed::Value::set (const Str &key, Value value)
|
|
67
|
+
{
|
|
68
|
+
return set(key.c_str(), std::move(value));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
packed::Value &
|
|
72
|
+
packed::Value::push (Value value)
|
|
73
|
+
{
|
|
74
|
+
std::get<Array>(data).push_back(std::move(value));
|
|
75
|
+
return *this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
Napi::Value
|
|
79
|
+
packed::Value::toNapi (Napi::Env env) const
|
|
80
|
+
{
|
|
81
|
+
return std::visit(Overload {
|
|
82
|
+
[&](std::monostate) -> Napi::Value { return env.Null(); },
|
|
83
|
+
[&](bool value) -> Napi::Value { return Napi::Boolean::New(env, value); },
|
|
84
|
+
[&](int32_t value) -> Napi::Value { return Napi::Number::New(env, value); },
|
|
85
|
+
[&](uint32_t value) -> Napi::Value { return Napi::Number::New(env, value); },
|
|
86
|
+
[&](double value) -> Napi::Value { return Napi::Number::New(env, value); },
|
|
87
|
+
[&](int64_t value) -> Napi::Value { return Napi::BigInt::New(env, value); },
|
|
88
|
+
[&](uint64_t value) -> Napi::Value { return Napi::BigInt::New(env, value); },
|
|
89
|
+
[&](const std::string *value) -> Napi::Value { return Napi::String::New(env, *value); },
|
|
90
|
+
[&](const std::string &value) -> Napi::Value { return Napi::String::New(env, value); },
|
|
91
|
+
[&](const Object &members) -> Napi::Value {
|
|
92
|
+
Napi::Object object = Napi::Object::New(env);
|
|
93
|
+
for (const Member &member : members) {
|
|
94
|
+
object.Set(member.key, member.value.toNapi(env));
|
|
95
|
+
}
|
|
96
|
+
return object;
|
|
97
|
+
},
|
|
98
|
+
[&](const Array &items) -> Napi::Value {
|
|
99
|
+
Napi::Array array = Napi::Array::New(env, items.size());
|
|
100
|
+
for (size_t i = 0; i < items.size(); i++) {
|
|
101
|
+
array.Set((uint32_t) i, items[i].toNapi(env));
|
|
102
|
+
}
|
|
103
|
+
return array;
|
|
104
|
+
},
|
|
105
|
+
}, data);
|
|
106
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
#ifndef _PACKED_H_
|
|
2
|
+
#define _PACKED_H_
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <vector>
|
|
7
|
+
#include <variant>
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
|
|
10
|
+
namespace packed {
|
|
11
|
+
|
|
12
|
+
struct Member;
|
|
13
|
+
struct Value;
|
|
14
|
+
|
|
15
|
+
using Object = std::vector<Member>;
|
|
16
|
+
using Array = std::vector<Value>;
|
|
17
|
+
|
|
18
|
+
struct Str {
|
|
19
|
+
Str () = default;
|
|
20
|
+
Str (const Str &) = delete;
|
|
21
|
+
Str &operator= (const Str &) = delete;
|
|
22
|
+
Str &operator= (const char *value) { string = value; return *this; }
|
|
23
|
+
|
|
24
|
+
const std::string &str () const { return string; }
|
|
25
|
+
const char *c_str () const { return string.c_str(); }
|
|
26
|
+
operator const std::string & () const { return string; }
|
|
27
|
+
|
|
28
|
+
private:
|
|
29
|
+
std::string string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
struct Value {
|
|
33
|
+
using Data = std::variant<
|
|
34
|
+
std::monostate,
|
|
35
|
+
bool,
|
|
36
|
+
int32_t,
|
|
37
|
+
uint32_t,
|
|
38
|
+
double,
|
|
39
|
+
int64_t,
|
|
40
|
+
uint64_t,
|
|
41
|
+
const std::string *,
|
|
42
|
+
std::string,
|
|
43
|
+
Object,
|
|
44
|
+
Array
|
|
45
|
+
>;
|
|
46
|
+
|
|
47
|
+
Data data;
|
|
48
|
+
|
|
49
|
+
Value () = default;
|
|
50
|
+
Value (bool value) : data(std::in_place_type<bool>, value) {}
|
|
51
|
+
Value (int32_t value) : data(std::in_place_type<int32_t>, value) {}
|
|
52
|
+
Value (uint32_t value) : data(std::in_place_type<uint32_t>, value) {}
|
|
53
|
+
Value (double value) : data(std::in_place_type<double>, value) {}
|
|
54
|
+
Value (float value) : data(std::in_place_type<double>, value) {}
|
|
55
|
+
Value (int64_t value) : data(std::in_place_type<int64_t>, value) {}
|
|
56
|
+
Value (uint64_t value) : data(std::in_place_type<uint64_t>, value) {}
|
|
57
|
+
Value (const char *value) : data(std::in_place_type<std::string>, value) {}
|
|
58
|
+
Value (const std::string &value) : data(std::in_place_type<std::string>, value) {}
|
|
59
|
+
Value (std::string &&value) : data(std::in_place_type<std::string>, std::move(value)) {}
|
|
60
|
+
|
|
61
|
+
static Value ref (const Str &value);
|
|
62
|
+
static Value ref (Str &&value) = delete;
|
|
63
|
+
static Value ref (const std::string &value) = delete;
|
|
64
|
+
static Value ref (std::string &&value) = delete;
|
|
65
|
+
static Value object ();
|
|
66
|
+
static Value array ();
|
|
67
|
+
|
|
68
|
+
bool isNull () const { return std::holds_alternative<std::monostate>(data); }
|
|
69
|
+
bool has (const char *key) const;
|
|
70
|
+
const Value *get (const char *key) const;
|
|
71
|
+
const Object &members () const { return std::get<Object>(data); }
|
|
72
|
+
Value &set (const char *key, Value value);
|
|
73
|
+
Value &set (const Str &key, Value value);
|
|
74
|
+
Value &set (Str &&key, Value value) = delete;
|
|
75
|
+
Value &set (const std::string &key, Value value) = delete;
|
|
76
|
+
Value &set (std::string &&key, Value value) = delete;
|
|
77
|
+
Value &push (Value value);
|
|
78
|
+
|
|
79
|
+
Napi::Value toNapi (Napi::Env env) const;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
struct Member {
|
|
83
|
+
// Non-owning on purpose: keys are always string literals or static strings, so
|
|
84
|
+
// skipping the copy keeps event packing cheap. Never pass a temporary here.
|
|
85
|
+
const char *key;
|
|
86
|
+
Value value;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
}; // namespace packed
|
|
90
|
+
|
|
91
|
+
#endif // _PACKED_H_
|
package/src/native/power.cpp
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
#include "power.h"
|
|
2
|
+
#include "global.h"
|
|
2
3
|
#include <SDL3/SDL.h>
|
|
3
4
|
#include <map>
|
|
4
5
|
|
|
5
6
|
|
|
6
|
-
std::map<SDL_PowerState,
|
|
7
|
+
std::map<SDL_PowerState, packed::Str> power::states;
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
Napi::Value
|
|
@@ -11,6 +12,11 @@ power::getInfo (const Napi::CallbackInfo &info)
|
|
|
11
12
|
{
|
|
12
13
|
Napi::Env env = info.Env();
|
|
13
14
|
|
|
15
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
16
|
+
|
|
17
|
+
// SDL_GetPowerInfo() reads the battery state straight from the OS and needs
|
|
18
|
+
// no SDL_Init(), so unlike the other worker-reachable bindings this one
|
|
19
|
+
// deliberately skips global::assertReady().
|
|
14
20
|
int _seconds, _percent;
|
|
15
21
|
SDL_PowerState _state = SDL_GetPowerInfo(&_seconds, &_percent);
|
|
16
22
|
|
package/src/native/power.h
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
#define _POWER_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include "packed.h"
|
|
5
6
|
#include <SDL3/SDL.h>
|
|
6
7
|
#include <map>
|
|
7
|
-
#include <string>
|
|
8
8
|
|
|
9
9
|
namespace power {
|
|
10
10
|
|
|
11
|
-
extern std::map<SDL_PowerState,
|
|
11
|
+
extern std::map<SDL_PowerState, packed::Str> states;
|
|
12
12
|
|
|
13
13
|
Napi::Value getInfo(const Napi::CallbackInfo &info);
|
|
14
14
|
|