@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.
Files changed (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -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 std::string APP;
12
- extern std::string VIDEO;
13
- extern std::string WINDOW;
14
- extern std::string KEYBOARD;
15
- extern std::string JOYSTICK;
16
- extern std::string GAMEPAD;
17
- extern std::string SENSOR;
18
- extern std::string CLIPBOARD;
19
- extern std::string JOYSTICK_DEVICE;
20
- extern std::string GAMEPAD_DEVICE;
21
- extern std::string AUDIO_DEVICE;
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
- extern std::string QUIT;
26
- extern std::string DISPLAY_ADD;
27
- extern std::string DISPLAY_REMOVE;
28
- extern std::string DISPLAY_ORIENT;
29
- extern std::string DISPLAY_MOVE;
30
- extern std::string DISPLAY_SCALE;
31
- extern std::string DISPLAY_MODE;
32
- extern std::string DISPLAY_USABLE;
33
- extern std::string DISPLAY_CHANGE;
34
- extern std::string SHOW;
35
- extern std::string HIDE;
36
- extern std::string EXPOSE;
37
- extern std::string MOVE;
38
- extern std::string RESIZE;
39
- extern std::string MINIMIZE;
40
- extern std::string MAXIMIZE;
41
- extern std::string RESTORE;
42
- extern std::string ENTER_FULLSCREEN;
43
- extern std::string LEAVE_FULLSCREEN;
44
- extern std::string FOCUS;
45
- extern std::string BLUR;
46
- extern std::string HOVER;
47
- extern std::string LEAVE;
48
- extern std::string KEY_DOWN;
49
- extern std::string KEY_UP;
50
- extern std::string TEXT_INPUT;
51
- extern std::string MOUSE_MOVE;
52
- extern std::string MOUSE_BUTTON_DOWN;
53
- extern std::string MOUSE_BUTTON_UP;
54
- extern std::string MOUSE_WHEEL;
55
- extern std::string DROP_BEGIN;
56
- extern std::string DROP_COMPLETE;
57
- extern std::string DROP_FILE;
58
- extern std::string DROP_TEXT;
59
- extern std::string CLOSE;
60
- extern std::string RENDER_DEVICE_LOST;
61
- extern std::string KEYMAP_CHANGE;
62
- extern std::string FINGER_DOWN;
63
- extern std::string FINGER_UP;
64
- extern std::string FINGER_MOVE;
65
- extern std::string FINGER_CANCEL;
66
- extern std::string DEVICE_ADD;
67
- extern std::string DEVICE_REMOVE;
68
- extern std::string DEVICE_FORMAT_CHANGE;
69
- extern std::string AXIS_MOTION;
70
- extern std::string BUTTON_DOWN;
71
- extern std::string BUTTON_UP;
72
- extern std::string BALL_MOTION;
73
- extern std::string HAT_MOTION;
74
- extern std::string POWER_UPDATE;
75
- extern std::string STEAM_HANDLE_UPDATE;
76
- extern std::string REMAP;
77
- extern std::string UPDATE;
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 dispatchEvent(const SDL_Event &event);
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
 
@@ -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, std::string> gamepad::types;
10
- std::map<SDL_GamepadAxis, std::string> gamepad::axes;
11
- std::map<SDL_GamepadButton, std::string> gamepad::buttons;
12
- std::map<SDL_GamepadButtonLabel, std::string> gamepad::button_labels;
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
- double
21
- gamepad::mapAxisValue (int value) {
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
- ? Napi::BigInt::New(env, _steam_handle)
32
- : env.Null();
87
+ ? packed::Value((uint64_t) _steam_handle)
88
+ : packed::Value();
33
89
  }
34
90
 
35
91
  void
36
- gamepad::getState (Napi::Env &env, SDL_Gamepad *gamepad, Napi::Object dst)
92
+ gamepad::getState (SDL_Gamepad *gamepad, packed::Value &dst)
37
93
  {
38
- Napi::Object axes_obj = Napi::Object::New(env);
39
- axes_obj.Set("leftStickX", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTX));
40
- axes_obj.Set("leftStickY", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTY));
41
- axes_obj.Set("rightStickX", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX));
42
- axes_obj.Set("rightStickY", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY));
43
- axes_obj.Set("leftTrigger", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER));
44
- axes_obj.Set("rightTrigger", gamepad::mapAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER));
45
-
46
- Napi::Object buttons_obj = Napi::Object::New(env);
47
- buttons_obj.Set("dpadLeft", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_LEFT));
48
- buttons_obj.Set("dpadRight", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT));
49
- buttons_obj.Set("dpadUp", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_UP));
50
- buttons_obj.Set("dpadDown", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_DPAD_DOWN));
51
- buttons_obj.Set("south", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH));
52
- buttons_obj.Set("east", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_EAST));
53
- buttons_obj.Set("west", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_WEST));
54
- buttons_obj.Set("north", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_NORTH));
55
- buttons_obj.Set("guide", SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_GUIDE));
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
- Napi::Value label = label_entry != gamepad::button_labels.end()
78
- ? Napi::String::New(env, label_entry->second)
79
- : env.Null();
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.Set("axes", axes_obj);
84
- dst.Set("buttons", buttons_obj);
85
- dst.Set("buttonLabels", button_labels_obj);
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
- SDL_ClearError();
156
+ if (!_isPlatformMatch(mapping)) { continue; }
99
157
  if (SDL_AddGamepadMapping(mapping.c_str()) == -1) {
100
- std::ostringstream message;
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
- int id = info[0].As<Napi::Number>().Int32Value();
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
- std::ostringstream message;
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
- Napi::Value firmware_version = _firmware_version != 0
139
- ? Napi::Number::New(env, _firmware_version)
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
- Napi::Value serial_number = _serial_number != nullptr
144
- ? Napi::String::New(env, _serial_number)
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
- Napi::Object result = Napi::Object::New(env);
153
- result.Set("firmwareVersion", firmware_version);
154
- result.Set("serialNumber", serial_number);
155
- result.Set("steamHandle", steam_handle);
156
- result.Set("power", power);
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(env, gamepad, result);
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
- int gamepad_id = info[0].As<Napi::Number>().Int32Value();
217
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
218
+ global::assertReady(env);
174
219
 
175
- SDL_Gamepad *gamepad = SDL_GetGamepadFromID(gamepad_id);
176
- if (gamepad == nullptr) {
177
- std::ostringstream message;
178
- message << "SDL_GetGamepadFromID(" << gamepad_id << ") error: invalid gamepad id";
179
- throw Napi::Error::New(env, message.str());
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
 
@@ -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, std::string> types;
12
- extern std::map<SDL_GamepadAxis, std::string> axes;
13
- extern std::map<SDL_GamepadButton, std::string> buttons;
14
- extern std::map<SDL_GamepadButtonLabel, std::string> button_labels;
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
- double mapAxisValue (int value);
18
- void getState (Napi::Env &env, SDL_Gamepad *gamepad, Napi::Object dst);
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