2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "HALPowerSyscall.h"
23 #include "utils/log.h"
26 #include <dbus/dbus.h>
29 CHALPowerSyscall::CHALPowerSyscall()
31 m_CanPowerdown = true;
32 m_CanSuspend = QueryCapability("power_management.can_suspend");
33 m_CanHibernate = QueryCapability("power_management.can_hibernate");
37 bool CHALPowerSyscall::Powerdown()
39 return doPowerCall("Shutdown");
41 bool CHALPowerSyscall::Suspend()
43 CPowerSyscallWithoutEvents::Suspend();
44 return doPowerCall("Suspend");
46 bool CHALPowerSyscall::Hibernate()
48 CPowerSyscallWithoutEvents::Hibernate();
49 return doPowerCall("Hibernate");
51 bool CHALPowerSyscall::Reboot()
53 return doPowerCall("Reboot");
56 bool CHALPowerSyscall::CanPowerdown()
58 return m_CanPowerdown;
60 bool CHALPowerSyscall::CanSuspend()
64 bool CHALPowerSyscall::CanHibernate()
66 return m_CanHibernate;
68 bool CHALPowerSyscall::CanReboot()
73 int CHALPowerSyscall::BatteryLevel()
78 bool CHALPowerSyscall::doPowerCall(const char *powerstate)
83 dbus_error_init (&error);
84 DBusConnection *connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
85 dbus_int32_t int32 = 0;
89 msg = dbus_message_new_method_call("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device.SystemPowerManagement", powerstate);
91 if (msg && strcmp(powerstate, "Suspend") == 0)
93 dbus_message_iter_init_append(msg, &args);
94 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &int32))
95 CLog::Log(LOGERROR, "DBus: Failed to append arguments");
98 CLog::Log(LOGERROR, "DBus: Create PowerManagement Message failed");
101 result = dbus_connection_send(connection, msg, NULL);
102 // Need to create a reader for the Message
103 dbus_message_unref(msg);
107 dbus_connection_unref(connection);
114 bool CHALPowerSyscall::QueryCapability(const char *capability)
117 DBusMessageIter args;
119 dbus_error_init (&error);
120 DBusConnection *connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
123 msg = dbus_message_new_method_call("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device", "GetProperty");
125 dbus_message_iter_init_append(msg, &args);
126 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &capability))
133 reply = dbus_connection_send_with_reply_and_block(connection, msg, -1, &error);
136 if (dbus_error_is_set(&error))
137 dbus_error_free(&error);
139 dbus_message_unref(msg);
143 dbus_bool_t b = false;
144 dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b);
146 if (dbus_error_is_set(&error))
147 dbus_error_free(&error);
149 dbus_message_unref(reply);
150 dbus_message_unref(msg);
155 if (dbus_error_is_set(&error))
156 dbus_error_free(&error);