@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
@@ -1,15 +1,39 @@
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
+ const std::map<Uint8, packed::Str> joystick::hat_positions {
13
+ { SDL_HAT_CENTERED, "centered" },
14
+ { SDL_HAT_UP, "up" },
15
+ { SDL_HAT_RIGHT, "right" },
16
+ { SDL_HAT_DOWN, "down" },
17
+ { SDL_HAT_LEFT, "left" },
18
+ { SDL_HAT_RIGHTUP, "rightUp" },
19
+ { SDL_HAT_RIGHTDOWN, "rightDown" },
20
+ { SDL_HAT_LEFTUP, "leftUp" },
21
+ { SDL_HAT_LEFTDOWN, "leftDown" },
22
+ };
23
+
24
+ const std::map<SDL_JoystickType, packed::Str> joystick::types {
25
+ // { SDL_JOYSTICK_TYPE_UNKNOWN, nullptr },
26
+ { SDL_JOYSTICK_TYPE_GAMEPAD, "gamepad" },
27
+ { SDL_JOYSTICK_TYPE_WHEEL, "wheel" },
28
+ { SDL_JOYSTICK_TYPE_ARCADE_STICK, "arcadeStick" },
29
+ { SDL_JOYSTICK_TYPE_FLIGHT_STICK, "flightStick" },
30
+ { SDL_JOYSTICK_TYPE_DANCE_PAD, "dancePad" },
31
+ { SDL_JOYSTICK_TYPE_GUITAR, "guitar" },
32
+ { SDL_JOYSTICK_TYPE_DRUM_KIT, "drumKit" },
33
+ { SDL_JOYSTICK_TYPE_ARCADE_PAD, "arcadePad" },
34
+ { SDL_JOYSTICK_TYPE_THROTTLE, "throttle" },
35
+ };
36
+
13
37
  SDL_GUID joystick::zero_guid;
14
38
 
15
39
  double
@@ -23,184 +47,195 @@ joystick::mapAxisValue (int value) {
23
47
  return value / range;
24
48
  }
25
49
 
26
- static Napi::Object
27
- _packDevice (Napi::Env &env, SDL_JoystickID id)
50
+ packed::Value
51
+ joystick::_getDevice (SDL_JoystickID id)
28
52
  {
53
+ joystick::JoysticksLock joysticks_lock;
54
+
29
55
  const char *_name = SDL_GetJoystickNameForID(id);
30
- Napi::Value name = _name != nullptr
31
- ? Napi::String::New(env, _name)
32
- : env.Null();
56
+ packed::Value name;
57
+ if (_name != nullptr) { name = _name; }
33
58
 
34
59
  const char *_path = SDL_GetJoystickPathForID(id);
35
- Napi::Value path = _path != nullptr
36
- ? Napi::String::New(env, _path)
37
- : env.Null();
60
+ packed::Value path;
61
+ if (_path != nullptr) { path = _path; }
38
62
 
39
63
  SDL_GUID _guid = SDL_GetJoystickGUIDForID(id);
40
- Napi::Value guid;
64
+ packed::Value guid;
41
65
  if (SDL_memcmp(&_guid, &joystick::zero_guid, sizeof(SDL_GUID)) != 0) {
42
66
  char guid_string[33];
43
67
  SDL_GUIDToString(_guid, guid_string, 33);
44
- guid = Napi::String::New(env, guid_string);
45
- }
46
- else {
47
- guid = env.Null();
68
+ guid = guid_string;
48
69
  }
49
70
 
50
71
  SDL_JoystickType _type = SDL_GetJoystickTypeForID(id);
51
72
  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();
73
+ packed::Value type;
74
+ if (type_entry != joystick::types.end()) { type = packed::Value::ref(type_entry->second); }
55
75
 
56
76
  int _vendor = SDL_GetJoystickVendorForID(id);
57
- Napi::Value vendor = _vendor != 0
58
- ? Napi::Number::New(env, _vendor)
59
- : env.Null();
77
+ packed::Value vendor;
78
+ if (_vendor != 0) { vendor = _vendor; }
60
79
 
61
80
  int _product = SDL_GetJoystickProductForID(id);
62
- Napi::Value product = _product != 0
63
- ? Napi::Number::New(env, _product)
64
- : env.Null();
81
+ packed::Value product;
82
+ if (_product != 0) { product = _product; }
65
83
 
66
84
  int _version = SDL_GetJoystickProductVersionForID(id);
67
- Napi::Value version = _version != 0
68
- ? Napi::Number::New(env, _version)
69
- : env.Null();
85
+ packed::Value version;
86
+ if (_version != 0) { version = _version; }
70
87
 
71
88
  int _player = SDL_GetJoystickPlayerIndexForID(id);
72
- Napi::Value player = _player != -1
73
- ? Napi::Number::New(env, _player)
74
- : env.Null();
89
+ packed::Value player;
90
+ if (_player != -1) { player = _player; }
75
91
 
76
92
  bool is_gamepad = SDL_IsGamepad(id);
77
93
 
78
- Napi::Value gamepad_mapping;
79
- Napi::Value gamepad_name;
80
- Napi::Value gamepad_type;
94
+ packed::Value gamepad_mapping;
95
+ packed::Value gamepad_name;
96
+ packed::Value gamepad_type;
81
97
 
82
98
  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
- }
99
+ global::SdlPtr<char> _gamepad_mapping(SDL_GetGamepadMappingForID(id));
100
+ if (_gamepad_mapping) { gamepad_mapping = _gamepad_mapping.get(); }
95
101
 
96
102
  const char *_gamepad_name = SDL_GetGamepadNameForID(id);
97
- gamepad_name = _gamepad_name != nullptr
98
- ? Napi::String::New(env, _gamepad_name)
99
- : env.Null();
103
+ if (_gamepad_name != nullptr) { gamepad_name = _gamepad_name; }
100
104
 
101
105
  SDL_GamepadType _gamepad_type = SDL_GetGamepadTypeForID(id);
102
106
  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();
107
+ if (gamepad_type_entry != gamepad::types.end()) { gamepad_type = packed::Value::ref(gamepad_type_entry->second); }
111
108
  }
112
109
 
113
110
  SDL_ClearError();
114
111
 
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);
112
+ packed::Value device = packed::Value::object();
113
+ device.set("id", id);
114
+ device.set("name", std::move(name));
115
+ device.set("path", std::move(path));
116
+ device.set("type", std::move(type));
117
+ device.set("guid", std::move(guid));
118
+ device.set("vendor", std::move(vendor));
119
+ device.set("product", std::move(product));
120
+ device.set("version", std::move(version));
121
+ device.set("player", std::move(player));
122
+ device.set("isGamepad", is_gamepad);
123
+ device.set("gamepadMapping", std::move(gamepad_mapping));
124
+ device.set("gamepadName", std::move(gamepad_name));
125
+ device.set("gamepadType", std::move(gamepad_type));
129
126
 
130
127
  return device;
131
128
  }
132
129
 
133
- Napi::Value
134
- joystick::_getDevice (Napi::Env &env, SDL_JoystickID id)
135
- {
136
- return _packDevice(env, id);
137
- }
138
-
139
130
  Napi::Array
140
131
  joystick::_getDevices (Napi::Env &env)
141
132
  {
142
133
  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());
134
+ global::SdlArray<SDL_JoystickID> joystick_ids(SDL_GetJoysticks(&num_devices));
135
+ if (!joystick_ids) {
136
+ throw global::sdlError(env, "SDL_GetJoysticks");
149
137
  }
150
138
 
151
139
  Napi::Array devices = Napi::Array::New(env, num_devices);
152
140
 
153
- try {
154
- for (int i = 0; i < num_devices; i++) {
155
- devices.Set(i, _packDevice(env, joystick_ids[i]));
156
- }
157
- }
158
- catch (...) {
159
- SDL_free(joystick_ids);
160
- throw;
141
+ for (int i = 0; i < num_devices; i++) {
142
+ devices.Set(i, joystick::_getDevice(joystick_ids[i]).toNapi(env));
161
143
  }
162
- SDL_free(joystick_ids);
163
144
 
164
145
  return devices;
165
146
  }
166
147
 
167
- Napi::Value
168
- joystick::getPowerInfo (Napi::Env &env, SDL_Joystick *joystick)
148
+ packed::Value
149
+ joystick::getPowerInfo (SDL_Joystick *joystick)
169
150
  {
170
151
  int percent;
171
152
  SDL_PowerState state = SDL_GetJoystickPowerInfo(joystick, &percent);
172
- return mapPowerInfo(env, state, percent);
153
+ return mapPowerInfo(state, percent);
173
154
  }
174
155
 
175
- Napi::Value
176
- joystick::mapPowerInfo (Napi::Env &env, SDL_PowerState _state, int _percent)
156
+ const char *
157
+ joystick::getState (SDL_Joystick *joystick, packed::Value &dst)
177
158
  {
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);
159
+ int num_axes = SDL_GetNumJoystickAxes(joystick);
160
+ if (num_axes < 0) {
161
+ return "SDL_GetNumJoystickAxes";
182
162
  }
183
- else {
184
- SDL_ClearError();
185
- state = env.Null();
163
+
164
+ packed::Value axes = packed::Value::array();
165
+ for (int i = 0; i < num_axes; i++) {
166
+ axes.push(joystick::mapAxis(joystick, i));
186
167
  }
187
- Napi::Value percent = _percent != -1
188
- ? Napi::Number::New(env, _percent)
189
- : env.Null();
190
168
 
191
- Napi::Object result = Napi::Object::New(env);
192
- result.Set("state", state);
193
- result.Set("percent", percent);
169
+ int num_balls = SDL_GetNumJoystickBalls(joystick);
170
+ if (num_balls < 0) {
171
+ return "SDL_GetNumJoystickBalls";
172
+ }
173
+
174
+ packed::Value balls = packed::Value::array();
175
+ for (int i = 0; i < num_balls; i++) {
176
+ packed::Value ball = packed::Value::object();
177
+ ball.set("x", 0);
178
+ ball.set("y", 0);
179
+ balls.push(std::move(ball));
180
+ }
181
+
182
+ int num_buttons = SDL_GetNumJoystickButtons(joystick);
183
+ if (num_buttons < 0) {
184
+ return "SDL_GetNumJoystickButtons";
185
+ }
186
+
187
+ packed::Value buttons = packed::Value::array();
188
+ for (int i = 0; i < num_buttons; i++) {
189
+ buttons.push(SDL_GetJoystickButton(joystick, i));
190
+ }
191
+
192
+ int num_hats = SDL_GetNumJoystickHats(joystick);
193
+ if (num_hats < 0) {
194
+ return "SDL_GetNumJoystickHats";
195
+ }
196
+
197
+ packed::Value hats = packed::Value::array();
198
+ for (int i = 0; i < num_hats; i++) {
199
+ auto hat_position_entry = joystick::hat_positions.find(SDL_GetJoystickHat(joystick, i));
200
+ packed::Value hat_position;
201
+ if (hat_position_entry != joystick::hat_positions.end()) { hat_position = packed::Value::ref(hat_position_entry->second); }
202
+ hats.push(std::move(hat_position));
203
+ }
204
+
205
+ dst.set("axes", std::move(axes));
206
+ dst.set("balls", std::move(balls));
207
+ dst.set("buttons", std::move(buttons));
208
+ dst.set("hats", std::move(hats));
209
+ return nullptr;
210
+ }
211
+
212
+ packed::Value
213
+ joystick::mapPowerInfo (SDL_PowerState _state, int _percent)
214
+ {
215
+ auto state_entry = power::states.find(_state);
216
+ packed::Value state;
217
+ if (state_entry != power::states.end()) { state = packed::Value::ref(state_entry->second); }
218
+ else { SDL_ClearError(); }
219
+
220
+ packed::Value percent;
221
+ if (_percent != -1) { percent = _percent; }
222
+
223
+ packed::Value result = packed::Value::object();
224
+ result.set("state", std::move(state));
225
+ result.set("percent", std::move(percent));
194
226
 
195
227
  return result;
196
228
  }
197
229
 
198
230
 
199
231
  Napi::Value
200
- joystick::getDevices(const Napi::CallbackInfo &info)
232
+ joystick::getDevices (const Napi::CallbackInfo &info)
201
233
  {
202
234
  Napi::Env env = info.Env();
203
235
 
236
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
237
+ global::assertReady(env);
238
+
204
239
  return joystick::_getDevices(env);
205
240
  }
206
241
 
@@ -209,123 +244,42 @@ joystick::open (const Napi::CallbackInfo &info)
209
244
  {
210
245
  Napi::Env env = info.Env();
211
246
 
212
- int id = info[0].As<Napi::Number>().Int32Value();
247
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
248
+ global::assertReady(env);
249
+
250
+ SDL_JoystickID id = info[0].As<Napi::Number>().Uint32Value();
213
251
 
214
- SDL_ClearError();
215
252
  SDL_Joystick *joystick = SDL_OpenJoystick(id);
216
253
  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());
254
+ throw global::sdlError(env, "SDL_OpenJoystick", id);
221
255
  }
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
256
  SDL_ClearError();
227
257
 
258
+ threads::trackOpen(env, threads::JOYSTICK, id);
259
+
228
260
  try {
229
261
  int _firmware_version = SDL_GetJoystickFirmwareVersion(joystick);
230
- Napi::Value firmware_version = _firmware_version != 0
231
- ? Napi::Number::New(env, _firmware_version)
232
- : env.Null();
262
+ packed::Value firmware_version;
263
+ if (_firmware_version != 0) { firmware_version = _firmware_version; }
233
264
 
234
265
  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);
266
+ packed::Value serial_number;
267
+ if (_serial_number != nullptr) { serial_number = _serial_number; }
277
268
 
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
- }
269
+ packed::Value result = packed::Value::object();
270
+ result.set("firmwareVersion", std::move(firmware_version));
271
+ result.set("serialNumber", std::move(serial_number));
272
+ result.set("power", getPowerInfo(joystick));
295
273
 
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());
274
+ const char *failed_call = joystick::getState(joystick, result);
275
+ if (failed_call != nullptr) {
276
+ throw global::sdlError(env, failed_call, id);
302
277
  }
303
278
 
304
- Napi::Array hats = Napi::Array::New(env, num_hats);
305
-
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);
313
- }
314
-
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;
279
+ return result.toNapi(env);
327
280
  }
328
281
  catch (...) {
282
+ threads::trackClose(env, threads::JOYSTICK, id);
329
283
  SDL_CloseJoystick(joystick);
330
284
  throw;
331
285
  }
@@ -336,23 +290,18 @@ joystick::rumble (const Napi::CallbackInfo &info)
336
290
  {
337
291
  Napi::Env env = info.Env();
338
292
 
339
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
293
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
294
+ global::assertReady(env);
295
+
296
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
340
297
  int low_freq_rumble = info[1].As<Napi::Number>().Int32Value();
341
298
  int high_freq_rumble = info[2].As<Napi::Number>().Int32Value();
342
299
  int duration = info[3].As<Napi::Number>().Int32Value();
343
300
 
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
- }
301
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
350
302
 
351
303
  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());
304
+ throw global::sdlError(env, "SDL_RumbleJoystick", joystick_id);
356
305
  }
357
306
 
358
307
  return env.Undefined();
@@ -363,25 +312,20 @@ joystick::getCapabilities (const Napi::CallbackInfo &info)
363
312
  {
364
313
  Napi::Env env = info.Env();
365
314
 
366
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
315
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
316
+ global::assertReady(env);
367
317
 
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
- }
318
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
319
+
320
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
374
321
 
375
322
  SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
376
323
  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());
324
+ throw global::sdlError(env, "SDL_GetJoystickProperties", joystick_id);
381
325
  }
382
326
 
383
327
  Napi::Object result = Napi::Object::New(env);
384
- result.Set("hasLed", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false));
328
+ 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
329
  result.Set("hasRumble", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false));
386
330
  result.Set("hasRumbleTriggers", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, false));
387
331
 
@@ -393,23 +337,18 @@ joystick::setLed (const Napi::CallbackInfo &info)
393
337
  {
394
338
  Napi::Env env = info.Env();
395
339
 
396
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
340
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
341
+ global::assertReady(env);
342
+
343
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
397
344
  double red = info[1].As<Napi::Number>().DoubleValue();
398
345
  double green = info[2].As<Napi::Number>().DoubleValue();
399
346
  double blue = info[3].As<Napi::Number>().DoubleValue();
400
347
 
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
- }
348
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
407
349
 
408
350
  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());
351
+ throw global::sdlError(env, "SDL_SetJoystickLED", joystick_id);
413
352
  }
414
353
 
415
354
  return env.Undefined();
@@ -420,21 +359,16 @@ joystick::setPlayer (const Napi::CallbackInfo &info)
420
359
  {
421
360
  Napi::Env env = info.Env();
422
361
 
423
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
362
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
363
+ global::assertReady(env);
364
+
365
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
424
366
  int player = info[1].As<Napi::Number>().Int32Value();
425
367
 
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
- }
368
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
432
369
 
433
370
  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());
371
+ throw global::sdlError(env, "SDL_SetJoystickPlayerIndex", joystick_id);
438
372
  }
439
373
 
440
374
  return env.Undefined();
@@ -445,23 +379,18 @@ joystick::rumbleTriggers (const Napi::CallbackInfo &info)
445
379
  {
446
380
  Napi::Env env = info.Env();
447
381
 
448
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
382
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
383
+ global::assertReady(env);
384
+
385
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
449
386
  int left_rumble = info[1].As<Napi::Number>().Int32Value();
450
387
  int right_rumble = info[2].As<Napi::Number>().Int32Value();
451
388
  int duration = info[3].As<Napi::Number>().Int32Value();
452
389
 
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
- }
390
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
459
391
 
460
392
  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());
393
+ throw global::sdlError(env, "SDL_RumbleJoystickTriggers", joystick_id);
465
394
  }
466
395
 
467
396
  return env.Undefined();
@@ -472,14 +401,14 @@ joystick::close (const Napi::CallbackInfo &info)
472
401
  {
473
402
  Napi::Env env = info.Env();
474
403
 
475
- int joystick_id = info[0].As<Napi::Number>().Int32Value();
404
+ std::lock_guard<std::recursive_mutex> guard(global::lock());
405
+ global::assertReady(env);
476
406
 
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
- }
407
+ SDL_JoystickID joystick_id = info[0].As<Napi::Number>().Uint32Value();
408
+
409
+ threads::trackClose(env, threads::JOYSTICK, joystick_id);
410
+
411
+ SDL_Joystick *joystick = global::fromIdOrThrow(env, SDL_GetJoystickFromID(joystick_id), "SDL_GetJoystickFromID", "joystick", joystick_id);
483
412
 
484
413
  SDL_CloseJoystick(joystick);
485
414