@kmamal/sdl 0.11.13 → 1.0.0-3
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 +113 -1
- package/README.md +26 -2685
- package/binding.gyp +82 -65
- package/package.json +17 -11
- package/scripts/build-sdl.mjs +15 -7
- package/scripts/build.mjs +30 -15
- package/scripts/download-release.mjs +9 -6
- package/scripts/download-sdl.mjs +9 -6
- package/scripts/install.mjs +13 -5
- package/scripts/util/common.js +9 -1
- package/src/javascript/audio/audio-playback-stream.js +50 -0
- package/src/javascript/audio/audio-recording-stream.js +32 -0
- package/src/javascript/audio/{audio-instance.js → audio-stream.js} +40 -34
- package/src/javascript/audio/device.js +34 -6
- package/src/javascript/audio/format-helpers.js +29 -34
- package/src/javascript/audio/index.js +37 -26
- package/src/javascript/audio/prevent-exit.js +19 -9
- package/src/javascript/cleanup.js +19 -10
- package/src/javascript/enums.js +5 -0
- package/src/javascript/events/events-via-poll.js +25 -7
- package/src/javascript/events/index.js +280 -174
- package/src/javascript/gamepad/device.js +23 -0
- package/src/javascript/gamepad/gamepad-instance.js +211 -0
- package/src/javascript/gamepad/index.js +51 -0
- package/src/javascript/globals.js +8 -5
- package/src/javascript/helpers.js +41 -7
- package/src/javascript/index.js +5 -3
- package/src/javascript/joystick/device.js +5 -10
- package/src/javascript/joystick/index.js +7 -7
- package/src/javascript/joystick/joystick-instance.js +81 -42
- package/src/javascript/joystick/shared.js +90 -0
- package/src/javascript/keyboard/index.js +6 -4
- package/src/javascript/keyboard/key-mapping.js +70 -28
- package/src/javascript/mouse/index.js +21 -10
- package/src/javascript/sensor/devices.js +17 -0
- package/src/javascript/sensor/index.js +2 -16
- package/src/javascript/sensor/sensor-instance.js +22 -6
- package/src/javascript/touch/devices.js +17 -0
- package/src/javascript/touch/index.js +3 -7
- package/src/javascript/video/format-helpers.js +117 -0
- package/src/javascript/video/index.js +44 -5
- package/src/javascript/video/window.js +289 -133
- package/src/native/audio.cpp +182 -58
- package/src/native/audio.h +10 -5
- package/src/native/clipboard.cpp +22 -5
- package/src/native/cocoa-window.h +1 -0
- package/src/native/cocoa-window.mm +1 -1
- package/src/native/enums.cpp +91 -58
- package/src/native/events.cpp +437 -284
- package/src/native/events.h +18 -12
- package/src/native/gamepad.cpp +185 -0
- package/src/native/gamepad.h +27 -0
- package/src/native/global.cpp +173 -167
- package/src/native/global.h +0 -2
- package/src/native/joystick.cpp +287 -225
- package/src/native/joystick.h +7 -6
- package/src/native/keyboard.cpp +28 -8
- package/src/native/keyboard.h +4 -2
- package/src/native/module.cpp +14 -8
- package/src/native/mouse.cpp +54 -34
- package/src/native/mouse.h +0 -1
- package/src/native/power.cpp +10 -4
- package/src/native/power.h +1 -1
- package/src/native/sensor.cpp +52 -52
- package/src/native/sensor.h +1 -1
- package/src/native/touch.cpp +45 -24
- package/src/native/touch.h +1 -1
- package/src/native/video.cpp +115 -75
- package/src/native/video.h +5 -2
- package/src/native/window.cpp +409 -321
- package/src/native/window.h +3 -1
- package/src/types/helpers.d.ts +45 -19
- package/src/types/index.d.ts +366 -273
- package/scripts/util/fetch.js +0 -85
- package/src/javascript/audio/audio-playback-instance.js +0 -23
- package/src/javascript/audio/audio-recording-instance.js +0 -17
- package/src/javascript/controller/controller-instance.js +0 -160
- package/src/javascript/controller/device.js +0 -26
- package/src/javascript/controller/index.js +0 -33
- package/src/javascript/events/reconcile-audio-devices.js +0 -16
- package/src/javascript/events/reconcile-displays.js +0 -17
- package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
- package/src/javascript/events/reconcile.js +0 -61
- package/src/javascript/video/display.js +0 -4
- package/src/native/controller.cpp +0 -192
- package/src/native/controller.h +0 -26
package/src/native/joystick.h
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#define _JOYSTICK_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
#include <SDL.h>
|
|
5
|
+
#include <SDL3/SDL.h>
|
|
6
6
|
#include <map>
|
|
7
7
|
#include <string>
|
|
8
8
|
|
|
@@ -10,17 +10,18 @@ namespace joystick {
|
|
|
10
10
|
|
|
11
11
|
extern std::map<SDL_JoystickType, std::string> types;
|
|
12
12
|
extern std::map<Uint8, std::string> hat_positions;
|
|
13
|
-
extern
|
|
14
|
-
extern SDL_JoystickGUID zero_guid;
|
|
13
|
+
extern SDL_GUID zero_guid;
|
|
15
14
|
|
|
16
15
|
double mapAxis (SDL_Joystick *joystick, int axis);
|
|
17
|
-
double mapAxisValue (
|
|
16
|
+
double mapAxisValue (int value);
|
|
17
|
+
Napi::Value _getDevice (Napi::Env &env, SDL_JoystickID id);
|
|
18
18
|
Napi::Array _getDevices (Napi::Env &env);
|
|
19
|
-
Napi::Value
|
|
20
|
-
Napi::Value
|
|
19
|
+
Napi::Value getPowerInfo(Napi::Env &env, SDL_Joystick *joystick);
|
|
20
|
+
Napi::Value mapPowerInfo(Napi::Env &env, SDL_PowerState state, int percent);
|
|
21
21
|
|
|
22
22
|
Napi::Value getDevices(const Napi::CallbackInfo &info);
|
|
23
23
|
Napi::Value open(const Napi::CallbackInfo &info);
|
|
24
|
+
Napi::Value getCapabilities(const Napi::CallbackInfo &info);
|
|
24
25
|
Napi::Value setLed(const Napi::CallbackInfo &info);
|
|
25
26
|
Napi::Value setPlayer(const Napi::CallbackInfo &info);
|
|
26
27
|
Napi::Value rumble(const Napi::CallbackInfo &info);
|
package/src/native/keyboard.cpp
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
#include "keyboard.h"
|
|
2
|
-
#include <SDL.h>
|
|
2
|
+
#include <SDL3/SDL.h>
|
|
3
3
|
#include <string>
|
|
4
4
|
#include <sstream>
|
|
5
5
|
|
|
6
6
|
int keyboard::num_keys;
|
|
7
|
-
const
|
|
7
|
+
const bool *keyboard::keys;
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
Napi::Value
|
|
11
|
+
keyboard::packKey(Napi::Env &env, SDL_Keycode keycode)
|
|
12
|
+
{
|
|
13
|
+
bool is_character = !(keycode & (SDLK_SCANCODE_MASK | SDLK_EXTENDED_MASK))
|
|
14
|
+
&& keycode >= 0x20
|
|
15
|
+
&& keycode != 0x7F;
|
|
16
|
+
|
|
17
|
+
if (is_character) {
|
|
18
|
+
char utf8[5];
|
|
19
|
+
char *end = SDL_UCS4ToUTF8(keycode, utf8);
|
|
20
|
+
*end = '\0';
|
|
21
|
+
return Napi::String::New(env, utf8);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const char *name = SDL_GetKeyName(keycode);
|
|
25
|
+
if (name[0] == '\0') {
|
|
26
|
+
SDL_ClearError();
|
|
27
|
+
return env.Null();
|
|
28
|
+
}
|
|
29
|
+
return Napi::String::New(env, name);
|
|
30
|
+
}
|
|
31
|
+
|
|
10
32
|
Napi::Value
|
|
11
33
|
keyboard::getKey(const Napi::CallbackInfo &info)
|
|
12
34
|
{
|
|
@@ -14,10 +36,8 @@ keyboard::getKey(const Napi::CallbackInfo &info)
|
|
|
14
36
|
|
|
15
37
|
int scancode = info[0].As<Napi::Number>().Int32Value();
|
|
16
38
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return Napi::String::New(env, keyname);
|
|
39
|
+
SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode, SDL_KMOD_NONE, true);
|
|
40
|
+
return packKey(env, keycode);
|
|
21
41
|
}
|
|
22
42
|
|
|
23
43
|
Napi::Value
|
|
@@ -33,7 +53,7 @@ keyboard::getScancode(const Napi::CallbackInfo &info)
|
|
|
33
53
|
return env.Null();
|
|
34
54
|
}
|
|
35
55
|
|
|
36
|
-
int scancode = SDL_GetScancodeFromKey(keycode);
|
|
56
|
+
int scancode = SDL_GetScancodeFromKey(keycode, nullptr);
|
|
37
57
|
if (scancode == 0) {
|
|
38
58
|
SDL_ClearError();
|
|
39
59
|
return env.Null();
|
|
@@ -50,7 +70,7 @@ keyboard::getState(const Napi::CallbackInfo &info)
|
|
|
50
70
|
Napi::Array result = Napi::Array::New(env, keyboard::num_keys);
|
|
51
71
|
|
|
52
72
|
for (int i = 0; i < keyboard::num_keys; i++) {
|
|
53
|
-
result.Set(i,
|
|
73
|
+
result.Set(i, keyboard::keys[i]);
|
|
54
74
|
}
|
|
55
75
|
|
|
56
76
|
return result;
|
package/src/native/keyboard.h
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
#define _KEYBOARD_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
#include <SDL.h>
|
|
5
|
+
#include <SDL3/SDL.h>
|
|
6
6
|
|
|
7
7
|
namespace keyboard {
|
|
8
8
|
|
|
9
9
|
extern int num_keys;
|
|
10
|
-
extern const
|
|
10
|
+
extern const bool *keys;
|
|
11
|
+
|
|
12
|
+
Napi::Value packKey(Napi::Env &env, SDL_Keycode keycode);
|
|
11
13
|
|
|
12
14
|
Napi::Value getKey(const Napi::CallbackInfo &info);
|
|
13
15
|
Napi::Value getScancode(const Napi::CallbackInfo &info);
|
package/src/native/module.cpp
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
#include "mouse.h"
|
|
10
10
|
#include "touch.h"
|
|
11
11
|
#include "joystick.h"
|
|
12
|
-
#include "
|
|
12
|
+
#include "gamepad.h"
|
|
13
13
|
#include "sensor.h"
|
|
14
14
|
#include "audio.h"
|
|
15
15
|
#include "clipboard.h"
|
|
@@ -25,6 +25,7 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
25
25
|
exports.Set("global_cleanup", Napi::Function::New<global::cleanup>(env));
|
|
26
26
|
|
|
27
27
|
exports.Set("events_poll", Napi::Function::New<events::poll>(env));
|
|
28
|
+
exports.Set("events_isDispatchingFromWatch", Napi::Function::New<events::isDispatchingFromWatch>(env));
|
|
28
29
|
|
|
29
30
|
exports.Set("video_getDisplays", Napi::Function::New<video::getDisplays>(env));
|
|
30
31
|
|
|
@@ -35,6 +36,8 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
35
36
|
exports.Set("window_setFullscreen", Napi::Function::New<window::setFullscreen>(env));
|
|
36
37
|
exports.Set("window_setResizable", Napi::Function::New<window::setResizable>(env));
|
|
37
38
|
exports.Set("window_setBorderless", Napi::Function::New<window::setBorderless>(env));
|
|
39
|
+
exports.Set("window_setRelativeMouseMode", Napi::Function::New<window::setRelativeMouseMode>(env));
|
|
40
|
+
exports.Set("window_getMouseCaptured", Napi::Function::New<window::getMouseCaptured>(env));
|
|
38
41
|
exports.Set("window_setAcceleratedAndVsync", Napi::Function::New<window::setAcceleratedAndVsync>(env));
|
|
39
42
|
exports.Set("window_focus", Napi::Function::New<window::focus>(env));
|
|
40
43
|
exports.Set("window_show", Napi::Function::New<window::show>(env));
|
|
@@ -65,15 +68,16 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
65
68
|
|
|
66
69
|
exports.Set("joystick_getDevices", Napi::Function::New<joystick::getDevices>(env));
|
|
67
70
|
exports.Set("joystick_open", Napi::Function::New<joystick::open>(env));
|
|
71
|
+
exports.Set("joystick_getCapabilities", Napi::Function::New<joystick::getCapabilities>(env));
|
|
68
72
|
exports.Set("joystick_setLed", Napi::Function::New<joystick::setLed>(env));
|
|
69
73
|
exports.Set("joystick_setPlayer", Napi::Function::New<joystick::setPlayer>(env));
|
|
70
74
|
exports.Set("joystick_rumble", Napi::Function::New<joystick::rumble>(env));
|
|
71
75
|
exports.Set("joystick_rumbleTriggers", Napi::Function::New<joystick::rumbleTriggers>(env));
|
|
72
76
|
exports.Set("joystick_close", Napi::Function::New<joystick::close>(env));
|
|
73
77
|
|
|
74
|
-
exports.Set("
|
|
75
|
-
exports.Set("
|
|
76
|
-
exports.Set("
|
|
78
|
+
exports.Set("gamepad_addMappings", Napi::Function::New<gamepad::addMappings>(env));
|
|
79
|
+
exports.Set("gamepad_open", Napi::Function::New<gamepad::open>(env));
|
|
80
|
+
exports.Set("gamepad_close", Napi::Function::New<gamepad::close>(env));
|
|
77
81
|
|
|
78
82
|
exports.Set("sensor_getDevices", Napi::Function::New<sensor::getDevices>(env));
|
|
79
83
|
exports.Set("sensor_open", Napi::Function::New<sensor::open>(env));
|
|
@@ -82,11 +86,13 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
82
86
|
|
|
83
87
|
exports.Set("audio_getDevices", Napi::Function::New<audio::getDevices>(env));
|
|
84
88
|
exports.Set("audio_open", Napi::Function::New<audio::open>(env));
|
|
89
|
+
exports.Set("audio_getDeviceFormat", Napi::Function::New<audio::getDeviceFormat>(env));
|
|
85
90
|
exports.Set("audio_play", Napi::Function::New<audio::play>(env));
|
|
86
|
-
exports.Set("
|
|
87
|
-
exports.Set("
|
|
88
|
-
exports.Set("
|
|
89
|
-
exports.Set("
|
|
91
|
+
exports.Set("audio_getQueued", Napi::Function::New<audio::getQueued>(env));
|
|
92
|
+
exports.Set("audio_getAvailable", Napi::Function::New<audio::getAvailable>(env));
|
|
93
|
+
exports.Set("audio_putData", Napi::Function::New<audio::putData>(env));
|
|
94
|
+
exports.Set("audio_getData", Napi::Function::New<audio::getData>(env));
|
|
95
|
+
exports.Set("audio_clear", Napi::Function::New<audio::clear>(env));
|
|
90
96
|
exports.Set("audio_close", Napi::Function::New<audio::close>(env));
|
|
91
97
|
|
|
92
98
|
exports.Set("clipboard_getText", Napi::Function::New<clipboard::getText>(env));
|
package/src/native/mouse.cpp
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#include "mouse.h"
|
|
2
|
-
#include <SDL.h>
|
|
2
|
+
#include <SDL3/SDL.h>
|
|
3
3
|
#include <string>
|
|
4
4
|
#include <sstream>
|
|
5
5
|
|
|
@@ -13,8 +13,8 @@ mouse::getButton (const Napi::CallbackInfo &info)
|
|
|
13
13
|
|
|
14
14
|
int button = info[0].As<Napi::Number>().Int32Value();
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
bool state = buttons &
|
|
16
|
+
Uint32 buttons = SDL_GetMouseState(nullptr, nullptr);
|
|
17
|
+
bool state = buttons & SDL_BUTTON_MASK(button);
|
|
18
18
|
|
|
19
19
|
return Napi::Boolean::New(env, state);
|
|
20
20
|
}
|
|
@@ -24,7 +24,7 @@ mouse::getPosition (const Napi::CallbackInfo &info)
|
|
|
24
24
|
{
|
|
25
25
|
Napi::Env env = info.Env();
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
float x, y;
|
|
28
28
|
SDL_GetGlobalMouseState(&x, &y);
|
|
29
29
|
|
|
30
30
|
Napi::Object result = Napi::Object::New(env);
|
|
@@ -39,10 +39,10 @@ mouse::setPosition (const Napi::CallbackInfo &info)
|
|
|
39
39
|
{
|
|
40
40
|
Napi::Env env = info.Env();
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
float x = info[0].As<Napi::Number>().FloatValue();
|
|
43
|
+
float y = info[1].As<Napi::Number>().FloatValue();
|
|
44
44
|
|
|
45
|
-
if (SDL_WarpMouseGlobal(x, y)
|
|
45
|
+
if (!SDL_WarpMouseGlobal(x, y)) {
|
|
46
46
|
std::ostringstream message;
|
|
47
47
|
message << "SDL_WarpMouseGlobal(" << x << ", " << y << ") error: " << SDL_GetError();
|
|
48
48
|
SDL_ClearError();
|
|
@@ -59,11 +59,6 @@ mouse::setCursor (const Napi::CallbackInfo &info)
|
|
|
59
59
|
|
|
60
60
|
int cursor_id = info[0].As<Napi::Number>().Int32Value();
|
|
61
61
|
|
|
62
|
-
if (allocated_cursor != nullptr) {
|
|
63
|
-
SDL_FreeCursor(allocated_cursor);
|
|
64
|
-
allocated_cursor = nullptr;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
62
|
SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor) cursor_id);
|
|
68
63
|
if (cursor == nullptr) {
|
|
69
64
|
std::ostringstream message;
|
|
@@ -72,7 +67,16 @@ mouse::setCursor (const Napi::CallbackInfo &info)
|
|
|
72
67
|
throw Napi::Error::New(env, message.str());
|
|
73
68
|
}
|
|
74
69
|
|
|
75
|
-
SDL_SetCursor(cursor)
|
|
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());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
|
|
79
|
+
allocated_cursor = cursor;
|
|
76
80
|
|
|
77
81
|
return env.Undefined();
|
|
78
82
|
}
|
|
@@ -82,11 +86,6 @@ mouse::resetCursor(const Napi::CallbackInfo &info)
|
|
|
82
86
|
{
|
|
83
87
|
Napi::Env env = info.Env();
|
|
84
88
|
|
|
85
|
-
if (allocated_cursor != nullptr) {
|
|
86
|
-
SDL_FreeCursor(allocated_cursor);
|
|
87
|
-
allocated_cursor = nullptr;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
89
|
SDL_Cursor *cursor = SDL_GetDefaultCursor();
|
|
91
90
|
if (cursor == nullptr) {
|
|
92
91
|
std::ostringstream message;
|
|
@@ -95,7 +94,17 @@ mouse::resetCursor(const Napi::CallbackInfo &info)
|
|
|
95
94
|
throw Napi::Error::New(env, message.str());
|
|
96
95
|
}
|
|
97
96
|
|
|
98
|
-
SDL_SetCursor(cursor)
|
|
97
|
+
if (!SDL_SetCursor(cursor)) {
|
|
98
|
+
std::ostringstream message;
|
|
99
|
+
message << "SDL_SetCursor() error: " << SDL_GetError();
|
|
100
|
+
SDL_ClearError();
|
|
101
|
+
throw Napi::Error::New(env, message.str());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (allocated_cursor != nullptr) {
|
|
105
|
+
SDL_DestroyCursor(allocated_cursor);
|
|
106
|
+
allocated_cursor = nullptr;
|
|
107
|
+
}
|
|
99
108
|
|
|
100
109
|
return env.Undefined();
|
|
101
110
|
}
|
|
@@ -113,32 +122,37 @@ mouse::setCursorImage (const Napi::CallbackInfo &info)
|
|
|
113
122
|
int x = info[5].As<Napi::Number>().Int32Value();
|
|
114
123
|
int y = info[6].As<Napi::Number>().Int32Value();
|
|
115
124
|
|
|
116
|
-
SDL_Surface* surface =
|
|
125
|
+
SDL_Surface* surface = SDL_CreateSurfaceFrom(w, h, (SDL_PixelFormat) format, pixels, stride);
|
|
117
126
|
if (surface == nullptr) {
|
|
118
127
|
std::ostringstream message;
|
|
119
|
-
message << "
|
|
128
|
+
message << "SDL_CreateSurfaceFrom(" << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
|
|
120
129
|
SDL_ClearError();
|
|
121
130
|
throw Napi::Error::New(env, message.str());
|
|
122
131
|
}
|
|
123
132
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
133
|
+
SDL_Cursor *cursor = SDL_CreateColorCursor(surface, x, y);
|
|
134
|
+
if (cursor == nullptr) {
|
|
135
|
+
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
141
|
}
|
|
128
142
|
|
|
129
|
-
|
|
130
|
-
if (allocated_cursor == nullptr) {
|
|
131
|
-
SDL_FreeSurface(surface);
|
|
143
|
+
SDL_DestroySurface(surface);
|
|
132
144
|
|
|
145
|
+
if (!SDL_SetCursor(cursor)) {
|
|
133
146
|
std::ostringstream message;
|
|
134
|
-
message << "
|
|
147
|
+
message << "SDL_SetCursor() error: " << SDL_GetError();
|
|
135
148
|
SDL_ClearError();
|
|
149
|
+
SDL_DestroyCursor(cursor);
|
|
136
150
|
throw Napi::Error::New(env, message.str());
|
|
137
151
|
}
|
|
138
152
|
|
|
139
|
-
|
|
153
|
+
if (allocated_cursor != nullptr) { SDL_DestroyCursor(allocated_cursor); }
|
|
154
|
+
allocated_cursor = cursor;
|
|
140
155
|
|
|
141
|
-
SDL_FreeSurface(surface);
|
|
142
156
|
return env.Undefined();
|
|
143
157
|
}
|
|
144
158
|
|
|
@@ -149,9 +163,10 @@ mouse::showCursor (const Napi::CallbackInfo &info)
|
|
|
149
163
|
|
|
150
164
|
bool should_show = info[0].As<Napi::Boolean>().Value();
|
|
151
165
|
|
|
152
|
-
|
|
166
|
+
bool success = should_show ? SDL_ShowCursor() : SDL_HideCursor();
|
|
167
|
+
if (!success) {
|
|
153
168
|
std::ostringstream message;
|
|
154
|
-
message << "SDL_ShowCursor("
|
|
169
|
+
message << (should_show ? "SDL_ShowCursor(" : "SDL_HideCursor(") << ") error: " << SDL_GetError();
|
|
155
170
|
SDL_ClearError();
|
|
156
171
|
throw Napi::Error::New(env, message.str());
|
|
157
172
|
}
|
|
@@ -164,7 +179,12 @@ mouse::redrawCursor (const Napi::CallbackInfo &info)
|
|
|
164
179
|
{
|
|
165
180
|
Napi::Env env = info.Env();
|
|
166
181
|
|
|
167
|
-
SDL_SetCursor(nullptr)
|
|
182
|
+
if (!SDL_SetCursor(nullptr)) {
|
|
183
|
+
std::ostringstream message;
|
|
184
|
+
message << "SDL_SetCursor(nullptr) error: " << SDL_GetError();
|
|
185
|
+
SDL_ClearError();
|
|
186
|
+
throw Napi::Error::New(env, message.str());
|
|
187
|
+
}
|
|
168
188
|
|
|
169
189
|
return env.Undefined();
|
|
170
190
|
}
|
|
@@ -176,7 +196,7 @@ mouse::capture (const Napi::CallbackInfo &info)
|
|
|
176
196
|
|
|
177
197
|
bool should_capture = info[0].As<Napi::Boolean>().Value();
|
|
178
198
|
|
|
179
|
-
if (SDL_CaptureMouse(should_capture
|
|
199
|
+
if (!SDL_CaptureMouse(should_capture)) {
|
|
180
200
|
std::ostringstream message;
|
|
181
201
|
message << "SDL_CaptureMouse(" << should_capture << ") error: " << SDL_GetError();
|
|
182
202
|
SDL_ClearError();
|
package/src/native/mouse.h
CHANGED
|
@@ -8,7 +8,6 @@ namespace mouse {
|
|
|
8
8
|
Napi::Value getButton(const Napi::CallbackInfo &info);
|
|
9
9
|
Napi::Value getPosition(const Napi::CallbackInfo &info);
|
|
10
10
|
Napi::Value setPosition(const Napi::CallbackInfo &info);
|
|
11
|
-
Napi::Value getCursor(const Napi::CallbackInfo &info);
|
|
12
11
|
Napi::Value setCursor(const Napi::CallbackInfo &info);
|
|
13
12
|
Napi::Value resetCursor(const Napi::CallbackInfo &info);
|
|
14
13
|
Napi::Value setCursorImage(const Napi::CallbackInfo &info);
|
package/src/native/power.cpp
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#include "power.h"
|
|
2
|
-
#include <SDL.h>
|
|
2
|
+
#include <SDL3/SDL.h>
|
|
3
3
|
#include <map>
|
|
4
4
|
|
|
5
5
|
|
|
@@ -14,9 +14,15 @@ power::getInfo (const Napi::CallbackInfo &info)
|
|
|
14
14
|
int _seconds, _percent;
|
|
15
15
|
SDL_PowerState _state = SDL_GetPowerInfo(&_seconds, &_percent);
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
auto state_entry = power::states.find(_state);
|
|
18
|
+
Napi::Value state;
|
|
19
|
+
if (state_entry != power::states.end()) {
|
|
20
|
+
state = Napi::String::New(env, state_entry->second);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
SDL_ClearError();
|
|
24
|
+
state = env.Null();
|
|
25
|
+
}
|
|
20
26
|
Napi::Value seconds = _seconds != -1
|
|
21
27
|
? Napi::Number::New(env, _seconds)
|
|
22
28
|
: env.Null();
|
package/src/native/power.h
CHANGED
package/src/native/sensor.cpp
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#include "sensor.h"
|
|
2
|
-
#include <SDL.h>
|
|
2
|
+
#include <SDL3/SDL.h>
|
|
3
3
|
#include <map>
|
|
4
4
|
#include <string>
|
|
5
5
|
#include <sstream>
|
|
@@ -14,49 +14,56 @@ sensor::getDevices (const Napi::CallbackInfo &info)
|
|
|
14
14
|
{
|
|
15
15
|
Napi::Env env = info.Env();
|
|
16
16
|
|
|
17
|
-
int num_devices
|
|
18
|
-
|
|
17
|
+
int num_devices;
|
|
18
|
+
SDL_SensorID *sensor_ids = SDL_GetSensors(&num_devices);
|
|
19
|
+
if (sensor_ids == nullptr) {
|
|
19
20
|
std::ostringstream message;
|
|
20
|
-
message << "
|
|
21
|
+
message << "SDL_GetSensors() error: " << SDL_GetError();
|
|
21
22
|
SDL_ClearError();
|
|
22
23
|
throw Napi::Error::New(env, message.str());
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
Napi::Array devices = Napi::Array::New(env, num_devices);
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
int
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
SDL_ClearError();
|
|
33
|
-
|
|
28
|
+
try {
|
|
29
|
+
for (int i = 0; i < num_devices; i++) {
|
|
30
|
+
SDL_SensorID id = sensor_ids[i];
|
|
31
|
+
|
|
32
|
+
SDL_SensorType _type = SDL_GetSensorTypeForID(id);
|
|
33
|
+
if (_type == SDL_SENSOR_INVALID) { SDL_ClearError(); }
|
|
34
|
+
auto type_entry = sensor::types.find(_type);
|
|
35
|
+
Napi::Value type = type_entry != sensor::types.end()
|
|
36
|
+
? Napi::String::New(env, type_entry->second)
|
|
37
|
+
: env.Null();
|
|
38
|
+
auto side_entry = sensor::sides.find(_type);
|
|
39
|
+
Napi::Value side = side_entry != sensor::sides.end()
|
|
40
|
+
? Napi::String::New(env, side_entry->second)
|
|
41
|
+
: env.Null();
|
|
42
|
+
|
|
43
|
+
const char *_name = SDL_GetSensorNameForID(id);
|
|
44
|
+
Napi::Value name;
|
|
45
|
+
if (_name != nullptr) {
|
|
46
|
+
name = Napi::String::New(env, _name);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
SDL_ClearError();
|
|
50
|
+
name = env.Null();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Napi::Object device = Napi::Object::New(env);
|
|
54
|
+
device.Set("id", id);
|
|
55
|
+
device.Set("name", name);
|
|
56
|
+
device.Set("type", type);
|
|
57
|
+
device.Set("side", side);
|
|
58
|
+
|
|
59
|
+
devices.Set(i, device);
|
|
34
60
|
}
|
|
35
|
-
|
|
36
|
-
SDL_SensorType _type = SDL_SensorGetDeviceType(i);
|
|
37
|
-
Napi::Value type = _type != SDL_SENSOR_UNKNOWN
|
|
38
|
-
? Napi::String::New(env, sensor::types[_type])
|
|
39
|
-
: env.Null();
|
|
40
|
-
Napi::Value side = (
|
|
41
|
-
_type != SDL_SENSOR_UNKNOWN &&
|
|
42
|
-
_type != SDL_SENSOR_ACCEL &&
|
|
43
|
-
_type != SDL_SENSOR_GYRO
|
|
44
|
-
)
|
|
45
|
-
? Napi::String::New(env, sensor::sides[_type])
|
|
46
|
-
: env.Null();
|
|
47
|
-
|
|
48
|
-
// This function can only error if the index is invalid.
|
|
49
|
-
const char *name = SDL_SensorGetDeviceName(i);
|
|
50
|
-
|
|
51
|
-
Napi::Object device = Napi::Object::New(env);
|
|
52
|
-
device.Set("_index", i);
|
|
53
|
-
device.Set("id", id);
|
|
54
|
-
device.Set("name", name);
|
|
55
|
-
device.Set("type", type);
|
|
56
|
-
device.Set("side", side);
|
|
57
|
-
|
|
58
|
-
devices.Set(i, device);
|
|
59
61
|
}
|
|
62
|
+
catch (...) {
|
|
63
|
+
SDL_free(sensor_ids);
|
|
64
|
+
throw;
|
|
65
|
+
}
|
|
66
|
+
SDL_free(sensor_ids);
|
|
60
67
|
|
|
61
68
|
return devices;
|
|
62
69
|
}
|
|
@@ -66,12 +73,12 @@ sensor::open (const Napi::CallbackInfo &info)
|
|
|
66
73
|
{
|
|
67
74
|
Napi::Env env = info.Env();
|
|
68
75
|
|
|
69
|
-
int
|
|
76
|
+
int id = info[0].As<Napi::Number>().Int32Value();
|
|
70
77
|
|
|
71
|
-
SDL_Sensor *sensor =
|
|
78
|
+
SDL_Sensor *sensor = SDL_OpenSensor(id);
|
|
72
79
|
if (sensor == nullptr) {
|
|
73
80
|
std::ostringstream message;
|
|
74
|
-
message << "
|
|
81
|
+
message << "SDL_OpenSensor(" << id << ") error: " << SDL_GetError();
|
|
75
82
|
SDL_ClearError();
|
|
76
83
|
throw Napi::Error::New(env, message.str());
|
|
77
84
|
}
|
|
@@ -86,28 +93,22 @@ sensor::getData (const Napi::CallbackInfo &info)
|
|
|
86
93
|
|
|
87
94
|
int sensor_id = info[0].As<Napi::Number>().Int32Value();
|
|
88
95
|
|
|
89
|
-
SDL_Sensor *sensor =
|
|
96
|
+
SDL_Sensor *sensor = SDL_GetSensorFromID(sensor_id);
|
|
90
97
|
if (sensor == nullptr) {
|
|
91
98
|
std::ostringstream message;
|
|
92
|
-
message << "
|
|
93
|
-
SDL_ClearError();
|
|
99
|
+
message << "SDL_GetSensorFromID(" << sensor_id << ") error: invalid sensor id";
|
|
94
100
|
throw Napi::Error::New(env, message.str());
|
|
95
101
|
}
|
|
96
102
|
|
|
97
|
-
Uint64 _timestamp;
|
|
98
103
|
float data[3];
|
|
99
|
-
if (
|
|
104
|
+
if (!SDL_GetSensorData(sensor, data, 3)) {
|
|
100
105
|
std::ostringstream message;
|
|
101
|
-
message << "
|
|
106
|
+
message << "SDL_GetSensorData(" << sensor_id << ") error: " << SDL_GetError();
|
|
102
107
|
SDL_ClearError();
|
|
103
108
|
throw Napi::Error::New(env, message.str());
|
|
104
109
|
}
|
|
105
|
-
Napi::Value timestamp = _timestamp != 0
|
|
106
|
-
? Napi::Number::New(env, _timestamp)
|
|
107
|
-
: env.Null();
|
|
108
110
|
|
|
109
111
|
Napi::Object result = Napi::Object::New(env);
|
|
110
|
-
result.Set("timestamp", timestamp);
|
|
111
112
|
result.Set("x", data[0]);
|
|
112
113
|
result.Set("y", data[1]);
|
|
113
114
|
result.Set("z", data[2]);
|
|
@@ -122,15 +123,14 @@ sensor::close (const Napi::CallbackInfo &info)
|
|
|
122
123
|
|
|
123
124
|
int sensor_id = info[0].As<Napi::Number>().Int32Value();
|
|
124
125
|
|
|
125
|
-
SDL_Sensor *sensor =
|
|
126
|
+
SDL_Sensor *sensor = SDL_GetSensorFromID(sensor_id);
|
|
126
127
|
if (sensor == nullptr) {
|
|
127
128
|
std::ostringstream message;
|
|
128
|
-
message << "
|
|
129
|
-
SDL_ClearError();
|
|
129
|
+
message << "SDL_GetSensorFromID(" << sensor_id << ") error: invalid sensor id";
|
|
130
130
|
throw Napi::Error::New(env, message.str());
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
SDL_CloseSensor(sensor);
|
|
134
134
|
|
|
135
135
|
return env.Undefined();
|
|
136
136
|
}
|
package/src/native/sensor.h
CHANGED
package/src/native/touch.cpp
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#include "touch.h"
|
|
2
2
|
#include "global.h"
|
|
3
|
-
#include <SDL.h>
|
|
3
|
+
#include <SDL3/SDL.h>
|
|
4
4
|
#include <string>
|
|
5
5
|
#include <sstream>
|
|
6
6
|
|
|
@@ -11,36 +11,57 @@ touch::getDevices(const Napi::CallbackInfo &info)
|
|
|
11
11
|
{
|
|
12
12
|
Napi::Env env = info.Env();
|
|
13
13
|
|
|
14
|
-
int num_devices
|
|
14
|
+
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());
|
|
21
|
+
}
|
|
15
22
|
|
|
16
|
-
Napi::Array devices = Napi::Array::New(env
|
|
23
|
+
Napi::Array devices = Napi::Array::New(env);
|
|
17
24
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
SDL_ClearError();
|
|
24
|
-
throw Napi::Error::New(env, message.str());
|
|
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
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
40
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
}
|
|
36
51
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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);
|
|
41
56
|
|
|
42
|
-
|
|
57
|
+
devices.Set(num_returned++, device);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (...) {
|
|
61
|
+
SDL_free(touch_ids);
|
|
62
|
+
throw;
|
|
43
63
|
}
|
|
64
|
+
SDL_free(touch_ids);
|
|
44
65
|
|
|
45
66
|
return devices;
|
|
46
67
|
}
|