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
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(peripheral.joystick)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

include(CheckCXXSourceCompiles)
include(CheckIncludeFiles)

# --- Add-on Dependencies ------------------------------------------------------
Expand All @@ -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 <kodi/c-api/addon-instance/peripheral.h>
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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
3 changes: 3 additions & 0 deletions src/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/api/Joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/api/linux/JoystickLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dirent.h>
#include <errno.h>
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/api/linux/JoystickLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions src/api/sdl/JoystickSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(battery);
return false;
#endif
}
#endif
Comment thread
sunlollyking marked this conversation as resolved.
Resolved

bool CJoystickSDL::ScanEvents(void)
{
if (m_pController == nullptr)
Expand Down
3 changes: 3 additions & 0 deletions src/api/sdl/JoystickSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/api/udev/JoystickUdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <cstdio>
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -192,6 +199,16 @@ void CJoystickUdev::UpdateMotorState(const std::array<uint16_t, MOTOR_COUNT>& 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];
Expand Down
4 changes: 4 additions & 0 deletions src/api/udev/JoystickUdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/api/xinput/JoystickXInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
sunlollyking marked this conversation as resolved.
Resolved
}
Comment thread
sunlollyking marked this conversation as resolved.
Resolved

// 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())
Expand Down
3 changes: 3 additions & 0 deletions src/api/xinput/JoystickXInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading