@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
@@ -1,164 +1,198 @@
1
1
  #include "joystick.h"
2
- #include "controller.h"
3
- #include "global.h"
4
- #include <SDL.h>
2
+ #include "gamepad.h"
3
+ #include "power.h"
4
+ #include <SDL3/SDL.h>
5
5
  #include <string>
6
6
  #include <sstream>
7
7
  #include <map>
8
+ #include <cmath>
8
9
 
9
10
 
10
11
  std::map<Uint8, std::string> joystick::hat_positions;
11
12
  std::map<SDL_JoystickType, std::string> joystick::types;
12
- std::map<SDL_JoystickPowerLevel, std::string> joystick::power_levels;
13
- SDL_JoystickGUID joystick::zero_guid;
13
+ SDL_GUID joystick::zero_guid;
14
14
 
15
15
  double
16
16
  joystick::mapAxis (SDL_Joystick *joystick, int axis) {
17
- return mapAxisValue(joystick, axis, SDL_JoystickGetAxis(joystick, axis));
17
+ return mapAxisValue(SDL_GetJoystickAxis(joystick, axis));
18
18
  }
19
19
 
20
20
  double
21
- joystick::mapAxisValue (SDL_Joystick *joystick, int axis, int value) {
22
- Sint16 initial;
23
- if (!SDL_JoystickGetAxisInitialState(joystick, axis, &initial)) { initial = 0; }
24
- double range = value < initial
25
- ? initial - SDL_JOYSTICK_AXIS_MIN
26
- : SDL_JOYSTICK_AXIS_MAX - initial;
27
- return (value - initial) / range;
21
+ joystick::mapAxisValue (int value) {
22
+ double range = value < 0 ? - SDL_JOYSTICK_AXIS_MIN : SDL_JOYSTICK_AXIS_MAX;
23
+ return value / range;
28
24
  }
29
25
 
30
- Napi::Array
31
- joystick::_getDevices (Napi::Env &env)
26
+ static Napi::Object
27
+ _packDevice (Napi::Env &env, SDL_JoystickID id)
32
28
  {
33
- int num_devices = SDL_NumJoysticks();
34
- if (num_devices < 0) {
35
- std::ostringstream message;
36
- message << "SDL_NumJoysticks() error: " << SDL_GetError();
37
- SDL_ClearError();
38
- throw Napi::Error::New(env, message.str());
29
+ const char *_name = SDL_GetJoystickNameForID(id);
30
+ Napi::Value name = _name != nullptr
31
+ ? Napi::String::New(env, _name)
32
+ : env.Null();
33
+
34
+ const char *_path = SDL_GetJoystickPathForID(id);
35
+ Napi::Value path = _path != nullptr
36
+ ? Napi::String::New(env, _path)
37
+ : env.Null();
38
+
39
+ SDL_GUID _guid = SDL_GetJoystickGUIDForID(id);
40
+ Napi::Value guid;
41
+ if (SDL_memcmp(&_guid, &joystick::zero_guid, sizeof(SDL_GUID)) != 0) {
42
+ char guid_string[33];
43
+ SDL_GUIDToString(_guid, guid_string, 33);
44
+ guid = Napi::String::New(env, guid_string);
45
+ }
46
+ else {
47
+ guid = env.Null();
39
48
  }
40
49
 
41
- Napi::Array devices = Napi::Array::New(env, num_devices);
50
+ SDL_JoystickType _type = SDL_GetJoystickTypeForID(id);
51
+ 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();
42
55
 
43
- for (int i = 0; i < num_devices; i++) {
44
- int id = SDL_JoystickGetDeviceInstanceID(i);
45
- if (id == -1) {
46
- std::ostringstream message;
47
- message << "SDL_JoystickGetDeviceInstanceID(" << i << ") error: " << SDL_GetError();
48
- SDL_ClearError();
49
- throw Napi::Error::New(env, message.str());
50
- }
56
+ int _vendor = SDL_GetJoystickVendorForID(id);
57
+ Napi::Value vendor = _vendor != 0
58
+ ? Napi::Number::New(env, _vendor)
59
+ : env.Null();
51
60
 
52
- const char *_name = SDL_JoystickNameForIndex(i);
53
- Napi::Value name = _name != nullptr
54
- ? Napi::String::New(env, _name)
55
- : env.Null();
61
+ int _product = SDL_GetJoystickProductForID(id);
62
+ Napi::Value product = _product != 0
63
+ ? Napi::Number::New(env, _product)
64
+ : env.Null();
56
65
 
57
- const char *_path = SDL_JoystickPathForIndex(i);
58
- Napi::Value path = _path != nullptr
59
- ? Napi::String::New(env, _path)
60
- : env.Null();
66
+ int _version = SDL_GetJoystickProductVersionForID(id);
67
+ Napi::Value version = _version != 0
68
+ ? Napi::Number::New(env, _version)
69
+ : env.Null();
61
70
 
62
- SDL_JoystickGUID _guid = SDL_JoystickGetDeviceGUID(i);
63
- Napi::Value guid;
64
- if (SDL_memcmp(&_guid, &zero_guid, sizeof(SDL_JoystickGUID)) != 0) {
65
- char guid_string[33];
66
- SDL_JoystickGetGUIDString(_guid, guid_string, 33);
67
- guid = Napi::String::New(env, guid_string);
71
+ int _player = SDL_GetJoystickPlayerIndexForID(id);
72
+ Napi::Value player = _player != -1
73
+ ? Napi::Number::New(env, _player)
74
+ : env.Null();
75
+
76
+ bool is_gamepad = SDL_IsGamepad(id);
77
+
78
+ Napi::Value gamepad_mapping;
79
+ Napi::Value gamepad_name;
80
+ Napi::Value gamepad_type;
81
+
82
+ 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);
68
91
  }
69
92
  else {
70
- guid = env.Null();
93
+ gamepad_mapping = env.Null();
71
94
  }
72
95
 
73
- SDL_JoystickType _type = SDL_JoystickGetDeviceType(i);
74
- Napi::Value type = _type != SDL_JOYSTICK_TYPE_UNKNOWN
75
- ? Napi::String::New(env, joystick::types[_type])
76
- : env.Null();
77
-
78
- int _vendor = SDL_JoystickGetDeviceVendor(i);
79
- Napi::Value vendor = _vendor != 0
80
- ? Napi::Number::New(env, _vendor)
96
+ const char *_gamepad_name = SDL_GetGamepadNameForID(id);
97
+ gamepad_name = _gamepad_name != nullptr
98
+ ? Napi::String::New(env, _gamepad_name)
81
99
  : env.Null();
82
100
 
83
- int _product = SDL_JoystickGetDeviceProduct(i);
84
- Napi::Value product = _product != 0
85
- ? Napi::Number::New(env, _product)
86
- : env.Null();
87
-
88
- int _version = SDL_JoystickGetDeviceProductVersion(i);
89
- Napi::Value version = _version != 0
90
- ? Napi::Number::New(env, _version)
101
+ SDL_GamepadType _gamepad_type = SDL_GetGamepadTypeForID(id);
102
+ 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)
91
105
  : env.Null();
106
+ }
107
+ else {
108
+ gamepad_mapping = env.Null();
109
+ gamepad_name = env.Null();
110
+ gamepad_type = env.Null();
111
+ }
92
112
 
93
- int _player = SDL_JoystickGetDevicePlayerIndex(i);
94
- Napi::Value player = _player != -1
95
- ? Napi::Number::New(env, _player)
96
- : env.Null();
113
+ SDL_ClearError();
97
114
 
98
- bool is_controller = SDL_IsGameController(i);
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);
129
+
130
+ return device;
131
+ }
99
132
 
100
- Napi::Value controller_mapping;
101
- Napi::Value controller_name;
102
- Napi::Value controller_type;
133
+ Napi::Value
134
+ joystick::_getDevice (Napi::Env &env, SDL_JoystickID id)
135
+ {
136
+ return _packDevice(env, id);
137
+ }
103
138
 
104
- if (is_controller) {
105
- char *_controller_mapping = SDL_GameControllerMappingForDeviceIndex(i);
106
- controller_mapping = _controller_mapping != nullptr
107
- ? Napi::String::New(env, _controller_mapping)
108
- : env.Null();
109
- SDL_free(_controller_mapping);
139
+ Napi::Array
140
+ joystick::_getDevices (Napi::Env &env)
141
+ {
142
+ 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());
149
+ }
110
150
 
111
- const char *_controller_name = SDL_GameControllerNameForIndex(i);
112
- controller_name = _controller_name != nullptr
113
- ? Napi::String::New(env, _controller_name)
114
- : env.Null();
151
+ Napi::Array devices = Napi::Array::New(env, num_devices);
115
152
 
116
- SDL_GameControllerType _controller_type = SDL_GameControllerTypeForIndex(i);
117
- controller_type = _controller_type != SDL_CONTROLLER_TYPE_UNKNOWN
118
- ? Napi::String::New(env, controller::types[_controller_type])
119
- : env.Null();
120
- }
121
- else {
122
- controller_mapping = env.Null();
123
- controller_name = env.Null();
124
- controller_type = env.Null();
153
+ try {
154
+ for (int i = 0; i < num_devices; i++) {
155
+ devices.Set(i, _packDevice(env, joystick_ids[i]));
125
156
  }
126
-
127
- Napi::Object device = Napi::Object::New(env);
128
- device.Set("_index", i);
129
- device.Set("id", id);
130
- device.Set("name", name);
131
- device.Set("path", path);
132
- device.Set("type", type);
133
- device.Set("guid", guid);
134
- device.Set("vendor", vendor);
135
- device.Set("product", product);
136
- device.Set("version", version);
137
- device.Set("player", player);
138
- device.Set("isController", is_controller);
139
- device.Set("controllerMapping", controller_mapping);
140
- device.Set("controllerName", controller_name);
141
- device.Set("controllerType", controller_type);
142
-
143
- devices.Set(i, device);
144
157
  }
158
+ catch (...) {
159
+ SDL_free(joystick_ids);
160
+ throw;
161
+ }
162
+ SDL_free(joystick_ids);
145
163
 
146
164
  return devices;
147
165
  }
148
166
 
149
167
  Napi::Value
150
- joystick::getPowerLevel (Napi::Env &env, SDL_Joystick *joystick)
168
+ joystick::getPowerInfo (Napi::Env &env, SDL_Joystick *joystick)
151
169
  {
152
- SDL_JoystickPowerLevel power = SDL_JoystickCurrentPowerLevel(joystick);
153
- return mapPowerLevel(env, power);
170
+ int percent;
171
+ SDL_PowerState state = SDL_GetJoystickPowerInfo(joystick, &percent);
172
+ return mapPowerInfo(env, state, percent);
154
173
  }
155
174
 
156
175
  Napi::Value
157
- joystick::mapPowerLevel (Napi::Env &env, SDL_JoystickPowerLevel power)
176
+ joystick::mapPowerInfo (Napi::Env &env, SDL_PowerState _state, int _percent)
158
177
  {
159
- return power != SDL_JOYSTICK_POWER_UNKNOWN
160
- ? Napi::String::New(env, joystick::power_levels[power])
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);
182
+ }
183
+ else {
184
+ SDL_ClearError();
185
+ state = env.Null();
186
+ }
187
+ Napi::Value percent = _percent != -1
188
+ ? Napi::Number::New(env, _percent)
161
189
  : env.Null();
190
+
191
+ Napi::Object result = Napi::Object::New(env);
192
+ result.Set("state", state);
193
+ result.Set("percent", percent);
194
+
195
+ return result;
162
196
  }
163
197
 
164
198
 
@@ -175,156 +209,183 @@ joystick::open (const Napi::CallbackInfo &info)
175
209
  {
176
210
  Napi::Env env = info.Env();
177
211
 
178
- int index = info[0].As<Napi::Number>().Int32Value();
212
+ int id = info[0].As<Napi::Number>().Int32Value();
179
213
 
180
- SDL_Joystick *joystick = SDL_JoystickOpen(index);
214
+ SDL_ClearError();
215
+ SDL_Joystick *joystick = SDL_OpenJoystick(id);
181
216
  if (joystick == nullptr) {
182
217
  std::ostringstream message;
183
- message << "SDL_JoystickOpen(" << index << ") error: " << SDL_GetError();
218
+ message << "SDL_OpenJoystick(" << id << ") error: " << SDL_GetError();
184
219
  SDL_ClearError();
185
220
  throw Napi::Error::New(env, message.str());
186
221
  }
187
222
 
188
- // SDL_JoystickOpen produces errors even though it succeeds
223
+ // SDL_OpenJoystick produces errors even though it succeeds
189
224
  const char *error = SDL_GetError();
190
- if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
225
+ if (error[0] != '\0') { fprintf(stderr, "SDL silent error: %s\n", error); }
191
226
  SDL_ClearError();
192
227
 
193
- int _firmware_version = SDL_JoystickGetFirmwareVersion(joystick);
194
- Napi::Value firmware_version = _firmware_version != 0
195
- ? Napi::Number::New(env, _firmware_version)
196
- : env.Null();
228
+ try {
229
+ int _firmware_version = SDL_GetJoystickFirmwareVersion(joystick);
230
+ Napi::Value firmware_version = _firmware_version != 0
231
+ ? Napi::Number::New(env, _firmware_version)
232
+ : env.Null();
197
233
 
198
- const char *_serial_number = SDL_JoystickGetSerial(joystick);
199
- Napi::Value serial_number = _serial_number != nullptr
200
- ? Napi::String::New(env, _serial_number)
201
- : env.Null();
234
+ 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
+ }
202
246
 
203
- SDL_bool has_led = SDL_JoystickHasLED(joystick);
204
- SDL_bool has_rumble = SDL_JoystickHasRumble(joystick);
205
- SDL_bool has_rumble_triggers = SDL_JoystickHasRumbleTriggers(joystick);
247
+ Napi::Array axes = Napi::Array::New(env, num_axes);
206
248
 
207
- int num_axes = SDL_JoystickNumAxes(joystick);
208
- if (num_axes < 0) {
209
- std::ostringstream message;
210
- message << "SDL_JoystickNumAxes(" << index << ") error: " << SDL_GetError();
211
- SDL_ClearError();
212
- throw Napi::Error::New(env, message.str());
213
- }
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
+ }
214
262
 
215
- Napi::Array axes = Napi::Array::New(env, num_axes);
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);
216
272
 
217
- for (int i = 0; i < num_axes; i++) {
218
- double value = joystick::mapAxis(joystick, i);
219
- error = SDL_GetError();
220
- if (error != global::no_error) {
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) {
221
283
  std::ostringstream message;
222
- message << "SDL_JoystickGetAxis(" << index << ", " << i << ") error: " << error;
284
+ message << "SDL_GetNumJoystickButtons(" << id << ") error: " << SDL_GetError();
223
285
  SDL_ClearError();
224
286
  throw Napi::Error::New(env, message.str());
225
287
  }
226
- axes.Set(i, value);
227
- }
228
288
 
229
- int num_balls = SDL_JoystickNumBalls(joystick);
230
- if (num_balls < 0) {
231
- std::ostringstream message;
232
- message << "SDL_JoystickNumBalls(" << index << ") error: " << SDL_GetError();
233
- SDL_ClearError();
234
- throw Napi::Error::New(env, message.str());
235
- }
289
+ Napi::Array buttons = Napi::Array::New(env, num_buttons);
236
290
 
237
- Napi::Array balls = Napi::Array::New(env, num_balls);
291
+ for (int i = 0; i < num_buttons; i++) {
292
+ bool pressed = SDL_GetJoystickButton(joystick, i);
293
+ buttons.Set(i, pressed);
294
+ }
238
295
 
239
- for (int i = 0; i < num_balls; i++) {
240
- int dx, dy;
241
- if (SDL_JoystickGetBall(joystick, i, &dx, &dy) == -1) {
296
+ int num_hats = SDL_GetNumJoystickHats(joystick);
297
+ if (num_hats < 0) {
242
298
  std::ostringstream message;
243
- message << "SDL_JoystickGetBall(" << index << ", " << i << ") error: " << SDL_GetError();
299
+ message << "SDL_GetNumJoystickHats(" << id << ") error: " << SDL_GetError();
244
300
  SDL_ClearError();
245
301
  throw Napi::Error::New(env, message.str());
246
302
  }
247
303
 
248
- Napi::Object ball = Napi::Object::New(env);
249
- ball.Set("x", dx);
250
- ball.Set("y", dy);
304
+ Napi::Array hats = Napi::Array::New(env, num_hats);
251
305
 
252
- balls.Set(i, ball);
253
- }
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
+ }
254
314
 
255
- int num_buttons = SDL_JoystickNumButtons(joystick);
256
- if (num_buttons < 0) {
257
- std::ostringstream message;
258
- message << "SDL_JoystickNumButtons(" << index << ") error: " << SDL_GetError();
259
- SDL_ClearError();
260
- throw Napi::Error::New(env, message.str());
261
- }
315
+ Napi::Value power = getPowerInfo(env, joystick);
262
316
 
263
- Napi::Array buttons = Napi::Array::New(env, num_buttons);
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);
264
325
 
265
- for (int i = 0; i < num_buttons; i++) {
266
- bool pressed = SDL_JoystickGetButton(joystick, i);
267
- buttons.Set(i, pressed);
326
+ return result;
268
327
  }
328
+ catch (...) {
329
+ SDL_CloseJoystick(joystick);
330
+ throw;
331
+ }
332
+ }
269
333
 
270
- int num_hats = SDL_JoystickNumHats(joystick);
271
- if (num_hats < 0) {
334
+ Napi::Value
335
+ joystick::rumble (const Napi::CallbackInfo &info)
336
+ {
337
+ Napi::Env env = info.Env();
338
+
339
+ int joystick_id = info[0].As<Napi::Number>().Int32Value();
340
+ int low_freq_rumble = info[1].As<Napi::Number>().Int32Value();
341
+ int high_freq_rumble = info[2].As<Napi::Number>().Int32Value();
342
+ int duration = info[3].As<Napi::Number>().Int32Value();
343
+
344
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
345
+ if (joystick == nullptr) {
272
346
  std::ostringstream message;
273
- message << "SDL_JoystickNumHats(" << index << ") error: " << SDL_GetError();
274
- SDL_ClearError();
347
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
275
348
  throw Napi::Error::New(env, message.str());
276
349
  }
277
350
 
278
- Napi::Array hats = Napi::Array::New(env, num_hats);
279
-
280
- for (int i = 0; i < num_hats; i++) {
281
- int hat_position = SDL_JoystickGetHat(joystick, i);
282
- hats.Set(i, joystick::hat_positions[hat_position]);
351
+ 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());
283
356
  }
284
357
 
285
- Napi::Value power = getPowerLevel(env, joystick);
286
-
287
- Napi::Object result = Napi::Object::New(env);
288
- result.Set("firmwareVersion", firmware_version);
289
- result.Set("serialNumber", serial_number);
290
- result.Set("hasLed", !!has_led);
291
- result.Set("hasRumble", !!has_rumble);
292
- result.Set("hasRumbleTriggers", !!has_rumble_triggers);
293
- result.Set("axes", axes);
294
- result.Set("balls", balls);
295
- result.Set("buttons", buttons);
296
- result.Set("hats", hats);
297
- result.Set("power", power);
298
-
299
- return result;
358
+ return env.Undefined();
300
359
  }
301
360
 
302
361
  Napi::Value
303
- joystick::rumble (const Napi::CallbackInfo &info)
362
+ joystick::getCapabilities (const Napi::CallbackInfo &info)
304
363
  {
305
364
  Napi::Env env = info.Env();
306
365
 
307
366
  int joystick_id = info[0].As<Napi::Number>().Int32Value();
308
- double low_freq_rumble = info[1].As<Napi::Number>().DoubleValue();
309
- double high_freq_rumble = info[2].As<Napi::Number>().DoubleValue();
310
- int duration = info[3].As<Napi::Number>().Int32Value();
311
367
 
312
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
368
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
313
369
  if (joystick == nullptr) {
314
370
  std::ostringstream message;
315
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
316
- SDL_ClearError();
371
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
317
372
  throw Napi::Error::New(env, message.str());
318
373
  }
319
374
 
320
- if (SDL_JoystickRumble(joystick, low_freq_rumble * 0xFFFF, high_freq_rumble * 0xFFFF, duration) == -1) {
375
+ SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
376
+ if (props == 0) {
321
377
  std::ostringstream message;
322
- message << "SDL_JoystickRumble(" << joystick_id << ") error: " << SDL_GetError();
378
+ message << "SDL_GetJoystickProperties(" << joystick_id << ") error: " << SDL_GetError();
323
379
  SDL_ClearError();
324
380
  throw Napi::Error::New(env, message.str());
325
381
  }
326
382
 
327
- return env.Undefined();
383
+ Napi::Object result = Napi::Object::New(env);
384
+ result.Set("hasLed", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false));
385
+ result.Set("hasRumble", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false));
386
+ result.Set("hasRumbleTriggers", SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, false));
387
+
388
+ return result;
328
389
  }
329
390
 
330
391
  Napi::Value
@@ -337,17 +398,16 @@ joystick::setLed (const Napi::CallbackInfo &info)
337
398
  double green = info[2].As<Napi::Number>().DoubleValue();
338
399
  double blue = info[3].As<Napi::Number>().DoubleValue();
339
400
 
340
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
401
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
341
402
  if (joystick == nullptr) {
342
403
  std::ostringstream message;
343
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
344
- SDL_ClearError();
404
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
345
405
  throw Napi::Error::New(env, message.str());
346
406
  }
347
407
 
348
- if (SDL_JoystickSetLED(joystick, red * 0xFF, green * 0xFF, blue * 0xFF) == -1) {
408
+ if (!SDL_SetJoystickLED(joystick, std::lround(red * 0xFF), std::lround(green * 0xFF), std::lround(blue * 0xFF))) {
349
409
  std::ostringstream message;
350
- message << "SDL_JoystickSetLED(" << joystick_id << ") error: " << SDL_GetError();
410
+ message << "SDL_SetJoystickLED(" << joystick_id << ") error: " << SDL_GetError();
351
411
  SDL_ClearError();
352
412
  throw Napi::Error::New(env, message.str());
353
413
  }
@@ -363,15 +423,19 @@ joystick::setPlayer (const Napi::CallbackInfo &info)
363
423
  int joystick_id = info[0].As<Napi::Number>().Int32Value();
364
424
  int player = info[1].As<Napi::Number>().Int32Value();
365
425
 
366
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
426
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
367
427
  if (joystick == nullptr) {
368
428
  std::ostringstream message;
369
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
370
- SDL_ClearError();
429
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
371
430
  throw Napi::Error::New(env, message.str());
372
431
  }
373
432
 
374
- SDL_JoystickSetPlayerIndex(joystick, player);
433
+ 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());
438
+ }
375
439
 
376
440
  return env.Undefined();
377
441
  }
@@ -382,21 +446,20 @@ joystick::rumbleTriggers (const Napi::CallbackInfo &info)
382
446
  Napi::Env env = info.Env();
383
447
 
384
448
  int joystick_id = info[0].As<Napi::Number>().Int32Value();
385
- double left_rumble = info[1].As<Napi::Number>().DoubleValue();
386
- double right_rumble = info[2].As<Napi::Number>().DoubleValue();
449
+ int left_rumble = info[1].As<Napi::Number>().Int32Value();
450
+ int right_rumble = info[2].As<Napi::Number>().Int32Value();
387
451
  int duration = info[3].As<Napi::Number>().Int32Value();
388
452
 
389
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
453
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
390
454
  if (joystick == nullptr) {
391
455
  std::ostringstream message;
392
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
393
- SDL_ClearError();
456
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
394
457
  throw Napi::Error::New(env, message.str());
395
458
  }
396
459
 
397
- if (SDL_JoystickRumbleTriggers(joystick, left_rumble * 0xFFFF, right_rumble * 0xFFFF, duration) == -1) {
460
+ if (!SDL_RumbleJoystickTriggers(joystick, left_rumble, right_rumble, duration)) {
398
461
  std::ostringstream message;
399
- message << "SDL_JoystickRumbleTriggers(" << joystick_id << ") error: " << SDL_GetError();
462
+ message << "SDL_RumbleJoystickTriggers(" << joystick_id << ") error: " << SDL_GetError();
400
463
  SDL_ClearError();
401
464
  throw Napi::Error::New(env, message.str());
402
465
  }
@@ -411,15 +474,14 @@ joystick::close (const Napi::CallbackInfo &info)
411
474
 
412
475
  int joystick_id = info[0].As<Napi::Number>().Int32Value();
413
476
 
414
- SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
477
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
415
478
  if (joystick == nullptr) {
416
479
  std::ostringstream message;
417
- message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
418
- SDL_ClearError();
480
+ message << "SDL_GetJoystickFromID(" << joystick_id << ") error: invalid joystick id";
419
481
  throw Napi::Error::New(env, message.str());
420
482
  }
421
483
 
422
- SDL_JoystickClose(joystick);
484
+ SDL_CloseJoystick(joystick);
423
485
 
424
486
  return env.Undefined();
425
487
  }