@kmamal/sdl 0.11.5 → 0.11.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.
- package/CHANGELOG.md +22 -0
- package/README.md +49 -10
- package/binding.gyp +1 -0
- package/package.json +2 -2
- package/scripts/build-sdl.mjs +4 -1
- package/scripts/install.mjs +6 -3
- package/scripts/util/fetch.js +6 -3
- package/src/javascript/audio/audio-instance.js +2 -3
- package/src/javascript/audio/device.js +1 -10
- package/src/javascript/audio/index.js +5 -7
- package/src/javascript/controller/controller-instance.js +2 -0
- package/src/javascript/controller/device.js +14 -3
- package/src/javascript/controller/index.js +5 -11
- package/src/javascript/events/index.js +56 -126
- package/src/javascript/events/reconcile-audio-devices.js +15 -0
- package/src/javascript/events/reconcile-displays.js +16 -0
- package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
- package/src/javascript/events/reconcile.js +61 -0
- package/src/javascript/joystick/device.js +9 -3
- package/src/javascript/joystick/index.js +13 -5
- package/src/javascript/joystick/joystick-instance.js +2 -0
- package/src/javascript/sensor/sensor-instance.js +2 -0
- package/src/javascript/video/display.js +4 -0
- package/src/javascript/video/index.js +1 -0
- package/src/native/audio.cpp +10 -12
- package/src/native/audio.h +4 -0
- package/src/native/controller.cpp +72 -56
- package/src/native/controller.h +3 -0
- package/src/native/enums.cpp +315 -315
- package/src/native/events.cpp +156 -80
- package/src/native/events.h +1 -0
- package/src/native/global.cpp +46 -20
- package/src/native/joystick.cpp +87 -48
- package/src/native/joystick.h +3 -2
- package/src/native/keyboard.cpp +1 -1
- package/src/native/mouse.cpp +2 -2
- package/src/native/power.cpp +9 -3
- package/src/native/sensor.cpp +24 -13
- package/src/native/video.cpp +21 -13
- package/src/native/video.h +1 -0
- package/src/native/window.cpp +28 -22
- package/src/types/index.d.ts +57 -17
|
@@ -7,24 +7,40 @@
|
|
|
7
7
|
#include <sstream>
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
std::map<SDL_GameControllerType, std::string> controller::types;
|
|
10
11
|
std::map<SDL_GameControllerAxis, std::string> controller::axes;
|
|
11
12
|
std::map<SDL_GameControllerButton, std::string> controller::buttons;
|
|
12
13
|
|
|
13
14
|
|
|
15
|
+
double
|
|
16
|
+
controller::mapAxis (SDL_GameController *controller, SDL_GameControllerAxis axis) {
|
|
17
|
+
return mapAxisValue(controller, axis, SDL_GameControllerGetAxis(controller, axis));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
double
|
|
21
|
+
controller::mapAxisValue (SDL_GameController *controller, SDL_GameControllerAxis axis, int value) {
|
|
22
|
+
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForAxis(controller, axis);
|
|
23
|
+
if (bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS) {
|
|
24
|
+
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
|
|
25
|
+
return joystick::mapAxisValue(joystick, bind.value.axis, value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
double range = value < 0 ? -SDL_JOYSTICK_AXIS_MIN : SDL_JOYSTICK_AXIS_MAX;
|
|
29
|
+
return value / range;
|
|
30
|
+
}
|
|
31
|
+
|
|
14
32
|
void
|
|
15
33
|
controller::getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst)
|
|
16
34
|
{
|
|
17
|
-
const char *error = SDL_GetError();
|
|
18
|
-
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
19
|
-
SDL_ClearError();
|
|
20
|
-
|
|
21
35
|
Napi::Object axes = Napi::Object::New(env);
|
|
22
|
-
axes.Set("leftStickX",
|
|
23
|
-
axes.Set("leftStickY",
|
|
24
|
-
axes.Set("rightStickX",
|
|
25
|
-
axes.Set("rightStickY",
|
|
26
|
-
axes.Set("leftTrigger",
|
|
27
|
-
axes.Set("rightTrigger",
|
|
36
|
+
axes.Set("leftStickX", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_LEFTX));
|
|
37
|
+
axes.Set("leftStickY", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_LEFTY));
|
|
38
|
+
axes.Set("rightStickX", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX));
|
|
39
|
+
axes.Set("rightStickY", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY));
|
|
40
|
+
axes.Set("leftTrigger", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT));
|
|
41
|
+
axes.Set("rightTrigger", controller::mapAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT));
|
|
42
|
+
|
|
43
|
+
const char *error;
|
|
28
44
|
|
|
29
45
|
error = SDL_GetError();
|
|
30
46
|
if (error != global::no_error) {
|
|
@@ -35,25 +51,25 @@ controller::getState (Napi::Env &env, SDL_GameController *controller, Napi::Obje
|
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
Napi::Object buttons = Napi::Object::New(env);
|
|
38
|
-
buttons.Set("dpadLeft",
|
|
39
|
-
buttons.Set("dpadRight",
|
|
40
|
-
buttons.Set("dpadUp",
|
|
41
|
-
buttons.Set("dpadDown",
|
|
42
|
-
buttons.Set("a",
|
|
43
|
-
buttons.Set("b",
|
|
44
|
-
buttons.Set("x",
|
|
45
|
-
buttons.Set("y",
|
|
46
|
-
buttons.Set("guide",
|
|
47
|
-
buttons.Set("back",
|
|
48
|
-
buttons.Set("start",
|
|
49
|
-
buttons.Set("leftStick",
|
|
50
|
-
buttons.Set("rightStick",
|
|
51
|
-
buttons.Set("leftShoulder",
|
|
52
|
-
buttons.Set("rightShoulder",
|
|
53
|
-
buttons.Set("paddle1",
|
|
54
|
-
buttons.Set("paddle2",
|
|
55
|
-
buttons.Set("paddle3",
|
|
56
|
-
buttons.Set("paddle4",
|
|
54
|
+
buttons.Set("dpadLeft", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
|
|
55
|
+
buttons.Set("dpadRight", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
|
|
56
|
+
buttons.Set("dpadUp", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP));
|
|
57
|
+
buttons.Set("dpadDown", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
|
|
58
|
+
buttons.Set("a", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A));
|
|
59
|
+
buttons.Set("b", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_B));
|
|
60
|
+
buttons.Set("x", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X));
|
|
61
|
+
buttons.Set("y", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_Y));
|
|
62
|
+
buttons.Set("guide", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_GUIDE));
|
|
63
|
+
buttons.Set("back", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_BACK));
|
|
64
|
+
buttons.Set("start", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_START));
|
|
65
|
+
buttons.Set("leftStick", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSTICK));
|
|
66
|
+
buttons.Set("rightStick", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK));
|
|
67
|
+
buttons.Set("leftShoulder", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
|
|
68
|
+
buttons.Set("rightShoulder", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
|
|
69
|
+
buttons.Set("paddle1", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE1));
|
|
70
|
+
buttons.Set("paddle2", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE2));
|
|
71
|
+
buttons.Set("paddle3", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE3));
|
|
72
|
+
buttons.Set("paddle4", !!SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE4));
|
|
57
73
|
|
|
58
74
|
error = SDL_GetError();
|
|
59
75
|
if (error != global::no_error) {
|
|
@@ -83,7 +99,13 @@ controller::addMappings (const Napi::CallbackInfo &info)
|
|
|
83
99
|
SDL_ClearError();
|
|
84
100
|
throw Napi::Error::New(env, message.str());
|
|
85
101
|
}
|
|
102
|
+
|
|
103
|
+
// SDL_GameControllerAddMapping produces errors even though it succeeds
|
|
104
|
+
const char *error = SDL_GetError();
|
|
105
|
+
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
106
|
+
SDL_ClearError();
|
|
86
107
|
}
|
|
108
|
+
|
|
87
109
|
return env.Undefined();
|
|
88
110
|
}
|
|
89
111
|
|
|
@@ -102,42 +124,36 @@ controller::open (const Napi::CallbackInfo &info)
|
|
|
102
124
|
throw Napi::Error::New(env, message.str());
|
|
103
125
|
}
|
|
104
126
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
SDL_ClearError();
|
|
110
|
-
throw Napi::Error::New(env, message.str());
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
SDL_JoystickID joystick_id = SDL_JoystickInstanceID(joystick);
|
|
114
|
-
if (joystick_id < 0) {
|
|
115
|
-
std::ostringstream message;
|
|
116
|
-
message << "SDL_JoystickInstanceID(" << index << ") error: " << SDL_GetError();
|
|
117
|
-
SDL_ClearError();
|
|
118
|
-
throw Napi::Error::New(env, message.str());
|
|
119
|
-
}
|
|
127
|
+
// SDL_GameControllerOpen produces errors even though it succeeds
|
|
128
|
+
const char *error = SDL_GetError();
|
|
129
|
+
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
|
|
130
|
+
SDL_ClearError();
|
|
120
131
|
|
|
121
|
-
int _firmware_version =
|
|
122
|
-
Napi::Value firmware_version = _firmware_version
|
|
132
|
+
int _firmware_version = SDL_GameControllerGetFirmwareVersion(controller);
|
|
133
|
+
Napi::Value firmware_version = _firmware_version != 0
|
|
134
|
+
? Napi::Number::New(env, _firmware_version)
|
|
135
|
+
: env.Null() ;
|
|
123
136
|
|
|
124
|
-
const char *_serial_number =
|
|
125
|
-
Napi::Value serial_number = _serial_number
|
|
137
|
+
const char *_serial_number = SDL_GameControllerGetSerial(controller);
|
|
138
|
+
Napi::Value serial_number = _serial_number != 0
|
|
139
|
+
? Napi::String::New(env, _serial_number)
|
|
140
|
+
: env.Null();
|
|
126
141
|
|
|
127
|
-
bool has_led =
|
|
128
|
-
bool has_rumble =
|
|
129
|
-
bool has_rumble_triggers =
|
|
142
|
+
bool has_led = SDL_GameControllerHasLED(controller);
|
|
143
|
+
bool has_rumble = SDL_GameControllerHasRumble(controller);
|
|
144
|
+
bool has_rumble_triggers = SDL_GameControllerHasRumbleTriggers(controller);
|
|
130
145
|
|
|
131
146
|
Uint64 _steam_handle = SDL_GameControllerGetSteamHandle(controller);
|
|
132
|
-
Napi::Value steam_handle = _steam_handle
|
|
147
|
+
Napi::Value steam_handle = _steam_handle != 0
|
|
148
|
+
? Napi::Buffer<Uint64>::Copy(env, &_steam_handle, 1)
|
|
149
|
+
: env.Null();
|
|
133
150
|
|
|
134
151
|
Napi::Object result = Napi::Object::New(env);
|
|
135
|
-
// result.Set("id", Napi::Number::New(env, joystick_id)); // NOTE: Unused. Same as the device id
|
|
136
152
|
result.Set("firmwareVersion", firmware_version);
|
|
137
153
|
result.Set("serialNumber", serial_number);
|
|
138
|
-
result.Set("hasLed",
|
|
139
|
-
result.Set("hasRumble",
|
|
140
|
-
result.Set("hasRumbleTriggers",
|
|
154
|
+
result.Set("hasLed", has_led);
|
|
155
|
+
result.Set("hasRumble", has_rumble);
|
|
156
|
+
result.Set("hasRumbleTriggers", has_rumble_triggers);
|
|
141
157
|
result.Set("steamHandle", steam_handle);
|
|
142
158
|
|
|
143
159
|
controller::getState(env, controller, result);
|
package/src/native/controller.h
CHANGED
|
@@ -8,9 +8,12 @@
|
|
|
8
8
|
|
|
9
9
|
namespace controller {
|
|
10
10
|
|
|
11
|
+
extern std::map<SDL_GameControllerType, std::string> types;
|
|
11
12
|
extern std::map<SDL_GameControllerAxis, std::string> axes;
|
|
12
13
|
extern std::map<SDL_GameControllerButton, std::string> buttons;
|
|
13
14
|
|
|
15
|
+
double mapAxis (SDL_GameController *controller, SDL_GameControllerAxis axis);
|
|
16
|
+
double mapAxisValue (SDL_GameController *controller, SDL_GameControllerAxis axis, int value);
|
|
14
17
|
void getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst);
|
|
15
18
|
|
|
16
19
|
Napi::Value addMappings(const Napi::CallbackInfo &info);
|