@kmamal/sdl 0.11.12 → 1.0.0-2

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 +257 -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} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  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 +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -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 +79 -42
  31. package/src/javascript/joystick/shared.js +88 -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 +20 -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 +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  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 +415 -305
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  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,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 std::map<SDL_JoystickPowerLevel, std::string> power_levels;
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 (SDL_Joystick *joystick, int axis, int value);
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 getPowerLevel(Napi::Env &env, SDL_Joystick *joystick);
20
- Napi::Value mapPowerLevel(Napi::Env &env, SDL_JoystickPowerLevel power);
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);
@@ -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 Uint8 *keyboard::keys;
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
- int keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode);
18
- const char *keyname = SDL_GetKeyName(keycode);
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, !!keyboard::keys[i]);
73
+ result.Set(i, keyboard::keys[i]);
54
74
  }
55
75
 
56
76
  return result;
@@ -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 Uint8 *keys;
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);
@@ -9,7 +9,7 @@
9
9
  #include "mouse.h"
10
10
  #include "touch.h"
11
11
  #include "joystick.h"
12
- #include "controller.h"
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("controller_addMappings", Napi::Function::New<controller::addMappings>(env));
75
- exports.Set("controller_open", Napi::Function::New<controller::open>(env));
76
- exports.Set("controller_close", Napi::Function::New<controller::close>(env));
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("audio_getQueueSize", Napi::Function::New<audio::getQueueSize>(env));
87
- exports.Set("audio_enqueue", Napi::Function::New<audio::enqueue>(env));
88
- exports.Set("audio_dequeue", Napi::Function::New<audio::dequeue>(env));
89
- exports.Set("audio_clearQueue", Napi::Function::New<audio::clearQueue>(env));
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));
@@ -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
- int buttons = SDL_GetMouseState(nullptr, nullptr);
17
- bool state = buttons & SDL_BUTTON(button);
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
- int x, y;
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
- int x = info[0].As<Napi::Number>().Int32Value();
43
- int y = info[1].As<Napi::Number>().Int32Value();
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) < 0) {
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 = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
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 << "SDL_CreateRGBSurfaceWithFormatFrom(" << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
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
- if (allocated_cursor != nullptr) {
125
- SDL_FreeCursor(allocated_cursor);
126
- allocated_cursor = nullptr;
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
- allocated_cursor = SDL_CreateColorCursor(surface, x, y);
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 << "SDL_CreateColorCursor(" << x << ", " << y << ") error: " << SDL_GetError();
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
- SDL_SetCursor(allocated_cursor);
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
- if (SDL_ShowCursor(should_show ? SDL_ENABLE : SDL_DISABLE) < 0) {
166
+ bool success = should_show ? SDL_ShowCursor() : SDL_HideCursor();
167
+ if (!success) {
153
168
  std::ostringstream message;
154
- message << "SDL_ShowCursor(" << should_show << ") error: " << SDL_GetError();
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 ? SDL_TRUE : SDL_FALSE) == -1) {
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();
@@ -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);
@@ -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
- Napi::Value state = _state != SDL_POWERSTATE_UNKNOWN
18
- ? Napi::String::New(env, power::states[_state])
19
- : env.Null();
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();
@@ -2,7 +2,7 @@
2
2
  #define _POWER_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
 
@@ -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 = SDL_NumSensors();
18
- if (num_devices < 0) {
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 << "SDL_NumSensors() error: " << SDL_GetError();
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
- for (int i = 0; i < num_devices; i++) {
28
- int id = SDL_SensorGetDeviceInstanceID(i);
29
- if (id == -1) {
30
- std::ostringstream message;
31
- message << "SDL_SensorGetDeviceInstanceID(" << i << ") error: " << SDL_GetError();
32
- SDL_ClearError();
33
- throw Napi::Error::New(env, message.str());
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 index = info[0].As<Napi::Number>().Int32Value();
76
+ int id = info[0].As<Napi::Number>().Int32Value();
70
77
 
71
- SDL_Sensor *sensor = SDL_SensorOpen(index);
78
+ SDL_Sensor *sensor = SDL_OpenSensor(id);
72
79
  if (sensor == nullptr) {
73
80
  std::ostringstream message;
74
- message << "SDL_SensorOpen(" << index << ") error: " << SDL_GetError();
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 = SDL_SensorFromInstanceID(sensor_id);
96
+ SDL_Sensor *sensor = SDL_GetSensorFromID(sensor_id);
90
97
  if (sensor == nullptr) {
91
98
  std::ostringstream message;
92
- message << "SDL_SensorFromInstanceID(" << sensor_id << ") error: " << SDL_GetError();
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 (SDL_SensorGetDataWithTimestamp(sensor, &_timestamp, data, 3) == -1) {
104
+ if (!SDL_GetSensorData(sensor, data, 3)) {
100
105
  std::ostringstream message;
101
- message << "SDL_SensorGetDataWithTimestamp(" << sensor_id << ") error: " << SDL_GetError();
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 = SDL_SensorFromInstanceID(sensor_id);
126
+ SDL_Sensor *sensor = SDL_GetSensorFromID(sensor_id);
126
127
  if (sensor == nullptr) {
127
128
  std::ostringstream message;
128
- message << "SDL_SensorFromInstanceID(" << sensor_id << ") error: " << SDL_GetError();
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
- SDL_SensorClose(sensor);
133
+ SDL_CloseSensor(sensor);
134
134
 
135
135
  return env.Undefined();
136
136
  }
@@ -2,7 +2,7 @@
2
2
  #define _SENSOR_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
 
@@ -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 = SDL_GetNumTouchDevices();
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, num_devices);
23
+ Napi::Array devices = Napi::Array::New(env);
17
24
 
18
- for (int i = 0; i < num_devices; i++) {
19
- SDL_TouchID id = SDL_GetTouchDevice(i);
20
- if (id == 0) {
21
- std::ostringstream message;
22
- message << "SDL_GetTouchDevice(" << i << ") error: " << SDL_GetError();
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
- const char *_name = SDL_GetTouchName(i);
28
- Napi::Value name = _name != nullptr
29
- ? Napi::String::New(env, _name)
30
- : env.Null();
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
- SDL_TouchDeviceType _type = SDL_GetTouchDeviceType(id);
33
- Napi::Value type = _type != SDL_TOUCH_DEVICE_INVALID
34
- ? Napi::String::New(env, device_types[_type])
35
- : env.Null();
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
- Napi::Object device = Napi::Object::New(env);
38
- device.Set("id", id);
39
- device.Set("name", name);
40
- device.Set("type", type);
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
- devices.Set(i, device);
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
  }