Skip to content

Commit

Permalink
Merge pull request xbmc#277 from huceke/battery-status
Browse files Browse the repository at this point in the history
added battery reading for UPower
  • Loading branch information
Tobias Arrskog committed Jul 13, 2011
2 parents 1c6125c + 671de9d commit 0b79092
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
5 changes: 5 additions & 0 deletions xbmc/linux/DBusUtil.cpp
Expand Up @@ -115,6 +115,7 @@ CVariant CDBusUtil::ParseType(DBusMessageIter *itr)
dbus_int64_t int64 = 0;
dbus_uint64_t uint64 = 0;
dbus_bool_t boolean = false;
double doublev = 0;

int type = dbus_message_iter_get_arg_type(itr);
switch (type)
Expand Down Expand Up @@ -145,6 +146,10 @@ CVariant CDBusUtil::ParseType(DBusMessageIter *itr)
dbus_message_iter_get_basic(itr, &boolean);
value = (bool)boolean;
break;
case DBUS_TYPE_DOUBLE:
dbus_message_iter_get_basic(itr, &doublev);
value = (double)doublev;
break;
case DBUS_TYPE_ARRAY:
DBusMessageIter array;
dbus_message_iter_recurse(itr, &array);
Expand Down
75 changes: 74 additions & 1 deletion xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp
Expand Up @@ -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");
Expand Down Expand Up @@ -57,6 +89,8 @@ CConsoleUPowerSyscall::CConsoleUPowerSyscall()
m_CanReboot = ConsoleKitMethodCall("CanRestart");

UpdateUPower();

EnumeratePowerSources();
}

CConsoleUPowerSyscall::~CConsoleUPowerSyscall()
Expand Down Expand Up @@ -122,7 +156,46 @@ bool CConsoleUPowerSyscall::CanReboot()

int CConsoleUPowerSyscall::BatteryLevel()
{
return 0;
unsigned int nBatteryCount = 0;
double subCapacity = 0;
double batteryLevel = 0;

std::list<CUPowerSource>::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()
Expand Down
22 changes: 22 additions & 0 deletions xbmc/powermanagement/linux/ConsoleUPowerSyscall.h
Expand Up @@ -22,6 +22,25 @@
#ifdef HAS_DBUS
#include "powermanagement/IPowerSyscall.h"
#include "DBusUtil.h"
#include "utils/StdString.h"

#include <list>

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
{
Expand Down Expand Up @@ -54,6 +73,9 @@ class CConsoleUPowerSyscall : public IPowerSyscall

bool m_lowBattery;

void EnumeratePowerSources();
std::list<CUPowerSource> m_powerSources;

DBusConnection *m_connection;
DBusError m_error;
};
Expand Down

0 comments on commit 0b79092

Please sign in to comment.