Skip to content
Open
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
40 changes: 35 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
name: Build and upload

on: workflow_dispatch
on:
workflow_dispatch:
push:
tags:
- "v*"

permissions:
contents: write

jobs:
empty:
runs-on: ubuntu-latest
build:
name: ${{ matrix.platform.name }}
strategy:
fail-fast: false
matrix:
platform:
- { name: 'Linux (x64)', os: ubuntu-latest }
- { name: 'Linux (arm64)', os: ubuntu-24.04-arm }
- { name: 'Windows (x64)', os: windows-latest }
- { name: 'Mac (x64)', os: macos-15-intel }
- { name: 'Mac (arm64)', os: macos-15 }
runs-on: ${{ matrix.platform.os }}
steps:
- run: 'echo "empty"'
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20

- if: ${{ startsWith(matrix.platform.os, 'macos-') }}
run: brew update && ./scripts/install-deps-mac.sh

- if: ${{ startsWith(matrix.platform.os, 'ubuntu-') }}
run: sudo apt-get update && sudo ./scripts/install-deps-ubuntu.sh

# This file is empty on the master branch. Releases are made from the 'workflow' branch.
- env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CROSS_COMPILE_ARCH: ${{ matrix.platform.arch }}
run: npm run release
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.11.13",
"version": "0.11.13-rawaxis.1",
"name": "@kmamal/sdl",
"description": "SDL bindings for Node.js",
"keywords": [
Expand Down Expand Up @@ -38,7 +38,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/kmamal/node-sdl.git"
"url": "git+ssh://git@github.com/rafaellehmkuhl/node-sdl.git"
},
"main": "./src/javascript/index.js",
"types": "./src/types/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/javascript/controller/controller-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const validEvents = [
]

class ControllerInstance extends EventsViaPoll {
constructor (device) {
constructor (device, options = {}) {
super(validEvents)

if (!Globals.controllerDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }

const result = Bindings.controller_open(device._index)
const { rawAxisMode = false } = options
const result = Bindings.controller_open(device._index, rawAxisMode)

this._firmwareVersion = result.firmwareVersion
this._serialNumber = result.serialNumber
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const controller = new class extends EventsViaPoll {
return Globals.controllerDevices
}

openDevice (device) { return new ControllerInstance(device) }
openDevice (device, options = {}) { return new ControllerInstance(device, options) }

addMappings (mappings) {
if (!Array.isArray(mappings)) { throw Object.assign(new Error("mappings must be an array"), { mappings }) }
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/joystick/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const joystick = new class extends EventsViaPoll {
return Globals.joystickDevices
}

openDevice (device) { return new JoystickInstance(device) }
openDevice (device, options = {}) { return new JoystickInstance(device, options) }
}()

module.exports = { joystick }
5 changes: 3 additions & 2 deletions src/javascript/joystick/joystick-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const validEvents = [
]

class JoystickInstance extends EventsViaPoll {
constructor (device) {
constructor (device, options = {}) {
super(validEvents)

if (!Globals.joystickDevices.includes(device)) { throw Object.assign(new Error("invalid device"), { device }) }

const result = Bindings.joystick_open(device._index)
const { rawAxisMode = false } = options
const result = Bindings.joystick_open(device._index, rawAxisMode)

this._firmwareVersion = result.firmwareVersion
this._serialNumber = result.serialNumber
Expand Down
12 changes: 11 additions & 1 deletion src/native/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ controller::open (const Napi::CallbackInfo &info)
Napi::Env env = info.Env();

int index = info[0].As<Napi::Number>().Int32Value();
bool rawAxisMode = info.Length() > 1 && info[1].As<Napi::Boolean>().Value();

SDL_GameController *controller = SDL_GameControllerOpen(index);
if (controller == nullptr) {
Expand All @@ -133,6 +134,12 @@ controller::open (const Napi::CallbackInfo &info)
throw Napi::Error::New(env, message.str());
}

SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
int joystick_id = SDL_JoystickInstanceID(joystick);
if (rawAxisMode) {
joystick::rawAxisModeDevices.insert(joystick_id);
}

// SDL_GameControllerOpen produces errors even though it succeeds
const char *error = SDL_GetError();
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
Expand All @@ -154,7 +161,6 @@ controller::open (const Napi::CallbackInfo &info)

Napi::Value steam_handle = getSteamHandle(env, controller);

SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
Napi::Value power = joystick::getPowerLevel(env, joystick);

Napi::Object result = Napi::Object::New(env);
Expand Down Expand Up @@ -186,6 +192,10 @@ controller::close (const Napi::CallbackInfo &info)
throw Napi::Error::New(env, message.str());
}

SDL_Joystick *joystick = SDL_GameControllerGetJoystick(controller);
int joystick_id = SDL_JoystickInstanceID(joystick);
joystick::rawAxisModeDevices.erase(joystick_id);

SDL_GameControllerClose(controller);

return env.Undefined();
Expand Down
29 changes: 23 additions & 6 deletions src/native/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#include <string>
#include <sstream>
#include <map>
#include <set>


std::map<Uint8, std::string> joystick::hat_positions;
std::map<SDL_JoystickType, std::string> joystick::types;
std::map<SDL_JoystickPowerLevel, std::string> joystick::power_levels;
SDL_JoystickGUID joystick::zero_guid;
std::set<int> joystick::rawAxisModeDevices;

double
joystick::mapAxis (SDL_Joystick *joystick, int axis) {
Expand All @@ -19,12 +21,20 @@ joystick::mapAxis (SDL_Joystick *joystick, int axis) {

double
joystick::mapAxisValue (SDL_Joystick *joystick, int axis, int value) {
Sint16 initial;
if (!SDL_JoystickGetAxisInitialState(joystick, axis, &initial)) { initial = 0; }
double range = value < initial
? initial - SDL_JOYSTICK_AXIS_MIN
: SDL_JOYSTICK_AXIS_MAX - initial;
return (value - initial) / range;
int joystick_id = SDL_JoystickInstanceID(joystick);
bool rawMode = rawAxisModeDevices.count(joystick_id) > 0;

if (!rawMode) {
Sint16 initial;
if (!SDL_JoystickGetAxisInitialState(joystick, axis, &initial)) { initial = 0; }
double range = value < initial
? initial - SDL_JOYSTICK_AXIS_MIN
: SDL_JOYSTICK_AXIS_MAX - initial;
return (value - initial) / range;
}

double range = value < 0 ? -SDL_JOYSTICK_AXIS_MIN : SDL_JOYSTICK_AXIS_MAX;
return value / range;
}

Napi::Array
Expand Down Expand Up @@ -176,6 +186,7 @@ joystick::open (const Napi::CallbackInfo &info)
Napi::Env env = info.Env();

int index = info[0].As<Napi::Number>().Int32Value();
bool rawAxisMode = info.Length() > 1 && info[1].As<Napi::Boolean>().Value();

SDL_Joystick *joystick = SDL_JoystickOpen(index);
if (joystick == nullptr) {
Expand All @@ -185,6 +196,11 @@ joystick::open (const Napi::CallbackInfo &info)
throw Napi::Error::New(env, message.str());
}

int joystick_id = SDL_JoystickInstanceID(joystick);
if (rawAxisMode) {
rawAxisModeDevices.insert(joystick_id);
}

// SDL_JoystickOpen produces errors even though it succeeds
const char *error = SDL_GetError();
if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
Expand Down Expand Up @@ -419,6 +435,7 @@ joystick::close (const Napi::CallbackInfo &info)
throw Napi::Error::New(env, message.str());
}

rawAxisModeDevices.erase(joystick_id);
SDL_JoystickClose(joystick);

return env.Undefined();
Expand Down
2 changes: 2 additions & 0 deletions src/native/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <napi.h>
#include <SDL.h>
#include <map>
#include <set>
#include <string>

namespace joystick {
Expand All @@ -12,6 +13,7 @@ namespace joystick {
extern std::map<Uint8, std::string> hat_positions;
extern std::map<SDL_JoystickPowerLevel, std::string> power_levels;
extern SDL_JoystickGUID zero_guid;
extern std::set<int> rawAxisModeDevices;

double mapAxis (SDL_Joystick *joystick, int axis);
double mapAxisValue (SDL_Joystick *joystick, int axis, int value);
Expand Down
22 changes: 20 additions & 2 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,15 @@ export namespace Sdl {

export namespace Joystick {

export interface OpenOptions {
/**
* When true, axis values are normalized without SDL's initial state calibration.
* This avoids incorrect zero-point calibration when joysticks aren't centered at startup.
* @default false
*/
rawAxisMode?: boolean
}

export interface BallPosition {
readonly x: number
readonly y: number
Expand Down Expand Up @@ -1050,13 +1059,22 @@ export namespace Sdl {

readonly devices: Device[]

openDevice (device: Device): JoystickInstance
openDevice (device: Device, options?: OpenOptions): JoystickInstance
}

}

export namespace Controller {

export interface OpenOptions {
/**
* When true, axis values are normalized without SDL's initial state calibration.
* This avoids incorrect zero-point calibration when controllers aren't centered at startup.
* @default false
*/
rawAxisMode?: boolean
}

export type ControllerType
= null
| 'xbox360'
Expand Down Expand Up @@ -1190,7 +1208,7 @@ export namespace Sdl {

readonly devices: Device[]

openDevice (device: Device): ControllerInstance
openDevice (device: Device, options?: OpenOptions): ControllerInstance
}

}
Expand Down