Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: sudo apt-get update
- run: sudo apt install -y build-essential cmake libsdl2-dev
- run: npm ci
Expand All @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: sudo apt-get update
- run: sudo apt install -y build-essential cmake git
- run: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Expand All @@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- name: Install SDL2 with brew
run: brew install sdl2
- name: Compile
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: sudo apt-get update
- run: sudo apt install -y build-essential cmake libsdl2-dev
- run: npm ci
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
Expand All @@ -42,7 +42,7 @@ jobs:
# - uses: actions/checkout@v2
# - uses: actions/setup-node@v2
# with:
# node-version: 14
# node-version: 16
# registry-url: https://npm.pkg.github.com/
# - run: npm publish --registry=https://npm.pkg.github.com/
# env:
Expand Down
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ project(sdl_gamecontroller CXX)
message(STATUS "Build type ${CMAKE_BUILD_TYPE}")

# Add SDL2
find_package(SDL2 REQUIRED)
find_package(SDL2 REQUIRED
HINTS /home/linuxbrew/.linuxbrew/lib/cmake
)
if(IS_DIRECTORY "${SDL2_INCLUDE_DIRS}")
get_filename_component(SDL2_PARENT_DIR ${SDL2_INCLUDE_DIRS} DIRECTORY)
message(STATUS "Found SDL2 includes in ${SDL2_INCLUDE_DIRS} in ${SDL2_PARENT_DIR}")
Expand Down
8 changes: 7 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v1.0.10
- Add controller battery events
- Fix build issue with brew on Linux

# v1.0.9
- Add support for apple M1

# v1.0.8
- 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.

Expand All @@ -6,5 +13,4 @@
- Add support for setting the polling interval

# v1.0.6

- Add typescript support
19 changes: 18 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const gamecontroller = require('sdl2-gamecontroller/custom')(options)
- [controller-device-removed](#controller-device-removed)
- [controller-device-remapped](#controller-device-remapped)
- [controller-sensor-update](#controller-sensor-update)
- [controller-battery-update](#controller-battery-update)
- [accelerometer:enabled](#accelerometerenabled)
- [accelerometer:disabled](#accelerometerdisabled)
- [gyroscope:enabled](#gyroscopeenabled)
Expand Down Expand Up @@ -247,7 +248,6 @@ Emitted when Game controller sensor is updated
z: 1.527079463005066
}
```

An alias for this event is also emitted with the event name set to either `gyroscope` or `accelerometer`.

For example:
Expand All @@ -258,6 +258,23 @@ gamecontroller.on('gyroscope', (data) =>
);
```

## controller-battery-update

Emitted when Game controller battery is updated
[SDL_JOYBATTERYUPDATED](https://wiki.libsdl.org/SDL2/SDL_JoystickPowerLevel)

```js
// SDL 2.0.24+
{
message: 'Game controller battery was updated',
which: 0,
timestamp: 498,
level: "low"
}
```
- The `level` field will be one of "empty", "low", "medium", "full", "wired", "max", or "unknown".
- The `timestamp` in milliseconds is relative to the start of node process.

## accelerometer:enabled

Emitted when the accelerometer is successfully enabled.
Expand Down
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type DeviceUpdated = Message & {
export type DeviceUpdateEvents =
| 'controller-device-removed'
| 'controller-device-remapped';
export type BatteryUpdateEvents = 'controller-battery-update';

export type AxisType =
| 'leftx'
Expand Down Expand Up @@ -103,6 +104,20 @@ export type ButtonTypeWithUpsAndDowns =
export type ControllerButtonDown = Message &
Player & {button: ButtonType; pressed: boolean};

export type BatteryLevelType =
| 'empty'
| 'low'
| 'medium'
| 'full'
| 'wired'
| 'max'
| 'unknown';
export type BatteryUpdate = Message &
DeviceUpdated & {
timestamp: number;
level: BatteryLevelType;
};

export type CallBack<T = Record<string, unknown>> = (data: T) => void;

type ON<TEventName, TCallBack> = (
Expand All @@ -115,6 +130,7 @@ type OnWarningCall = ON<'warning', Warning>;
type OnSdlInitCall = ON<'sdl-init', SdlInit>;
type OnDeviceAddedCall = ON<'controller-device-added', DeviceAdded>;
type OnDeviceUpdated = ON<DeviceUpdateEvents, DeviceUpdated>;
type OnBatteryUpdated = ON<BatteryUpdateEvents, BatteryUpdate>;
type OnAxisUpdate = ON<AxisType, AxisMotionData>;
type OnButtonPressCall = ON<ButtonTypeWithUpsAndDowns, ButtonPress>;
type OnSensorUpdate = ON<SensorUpdateEvents, SensorUpdate>;
Expand All @@ -131,6 +147,7 @@ type AllOnOptions = OnButtonPressCall &
OnSdlInitCall &
OnDeviceAddedCall &
OnDeviceUpdated &
OnBatteryUpdated &
OnSensorUpdate &
OnSensorStateChange &
OnTouchpadUpdate &
Expand Down
41 changes: 41 additions & 0 deletions src/sdlgamecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ Napi::Value SdlGameController::pollEvents(const Napi::CallbackInfo &info) {
SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1");
}
#endif
#if SDL_VERSION_ATLEAST(2, 0, 24)
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_SHIELD, "1");
#endif
#if SDL_VERSION_ATLEAST(2, 0, 26)
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_WII_PLAYER_LED, "1");
#endif

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)
< 0) {
Expand Down Expand Up @@ -444,6 +455,36 @@ Napi::Value SdlGameController::pollEvents(const Napi::CallbackInfo &info) {
}
emit({Napi::String::New(env, "controller-sensor-update"), obj});
break;
#endif
#if SDL_VERSION_ATLEAST(2, 0, 24)
case SDL_JOYBATTERYUPDATED:
obj.Set("message", "Game controller battery was updated");
obj.Set("timestamp", event.jbattery.timestamp);
obj.Set("which", static_cast<int>(event.jbattery.which));
switch (event.jbattery.level) {
case SDL_JOYSTICK_POWER_EMPTY:
obj.Set("level", "empty");
break;
case SDL_JOYSTICK_POWER_LOW:
obj.Set("level", "low");
break;
case SDL_JOYSTICK_POWER_MEDIUM:
obj.Set("level", "medium");
break;
case SDL_JOYSTICK_POWER_FULL:
obj.Set("level", "full");
break;
case SDL_JOYSTICK_POWER_WIRED:
obj.Set("level", "wired");
break;
case SDL_JOYSTICK_POWER_MAX:
obj.Set("level", "max");
break;
default:
obj.Set("level", "unknown");
}
emit({Napi::String::New(env, "controller-battery-update"), obj});
break;
#endif
// LIMITED support for keyboard events - probably only helpful for
// testing
Expand Down
8 changes: 4 additions & 4 deletions test/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ gamecontroller.on('sdl-init', (data) => console.log('SDL2 Initialized', data));
gamecontroller.on('a:down', () => console.log('Hello A button world'));
gamecontroller.on('controller-device-added', (data) => {
console.log('controller connected', data.name);
gamecontroller.rumble("bad",{bad:true}, false, undefined);
gamecontroller.rumbleTriggers("bad",{bad:true}, false, undefined);
gamecontroller.rumble('bad', {bad: true}, false, undefined);
gamecontroller.rumbleTriggers('bad', {bad: true}, false, undefined);
gamecontroller.enableGyroscope(1, null);
gamecontroller.enableAccelerometer(0, "one");
gamecontroller.setLeds(undefined, false, "blue", "");
gamecontroller.enableAccelerometer(0, 'one');
gamecontroller.setLeds(undefined, false, 'blue', '');
process.exit(0);
});
5 changes: 5 additions & 0 deletions test/lengthy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ gamecontroller.on('controller-button-down', (data) =>
console.log('button pressed', data),
);

// Print information about the controller battery
gamecontroller.on('controller-battery-update', (data) =>
console.log('battery update', data),
);

gamecontroller.on('back', () => process.exit(0));
6 changes: 5 additions & 1 deletion test/lengthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ gamecontroller.on('controller-touchpad-motion', (data) =>
),
);

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

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

gamecontroller.on('controller-battery-update', (data) =>
console.log('battery update', data),
);

gamecontroller.on('back', () => process.exit(0));