@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.
Files changed (86) hide show
  1. package/CHANGELOG.md +113 -1
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +40 -34
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +29 -34
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +280 -174
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +211 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +41 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +81 -42
  31. package/src/javascript/joystick/shared.js +90 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +22 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +117 -0
  41. package/src/javascript/video/index.js +44 -5
  42. package/src/javascript/video/window.js +289 -133
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +409 -321
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +45 -19
  73. package/src/types/index.d.ts +366 -273
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
@@ -2,27 +2,23 @@
2
2
  #define _EVENTS_H_
3
3
 
4
4
  #include <napi.h>
5
- #include <SDL.h>
5
+ #include <SDL3/SDL.h>
6
6
  #include <string>
7
7
 
8
8
  namespace events {
9
9
 
10
- namespace families {
10
+ namespace targets {
11
11
  extern std::string APP;
12
- extern std::string DISPLAY;
12
+ extern std::string VIDEO;
13
13
  extern std::string WINDOW;
14
- extern std::string DROP;
15
- extern std::string KEYMAP;
16
14
  extern std::string KEYBOARD;
17
- extern std::string TEXT;
18
- extern std::string MOUSE;
19
- extern std::string TOUCH;
20
- extern std::string JOYSTICK_DEVICE;
21
15
  extern std::string JOYSTICK;
22
- extern std::string CONTROLLER;
16
+ extern std::string GAMEPAD;
23
17
  extern std::string SENSOR;
24
- extern std::string AUDIO_DEVICE;
25
18
  extern std::string CLIPBOARD;
19
+ extern std::string JOYSTICK_DEVICE;
20
+ extern std::string GAMEPAD_DEVICE;
21
+ extern std::string AUDIO_DEVICE;
26
22
  };
27
23
 
28
24
  namespace types {
@@ -31,6 +27,9 @@ namespace events {
31
27
  extern std::string DISPLAY_REMOVE;
32
28
  extern std::string DISPLAY_ORIENT;
33
29
  extern std::string DISPLAY_MOVE;
30
+ extern std::string DISPLAY_SCALE;
31
+ extern std::string DISPLAY_MODE;
32
+ extern std::string DISPLAY_USABLE;
34
33
  extern std::string DISPLAY_CHANGE;
35
34
  extern std::string SHOW;
36
35
  extern std::string HIDE;
@@ -40,6 +39,8 @@ namespace events {
40
39
  extern std::string MINIMIZE;
41
40
  extern std::string MAXIMIZE;
42
41
  extern std::string RESTORE;
42
+ extern std::string ENTER_FULLSCREEN;
43
+ extern std::string LEAVE_FULLSCREEN;
43
44
  extern std::string FOCUS;
44
45
  extern std::string BLUR;
45
46
  extern std::string HOVER;
@@ -56,12 +57,15 @@ namespace events {
56
57
  extern std::string DROP_FILE;
57
58
  extern std::string DROP_TEXT;
58
59
  extern std::string CLOSE;
60
+ extern std::string RENDER_DEVICE_LOST;
59
61
  extern std::string KEYMAP_CHANGE;
60
62
  extern std::string FINGER_DOWN;
61
63
  extern std::string FINGER_UP;
62
64
  extern std::string FINGER_MOVE;
65
+ extern std::string FINGER_CANCEL;
63
66
  extern std::string DEVICE_ADD;
64
67
  extern std::string DEVICE_REMOVE;
68
+ extern std::string DEVICE_FORMAT_CHANGE;
65
69
  extern std::string AXIS_MOTION;
66
70
  extern std::string BUTTON_DOWN;
67
71
  extern std::string BUTTON_UP;
@@ -73,8 +77,10 @@ namespace events {
73
77
  extern std::string UPDATE;
74
78
  };
75
79
 
76
- void dispatchEvent(const SDL_Event &event);
80
+ bool dispatchEvent(const SDL_Event &event);
81
+ bool dispatchEventFromWatch(const SDL_Event &event);
77
82
 
83
+ Napi::Value isDispatchingFromWatch(const Napi::CallbackInfo &info);
78
84
  Napi::Value poll(const Napi::CallbackInfo &info);
79
85
 
80
86
  }; // namespace events
@@ -0,0 +1,185 @@
1
+ #include "gamepad.h"
2
+ #include "joystick.h"
3
+ #include <SDL3/SDL.h>
4
+ #include <map>
5
+ #include <string>
6
+ #include <sstream>
7
+
8
+
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;
13
+
14
+
15
+ double
16
+ gamepad::mapAxis (SDL_Gamepad *gamepad, SDL_GamepadAxis axis) {
17
+ return mapAxisValue(SDL_GetGamepadAxis(gamepad, axis));
18
+ }
19
+
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)
28
+ {
29
+ Uint64 _steam_handle = SDL_GetGamepadSteamHandle(gamepad);
30
+ return _steam_handle != 0
31
+ ? Napi::BigInt::New(env, _steam_handle)
32
+ : env.Null();
33
+ }
34
+
35
+ void
36
+ gamepad::getState (Napi::Env &env, SDL_Gamepad *gamepad, Napi::Object dst)
37
+ {
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);
75
+ for (auto button : { SDL_GAMEPAD_BUTTON_SOUTH, SDL_GAMEPAD_BUTTON_EAST, SDL_GAMEPAD_BUTTON_WEST, SDL_GAMEPAD_BUTTON_NORTH }) {
76
+ 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);
81
+ }
82
+
83
+ dst.Set("axes", axes_obj);
84
+ dst.Set("buttons", buttons_obj);
85
+ dst.Set("buttonLabels", button_labels_obj);
86
+ }
87
+
88
+
89
+ Napi::Value
90
+ gamepad::addMappings (const Napi::CallbackInfo &info)
91
+ {
92
+ Napi::Env env = info.Env();
93
+
94
+ Napi::Array mappings = info[0].As<Napi::Array>();
95
+
96
+ for (int i = 0; i < (int) mappings.Length(); i++) {
97
+ std::string mapping = mappings.Get(i).As<Napi::String>().Utf8Value();
98
+ SDL_ClearError();
99
+ 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());
104
+ }
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
+ SDL_ClearError();
110
+ }
111
+
112
+ return env.Undefined();
113
+ }
114
+
115
+ Napi::Value
116
+ gamepad::open (const Napi::CallbackInfo &info)
117
+ {
118
+ Napi::Env env = info.Env();
119
+
120
+ int id = info[0].As<Napi::Number>().Int32Value();
121
+
122
+ SDL_ClearError();
123
+ SDL_Gamepad *gamepad = SDL_OpenGamepad(id);
124
+ 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());
129
+ }
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
+ SDL_ClearError();
135
+
136
+ try {
137
+ int _firmware_version = SDL_GetGamepadFirmwareVersion(gamepad);
138
+ Napi::Value firmware_version = _firmware_version != 0
139
+ ? Napi::Number::New(env, _firmware_version)
140
+ : env.Null();
141
+
142
+ 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);
148
+
149
+ SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
150
+ Napi::Value power = joystick::getPowerInfo(env, joystick);
151
+
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);
157
+
158
+ gamepad::getState(env, gamepad, result);
159
+
160
+ return result;
161
+ }
162
+ catch (...) {
163
+ SDL_CloseGamepad(gamepad);
164
+ throw;
165
+ }
166
+ }
167
+
168
+ Napi::Value
169
+ gamepad::close (const Napi::CallbackInfo &info)
170
+ {
171
+ Napi::Env env = info.Env();
172
+
173
+ int gamepad_id = info[0].As<Napi::Number>().Int32Value();
174
+
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
+ }
181
+
182
+ SDL_CloseGamepad(gamepad);
183
+
184
+ return env.Undefined();
185
+ }
@@ -0,0 +1,27 @@
1
+ #ifndef _GAMEPAD_H_
2
+ #define _GAMEPAD_H_
3
+
4
+ #include <napi.h>
5
+ #include <SDL3/SDL.h>
6
+ #include <map>
7
+ #include <string>
8
+
9
+ namespace gamepad {
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;
15
+
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);
20
+
21
+ Napi::Value addMappings(const Napi::CallbackInfo &info);
22
+ Napi::Value open(const Napi::CallbackInfo &info);
23
+ Napi::Value close(const Napi::CallbackInfo &info);
24
+
25
+ }; // namespace gamepad
26
+
27
+ #endif // _GAMEPAD_H_