@kmamal/sdl 0.11.13 → 1.0.0-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +113 -1
- package/README.md +26 -2685
- package/binding.gyp +82 -65
- package/package.json +17 -11
- package/scripts/build-sdl.mjs +15 -7
- package/scripts/build.mjs +30 -15
- package/scripts/download-release.mjs +9 -6
- package/scripts/download-sdl.mjs +9 -6
- package/scripts/install.mjs +13 -5
- package/scripts/util/common.js +9 -1
- package/src/javascript/audio/audio-playback-stream.js +50 -0
- package/src/javascript/audio/audio-recording-stream.js +32 -0
- package/src/javascript/audio/{audio-instance.js → audio-stream.js} +40 -34
- package/src/javascript/audio/device.js +34 -6
- package/src/javascript/audio/format-helpers.js +29 -34
- package/src/javascript/audio/index.js +37 -26
- package/src/javascript/audio/prevent-exit.js +19 -9
- package/src/javascript/cleanup.js +19 -10
- package/src/javascript/enums.js +5 -0
- package/src/javascript/events/events-via-poll.js +25 -7
- package/src/javascript/events/index.js +280 -174
- package/src/javascript/gamepad/device.js +23 -0
- package/src/javascript/gamepad/gamepad-instance.js +211 -0
- package/src/javascript/gamepad/index.js +51 -0
- package/src/javascript/globals.js +8 -5
- package/src/javascript/helpers.js +41 -7
- package/src/javascript/index.js +5 -3
- package/src/javascript/joystick/device.js +5 -10
- package/src/javascript/joystick/index.js +7 -7
- package/src/javascript/joystick/joystick-instance.js +81 -42
- package/src/javascript/joystick/shared.js +90 -0
- package/src/javascript/keyboard/index.js +6 -4
- package/src/javascript/keyboard/key-mapping.js +70 -28
- package/src/javascript/mouse/index.js +21 -10
- package/src/javascript/sensor/devices.js +17 -0
- package/src/javascript/sensor/index.js +2 -16
- package/src/javascript/sensor/sensor-instance.js +22 -6
- package/src/javascript/touch/devices.js +17 -0
- package/src/javascript/touch/index.js +3 -7
- package/src/javascript/video/format-helpers.js +117 -0
- package/src/javascript/video/index.js +44 -5
- package/src/javascript/video/window.js +289 -133
- package/src/native/audio.cpp +182 -58
- package/src/native/audio.h +10 -5
- package/src/native/clipboard.cpp +22 -5
- package/src/native/cocoa-window.h +1 -0
- package/src/native/cocoa-window.mm +1 -1
- package/src/native/enums.cpp +91 -58
- package/src/native/events.cpp +437 -284
- package/src/native/events.h +18 -12
- package/src/native/gamepad.cpp +185 -0
- package/src/native/gamepad.h +27 -0
- package/src/native/global.cpp +173 -167
- package/src/native/global.h +0 -2
- package/src/native/joystick.cpp +287 -225
- package/src/native/joystick.h +7 -6
- package/src/native/keyboard.cpp +28 -8
- package/src/native/keyboard.h +4 -2
- package/src/native/module.cpp +14 -8
- package/src/native/mouse.cpp +54 -34
- package/src/native/mouse.h +0 -1
- package/src/native/power.cpp +10 -4
- package/src/native/power.h +1 -1
- package/src/native/sensor.cpp +52 -52
- package/src/native/sensor.h +1 -1
- package/src/native/touch.cpp +45 -24
- package/src/native/touch.h +1 -1
- package/src/native/video.cpp +115 -75
- package/src/native/video.h +5 -2
- package/src/native/window.cpp +409 -321
- package/src/native/window.h +3 -1
- package/src/types/helpers.d.ts +45 -19
- package/src/types/index.d.ts +366 -273
- package/scripts/util/fetch.js +0 -85
- package/src/javascript/audio/audio-playback-instance.js +0 -23
- package/src/javascript/audio/audio-recording-instance.js +0 -17
- package/src/javascript/controller/controller-instance.js +0 -160
- package/src/javascript/controller/device.js +0 -26
- package/src/javascript/controller/index.js +0 -33
- package/src/javascript/events/reconcile-audio-devices.js +0 -16
- package/src/javascript/events/reconcile-displays.js +0 -17
- package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
- package/src/javascript/events/reconcile.js +0 -61
- package/src/javascript/video/display.js +0 -4
- package/src/native/controller.cpp +0 -192
- package/src/native/controller.h +0 -26
package/src/native/joystick.cpp
CHANGED
|
@@ -1,164 +1,198 @@
|
|
|
1
1
|
#include "joystick.h"
|
|
2
|
-
#include "
|
|
3
|
-
#include "
|
|
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
|
-
|
|
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(
|
|
17
|
+
return mapAxisValue(SDL_GetJoystickAxis(joystick, axis));
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
double
|
|
21
|
-
joystick::mapAxisValue (
|
|
22
|
-
|
|
23
|
-
|
|
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::
|
|
31
|
-
|
|
26
|
+
static Napi::Object
|
|
27
|
+
_packDevice (Napi::Env &env, SDL_JoystickID id)
|
|
32
28
|
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
int _product = SDL_GetJoystickProductForID(id);
|
|
62
|
+
Napi::Value product = _product != 0
|
|
63
|
+
? Napi::Number::New(env, _product)
|
|
64
|
+
: env.Null();
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
int _version = SDL_GetJoystickProductVersionForID(id);
|
|
67
|
+
Napi::Value version = _version != 0
|
|
68
|
+
? Napi::Number::New(env, _version)
|
|
69
|
+
: env.Null();
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
93
|
+
gamepad_mapping = env.Null();
|
|
71
94
|
}
|
|
72
95
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
? Napi::String::New(env,
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
94
|
-
Napi::Value player = _player != -1
|
|
95
|
-
? Napi::Number::New(env, _player)
|
|
96
|
-
: env.Null();
|
|
113
|
+
SDL_ClearError();
|
|
97
114
|
|
|
98
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
133
|
+
Napi::Value
|
|
134
|
+
joystick::_getDevice (Napi::Env &env, SDL_JoystickID id)
|
|
135
|
+
{
|
|
136
|
+
return _packDevice(env, id);
|
|
137
|
+
}
|
|
103
138
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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::
|
|
168
|
+
joystick::getPowerInfo (Napi::Env &env, SDL_Joystick *joystick)
|
|
151
169
|
{
|
|
152
|
-
|
|
153
|
-
|
|
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::
|
|
176
|
+
joystick::mapPowerInfo (Napi::Env &env, SDL_PowerState _state, int _percent)
|
|
158
177
|
{
|
|
159
|
-
|
|
160
|
-
|
|
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
|
|
212
|
+
int id = info[0].As<Napi::Number>().Int32Value();
|
|
179
213
|
|
|
180
|
-
|
|
214
|
+
SDL_ClearError();
|
|
215
|
+
SDL_Joystick *joystick = SDL_OpenJoystick(id);
|
|
181
216
|
if (joystick == nullptr) {
|
|
182
217
|
std::ostringstream message;
|
|
183
|
-
message << "
|
|
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
|
-
//
|
|
223
|
+
// SDL_OpenJoystick produces errors even though it succeeds
|
|
189
224
|
const char *error = SDL_GetError();
|
|
190
|
-
if (error !=
|
|
225
|
+
if (error[0] != '\0') { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
191
226
|
SDL_ClearError();
|
|
192
227
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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 << "
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
240
|
-
|
|
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 << "
|
|
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::
|
|
249
|
-
ball.Set("x", dx);
|
|
250
|
-
ball.Set("y", dy);
|
|
304
|
+
Napi::Array hats = Napi::Array::New(env, num_hats);
|
|
251
305
|
|
|
252
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
271
|
-
|
|
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 << "
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
-
|
|
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::
|
|
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 =
|
|
368
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
|
|
313
369
|
if (joystick == nullptr) {
|
|
314
370
|
std::ostringstream message;
|
|
315
|
-
message << "
|
|
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
|
-
|
|
375
|
+
SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
|
|
376
|
+
if (props == 0) {
|
|
321
377
|
std::ostringstream message;
|
|
322
|
-
message << "
|
|
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
|
-
|
|
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 =
|
|
401
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
|
|
341
402
|
if (joystick == nullptr) {
|
|
342
403
|
std::ostringstream message;
|
|
343
|
-
message << "
|
|
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 (
|
|
408
|
+
if (!SDL_SetJoystickLED(joystick, std::lround(red * 0xFF), std::lround(green * 0xFF), std::lround(blue * 0xFF))) {
|
|
349
409
|
std::ostringstream message;
|
|
350
|
-
message << "
|
|
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 =
|
|
426
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
|
|
367
427
|
if (joystick == nullptr) {
|
|
368
428
|
std::ostringstream message;
|
|
369
|
-
message << "
|
|
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
|
-
|
|
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
|
-
|
|
386
|
-
|
|
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 =
|
|
453
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
|
|
390
454
|
if (joystick == nullptr) {
|
|
391
455
|
std::ostringstream message;
|
|
392
|
-
message << "
|
|
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 (
|
|
460
|
+
if (!SDL_RumbleJoystickTriggers(joystick, left_rumble, right_rumble, duration)) {
|
|
398
461
|
std::ostringstream message;
|
|
399
|
-
message << "
|
|
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 =
|
|
477
|
+
SDL_Joystick *joystick = SDL_GetJoystickFromID(joystick_id);
|
|
415
478
|
if (joystick == nullptr) {
|
|
416
479
|
std::ostringstream message;
|
|
417
|
-
message << "
|
|
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
|
-
|
|
484
|
+
SDL_CloseJoystick(joystick);
|
|
423
485
|
|
|
424
486
|
return env.Undefined();
|
|
425
487
|
}
|