Permalink
Browse files

systemd/logind power management support

  • Loading branch information...
1 parent fc4b66b commit 58d600d9abc651569749fa0e2d37e459de283732 @singular0 singular0 committed Nov 15, 2012
@@ -43,6 +43,8 @@
#elif defined(_LINUX) && defined(HAS_DBUS)
#include "linux/ConsoleUPowerSyscall.h"
#include "linux/ConsoleDeviceKitPowerSyscall.h"
+#include "linux/SystemdUPowerSyscall.h"
+#include "linux/UPowerSyscall.h"
#ifdef HAS_HAL
#include "linux/HALPowerSyscall.h"
#endif
@@ -72,10 +74,14 @@ void CPowerManager::Initialize()
#elif defined(TARGET_ANDROID)
m_instance = new CAndroidPowerSyscall();
#elif defined(_LINUX) && defined(HAS_DBUS)
- if (CConsoleUPowerSyscall::HasDeviceConsoleKit())
+ if (CConsoleUPowerSyscall::HasConsoleKitAndUPower())
m_instance = new CConsoleUPowerSyscall();
else if (CConsoleDeviceKitPowerSyscall::HasDeviceConsoleKit())
m_instance = new CConsoleDeviceKitPowerSyscall();
+ else if (CSystemdUPowerSyscall::HasSystemdAndUPower())
+ m_instance = new CSystemdUPowerSyscall();
+ else if (CUPowerSyscall::HasUPower())
+ m_instance = new CUPowerSyscall();
#ifdef HAS_HAL
else
m_instance = new CHALPowerSyscall();
@@ -24,83 +24,10 @@
#ifdef HAS_DBUS
-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");
-
- m_lowBattery = false;
-
- dbus_error_init (&m_error);
- // TODO: do not use dbus_connection_pop_message() that requires the use of a
- // private connection
- m_connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &m_error);
-
- if (m_connection)
- {
- dbus_connection_set_exit_on_disconnect(m_connection, false);
-
- dbus_bus_add_match(m_connection, "type='signal',interface='org.freedesktop.UPower'", &m_error);
- dbus_connection_flush(m_connection);
- }
-
- if (dbus_error_is_set(&m_error))
- {
- CLog::Log(LOGERROR, "UPower: Failed to attach to signal %s", m_error.message);
- dbus_connection_close(m_connection);
- dbus_connection_unref(m_connection);
- m_connection = NULL;
- }
-
m_CanPowerdown = ConsoleKitMethodCall("CanStop");
m_CanReboot = ConsoleKitMethodCall("CanRestart");
-
- UpdateUPower();
-
- EnumeratePowerSources();
-}
-
-CConsoleUPowerSyscall::~CConsoleUPowerSyscall()
-{
- if (m_connection)
- {
- dbus_connection_close(m_connection);
- dbus_connection_unref(m_connection);
- m_connection = NULL;
- }
-
- dbus_error_free (&m_error);
}
bool CConsoleUPowerSyscall::Powerdown()
@@ -109,94 +36,13 @@ bool CConsoleUPowerSyscall::Powerdown()
return message.SendSystem() != NULL;
}
-bool CConsoleUPowerSyscall::Suspend()
-{
- // UPower 0.9.1 does not signal sleeping unless you tell that its about to sleep...
- CDBusMessage aboutToSleepMessage("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "AboutToSleep");
- aboutToSleepMessage.SendAsyncSystem();
-
- CDBusMessage message("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "Suspend");
- return message.SendAsyncSystem();
-}
-
-bool CConsoleUPowerSyscall::Hibernate()
-{
- // UPower 0.9.1 does not signal sleeping unless you tell that its about to sleep...
- CDBusMessage aboutToSleepMessage("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "AboutToSleep");
- aboutToSleepMessage.SendAsyncSystem();
-
- CDBusMessage message("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "Hibernate");
- return message.SendAsyncSystem();
-}
-
bool CConsoleUPowerSyscall::Reboot()
{
CDBusMessage message("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "Restart");
return message.SendSystem() != NULL;
}
-bool CConsoleUPowerSyscall::CanPowerdown()
-{
- return m_CanPowerdown;
-}
-bool CConsoleUPowerSyscall::CanSuspend()
-{
- return m_CanSuspend;
-}
-bool CConsoleUPowerSyscall::CanHibernate()
-{
- return m_CanHibernate;
-}
-bool CConsoleUPowerSyscall::CanReboot()
-{
- return m_CanReboot;
-}
-
-int CConsoleUPowerSyscall::BatteryLevel()
-{
- 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()
+bool CConsoleUPowerSyscall::HasConsoleKitAndUPower()
{
bool hasConsoleKitManager = false;
CDBusMessage consoleKitMessage("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "CanStop");
@@ -221,58 +67,7 @@ bool CConsoleUPowerSyscall::HasDeviceConsoleKit()
dbus_error_free (&error);
- bool hasUPower = false;
- CDBusMessage deviceKitMessage("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "EnumerateDevices");
-
- deviceKitMessage.Send(con, &error);
-
- if (!dbus_error_is_set(&error))
- hasUPower = true;
- else
- CLog::Log(LOGDEBUG, "UPower: %s - %s", error.name, error.message);
-
- dbus_error_free (&error);
- dbus_connection_unref(con);
-
- return hasUPower && hasConsoleKitManager;
-}
-
-bool CConsoleUPowerSyscall::PumpPowerEvents(IPowerEventsCallback *callback)
-{
- bool result = false;
-
- if (m_connection)
- {
- dbus_connection_read_write(m_connection, 0);
- DBusMessage *msg = dbus_connection_pop_message(m_connection);
-
- if (msg)
- {
- result = true;
- if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Sleeping"))
- callback->OnSleep();
- else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Resuming"))
- callback->OnWake();
- else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Changed"))
- {
- bool lowBattery = m_lowBattery;
- UpdateUPower();
- if (m_lowBattery && !lowBattery)
- callback->OnLowBattery();
- }
- else
- CLog::Log(LOGDEBUG, "UPower: Recieved an unknown signal %s", dbus_message_get_member(msg));
-
- dbus_message_unref(msg);
- }
- }
- return result;
-}
-
-void CConsoleUPowerSyscall::UpdateUPower()
-{
- m_CanSuspend = CDBusUtil::GetVariant("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "CanSuspend").asBoolean(false);
- m_CanHibernate = CDBusUtil::GetVariant("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "CanHibernate").asBoolean(false);
+ return HasUPower() && hasConsoleKitManager;
}
bool CConsoleUPowerSyscall::ConsoleKitMethodCall(const char *method)
@@ -19,63 +19,19 @@
*/
#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;
-};
+#include "UPowerSyscall.h"
+#include "DBusUtil.h"
-class CConsoleUPowerSyscall : public IPowerSyscall
+class CConsoleUPowerSyscall : public CUPowerSyscall
{
public:
CConsoleUPowerSyscall();
- virtual ~CConsoleUPowerSyscall();
-
virtual bool Powerdown();
- virtual bool Suspend();
- virtual bool Hibernate();
virtual bool Reboot();
-
- virtual bool CanPowerdown();
- virtual bool CanSuspend();
- virtual bool CanHibernate();
- virtual bool CanReboot();
- virtual int BatteryLevel();
-
- virtual bool PumpPowerEvents(IPowerEventsCallback *callback);
-
- static bool HasDeviceConsoleKit();
+ static bool HasConsoleKitAndUPower();
private:
static bool ConsoleKitMethodCall(const char *method);
- void UpdateUPower();
-
- bool m_CanPowerdown;
- bool m_CanSuspend;
- bool m_CanHibernate;
- bool m_CanReboot;
-
- bool m_lowBattery;
-
- void EnumeratePowerSources();
- std::list<CUPowerSource> m_powerSources;
-
- DBusConnection *m_connection;
- DBusError m_error;
};
+
#endif
@@ -1,7 +1,9 @@
SRCS=ConsoleDeviceKitPowerSyscall.cpp \
ConsoleUPowerSyscall.cpp \
HALPowerSyscall.cpp \
-
+ UPowerSyscall.cpp \
+ SystemdUPowerSyscall.cpp
+
LIB=powermanagement_linux.a
include ../../../Makefile.include
Oops, something went wrong.

0 comments on commit 58d600d

Please sign in to comment.