aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/battery/e_mod_dbus.c
blob: 6dcfd6c58d52133de886961acb01a8ad03df3e72 (plain)
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
#include "e.h"
#include "e_mod_main.h"

static void _battery_dbus_battery_props(void *data, void *reply_data, DBusError *error);
static void _battery_dbus_ac_adapter_props(void *data, void *reply_data, DBusError *error);
static void _battery_dbus_battery_property_changed(void *data, DBusMessage *msg);
static void _battery_dbus_battery_add(const char *udi);
static void _battery_dbus_battery_del(const char *udi);
static void _battery_dbus_ac_adapter_add(const char *udi);
static void _battery_dbus_ac_adapter_del(const char *udi);
static void _battery_dbus_find_battery(void *user_data, void *reply_data, DBusError *err);
static void _battery_dbus_find_ac(void *user_data, void *reply_data, DBusError *err);
static void _battery_dbus_is_battery(void *user_data, void *reply_data, DBusError *err);
static void _battery_dbus_is_ac_adapter(void *user_data, void *reply_data, DBusError *err);
static void _battery_dbus_dev_add(void *data, DBusMessage *msg);
static void _battery_dbus_dev_del(void *data, DBusMessage *msg);

void
_battery_dbus_start(void)
{
   E_DBus_Connection *conn;

   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_manager_find_device_by_capability
     (conn, "battery", _battery_dbus_find_battery, NULL);
   e_hal_manager_find_device_by_capability
     (conn, "ac_adapter", _battery_dbus_find_ac, NULL);
   battery_config->dbus.dev_add = 
     e_dbus_signal_handler_add(conn, E_HAL_SENDER,
                               E_HAL_MANAGER_PATH,
                               E_HAL_MANAGER_INTERFACE,
                               "DeviceAdded", _battery_dbus_dev_add, NULL);
   battery_config->dbus.dev_del = 
     e_dbus_signal_handler_add(conn, E_HAL_SENDER,
                               E_HAL_MANAGER_PATH,
                               E_HAL_MANAGER_INTERFACE,
                               "DeviceRemoved", _battery_dbus_dev_del, NULL);
   init_time = ecore_time_get();
}


void
_battery_dbus_stop(void)
{
   E_DBus_Connection *conn;
   Ac_Adapter *ac;
   Battery *bat;
   
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   if (battery_config->dbus.have)
     {
        dbus_pending_call_cancel(battery_config->dbus.have);
        battery_config->dbus.have = NULL;
     }
   if (battery_config->dbus.dev_add)
     {
        e_dbus_signal_handler_del(conn, battery_config->dbus.dev_add);
        battery_config->dbus.dev_add = NULL;
     }
   if (battery_config->dbus.dev_del)
     {
        e_dbus_signal_handler_del(conn, battery_config->dbus.dev_del);
        battery_config->dbus.dev_del = NULL;
     }
   EINA_LIST_FREE(device_ac_adapters, ac)
     {
        e_dbus_signal_handler_del(conn, ac->prop_change);
        eina_stringshare_del(ac->udi);
        eina_stringshare_del(ac->product);
        free(ac);
     }
   EINA_LIST_FREE(device_batteries, bat)
     {
        e_dbus_signal_handler_del(conn, bat->prop_change);
        eina_stringshare_del(bat->udi);
        eina_stringshare_del(bat->technology);
        eina_stringshare_del(bat->type);
        eina_stringshare_del(bat->charge_units);
        eina_stringshare_del(bat->model);
        eina_stringshare_del(bat->vendor);
        free(bat);
     }
}

static void
_battery_dbus_battery_props(void *data, void *reply_data, DBusError *error __UNUSED__)
{
   E_Hal_Properties *ret = reply_data;
   Battery *bat = data;
   int err = 0;
   const char *str;

   if (dbus_error_is_set(error))
     {
        dbus_error_free(error);
        return;
     }
   if (!ret) return;

#undef GET_BOOL
#undef GET_INT
#undef GET_STR
#define GET_BOOL(val, s) bat->val = e_hal_property_bool_get(ret, s, &err)
#define GET_INT(val, s) bat->val = e_hal_property_int_get(ret, s, &err)
#define GET_STR(val, s) \
   if (bat->val) eina_stringshare_del(bat->val); \
   bat->val = NULL; \
   str = e_hal_property_string_get(ret, s, &err); \
   if (str) \
     { \
        bat->val = eina_stringshare_ref(str); \
     }
   
   GET_BOOL(present, "battery.present");
   GET_STR(technology, "battery.reporting.technology");
   GET_STR(model, "battery.model");
   GET_STR(vendor, "battery.vendor");
   GET_STR(type, "battery.type");
   GET_STR(charge_units, "battery.reporting.unit");
   GET_INT(percent, "battery.charge_level.percentage");
   GET_BOOL(can_charge, "battery.is_rechargeable");
   GET_INT(current_charge, "battery.charge_level.current");
   GET_INT(charge_rate, "battery.charge_level.rate");
   GET_INT(design_charge, "battery.charge_level.design");
   GET_INT(last_full_charge, "battery.charge_level.last_full");
   if (e_hal_property_bool_get(ret, "battery.rechargeable.is_charging", &err))
     {
        bat->charging = 1;
        GET_INT(time_full, "battery.remaining_time");
        bat->time_left = -1;
     }
   else
     {
        bat->charging = 0;
        GET_INT(time_left, "battery.remaining_time");
        bat->time_full = -1;
     }
   bat->got_prop = 1;
   _battery_device_update();
}

static void
_battery_dbus_ac_adapter_props(void *data, void *reply_data, DBusError *error __UNUSED__)
{
   E_Hal_Properties *ret = reply_data;
   Ac_Adapter *ac = data;
   int err = 0;
   const char *str;

   if (dbus_error_is_set(error))
     {
        dbus_error_free(error);
        return;
     }
   if (!ret) return;
   
#undef GET_BOOL
#undef GET_STR
#define GET_BOOL(val, s) ac->val = e_hal_property_bool_get(ret, s, &err)
#define GET_STR(val, s) \
   if (ac->val) eina_stringshare_del(ac->val); \
   ac->val = NULL; \
   str = e_hal_property_string_get(ret, s, &err); \
   if (str) \
     { \
        ac->val = eina_stringshare_ref(str); \
     }
   
   GET_BOOL(present, "ac_adapter.present");
   GET_STR(product, "info.product");
   _battery_device_update();
}

static void
_battery_dbus_battery_property_changed(void *data, DBusMessage *msg __UNUSED__)
{
   E_DBus_Connection *conn;
   
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_device_get_all_properties(conn, ((Battery *)data)->udi,
                                   _battery_dbus_battery_props, data);
}

static void
_battery_dbus_ac_adapter_property_changed(void *data, DBusMessage *msg __UNUSED__)
{
   E_DBus_Connection *conn;
   
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_device_get_all_properties(conn, ((Ac_Adapter *)data)->udi,
                                   _battery_dbus_ac_adapter_props, data);
}

static void
_battery_dbus_battery_add(const char *udi)
{
   E_DBus_Connection *conn;
   Battery *bat;

   bat = _battery_battery_find(udi);
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   if (!bat)
     {
        bat = E_NEW(Battery, 1);
        if (!bat) return;
        bat->udi = eina_stringshare_add(udi);
        device_batteries = eina_list_append(device_batteries, bat);
        bat->prop_change =
     e_dbus_signal_handler_add(conn, E_HAL_SENDER, udi,
                               E_HAL_DEVICE_INTERFACE, "PropertyModified",
                               _battery_dbus_battery_property_changed,
                               bat);
     }
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_device_get_all_properties(conn, udi, 
                                   _battery_dbus_battery_props, bat);

   _battery_device_update();
}

static void
_battery_dbus_battery_del(const char *udi)
{
   E_DBus_Connection *conn;
   Eina_List *l;
   Battery *bat;
   
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   bat = _battery_battery_find(udi);
   if (bat)
     {
        e_dbus_signal_handler_del(conn, bat->prop_change);
        l = eina_list_data_find(device_batteries, bat);
        eina_stringshare_del(bat->udi);
        eina_stringshare_del(bat->technology);
        eina_stringshare_del(bat->type);
        eina_stringshare_del(bat->charge_units);
        eina_stringshare_del(bat->model);
        eina_stringshare_del(bat->vendor);
        free(bat);
        device_batteries = eina_list_remove_list(device_batteries, l);
        return;
     }
   _battery_device_update();
}

static void
_battery_dbus_ac_adapter_add(const char *udi)
{
   E_DBus_Connection *conn;
   Ac_Adapter *ac;

   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   ac = E_NEW(Ac_Adapter, 1);
   if (!ac) return;
   ac->udi = eina_stringshare_add(udi);
   device_ac_adapters = eina_list_append(device_ac_adapters, ac);
   ac->prop_change =
     e_dbus_signal_handler_add(conn, E_HAL_SENDER, udi,
                               E_HAL_DEVICE_INTERFACE, "PropertyModified",
                               _battery_dbus_ac_adapter_property_changed, 
                               ac);
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_device_get_all_properties(conn, udi, 
                                   _battery_dbus_ac_adapter_props, ac);
   _battery_device_update();
}

static void
_battery_dbus_ac_adapter_del(const char *udi)
{
   E_DBus_Connection *conn;
   Eina_List *l;
   Ac_Adapter *ac;
   
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   ac = _battery_ac_adapter_find(udi);
   if (ac)
     {
        e_dbus_signal_handler_del(conn, ac->prop_change);
        l = eina_list_data_find(device_ac_adapters, ac);
        eina_stringshare_del(ac->udi);
        eina_stringshare_del(ac->product);
        free(ac);
        device_ac_adapters = eina_list_remove_list(device_ac_adapters, l);
        return;
     }
   _battery_device_update();
}

static void
_battery_dbus_find_battery(void *user_data __UNUSED__, void *reply_data, DBusError *err __UNUSED__)
{
   Eina_List *l;
   char *device;
   E_Hal_Manager_Find_Device_By_Capability_Return *ret;

   ret = reply_data;
   if (dbus_error_is_set(err))
     {
        dbus_error_free(err);
        return;
     }
   if (!ret) return;

   if (eina_list_count(ret->strings) < 1) return;
   EINA_LIST_FOREACH(ret->strings, l, device)
     _battery_dbus_battery_add(device);
}

static void
_battery_dbus_find_ac(void *user_data __UNUSED__, void *reply_data, DBusError *err __UNUSED__)
{
   Eina_List *l;
   char *device;
   E_Hal_Manager_Find_Device_By_Capability_Return *ret;

   
   ret = reply_data;
   if (dbus_error_is_set(err))
     {
        dbus_error_free(err);
        return;
     }
   if (!ret) return;

   if (eina_list_count(ret->strings) < 1) return;
   EINA_LIST_FOREACH(ret->strings, l, device)
     _battery_dbus_ac_adapter_add(device);

}

static void
_battery_dbus_is_battery(void *user_data, void *reply_data, DBusError *err)
{
   char *udi = user_data;
   E_Hal_Device_Query_Capability_Return *ret;

   
   ret = reply_data;
   if (dbus_error_is_set(err))
     {
        dbus_error_free(err);
        goto error;
     }
   if (!ret) goto error;
   if (ret->boolean)
     _battery_dbus_battery_add(udi);
   error:
   eina_stringshare_del(udi);
}

static void
_battery_dbus_is_ac_adapter(void *user_data, void *reply_data, DBusError *err)
{
   char *udi = user_data;
   E_Hal_Device_Query_Capability_Return *ret;

   
   ret = reply_data;
   if (dbus_error_is_set(err))
     {
        dbus_error_free(err);
        goto error;
     }
   if (!ret) goto error;

   if (ret->boolean)
     _battery_dbus_ac_adapter_add(udi);
   error:
   eina_stringshare_del(udi);
}

static void
_battery_dbus_dev_add(void *data __UNUSED__, DBusMessage *msg)
{
   DBusError err;
   char *udi = NULL;
   E_DBus_Connection *conn;
   
   dbus_error_init(&err);
   dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &udi, DBUS_TYPE_INVALID);
   if (!udi) return;
   conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
   if (!conn) return;
   // FIXME: e_dbus doesnt allow us to track this pending call
   e_hal_device_query_capability(conn, udi, "battery",
                                 _battery_dbus_is_battery, (void*)eina_stringshare_add(udi));
   e_hal_device_query_capability(conn, udi, "ac_adapter",
                                 _battery_dbus_is_ac_adapter, (void*)eina_stringshare_add(udi));
}

static void
_battery_dbus_dev_del(void *data __UNUSED__, DBusMessage *msg)
{
   DBusError err;
   char *udi = NULL;
   
   dbus_error_init(&err);
   dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &udi, DBUS_TYPE_INVALID);
   if (!udi) return;
   _battery_dbus_battery_del(udi);
   _battery_dbus_ac_adapter_del(udi);
}