@kmamal/sdl 1.0.0-5 → 1.0.0-9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -6
- package/README.md +2 -2
- package/binding.gyp +21 -4
- package/package.json +5 -12
- package/scripts/build.mjs +3 -1
- package/scripts/install.mjs +2 -0
- package/scripts/util/common.js +3 -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 +101 -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 +13 -10
- 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 +32 -28
- package/src/javascript/events/index.js +85 -479
- package/src/javascript/events/try.js +25 -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 +176 -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 +119 -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 +196 -167
- package/src/native/audio.cpp +457 -153
- package/src/native/audio.h +25 -19
- package/src/native/clipboard.cpp +8 -37
- package/src/native/clipboard.h +2 -2
- package/src/native/enums.cpp +16 -1
- package/src/native/enums.h +1 -1
- package/src/native/events.cpp +351 -357
- package/src/native/events.h +70 -68
- package/src/native/gamepad.cpp +147 -103
- package/src/native/gamepad.h +10 -11
- package/src/native/global.cpp +183 -340
- package/src/native/global.h +51 -5
- package/src/native/joystick.cpp +204 -275
- package/src/native/joystick.h +21 -15
- package/src/native/keyboard.cpp +63 -25
- package/src/native/keyboard.h +5 -4
- package/src/native/module.cpp +23 -3
- package/src/native/mouse.cpp +28 -59
- package/src/native/mouse.h +9 -9
- package/src/native/packed.cpp +99 -0
- package/src/native/packed.h +86 -0
- package/src/native/power.cpp +13 -1
- package/src/native/power.h +3 -3
- package/src/native/sensor.cpp +78 -69
- package/src/native/sensor.h +7 -7
- package/src/native/testing.cpp +148 -0
- package/src/native/testing.h +17 -0
- package/src/native/threads.cpp +301 -0
- package/src/native/threads.h +36 -0
- package/src/native/touch.cpp +38 -44
- package/src/native/touch.h +3 -2
- package/src/native/video.cpp +211 -98
- package/src/native/video.h +7 -4
- package/src/native/window.cpp +201 -219
- package/src/native/window.h +24 -20
- package/src/types/index.d.ts +157 -70
- 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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
inline constexpr packed::Str APP { "app" };
|
|
13
|
+
inline constexpr packed::Str VIDEO { "video" };
|
|
14
|
+
inline constexpr packed::Str WINDOW { "window" };
|
|
15
|
+
inline constexpr packed::Str KEYBOARD { "keyboard" };
|
|
16
|
+
inline constexpr packed::Str JOYSTICK { "joystick" };
|
|
17
|
+
inline constexpr packed::Str GAMEPAD { "gamepad" };
|
|
18
|
+
inline constexpr packed::Str SENSOR { "sensor" };
|
|
19
|
+
inline constexpr packed::Str CLIPBOARD { "clipboard" };
|
|
20
|
+
inline constexpr packed::Str JOYSTICK_DEVICE { "joystickDevice" };
|
|
21
|
+
inline constexpr packed::Str GAMEPAD_DEVICE { "gamepadDevice" };
|
|
22
|
+
inline constexpr packed::Str AUDIO_DEVICE { "audioDevice" };
|
|
23
|
+
inline constexpr packed::Str CHANGE { "change" };
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
namespace types {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
27
|
+
inline constexpr packed::Str QUIT { "quit" };
|
|
28
|
+
inline constexpr packed::Str DISPLAY_ADD { "displayAdd" };
|
|
29
|
+
inline constexpr packed::Str DISPLAY_REMOVE { "displayRemove" };
|
|
30
|
+
inline constexpr packed::Str DISPLAY_ORIENT { "displayOrient" };
|
|
31
|
+
inline constexpr packed::Str DISPLAY_MOVE { "displayMove" };
|
|
32
|
+
inline constexpr packed::Str DISPLAY_SCALE { "displayScaleChange" };
|
|
33
|
+
inline constexpr packed::Str DISPLAY_MODE { "displayModeChange" };
|
|
34
|
+
inline constexpr packed::Str DISPLAY_USABLE { "displayUsableChange" };
|
|
35
|
+
inline constexpr packed::Str DISPLAY_CHANGE { "displayChange" };
|
|
36
|
+
inline constexpr packed::Str SHOW { "show" };
|
|
37
|
+
inline constexpr packed::Str HIDE { "hide" };
|
|
38
|
+
inline constexpr packed::Str EXPOSE { "expose" };
|
|
39
|
+
inline constexpr packed::Str MOVE { "move" };
|
|
40
|
+
inline constexpr packed::Str RESIZE { "resize" };
|
|
41
|
+
inline constexpr packed::Str MINIMIZE { "minimize" };
|
|
42
|
+
inline constexpr packed::Str MAXIMIZE { "maximize" };
|
|
43
|
+
inline constexpr packed::Str RESTORE { "restore" };
|
|
44
|
+
inline constexpr packed::Str ENTER_FULLSCREEN { "enterFullscreen" };
|
|
45
|
+
inline constexpr packed::Str LEAVE_FULLSCREEN { "leaveFullscreen" };
|
|
46
|
+
inline constexpr packed::Str FOCUS { "focus" };
|
|
47
|
+
inline constexpr packed::Str BLUR { "blur" };
|
|
48
|
+
inline constexpr packed::Str HOVER { "hover" };
|
|
49
|
+
inline constexpr packed::Str LEAVE { "leave" };
|
|
50
|
+
inline constexpr packed::Str KEY_DOWN { "keyDown" };
|
|
51
|
+
inline constexpr packed::Str KEY_UP { "keyUp" };
|
|
52
|
+
inline constexpr packed::Str TEXT_INPUT { "textInput" };
|
|
53
|
+
inline constexpr packed::Str MOUSE_MOVE { "mouseMove" };
|
|
54
|
+
inline constexpr packed::Str MOUSE_BUTTON_DOWN { "mouseButtonDown" };
|
|
55
|
+
inline constexpr packed::Str MOUSE_BUTTON_UP { "mouseButtonUp" };
|
|
56
|
+
inline constexpr packed::Str MOUSE_WHEEL { "mouseWheel" };
|
|
57
|
+
inline constexpr packed::Str DROP_BEGIN { "dropBegin" };
|
|
58
|
+
inline constexpr packed::Str DROP_COMPLETE { "dropComplete" };
|
|
59
|
+
inline constexpr packed::Str DROP_FILE { "dropFile" };
|
|
60
|
+
inline constexpr packed::Str DROP_TEXT { "dropText" };
|
|
61
|
+
inline constexpr packed::Str CLOSE { "close" };
|
|
62
|
+
inline constexpr packed::Str RENDER_DEVICE_LOST { "renderDeviceLost" };
|
|
63
|
+
inline constexpr packed::Str KEYMAP_CHANGE { "keymapChange" };
|
|
64
|
+
inline constexpr packed::Str FINGER_DOWN { "fingerDown" };
|
|
65
|
+
inline constexpr packed::Str FINGER_UP { "fingerUp" };
|
|
66
|
+
inline constexpr packed::Str FINGER_MOVE { "fingerMove" };
|
|
67
|
+
inline constexpr packed::Str FINGER_CANCEL { "fingerCancel" };
|
|
68
|
+
inline constexpr packed::Str DEVICE_ADD { "deviceAdd" };
|
|
69
|
+
inline constexpr packed::Str DEVICE_REMOVE { "deviceRemove" };
|
|
70
|
+
inline constexpr packed::Str DEVICE_FORMAT_CHANGE { "deviceFormatChange" };
|
|
71
|
+
inline constexpr packed::Str AXIS_MOTION { "axisMotion" };
|
|
72
|
+
inline constexpr packed::Str BUTTON_DOWN { "buttonDown" };
|
|
73
|
+
inline constexpr packed::Str BUTTON_UP { "buttonUp" };
|
|
74
|
+
inline constexpr packed::Str BALL_MOTION { "ballMotion" };
|
|
75
|
+
inline constexpr packed::Str HAT_MOTION { "hatMotion" };
|
|
76
|
+
inline constexpr packed::Str POWER_UPDATE { "powerUpdate" };
|
|
77
|
+
inline constexpr packed::Str STEAM_HANDLE_UPDATE { "steamHandleUpdate" };
|
|
78
|
+
inline constexpr packed::Str REMAP { "remap" };
|
|
79
|
+
inline constexpr packed::Str UPDATE { "update" };
|
|
78
80
|
};
|
|
79
81
|
|
|
80
|
-
bool
|
|
81
|
-
bool dispatchEventFromWatch(const SDL_Event &event);
|
|
82
|
+
bool dispatchEventFromWatch (const SDL_Event &event);
|
|
82
83
|
|
|
83
|
-
Napi::Value isDispatchingFromWatch(const Napi::CallbackInfo &info);
|
|
84
|
-
Napi::Value poll(const Napi::CallbackInfo &info);
|
|
84
|
+
Napi::Value isDispatchingFromWatch (const Napi::CallbackInfo &info);
|
|
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,162 @@
|
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const std::map<SDL_GamepadType, packed::Str> gamepad::types {
|
|
11
|
+
// { SDL_GAMEPAD_TYPE_UNKNOWN, nullptr },
|
|
12
|
+
{ SDL_GAMEPAD_TYPE_STANDARD, "standard" },
|
|
13
|
+
{ SDL_GAMEPAD_TYPE_XBOX360, "xbox360" },
|
|
14
|
+
{ SDL_GAMEPAD_TYPE_XBOXONE, "xboxOne" },
|
|
15
|
+
{ SDL_GAMEPAD_TYPE_PS3, "ps3" },
|
|
16
|
+
{ SDL_GAMEPAD_TYPE_PS4, "ps4" },
|
|
17
|
+
{ SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO, "nintendoSwitchPro" },
|
|
18
|
+
{ SDL_GAMEPAD_TYPE_PS5, "ps5" },
|
|
19
|
+
{ SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT, "nintendoSwitchJoyconLeft" },
|
|
20
|
+
{ SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT, "nintendoSwitchJoyconRight" },
|
|
21
|
+
{ SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR, "nintendoSwitchJoyconPair" },
|
|
22
|
+
{ SDL_GAMEPAD_TYPE_GAMECUBE, "gamecube" },
|
|
23
|
+
{ SDL_GAMEPAD_TYPE_STEAM, "steam" },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const std::map<SDL_GamepadAxis, packed::Str> gamepad::axes {
|
|
27
|
+
{ SDL_GAMEPAD_AXIS_LEFTX, "leftStickX" },
|
|
28
|
+
{ SDL_GAMEPAD_AXIS_LEFTY, "leftStickY" },
|
|
29
|
+
{ SDL_GAMEPAD_AXIS_RIGHTX, "rightStickX" },
|
|
30
|
+
{ SDL_GAMEPAD_AXIS_RIGHTY, "rightStickY" },
|
|
31
|
+
{ SDL_GAMEPAD_AXIS_LEFT_TRIGGER, "leftTrigger" },
|
|
32
|
+
{ SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, "rightTrigger" },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const std::map<SDL_GamepadButton, packed::Str> gamepad::buttons {
|
|
36
|
+
{ SDL_GAMEPAD_BUTTON_DPAD_LEFT, "dpadLeft" },
|
|
37
|
+
{ SDL_GAMEPAD_BUTTON_DPAD_RIGHT, "dpadRight" },
|
|
38
|
+
{ SDL_GAMEPAD_BUTTON_DPAD_UP, "dpadUp" },
|
|
39
|
+
{ SDL_GAMEPAD_BUTTON_DPAD_DOWN, "dpadDown" },
|
|
40
|
+
{ SDL_GAMEPAD_BUTTON_SOUTH, "south" },
|
|
41
|
+
{ SDL_GAMEPAD_BUTTON_EAST, "east" },
|
|
42
|
+
{ SDL_GAMEPAD_BUTTON_WEST, "west" },
|
|
43
|
+
{ SDL_GAMEPAD_BUTTON_NORTH, "north" },
|
|
44
|
+
{ SDL_GAMEPAD_BUTTON_GUIDE, "guide" },
|
|
45
|
+
{ SDL_GAMEPAD_BUTTON_BACK, "back" },
|
|
46
|
+
{ SDL_GAMEPAD_BUTTON_START, "start" },
|
|
47
|
+
{ SDL_GAMEPAD_BUTTON_LEFT_STICK, "leftStick" },
|
|
48
|
+
{ SDL_GAMEPAD_BUTTON_RIGHT_STICK, "rightStick" },
|
|
49
|
+
{ SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "leftShoulder" },
|
|
50
|
+
{ SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "rightShoulder" },
|
|
51
|
+
{ SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1, "rightPaddle1" },
|
|
52
|
+
{ SDL_GAMEPAD_BUTTON_LEFT_PADDLE1, "leftPaddle1" },
|
|
53
|
+
{ SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2, "rightPaddle2" },
|
|
54
|
+
{ SDL_GAMEPAD_BUTTON_LEFT_PADDLE2, "leftPaddle2" },
|
|
55
|
+
{ SDL_GAMEPAD_BUTTON_MISC1, "misc1" },
|
|
56
|
+
{ SDL_GAMEPAD_BUTTON_MISC2, "misc2" },
|
|
57
|
+
{ SDL_GAMEPAD_BUTTON_MISC3, "misc3" },
|
|
58
|
+
{ SDL_GAMEPAD_BUTTON_MISC4, "misc4" },
|
|
59
|
+
{ SDL_GAMEPAD_BUTTON_MISC5, "misc5" },
|
|
60
|
+
{ SDL_GAMEPAD_BUTTON_MISC6, "misc6" },
|
|
61
|
+
{ SDL_GAMEPAD_BUTTON_TOUCHPAD, "touchpad" },
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const std::map<SDL_GamepadButtonLabel, packed::Str> gamepad::button_labels {
|
|
65
|
+
// { SDL_GAMEPAD_BUTTON_LABEL_UNKNOWN, nullptr },
|
|
66
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_A, "a" },
|
|
67
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_B, "b" },
|
|
68
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_X, "x" },
|
|
69
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_Y, "y" },
|
|
70
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_CROSS, "cross" },
|
|
71
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_CIRCLE, "circle" },
|
|
72
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_SQUARE, "square" },
|
|
73
|
+
{ SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE, "triangle" },
|
|
74
|
+
};
|
|
13
75
|
|
|
14
76
|
|
|
15
77
|
double
|
|
16
78
|
gamepad::mapAxis (SDL_Gamepad *gamepad, SDL_GamepadAxis axis) {
|
|
17
|
-
return mapAxisValue(SDL_GetGamepadAxis(gamepad, axis));
|
|
79
|
+
return joystick::mapAxisValue(SDL_GetGamepadAxis(gamepad, axis));
|
|
18
80
|
}
|
|
19
81
|
|
|
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)
|
|
82
|
+
packed::Value
|
|
83
|
+
gamepad::getSteamHandle (SDL_Gamepad *gamepad)
|
|
28
84
|
{
|
|
29
85
|
Uint64 _steam_handle = SDL_GetGamepadSteamHandle(gamepad);
|
|
30
86
|
return _steam_handle != 0
|
|
31
|
-
?
|
|
32
|
-
:
|
|
87
|
+
? packed::Value((uint64_t) _steam_handle)
|
|
88
|
+
: packed::Value();
|
|
33
89
|
}
|
|
34
90
|
|
|
35
91
|
void
|
|
36
|
-
gamepad::getState (
|
|
92
|
+
gamepad::getState (SDL_Gamepad *gamepad, packed::Value &dst)
|
|
37
93
|
{
|
|
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);
|
|
94
|
+
packed::Value axes_obj = packed::Value::object();
|
|
95
|
+
for (auto axis : {
|
|
96
|
+
SDL_GAMEPAD_AXIS_LEFTX,
|
|
97
|
+
SDL_GAMEPAD_AXIS_LEFTY,
|
|
98
|
+
SDL_GAMEPAD_AXIS_RIGHTX,
|
|
99
|
+
SDL_GAMEPAD_AXIS_RIGHTY,
|
|
100
|
+
SDL_GAMEPAD_AXIS_LEFT_TRIGGER,
|
|
101
|
+
SDL_GAMEPAD_AXIS_RIGHT_TRIGGER,
|
|
102
|
+
}) {
|
|
103
|
+
axes_obj.set(gamepad::axes.at(axis), gamepad::mapAxis(gamepad, axis));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
packed::Value buttons_obj = packed::Value::object();
|
|
107
|
+
for (auto &entry : gamepad::buttons) {
|
|
108
|
+
buttons_obj.set(entry.second, SDL_GetGamepadButton(gamepad, entry.first));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
packed::Value button_labels_obj = packed::Value::object();
|
|
75
112
|
for (auto button : { SDL_GAMEPAD_BUTTON_SOUTH, SDL_GAMEPAD_BUTTON_EAST, SDL_GAMEPAD_BUTTON_WEST, SDL_GAMEPAD_BUTTON_NORTH }) {
|
|
76
113
|
auto label_entry = gamepad::button_labels.find(SDL_GetGamepadButtonLabel(gamepad, button));
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
button_labels_obj.Set(gamepad::buttons[button], label);
|
|
114
|
+
packed::Value label;
|
|
115
|
+
if (label_entry != gamepad::button_labels.end()) { label = packed::Value::ref(label_entry->second); }
|
|
116
|
+
button_labels_obj.set(gamepad::buttons.at(button), std::move(label));
|
|
81
117
|
}
|
|
82
118
|
|
|
83
|
-
dst.
|
|
84
|
-
dst.
|
|
85
|
-
dst.
|
|
119
|
+
dst.set("axes", std::move(axes_obj));
|
|
120
|
+
dst.set("buttons", std::move(buttons_obj));
|
|
121
|
+
dst.set("buttonLabels", std::move(button_labels_obj));
|
|
86
122
|
}
|
|
87
123
|
|
|
124
|
+
static bool
|
|
125
|
+
_isPlatformMatch (const std::string &mapping)
|
|
126
|
+
{
|
|
127
|
+
static const char field[] = "platform:";
|
|
128
|
+
size_t start = mapping.find(field);
|
|
129
|
+
if (start == std::string::npos) { return true; }
|
|
130
|
+
start += sizeof(field) - 1;
|
|
131
|
+
size_t end = mapping.find(',', start);
|
|
132
|
+
if (end == std::string::npos) { end = mapping.size(); }
|
|
133
|
+
const char *platform = mapping.c_str() + start;
|
|
134
|
+
size_t length = end - start;
|
|
135
|
+
|
|
136
|
+
#ifdef SDL_PLATFORM_MACOS
|
|
137
|
+
if (length == 8 && SDL_strncasecmp(platform, "Mac OS X", length) == 0) { return true; }
|
|
138
|
+
#endif
|
|
139
|
+
|
|
140
|
+
const char *current = SDL_GetPlatform();
|
|
141
|
+
return length == SDL_strlen(current) && SDL_strncasecmp(platform, current, length) == 0;
|
|
142
|
+
}
|
|
88
143
|
|
|
89
144
|
Napi::Value
|
|
90
145
|
gamepad::addMappings (const Napi::CallbackInfo &info)
|
|
91
146
|
{
|
|
92
147
|
Napi::Env env = info.Env();
|
|
93
148
|
|
|
149
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
150
|
+
global::assertReady(env);
|
|
151
|
+
|
|
94
152
|
Napi::Array mappings = info[0].As<Napi::Array>();
|
|
95
153
|
|
|
96
154
|
for (int i = 0; i < (int) mappings.Length(); i++) {
|
|
97
155
|
std::string mapping = mappings.Get(i).As<Napi::String>().Utf8Value();
|
|
98
|
-
|
|
156
|
+
if (!_isPlatformMatch(mapping)) { continue; }
|
|
99
157
|
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());
|
|
158
|
+
throw global::sdlError(env, "SDL_AddGamepadMapping", mapping);
|
|
104
159
|
}
|
|
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
160
|
SDL_ClearError();
|
|
110
161
|
}
|
|
111
162
|
|
|
@@ -117,49 +168,42 @@ gamepad::open (const Napi::CallbackInfo &info)
|
|
|
117
168
|
{
|
|
118
169
|
Napi::Env env = info.Env();
|
|
119
170
|
|
|
120
|
-
|
|
171
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
172
|
+
global::assertReady(env);
|
|
173
|
+
|
|
174
|
+
SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
|
|
121
175
|
|
|
122
|
-
SDL_ClearError();
|
|
123
176
|
SDL_Gamepad *gamepad = SDL_OpenGamepad(id);
|
|
124
177
|
if (gamepad == nullptr) {
|
|
125
|
-
|
|
126
|
-
message << "SDL_OpenGamepad(" << id << ") error: " << SDL_GetError();
|
|
127
|
-
SDL_ClearError();
|
|
128
|
-
throw Napi::Error::New(env, message.str());
|
|
178
|
+
throw global::sdlError(env, "SDL_OpenGamepad", id);
|
|
129
179
|
}
|
|
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
180
|
SDL_ClearError();
|
|
135
181
|
|
|
182
|
+
threads::trackOpen(env, threads::GAMEPAD, id);
|
|
183
|
+
|
|
136
184
|
try {
|
|
137
185
|
int _firmware_version = SDL_GetGamepadFirmwareVersion(gamepad);
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
: env.Null();
|
|
186
|
+
packed::Value firmware_version;
|
|
187
|
+
if (_firmware_version != 0) { firmware_version = _firmware_version; }
|
|
141
188
|
|
|
142
189
|
const char *_serial_number = SDL_GetGamepadSerial(gamepad);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
: env.Null();
|
|
146
|
-
|
|
147
|
-
Napi::Value steam_handle = getSteamHandle(env, gamepad);
|
|
190
|
+
packed::Value serial_number;
|
|
191
|
+
if (_serial_number != nullptr) { serial_number = _serial_number; }
|
|
148
192
|
|
|
149
193
|
SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
|
|
150
|
-
Napi::Value power = joystick::getPowerInfo(env, joystick);
|
|
151
194
|
|
|
152
|
-
|
|
153
|
-
result.
|
|
154
|
-
result.
|
|
155
|
-
result.
|
|
156
|
-
result.
|
|
195
|
+
packed::Value result = packed::Value::object();
|
|
196
|
+
result.set("firmwareVersion", std::move(firmware_version));
|
|
197
|
+
result.set("serialNumber", std::move(serial_number));
|
|
198
|
+
result.set("steamHandle", getSteamHandle(gamepad));
|
|
199
|
+
result.set("power", joystick::getPowerInfo(joystick));
|
|
157
200
|
|
|
158
|
-
gamepad::getState(
|
|
201
|
+
gamepad::getState(gamepad, result);
|
|
159
202
|
|
|
160
|
-
return result;
|
|
203
|
+
return result.toNapi(env);
|
|
161
204
|
}
|
|
162
205
|
catch (...) {
|
|
206
|
+
threads::trackClose(env, threads::GAMEPAD, id);
|
|
163
207
|
SDL_CloseGamepad(gamepad);
|
|
164
208
|
throw;
|
|
165
209
|
}
|
|
@@ -170,14 +214,14 @@ gamepad::close (const Napi::CallbackInfo &info)
|
|
|
170
214
|
{
|
|
171
215
|
Napi::Env env = info.Env();
|
|
172
216
|
|
|
173
|
-
|
|
217
|
+
std::lock_guard<std::recursive_mutex> guard(global::lock());
|
|
218
|
+
global::assertReady(env);
|
|
174
219
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
220
|
+
SDL_JoystickID gamepad_id = info[0].As<Napi::Number>().Uint32Value();
|
|
221
|
+
|
|
222
|
+
threads::trackClose(env, threads::GAMEPAD, gamepad_id);
|
|
223
|
+
|
|
224
|
+
SDL_Gamepad *gamepad = global::fromIdOrThrow(env, SDL_GetGamepadFromID(gamepad_id), "SDL_GetGamepadFromID", "gamepad", gamepad_id);
|
|
181
225
|
|
|
182
226
|
SDL_CloseGamepad(gamepad);
|
|
183
227
|
|
package/src/native/gamepad.h
CHANGED
|
@@ -2,25 +2,24 @@
|
|
|
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 const std::map<SDL_GamepadType, packed::Str> types;
|
|
12
|
+
extern const std::map<SDL_GamepadAxis, packed::Str> axes;
|
|
13
|
+
extern const std::map<SDL_GamepadButton, packed::Str> buttons;
|
|
14
|
+
extern const 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
|
-
Napi::Value addMappings(const Napi::CallbackInfo &info);
|
|
22
|
-
Napi::Value open(const Napi::CallbackInfo &info);
|
|
23
|
-
Napi::Value close(const Napi::CallbackInfo &info);
|
|
20
|
+
Napi::Value addMappings (const Napi::CallbackInfo &info);
|
|
21
|
+
Napi::Value open (const Napi::CallbackInfo &info);
|
|
22
|
+
Napi::Value close (const Napi::CallbackInfo &info);
|
|
24
23
|
|
|
25
24
|
}; // namespace gamepad
|
|
26
25
|
|