@kmamal/sdl 1.0.0-5 → 1.0.0-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -6
- package/binding.gyp +3 -0
- package/package.json +5 -12
- package/scripts/util/common.js +2 -2
- package/src/javascript/audio/audio-playback-stream.js +9 -10
- package/src/javascript/audio/audio-recording-stream.js +9 -8
- package/src/javascript/audio/audio-stream.js +100 -44
- package/src/javascript/audio/conversion.js +41 -0
- package/src/javascript/audio/devices.js +11 -0
- package/src/javascript/audio/events.js +65 -0
- package/src/javascript/audio/format-helpers.js +8 -4
- package/src/javascript/audio/index.js +25 -20
- package/src/javascript/audio/prevent-exit.js +12 -9
- package/src/javascript/audio/validate.js +9 -0
- package/src/javascript/cleanup.js +9 -6
- package/src/javascript/clipboard/index.js +8 -3
- package/src/javascript/device-instance.js +61 -0
- package/src/javascript/events/events-via-poll.js +33 -27
- package/src/javascript/events/index.js +75 -482
- package/src/javascript/events/try.js +22 -0
- package/src/javascript/gamepad/{device.js → devices.js} +4 -5
- package/src/javascript/gamepad/events.js +66 -0
- package/src/javascript/gamepad/gamepad-instance.js +12 -160
- package/src/javascript/gamepad/index.js +15 -16
- package/src/javascript/globals.js +10 -1
- package/src/javascript/index.js +2 -5
- package/src/javascript/joystick/devices.js +43 -0
- package/src/javascript/joystick/events.js +129 -0
- package/src/javascript/joystick/index.js +9 -18
- package/src/javascript/joystick/joystick-device-instance.js +175 -0
- package/src/javascript/joystick/joystick-instance.js +12 -158
- package/src/javascript/keyboard/index.js +20 -10
- package/src/javascript/keyboard/key-mapping.js +46 -27
- package/src/javascript/mouse/index.js +36 -29
- package/src/javascript/sensor/events.js +21 -0
- package/src/javascript/sensor/index.js +13 -2
- package/src/javascript/sensor/sensor-instance.js +7 -47
- package/src/javascript/subsystems.js +56 -0
- package/src/javascript/touch/index.js +9 -4
- package/src/javascript/validate.js +114 -0
- package/src/javascript/video/conversion.js +63 -0
- package/src/javascript/video/events.js +179 -0
- package/src/javascript/video/format-helpers.js +10 -8
- package/src/javascript/video/index.js +28 -14
- package/src/javascript/video/validate.js +45 -0
- package/src/javascript/video/window.js +144 -161
- package/src/native/audio.cpp +457 -151
- package/src/native/audio.h +12 -6
- package/src/native/clipboard.cpp +8 -37
- package/src/native/enums.cpp +2 -0
- package/src/native/events.cpp +416 -354
- package/src/native/events.h +67 -65
- package/src/native/gamepad.cpp +86 -103
- package/src/native/gamepad.h +7 -8
- package/src/native/global.cpp +190 -85
- package/src/native/global.h +48 -2
- package/src/native/joystick.cpp +180 -274
- package/src/native/joystick.h +12 -6
- package/src/native/keyboard.cpp +60 -22
- package/src/native/keyboard.h +2 -1
- package/src/native/module.cpp +20 -3
- package/src/native/mouse.cpp +27 -58
- package/src/native/packed.cpp +106 -0
- package/src/native/packed.h +91 -0
- package/src/native/power.cpp +7 -1
- package/src/native/power.h +2 -2
- package/src/native/sensor.cpp +61 -69
- package/src/native/sensor.h +3 -3
- package/src/native/testing.cpp +131 -0
- package/src/native/testing.h +16 -0
- package/src/native/threads.cpp +300 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +32 -43
- package/src/native/touch.h +2 -1
- package/src/native/video.cpp +145 -95
- package/src/native/video.h +6 -3
- package/src/native/window.cpp +155 -218
- package/src/native/window.h +21 -20
- package/src/types/index.d.ts +141 -69
- package/src/javascript/audio/device.js +0 -37
- package/src/javascript/joystick/device.js +0 -13
- package/src/javascript/joystick/shared.js +0 -90
- package/src/javascript/worker.js +0 -88
- package/src/types/worker.d.ts +0 -71
package/src/native/events.h
CHANGED
|
@@ -2,86 +2,88 @@
|
|
|
2
2
|
#define _EVENTS_H_
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
+
#include "packed.h"
|
|
5
6
|
#include <SDL3/SDL.h>
|
|
6
7
|
#include <string>
|
|
7
8
|
|
|
8
9
|
namespace events {
|
|
9
10
|
|
|
10
11
|
namespace targets {
|
|
11
|
-
extern
|
|
12
|
-
extern
|
|
13
|
-
extern
|
|
14
|
-
extern
|
|
15
|
-
extern
|
|
16
|
-
extern
|
|
17
|
-
extern
|
|
18
|
-
extern
|
|
19
|
-
extern
|
|
20
|
-
extern
|
|
21
|
-
extern
|
|
12
|
+
extern packed::Str APP;
|
|
13
|
+
extern packed::Str VIDEO;
|
|
14
|
+
extern packed::Str WINDOW;
|
|
15
|
+
extern packed::Str KEYBOARD;
|
|
16
|
+
extern packed::Str JOYSTICK;
|
|
17
|
+
extern packed::Str GAMEPAD;
|
|
18
|
+
extern packed::Str SENSOR;
|
|
19
|
+
extern packed::Str CLIPBOARD;
|
|
20
|
+
extern packed::Str JOYSTICK_DEVICE;
|
|
21
|
+
extern packed::Str GAMEPAD_DEVICE;
|
|
22
|
+
extern packed::Str AUDIO_DEVICE;
|
|
23
|
+
extern packed::Str CHANGE;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
namespace types {
|
|
25
|
-
extern
|
|
26
|
-
extern
|
|
27
|
-
extern
|
|
28
|
-
extern
|
|
29
|
-
extern
|
|
30
|
-
extern
|
|
31
|
-
extern
|
|
32
|
-
extern
|
|
33
|
-
extern
|
|
34
|
-
extern
|
|
35
|
-
extern
|
|
36
|
-
extern
|
|
37
|
-
extern
|
|
38
|
-
extern
|
|
39
|
-
extern
|
|
40
|
-
extern
|
|
41
|
-
extern
|
|
42
|
-
extern
|
|
43
|
-
extern
|
|
44
|
-
extern
|
|
45
|
-
extern
|
|
46
|
-
extern
|
|
47
|
-
extern
|
|
48
|
-
extern
|
|
49
|
-
extern
|
|
50
|
-
extern
|
|
51
|
-
extern
|
|
52
|
-
extern
|
|
53
|
-
extern
|
|
54
|
-
extern
|
|
55
|
-
extern
|
|
56
|
-
extern
|
|
57
|
-
extern
|
|
58
|
-
extern
|
|
59
|
-
extern
|
|
60
|
-
extern
|
|
61
|
-
extern
|
|
62
|
-
extern
|
|
63
|
-
extern
|
|
64
|
-
extern
|
|
65
|
-
extern
|
|
66
|
-
extern
|
|
67
|
-
extern
|
|
68
|
-
extern
|
|
69
|
-
extern
|
|
70
|
-
extern
|
|
71
|
-
extern
|
|
72
|
-
extern
|
|
73
|
-
extern
|
|
74
|
-
extern
|
|
75
|
-
extern
|
|
76
|
-
extern
|
|
77
|
-
extern
|
|
27
|
+
extern packed::Str QUIT;
|
|
28
|
+
extern packed::Str DISPLAY_ADD;
|
|
29
|
+
extern packed::Str DISPLAY_REMOVE;
|
|
30
|
+
extern packed::Str DISPLAY_ORIENT;
|
|
31
|
+
extern packed::Str DISPLAY_MOVE;
|
|
32
|
+
extern packed::Str DISPLAY_SCALE;
|
|
33
|
+
extern packed::Str DISPLAY_MODE;
|
|
34
|
+
extern packed::Str DISPLAY_USABLE;
|
|
35
|
+
extern packed::Str DISPLAY_CHANGE;
|
|
36
|
+
extern packed::Str SHOW;
|
|
37
|
+
extern packed::Str HIDE;
|
|
38
|
+
extern packed::Str EXPOSE;
|
|
39
|
+
extern packed::Str MOVE;
|
|
40
|
+
extern packed::Str RESIZE;
|
|
41
|
+
extern packed::Str MINIMIZE;
|
|
42
|
+
extern packed::Str MAXIMIZE;
|
|
43
|
+
extern packed::Str RESTORE;
|
|
44
|
+
extern packed::Str ENTER_FULLSCREEN;
|
|
45
|
+
extern packed::Str LEAVE_FULLSCREEN;
|
|
46
|
+
extern packed::Str FOCUS;
|
|
47
|
+
extern packed::Str BLUR;
|
|
48
|
+
extern packed::Str HOVER;
|
|
49
|
+
extern packed::Str LEAVE;
|
|
50
|
+
extern packed::Str KEY_DOWN;
|
|
51
|
+
extern packed::Str KEY_UP;
|
|
52
|
+
extern packed::Str TEXT_INPUT;
|
|
53
|
+
extern packed::Str MOUSE_MOVE;
|
|
54
|
+
extern packed::Str MOUSE_BUTTON_DOWN;
|
|
55
|
+
extern packed::Str MOUSE_BUTTON_UP;
|
|
56
|
+
extern packed::Str MOUSE_WHEEL;
|
|
57
|
+
extern packed::Str DROP_BEGIN;
|
|
58
|
+
extern packed::Str DROP_COMPLETE;
|
|
59
|
+
extern packed::Str DROP_FILE;
|
|
60
|
+
extern packed::Str DROP_TEXT;
|
|
61
|
+
extern packed::Str CLOSE;
|
|
62
|
+
extern packed::Str RENDER_DEVICE_LOST;
|
|
63
|
+
extern packed::Str KEYMAP_CHANGE;
|
|
64
|
+
extern packed::Str FINGER_DOWN;
|
|
65
|
+
extern packed::Str FINGER_UP;
|
|
66
|
+
extern packed::Str FINGER_MOVE;
|
|
67
|
+
extern packed::Str FINGER_CANCEL;
|
|
68
|
+
extern packed::Str DEVICE_ADD;
|
|
69
|
+
extern packed::Str DEVICE_REMOVE;
|
|
70
|
+
extern packed::Str DEVICE_FORMAT_CHANGE;
|
|
71
|
+
extern packed::Str AXIS_MOTION;
|
|
72
|
+
extern packed::Str BUTTON_DOWN;
|
|
73
|
+
extern packed::Str BUTTON_UP;
|
|
74
|
+
extern packed::Str BALL_MOTION;
|
|
75
|
+
extern packed::Str HAT_MOTION;
|
|
76
|
+
extern packed::Str POWER_UPDATE;
|
|
77
|
+
extern packed::Str STEAM_HANDLE_UPDATE;
|
|
78
|
+
extern packed::Str REMAP;
|
|
79
|
+
extern packed::Str UPDATE;
|
|
78
80
|
};
|
|
79
81
|
|
|
80
|
-
bool dispatchEvent(const SDL_Event &event);
|
|
81
82
|
bool dispatchEventFromWatch(const SDL_Event &event);
|
|
82
83
|
|
|
83
84
|
Napi::Value isDispatchingFromWatch(const Napi::CallbackInfo &info);
|
|
84
85
|
Napi::Value poll(const Napi::CallbackInfo &info);
|
|
86
|
+
Napi::Value broadcastChange(const Napi::CallbackInfo &info);
|
|
85
87
|
|
|
86
88
|
}; // namespace events
|
|
87
89
|
|
package/src/native/gamepad.cpp
CHANGED
|
@@ -1,111 +1,101 @@
|
|
|
1
1
|
#include "gamepad.h"
|
|
2
|
+
#include "global.h"
|
|
3
|
+
#include "threads.h"
|
|
2
4
|
#include "joystick.h"
|
|
3
5
|
#include <SDL3/SDL.h>
|
|
4
6
|
#include <map>
|
|
5
7
|
#include <string>
|
|
6
|
-
#include <sstream>
|
|
7
8
|
|
|
8
9
|
|
|
9
|
-
std::map<SDL_GamepadType,
|
|
10
|
-
std::map<SDL_GamepadAxis,
|
|
11
|
-
std::map<SDL_GamepadButton,
|
|
12
|
-
std::map<SDL_GamepadButtonLabel,
|
|
10
|
+
std::map<SDL_GamepadType, packed::Str> gamepad::types;
|
|
11
|
+
std::map<SDL_GamepadAxis, packed::Str> gamepad::axes;
|
|
12
|
+
std::map<SDL_GamepadButton, packed::Str> gamepad::buttons;
|
|
13
|
+
std::map<SDL_GamepadButtonLabel, packed::Str> gamepad::button_labels;
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
double
|
|
16
17
|
gamepad::mapAxis (SDL_Gamepad *gamepad, SDL_GamepadAxis axis) {
|
|
17
|
-
return mapAxisValue(SDL_GetGamepadAxis(gamepad, axis));
|
|
18
|
+
return joystick::mapAxisValue(SDL_GetGamepadAxis(gamepad, axis));
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
gamepad::
|
|
22
|
-
double range = value < 0 ? -SDL_JOYSTICK_AXIS_MIN : SDL_JOYSTICK_AXIS_MAX;
|
|
23
|
-
return value / range;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
Napi::Value
|
|
27
|
-
gamepad::getSteamHandle (Napi::Env &env, SDL_Gamepad *gamepad)
|
|
21
|
+
packed::Value
|
|
22
|
+
gamepad::getSteamHandle (SDL_Gamepad *gamepad)
|
|
28
23
|
{
|
|
29
24
|
Uint64 _steam_handle = SDL_GetGamepadSteamHandle(gamepad);
|
|
30
25
|
return _steam_handle != 0
|
|
31
|
-
?
|
|
32
|
-
:
|
|
26
|
+
? packed::Value((uint64_t) _steam_handle)
|
|
27
|
+
: packed::Value();
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
void
|
|
36
|
-
gamepad::getState (
|
|
31
|
+
gamepad::getState (SDL_Gamepad *gamepad, packed::Value &dst)
|
|
37
32
|
{
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
buttons_obj
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
buttons_obj.Set("back", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_BACK));
|
|
57
|
-
buttons_obj.Set("start", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_START));
|
|
58
|
-
buttons_obj.Set("leftStick", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_STICK));
|
|
59
|
-
buttons_obj.Set("rightStick", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_STICK));
|
|
60
|
-
buttons_obj.Set("leftShoulder", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER));
|
|
61
|
-
buttons_obj.Set("rightShoulder", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER));
|
|
62
|
-
buttons_obj.Set("rightPaddle1", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1));
|
|
63
|
-
buttons_obj.Set("leftPaddle1", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_PADDLE1));
|
|
64
|
-
buttons_obj.Set("rightPaddle2", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2));
|
|
65
|
-
buttons_obj.Set("leftPaddle2", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_LEFT_PADDLE2));
|
|
66
|
-
buttons_obj.Set("misc1", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC1));
|
|
67
|
-
buttons_obj.Set("misc2", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC2));
|
|
68
|
-
buttons_obj.Set("misc3", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC3));
|
|
69
|
-
buttons_obj.Set("misc4", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC4));
|
|
70
|
-
buttons_obj.Set("misc5", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC5));
|
|
71
|
-
buttons_obj.Set("misc6", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_MISC6));
|
|
72
|
-
buttons_obj.Set("touchpad", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_TOUCHPAD));
|
|
73
|
-
|
|
74
|
-
Napi::Object button_labels_obj = Napi::Object::New(env);
|
|
33
|
+
packed::Value axes_obj = packed::Value::object();
|
|
34
|
+
for (auto axis : {
|
|
35
|
+
SDL_GAMEPAD_AXIS_LEFTX,
|
|
36
|
+
SDL_GAMEPAD_AXIS_LEFTY,
|
|
37
|
+
SDL_GAMEPAD_AXIS_RIGHTX,
|
|
38
|
+
SDL_GAMEPAD_AXIS_RIGHTY,
|
|
39
|
+
SDL_GAMEPAD_AXIS_LEFT_TRIGGER,
|
|
40
|
+
SDL_GAMEPAD_AXIS_RIGHT_TRIGGER,
|
|
41
|
+
}) {
|
|
42
|
+
axes_obj.set(gamepad::axes.at(axis), gamepad::mapAxis(gamepad, axis));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
packed::Value buttons_obj = packed::Value::object();
|
|
46
|
+
for (auto &entry : gamepad::buttons) {
|
|
47
|
+
buttons_obj.set(entry.second, SDL_GetGamepadButton(gamepad, entry.first));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
packed::Value button_labels_obj = packed::Value::object();
|
|
75
51
|
for (auto button : { SDL_GAMEPAD_BUTTON_SOUTH, SDL_GAMEPAD_BUTTON_EAST, SDL_GAMEPAD_BUTTON_WEST, SDL_GAMEPAD_BUTTON_NORTH }) {
|
|
76
52
|
auto label_entry = gamepad::button_labels.find(SDL_GetGamepadButtonLabel(gamepad, button));
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
button_labels_obj.Set(gamepad::buttons[button], label);
|
|
53
|
+
packed::Value label;
|
|
54
|
+
if (label_entry != gamepad::button_labels.end()) { label = packed::Value::ref(label_entry->second); }
|
|
55
|
+
button_labels_obj.set(gamepad::buttons.at(button), std::move(label));
|
|
81
56
|
}
|
|
82
57
|
|
|
83
|
-
dst.
|
|
84
|
-
dst.
|
|
85
|
-
dst.
|
|
58
|
+
dst.set("axes", std::move(axes_obj));
|
|
59
|
+
dst.set("buttons", std::move(buttons_obj));
|
|
60
|
+
dst.set("buttonLabels", std::move(button_labels_obj));
|
|
86
61
|
}
|
|
87
62
|
|
|
63
|
+
static bool
|
|
64
|
+
_isPlatformMatch (const std::string &mapping)
|
|
65
|
+
{
|
|
66
|
+
static const char field[] = "platform:";
|
|
67
|
+
size_t start = mapping.find(field);
|
|
68
|
+
if (start == std::string::npos) { return true; }
|
|
69
|
+
start += sizeof(field) - 1;
|
|
70
|
+
size_t end = mapping.find(',', start);
|
|
71
|
+
if (end == std::string::npos) { end = mapping.size(); }
|
|
72
|
+
const char *platform = mapping.c_str() + start;
|
|
73
|
+
size_t length = end - start;
|
|
74
|
+
|
|
75
|
+
#ifdef SDL_PLATFORM_MACOS
|
|
76
|
+
if (length == 8 && SDL_strncasecmp(platform, "Mac OS X", length) == 0) { return true; }
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
const char *current = SDL_GetPlatform();
|
|
80
|
+
return length == SDL_strlen(current) && SDL_strncasecmp(platform, current, length) == 0;
|
|
81
|
+
}
|
|
88
82
|
|
|
89
83
|
Napi::Value
|
|
90
84
|
gamepad::addMappings (const Napi::CallbackInfo &info)
|
|
91
85
|
{
|
|
92
86
|
Napi::Env env = info.Env();
|
|
93
87
|
|
|
88
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
89
|
+
global::assertReady(env);
|
|
90
|
+
|
|
94
91
|
Napi::Array mappings = info[0].As<Napi::Array>();
|
|
95
92
|
|
|
96
93
|
for (int i = 0; i < (int) mappings.Length(); i++) {
|
|
97
94
|
std::string mapping = mappings.Get(i).As<Napi::String>().Utf8Value();
|
|
98
|
-
|
|
95
|
+
if (!_isPlatformMatch(mapping)) { continue; }
|
|
99
96
|
if (SDL_AddGamepadMapping(mapping.c_str()) == -1) {
|
|
100
|
-
|
|
101
|
-
message << "SDL_AddGamepadMapping(" << mapping << ") error: " << SDL_GetError();
|
|
102
|
-
SDL_ClearError();
|
|
103
|
-
throw Napi::Error::New(env, message.str());
|
|
97
|
+
throw global::sdlError(env, "SDL_AddGamepadMapping", mapping);
|
|
104
98
|
}
|
|
105
|
-
|
|
106
|
-
// SDL_AddGamepadMapping produces errors even though it succeeds
|
|
107
|
-
const char *error = SDL_GetError();
|
|
108
|
-
if (error[0] != '\0') { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
109
99
|
SDL_ClearError();
|
|
110
100
|
}
|
|
111
101
|
|
|
@@ -117,49 +107,42 @@ gamepad::open (const Napi::CallbackInfo &info)
|
|
|
117
107
|
{
|
|
118
108
|
Napi::Env env = info.Env();
|
|
119
109
|
|
|
120
|
-
|
|
110
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
111
|
+
global::assertReady(env);
|
|
112
|
+
|
|
113
|
+
SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
|
|
121
114
|
|
|
122
|
-
SDL_ClearError();
|
|
123
115
|
SDL_Gamepad *gamepad = SDL_OpenGamepad(id);
|
|
124
116
|
if (gamepad == nullptr) {
|
|
125
|
-
|
|
126
|
-
message << "SDL_OpenGamepad(" << id << ") error: " << SDL_GetError();
|
|
127
|
-
SDL_ClearError();
|
|
128
|
-
throw Napi::Error::New(env, message.str());
|
|
117
|
+
throw global::sdlError(env, "SDL_OpenGamepad", id);
|
|
129
118
|
}
|
|
130
|
-
|
|
131
|
-
// SDL_OpenGamepad produces errors even though it succeeds
|
|
132
|
-
const char *error = SDL_GetError();
|
|
133
|
-
if (error[0] != '\0') { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
134
119
|
SDL_ClearError();
|
|
135
120
|
|
|
121
|
+
threads::trackOpen(env, threads::GAMEPAD, id);
|
|
122
|
+
|
|
136
123
|
try {
|
|
137
124
|
int _firmware_version = SDL_GetGamepadFirmwareVersion(gamepad);
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
: env.Null();
|
|
125
|
+
packed::Value firmware_version;
|
|
126
|
+
if (_firmware_version != 0) { firmware_version = _firmware_version; }
|
|
141
127
|
|
|
142
128
|
const char *_serial_number = SDL_GetGamepadSerial(gamepad);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
: env.Null();
|
|
146
|
-
|
|
147
|
-
Napi::Value steam_handle = getSteamHandle(env, gamepad);
|
|
129
|
+
packed::Value serial_number;
|
|
130
|
+
if (_serial_number != nullptr) { serial_number = _serial_number; }
|
|
148
131
|
|
|
149
132
|
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
|
|
150
|
-
Napi::Value power = joystick::getPowerInfo(env, joystick);
|
|
151
133
|
|
|
152
|
-
|
|
153
|
-
result.
|
|
154
|
-
result.
|
|
155
|
-
result.
|
|
156
|
-
result.
|
|
134
|
+
packed::Value result = packed::Value::object();
|
|
135
|
+
result.set("firmwareVersion", std::move(firmware_version));
|
|
136
|
+
result.set("serialNumber", std::move(serial_number));
|
|
137
|
+
result.set("steamHandle", getSteamHandle(gamepad));
|
|
138
|
+
result.set("power", joystick::getPowerInfo(joystick));
|
|
157
139
|
|
|
158
|
-
gamepad::getState(
|
|
140
|
+
gamepad::getState(gamepad, result);
|
|
159
141
|
|
|
160
|
-
return result;
|
|
142
|
+
return result.toNapi(env);
|
|
161
143
|
}
|
|
162
144
|
catch (...) {
|
|
145
|
+
threads::trackClose(env, threads::GAMEPAD, id);
|
|
163
146
|
SDL_CloseGamepad(gamepad);
|
|
164
147
|
throw;
|
|
165
148
|
}
|
|
@@ -170,14 +153,14 @@ gamepad::close (const Napi::CallbackInfo &info)
|
|
|
170
153
|
{
|
|
171
154
|
Napi::Env env = info.Env();
|
|
172
155
|
|
|
173
|
-
|
|
156
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
157
|
+
global::assertReady(env);
|
|
174
158
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
159
|
+
SDL_JoystickID gamepad_id = info[0].As<Napi::Number>().Uint32Value();
|
|
160
|
+
|
|
161
|
+
threads::trackClose(env, threads::GAMEPAD, gamepad_id);
|
|
162
|
+
|
|
163
|
+
SDL_Gamepad *gamepad = global::fromIdOrThrow(env, SDL_GetGamepadFromID(gamepad_id), "SDL_GetGamepadFromID", "gamepad", gamepad_id);
|
|
181
164
|
|
|
182
165
|
SDL_CloseGamepad(gamepad);
|
|
183
166
|
|
package/src/native/gamepad.h
CHANGED
|
@@ -2,21 +2,20 @@
|
|
|
2
2
|
#define _GAMEPAD_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 gamepad {
|
|
10
10
|
|
|
11
|
-
extern std::map<SDL_GamepadType,
|
|
12
|
-
extern std::map<SDL_GamepadAxis,
|
|
13
|
-
extern std::map<SDL_GamepadButton,
|
|
14
|
-
extern std::map<SDL_GamepadButtonLabel,
|
|
11
|
+
extern std::map<SDL_GamepadType, packed::Str> types;
|
|
12
|
+
extern std::map<SDL_GamepadAxis, packed::Str> axes;
|
|
13
|
+
extern std::map<SDL_GamepadButton, packed::Str> buttons;
|
|
14
|
+
extern std::map<SDL_GamepadButtonLabel, packed::Str> button_labels;
|
|
15
15
|
|
|
16
16
|
double mapAxis (SDL_Gamepad *gamepad, SDL_GamepadAxis axis);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Napi::Value getSteamHandle(Napi::Env &env, SDL_Gamepad *gamepad);
|
|
17
|
+
void getState (SDL_Gamepad *gamepad, packed::Value &dst);
|
|
18
|
+
packed::Value getSteamHandle(SDL_Gamepad *gamepad);
|
|
20
19
|
|
|
21
20
|
Napi::Value addMappings(const Napi::CallbackInfo &info);
|
|
22
21
|
Napi::Value open(const Napi::CallbackInfo &info);
|