summaryrefslogtreecommitdiff
path: root/src/modules/ecore/system/upower/ecore_system_upower.c
blob: e07694c5d8dba25a6fa5caeb4f55e2dc68418c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eldbus.h>
#include <Ecore.h>
#include <locale.h>

static int _log_dom = -1;
static Eldbus_Connection *_conn = NULL;

static Eldbus_Object *_obj = NULL;
static Eldbus_Proxy *_proxy = NULL;

static Eldbus_Object *_disp_obj = NULL;
static Eldbus_Proxy *_disp_proxy = NULL;

typedef enum {
     VERSION_ON_LOW_BATTERY,
     VERSION_WARNING_LEVEL
}Ecore_System_Upower_Version;
static Ecore_System_Upower_Version _version = 0;

#ifdef CRI
#undef CRI
#endif
#define CRI(...) EINA_LOG_DOM_CRIT(_log_dom, __VA_ARGS__)

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)

#ifdef WRN
#undef WRN
#endif
#define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)

#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)

static Eina_Bool _ecore_on_battery = EINA_FALSE;
static Eina_Bool _ecore_low_battery = EINA_FALSE;
static int _ecore_battery_level = -1;

static Eina_Bool _ecore_system_upower_display_device_init(void);
static void _ecore_system_upower_shutdown(void);

static void
_battery_eval(void)
{
   Ecore_Power_State power_state = ECORE_POWER_STATE_MAINS;

   if (_ecore_low_battery)
     {
        power_state = ECORE_POWER_STATE_LOW;
     }
   else if (_ecore_on_battery)
     {
        power_state = ECORE_POWER_STATE_BATTERY;

        /* FIXME: get level value from libupower? */
        if (_ecore_battery_level >= 3)
          {
             power_state = ECORE_POWER_STATE_LOW;
          }
     }

   ecore_power_state_set(power_state);
}

static void
_warning_level_from_variant(Eldbus_Message_Iter *variant)
{
   unsigned int val;

   if (!eldbus_message_iter_get_and_next(variant, 'u', &val))
     {
        ERR("Error getting WarningLevel.");
        return;
     }

   _ecore_battery_level = val;
   _battery_eval();
}

static void
_warning_level_get_cb(void *data EINA_UNUSED,
                      const Eldbus_Message *msg,
                      Eldbus_Pending *pending EINA_UNUSED)
{
   Eldbus_Message_Iter *variant;
   const char *errname, *errmsg;

   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
// don't print errors because this results in complaints about upower not
// existing and it's OK if it doesn't exist. just no feature enabled then
//        ERR("Message error %s - %s", errname, errmsg);
        return;
     }
   if (!eldbus_message_arguments_get(msg, "v", &variant))
     {
        ERR("Error getting arguments.");
        return;
     }

   _warning_level_from_variant(variant);
}

static void
_warning_level_get(Eldbus_Proxy *proxy)
{
   eldbus_proxy_property_get(proxy, "WarningLevel",
                             _warning_level_get_cb, NULL);
}

static void
_on_low_battery_from_variant(Eldbus_Message_Iter *variant)
{
   Eina_Bool val;

   if (!eldbus_message_iter_get_and_next(variant, 'b', &val))
     {
        ERR("Error getting OnLowBattery.");
        return;
     }

   DBG("OnLowBattery=%hhu", val);
   _ecore_low_battery = val;
   _battery_eval();
}

static void
_on_low_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
                        Eldbus_Pending *pending EINA_UNUSED)
{
   Eldbus_Message_Iter *variant;
   const char *errname, *errmsg;

   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
        ERR("Message error %s - %s", errname, errmsg);
        return;
     }
   if (!eldbus_message_arguments_get(msg, "v", &variant))
     {
        ERR("Error getting arguments.");
        return;
     }

   _on_low_battery_from_variant(variant);
}

static void
_on_low_battery_get(Eldbus_Proxy *proxy)
{
   eldbus_proxy_property_get(proxy, "OnLowBattery",
                             _on_low_battery_get_cb, NULL);
}

static void
_on_battery_from_variant(Eldbus_Message_Iter *variant)
{
   Eina_Bool val;

   if (!eldbus_message_iter_get_and_next(variant, 'b', &val))
     {
        ERR("Error getting OnBattery.");
        return;
     }

   DBG("OnBattery=%hhu", val);
   _ecore_on_battery = val;
   _battery_eval();
}

static void
_on_battery_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
                        Eldbus_Pending *pending EINA_UNUSED)
{
   Eldbus_Message_Iter *variant;
   const char *errname, *errmsg;

   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
        ERR("Message error %s - %s", errname, errmsg);
        return;
     }
   if (!eldbus_message_arguments_get(msg, "v", &variant))
     {
        ERR("Error getting arguments.");
        return;
     }

   _on_battery_from_variant(variant);
}

static void
_on_battery_get(Eldbus_Proxy *proxy)
{
   eldbus_proxy_property_get(proxy, "OnBattery",
                             _on_battery_get_cb, NULL);
}

static void
_battery_state_get()
{
   switch (_version)
     {
      case VERSION_ON_LOW_BATTERY:
         _on_low_battery_get(_proxy);
         break;
      case VERSION_WARNING_LEVEL:
         if (_ecore_system_upower_display_device_init())
           _warning_level_get(_disp_proxy);
         break;
      default:
         break;
     }
}

static void
_daemon_version_from_variant(Eldbus_Message_Iter *variant)
{
   const char *val;
   char **version;
   int standard[3] = {0, 99, 0}; // upower >= 0.99.0 provides WarningLevel instead of OnLowBattery
   int i;

   if (!eldbus_message_iter_get_and_next(variant, 's', &val))
     {
        ERR("Error getting DaemonVersion.");
        return;
     }
   version = eina_str_split(val, ".", 3);

   for (i = 0; i < 3; i ++)
     {
        if (atoi(version[i]) > standard[i])
          {
             _version = VERSION_WARNING_LEVEL;
             break;
          }
        else if (atoi(version[i]) < standard[i])
          {
             _version = VERSION_ON_LOW_BATTERY;
             break;
          }
        else if (i == 2)
          {
             _version = VERSION_WARNING_LEVEL;
             break;
          }
     }
   free(version[0]);
   free(version);

   _battery_state_get();
}

static void
_daemon_version_get_cb(void *data EINA_UNUSED, const Eldbus_Message *msg,
                          Eldbus_Pending *pending EINA_UNUSED)
{
   Eldbus_Message_Iter *variant;
   const char *errname, *errmsg;

   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
        ERR("Message error %s - %s", errname, errmsg);
        return;
     }
   if (!eldbus_message_arguments_get(msg, "v", &variant))
     {
        ERR("Error getting arguments.");
        return;
     }

   _daemon_version_from_variant(variant);
}

static void
_daemon_version_get(Eldbus_Proxy *proxy)
{
   eldbus_proxy_property_get(proxy, "DaemonVersion",
                             _daemon_version_get_cb, NULL);
}

static void
_props_changed(void *data, const Eldbus_Message *msg)
{
   Eldbus_Proxy *proxy = data;
   Eldbus_Message_Iter *changed, *entry, *invalidated;
   const char *iface, *prop;

   if (!eldbus_message_arguments_get(msg, "sa{sv}as",
                                     &iface, &changed, &invalidated))
     {
        ERR("Error getting data from properties changed signal.");
        return;
     }

   while (eldbus_message_iter_get_and_next(changed, 'e', &entry))
     {
        const void *key;
        Eldbus_Message_Iter *var;
        if (!eldbus_message_iter_arguments_get(entry, "sv", &key, &var))
          continue;
        if (strcmp(key, "OnBattery") == 0)
          _on_battery_from_variant(var);
        if (strcmp(key, "OnLowBattery") == 0)
          _on_low_battery_from_variant(var);
        if (strcmp(key, "WarningLevel") == 0)
          _warning_level_from_variant(var);
     }

   while (eldbus_message_iter_get_and_next(invalidated, 's', &prop))
     {
        if (strcmp(prop, "OnBattery") == 0)
          _on_battery_get(proxy);
        if (strcmp(prop, "OnLowBattery") == 0)
          _on_low_battery_get(proxy);
        if (strcmp(prop, "WarningLevel") == 0)
          _warning_level_get(proxy);
     }
}

static void _upower_name_owner_cb(void *data,
                                  const char *bus EINA_UNUSED,
                                  const char *old_id,
                                  const char *new_id)
{
   Eldbus_Proxy *proxy = data;

   DBG("org.freedesktop.UPower name owner changed from '%s' to '%s'",
       old_id, new_id);

   if ((new_id) && (new_id[0]))
     {
        _daemon_version_get(proxy);
     }
}

static Eina_Bool
_ecore_system_upower_display_device_init(void)
{
   Eldbus_Signal_Handler *s;

   _disp_obj =
      eldbus_object_get(_conn, "org.freedesktop.UPower",
                        "/org/freedesktop/UPower/devices/DisplayDevice");
   if (!_disp_obj)
     {
        ERR("could not get object name=org.freedesktop.UPower, "
            "path=/org/freedesktop/UPower/devices/DisplayDevice");
        goto disp_error;
     }

   _disp_proxy = eldbus_proxy_get(_disp_obj, "org.freedesktop.UPower");
   if (!_disp_proxy)
     {
        ERR("could not get proxy interface=org.freedesktop.UPower, "
            "name=org.freedesktop.UPower, "
            "path=/org/freedesktop/UPower/devices/DisplayDevice");
        goto disp_error;
     }

   s = eldbus_proxy_properties_changed_callback_add(_disp_proxy,
                                                    _props_changed,
                                                    _disp_proxy);
   if (!s)
     {
        ERR("could not add signal handler for properties changed for proxy "
            "interface=org.freedesktop.UPower, "
            "name=org.freedesktop.UPower, "
            "path=/org/freedesktop/UPower/devices/DisplayDevice");
        goto disp_error;
     }

   return EINA_TRUE;

disp_error:
   _ecore_system_upower_shutdown();
   return EINA_FALSE;
}

static Eina_Bool
_ecore_system_upower_init(void)
{
   Eldbus_Signal_Handler *s;

   eldbus_init();

   _log_dom = eina_log_domain_register("ecore_system_upower", NULL);
   if (_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: ecore_system_upower");
        goto error;
     }

   _conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM);

   _obj = eldbus_object_get(_conn, "org.freedesktop.UPower",
                            "/org/freedesktop/UPower");
   if (!_obj)
     {
        ERR("could not get object name=org.freedesktop.UPower, "
            "path=/org/freedesktop/UPower");
        goto error;
     }

   _proxy = eldbus_proxy_get(_obj, "org.freedesktop.UPower");
   if (!_proxy)
     {
        ERR("could not get proxy interface=org.freedesktop.UPower, "
            "name=org.freedesktop.UPower, path=/org/freedesktop/UPower");
        goto error;
     }

   s = eldbus_proxy_properties_changed_callback_add(_proxy, _props_changed,
                                                    _proxy);
   if (!s)
     {
        ERR("could not add signal handler for properties changed for proxy "
            "interface=org.freedesktop.UPower, name=org.freedesktop.UPower, "
            "path=/org/freedesktop/UPower");
        goto error;
     }

   eldbus_name_owner_changed_callback_add(_conn, "org.freedesktop.UPower",
                                          _upower_name_owner_cb,
                                          _proxy, EINA_TRUE);

   DBG("ecore system 'upower' loaded");
   return EINA_TRUE;

 error:
   _ecore_system_upower_shutdown();
   return EINA_FALSE;
}

static void
_ecore_system_upower_shutdown(void)
{
   DBG("ecore system 'upower' unloaded");

   eldbus_name_owner_changed_callback_del(_conn, "org.freedesktop.UPower",
                                          _upower_name_owner_cb,
                                          NULL);
  if (_disp_proxy)
     {
        eldbus_proxy_unref(_disp_proxy);
        _disp_proxy = NULL;
     }

   if (_disp_obj)
     {
        eldbus_object_unref(_disp_obj);
        _disp_obj = NULL;
     }

   if (_proxy)
     {
        eldbus_proxy_unref(_proxy);
        _proxy = NULL;
     }

   if (_obj)
     {
        eldbus_object_unref(_obj);
        _obj = NULL;
     }

   if (_conn)
     {
        eldbus_connection_unref(_conn);
        _conn = NULL;
     }

   if (_log_dom > 0)
     {
        eina_log_domain_unregister(_log_dom);
        _log_dom = -1;
     }

   eldbus_shutdown();
}

EINA_MODULE_INIT(_ecore_system_upower_init);
EINA_MODULE_SHUTDOWN(_ecore_system_upower_shutdown);