diff --git a/xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp b/xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp index 5639dde4a4fe1..69106265f17a4 100644 --- a/xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp +++ b/xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp @@ -26,6 +26,38 @@ #ifdef HAS_DBUS #include "Application.h" +CUPowerSource::CUPowerSource(const char *powerSource) +{ + if(powerSource == NULL) + m_powerSource = ""; + else + m_powerSource = powerSource; + + CVariant properties = CDBusUtil::GetAll("org.freedesktop.UPower", m_powerSource.c_str(), "org.freedesktop.UPower.Device"); + m_isRechargeable = properties["IsRechargeable"].asBoolean(); + Update(); +} + +CUPowerSource::~CUPowerSource() +{ +} + +void CUPowerSource::Update() +{ + CVariant properties = CDBusUtil::GetAll("org.freedesktop.UPower", m_powerSource.c_str(), "org.freedesktop.UPower.Device"); + m_batteryLevel = properties["Percentage"].asDouble(); +} + +bool CUPowerSource::IsRechargeable() +{ + return m_isRechargeable; +} + +double CUPowerSource::BatteryLevel() +{ + return m_batteryLevel; +} + CConsoleUPowerSyscall::CConsoleUPowerSyscall() { CLog::Log(LOGINFO, "Selected UPower and ConsoleKit as PowerSyscall"); @@ -57,6 +89,8 @@ CConsoleUPowerSyscall::CConsoleUPowerSyscall() m_CanReboot = ConsoleKitMethodCall("CanRestart"); UpdateUPower(); + + EnumeratePowerSources(); } CConsoleUPowerSyscall::~CConsoleUPowerSyscall() @@ -122,7 +156,46 @@ bool CConsoleUPowerSyscall::CanReboot() int CConsoleUPowerSyscall::BatteryLevel() { - return 0; + unsigned int nBatteryCount = 0; + double subCapacity = 0; + double batteryLevel = 0; + + std::list::iterator itr; + for (itr = m_powerSources.begin(); itr != m_powerSources.end(); ++itr) + { + itr->Update(); + if(itr->IsRechargeable()) + { + nBatteryCount++; + subCapacity += itr->BatteryLevel(); + } + } + + if(nBatteryCount) + batteryLevel = subCapacity / (double)nBatteryCount; + + return (int) batteryLevel; +} + +void CConsoleUPowerSyscall::EnumeratePowerSources() +{ + CDBusMessage message("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "EnumerateDevices"); + DBusMessage *reply = message.SendSystem(); + if (reply) + { + char** source = NULL; + int length = 0; + + if (dbus_message_get_args (reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &source, &length, DBUS_TYPE_INVALID)) + { + for (int i = 0; i < length; i++) + { + m_powerSources.push_back(CUPowerSource(source[i])); + } + + dbus_free_string_array(source); + } + } } bool CConsoleUPowerSyscall::HasDeviceConsoleKit() diff --git a/xbmc/powermanagement/linux/ConsoleUPowerSyscall.h b/xbmc/powermanagement/linux/ConsoleUPowerSyscall.h index 4fd1ebfe0ea79..02aa6b3650885 100644 --- a/xbmc/powermanagement/linux/ConsoleUPowerSyscall.h +++ b/xbmc/powermanagement/linux/ConsoleUPowerSyscall.h @@ -22,6 +22,25 @@ #ifdef HAS_DBUS #include "powermanagement/IPowerSyscall.h" #include "DBusUtil.h" +#include "utils/StdString.h" + +#include + +class CUPowerSource +{ +public: + CUPowerSource(const char *powerSource); + ~CUPowerSource(); + + void Update(); + bool IsRechargeable(); + double BatteryLevel(); + +private: + CStdString m_powerSource; + bool m_isRechargeable; + double m_batteryLevel; +}; class CConsoleUPowerSyscall : public IPowerSyscall { @@ -54,6 +73,9 @@ class CConsoleUPowerSyscall : public IPowerSyscall bool m_lowBattery; + void EnumeratePowerSources(); + std::list m_powerSources; + DBusConnection *m_connection; DBusError m_error; };