From 6f9c3fcb1f01bf52f804234af2749d0a278d1935 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 21 Oct 2026 13:59:10 +0100 Subject: [PATCH] Report the battery of each joystick Kodi can ask for a joystick's battery so it can show it during games. On Linux the level comes from the kernel's power supply class, which covers Bluetooth HID pads as well as the PlayStation, Switch and Xbox drivers. XInput and SDL only report it in coarse steps. The battery code is only built when Kodi's headers have the call, so the add-on still builds against Kodi 22. --- CMakeLists.txt | 16 ++++ src/addon.cpp | 14 ++++ src/addon.h | 3 + src/api/Joystick.h | 9 +++ src/api/linux/JoystickLinux.cpp | 15 ++++ src/api/linux/JoystickLinux.h | 3 + src/api/sdl/JoystickSDL.cpp | 44 ++++++++++ src/api/sdl/JoystickSDL.h | 3 + src/api/udev/JoystickUdev.cpp | 17 ++++ src/api/udev/JoystickUdev.h | 4 + src/api/xinput/JoystickXInput.cpp | 47 +++++++++++ src/api/xinput/JoystickXInput.h | 3 + src/utils/linux/PowerSupply.cpp | 130 ++++++++++++++++++++++++++++++ src/utils/linux/PowerSupply.h | 34 ++++++++ 14 files changed, 342 insertions(+) create mode 100644 src/utils/linux/PowerSupply.cpp create mode 100644 src/utils/linux/PowerSupply.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d76a48b1..e343328e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(peripheral.joystick) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +include(CheckCXXSourceCompiles) include(CheckIncludeFiles) # --- Add-on Dependencies ------------------------------------------------------ @@ -14,6 +15,16 @@ find_package(Threads REQUIRED) include_directories(${PROJECT_SOURCE_DIR}/src ${KODI_INCLUDE_DIR}/..) # Hack way with "/..", need bigger Kodi cmake rework to match right include ways (becomes done in future) +# Kodi 22 has no battery call in its peripheral API +set(CMAKE_REQUIRED_INCLUDES ${KODI_INCLUDE_DIR} ${KODI_INCLUDE_DIR}/..) +check_cxx_source_compiles("#include +int main() { JOYSTICK_BATTERY battery{}; return battery.level; }" + HAVE_PERIPHERAL_JOYSTICK_BATTERY) +unset(CMAKE_REQUIRED_INCLUDES) +if(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + add_definitions(-DHAVE_PERIPHERAL_JOYSTICK_BATTERY) +endif() + set(JOYSTICK_SOURCES src/addon.cpp src/api/IJoystickInterface.cpp src/api/Joystick.cpp @@ -112,6 +123,11 @@ if(CORE_SYSTEM_NAME MATCHES windows) list(APPEND JOYSTICK_HEADERS src/utils/windows/CharsetConverter.h) endif() +if(CORE_SYSTEM_NAME STREQUAL linux AND HAVE_PERIPHERAL_JOYSTICK_BATTERY) + list(APPEND JOYSTICK_SOURCES src/utils/linux/PowerSupply.cpp) + list(APPEND JOYSTICK_HEADERS src/utils/linux/PowerSupply.h) +endif() + check_include_files("syslog.h" HAVE_SYSLOG) if(HAVE_SYSLOG) diff --git a/src/addon.cpp b/src/addon.cpp index 5ee6a95e..83752af4 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -185,4 +185,18 @@ void CPeripheralJoystick::PowerOffJoystick(unsigned int index) joystick->PowerOff(); } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +PERIPHERAL_ERROR CPeripheralJoystick::GetJoystickBattery(unsigned int index, JOYSTICK_BATTERY& battery) +{ + JoystickPtr joystick = CJoystickManager::Get().GetJoystick(index); + if (!joystick) + return PERIPHERAL_ERROR_NOT_CONNECTED; + + if (!joystick->GetBattery(battery)) + return PERIPHERAL_ERROR_NOT_IMPLEMENTED; + + return PERIPHERAL_NO_ERROR; +} +#endif + ADDONCREATOR(CPeripheralJoystick) // Don't touch this! diff --git a/src/addon.h b/src/addon.h index 3dc13e74..2fc666c2 100644 --- a/src/addon.h +++ b/src/addon.h @@ -49,6 +49,9 @@ class DLL_PRIVATE CPeripheralJoystick void RevertButtonMap(const kodi::addon::Joystick& joystick) override; void ResetButtonMap(const kodi::addon::Joystick& joystick, const std::string& controller_id) override; void PowerOffJoystick(unsigned int index) override; +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + PERIPHERAL_ERROR GetJoystickBattery(unsigned int index, JOYSTICK_BATTERY& battery) override; +#endif private: JOYSTICK::CPeripheralScanner* m_scanner; diff --git a/src/api/Joystick.h b/src/api/Joystick.h index b3b4be32..fd8a2c89 100644 --- a/src/api/Joystick.h +++ b/src/api/Joystick.h @@ -71,6 +71,15 @@ namespace JOYSTICK */ virtual void PowerOff() { } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + /*! + * Get how the joystick is powered and how charged it is + * + * \return False if the driver can't tell + */ + virtual bool GetBattery(JOYSTICK_BATTERY& battery) { return false; } +#endif + protected: /*! * Implemented by derived class to scan for events diff --git a/src/api/linux/JoystickLinux.cpp b/src/api/linux/JoystickLinux.cpp index eb739010..9b73e5cf 100644 --- a/src/api/linux/JoystickLinux.cpp +++ b/src/api/linux/JoystickLinux.cpp @@ -11,6 +11,9 @@ #include "api/JoystickTypes.h" #include "log/Log.h" #include "utils/CommonMacros.h" +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +#include "utils/linux/PowerSupply.h" +#endif #include #include @@ -54,6 +57,18 @@ bool CJoystickLinux::Equals(const CJoystick* rhs) const return m_strFilename == rhsLinux->m_strFilename; } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +bool CJoystickLinux::GetBattery(JOYSTICK_BATTERY& battery) +{ + // /dev/input/js0 -> /sys/class/input/js0 + const size_t lastSlash = m_strFilename.find_last_of('/'); + const std::string nodeName = + lastSlash == std::string::npos ? m_strFilename : m_strFilename.substr(lastSlash + 1); + + return CPowerSupply::GetBattery("/sys/class/input/" + nodeName, battery); +} +#endif + bool CJoystickLinux::ScanEvents(void) { js_event joyEvent; diff --git a/src/api/linux/JoystickLinux.h b/src/api/linux/JoystickLinux.h index 87bed3b3..36e2c5d5 100644 --- a/src/api/linux/JoystickLinux.h +++ b/src/api/linux/JoystickLinux.h @@ -24,6 +24,9 @@ namespace JOYSTICK // implementation of CJoystick virtual void Deinitialize(void) override; virtual bool Equals(const CJoystick* rhs) const override; +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + virtual bool GetBattery(JOYSTICK_BATTERY& battery) override; +#endif protected: virtual bool ScanEvents(void) override; diff --git a/src/api/sdl/JoystickSDL.cpp b/src/api/sdl/JoystickSDL.cpp index 79e4d18d..7f1de72c 100644 --- a/src/api/sdl/JoystickSDL.cpp +++ b/src/api/sdl/JoystickSDL.cpp @@ -71,6 +71,50 @@ void CJoystickSDL::Deinitialize(void) CJoystick::Deinitialize(); } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +bool CJoystickSDL::GetBattery(JOYSTICK_BATTERY& battery) +{ +#if SDL_VERSION_ATLEAST(2, 0, 4) + if (m_pController == nullptr) + return false; + + SDL_Joystick* joystick = SDL_GameControllerGetJoystick(m_pController); + if (joystick == nullptr) + return false; + + switch (SDL_JoystickCurrentPowerLevel(joystick)) + { + case SDL_JOYSTICK_POWER_WIRED: + battery.state = JOYSTICK_BATTERY_STATE_WIRED; + battery.level = -1; + return true; + case SDL_JOYSTICK_POWER_EMPTY: + battery.level = 5; + break; + case SDL_JOYSTICK_POWER_LOW: + battery.level = 20; + break; + case SDL_JOYSTICK_POWER_MEDIUM: + battery.level = 70; + break; + case SDL_JOYSTICK_POWER_FULL: + case SDL_JOYSTICK_POWER_MAX: + battery.level = 100; + break; + default: + return false; + } + + battery.state = JOYSTICK_BATTERY_STATE_DISCHARGING; + return true; +#else + // SDL only reports the power level from 2.0.4 + static_cast(battery); + return false; +#endif +} +#endif + bool CJoystickSDL::ScanEvents(void) { if (m_pController == nullptr) diff --git a/src/api/sdl/JoystickSDL.h b/src/api/sdl/JoystickSDL.h index fa84d7f1..d58d73bd 100644 --- a/src/api/sdl/JoystickSDL.h +++ b/src/api/sdl/JoystickSDL.h @@ -25,6 +25,9 @@ namespace JOYSTICK virtual bool Equals(const CJoystick* rhs) const override; virtual bool Initialize(void) override; virtual void Deinitialize(void) override; +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + virtual bool GetBattery(JOYSTICK_BATTERY& battery) override; +#endif protected: virtual bool ScanEvents(void) override; diff --git a/src/api/udev/JoystickUdev.cpp b/src/api/udev/JoystickUdev.cpp index 9a40992c..e98a1c90 100644 --- a/src/api/udev/JoystickUdev.cpp +++ b/src/api/udev/JoystickUdev.cpp @@ -9,6 +9,9 @@ #include "JoystickUdev.h" #include "api/JoystickTypes.h" #include "log/Log.h" +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +#include "utils/linux/PowerSupply.h" +#endif #include #include @@ -46,6 +49,10 @@ CJoystickUdev::CJoystickUdev(udev_device* dev, const char* path) m_motors(), m_previousMotors() { + // The device is released once the joystick is constructed + if (const char* syspath = udev_device_get_syspath(dev)) + m_syspath = syspath; + // Must initialize in the constructor to fill out joystick properties Initialize(); } @@ -192,6 +199,16 @@ void CJoystickUdev::UpdateMotorState(const std::array& mo } } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +bool CJoystickUdev::GetBattery(JOYSTICK_BATTERY& battery) +{ + if (m_syspath.empty()) + return false; + + return CPowerSupply::GetBattery(m_syspath, battery); +} +#endif + bool CJoystickUdev::ScanEvents(void) { input_event events[32]; diff --git a/src/api/udev/JoystickUdev.h b/src/api/udev/JoystickUdev.h index 98f0d3aa..c9a3ace3 100644 --- a/src/api/udev/JoystickUdev.h +++ b/src/api/udev/JoystickUdev.h @@ -58,6 +58,9 @@ namespace JOYSTICK virtual bool Initialize(void) override; virtual void Deinitialize(void) override; virtual void ProcessEvents(void) override; +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + virtual bool GetBattery(JOYSTICK_BATTERY& battery) override; +#endif // udev API bool IsInitialized() const { return m_bInitialized; } @@ -84,6 +87,7 @@ namespace JOYSTICK // Udev properties udev_device* m_dev; std::string m_path; + std::string m_syspath; dev_t m_deviceNumber; int m_fd; bool m_bInitialized; diff --git a/src/api/xinput/JoystickXInput.cpp b/src/api/xinput/JoystickXInput.cpp index 6390c128..94b316f9 100644 --- a/src/api/xinput/JoystickXInput.cpp +++ b/src/api/xinput/JoystickXInput.cpp @@ -59,6 +59,53 @@ void CJoystickXInput::PowerOff() CXInputDLL::Get().PowerOff(m_controllerID); } +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) +bool CJoystickXInput::GetBattery(JOYSTICK_BATTERY& battery) +{ + XINPUT_BATTERY_INFORMATION info{}; + if (!CXInputDLL::Get().GetBatteryInformation(m_controllerID, + CXInputDLL::BatteryDeviceType::Controller, info)) + return false; + + switch (info.BatteryType) + { + case BATTERY_TYPE_DISCONNECTED: + case BATTERY_TYPE_UNKNOWN: + return false; + + case BATTERY_TYPE_WIRED: + battery.state = JOYSTICK_BATTERY_STATE_WIRED; + battery.level = -1; + return true; + + default: + break; + } + + // XInput only reports the charge in four steps + battery.state = JOYSTICK_BATTERY_STATE_DISCHARGING; + switch (info.BatteryLevel) + { + case BATTERY_LEVEL_EMPTY: + battery.level = 5; + break; + case BATTERY_LEVEL_LOW: + battery.level = 20; + break; + case BATTERY_LEVEL_MEDIUM: + battery.level = 60; + break; + case BATTERY_LEVEL_FULL: + battery.level = 100; + break; + default: + return false; + } + + return true; +} +#endif + bool CJoystickXInput::ScanEvents(void) { if (CXInputDLL::Get().HasGuideButton()) diff --git a/src/api/xinput/JoystickXInput.h b/src/api/xinput/JoystickXInput.h index 45dccbff..655e5c94 100644 --- a/src/api/xinput/JoystickXInput.h +++ b/src/api/xinput/JoystickXInput.h @@ -30,6 +30,9 @@ namespace JOYSTICK virtual bool Equals(const CJoystick* rhs) const override; virtual void PowerOff() override; +#if defined(HAVE_PERIPHERAL_JOYSTICK_BATTERY) + virtual bool GetBattery(JOYSTICK_BATTERY& battery) override; +#endif protected: virtual bool ScanEvents(void) override; diff --git a/src/utils/linux/PowerSupply.cpp b/src/utils/linux/PowerSupply.cpp new file mode 100644 index 00000000..3297a157 --- /dev/null +++ b/src/utils/linux/PowerSupply.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2026 Team Kodi + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSE.md for more information. + */ + +#include "PowerSupply.h" + +#include +#include +#include +#include +#include + +using namespace JOYSTICK; + +namespace +{ + // From the input node, the battery hangs off the HID device or the USB + // interface: event5 -> input12 -> 0005:045E:02E0.0006/power_supply + constexpr unsigned int MAX_PARENT_DEPTH = 3; +} + +bool CPowerSupply::GetBattery(const std::string& sysfsPath, JOYSTICK_BATTERY& battery) +{ + char resolvedPath[PATH_MAX]; + if (realpath(sysfsPath.c_str(), resolvedPath) == nullptr) + return false; + + std::string devicePath = resolvedPath; + + for (unsigned int depth = 0; depth <= MAX_PARENT_DEPTH; depth++) + { + const std::string powerSupplyDir = devicePath + "/power_supply"; + + DIR* dir = opendir(powerSupplyDir.c_str()); + if (dir != nullptr) + { + bool bFound = false; + + while (dirent* entry = readdir(dir)) + { + const std::string name = entry->d_name; + if (name == "." || name == "..") + continue; + + if (ReadBattery(powerSupplyDir + "/" + name, battery)) + { + bFound = true; + break; + } + } + + closedir(dir); + + if (bFound) + return true; + } + + const size_t lastSlash = devicePath.find_last_of('/'); + if (lastSlash == std::string::npos || lastSlash == 0) + break; + + devicePath.erase(lastSlash); + } + + return false; +} + +bool CPowerSupply::ReadBattery(const std::string& powerSupplyPath, JOYSTICK_BATTERY& battery) +{ + if (ReadAttribute(powerSupplyPath + "/type") != "Battery") + return false; + + // A device's own battery, not one powering the whole system + if (ReadAttribute(powerSupplyPath + "/scope") == "System") + return false; + + int level = -1; + + // Fails until the device has reported its charge + const std::string capacity = ReadAttribute(powerSupplyPath + "/capacity"); + if (!capacity.empty()) + { + level = std::atoi(capacity.c_str()); + } + else + { + // Some drivers only report the charge in steps + const std::string capacityLevel = ReadAttribute(powerSupplyPath + "/capacity_level"); + if (capacityLevel == "Critical") + level = 5; + else if (capacityLevel == "Low") + level = 25; + else if (capacityLevel == "Normal") + level = 50; + else if (capacityLevel == "High") + level = 75; + else if (capacityLevel == "Full") + level = 100; + } + + if (level < 0) + return false; + + const std::string status = ReadAttribute(powerSupplyPath + "/status"); + if (status == "Charging") + battery.state = JOYSTICK_BATTERY_STATE_CHARGING; + else if (status == "Full" || (status == "Not charging" && level >= 100)) + battery.state = JOYSTICK_BATTERY_STATE_FULL; + else if (status == "Discharging" || status == "Not charging") + battery.state = JOYSTICK_BATTERY_STATE_DISCHARGING; + else + battery.state = JOYSTICK_BATTERY_STATE_UNKNOWN; + + battery.level = std::min(level, 100); + + return true; +} + +std::string CPowerSupply::ReadAttribute(const std::string& path) +{ + std::ifstream file(path); + + std::string value; + std::getline(file, value); + + return value; +} diff --git a/src/utils/linux/PowerSupply.h b/src/utils/linux/PowerSupply.h new file mode 100644 index 00000000..d179ed8d --- /dev/null +++ b/src/utils/linux/PowerSupply.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2026 Team Kodi + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSE.md for more information. + */ + +#pragma once + +#include + +#include + +namespace JOYSTICK +{ + class CPowerSupply + { + public: + /*! + * \brief Read the battery the kernel reports for an input device + * + * \param sysfsPath The input device's directory in sysfs, such as + * /sys/class/input/event5 + * \param battery The battery, if the device has one + * + * \return True if the kernel reports a battery for the device + */ + static bool GetBattery(const std::string& sysfsPath, JOYSTICK_BATTERY& battery); + + private: + static bool ReadBattery(const std::string& powerSupplyPath, JOYSTICK_BATTERY& battery); + static std::string ReadAttribute(const std::string& path); + }; +}