From 1a2a3370d03ebcda9b9a5c6b330b790327e39d41 Mon Sep 17 00:00:00 2001 From: snippet <874740+musshorn@users.noreply.github.com> Date: Sat, 27 Sep 2025 13:50:31 +1000 Subject: [PATCH 1/3] Expose more battery and power connection state --- changes.txt | 2 + src/modules/joystick/Joystick.cpp | 20 +++++++ src/modules/joystick/Joystick.h | 24 ++++++++ src/modules/joystick/sdl/Joystick.cpp | 76 ++++++++++++++++++++++++++ src/modules/joystick/sdl/Joystick.h | 13 +++++ src/modules/joystick/wrap_Joystick.cpp | 40 ++++++++++++++ 6 files changed, 175 insertions(+) diff --git a/changes.txt b/changes.txt index 5d20dc20b..83de36da2 100644 --- a/changes.txt +++ b/changes.txt @@ -29,6 +29,8 @@ Released: N/A * Added Joystick:hasSensor. * Added Joystick:isSensorEnabled and Joystick:setSensorEnabled. * Added Joystick:getSensorData. +* Added Joystick:getDeviceBatteryPercent and Joystick:getDevicePowerState, these expose how charged batteries in a controller are and if they're charging +* Added Joystick:getConnectionState which indicates if a joystick is connected wired or wireless * Added new Gamepad API buttons: "misc1", "paddle1", "paddle2", "paddle3", "paddle4". and "touchpad". * Added World:getFixturesInArea(). * Added support for saving .exr image files via ImageData:encode. diff --git a/src/modules/joystick/Joystick.cpp b/src/modules/joystick/Joystick.cpp index 526d15d65..d486c5ae7 100644 --- a/src/modules/joystick/Joystick.cpp +++ b/src/modules/joystick/Joystick.cpp @@ -135,5 +135,25 @@ STRINGMAP_CLASS_BEGIN(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_EN } STRINGMAP_CLASS_END(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM, inputType) +STRINGMAP_CLASS_BEGIN(Joystick, Joystick::PowerState, Joystick::POWERSTATE_MAX_ENUM, powerState) +{ + { "error", Joystick::POWERSTATE_ERROR }, + { "unknown", Joystick::POWERSTATE_UNKNOWN }, + { "onbattery", Joystick::POWERSTATE_ON_BATTERY }, + { "nobattery", Joystick::POWERSTATE_NO_BATTERY }, + { "charging", Joystick::POWERSTATE_CHARGING }, + { "charged", Joystick::POWERSTATE_CHARGED }, +} +STRINGMAP_CLASS_END(Joystick, Joystick::PowerState, Joystick::POWERSTATE_MAX_ENUM, powerState) + +STRINGMAP_CLASS_BEGIN(Joystick, Joystick::ConnectionState, Joystick::CONNECTION_MAX_ENUM, connectionState) +{ + { "error", Joystick::CONNECTION_INVALID }, + { "unknown", Joystick::CONNECTION_UNKNOWN }, + { "wired", Joystick::CONNECTION_WIRED }, + { "wireless", Joystick::CONNECTION_WIRELESS }, +} +STRINGMAP_CLASS_END(Joystick, Joystick::ConnectionState, Joystick::CONNECTION_MAX_ENUM, connectionState) + } // joystick } // love diff --git a/src/modules/joystick/Joystick.h b/src/modules/joystick/Joystick.h index 9bfa62aa2..8aebd4e99 100644 --- a/src/modules/joystick/Joystick.h +++ b/src/modules/joystick/Joystick.h @@ -144,6 +144,26 @@ class Joystick : public Object INPUT_TYPE_MAX_ENUM }; + enum PowerState + { + POWERSTATE_ERROR = -1, /**< error determining power status */ + POWERSTATE_UNKNOWN, /**< cannot determine power status */ + POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */ + POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */ + POWERSTATE_CHARGING, /**< Plugged in, charging battery */ + POWERSTATE_CHARGED, /**< Plugged in, battery charged */ + POWERSTATE_MAX_ENUM + }; + + enum ConnectionState + { + CONNECTION_INVALID = -1, + CONNECTION_UNKNOWN, + CONNECTION_WIRED, + CONNECTION_WIRELESS, + CONNECTION_MAX_ENUM + }; + // Represents a gamepad input value, e.g. the "x" button or the left trigger. struct GamepadInput { @@ -223,6 +243,8 @@ class Joystick : public Object virtual bool isSensorEnabled(Sensor::SensorType type) const = 0; virtual void setSensorEnabled(Sensor::SensorType type, bool enabled) = 0; virtual std::vector getSensorData(Sensor::SensorType type) const = 0; + virtual PowerState getPowerInfo(int &batteryPercent) const = 0; + virtual ConnectionState getConnectionState() const = 0; STRINGMAP_CLASS_DECLARE(Hat); STRINGMAP_CLASS_DECLARE(JoystickType); @@ -230,6 +252,8 @@ class Joystick : public Object STRINGMAP_CLASS_DECLARE(GamepadAxis); STRINGMAP_CLASS_DECLARE(GamepadButton); STRINGMAP_CLASS_DECLARE(InputType); + STRINGMAP_CLASS_DECLARE(PowerState); + STRINGMAP_CLASS_DECLARE(ConnectionState); static float clampval(float x); diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index ae0168de4..a31047c02 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -462,6 +462,40 @@ void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion) } } +Joystick::PowerState Joystick::getPowerInfo(int& batteryPercent) const +{ + // Gets the battery state of a joystick/gamepad + Joystick::PowerState powerState; + SDL_PowerState batteryState; + if (!isGamepad()) + { + batteryState = SDL_GetJoystickPowerInfo(joyhandle, &batteryPercent); + } + else + { + batteryState = SDL_GetGamepadPowerInfo(controller, &batteryPercent); + } + getConstant(batteryState, powerState); + return powerState; +} + +Joystick::ConnectionState Joystick::getConnectionState() const +{ + // Gets the connection state of a joystick/gamepad (wired / wireless etc) + Joystick::ConnectionState connectionState; + SDL_JoystickConnectionState sdlConnectionState; + if (!isGamepad()) + { + sdlConnectionState = SDL_GetJoystickConnectionState(joyhandle); + } + else + { + sdlConnectionState = SDL_GetGamepadConnectionState(controller); + } + getConstant(sdlConnectionState, connectionState); + return connectionState; +} + bool Joystick::isVibrationSupported() { if (!isConnected()) @@ -614,6 +648,26 @@ bool Joystick::getConstant(Joystick::GamepadButton in, SDL_GamepadButton &out) return gpButtons.find(in, out); } +bool Joystick::getConstant(SDL_PowerState in, Joystick::PowerState &out) +{ + return powerStates.find(in, out); +} + +bool Joystick::getConstant(Joystick::PowerState in, SDL_PowerState &out) +{ + return powerStates.find(in, out); +} + +bool Joystick::getConstant(SDL_JoystickConnectionState in, Joystick::ConnectionState &out) +{ + return connectionStates.find(in, out); +} + +bool Joystick::getConstant(Joystick::ConnectionState in, SDL_JoystickConnectionState &out) +{ + return connectionStates.find(in, out); +} + EnumMap::Entry Joystick::hatEntries[] = { {Joystick::HAT_CENTERED, SDL_HAT_CENTERED}, @@ -668,6 +722,28 @@ EnumMap Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries)); +EnumMap::Entry Joystick::powerStateEntries[] = +{ + {Joystick::POWERSTATE_ERROR, SDL_POWERSTATE_ERROR}, + {Joystick::POWERSTATE_UNKNOWN, SDL_POWERSTATE_UNKNOWN}, + {Joystick::POWERSTATE_ON_BATTERY, SDL_POWERSTATE_ON_BATTERY}, + {Joystick::POWERSTATE_NO_BATTERY, SDL_POWERSTATE_NO_BATTERY}, + {Joystick::POWERSTATE_CHARGING, SDL_POWERSTATE_CHARGING}, + {Joystick::POWERSTATE_CHARGED, SDL_POWERSTATE_CHARGED}, +}; + +EnumMap Joystick::powerStates(Joystick::powerStateEntries, sizeof(Joystick::powerStateEntries)); + +EnumMap::Entry Joystick::connectionStateEntries[] = +{ + {Joystick::CONNECTION_INVALID, SDL_JOYSTICK_CONNECTION_INVALID }, + {Joystick::CONNECTION_UNKNOWN, SDL_JOYSTICK_CONNECTION_UNKNOWN}, + {Joystick::CONNECTION_WIRED, SDL_JOYSTICK_CONNECTION_WIRED}, + {Joystick::CONNECTION_WIRELESS, SDL_JOYSTICK_CONNECTION_WIRELESS}, +}; + +EnumMap Joystick::connectionStates(Joystick::connectionStateEntries, sizeof(Joystick::connectionStateEntries)); + } // sdl } // joystick } // love diff --git a/src/modules/joystick/sdl/Joystick.h b/src/modules/joystick/sdl/Joystick.h index 99b92a764..3709b66c3 100644 --- a/src/modules/joystick/sdl/Joystick.h +++ b/src/modules/joystick/sdl/Joystick.h @@ -84,6 +84,8 @@ class Joystick : public love::joystick::Joystick int getID() const override; void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const override; + PowerState getPowerInfo(int& batteryPercent) const override; + ConnectionState getConnectionState() const override; bool isVibrationSupported() override; bool setVibration(float left, float right, float duration = -1.0f) override; @@ -104,6 +106,12 @@ class Joystick : public love::joystick::Joystick static bool getConstant(SDL_GamepadButton in, GamepadButton &out); static bool getConstant(GamepadButton in, SDL_GamepadButton &out); + static bool getConstant(SDL_PowerState in, PowerState &out); + static bool getConstant(PowerState in, SDL_PowerState &out); + + static bool getConstant(SDL_JoystickConnectionState in, ConnectionState &out); + static bool getConstant(ConnectionState in, SDL_JoystickConnectionState &out); + private: Joystick() {} @@ -129,6 +137,11 @@ class Joystick : public love::joystick::Joystick static EnumMap::Entry gpButtonEntries[]; static EnumMap gpButtons; + static EnumMap::Entry powerStateEntries[]; + static EnumMap powerStates; + + static EnumMap::Entry connectionStateEntries[]; + static EnumMap connectionStates; }; } // sdl diff --git a/src/modules/joystick/wrap_Joystick.cpp b/src/modules/joystick/wrap_Joystick.cpp index 5c49bf1f4..50b425179 100644 --- a/src/modules/joystick/wrap_Joystick.cpp +++ b/src/modules/joystick/wrap_Joystick.cpp @@ -447,6 +447,43 @@ int w_Joystick_getSensorData(lua_State *L) return (int) data.size(); } +int w_Joystick_getDeviceBatteryPercent(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + + int batteryPercent = 0; + j->getPowerInfo(batteryPercent); + + lua_pushnumber(L, batteryPercent); + + return 1; +} + +int w_Joystick_getDevicePowerState(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + + int batteryPercent = 0; + const char *str = "unknown"; + Joystick::getConstant(j->getPowerInfo(batteryPercent), str); + + lua_pushstring(L, str); + + return 1; +} + +int w_Joystick_getDeviceConnectionState(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + + const char *str = "unknown"; + Joystick::getConstant(j->getConnectionState(), str); + + lua_pushstring(L, str); + + return 1; +} + #endif // LOVE_ENABLE_SENSOR // List of functions to wrap. @@ -457,6 +494,9 @@ static const luaL_Reg w_Joystick_functions[] = { "getID", w_Joystick_getID }, { "getGUID", w_Joystick_getGUID }, { "getDeviceInfo", w_Joystick_getDeviceInfo }, + { "getDeviceBatteryPercent", w_Joystick_getDeviceBatteryPercent }, + { "getDevicePowerState", w_Joystick_getDevicePowerState }, + { "getDeviceConnectionState", w_Joystick_getDeviceConnectionState }, { "getJoystickType", w_Joystick_getJoystickType }, { "getAxisCount", w_Joystick_getAxisCount }, { "getButtonCount", w_Joystick_getButtonCount }, From 7b64f24a316c68f52a04ffc31fec472f654c0b68 Mon Sep 17 00:00:00 2001 From: snippet <874740+musshorn@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:40:11 +1000 Subject: [PATCH 2/3] Expose more battery and power connection state --- changes.txt | 2 + src/modules/joystick/Joystick.cpp | 18 ++++++ src/modules/joystick/Joystick.h | 22 +++++++ src/modules/joystick/sdl/Joystick.cpp | 69 +++++++++++++++++++++ src/modules/joystick/sdl/Joystick.h | 13 ++++ src/modules/joystick/sdl/JoystickModule.cpp | 6 +- src/modules/joystick/wrap_Joystick.cpp | 35 +++++++++++ 7 files changed, 164 insertions(+), 1 deletion(-) diff --git a/changes.txt b/changes.txt index 5d20dc20b..83de36da2 100644 --- a/changes.txt +++ b/changes.txt @@ -29,6 +29,8 @@ Released: N/A * Added Joystick:hasSensor. * Added Joystick:isSensorEnabled and Joystick:setSensorEnabled. * Added Joystick:getSensorData. +* Added Joystick:getDeviceBatteryPercent and Joystick:getDevicePowerState, these expose how charged batteries in a controller are and if they're charging +* Added Joystick:getConnectionState which indicates if a joystick is connected wired or wireless * Added new Gamepad API buttons: "misc1", "paddle1", "paddle2", "paddle3", "paddle4". and "touchpad". * Added World:getFixturesInArea(). * Added support for saving .exr image files via ImageData:encode. diff --git a/src/modules/joystick/Joystick.cpp b/src/modules/joystick/Joystick.cpp index 526d15d65..dfd95880d 100644 --- a/src/modules/joystick/Joystick.cpp +++ b/src/modules/joystick/Joystick.cpp @@ -135,5 +135,23 @@ STRINGMAP_CLASS_BEGIN(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_EN } STRINGMAP_CLASS_END(Joystick, Joystick::InputType, Joystick::INPUT_TYPE_MAX_ENUM, inputType) +STRINGMAP_CLASS_BEGIN(Joystick, Joystick::PowerType, Joystick::POWER_MAX_ENUM, powerState) +{ + { "unknown", Joystick::POWER_UNKNOWN }, + { "onbattery", Joystick::POWER_ON_BATTERY }, + { "nobattery", Joystick::POWER_NO_BATTERY }, + { "charging", Joystick::POWER_CHARGING }, + { "charged", Joystick::POWER_CHARGED }, +} +STRINGMAP_CLASS_END(Joystick, Joystick::PowerType, Joystick::POWER_MAX_ENUM, powerState) + +STRINGMAP_CLASS_BEGIN(Joystick, Joystick::ConnectionType, Joystick::CONNECTION_MAX_ENUM, connectionState) +{ + { "unknown", Joystick::CONNECTION_UNKNOWN }, + { "wired", Joystick::CONNECTION_WIRED }, + { "wireless", Joystick::CONNECTION_WIRELESS }, +} +STRINGMAP_CLASS_END(Joystick, Joystick::ConnectionType, Joystick::CONNECTION_MAX_ENUM, connectionState) + } // joystick } // love diff --git a/src/modules/joystick/Joystick.h b/src/modules/joystick/Joystick.h index 9bfa62aa2..300613b8c 100644 --- a/src/modules/joystick/Joystick.h +++ b/src/modules/joystick/Joystick.h @@ -144,6 +144,24 @@ class Joystick : public Object INPUT_TYPE_MAX_ENUM }; + enum PowerType + { + POWER_UNKNOWN, /**< cannot determine power status */ + POWER_ON_BATTERY, /**< Not plugged in, running on the battery */ + POWER_NO_BATTERY, /**< Plugged in, no battery available */ + POWER_CHARGING, /**< Plugged in, charging battery */ + POWER_CHARGED, /**< Plugged in, battery charged */ + POWER_MAX_ENUM + }; + + enum ConnectionType + { + CONNECTION_UNKNOWN, + CONNECTION_WIRED, + CONNECTION_WIRELESS, + CONNECTION_MAX_ENUM + }; + // Represents a gamepad input value, e.g. the "x" button or the left trigger. struct GamepadInput { @@ -223,6 +241,8 @@ class Joystick : public Object virtual bool isSensorEnabled(Sensor::SensorType type) const = 0; virtual void setSensorEnabled(Sensor::SensorType type, bool enabled) = 0; virtual std::vector getSensorData(Sensor::SensorType type) const = 0; + virtual PowerType getPowerInfo(int &batteryPercent) const = 0; + virtual ConnectionType getConnectionState() const = 0; STRINGMAP_CLASS_DECLARE(Hat); STRINGMAP_CLASS_DECLARE(JoystickType); @@ -230,6 +250,8 @@ class Joystick : public Object STRINGMAP_CLASS_DECLARE(GamepadAxis); STRINGMAP_CLASS_DECLARE(GamepadButton); STRINGMAP_CLASS_DECLARE(InputType); + STRINGMAP_CLASS_DECLARE(PowerType); + STRINGMAP_CLASS_DECLARE(ConnectionType); static float clampval(float x); diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index ae0168de4..9c3c25013 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -158,6 +158,11 @@ bool Joystick::open(int64 deviceid) else gamepadType = GAMEPAD_TYPE_UNKNOWN; } + else + { + auto err = SDL_GetError(); + auto t = 1; + } return isConnected(); } @@ -462,6 +467,30 @@ void Joystick::getDeviceInfo(int &vendorID, int &productID, int &productVersion) } } +Joystick::PowerType Joystick::getPowerInfo(int& batteryPercent) const +{ + // Gets the battery state of a joystick/gamepad + Joystick::PowerType powerState; + SDL_PowerState batteryState; + + batteryState = SDL_GetJoystickPowerInfo(joyhandle, &batteryPercent); + + getConstant(batteryState, powerState); + return powerState; +} + +Joystick::ConnectionType Joystick::getConnectionState() const +{ + // Gets the connection state of a joystick/gamepad (wired / wireless etc) + Joystick::ConnectionType connectionState; + SDL_JoystickConnectionState sdlConnectionState; + + sdlConnectionState = SDL_GetJoystickConnectionState(joyhandle); + + getConstant(sdlConnectionState, connectionState); + return connectionState; +} + bool Joystick::isVibrationSupported() { if (!isConnected()) @@ -614,6 +643,26 @@ bool Joystick::getConstant(Joystick::GamepadButton in, SDL_GamepadButton &out) return gpButtons.find(in, out); } +bool Joystick::getConstant(SDL_PowerState in, Joystick::PowerType &out) +{ + return powerStates.find(in, out); +} + +bool Joystick::getConstant(Joystick::PowerType in, SDL_PowerState &out) +{ + return powerStates.find(in, out); +} + +bool Joystick::getConstant(SDL_JoystickConnectionState in, Joystick::ConnectionType &out) +{ + return connectionStates.find(in, out); +} + +bool Joystick::getConstant(Joystick::ConnectionType in, SDL_JoystickConnectionState &out) +{ + return connectionStates.find(in, out); +} + EnumMap::Entry Joystick::hatEntries[] = { {Joystick::HAT_CENTERED, SDL_HAT_CENTERED}, @@ -668,6 +717,26 @@ EnumMap Joystick::gpButtons(Joystick::gpButtonEntries, sizeof(Joystick::gpButtonEntries)); +EnumMap::Entry Joystick::powerEntries[] = +{ + {Joystick::POWER_UNKNOWN, SDL_POWERSTATE_UNKNOWN}, + {Joystick::POWER_ON_BATTERY, SDL_POWERSTATE_ON_BATTERY}, + {Joystick::POWER_NO_BATTERY, SDL_POWERSTATE_NO_BATTERY}, + {Joystick::POWER_CHARGING, SDL_POWERSTATE_CHARGING}, + {Joystick::POWER_CHARGED, SDL_POWERSTATE_CHARGED}, +}; + +EnumMap Joystick::powerStates(Joystick::powerEntries, sizeof(Joystick::powerEntries)); + +EnumMap::Entry Joystick::connectionStateEntries[] = +{ + {Joystick::CONNECTION_UNKNOWN, SDL_JOYSTICK_CONNECTION_UNKNOWN}, + {Joystick::CONNECTION_WIRED, SDL_JOYSTICK_CONNECTION_WIRED}, + {Joystick::CONNECTION_WIRELESS, SDL_JOYSTICK_CONNECTION_WIRELESS}, +}; + +EnumMap Joystick::connectionStates(Joystick::connectionStateEntries, sizeof(Joystick::connectionStateEntries)); + } // sdl } // joystick } // love diff --git a/src/modules/joystick/sdl/Joystick.h b/src/modules/joystick/sdl/Joystick.h index 99b92a764..13ca2177d 100644 --- a/src/modules/joystick/sdl/Joystick.h +++ b/src/modules/joystick/sdl/Joystick.h @@ -84,6 +84,8 @@ class Joystick : public love::joystick::Joystick int getID() const override; void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const override; + PowerType getPowerInfo(int& batteryPercent) const override; + ConnectionType getConnectionState() const override; bool isVibrationSupported() override; bool setVibration(float left, float right, float duration = -1.0f) override; @@ -104,6 +106,12 @@ class Joystick : public love::joystick::Joystick static bool getConstant(SDL_GamepadButton in, GamepadButton &out); static bool getConstant(GamepadButton in, SDL_GamepadButton &out); + static bool getConstant(SDL_PowerState in, PowerType &out); + static bool getConstant(PowerType in, SDL_PowerState &out); + + static bool getConstant(SDL_JoystickConnectionState in, ConnectionType &out); + static bool getConstant(ConnectionType in, SDL_JoystickConnectionState &out); + private: Joystick() {} @@ -129,6 +137,11 @@ class Joystick : public love::joystick::Joystick static EnumMap::Entry gpButtonEntries[]; static EnumMap gpButtons; + static EnumMap::Entry powerEntries[]; + static EnumMap powerStates; + + static EnumMap::Entry connectionStateEntries[]; + static EnumMap connectionStates; }; } // sdl diff --git a/src/modules/joystick/sdl/JoystickModule.cpp b/src/modules/joystick/sdl/JoystickModule.cpp index 71be57de0..80af58328 100644 --- a/src/modules/joystick/sdl/JoystickModule.cpp +++ b/src/modules/joystick/sdl/JoystickModule.cpp @@ -49,7 +49,11 @@ JoystickModule::JoystickModule() int count = 0; SDL_JoystickID *sticks = SDL_GetJoysticks(&count); for (int i = 0; i < count; i++) - addJoystick((int64) sticks[i]); + { + auto test = SDL_OpenGamepad(sticks[i]); + auto err = SDL_GetError(); + addJoystick((int64)sticks[i]); + } SDL_free(sticks); // Start joystick event watching. Joysticks are automatically added and diff --git a/src/modules/joystick/wrap_Joystick.cpp b/src/modules/joystick/wrap_Joystick.cpp index 5c49bf1f4..a58a873f5 100644 --- a/src/modules/joystick/wrap_Joystick.cpp +++ b/src/modules/joystick/wrap_Joystick.cpp @@ -447,6 +447,39 @@ int w_Joystick_getSensorData(lua_State *L) return (int) data.size(); } +int w_Joystick_getDevicePowerInfo(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + + int batteryPercent = 0; + const char *str; + Joystick::PowerType state = j->getPowerInfo(batteryPercent); + + if (!Joystick::getConstant(state, str)) + str = "unknown"; + + lua_pushstring(L, str); + + if (batteryPercent >= 0) + lua_pushnumber(L, batteryPercent); + else + lua_pushnil(L); + + return 2; +} + +int w_Joystick_getDeviceConnectionState(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + + const char *str = "unknown"; + Joystick::getConstant(j->getConnectionState(), str); + + lua_pushstring(L, str); + + return 1; +} + #endif // LOVE_ENABLE_SENSOR // List of functions to wrap. @@ -457,6 +490,8 @@ static const luaL_Reg w_Joystick_functions[] = { "getID", w_Joystick_getID }, { "getGUID", w_Joystick_getGUID }, { "getDeviceInfo", w_Joystick_getDeviceInfo }, + { "getDevicePowerInfo", w_Joystick_getDevicePowerInfo }, + { "getDeviceConnectionState", w_Joystick_getDeviceConnectionState }, { "getJoystickType", w_Joystick_getJoystickType }, { "getAxisCount", w_Joystick_getAxisCount }, { "getButtonCount", w_Joystick_getButtonCount }, From a8ed7ac7d9bb619c31a9e74af7202313d2013a33 Mon Sep 17 00:00:00 2001 From: snippet <874740+musshorn@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:43:01 +1000 Subject: [PATCH 3/3] Remove code added accidently for testing --- src/modules/joystick/sdl/Joystick.cpp | 5 ----- src/modules/joystick/sdl/JoystickModule.cpp | 4 ---- 2 files changed, 9 deletions(-) diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index 9c3c25013..e0d8a0bd2 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -158,11 +158,6 @@ bool Joystick::open(int64 deviceid) else gamepadType = GAMEPAD_TYPE_UNKNOWN; } - else - { - auto err = SDL_GetError(); - auto t = 1; - } return isConnected(); } diff --git a/src/modules/joystick/sdl/JoystickModule.cpp b/src/modules/joystick/sdl/JoystickModule.cpp index 80af58328..3a0a6d260 100644 --- a/src/modules/joystick/sdl/JoystickModule.cpp +++ b/src/modules/joystick/sdl/JoystickModule.cpp @@ -49,11 +49,7 @@ JoystickModule::JoystickModule() int count = 0; SDL_JoystickID *sticks = SDL_GetJoysticks(&count); for (int i = 0; i < count; i++) - { - auto test = SDL_OpenGamepad(sticks[i]); - auto err = SDL_GetError(); addJoystick((int64)sticks[i]); - } SDL_free(sticks); // Start joystick event watching. Joysticks are automatically added and