[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / powermanagement / linux / HALPowerSyscall.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "system.h"
22 #include "HALPowerSyscall.h"
23 #include "utils/log.h"
24
25 #ifdef HAS_HAL
26 #include <dbus/dbus.h>
27 #include <stdlib.h>
28
29 CHALPowerSyscall::CHALPowerSyscall()
30 {
31   m_CanPowerdown = true;
32   m_CanSuspend   = QueryCapability("power_management.can_suspend");
33   m_CanHibernate = QueryCapability("power_management.can_hibernate");
34   m_CanReboot    = true;
35 }
36
37 bool CHALPowerSyscall::Powerdown()
38 {
39   return doPowerCall("Shutdown");
40 }
41 bool CHALPowerSyscall::Suspend()
42 {
43   CPowerSyscallWithoutEvents::Suspend();
44   return doPowerCall("Suspend");
45 }
46 bool CHALPowerSyscall::Hibernate()
47 {
48   CPowerSyscallWithoutEvents::Hibernate();
49   return doPowerCall("Hibernate");
50 }
51 bool CHALPowerSyscall::Reboot()
52 {
53   return doPowerCall("Reboot");
54 }
55
56 bool CHALPowerSyscall::CanPowerdown()
57 {
58   return m_CanPowerdown;
59 }
60 bool CHALPowerSyscall::CanSuspend()
61 {
62   return m_CanSuspend;
63 }
64 bool CHALPowerSyscall::CanHibernate()
65 {
66   return m_CanHibernate;
67 }
68 bool CHALPowerSyscall::CanReboot()
69 {
70   return m_CanReboot;
71 }
72
73 int CHALPowerSyscall::BatteryLevel()
74 {
75   return 0;
76 }
77
78 bool CHALPowerSyscall::doPowerCall(const char *powerstate)
79 {
80   DBusMessage* msg;
81   DBusMessageIter args;
82   DBusError error;
83   dbus_error_init (&error);
84   DBusConnection *connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
85   dbus_int32_t int32 = 0;
86   if (connection)
87   {
88     bool result = false;
89     msg = dbus_message_new_method_call("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device.SystemPowerManagement", powerstate);
90
91     if (msg && strcmp(powerstate, "Suspend") == 0)
92     {
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");
96     }
97     if (msg == NULL)
98       CLog::Log(LOGERROR, "DBus: Create PowerManagement Message failed");
99     else
100     {
101       result = dbus_connection_send(connection, msg, NULL);
102       // Need to create a reader for the Message
103       dbus_message_unref(msg);
104       msg = NULL;
105     }
106
107     dbus_connection_unref(connection);
108     connection = NULL;
109     return result;
110   }
111   return false;
112 }
113
114 bool CHALPowerSyscall::QueryCapability(const char *capability)
115 {
116   DBusMessage* msg;
117   DBusMessageIter args;
118   DBusError error;
119   dbus_error_init (&error);
120   DBusConnection *connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
121   if (connection)
122   {
123     msg = dbus_message_new_method_call("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer", "org.freedesktop.Hal.Device", "GetProperty");
124
125     dbus_message_iter_init_append(msg, &args);
126     if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &capability))
127       return false;
128
129     if (msg == NULL)
130       return false;
131
132     DBusMessage *reply;
133     reply = dbus_connection_send_with_reply_and_block(connection, msg, -1, &error);
134     if (reply == NULL)
135     {
136       if (dbus_error_is_set(&error))
137         dbus_error_free(&error);
138
139       dbus_message_unref(msg);
140       return false;
141     }
142
143     dbus_bool_t b = false;
144     dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b);
145
146     if (dbus_error_is_set(&error))
147       dbus_error_free(&error);
148
149     dbus_message_unref(reply);
150     dbus_message_unref(msg);
151     msg = NULL;
152     return b;
153   }
154
155   if (dbus_error_is_set(&error))
156     dbus_error_free(&error);
157   return false;
158 }
159 #endif