summaryrefslogtreecommitdiff
path: root/src/modules/ecore/system
diff options
context:
space:
mode:
authorMinJeong Kim <minjjj.kim@samsung.com>2014-12-12 13:09:09 +0100
committerStefan Schmidt <s.schmidt@samsung.com>2014-12-12 16:41:40 +0100
commit792af971f12fddc3e9637096f0d2292ee8eaf46a (patch)
tree419fc1c6bd9d7633e218dfd5f042042c21e75898 /src/modules/ecore/system
parent9867fe58633f42c24f9888f70e1500c57c59028c (diff)
ecore: Added getting "WarningLevel" on upower module.
Summary: From UPower 0.99.0, a property "OnLowBattery" was removed and upower have recommended to use "WarningLevel" instead. This revision provides "WarningLevel" using the property "DaemonVersion" and it keeps dealing with "OnLowBattery" as well for old system. Fixes T1909 Reviewers: gwanglim, barbieri, zmike, stefan_schmidt Reviewed By: barbieri Subscribers: zmike, stefan_schmidt, cedric Differential Revision: https://phab.enlightenment.org/D1717
Diffstat (limited to 'src/modules/ecore/system')
-rw-r--r--src/modules/ecore/system/upower/ecore_system_upower.c337
1 files changed, 220 insertions, 117 deletions
diff --git a/src/modules/ecore/system/upower/ecore_system_upower.c b/src/modules/ecore/system/upower/ecore_system_upower.c
index d09995ba57..e4188e3638 100644
--- a/src/modules/ecore/system/upower/ecore_system_upower.c
+++ b/src/modules/ecore/system/upower/ecore_system_upower.c
@@ -12,6 +12,15 @@ static Eldbus_Connection *_conn = NULL;
12static Eldbus_Object *_obj = NULL; 12static Eldbus_Object *_obj = NULL;
13static Eldbus_Proxy *_proxy = NULL; 13static Eldbus_Proxy *_proxy = NULL;
14 14
15static Eldbus_Object *_disp_obj = NULL;
16static Eldbus_Proxy *_disp_proxy = NULL;
17
18typedef enum {
19 VERSION_ON_LOW_BATTERY,
20 VERSION_WARNING_LEVEL
21}Ecore_System_Upower_Version;
22static Ecore_System_Upower_Version _version = 0;
23
15#ifdef CRI 24#ifdef CRI
16#undef CRI 25#undef CRI
17#endif 26#endif
@@ -33,26 +42,77 @@ static Eldbus_Proxy *_proxy = NULL;
33#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__) 42#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
34 43
35static Eina_Bool _ecore_on_battery = EINA_FALSE; 44static Eina_Bool _ecore_on_battery = EINA_FALSE;
36static unsigned int _ecore_low_battery = 0; 45static Eina_Bool _ecore_low_battery = EINA_FALSE;
46static int _ecore_battery_level = -1;
37 47
38static int uversions[3] = {0}; //major, minor, micro 48static Eina_Bool _ecore_system_upower_display_device_init(void);
39static void _ecore_system_upower_shutdown(void); 49static void _ecore_system_upower_shutdown(void);
40 50
41static void 51static void
42_battery_eval(void) 52_battery_eval(void)
43{ 53{
44 if (uversions[0] >= 1) return; 54 Ecore_Power_State power_state = ECORE_POWER_STATE_MAINS;
45 if (uversions[1] >= 99) 55
46 ecore_power_state_set(_ecore_low_battery); 56 if (_ecore_low_battery)
47 else 57 {
48 { 58 power_state = ECORE_POWER_STATE_LOW;
49 if (_ecore_low_battery) 59 }
50 ecore_power_state_set(ECORE_POWER_STATE_LOW); 60 else if (_ecore_on_battery)
51 else if (_ecore_on_battery) 61 {
52 ecore_power_state_set(ECORE_POWER_STATE_BATTERY); 62 power_state = ECORE_POWER_STATE_BATTERY;
53 else 63
54 ecore_power_state_set(ECORE_POWER_STATE_MAINS); 64 /* FIXME: get level value from libupower? */
65 if (_ecore_battery_level >= 3)
66 {
67 power_state = ECORE_POWER_STATE_LOW;
68 }
69 }
70
71 ecore_power_state_set(power_state);
72}
73
74static void
75_warning_level_from_variant(Eldbus_Message_Iter *variant)
76{
77 unsigned int val;
78
79 if (!eldbus_message_iter_get_and_next(variant, 'u', &val))
80 {
81 ERR("Error getting WarningLevel.");
82 return;
83 }
84
85 _ecore_battery_level = val;
86 _battery_eval();
87}
88
89static void
90_warning_level_get_cb(void *data EINA_UNUSED,
91 const Eldbus_Message *msg,
92 Eldbus_Pending *pending EINA_UNUSED)
93{
94 Eldbus_Message_Iter *variant;
95 const char *errname, *errmsg;
96
97 if (eldbus_message_error_get(msg, &errname, &errmsg))
98 {
99 ERR("Message error %s - %s", errname, errmsg);
100 return;
101 }
102 if (!eldbus_message_arguments_get(msg, "v", &variant))
103 {
104 ERR("Error getting arguments.");
105 return;
55 } 106 }
107
108 _warning_level_from_variant(variant);
109}
110
111static void
112_warning_level_get(Eldbus_Proxy *proxy)
113{
114 eldbus_proxy_property_get(proxy, "WarningLevel",
115 _warning_level_get_cb, NULL);
56} 116}
57 117
58static void 118static void
@@ -72,17 +132,10 @@ _on_low_battery_from_variant(Eldbus_Message_Iter *variant)
72} 132}
73 133
74static void 134static void
75_on_low_battery_from_uint(unsigned int level)
76{
77 DBG("OnLowBattery=%hhu", level);
78 _ecore_low_battery = level;
79 _battery_eval();
80}
81
82static void
83_on_low_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, 135_on_low_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
84 Eldbus_Pending *pending EINA_UNUSED) 136 Eldbus_Pending *pending EINA_UNUSED)
85{ 137{
138 Eldbus_Message_Iter *variant;
86 const char *errname, *errmsg; 139 const char *errname, *errmsg;
87 140
88 if (eldbus_message_error_get(msg, &errname, &errmsg)) 141 if (eldbus_message_error_get(msg, &errname, &errmsg))
@@ -90,49 +143,20 @@ _on_low_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
90 ERR("Message error %s - %s", errname, errmsg); 143 ERR("Message error %s - %s", errname, errmsg);
91 return; 144 return;
92 } 145 }
93 146 if (!eldbus_message_arguments_get(msg, "v", &variant))
94 if (uversions[0] >= 1)
95 { 147 {
96 ERR("Unsupported new UPower version!"); 148 ERR("Error getting arguments.");
97 return; 149 return;
98 } 150 }
99 if (uversions[1] >= 99)
100 {
101 unsigned int level;
102
103 if (!eldbus_message_arguments_get(msg, "u", &level))
104 {
105 ERR("Error getting arguments.");
106 return;
107 }
108 _on_low_battery_from_uint(level);
109 }
110 else
111 {
112 Eldbus_Message_Iter *variant;
113 151
114 if (!eldbus_message_arguments_get(msg, "v", &variant)) 152 _on_low_battery_from_variant(variant);
115 {
116 ERR("Error getting arguments.");
117 return;
118 }
119 _on_low_battery_from_variant(variant);
120 }
121} 153}
122 154
123static void 155static void
124_on_low_battery_get(Eldbus_Proxy *proxy) 156_on_low_battery_get(Eldbus_Proxy *proxy)
125{ 157{
126 /* version specific battery properties */ 158 eldbus_proxy_property_get(proxy, "OnLowBattery",
127 if (uversions[0] < 1) 159 _on_low_battery_get_cb, NULL);
128 {
129 if (uversions[1] >= 99)
130 {/* FIXME: this module needs a huge refactoring since WarningLevel property is per-device */}
131 else
132 eldbus_proxy_property_get(proxy, "OnLowBattery", _on_low_battery_get_cb, NULL);
133 }
134 else
135 CRI("SOMEBODY SHOULD BE MAINTAINING THIS MODULE!!!!!!!");
136} 160}
137 161
138static void 162static void
@@ -180,98 +204,124 @@ _on_battery_get(Eldbus_Proxy *proxy)
180} 204}
181 205
182static void 206static void
183_props_changed(void *data, const Eldbus_Message *msg) 207_battery_state_get()
184{ 208{
185 Eldbus_Proxy *proxy = data; 209 switch (_version)
186 Eldbus_Message_Iter *changed, *entry, *invalidated; 210 {
187 const char *iface, *prop; 211 case VERSION_ON_LOW_BATTERY:
212 _on_low_battery_get(_proxy);
213 break;
214 case VERSION_WARNING_LEVEL:
215 if (_ecore_system_upower_display_device_init())
216 _warning_level_get(_disp_proxy);
217 break;
218 default:
219 break;
220 }
221}
188 222
189 if (!eldbus_message_arguments_get(msg, "sa{sv}as", 223static void
190 &iface, &changed, &invalidated)) 224_daemon_version_from_variant(Eldbus_Message_Iter *variant)
225{
226 const char *val;
227 char **version;
228 int standard[3] = {0, 99, 0}; // upower >= 0.99.0 provides WarningLevel instead of OnLowBattery
229 int i;
230
231 if (!eldbus_message_iter_get_and_next(variant, 's', &val))
191 { 232 {
192 ERR("Error getting data from properties changed signal."); 233 ERR("Error getting DaemonVersion.");
193 return; 234 return;
194 } 235 }
236 version = eina_str_split(val, ".", 3);
195 237
196 while (eldbus_message_iter_get_and_next(changed, 'e', &entry)) 238 for (i = 0; i < 3; i ++)
197 { 239 {
198 const void *key; 240 if (atoi(version[i]) > standard[i])
199 Eldbus_Message_Iter *var;
200 if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
201 continue;
202 if (strcmp(key, "OnBattery") == 0)
203 _on_battery_from_variant(var);
204 if (uversions[0] >= 1)
205 { 241 {
206 ERR("Unsupported new UPower version!"); 242 _version = VERSION_WARNING_LEVEL;
207 return; 243 break;
208 } 244 }
209 if (uversions[1] >= 99) 245 else if (atoi(version[i]) < standard[i])
210 { 246 {
211 /* FIXME: this will never be hit since it's on the wrong proxy */ 247 _version = VERSION_ON_LOW_BATTERY;
212#if 0 248 break;
213 if (strcmp(key, "WarningLevel") == 0)
214 {
215 unsigned int level;
216
217 if (!eldbus_message_iter_get_and_next(var, 'u', &level))
218 ERR("Error getting OnBattery.");
219 else
220 _on_low_battery_from_uint(level);
221 }
222#endif
223 } 249 }
224 else 250 else if (i == 2)
225 { 251 {
226 if (strcmp(key, "OnLowBattery") == 0) 252 _version = VERSION_WARNING_LEVEL;
227 _on_low_battery_from_variant(var); 253 break;
228 } 254 }
229 } 255 }
230 256
231 while (eldbus_message_iter_get_and_next(invalidated, 's', &prop)) 257 _battery_state_get();
232 {
233 if (strcmp(prop, "OnBattery") == 0)
234 _on_battery_get(proxy);
235 if (strcmp(prop, "OnLowBattery") == 0)
236 _on_low_battery_get(proxy);
237 }
238} 258}
239 259
240static void 260static void
241_version_get(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED) 261_daemon_version_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
262 Eldbus_Pending *pending EINA_UNUSED)
242{ 263{
243 const char *errname, *errmsg, *v;
244 char vers[128], *e;
245 unsigned int i;
246 Eldbus_Message_Iter *variant; 264 Eldbus_Message_Iter *variant;
265 const char *errname, *errmsg;
247 266
248 /* FIXME: if either of these fail...do something? */
249 if (eldbus_message_error_get(msg, &errname, &errmsg)) 267 if (eldbus_message_error_get(msg, &errname, &errmsg))
250 { 268 {
251 ERR("Message error %s - %s", errname, errmsg); 269 ERR("Message error %s - %s", errname, errmsg);
252 return; 270 return;
253 } 271 }
254 if ((!eldbus_message_arguments_get(msg, "v", &variant)) || 272 if (!eldbus_message_arguments_get(msg, "v", &variant))
255 (!eldbus_message_iter_get_and_next(variant, 's', &v)))
256 { 273 {
257 ERR("Error getting version."); 274 ERR("Error getting arguments.");
258 return; 275 return;
259 } 276 }
260 e = strncpy(vers, v, sizeof(vers) - 1); 277
261 for (i = 0; e[0] && (i < 3); i++, e++) 278 _daemon_version_from_variant(variant);
279}
280
281static void
282_daemon_version_get(Eldbus_Proxy *proxy)
283{
284 eldbus_proxy_property_get(proxy, "DaemonVersion",
285 _daemon_version_get_cb, NULL);
286}
287
288static void
289_props_changed(void *data, const Eldbus_Message *msg)
290{
291 Eldbus_Proxy *proxy = data;
292 Eldbus_Message_Iter *changed, *entry, *invalidated;
293 const char *iface, *prop;
294
295 if (!eldbus_message_arguments_get(msg, "sa{sv}as",
296 &iface, &changed, &invalidated))
262 { 297 {
263 errno = 0; 298 ERR("Error getting data from properties changed signal.");
264 uversions[i] = strtol(e, &e, 10); 299 return;
265 if (errno) break;
266 } 300 }
267 if ((uversions[0] >= 1) || (uversions[1] >= 99)) 301
302 while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
268 { 303 {
269 /* may as well kill the module since it'll do more harm than good */ 304 const void *key;
270 ERR("Unsupported new UPower version!"); 305 Eldbus_Message_Iter *var;
271 _ecore_system_upower_shutdown(); 306 if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
272 return; 307 continue;
308 if (strcmp(key, "OnBattery") == 0)
309 _on_battery_from_variant(var);
310 if (strcmp(key, "OnLowBattery") == 0)
311 _on_low_battery_from_variant(var);
312 if (strcmp(key, "WarningLevel") == 0)
313 _warning_level_from_variant(var);
314 }
315
316 while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
317 {
318 if (strcmp(prop, "OnBattery") == 0)
319 _on_battery_get(proxy);
320 if (strcmp(prop, "OnLowBattery") == 0)
321 _on_low_battery_get(proxy);
322 if (strcmp(prop, "WarningLevel") == 0)
323 _warning_level_get(proxy);
273 } 324 }
274 _on_low_battery_get(data);
275} 325}
276 326
277static void _upower_name_owner_cb(void *data, 327static void _upower_name_owner_cb(void *data,
@@ -285,10 +335,53 @@ static void _upower_name_owner_cb(void *data,
285 old_id, new_id); 335 old_id, new_id);
286 336
287 if ((new_id) && (new_id[0])) 337 if ((new_id) && (new_id[0]))
288 eldbus_proxy_property_get(proxy, "DaemonVersion", _version_get, proxy); 338 {
339 _daemon_version_get(proxy);
340 }
289} 341}
290 342
291static void _ecore_system_upower_shutdown(void); 343static Eina_Bool
344_ecore_system_upower_display_device_init(void)
345{
346 Eldbus_Signal_Handler *s;
347
348 _disp_obj =
349 eldbus_object_get(_conn, "org.freedesktop.UPower",
350 "/org/freedesktop/UPower/devices/DisplayDevice");
351 if (!_disp_obj)
352 {
353 ERR("could not get object name=org.freedesktop.UPower, "
354 "path=/org/freedesktop/UPower/devices/DisplayDevice");
355 goto disp_error;
356 }
357
358 _disp_proxy = eldbus_proxy_get(_disp_obj, "org.freedesktop.UPower");
359 if (!_disp_proxy)
360 {
361 ERR("could not get proxy interface=org.freedesktop.UPower, "
362 "name=org.freedesktop.UPower, "
363 "path=/org/freedesktop/UPower/devices/DisplayDevice");
364 goto disp_error;
365 }
366
367 s = eldbus_proxy_properties_changed_callback_add(_disp_proxy,
368 _props_changed,
369 _disp_proxy);
370 if (!s)
371 {
372 ERR("could not add signal handler for properties changed for proxy "
373 "interface=org.freedesktop.UPower, "
374 "name=org.freedesktop.UPower, "
375 "path=/org/freedesktop/UPower/devices/DisplayDevice");
376 goto disp_error;
377 }
378
379 return EINA_TRUE;
380
381disp_error:
382 _ecore_system_upower_shutdown();
383 return EINA_FALSE;
384}
292 385
293static Eina_Bool 386static Eina_Bool
294_ecore_system_upower_init(void) 387_ecore_system_upower_init(void)
@@ -353,6 +446,17 @@ _ecore_system_upower_shutdown(void)
353 eldbus_name_owner_changed_callback_del(_conn, "org.freedesktop.UPower", 446 eldbus_name_owner_changed_callback_del(_conn, "org.freedesktop.UPower",
354 _upower_name_owner_cb, 447 _upower_name_owner_cb,
355 NULL); 448 NULL);
449 if (_disp_proxy)
450 {
451 eldbus_proxy_unref(_disp_proxy);
452 _disp_proxy = NULL;
453 }
454
455 if (_disp_obj)
456 {
457 eldbus_object_unref(_disp_obj);
458 _disp_obj = NULL;
459 }
356 460
357 if (_proxy) 461 if (_proxy)
358 { 462 {
@@ -379,7 +483,6 @@ _ecore_system_upower_shutdown(void)
379 } 483 }
380 484
381 eldbus_shutdown(); 485 eldbus_shutdown();
382 memset(&uversions, 0, sizeof(uversions));
383} 486}
384 487
385EINA_MODULE_INIT(_ecore_system_upower_init); 488EINA_MODULE_INIT(_ecore_system_upower_init);