@kmamal/sdl 0.11.10 → 0.11.11
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 +10 -1
- package/README.md +1042 -1030
- package/package.json +4 -4
- package/src/javascript/controller/controller-instance.js +4 -1
- package/src/javascript/events/index.js +27 -0
- package/src/javascript/joystick/joystick-instance.js +3 -1
- package/src/javascript/keyboard/index.js +11 -6
- package/src/native/controller.cpp +15 -5
- package/src/native/controller.h +1 -0
- package/src/native/events.cpp +41 -5
- package/src/native/events.h +4 -0
- package/src/native/global.cpp +4 -0
- package/src/native/joystick.cpp +18 -21
- package/src/native/joystick.h +2 -1
- package/src/native/keyboard.cpp +1 -1
- package/src/native/module.cpp +0 -1
- package/src/types/index.d.ts +34 -0
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.11.
|
|
2
|
+
"version": "0.11.11",
|
|
3
3
|
"name": "@kmamal/sdl",
|
|
4
4
|
"description": "SDL bindings for Node.js",
|
|
5
5
|
"keywords": [
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@kmamal/evdev": "^0.0.2",
|
|
67
67
|
"@kmamal/testing": "^0.0.31",
|
|
68
|
-
"@types/node": "^
|
|
69
|
-
"node-addon-api": "^8.
|
|
70
|
-
"node-gyp": "^11.
|
|
68
|
+
"@types/node": "^24.0.15",
|
|
69
|
+
"node-addon-api": "^8.5.0",
|
|
70
|
+
"node-gyp": "^11.2.0"
|
|
71
71
|
},
|
|
72
72
|
"config": {
|
|
73
73
|
"sdl": {
|
|
@@ -6,6 +6,8 @@ const validEvents = [
|
|
|
6
6
|
'axisMotion',
|
|
7
7
|
'buttonDown',
|
|
8
8
|
'buttonUp',
|
|
9
|
+
'powerUpdate',
|
|
10
|
+
'steamHandleUpdate',
|
|
9
11
|
'remap',
|
|
10
12
|
'close',
|
|
11
13
|
]
|
|
@@ -24,6 +26,7 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
24
26
|
this._hasRumble = result.hasRumble
|
|
25
27
|
this._hasRumbleTriggers = result.hasRumbleTriggers
|
|
26
28
|
this._steamHandle = result.steamHandle
|
|
29
|
+
this._power = result.power
|
|
27
30
|
this._axes = result.axes
|
|
28
31
|
this._buttons = result.buttons
|
|
29
32
|
|
|
@@ -57,7 +60,7 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
57
60
|
return this._buttons
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
get power () { return
|
|
63
|
+
get power () { return this._power }
|
|
61
64
|
|
|
62
65
|
setPlayer (player) {
|
|
63
66
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
@@ -2,6 +2,7 @@ const Globals = require('../globals')
|
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const { video: videoModule } = require('../video')
|
|
4
4
|
const { reconcileDisplays } = require('./reconcile-displays')
|
|
5
|
+
const { keyboard: keyboardModule } = require('../keyboard')
|
|
5
6
|
const { mapping } = require('../keyboard/key-mapping')
|
|
6
7
|
const { reconcileAudioDevices } = require('./reconcile-audio-devices')
|
|
7
8
|
const { reconcileJoystickAndControllerDevices } = require('./reconcile-joystick-and-controller-devices')
|
|
@@ -141,6 +142,10 @@ const handleEvent = (event) => {
|
|
|
141
142
|
window.emit(type, event)
|
|
142
143
|
} break
|
|
143
144
|
|
|
145
|
+
case 'keymap': {
|
|
146
|
+
keyboardModule.emit(type, event)
|
|
147
|
+
} break
|
|
148
|
+
|
|
144
149
|
case 'keyboard': {
|
|
145
150
|
const { windowId } = event
|
|
146
151
|
delete event.windowId
|
|
@@ -238,6 +243,21 @@ const handleEvent = (event) => {
|
|
|
238
243
|
}
|
|
239
244
|
} break
|
|
240
245
|
|
|
246
|
+
case 'powerUpdate': {
|
|
247
|
+
for (const joystickInstance of collection) {
|
|
248
|
+
joystickInstance._power = event.power
|
|
249
|
+
joystickInstance.emit(type, event)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const otherCollection = Globals.controllerInstances.byId.get(joystickId)
|
|
253
|
+
if (!otherCollection) { break }
|
|
254
|
+
|
|
255
|
+
for (const controllerInstance of otherCollection) {
|
|
256
|
+
controllerInstance._power = event.power
|
|
257
|
+
controllerInstance.emit(type, event)
|
|
258
|
+
}
|
|
259
|
+
} break
|
|
260
|
+
|
|
241
261
|
// No default
|
|
242
262
|
}
|
|
243
263
|
} break
|
|
@@ -271,6 +291,13 @@ const handleEvent = (event) => {
|
|
|
271
291
|
}
|
|
272
292
|
} break
|
|
273
293
|
|
|
294
|
+
case 'steamHandleUpdate': {
|
|
295
|
+
for (const controllerInstance of collection.values()) {
|
|
296
|
+
controllerInstance._steamHandle = event.steamHandle
|
|
297
|
+
controllerInstance.emit(type, event)
|
|
298
|
+
}
|
|
299
|
+
} break
|
|
300
|
+
|
|
274
301
|
case 'remap': {
|
|
275
302
|
const { axes, buttons } = event
|
|
276
303
|
delete event.axes
|
|
@@ -8,6 +8,7 @@ const validEvents = [
|
|
|
8
8
|
'buttonDown',
|
|
9
9
|
'buttonUp',
|
|
10
10
|
'hatMotion',
|
|
11
|
+
'powerUpdate',
|
|
11
12
|
'close',
|
|
12
13
|
]
|
|
13
14
|
|
|
@@ -28,6 +29,7 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
28
29
|
this._balls = result.balls
|
|
29
30
|
this._buttons = result.buttons
|
|
30
31
|
this._hats = result.hats
|
|
32
|
+
this._power = result.power
|
|
31
33
|
|
|
32
34
|
this._device = device
|
|
33
35
|
|
|
@@ -68,7 +70,7 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
68
70
|
return this._hats
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
get power () { return
|
|
73
|
+
get power () { return this._power }
|
|
72
74
|
|
|
73
75
|
setPlayer (player) {
|
|
74
76
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
const Globals = require('../globals')
|
|
2
2
|
const Bindings = require('../bindings')
|
|
3
3
|
const Enums = require('../enums')
|
|
4
|
+
const { EventsViaPoll } = require('../events/events-via-poll')
|
|
4
5
|
const { mapping, reverseMapping } = require('./key-mapping')
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
|
|
7
|
+
const validEvents = [ 'keymapChange' ]
|
|
8
|
+
|
|
9
|
+
const keyboard = new class extends EventsViaPoll {
|
|
10
|
+
constructor () { super(validEvents) }
|
|
11
|
+
|
|
12
|
+
get SCANCODE () { return Enums.scancodes }
|
|
8
13
|
|
|
9
14
|
getKey (scancode) {
|
|
10
15
|
if (!Number.isInteger(scancode)) { throw Object.assign(new Error("scancode must be an integer"), { scancode }) }
|
|
@@ -12,7 +17,7 @@ const keyboard = {
|
|
|
12
17
|
|
|
13
18
|
const _key = Bindings.keyboard_getKey(scancode)
|
|
14
19
|
return mapping[_key] ?? (_key?.length === 1 ? _key : null)
|
|
15
|
-
}
|
|
20
|
+
}
|
|
16
21
|
|
|
17
22
|
getScancode (key) {
|
|
18
23
|
if (typeof key !== 'string') { throw Object.assign(new Error("key must be a string"), { key }) }
|
|
@@ -21,12 +26,12 @@ const keyboard = {
|
|
|
21
26
|
if (_key === null) { throw Object.assign(new Error("invalid key"), { key }) }
|
|
22
27
|
|
|
23
28
|
return Bindings.keyboard_getScancode(_key)
|
|
24
|
-
}
|
|
29
|
+
}
|
|
25
30
|
|
|
26
31
|
getState () {
|
|
27
32
|
Globals.events.poll()
|
|
28
33
|
return Bindings.keyboard_getState()
|
|
29
|
-
}
|
|
30
|
-
}
|
|
34
|
+
}
|
|
35
|
+
}()
|
|
31
36
|
|
|
32
37
|
module.exports = { keyboard }
|
|
@@ -29,6 +29,15 @@ controller::mapAxisValue (SDL_GameController *controller, SDL_GameControllerAxis
|
|
|
29
29
|
return value / range;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
Napi::Value
|
|
33
|
+
controller::getSteamHandle (Napi::Env &env, SDL_GameController *controller)
|
|
34
|
+
{
|
|
35
|
+
Uint64 _steam_handle = SDL_GameControllerGetSteamHandle(controller);
|
|
36
|
+
return _steam_handle != 0
|
|
37
|
+
? Napi::Buffer<Uint64>::Copy(env, &_steam_handle, 1)
|
|
38
|
+
: env.Null();
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
void
|
|
33
42
|
controller::getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst)
|
|
34
43
|
{
|
|
@@ -132,7 +141,7 @@ controller::open (const Napi::CallbackInfo &info)
|
|
|
132
141
|
int _firmware_version = SDL_GameControllerGetFirmwareVersion(controller);
|
|
133
142
|
Napi::Value firmware_version = _firmware_version != 0
|
|
134
143
|
? Napi::Number::New(env, _firmware_version)
|
|
135
|
-
: env.Null()
|
|
144
|
+
: env.Null();
|
|
136
145
|
|
|
137
146
|
const char *_serial_number = SDL_GameControllerGetSerial(controller);
|
|
138
147
|
Napi::Value serial_number = _serial_number != nullptr
|
|
@@ -143,10 +152,10 @@ controller::open (const Napi::CallbackInfo &info)
|
|
|
143
152
|
bool has_rumble = SDL_GameControllerHasRumble(controller);
|
|
144
153
|
bool has_rumble_triggers = SDL_GameControllerHasRumbleTriggers(controller);
|
|
145
154
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
155
|
+
Napi::Value steam_handle = getSteamHandle(env, controller);
|
|
156
|
+
|
|
157
|
+
SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
|
|
158
|
+
Napi::Value power = joystick::getPowerLevel(env, joystick);
|
|
150
159
|
|
|
151
160
|
Napi::Object result = Napi::Object::New(env);
|
|
152
161
|
result.Set("firmwareVersion", firmware_version);
|
|
@@ -155,6 +164,7 @@ controller::open (const Napi::CallbackInfo &info)
|
|
|
155
164
|
result.Set("hasRumble", has_rumble);
|
|
156
165
|
result.Set("hasRumbleTriggers", has_rumble_triggers);
|
|
157
166
|
result.Set("steamHandle", steam_handle);
|
|
167
|
+
result.Set("power", power);
|
|
158
168
|
|
|
159
169
|
controller::getState(env, controller, result);
|
|
160
170
|
|
package/src/native/controller.h
CHANGED
|
@@ -15,6 +15,7 @@ namespace controller {
|
|
|
15
15
|
double mapAxis (SDL_GameController *controller, SDL_GameControllerAxis axis);
|
|
16
16
|
double mapAxisValue (SDL_GameController *controller, SDL_GameControllerAxis axis, int value);
|
|
17
17
|
void getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst);
|
|
18
|
+
Napi::Value getSteamHandle(Napi::Env &env, SDL_GameController *controller);
|
|
18
19
|
|
|
19
20
|
Napi::Value addMappings(const Napi::CallbackInfo &info);
|
|
20
21
|
Napi::Value open(const Napi::CallbackInfo &info);
|
package/src/native/events.cpp
CHANGED
|
@@ -14,6 +14,7 @@ std::string events::families::APP;
|
|
|
14
14
|
std::string events::families::DISPLAY;
|
|
15
15
|
std::string events::families::WINDOW;
|
|
16
16
|
std::string events::families::DROP;
|
|
17
|
+
std::string events::families::KEYMAP;
|
|
17
18
|
std::string events::families::KEYBOARD;
|
|
18
19
|
std::string events::families::TEXT;
|
|
19
20
|
std::string events::families::MOUSE;
|
|
@@ -54,6 +55,7 @@ std::string events::types::DROP_COMPLETE;
|
|
|
54
55
|
std::string events::types::DROP_FILE;
|
|
55
56
|
std::string events::types::DROP_TEXT;
|
|
56
57
|
std::string events::types::CLOSE;
|
|
58
|
+
std::string events::types::KEYMAP_CHANGE;
|
|
57
59
|
std::string events::types::DEVICE_ADD;
|
|
58
60
|
std::string events::types::DEVICE_REMOVE;
|
|
59
61
|
std::string events::types::AXIS_MOTION;
|
|
@@ -61,6 +63,8 @@ std::string events::types::BUTTON_DOWN;
|
|
|
61
63
|
std::string events::types::BUTTON_UP;
|
|
62
64
|
std::string events::types::BALL_MOTION;
|
|
63
65
|
std::string events::types::HAT_MOTION;
|
|
66
|
+
std::string events::types::POWER_UPDATE;
|
|
67
|
+
std::string events::types::STEAM_HANDLE_UPDATE;
|
|
64
68
|
std::string events::types::REMAP;
|
|
65
69
|
std::string events::types::UPDATE;
|
|
66
70
|
|
|
@@ -237,7 +241,13 @@ events::dispatchEvent(const SDL_Event &event)
|
|
|
237
241
|
packed.Set("type", events::types::TEXT_INPUT);
|
|
238
242
|
packed.Set("windowId", event.text.windowID);
|
|
239
243
|
packed.Set("text", event.text.text);
|
|
240
|
-
break
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
case SDL_KEYMAPCHANGED: {
|
|
248
|
+
packed.Set("family", events::families::KEYMAP);
|
|
249
|
+
packed.Set("type", events::types::KEYMAP_CHANGE);
|
|
250
|
+
break;
|
|
241
251
|
}
|
|
242
252
|
|
|
243
253
|
case SDL_MOUSEMOTION: {
|
|
@@ -312,7 +322,7 @@ events::dispatchEvent(const SDL_Event &event)
|
|
|
312
322
|
packed.Set("joystickId", joystick_id);
|
|
313
323
|
packed.Set("axis", event.jaxis.axis);
|
|
314
324
|
packed.Set("value", joystick::mapAxisValue(joystick, event.jaxis.axis, event.jaxis.value));
|
|
315
|
-
break
|
|
325
|
+
break;
|
|
316
326
|
}
|
|
317
327
|
case SDL_JOYBALLMOTION: {
|
|
318
328
|
packed.Set("family", events::families::JOYSTICK);
|
|
@@ -321,7 +331,7 @@ events::dispatchEvent(const SDL_Event &event)
|
|
|
321
331
|
packed.Set("ball", event.jball.ball);
|
|
322
332
|
packed.Set("x", event.jball.xrel);
|
|
323
333
|
packed.Set("y", event.jball.yrel);
|
|
324
|
-
break
|
|
334
|
+
break;
|
|
325
335
|
}
|
|
326
336
|
case SDL_JOYBUTTONDOWN:
|
|
327
337
|
case SDL_JOYBUTTONUP: {
|
|
@@ -334,7 +344,7 @@ events::dispatchEvent(const SDL_Event &event)
|
|
|
334
344
|
);
|
|
335
345
|
packed.Set("joystickId", event.jbutton.which);
|
|
336
346
|
packed.Set("button", event.jbutton.button);
|
|
337
|
-
break
|
|
347
|
+
break;
|
|
338
348
|
}
|
|
339
349
|
case SDL_JOYHATMOTION: {
|
|
340
350
|
packed.Set("family", events::families::JOYSTICK);
|
|
@@ -342,7 +352,33 @@ events::dispatchEvent(const SDL_Event &event)
|
|
|
342
352
|
packed.Set("joystickId", event.jhat.which);
|
|
343
353
|
packed.Set("hat", event.jhat.hat);
|
|
344
354
|
packed.Set("value", joystick::hat_positions[event.jhat.value]);
|
|
345
|
-
break
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
case SDL_JOYBATTERYUPDATED: {
|
|
359
|
+
packed.Set("family", events::families::JOYSTICK);
|
|
360
|
+
packed.Set("type", events::types::POWER_UPDATE);
|
|
361
|
+
packed.Set("joystickId", event.jbattery.which);
|
|
362
|
+
packed.Set("power", joystick::mapPowerLevel(env, event.jbattery.level));
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
case SDL_CONTROLLERSTEAMHANDLEUPDATED: {
|
|
367
|
+
packed.Set("family", events::families::CONTROLLER);
|
|
368
|
+
packed.Set("type", events::types::REMAP);
|
|
369
|
+
|
|
370
|
+
SDL_JoystickID controller_id = event.cdevice.which;
|
|
371
|
+
SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
|
|
372
|
+
if (controller == nullptr) {
|
|
373
|
+
std::ostringstream message;
|
|
374
|
+
message << "SDL_GameControllerFromInstanceID(" << controller_id << ") error: " << SDL_GetError();
|
|
375
|
+
SDL_ClearError();
|
|
376
|
+
throw Napi::Error::New(env, message.str());
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
packed.Set("controllerId", controller_id);
|
|
380
|
+
packed.Set("steamHandle", controller::getSteamHandle(env, controller));
|
|
381
|
+
break;
|
|
346
382
|
}
|
|
347
383
|
|
|
348
384
|
case SDL_CONTROLLERDEVICEREMAPPED: {
|
package/src/native/events.h
CHANGED
|
@@ -12,6 +12,7 @@ namespace events {
|
|
|
12
12
|
extern std::string DISPLAY;
|
|
13
13
|
extern std::string WINDOW;
|
|
14
14
|
extern std::string DROP;
|
|
15
|
+
extern std::string KEYMAP;
|
|
15
16
|
extern std::string KEYBOARD;
|
|
16
17
|
extern std::string TEXT;
|
|
17
18
|
extern std::string MOUSE;
|
|
@@ -54,6 +55,7 @@ namespace events {
|
|
|
54
55
|
extern std::string DROP_FILE;
|
|
55
56
|
extern std::string DROP_TEXT;
|
|
56
57
|
extern std::string CLOSE;
|
|
58
|
+
extern std::string KEYMAP_CHANGE;
|
|
57
59
|
extern std::string DEVICE_ADD;
|
|
58
60
|
extern std::string DEVICE_REMOVE;
|
|
59
61
|
extern std::string AXIS_MOTION;
|
|
@@ -61,6 +63,8 @@ namespace events {
|
|
|
61
63
|
extern std::string BUTTON_UP;
|
|
62
64
|
extern std::string BALL_MOTION;
|
|
63
65
|
extern std::string HAT_MOTION;
|
|
66
|
+
extern std::string POWER_UPDATE;
|
|
67
|
+
extern std::string STEAM_HANDLE_UPDATE;
|
|
64
68
|
extern std::string REMAP;
|
|
65
69
|
extern std::string UPDATE;
|
|
66
70
|
};
|
package/src/native/global.cpp
CHANGED
|
@@ -45,6 +45,7 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
45
45
|
events::families::DISPLAY = "display";
|
|
46
46
|
events::families::WINDOW = "window";
|
|
47
47
|
events::families::DROP = "drop";
|
|
48
|
+
events::families::KEYMAP = "keymap";
|
|
48
49
|
events::families::KEYBOARD = "keyboard";
|
|
49
50
|
events::families::TEXT = "text";
|
|
50
51
|
events::families::MOUSE = "mouse";
|
|
@@ -85,6 +86,7 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
85
86
|
events::types::DROP_FILE = "dropFile";
|
|
86
87
|
events::types::DROP_TEXT = "dropText";
|
|
87
88
|
events::types::CLOSE = "close";
|
|
89
|
+
events::types::KEYMAP_CHANGE = "keymapChange";
|
|
88
90
|
events::types::DEVICE_ADD = "deviceAdd";
|
|
89
91
|
events::types::DEVICE_REMOVE = "deviceRemove";
|
|
90
92
|
events::types::AXIS_MOTION = "axisMotion";
|
|
@@ -92,6 +94,8 @@ global::initialize(const Napi::CallbackInfo &info)
|
|
|
92
94
|
events::types::BUTTON_UP = "buttonUp";
|
|
93
95
|
events::types::BALL_MOTION = "ballMotion";
|
|
94
96
|
events::types::HAT_MOTION = "hatMotion";
|
|
97
|
+
events::types::POWER_UPDATE = "powerUpdate";
|
|
98
|
+
events::types::STEAM_HANDLE_UPDATE = "steamHandleUpdate";
|
|
95
99
|
events::types::REMAP = "remap";
|
|
96
100
|
events::types::UPDATE = "update";
|
|
97
101
|
|
package/src/native/joystick.cpp
CHANGED
|
@@ -146,6 +146,21 @@ joystick::_getDevices (Napi::Env &env)
|
|
|
146
146
|
return devices;
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
Napi::Value
|
|
150
|
+
joystick::getPowerLevel (Napi::Env &env, SDL_Joystick *joystick)
|
|
151
|
+
{
|
|
152
|
+
SDL_JoystickPowerLevel power = SDL_JoystickCurrentPowerLevel(joystick);
|
|
153
|
+
return mapPowerLevel(env, power);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
Napi::Value
|
|
157
|
+
joystick::mapPowerLevel (Napi::Env &env, SDL_JoystickPowerLevel power)
|
|
158
|
+
{
|
|
159
|
+
return power != SDL_JOYSTICK_POWER_UNKNOWN
|
|
160
|
+
? Napi::String::New(env, joystick::power_levels[power])
|
|
161
|
+
: env.Null();
|
|
162
|
+
}
|
|
163
|
+
|
|
149
164
|
|
|
150
165
|
Napi::Value
|
|
151
166
|
joystick::getDevices(const Napi::CallbackInfo &info)
|
|
@@ -267,6 +282,8 @@ joystick::open (const Napi::CallbackInfo &info)
|
|
|
267
282
|
hats.Set(i, joystick::hat_positions[hat_position]);
|
|
268
283
|
}
|
|
269
284
|
|
|
285
|
+
Napi::Value power = getPowerLevel(env, joystick);
|
|
286
|
+
|
|
270
287
|
Napi::Object result = Napi::Object::New(env);
|
|
271
288
|
result.Set("firmwareVersion", firmware_version);
|
|
272
289
|
result.Set("serialNumber", serial_number);
|
|
@@ -277,31 +294,11 @@ joystick::open (const Napi::CallbackInfo &info)
|
|
|
277
294
|
result.Set("balls", balls);
|
|
278
295
|
result.Set("buttons", buttons);
|
|
279
296
|
result.Set("hats", hats);
|
|
297
|
+
result.Set("power", power);
|
|
280
298
|
|
|
281
299
|
return result;
|
|
282
300
|
}
|
|
283
301
|
|
|
284
|
-
Napi::Value
|
|
285
|
-
joystick::getPower (const Napi::CallbackInfo &info)
|
|
286
|
-
{
|
|
287
|
-
Napi::Env env = info.Env();
|
|
288
|
-
|
|
289
|
-
int joystick_id = info[0].As<Napi::Number>().Int32Value();
|
|
290
|
-
|
|
291
|
-
SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystick_id);
|
|
292
|
-
if (joystick == nullptr) {
|
|
293
|
-
std::ostringstream message;
|
|
294
|
-
message << "SDL_JoystickFromInstanceID(" << joystick_id << ") error: " << SDL_GetError();
|
|
295
|
-
SDL_ClearError();
|
|
296
|
-
throw Napi::Error::New(env, message.str());
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
SDL_JoystickPowerLevel power_level = SDL_JoystickCurrentPowerLevel(joystick);
|
|
300
|
-
if (power_level == SDL_JOYSTICK_POWER_UNKNOWN) { return env.Null(); }
|
|
301
|
-
|
|
302
|
-
return Napi::String::New(env, joystick::power_levels[power_level]);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
302
|
Napi::Value
|
|
306
303
|
joystick::rumble (const Napi::CallbackInfo &info)
|
|
307
304
|
{
|
package/src/native/joystick.h
CHANGED
|
@@ -16,10 +16,11 @@ namespace joystick {
|
|
|
16
16
|
double mapAxis (SDL_Joystick *joystick, int axis);
|
|
17
17
|
double mapAxisValue (SDL_Joystick *joystick, int axis, int value);
|
|
18
18
|
Napi::Array _getDevices (Napi::Env &env);
|
|
19
|
+
Napi::Value getPowerLevel(Napi::Env &env, SDL_Joystick *joystick);
|
|
20
|
+
Napi::Value mapPowerLevel(Napi::Env &env, SDL_JoystickPowerLevel power);
|
|
19
21
|
|
|
20
22
|
Napi::Value getDevices(const Napi::CallbackInfo &info);
|
|
21
23
|
Napi::Value open(const Napi::CallbackInfo &info);
|
|
22
|
-
Napi::Value getPower(const Napi::CallbackInfo &info);
|
|
23
24
|
Napi::Value setLed(const Napi::CallbackInfo &info);
|
|
24
25
|
Napi::Value setPlayer(const Napi::CallbackInfo &info);
|
|
25
26
|
Napi::Value rumble(const Napi::CallbackInfo &info);
|
package/src/native/keyboard.cpp
CHANGED
|
@@ -49,7 +49,7 @@ keyboard::getState(const Napi::CallbackInfo &info)
|
|
|
49
49
|
|
|
50
50
|
Napi::Array result = Napi::Array::New(env, keyboard::num_keys);
|
|
51
51
|
|
|
52
|
-
for (int i = 0
|
|
52
|
+
for (int i = 0; i < keyboard::num_keys; i++) {
|
|
53
53
|
result.Set(i, !!keyboard::keys[i]);
|
|
54
54
|
}
|
|
55
55
|
|
package/src/native/module.cpp
CHANGED
|
@@ -62,7 +62,6 @@ init (Napi::Env env, Napi::Object exports)
|
|
|
62
62
|
|
|
63
63
|
exports.Set("joystick_getDevices", Napi::Function::New<joystick::getDevices>(env));
|
|
64
64
|
exports.Set("joystick_open", Napi::Function::New<joystick::open>(env));
|
|
65
|
-
exports.Set("joystick_getPower", Napi::Function::New<joystick::getPower>(env));
|
|
66
65
|
exports.Set("joystick_setLed", Napi::Function::New<joystick::setLed>(env));
|
|
67
66
|
exports.Set("joystick_setPlayer", Napi::Function::New<joystick::setPlayer>(env));
|
|
68
67
|
exports.Set("joystick_rumble", Napi::Function::New<joystick::rumble>(env));
|
package/src/types/index.d.ts
CHANGED
|
@@ -155,6 +155,17 @@ export namespace Events {
|
|
|
155
155
|
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
export namespace Keyboard {
|
|
159
|
+
|
|
160
|
+
interface KeyboardEvent extends BaseEvent {}
|
|
161
|
+
|
|
162
|
+
export interface KeymapChange extends KeyboardEvent { readonly type: 'keymapChange' }
|
|
163
|
+
|
|
164
|
+
export type Any
|
|
165
|
+
= KeymapChange
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
|
|
158
169
|
export namespace Joystick {
|
|
159
170
|
|
|
160
171
|
interface JoystickEvent extends BaseEvent {}
|
|
@@ -185,6 +196,11 @@ export namespace Events {
|
|
|
185
196
|
readonly value: Sdl.Joystick.HatPosition
|
|
186
197
|
}
|
|
187
198
|
|
|
199
|
+
export interface PowerUpdate extends JoystickEvent {
|
|
200
|
+
readonly type: 'powerUpdate'
|
|
201
|
+
readonly power: Sdl.Joystick.PowerLevel | null
|
|
202
|
+
}
|
|
203
|
+
|
|
188
204
|
export interface Close extends JoystickEvent { readonly type: 'close' }
|
|
189
205
|
|
|
190
206
|
export type Any
|
|
@@ -193,6 +209,7 @@ export namespace Events {
|
|
|
193
209
|
| ButtonDown
|
|
194
210
|
| ButtonUp
|
|
195
211
|
| HatMotion
|
|
212
|
+
| PowerUpdate
|
|
196
213
|
| Close
|
|
197
214
|
|
|
198
215
|
}
|
|
@@ -227,6 +244,16 @@ export namespace Events {
|
|
|
227
244
|
export interface ButtonDown extends ButtonEvent { readonly type: 'buttonDown' }
|
|
228
245
|
export interface ButtonUp extends ButtonEvent { readonly type: 'buttonUp' }
|
|
229
246
|
|
|
247
|
+
export interface PowerUpdate extends ControllerEvent {
|
|
248
|
+
readonly type: 'powerUpdate'
|
|
249
|
+
readonly power: Sdl.Joystick.PowerLevel | null
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export interface SteamHandleUpdate extends ControllerEvent {
|
|
253
|
+
readonly type: 'steamHandleUpdate'
|
|
254
|
+
readonly steamHandle: Buffer | null
|
|
255
|
+
}
|
|
256
|
+
|
|
230
257
|
export interface Remap extends ControllerEvent { readonly type: 'remap' }
|
|
231
258
|
|
|
232
259
|
export interface Close extends ControllerEvent { readonly type: 'close' }
|
|
@@ -235,6 +262,8 @@ export namespace Events {
|
|
|
235
262
|
= AxisMotion
|
|
236
263
|
| ButtonDown
|
|
237
264
|
| ButtonUp
|
|
265
|
+
| PowerUpdate
|
|
266
|
+
| SteamHandleUpdate
|
|
238
267
|
| Remap
|
|
239
268
|
| Close
|
|
240
269
|
|
|
@@ -814,6 +843,8 @@ export namespace Sdl {
|
|
|
814
843
|
interface Module {
|
|
815
844
|
readonly SCANCODE: { [name in ScancodeNames]: Scancode }
|
|
816
845
|
|
|
846
|
+
on (event: 'keymapChange', listener: (event: Events.Keyboard.KeymapChange) => void): this
|
|
847
|
+
|
|
817
848
|
getKey (scancode: Scancode): Key | null
|
|
818
849
|
getScancode (key: Key): Scancode | null
|
|
819
850
|
|
|
@@ -926,6 +957,7 @@ export namespace Sdl {
|
|
|
926
957
|
on (event: 'buttonDown', listener: (event: Events.Joystick.ButtonDown) => void): this
|
|
927
958
|
on (event: 'buttonUp', listener: (event: Events.Joystick.ButtonUp) => void): this
|
|
928
959
|
on (event: 'hatMotion', listener: (event: Events.Joystick.HatMotion) => void): this
|
|
960
|
+
on (event: 'powerUpdate', listener: (event: Events.Joystick.PowerUpdate) => void): this
|
|
929
961
|
on (event: 'close', listener: (event: Events.Joystick.Close) => void): this
|
|
930
962
|
on (event: '*', listener: (type: string, event: Events.Joystick.Any) => void): this
|
|
931
963
|
|
|
@@ -1034,6 +1066,8 @@ export namespace Sdl {
|
|
|
1034
1066
|
on (event: 'axisMotion', listener: (event: Events.Controller.AxisMotion) => void): this
|
|
1035
1067
|
on (event: 'buttonDown', listener: (event: Events.Controller.ButtonDown) => void): this
|
|
1036
1068
|
on (event: 'buttonUp', listener: (event: Events.Controller.ButtonUp) => void): this
|
|
1069
|
+
on (event: 'powerUpdate', listener: (event: Events.Controller.PowerUpdate) => void): this
|
|
1070
|
+
on (event: 'steamHandleUpdate', listener: (event: Events.Controller.SteamHandleUpdate) => void): this
|
|
1037
1071
|
on (event: 'remap', listener: (event: Events.Controller.Remap) => void): this
|
|
1038
1072
|
on (event: 'close', listener: (event: Events.Controller.Close) => void): this
|
|
1039
1073
|
on (event: '*', listener: (type: string, event: Events.Controller.Any) => void): this
|