Skip to content

Commit a769281

Browse files
authored
Merge pull request #21 from IBM/feat-battery-event
feat: add support for latest SDL2 including the battery update event
2 parents 217f2f2 + e7e6c8b commit a769281

File tree

10 files changed

+106
-14
lines changed

10 files changed

+106
-14
lines changed

.github/workflows/build-pr.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
- uses: actions/checkout@v2
1111
- uses: actions/setup-node@v2
1212
with:
13-
node-version: 14
13+
node-version: 16
1414
- run: sudo apt-get update
1515
- run: sudo apt install -y build-essential cmake libsdl2-dev
1616
- run: npm ci
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@v2
2424
- uses: actions/setup-node@v2
2525
with:
26-
node-version: 14
26+
node-version: 16
2727
- run: sudo apt-get update
2828
- run: sudo apt install -y build-essential cmake git
2929
- run: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
@@ -38,7 +38,7 @@ jobs:
3838
- uses: actions/checkout@v2
3939
- uses: actions/setup-node@v2
4040
with:
41-
node-version: 14
41+
node-version: 16
4242
- name: Install SDL2 with brew
4343
run: brew install sdl2
4444
- name: Compile

.github/workflows/npm-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/checkout@v2
1515
- uses: actions/setup-node@v2
1616
with:
17-
node-version: 14
17+
node-version: 16
1818
- run: sudo apt-get update
1919
- run: sudo apt install -y build-essential cmake libsdl2-dev
2020
- run: npm ci
@@ -26,7 +26,7 @@ jobs:
2626
- uses: actions/checkout@v2
2727
- uses: actions/setup-node@v2
2828
with:
29-
node-version: 14
29+
node-version: 16
3030
registry-url: https://registry.npmjs.org/
3131
- run: npm publish
3232
env:
@@ -42,7 +42,7 @@ jobs:
4242
# - uses: actions/checkout@v2
4343
# - uses: actions/setup-node@v2
4444
# with:
45-
# node-version: 14
45+
# node-version: 16
4646
# registry-url: https://npm.pkg.github.com/
4747
# - run: npm publish --registry=https://npm.pkg.github.com/
4848
# env:

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ project(sdl_gamecontroller CXX)
44
message(STATUS "Build type ${CMAKE_BUILD_TYPE}")
55

66
# Add SDL2
7-
find_package(SDL2 REQUIRED)
7+
find_package(SDL2 REQUIRED
8+
HINTS /home/linuxbrew/.linuxbrew/lib/cmake
9+
)
810
if(IS_DIRECTORY "${SDL2_INCLUDE_DIRS}")
911
get_filename_component(SDL2_PARENT_DIR ${SDL2_INCLUDE_DIRS} DIRECTORY)
1012
message(STATUS "Found SDL2 includes in ${SDL2_INCLUDE_DIRS} in ${SDL2_PARENT_DIR}")

RELEASENOTES.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v1.0.10
2+
- Add controller battery events
3+
- Fix build issue with brew on Linux
4+
5+
# v1.0.9
6+
- Add support for apple M1
7+
18
# v1.0.8
29
- Issue warning for mismatch params instead of throwing error. Resolve issue where a param is passed to `rumble` or another function but the param was undefined and an error is thrown. This was particularly an issue for the player number which is often not supported.
310

@@ -6,5 +13,4 @@
613
- Add support for setting the polling interval
714

815
# v1.0.6
9-
1016
- Add typescript support

docs/API.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const gamecontroller = require('sdl2-gamecontroller/custom')(options)
2424
- [controller-device-removed](#controller-device-removed)
2525
- [controller-device-remapped](#controller-device-remapped)
2626
- [controller-sensor-update](#controller-sensor-update)
27+
- [controller-battery-update](#controller-battery-update)
2728
- [accelerometer:enabled](#accelerometerenabled)
2829
- [accelerometer:disabled](#accelerometerdisabled)
2930
- [gyroscope:enabled](#gyroscopeenabled)
@@ -247,7 +248,6 @@ Emitted when Game controller sensor is updated
247248
z: 1.527079463005066
248249
}
249250
```
250-
251251
An alias for this event is also emitted with the event name set to either `gyroscope` or `accelerometer`.
252252

253253
For example:
@@ -258,6 +258,23 @@ gamecontroller.on('gyroscope', (data) =>
258258
);
259259
```
260260

261+
## controller-battery-update
262+
263+
Emitted when Game controller battery is updated
264+
[SDL_JOYBATTERYUPDATED](https://wiki.libsdl.org/SDL2/SDL_JoystickPowerLevel)
265+
266+
```js
267+
// SDL 2.0.24+
268+
{
269+
message: 'Game controller battery was updated',
270+
which: 0,
271+
timestamp: 498,
272+
level: "low"
273+
}
274+
```
275+
- The `level` field will be one of "empty", "low", "medium", "full", "wired", "max", or "unknown".
276+
- The `timestamp` in milliseconds is relative to the start of node process.
277+
261278
## accelerometer:enabled
262279

263280
Emitted when the accelerometer is successfully enabled.

index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export type DeviceUpdated = Message & {
6060
export type DeviceUpdateEvents =
6161
| 'controller-device-removed'
6262
| 'controller-device-remapped';
63+
export type BatteryUpdateEvents = 'controller-battery-update';
6364

6465
export type AxisType =
6566
| 'leftx'
@@ -103,6 +104,20 @@ export type ButtonTypeWithUpsAndDowns =
103104
export type ControllerButtonDown = Message &
104105
Player & {button: ButtonType; pressed: boolean};
105106

107+
export type BatteryLevelType =
108+
| 'empty'
109+
| 'low'
110+
| 'medium'
111+
| 'full'
112+
| 'wired'
113+
| 'max'
114+
| 'unknown';
115+
export type BatteryUpdate = Message &
116+
DeviceUpdated & {
117+
timestamp: number;
118+
level: BatteryLevelType;
119+
};
120+
106121
export type CallBack<T = Record<string, unknown>> = (data: T) => void;
107122

108123
type ON<TEventName, TCallBack> = (
@@ -115,6 +130,7 @@ type OnWarningCall = ON<'warning', Warning>;
115130
type OnSdlInitCall = ON<'sdl-init', SdlInit>;
116131
type OnDeviceAddedCall = ON<'controller-device-added', DeviceAdded>;
117132
type OnDeviceUpdated = ON<DeviceUpdateEvents, DeviceUpdated>;
133+
type OnBatteryUpdated = ON<BatteryUpdateEvents, BatteryUpdate>;
118134
type OnAxisUpdate = ON<AxisType, AxisMotionData>;
119135
type OnButtonPressCall = ON<ButtonTypeWithUpsAndDowns, ButtonPress>;
120136
type OnSensorUpdate = ON<SensorUpdateEvents, SensorUpdate>;
@@ -131,6 +147,7 @@ type AllOnOptions = OnButtonPressCall &
131147
OnSdlInitCall &
132148
OnDeviceAddedCall &
133149
OnDeviceUpdated &
150+
OnBatteryUpdated &
134151
OnSensorUpdate &
135152
OnSensorStateChange &
136153
OnTouchpadUpdate &

src/sdlgamecontroller.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ Napi::Value SdlGameController::pollEvents(const Napi::CallbackInfo &info) {
230230
SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1");
231231
}
232232
#endif
233+
#if SDL_VERSION_ATLEAST(2, 0, 24)
234+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_SHIELD, "1");
235+
#endif
236+
#if SDL_VERSION_ATLEAST(2, 0, 26)
237+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, "1");
238+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED, "1");
239+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS, "1");
240+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE, "1");
241+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED, "1");
242+
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_WII_PLAYER_LED, "1");
243+
#endif
233244

234245
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)
235246
< 0) {
@@ -444,6 +455,36 @@ Napi::Value SdlGameController::pollEvents(const Napi::CallbackInfo &info) {
444455
}
445456
emit({Napi::String::New(env, "controller-sensor-update"), obj});
446457
break;
458+
#endif
459+
#if SDL_VERSION_ATLEAST(2, 0, 24)
460+
case SDL_JOYBATTERYUPDATED:
461+
obj.Set("message", "Game controller battery was updated");
462+
obj.Set("timestamp", event.jbattery.timestamp);
463+
obj.Set("which", static_cast<int>(event.jbattery.which));
464+
switch (event.jbattery.level) {
465+
case SDL_JOYSTICK_POWER_EMPTY:
466+
obj.Set("level", "empty");
467+
break;
468+
case SDL_JOYSTICK_POWER_LOW:
469+
obj.Set("level", "low");
470+
break;
471+
case SDL_JOYSTICK_POWER_MEDIUM:
472+
obj.Set("level", "medium");
473+
break;
474+
case SDL_JOYSTICK_POWER_FULL:
475+
obj.Set("level", "full");
476+
break;
477+
case SDL_JOYSTICK_POWER_WIRED:
478+
obj.Set("level", "wired");
479+
break;
480+
case SDL_JOYSTICK_POWER_MAX:
481+
obj.Set("level", "max");
482+
break;
483+
default:
484+
obj.Set("level", "unknown");
485+
}
486+
emit({Napi::String::New(env, "controller-battery-update"), obj});
487+
break;
447488
#endif
448489
// LIMITED support for keyboard events - probably only helpful for
449490
// testing

test/arguments.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ gamecontroller.on('sdl-init', (data) => console.log('SDL2 Initialized', data));
66
gamecontroller.on('a:down', () => console.log('Hello A button world'));
77
gamecontroller.on('controller-device-added', (data) => {
88
console.log('controller connected', data.name);
9-
gamecontroller.rumble("bad",{bad:true}, false, undefined);
10-
gamecontroller.rumbleTriggers("bad",{bad:true}, false, undefined);
9+
gamecontroller.rumble('bad', {bad: true}, false, undefined);
10+
gamecontroller.rumbleTriggers('bad', {bad: true}, false, undefined);
1111
gamecontroller.enableGyroscope(1, null);
12-
gamecontroller.enableAccelerometer(0, "one");
13-
gamecontroller.setLeds(undefined, false, "blue", "");
12+
gamecontroller.enableAccelerometer(0, 'one');
13+
gamecontroller.setLeds(undefined, false, 'blue', '');
1414
process.exit(0);
1515
});

test/lengthy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ gamecontroller.on('controller-button-down', (data) =>
102102
console.log('button pressed', data),
103103
);
104104

105+
// Print information about the controller battery
106+
gamecontroller.on('controller-battery-update', (data) =>
107+
console.log('battery update', data),
108+
);
109+
105110
gamecontroller.on('back', () => process.exit(0));

test/lengthy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ gamecontroller.on('controller-touchpad-motion', (data) =>
9696
),
9797
);
9898

99-
gamecontroller.on('led', (data) => console.log('LEDS set', data));
99+
gamecontroller.on('led', (data) => console.log('LEDs set', data));
100100

101101
// Respond to both up & down events
102102
gamecontroller.on('leftshoulder', (data) =>
@@ -110,4 +110,8 @@ gamecontroller.on('controller-button-down', (data) =>
110110
console.log('button pressed', data),
111111
);
112112

113+
gamecontroller.on('controller-battery-update', (data) =>
114+
console.log('battery update', data),
115+
);
116+
113117
gamecontroller.on('back', () => process.exit(0));

0 commit comments

Comments
 (0)