summaryrefslogtreecommitdiff
path: root/efl/dbus_mainloop/e_dbus.c
blob: 70d651774718cc91a59222aa103f72c66bb4388c (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
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
/*
 * Copyright (C) 2007-2015 various contributors (see AUTHORS)
 * 
 * This file is part of Python-EFL.
 * 
 * Python-EFL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * Python-EFL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "e_dbus.h"
#include <stdbool.h>


#ifndef E_DBUS_COLOR_DEFAULT
#define E_DBUS_COLOR_DEFAULT EINA_COLOR_CYAN
#endif
static int _e_dbus_log_dom = -1;
#define DBG(...)   EINA_LOG_DOM_DBG(_e_dbus_log_dom, __VA_ARGS__)
#define INFO(...)    EINA_LOG_DOM_INFO(_e_dbus_log_dom, __VA_ARGS__)
#define WARN(...) EINA_LOG_DOM_WARN(_e_dbus_log_dom, __VA_ARGS__)
#define ERR(...)   EINA_LOG_DOM_ERR(_e_dbus_log_dom, __VA_ARGS__)

static int connection_slot = -1;
static int e_dbus_idler_active = 0;
static int close_connection = 0;
static int _edbus_init_count = 0;

// change this define for extra debug
// #define DDBG(...) printf(__VA_ARGS__); printf("\n");
#define DDBG(...)


static Eina_Bool
e_dbus_idler(void *data)
{
   E_DBus_Connection *cd;
   cd = data;

   if (DBUS_DISPATCH_COMPLETE == dbus_connection_get_dispatch_status(cd->conn))
   {
      DBG("done dispatching!");
      cd->idler = NULL;
      return ECORE_CALLBACK_CANCEL;
   }
   e_dbus_idler_active++;
   dbus_connection_ref(cd->conn);
   DBG("dispatch()");
   dbus_connection_dispatch(cd->conn);
   dbus_connection_unref(cd->conn);
   e_dbus_idler_active--;
   if (!e_dbus_idler_active && close_connection)
   {
      do
      {
         e_dbus_connection_close(cd);
      } while (--close_connection);
   }
   return ECORE_CALLBACK_RENEW;
}

static void
cb_dispatch_status(DBusConnection *conn, DBusDispatchStatus new_status, void *data)
{
   E_DBus_Connection *cd;

   DBG("dispatch status: %d!", new_status);
   cd = data;

   if (new_status == DBUS_DISPATCH_DATA_REMAINS && !cd->idler)
      cd->idler = ecore_idler_add(e_dbus_idler, cd);
   else if (new_status != DBUS_DISPATCH_DATA_REMAINS && cd->idler) 
   {
      ecore_idler_del(cd->idler);
      cd->idler = NULL;
   }
}


/* ecore fd handler */
static Eina_Bool
e_dbus_fd_handler(void *data, Ecore_Fd_Handler *fd_handler)
{
   E_DBus_Handler_Data *hd = data;
   unsigned int condition = 0;

   DDBG("DATA READY ON FD");
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
      condition |= DBUS_WATCH_READABLE;
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
      condition |= DBUS_WATCH_WRITABLE;
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
      condition |= DBUS_WATCH_ERROR;

   if (hd->watch_read && dbus_watch_get_enabled(hd->watch_read))
      dbus_watch_handle(hd->watch_read, condition);
   if (hd->watch_read != hd->watch_write &&
       hd->watch_write && dbus_watch_get_enabled(hd->watch_write))
      dbus_watch_handle(hd->watch_write, condition);
   
   return ECORE_CALLBACK_RENEW;
}

static void
e_dbus_fd_handler_update(E_DBus_Handler_Data *hd)
{
   Ecore_Fd_Handler_Flags eflags = ECORE_FD_ERROR;

   if (hd->watch_read && dbus_watch_get_enabled(hd->watch_read))
      eflags |= ECORE_FD_READ;
   if (hd->watch_write && dbus_watch_get_enabled(hd->watch_write))
      eflags |= ECORE_FD_WRITE;

   DDBG("FD update %d (flags: %d)", hd->fd, eflags)
   ecore_main_fd_handler_active_set(hd->fd_handler, eflags);
}

static void
e_dbus_fd_handler_add(E_DBus_Connection *cd, DBusWatch *watch)
{
   E_DBus_Handler_Data *hd = NULL, *hd2;
   Eina_List *l;
   int dflags;
   int fd;

   fd = dbus_watch_get_unix_fd(watch);
   dflags = dbus_watch_get_flags(watch);

   EINA_LIST_FOREACH(cd->fd_handlers, l, hd2)
   {
      if (hd2->fd == fd)
      {
         hd = hd2;
         break;
      }
   }

   if (!hd)
   {
      hd = calloc(1, sizeof(E_DBus_Handler_Data));
      if (!hd) return;
      hd->cd = cd;
      hd->fd = fd;
      hd->fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_ERROR,
                                                 e_dbus_fd_handler,
                                                 hd, NULL, NULL);
      if (!hd->fd_handler) { DDBG("ERROR! cannot create FD handler") }
      cd->fd_handlers = eina_list_append(cd->fd_handlers, hd);
   }

   if (dflags & DBUS_WATCH_READABLE) hd->watch_read = watch;
   if (dflags & DBUS_WATCH_WRITABLE) hd->watch_write = watch;
   dbus_watch_set_data(watch, hd, NULL);
   e_dbus_fd_handler_update(hd);
}

static void
e_dbus_fd_handler_del(E_DBus_Handler_Data *hd)
{
   DDBG("  FD handler del");
   hd->cd->fd_handlers = eina_list_remove(hd->cd->fd_handlers, hd);
   ecore_main_fd_handler_del(hd->fd_handler);
   free(hd);
}

/* watch */
static dbus_bool_t 
cb_watch_add(DBusWatch *watch, void *data)
{
   E_DBus_Connection *cd = data;

   DDBG("Watch add on fd: %d (flags: %d) enable: %d",
        dbus_watch_get_unix_fd(watch), dbus_watch_get_flags(watch),
        dbus_watch_get_enabled(watch));

   e_dbus_fd_handler_add(cd, watch);

   return true;
}

static void
cb_watch_del(DBusWatch *watch, void *data)
{
   E_DBus_Handler_Data *hd;

   DDBG("Watch del on fd: %d (flags: %d)", dbus_watch_get_unix_fd(watch),
        dbus_watch_get_flags(watch));

   hd = dbus_watch_get_data(watch);

   int dflags = dbus_watch_get_flags(watch);
   if (dflags & DBUS_WATCH_READABLE) hd->watch_read = NULL;
   if (dflags & DBUS_WATCH_WRITABLE) hd->watch_write = NULL;

   if (!hd->watch_read && !hd->watch_write)
      e_dbus_fd_handler_del(hd);
   else
      e_dbus_fd_handler_update(hd);
}

static void
cb_watch_toggle(DBusWatch *watch, void *data)
{
   E_DBus_Handler_Data *hd;

   DDBG("Watch toggle on fd: %d (flags: %d) enable: %d",
        dbus_watch_get_unix_fd(watch), dbus_watch_get_flags(watch),
        dbus_watch_get_enabled(watch));

   hd = dbus_watch_get_data(watch);
   e_dbus_fd_handler_update(hd);
}


/* timeout */
static Eina_Bool
e_dbus_timeout_handler(void *data)
{
   E_DBus_Timeout_Data *td;

   td = data;

   if (!dbus_timeout_get_enabled(td->timeout))
   {
      DBG("timeout_handler (not enabled, ending)");
      td->handler = NULL;
      return ECORE_CALLBACK_CANCEL;
   }

   DBG("timeout handler!");
   dbus_timeout_handle(td->timeout);
   return ECORE_CALLBACK_CANCEL;
}

static void
e_dbus_timeout_data_free(void *timeout_data)
{
   E_DBus_Timeout_Data *td = timeout_data;
   DBG("e_dbus_timeout_data_free");
   if (td->handler) ecore_timer_del(td->handler);
   free(td);
}

static dbus_bool_t 
cb_timeout_add(DBusTimeout *timeout, void *data)
{
   E_DBus_Connection *cd;
   E_DBus_Timeout_Data *td;

   cd = data;
   DBG("timeout add!");
   td = calloc(1, sizeof(E_DBus_Timeout_Data));
   td->cd = cd;
   dbus_timeout_set_data(timeout, (void *)td, e_dbus_timeout_data_free);

   td->interval = dbus_timeout_get_interval(timeout);
   td->timeout = timeout;

   if (dbus_timeout_get_enabled(timeout))
      td->handler = ecore_timer_add(td->interval, e_dbus_timeout_handler, td);
   td->cd->timeouts = eina_list_append(td->cd->timeouts, td->handler);

   return true;
}

static void
cb_timeout_del(DBusTimeout *timeout, void *data)
{
   E_DBus_Timeout_Data *td;
   DBG("timeout del!");

   td = (E_DBus_Timeout_Data *)dbus_timeout_get_data(timeout);

   if (td->handler) 
   {
      td->cd->timeouts = eina_list_remove(td->cd->timeouts, td->handler);
      ecore_timer_del(td->handler);
      td->handler = NULL;
   }

   /* Note: timeout data gets freed when the timeout itself is freed by dbus */
}

static void
cb_timeout_toggle(DBusTimeout *timeout, void *data)
{
   E_DBus_Timeout_Data *td;
   DBG("timeout toggle!");

   td = (E_DBus_Timeout_Data *)dbus_timeout_get_data(timeout);

   if (dbus_timeout_get_enabled(td->timeout))
   {
      td->interval = dbus_timeout_get_interval(timeout);
      td->handler = ecore_timer_add(td->interval, e_dbus_timeout_handler, td);
   }
   else
   {
      ecore_timer_del(td->handler);
      td->handler = NULL;
   }
}


/* dbus connection */
static E_DBus_Connection *
e_dbus_connection_new(DBusConnection *conn)
{
   E_DBus_Connection *cd;

   cd = calloc(1, sizeof(E_DBus_Connection));
   if (!cd) return NULL;
   cd->conn = conn;

   return cd;
}

static void
e_dbus_connection_free(void *data)
{
   E_DBus_Connection *cd = data;
   E_DBus_Handler_Data *hd;
   Ecore_Timer *timer;
   DBG("e_dbus_connection free!");

   EINA_LIST_FREE(cd->fd_handlers, hd)
      e_dbus_fd_handler_del(hd);

   EINA_LIST_FREE(cd->timeouts, timer)
      ecore_timer_del(timer);

   if (cd->idler) ecore_idler_del(cd->idler);

   free(cd);
}


/* public functions */
int
e_dbus_init(void)
{
   if (++_edbus_init_count != 1)
      return _edbus_init_count;
  
   if (!eina_init())
   {
      fprintf(stderr,"E-dbus: Enable to initialize eina\n");
      return --_edbus_init_count;
   }

   _e_dbus_log_dom = eina_log_domain_register("e_dbus", E_DBUS_COLOR_DEFAULT);
   if (_e_dbus_log_dom < 0)
   {
      EINA_LOG_ERR("Unable to create an 'e_dbus' log domain");
      eina_shutdown();
      return --_edbus_init_count;
   }

   if (!ecore_init())
   {
      ERR("E-dbus: Unable to initialize ecore");
      eina_shutdown();
      return --_edbus_init_count;
   }

   return _edbus_init_count;
}

int
e_dbus_shutdown(void)
{
   if (_edbus_init_count <= 0)
   {
      EINA_LOG_ERR("Init count not greater than 0 in shutdown.");
      return 0;
   }
   if (--_edbus_init_count)
      return _edbus_init_count;

   ecore_shutdown();
   eina_log_domain_unregister(_e_dbus_log_dom);
   _e_dbus_log_dom = -1;
   eina_shutdown();

   return _edbus_init_count;
}

E_DBus_Connection *
e_dbus_connection_setup(DBusConnection *conn)
{
   E_DBus_Connection *cd;

   cd = e_dbus_connection_new(conn);
   if (!cd) return NULL;

   /* connection_setup */
   dbus_connection_set_exit_on_disconnect(cd->conn, EINA_FALSE);
   dbus_connection_allocate_data_slot(&connection_slot);

   dbus_connection_set_data(cd->conn, connection_slot, (void *)cd,
                            e_dbus_connection_free);
   dbus_connection_set_watch_functions(cd->conn,
                                       cb_watch_add,
                                       cb_watch_del,
                                       cb_watch_toggle,
                                       cd,
                                       NULL);

   dbus_connection_set_timeout_functions(cd->conn,
                                         cb_timeout_add,
                                         cb_timeout_del,
                                         cb_timeout_toggle,
                                         cd,
                                         NULL);

   dbus_connection_set_dispatch_status_function(cd->conn, cb_dispatch_status, cd, NULL);
   cb_dispatch_status(cd->conn, dbus_connection_get_dispatch_status(cd->conn), cd);

   return cd;
}

void
e_dbus_connection_close(E_DBus_Connection *conn)
{
   if (!conn) return;
   DBG("e_dbus_connection_close");

   if (e_dbus_idler_active)
   {
      close_connection++;
      return;
   }
   if (--(conn->refcount) != 0) return;

   dbus_connection_free_data_slot(&connection_slot);
   dbus_connection_set_watch_functions(conn->conn,
                                       NULL,
                                       NULL,
                                       NULL,
                                       NULL, NULL);

   dbus_connection_set_timeout_functions(conn->conn,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL, NULL);

   dbus_connection_set_dispatch_status_function(conn->conn, NULL, NULL, NULL);

   /* Idler functin must be cancelled when dbus connection is  unreferenced */
   if (conn->idler)
   {
      ecore_idler_del(conn->idler);
      conn->idler = NULL;
   }

   dbus_connection_close(conn->conn);
   dbus_connection_unref(conn->conn);

   // Note: the E_DBus_Connection gets freed when the dbus_connection is cleaned up by the previous unref
}


#undef DDBG