@kmamal/sdl 1.0.0-4 → 1.0.0-8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/binding.gyp +3 -0
  3. package/package.json +6 -13
  4. package/scripts/util/common.js +2 -2
  5. package/src/javascript/audio/audio-playback-stream.js +9 -10
  6. package/src/javascript/audio/audio-recording-stream.js +9 -8
  7. package/src/javascript/audio/audio-stream.js +100 -44
  8. package/src/javascript/audio/conversion.js +41 -0
  9. package/src/javascript/audio/devices.js +11 -0
  10. package/src/javascript/audio/events.js +65 -0
  11. package/src/javascript/audio/format-helpers.js +8 -4
  12. package/src/javascript/audio/index.js +25 -20
  13. package/src/javascript/audio/prevent-exit.js +12 -9
  14. package/src/javascript/audio/validate.js +9 -0
  15. package/src/javascript/cleanup.js +9 -6
  16. package/src/javascript/clipboard/index.js +8 -3
  17. package/src/javascript/device-instance.js +61 -0
  18. package/src/javascript/events/events-via-poll.js +33 -27
  19. package/src/javascript/events/index.js +76 -482
  20. package/src/javascript/events/try.js +22 -0
  21. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  22. package/src/javascript/gamepad/events.js +66 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  24. package/src/javascript/gamepad/index.js +15 -16
  25. package/src/javascript/globals.js +10 -1
  26. package/src/javascript/index.js +2 -5
  27. package/src/javascript/joystick/devices.js +43 -0
  28. package/src/javascript/joystick/events.js +129 -0
  29. package/src/javascript/joystick/index.js +9 -18
  30. package/src/javascript/joystick/joystick-device-instance.js +175 -0
  31. package/src/javascript/joystick/joystick-instance.js +12 -158
  32. package/src/javascript/keyboard/index.js +20 -10
  33. package/src/javascript/keyboard/key-mapping.js +46 -27
  34. package/src/javascript/mouse/index.js +36 -29
  35. package/src/javascript/sensor/events.js +21 -0
  36. package/src/javascript/sensor/index.js +13 -2
  37. package/src/javascript/sensor/sensor-instance.js +7 -47
  38. package/src/javascript/subsystems.js +56 -0
  39. package/src/javascript/touch/index.js +9 -4
  40. package/src/javascript/validate.js +114 -0
  41. package/src/javascript/video/conversion.js +63 -0
  42. package/src/javascript/video/events.js +179 -0
  43. package/src/javascript/video/format-helpers.js +10 -8
  44. package/src/javascript/video/index.js +28 -14
  45. package/src/javascript/video/validate.js +45 -0
  46. package/src/javascript/video/window.js +144 -161
  47. package/src/native/audio.cpp +457 -151
  48. package/src/native/audio.h +12 -6
  49. package/src/native/clipboard.cpp +8 -37
  50. package/src/native/enums.cpp +2 -0
  51. package/src/native/events.cpp +416 -354
  52. package/src/native/events.h +67 -65
  53. package/src/native/gamepad.cpp +86 -103
  54. package/src/native/gamepad.h +7 -8
  55. package/src/native/global.cpp +190 -85
  56. package/src/native/global.h +48 -2
  57. package/src/native/joystick.cpp +180 -274
  58. package/src/native/joystick.h +12 -6
  59. package/src/native/keyboard.cpp +60 -22
  60. package/src/native/keyboard.h +2 -1
  61. package/src/native/module.cpp +20 -3
  62. package/src/native/mouse.cpp +27 -58
  63. package/src/native/packed.cpp +106 -0
  64. package/src/native/packed.h +91 -0
  65. package/src/native/power.cpp +7 -1
  66. package/src/native/power.h +2 -2
  67. package/src/native/sensor.cpp +61 -69
  68. package/src/native/sensor.h +3 -3
  69. package/src/native/testing.cpp +131 -0
  70. package/src/native/testing.h +16 -0
  71. package/src/native/threads.cpp +300 -0
  72. package/src/native/threads.h +36 -0
  73. package/src/native/touch.cpp +32 -43
  74. package/src/native/touch.h +2 -1
  75. package/src/native/video.cpp +145 -95
  76. package/src/native/video.h +6 -3
  77. package/src/native/window.cpp +155 -218
  78. package/src/native/window.h +21 -20
  79. package/src/types/index.d.ts +141 -69
  80. package/src/javascript/audio/device.js +0 -37
  81. package/src/javascript/joystick/device.js +0 -13
  82. package/src/javascript/joystick/shared.js +0 -90
  83. package/src/javascript/worker.js +0 -88
  84. package/src/types/worker.d.ts +0 -71
@@ -1,15 +1,16 @@
1
1
  #include "joystick.h"
2
+ #include "global.h"
3
+ #include "threads.h"
2
4
  #include "gamepad.h"
3
5
  #include "power.h"
4
6
  #include <SDL3/SDL.h>
5
7
  #include <string>
6
- #include <sstream>
7
8
  #include <map>
8
9
  #include <cmath>
9
10
 
10
11
 
11
- std::map<Uint8, std::string> joystick::hat_positions;
12
- std::map<SDL_JoystickType, std::string> joystick::types;
12
+ std::map<Uint8, packed::Str> joystick::hat_positions;
13
+ std::map<SDL_JoystickType, packed::Str> joystick::types;
13
14
  SDL_GUID joystick::zero_guid;
14
15
 
15
16
  double
@@ -23,174 +24,182 @@ joystick::mapAxisValue (int value) {
23
24
  return value / range;
24
25
  }
25
26
 
26
- static Napi::Object
27
- _packDevice (Napi::Env &env, SDL_JoystickID id)
27
+ packed::Value
28
+ joystick::_getDevice (SDL_JoystickID id)
28
29
  {
30
+ joystick::JoysticksLock joysticks_lock;
31
+
29
32
  const char *_name = SDL_GetJoystickNameForID(id);
30
- Napi::Value name = _name != nullptr
31
- ? Napi::String::New(env, _name)
32
- : env.Null();
33
+ packed::Value name;
34
+ if (_name != nullptr) { name = _name; }
33
35
 
34
36
  const char *_path = SDL_GetJoystickPathForID(id);
35
- Napi::Value path = _path != nullptr
36
- ? Napi::String::New(env, _path)
37
- : env.Null();
37
+ packed::Value path;
38
+ if (_path != nullptr) { path = _path; }
38
39
 
39
40
  SDL_GUID _guid = SDL_GetJoystickGUIDForID(id);
40
- Napi::Value guid;
41
+ packed::Value guid;
41
42
  if (SDL_memcmp(&_guid, &joystick::zero_guid, sizeof(SDL_GUID)) != 0) {
42
43
  char guid_string[33];
43
44
  SDL_GUIDToString(_guid, guid_string, 33);
44
- guid = Napi::String::New(env, guid_string);
45
- }
46
- else {
47
- guid = env.Null();
45
+ guid = guid_string;
48
46
  }
49
47
 
50
48
  SDL_JoystickType _type = SDL_GetJoystickTypeForID(id);
51
49
  auto type_entry = joystick::types.find(_type);
52
- Napi::Value type = type_entry != joystick::types.end()
53
- ? Napi::String::New(env, type_entry->second)
54
- : env.Null();
50
+ packed::Value type;
51
+ if (type_entry != joystick::types.end()) { type = packed::Value::ref(type_entry->second); }
55
52
 
56
53
  int _vendor = SDL_GetJoystickVendorForID(id);
57
- Napi::Value vendor = _vendor != 0
58
- ? Napi::Number::New(env, _vendor)
59
- : env.Null();
54
+ packed::Value vendor;
55
+ if (_vendor != 0) { vendor = _vendor; }
60
56
 
61
57
  int _product = SDL_GetJoystickProductForID(id);
62
- Napi::Value product = _product != 0
63
- ? Napi::Number::New(env, _product)
64
- : env.Null();
58
+ packed::Value product;
59
+ if (_product != 0) { product = _product; }
65
60
 
66
61
  int _version = SDL_GetJoystickProductVersionForID(id);
67
- Napi::Value version = _version != 0
68
- ? Napi::Number::New(env, _version)
69
- : env.Null();
62
+ packed::Value version;
63
+ if (_version != 0) { version = _version; }
70
64
 
71
65
  int _player = SDL_GetJoystickPlayerIndexForID(id);
72
- Napi::Value player = _player != -1
73
- ? Napi::Number::New(env, _player)
74
- : env.Null();
66
+ packed::Value player;
67
+ if (_player != -1) { player = _player; }
75
68
 
76
69
  bool is_gamepad = SDL_IsGamepad(id);
77
70
 
78
- Napi::Value gamepad_mapping;
79
- Napi::Value gamepad_name;
80
- Napi::Value gamepad_type;
71
+ packed::Value gamepad_mapping;
72
+ packed::Value gamepad_name;
73
+ packed::Value gamepad_type;
81
74
 
82
75
  if (is_gamepad) {
83
- char *_gamepad_mapping = SDL_GetGamepadMappingForID(id);
84
- if (_gamepad_mapping != nullptr) {
85
- try { gamepad_mapping = Napi::String::New(env, _gamepad_mapping); }
86
- catch (...) {
87
- SDL_free(_gamepad_mapping);
88
- throw;
89
- }
90
- SDL_free(_gamepad_mapping);
91
- }
92
- else {
93
- gamepad_mapping = env.Null();
94
- }
76
+ global::SdlPtr<char> _gamepad_mapping(SDL_GetGamepadMappingForID(id));
77
+ if (_gamepad_mapping) { gamepad_mapping = _gamepad_mapping.get(); }
95
78
 
96
79
  const char *_gamepad_name = SDL_GetGamepadNameForID(id);
97
- gamepad_name = _gamepad_name != nullptr
98
- ? Napi::String::New(env, _gamepad_name)
99
- : env.Null();
80
+ if (_gamepad_name != nullptr) { gamepad_name = _gamepad_name; }
100
81
 
101
82
  SDL_GamepadType _gamepad_type = SDL_GetGamepadTypeForID(id);
102
83
  auto gamepad_type_entry = gamepad::types.find(_gamepad_type);
103
- gamepad_type = gamepad_type_entry != gamepad::types.end()
104
- ? Napi::String::New(env, gamepad_type_entry->second)
105
- : env.Null();
106
- }
107
- else {
108
- gamepad_mapping = env.Null();
109
- gamepad_name = env.Null();
110
- gamepad_type = env.Null();
84
+ if (gamepad_type_entry != gamepad::types.end()) { gamepad_type = packed::Value::ref(gamepad_type_entry->second); }
111
85
  }
112
86
 
113
87
  SDL_ClearError();
114
88
 
115
- Napi::Object device = Napi::Object::New(env);
116
- device.Set("id", id);
117
- device.Set("name", name);
118
- device.Set("path", path);
119
- device.Set("type", type);
120
- device.Set("guid", guid);
121
- device.Set("vendor", vendor);
122
- device.Set("product", product);
123
- device.Set("version", version);
124
- device.Set("player", player);
125
- device.Set("isGamepad", is_gamepad);
126
- device.Set("gamepadMapping", gamepad_mapping);
127
- device.Set("gamepadName", gamepad_name);
128
- device.Set("gamepadType", gamepad_type);
89
+ packed::Value device = packed::Value::object();
90
+ device.set("id", id);
91
+ device.set("name", std::move(name));
92
+ device.set("path", std::move(path));
93
+ device.set("type", std::move(type));
94
+ device.set("guid", std::move(guid));
95
+ device.set("vendor", std::move(vendor));
96
+ device.set("product", std::move(product));
97
+ device.set("version", std::move(version));
98
+ device.set("player", std::move(player));
99
+ device.set("isGamepad", is_gamepad);
100
+ device.set("gamepadMapping", std::move(gamepad_mapping));
101
+ device.set("gamepadName", std::move(gamepad_name));
102
+ device.set("gamepadType", std::move(gamepad_type));
129
103
 
130
104
  return device;
131
105
  }
132
106
 
133
- Napi::Value
134
- joystick::_getDevice (Napi::Env &env, SDL_JoystickID id)
135
- {
136
- return _packDevice(env, id);
137
- }
138
-
139
107
  Napi::Array
140
108
  joystick::_getDevices (Napi::Env &env)
141
109
  {
142
110
  int num_devices;
143
- SDL_JoystickID *joystick_ids = SDL_GetJoysticks(&num_devices);
144
- if (joystick_ids == nullptr) {
145
- std::ostringstream message;
146
- message << "SDL_GetJoysticks() error: " << SDL_GetError();
147
- SDL_ClearError();
148
- throw Napi::Error::New(env, message.str());
111
+ global::SdlArray<SDL_JoystickID> joystick_ids(SDL_GetJoysticks(&num_devices));
112
+ if (!joystick_ids) {
113
+ throw global::sdlError(env, "SDL_GetJoysticks");
149
114
  }
150
115
 
151
116
  Napi::Array devices = Napi::Array::New(env, num_devices);
152
117
 
153
- try {
154
- for (int i = 0; i < num_devices; i++) {
155
- devices.Set(i, _packDevice(env, joystick_ids[i]));
156
- }
118
+ for (int i = 0; i < num_devices; i++) {
119
+ devices.Set(i, joystick::_getDevice(joystick_ids[i]).toNapi(env));
157
120
  }
158
- catch (...) {
159
- SDL_free(joystick_ids);
160
- throw;
161
- }
162
- SDL_free(joystick_ids);
163
121
 
164
122
  return devices;
165
123
  }
166
124
 
167
- Napi::Value
168
- joystick::getPowerInfo (Napi::Env &env, SDL_Joystick *joystick)
125
+ packed::Value
126
+ joystick::getPowerInfo (SDL_Joystick *joystick)
169
127
  {
170
128
  int percent;
171
129
  SDL_PowerState state = SDL_GetJoystickPowerInfo(joystick, &percent);
172
- return mapPowerInfo(env, state, percent);
130
+ return mapPowerInfo(state, percent);
173
131
  }
174
132
 
175
- Napi::Value
176
- joystick::mapPowerInfo (Napi::Env &env, SDL_PowerState _state, int _percent)
133
+ const char *
134
+ joystick::getState (SDL_Joystick *joystick, packed::Value &dst)
177
135
  {
178
- auto state_entry = power::states.find(_state);
179
- Napi::Value state;
180
- if (state_entry != power::states.end()) {
181
- state = Napi::String::New(env, state_entry->second);
136
+ int num_axes = SDL_GetNumJoystickAxes(joystick);
137
+ if (num_axes < 0) {
138
+ return "SDL_GetNumJoystickAxes";
182
139
  }
183
- else {
184
- SDL_ClearError();
185
- state = env.Null();
140
+
141
+ packed::Value axes = packed::Value::array();
142
+ for (int i = 0; i < num_axes; i++) {
143
+ axes.push(joystick::mapAxis(joystick, i));
186
144
  }
187
- Napi::Value percent = _percent != -1
188
- ? Napi::Number::New(env, _percent)
189
- : env.Null();
190
145
 
191
- Napi::Object result = Napi::Object::New(env);
192
- result.Set("state", state);
193
- result.Set("percent", percent);
146
+ int num_balls = SDL_GetNumJoystickBalls(joystick);
147
+ if (num_balls < 0) {
148
+ return "SDL_GetNumJoystickBalls";
149
+ }
150
+
151
+ packed::Value balls = packed::Value::array();
152
+ for (int i = 0; i < num_balls; i++) {
153
+ packed::Value ball = packed::Value::object();
154
+ ball.set("x", 0);
155
+ ball.set("y", 0);
156
+ balls.push(std::move(ball));
157
+ }
158
+
159
+ int num_buttons = SDL_GetNumJoystickButtons(joystick);
160
+ if (num_buttons < 0) {
161
+ return "SDL_GetNumJoystickButtons";
162
+ }
163
+
164
+ packed::Value buttons = packed::Value::array();
165
+ for (int i = 0; i < num_buttons; i++) {
166
+ buttons.push(SDL_GetJoystickButton(joystick, i));
167
+ }
168
+
169
+ int num_hats = SDL_GetNumJoystickHats(joystick);
170
+ if (num_hats < 0) {
171
+ return "SDL_GetNumJoystickHats";
172
+ }
173
+
174
+ packed::Value hats = packed::Value::array();
175
+ for (int i = 0; i < num_hats; i++) {
176
+ auto hat_position_entry = joystick::hat_positions.find(SDL_GetJoystickHat(joystick, i));
177
+ packed::Value hat_position;
178
+ if (hat_position_entry != joystick::hat_positions.end()) { hat_position = packed::Value::ref(hat_position_entry->second); }
179
+ hats.push(std::move(hat_position));
180
+ }
181
+
182
+ dst.set("axes", std::move(axes));
183
+ dst.set("balls", std::move(balls));
184
+ dst.set("buttons", std::move(buttons));
185
+ dst.set("hats", std::move(hats));
186
+ return nullptr;
187
+ }
188
+
189
+ packed::Value
190
+ joystick::mapPowerInfo (SDL_PowerState _state, int _percent)
191
+ {
192
+ auto state_entry = power::states.find(_state);
193
+ packed::Value state;
194
+ if (state_entry != power::states.end()) { state = packed::Value::ref(state_entry->second); }
195
+ else { SDL_ClearError(); }
196
+
197
+ packed::Value percent;
198
+ if (_percent != -1) { percent = _percent; }
199
+
200
+ packed::Value result = packed::Value::object();
201
+ result.set("state", std::move(state));
202
+ result.set("percent", std::move(percent));
194
203
 
195
204
  return result;
196
205
  }
@@ -201,6 +210,9 @@ joystick::getDevices(const Napi::CallbackInfo &info)
201
210
  {
202
211
  Napi::Env env = info.Env();
203
212
 
213
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
214
+ global::assertReady(env);
215
+
204
216
  return joystick::_getDevices(env);
205
217
  }
206
218
 
@@ -209,123 +221,42 @@ joystick::open (const Napi::CallbackInfo &info)
209
221
  {
210
222
  Napi::Env env = info.Env();
211
223
 
212
- int id = info[0].As<Napi::Number>().Int32Value();
224
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
225
+ global::assertReady(env);
226
+
227
+ SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
213
228
 
214
- SDL_ClearError();
215
229
  SDL_Joystick *joystick = SDL_OpenJoystick(id);
216
230
  if (joystick == nullptr) {
217
- std::ostringstream message;
218
- message << "SDL_OpenJoystick(" << id << ") error: " << SDL_GetError();
219
- SDL_ClearError();
220
- throw Napi::Error::New(env, message.str());
231
+ throw global::sdlError(env, "SDL_OpenJoystick", id);
221
232
  }
222
-
223
- // SDL_OpenJoystick produces errors even though it succeeds
224
- const char *error = SDL_GetError();
225
- if (error[0] != '\0') { fprintf(stderr, "SDL silent error: %s\n", error); }
226
233
  SDL_ClearError();
227
234
 
235
+ threads::trackOpen(env, threads::JOYSTICK, id);
236
+
228
237
  try {
229
238
  int _firmware_version = SDL_GetJoystickFirmwareVersion(joystick);
230
- Napi::Value firmware_version = _firmware_version != 0
231
- ? Napi::Number::New(env, _firmware_version)
232
- : env.Null();
239
+ packed::Value firmware_version;
240
+ if (_firmware_version != 0) { firmware_version = _firmware_version; }
233
241
 
234
242
  const char *_serial_number = SDL_GetJoystickSerial(joystick);
235
- Napi::Value serial_number = _serial_number != nullptr
236
- ? Napi::String::New(env, _serial_number)
237
- : env.Null();
238
-
239
- int num_axes = SDL_GetNumJoystickAxes(joystick);
240
- if (num_axes < 0) {
241
- std::ostringstream message;
242
- message << "SDL_GetNumJoystickAxes(" << id << ") error: " << SDL_GetError();
243
- SDL_ClearError();
244
- throw Napi::Error::New(env, message.str());
245
- }
246
-
247
- Napi::Array axes = Napi::Array::New(env, num_axes);
248
-
249
- for (int i = 0; i < num_axes; i++) {
250
- // Clear any stale error, since failure is detected via the error state
251
- SDL_ClearError();
252
- double value = joystick::mapAxis(joystick, i);
253
- error = SDL_GetError();
254
- if (error[0] != '\0') {
255
- std::ostringstream message;
256
- message << "SDL_GetJoystickAxis(" << id << ", " << i << ") error: " << error;
257
- SDL_ClearError();
258
- throw Napi::Error::New(env, message.str());
259
- }
260
- axes.Set(i, value);
261
- }
262
-
263
- int num_balls = SDL_GetNumJoystickBalls(joystick);
264
- if (num_balls < 0) {
265
- std::ostringstream message;
266
- message << "SDL_GetNumJoystickBalls(" << id << ") error: " << SDL_GetError();
267
- SDL_ClearError();
268
- throw Napi::Error::New(env, message.str());
269
- }
270
-
271
- Napi::Array balls = Napi::Array::New(env, num_balls);
272
-
273
- for (int i = 0; i < num_balls; i++) {
274
- Napi::Object ball = Napi::Object::New(env);
275
- ball.Set("x", 0);
276
- ball.Set("y", 0);
277
-
278
- balls.Set(i, ball);
279
- }
280
-
281
- int num_buttons = SDL_GetNumJoystickButtons(joystick);
282
- if (num_buttons < 0) {
283
- std::ostringstream message;
284
- message << "SDL_GetNumJoystickButtons(" << id << ") error: " << SDL_GetError();
285
- SDL_ClearError();
286
- throw Napi::Error::New(env, message.str());
287
- }
288
-
289
- Napi::Array buttons = Napi::Array::New(env, num_buttons);
290
-
291
- for (int i = 0; i < num_buttons; i++) {
292
- bool pressed = SDL_GetJoystickButton(joystick, i);
293
- buttons.Set(i, pressed);
294
- }
295
-
296
- int num_hats = SDL_GetNumJoystickHats(joystick);
297
- if (num_hats < 0) {
298
- std::ostringstream message;
299
- message << "SDL_GetNumJoystickHats(" << id << ") error: " << SDL_GetError();
300
- SDL_ClearError();
301
- throw Napi::Error::New(env, message.str());
302
- }
243
+ packed::Value serial_number;
244
+ if (_serial_number != nullptr) { serial_number = _serial_number; }
303
245
 
304
- Napi::Array hats = Napi::Array::New(env, num_hats);
246
+ packed::Value result = packed::Value::object();
247
+ result.set("firmwareVersion", std::move(firmware_version));
248
+ result.set("serialNumber", std::move(serial_number));
249
+ result.set("power", getPowerInfo(joystick));
305
250
 
306
- for (int i = 0; i < num_hats; i++) {
307
- int hat_position = SDL_GetJoystickHat(joystick, i);
308
- auto hat_position_entry = joystick::hat_positions.find(hat_position);
309
- Napi::Value hat_position_name = hat_position_entry != joystick::hat_positions.end()
310
- ? Napi::String::New(env, hat_position_entry->second)
311
- : env.Null();
312
- hats.Set(i, hat_position_name);
251
+ const char *failed_call = joystick::getState(joystick, result);
252
+ if (failed_call != nullptr) {
253
+ throw global::sdlError(env, failed_call, id);
313
254
  }
314
255
 
315
- Napi::Value power = getPowerInfo(env, joystick);
316
-
317
- Napi::Object result = Napi::Object::New(env);
318
- result.Set("firmwareVersion", firmware_version);
319
- result.Set("serialNumber", serial_number);
320
- result.Set("axes", axes);
321
- result.Set("balls", balls);
322
- result.Set("buttons", buttons);
323
- result.Set("hats", hats);
324
- result.Set("power", power);
325
-
326
- return result;
256
+ return result.toNapi(env);
327
257
  }
328
258
  catch (...) {
259
+ threads::trackClose(env, threads::JOYSTICK, id);
329
260
  SDL_CloseJoystick(joystick);
330
261
  throw;
331
262
  }
@@ -336,23 +267,18 @@ joystick::rumble (const Napi::CallbackInfo &info)
336
267
  {
337
268
  Napi::Env env = info.Env();
338
269
 
339
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
270
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
271
+ global::assertReady(env);
272
+
273
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
340
274
  int low_freq_rumble = info[1].As<Napi::Number>().Int32Value();
341
275
  int high_freq_rumble = info[2].As<Napi::Number>().Int32Value();
342
276
  int duration = info[3].As<Napi::Number>().Int32Value();
343
277
 
344
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
345
- if (joystick == nullptr) {
346
- std::ostringstream message;
347
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
348
- throw Napi::Error::New(env, message.str());
349
- }
278
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
350
279
 
351
280
  if (!SDL_RumbleJoystick(joystick, low_freq_rumble, high_freq_rumble, duration)) {
352
- std::ostringstream message;
353
- message << "SDL_RumbleJoystick(" << joystick_id << ") error: " << SDL_GetError();
354
- SDL_ClearError();
355
- throw Napi::Error::New(env, message.str());
281
+ throw global::sdlError(env, "SDL_RumbleJoystick", joystick_id);
356
282
  }
357
283
 
358
284
  return env.Undefined();
@@ -363,25 +289,20 @@ joystick::getCapabilities (const Napi::CallbackInfo &info)
363
289
  {
364
290
  Napi::Env env = info.Env();
365
291
 
366
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
292
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
293
+ global::assertReady(env);
367
294
 
368
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
369
- if (joystick == nullptr) {
370
- std::ostringstream message;
371
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
372
- throw Napi::Error::New(env, message.str());
373
- }
295
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
296
+
297
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
374
298
 
375
299
  SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
376
300
  if (props == 0) {
377
- std::ostringstream message;
378
- message << "SDL_GetJoystickProperties(" << joystick_id << ") error: " << SDL_GetError();
379
- SDL_ClearError();
380
- throw Napi::Error::New(env, message.str());
301
+ throw global::sdlError(env, "SDL_GetJoystickProperties", joystick_id);
381
302
  }
382
303
 
383
304
  Napi::Object result = Napi::Object::New(env);
384
- result.Set("hasLed", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false));
305
+ result.Set("hasLed", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false) || SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, false));
385
306
  result.Set("hasRumble", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false));
386
307
  result.Set("hasRumbleTriggers", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, false));
387
308
 
@@ -393,23 +314,18 @@ joystick::setLed (const Napi::CallbackInfo &info)
393
314
  {
394
315
  Napi::Env env = info.Env();
395
316
 
396
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
317
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
318
+ global::assertReady(env);
319
+
320
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
397
321
  double red = info[1].As<Napi::Number>().DoubleValue();
398
322
  double green = info[2].As<Napi::Number>().DoubleValue();
399
323
  double blue = info[3].As<Napi::Number>().DoubleValue();
400
324
 
401
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
402
- if (joystick == nullptr) {
403
- std::ostringstream message;
404
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
405
- throw Napi::Error::New(env, message.str());
406
- }
325
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
407
326
 
408
327
  if (!SDL_SetJoystickLED(joystick, std::lround(red * 0xFF), std::lround(green * 0xFF), std::lround(blue * 0xFF))) {
409
- std::ostringstream message;
410
- message << "SDL_SetJoystickLED(" << joystick_id << ") error: " << SDL_GetError();
411
- SDL_ClearError();
412
- throw Napi::Error::New(env, message.str());
328
+ throw global::sdlError(env, "SDL_SetJoystickLED", joystick_id);
413
329
  }
414
330
 
415
331
  return env.Undefined();
@@ -420,21 +336,16 @@ joystick::setPlayer (const Napi::CallbackInfo &info)
420
336
  {
421
337
  Napi::Env env = info.Env();
422
338
 
423
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
339
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
340
+ global::assertReady(env);
341
+
342
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
424
343
  int player = info[1].As<Napi::Number>().Int32Value();
425
344
 
426
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
427
- if (joystick == nullptr) {
428
- std::ostringstream message;
429
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
430
- throw Napi::Error::New(env, message.str());
431
- }
345
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
432
346
 
433
347
  if (!SDL_SetJoystickPlayerIndex(joystick, player)) {
434
- std::ostringstream message;
435
- message << "SDL_SetJoystickPlayerIndex(" << joystick_id << ") error: " << SDL_GetError();
436
- SDL_ClearError();
437
- throw Napi::Error::New(env, message.str());
348
+ throw global::sdlError(env, "SDL_SetJoystickPlayerIndex", joystick_id);
438
349
  }
439
350
 
440
351
  return env.Undefined();
@@ -445,23 +356,18 @@ joystick::rumbleTriggers (const Napi::CallbackInfo &info)
445
356
  {
446
357
  Napi::Env env = info.Env();
447
358
 
448
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
359
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
360
+ global::assertReady(env);
361
+
362
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
449
363
  int left_rumble = info[1].As<Napi::Number>().Int32Value();
450
364
  int right_rumble = info[2].As<Napi::Number>().Int32Value();
451
365
  int duration = info[3].As<Napi::Number>().Int32Value();
452
366
 
453
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
454
- if (joystick == nullptr) {
455
- std::ostringstream message;
456
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
457
- throw Napi::Error::New(env, message.str());
458
- }
367
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
459
368
 
460
369
  if (!SDL_RumbleJoystickTriggers(joystick, left_rumble, right_rumble, duration)) {
461
- std::ostringstream message;
462
- message << "SDL_RumbleJoystickTriggers(" << joystick_id << ") error: " << SDL_GetError();
463
- SDL_ClearError();
464
- throw Napi::Error::New(env, message.str());
370
+ throw global::sdlError(env, "SDL_RumbleJoystickTriggers", joystick_id);
465
371
  }
466
372
 
467
373
  return env.Undefined();
@@ -472,14 +378,14 @@ joystick::close (const Napi::CallbackInfo &info)
472
378
  {
473
379
  Napi::Env env = info.Env();
474
380
 
475
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
381
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
382
+ global::assertReady(env);
476
383
 
477
- SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
478
- if (joystick == nullptr) {
479
- std::ostringstream message;
480
- message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
481
- throw Napi::Error::New(env, message.str());
482
- }
384
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
385
+
386
+ threads::trackClose(env, threads::JOYSTICK, joystick_id);
387
+
388
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
483
389
 
484
390
  SDL_CloseJoystick(joystick);
485
391
 
@@ -2,22 +2,28 @@
2
2
  #define _JOYSTICK_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 joystick {
10
10
 
11
- extern std::map<SDL_JoystickType, std::string> types;
12
- extern std::map<Uint8, std::string> hat_positions;
11
+ struct JoysticksLock {
12
+ JoysticksLock () { SDL_LockJoysticks(); }
13
+ ~JoysticksLock () { SDL_UnlockJoysticks(); }
14
+ };
15
+
16
+ extern std::map<SDL_JoystickType, packed::Str> types;
17
+ extern std::map<Uint8, packed::Str> hat_positions;
13
18
  extern SDL_GUID zero_guid;
14
19
 
15
20
  double mapAxis (SDL_Joystick *joystick, int axis);
16
21
  double mapAxisValue (int value);
17
- Napi::Value _getDevice (Napi::Env &env, SDL_JoystickID id);
22
+ packed::Value _getDevice (SDL_JoystickID id);
18
23
  Napi::Array _getDevices (Napi::Env &env);
19
- Napi::Value getPowerInfo(Napi::Env &env, SDL_Joystick *joystick);
20
- Napi::Value mapPowerInfo(Napi::Env &env, SDL_PowerState state, int percent);
24
+ packed::Value getPowerInfo(SDL_Joystick *joystick);
25
+ const char *getState (SDL_Joystick *joystick, packed::Value &dst);
26
+ packed::Value mapPowerInfo(SDL_PowerState state, int percent);
21
27
 
22
28
  Napi::Value getDevices(const Napi::CallbackInfo &info);
23
29
  Napi::Value open(const Napi::CallbackInfo &info);