summaryrefslogtreecommitdiff
path: root/src/modules/ecore
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-08-09 16:39:10 -0300
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-08-09 16:39:10 -0300
commit6f2871a3050d845ea91b2f7ba360298e84515b32 (patch)
treee91e461c830b685ca3468a28f7a767d2c61decef /src/modules/ecore
parentc0c5736a4e0d41adc9c97577006c289718c926de (diff)
ecore: add upower system module.
It's always enabled as it's a dbus module and links to nothing, actually the daemon doesn't need to be running -- in that case it will do nothing. In the case the daemon becomes active then it will get the OnLowBattery property and keep it in sync. NOTE: I couldn't test the property change as my laptop takes many hours to get to that situation... let's hope it works :-)
Diffstat (limited to 'src/modules/ecore')
-rw-r--r--src/modules/ecore/system/upower/ecore_system_upower.c225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/modules/ecore/system/upower/ecore_system_upower.c b/src/modules/ecore/system/upower/ecore_system_upower.c
new file mode 100644
index 0000000000..d89c4b91e8
--- /dev/null
+++ b/src/modules/ecore/system/upower/ecore_system_upower.c
@@ -0,0 +1,225 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Eldbus.h>
6#include <Ecore.h>
7#include <locale.h>
8
9static int _log_dom = -1;
10static Eldbus_Connection *_conn = NULL;
11
12static Eldbus_Object *_obj = NULL;
13static Eldbus_Proxy *_proxy = NULL;
14
15#ifdef CRITICAL
16#undef CRITICAL
17#endif
18#define CRITICAL(...) EINA_LOG_DOM_CRIT(_log_dom, __VA_ARGS__)
19
20#ifdef ERR
21#undef ERR
22#endif
23#define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
24
25#ifdef WRN
26#undef WRN
27#endif
28#define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
29
30#ifdef DBG
31#undef DBG
32#endif
33#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
34
35static void
36_on_low_battery_from_variant(Eldbus_Message_Iter *variant)
37{
38 Eina_Bool val;
39
40 if (!eldbus_message_iter_get_and_next(variant, 'b', &val))
41 {
42 ERR("Error getting OnLowBattery.");
43 return;
44 }
45
46 DBG("OnLowBattery=%hhu", val);
47 ecore_low_battery_set(val);
48}
49
50static void
51_on_low_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
52 Eldbus_Pending *pending EINA_UNUSED)
53{
54 Eldbus_Message_Iter *variant;
55 const char *errname, *errmsg;
56
57 if (eldbus_message_error_get(msg, &errname, &errmsg))
58 {
59 ERR("Message error %s - %s", errname, errmsg);
60 return;
61 }
62 if (!eldbus_message_arguments_get(msg, "v", &variant))
63 {
64 ERR("Error getting arguments.");
65 return;
66 }
67
68 _on_low_battery_from_variant(variant);
69}
70
71static void
72_on_low_battery_get(Eldbus_Proxy *proxy)
73{
74 eldbus_proxy_property_get(proxy, "OnLowBattery",
75 _on_low_battery_get_cb, NULL);
76}
77
78static void
79_props_changed(void *data, const Eldbus_Message *msg)
80{
81 Eldbus_Proxy *proxy = data;
82 Eldbus_Message_Iter *changed, *entry, *invalidated;
83 const char *iface, *prop;
84
85 if (!eldbus_message_arguments_get(msg, "sa{sv}as",
86 &iface, &changed, &invalidated))
87 {
88 ERR("Error getting data from properties changed signal.");
89 return;
90 }
91
92 while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
93 {
94 const void *key;
95 Eldbus_Message_Iter *var;
96 if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
97 continue;
98 printf("changed on low battery\n");
99 if (strcmp(key, "OnLowBattery") == 0)
100 {
101 _on_low_battery_from_variant(var);
102 return;
103 }
104 }
105
106 while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
107 {
108 printf("invalidated on low battery\n");
109 if (strcmp(prop, "OnLowBattery") == 0)
110 {
111 _on_low_battery_get(proxy);
112 return;
113 }
114 }
115}
116
117static void _upower_name_owner_cb(void *data,
118 const char *bus EINA_UNUSED,
119 const char *old_id,
120 const char *new_id)
121{
122 Eldbus_Proxy *proxy = data;
123
124 DBG("org.freedesktop.UPower name owner changed from '%s' to '%s'",
125 old_id, new_id);
126
127 if ((new_id) && (new_id[0]))
128 _on_low_battery_get(proxy);
129}
130
131static void _ecore_system_upower_shutdown(void);
132
133static Eina_Bool
134_ecore_system_upower_init(void)
135{
136 Eldbus_Signal_Handler *s;
137
138 eldbus_init();
139
140 _log_dom = eina_log_domain_register("ecore_system_upower", NULL);
141 if (_log_dom < 0)
142 {
143 EINA_LOG_ERR("Could not register log domain: ecore_system_upower");
144 goto error;
145 }
146
147 _conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM);
148
149 _obj = eldbus_object_get(_conn, "org.freedesktop.UPower",
150 "/org/freedesktop/UPower");
151 if (!_obj)
152 {
153 ERR("could not get object name=org.freedesktop.UPower, "
154 "path=/org/freedesktop/UPower");
155 goto error;
156 }
157
158 _proxy = eldbus_proxy_get(_obj, "org.freedesktop.UPower");
159 if (!_proxy)
160 {
161 ERR("could not get proxy interface=org.freedesktop.UPower, "
162 "name=org.freedesktop.UPower, path=/org/freedesktop/UPower");
163 goto error;
164 }
165
166 s = eldbus_proxy_properties_changed_callback_add(_proxy, _props_changed,
167 _proxy);
168 if (!s)
169 {
170 ERR("could not add signal handler for properties changed for proxy "
171 "interface=org.freedesktop.UPower, name=org.freedesktop.UPower, "
172 "path=/org/freedesktop/UPower");
173 goto error;
174 }
175
176 eldbus_name_owner_changed_callback_add(_conn, "org.freedesktop.UPower",
177 _upower_name_owner_cb,
178 _proxy, EINA_TRUE);
179
180 DBG("ecore system 'upower' loaded");
181 return EINA_TRUE;
182
183 error:
184 _ecore_system_upower_shutdown();
185 return EINA_FALSE;
186}
187
188static void
189_ecore_system_upower_shutdown(void)
190{
191 DBG("ecore system 'upower' unloaded");
192
193 eldbus_name_owner_changed_callback_del(_conn, "org.freedesktop.UPower",
194 _upower_name_owner_cb,
195 NULL);
196
197 if (_proxy)
198 {
199 eldbus_proxy_unref(_proxy);
200 _proxy = NULL;
201 }
202
203 if (_obj)
204 {
205 eldbus_object_unref(_obj);
206 _obj = NULL;
207 }
208
209 if (_conn)
210 {
211 eldbus_connection_unref(_conn);
212 _conn = NULL;
213 }
214
215 if (_log_dom > 0)
216 {
217 eina_log_domain_unregister(_log_dom);
218 _log_dom = -1;
219 }
220
221 eldbus_shutdown();
222}
223
224EINA_MODULE_INIT(_ecore_system_upower_init);
225EINA_MODULE_SHUTDOWN(_ecore_system_upower_shutdown);