Tizen 2.1 release
[platform/core/uifw/e17.git] / src / modules / bluez / e_mod_main.c
1 /*
2  * TODO:
3  *
4  *   HIGH:
5  *
6  *     - check why return NULL from method call triggers cancel error
7  *       after timeout.
8  *     - find out why alias == address in _bluez_request_pincode_cb
9  *     - more complete agent support (handle requests from devices)
10  *     - handle device-disappeared events
11  *     - icon with device state (trusted, connected, paired)
12  *
13  *   LOW:
14  *
15  *     - configure (probably module) timeout to trigger automatic rescan.
16  *     - gadgets to show different adapters (see mixer module configuration)
17  *     - module to choose the default adapter (see mixer module configuration)
18  *     - icon with device class
19  */
20 #include "e.h"
21 #include "e_mod_main.h"
22
23 static E_Module *bluez_mod = NULL;
24 static char tmpbuf[4096]; /* general purpose buffer, just use immediately */
25
26 static const char _e_bluez_agent_path[] = "/org/enlightenment/bluez/Agent";
27 const char _e_bluez_name[] = "bluez";
28 const char _e_bluez_Name[] = "Bluetooth Manager";
29 int _e_bluez_log_dom = -1;
30
31 static void _bluez_gadget_update(E_Bluez_Instance *inst);
32 static void _bluez_tip_update(E_Bluez_Instance *inst);
33 static void _bluez_popup_update(E_Bluez_Instance *inst);
34
35 struct bluez_pincode_data
36 {
37    void                    (*cb)(struct bluez_pincode_data *d);
38    DBusMessage            *msg;
39    E_Bluez_Module_Context *ctxt;
40    char                   *pincode;
41    const char             *alias;
42    E_Dialog               *dia;
43    Evas_Object            *entry;
44    Eina_Bool               canceled;
45 };
46
47 const char *
48 e_bluez_theme_path(void)
49 {
50 #define TF "/e-module-bluez.edj"
51    size_t dirlen;
52
53    dirlen = strlen(bluez_mod->dir);
54    if (dirlen >= sizeof(tmpbuf) - sizeof(TF))
55      return NULL;
56
57    memcpy(tmpbuf, bluez_mod->dir, dirlen);
58    memcpy(tmpbuf + dirlen, TF, sizeof(TF));
59
60    return tmpbuf;
61 #undef TF
62 }
63
64 static void
65 _bluez_devices_clear(E_Bluez_Instance *inst)
66 {
67    E_Bluez_Instance_Device *d;
68    EINA_LIST_FREE(inst->devices, d)
69      {
70         eina_stringshare_del(d->address);
71         eina_stringshare_del(d->alias);
72         free(d);
73      }
74    inst->address = NULL;
75    inst->alias = NULL;
76 }
77
78 static void
79 _bluez_discovery_cb(void            *data,
80                     DBusMessage *msg __UNUSED__,
81                     DBusError       *error)
82 {
83    E_Bluez_Instance *inst = data;
84    char *label;
85
86    if (error && dbus_error_is_set(error))
87      {
88         _bluez_dbus_error_show(_("Cannot change adapter's discovery."), error);
89         dbus_error_free(error);
90         return;
91      }
92
93    inst->discovering = !inst->discovering;
94
95    label = !inst->discovering ? _("Start Scan") : _("Stop Scan");
96    e_widget_button_label_set(inst->ui.button, label);
97 }
98
99 static void
100 _bluez_create_paired_device_cb(void            *data,
101                                DBusMessage *msg __UNUSED__,
102                                DBusError       *error)
103 {
104    const char *alias = data;
105
106    if (error && dbus_error_is_set(error))
107      {
108         if (strcmp(error->name, "org.bluez.Error.AlreadyExists") != 0)
109           _bluez_dbus_error_show(_("Cannot pair with device."), error);
110         dbus_error_free(error);
111         eina_stringshare_del(alias);
112         return;
113      }
114
115    e_util_dialog_show
116      (_("Bluetooth Manager"), _("Device '%s' successfully paired."), alias);
117    eina_stringshare_del(alias);
118 }
119
120 static void
121 _bluez_toggle_powered_cb(void            *data,
122                          DBusMessage *msg __UNUSED__,
123                          DBusError       *error)
124 {
125    E_Bluez_Instance *inst = data;
126
127    if ((!error) || (!dbus_error_is_set(error)))
128      {
129         inst->powered_pending = EINA_FALSE;
130         inst->powered = !inst->powered;
131
132         if (!inst->powered)
133           {
134              _bluez_devices_clear(inst);
135
136              if (inst->popup)
137                _bluez_popup_update(inst);
138           }
139
140         _bluez_gadget_update(inst);
141         return;
142      }
143
144    _bluez_dbus_error_show(_("Cannot toggle adapter's powered."), error);
145    dbus_error_free(error);
146 }
147
148 void
149 _bluez_toggle_powered(E_Bluez_Instance *inst)
150 {
151    Eina_Bool powered;
152
153    if ((!inst) || (!inst->ctxt->has_manager))
154      {
155         _bluez_operation_error_show(_("BlueZ Daemon is not running."));
156         return;
157      }
158
159    if (!inst->adapter)
160      {
161         _bluez_operation_error_show(_("No bluetooth adapter."));
162         return;
163      }
164
165    if (!e_bluez_adapter_powered_get(inst->adapter, &powered))
166      {
167         _bluez_operation_error_show(_("Query adapter's powered."));
168         return;
169      }
170
171    powered = !powered;
172
173    if (!e_bluez_adapter_powered_set
174          (inst->adapter, powered, _bluez_toggle_powered_cb, inst))
175      {
176         _bluez_operation_error_show(_("Query adapter's powered."));
177         return;
178      }
179 }
180
181 static void
182 _bluez_cb_toggle_powered(E_Object *obj      __UNUSED__,
183                          const char *params __UNUSED__)
184 {
185    E_Bluez_Module_Context *ctxt;
186    const Eina_List *l;
187    E_Bluez_Instance *inst;
188
189    if (!bluez_mod)
190      return;
191
192    ctxt = bluez_mod->data;
193    EINA_LIST_FOREACH(ctxt->instances, l, inst)
194      if (inst->adapter) _bluez_toggle_powered(inst);
195 }
196
197 static void _bluez_popup_del(E_Bluez_Instance *inst);
198
199 static Eina_Bool
200 _bluez_popup_input_window_mouse_up_cb(void    *data,
201                                       int type __UNUSED__,
202                                       void    *event)
203 {
204    Ecore_Event_Mouse_Button *ev = event;
205    E_Bluez_Instance *inst = data;
206
207    if (ev->window != inst->ui.input.win)
208      return ECORE_CALLBACK_PASS_ON;
209
210    _bluez_popup_del(inst);
211
212    return ECORE_CALLBACK_PASS_ON;
213 }
214
215 static Eina_Bool
216 _bluez_popup_input_window_key_down_cb(void    *data,
217                                       int type __UNUSED__,
218                                       void    *event)
219 {
220    Ecore_Event_Key *ev = event;
221    E_Bluez_Instance *inst = data;
222    const char *keysym;
223
224    if (ev->window != inst->ui.input.win)
225      return ECORE_CALLBACK_PASS_ON;
226
227    keysym = ev->key;
228    if (strcmp(keysym, "Escape") == 0)
229      _bluez_popup_del(inst);
230
231    return ECORE_CALLBACK_PASS_ON;
232 }
233
234 static void
235 _bluez_popup_input_window_destroy(E_Bluez_Instance *inst)
236 {
237    ecore_x_window_free(inst->ui.input.win);
238    inst->ui.input.win = 0;
239
240    ecore_event_handler_del(inst->ui.input.mouse_up);
241    inst->ui.input.mouse_up = NULL;
242
243    ecore_event_handler_del(inst->ui.input.key_down);
244    inst->ui.input.key_down = NULL;
245 }
246
247 static void
248 _bluez_popup_input_window_create(E_Bluez_Instance *inst)
249 {
250    Ecore_X_Window_Configure_Mask mask;
251    Ecore_X_Window w, popup_w;
252    E_Manager *man;
253
254    man = e_manager_current_get();
255
256    w = ecore_x_window_input_new(man->root, 0, 0, man->w, man->h);
257    mask = (ECORE_X_WINDOW_CONFIGURE_MASK_STACK_MODE |
258            ECORE_X_WINDOW_CONFIGURE_MASK_SIBLING);
259    popup_w = inst->popup->win->evas_win;
260    ecore_x_window_configure(w, mask, 0, 0, 0, 0, 0, popup_w,
261                             ECORE_X_WINDOW_STACK_BELOW);
262    ecore_x_window_show(w);
263
264    inst->ui.input.mouse_up =
265      ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP,
266                              _bluez_popup_input_window_mouse_up_cb, inst);
267
268    inst->ui.input.key_down =
269      ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
270                              _bluez_popup_input_window_key_down_cb, inst);
271
272    inst->ui.input.win = w;
273 }
274
275 static void
276 _bluez_popup_cb_powered_changed(void        *data,
277                                 Evas_Object *obj)
278 {
279    E_Bluez_Instance *inst = data;
280    E_Bluez_Module_Context *ctxt = inst->ctxt;
281    Eina_Bool powered = e_widget_check_checked_get(obj);
282
283    if ((!ctxt) || (!ctxt->has_manager))
284      {
285         _bluez_operation_error_show(_("BlueZ Daemon is not running."));
286         return;
287      }
288
289    if (!inst->adapter)
290      {
291         _bluez_operation_error_show(_("No bluetooth adapter."));
292         return;
293      }
294
295    if (!e_bluez_adapter_powered_set
296          (inst->adapter, powered, _bluez_toggle_powered_cb, inst))
297      {
298         _bluez_operation_error_show
299           (_("Cannot toggle adapter's powered."));
300         return;
301      }
302
303    inst->powered_pending = EINA_TRUE;
304 }
305
306 static void
307 _bluez_pincode_ask_cb(struct bluez_pincode_data *d)
308 {
309    DBusMessage *reply;
310
311    if (!d->pincode)
312      {
313         e_util_dialog_show(_("Bluetooth Manager"), _("Invalid Pin Code."));
314         return;
315      }
316
317    reply = dbus_message_new_method_return(d->msg);
318    dbus_message_append_args
319      (reply, DBUS_TYPE_STRING, &d->pincode, DBUS_TYPE_INVALID);
320
321    dbus_message_set_no_reply(reply, EINA_TRUE);
322    e_dbus_message_send(d->ctxt->agent.conn, reply, NULL, -1, NULL);
323 }
324
325 static void
326 bluez_pincode_ask_ok(void     *data,
327                      E_Dialog *dia)
328 {
329    struct bluez_pincode_data *d = data;
330    d->canceled = EINA_FALSE;
331    e_object_del(E_OBJECT(dia));
332 }
333
334 static void
335 bluez_pincode_ask_cancel(void     *data,
336                          E_Dialog *dia)
337 {
338    struct bluez_pincode_data *d = data;
339    d->canceled = EINA_TRUE;
340    e_object_del(E_OBJECT(dia));
341 }
342
343 static void
344 bluez_pincode_ask_del(void *data)
345 {
346    E_Dialog *dia = data;
347    struct bluez_pincode_data *d = e_object_data_get(E_OBJECT(dia));
348
349    if (!d->canceled)
350      d->cb(d);
351
352    d->ctxt->agent.pending = eina_list_remove(d->ctxt->agent.pending, dia);
353
354    free(d->pincode);
355    dbus_message_unref(d->msg);
356    eina_stringshare_del(d->alias);
357    E_FREE(d);
358 }
359
360 static void
361 bluez_pincode_ask_key_down(void          *data,
362                            Evas *e        __UNUSED__,
363                            Evas_Object *o __UNUSED__,
364                            void          *event)
365 {
366    Evas_Event_Key_Down *ev = event;
367    struct bluez_pincode_data *d = data;
368
369    if (strcmp(ev->keyname, "Return") == 0)
370      bluez_pincode_ask_ok(d, d->dia);
371    else if (strcmp(ev->keyname, "Escape") == 0)
372      bluez_pincode_ask_cancel(d, d->dia);
373 }
374
375 static void
376 bluez_pincode_ask(void                    (*cb)(struct bluez_pincode_data *),
377                   DBusMessage            *msg,
378                   const char             *alias,
379                   E_Bluez_Module_Context *ctxt)
380 {
381    struct bluez_pincode_data *d;
382    Evas_Object *list, *o;
383    Evas *evas;
384    char buf[512];
385    int mw, mh;
386
387    if (!cb)
388      return;
389
390    d = E_NEW(struct bluez_pincode_data, 1);
391    if (!d)
392      return;
393
394    d->cb = cb;
395    d->ctxt = ctxt;
396    d->alias = eina_stringshare_add(alias);
397    d->msg = dbus_message_ref(msg);
398    d->canceled = EINA_TRUE; /* closing the dialog defaults to cancel */
399    d->dia = e_dialog_new(NULL, "E", "bluez_ask_pincode");
400
401    snprintf(buf, sizeof(buf), _("Pairing with device '%s'"), alias);
402    e_dialog_title_set(d->dia, buf);
403    e_dialog_icon_set(d->dia, "dialog-ask", 32);
404    e_dialog_border_icon_set(d->dia, "dialog-ask");
405
406    evas = d->dia->win->evas;
407
408    list = e_widget_list_add(evas, 0, 0);
409
410    o = edje_object_add(evas);
411    e_theme_edje_object_set(o, "base/theme/dialog",
412                            "e/widgets/dialog/text");
413    snprintf(buf, sizeof(buf),
414             _("Enter the PIN code: "));
415    edje_object_part_text_set(o, "e.textblock.message", buf);
416    edje_object_size_min_calc(o, &mw, &mh);
417    evas_object_size_hint_min_set(o, mw, mh);
418    evas_object_resize(o, mw, mh);
419    evas_object_show(o);
420    e_widget_list_object_append(list, o, 1, 1, 0.5);
421
422    d->entry = o = e_widget_entry_add(evas, &d->pincode, NULL, NULL, NULL);
423    e_widget_entry_password_set(o, 0);
424    evas_object_show(o);
425    e_widget_list_object_append(list, o, 1, 0, 0.0);
426
427    e_widget_size_min_get(list, &mw, &mh);
428    if (mw < 200)
429      mw = 200;
430    if (mh < 60)
431      mh = 60;
432    e_dialog_content_set(d->dia, list, mw, mh);
433
434    e_dialog_button_add
435      (d->dia, _("Ok"), NULL, bluez_pincode_ask_ok, d);
436    e_dialog_button_add
437      (d->dia, _("Cancel"), NULL, bluez_pincode_ask_cancel, d);
438
439    evas_object_event_callback_add
440      (d->dia->bg_object, EVAS_CALLBACK_KEY_DOWN,
441      bluez_pincode_ask_key_down, d);
442
443    e_object_del_attach_func_set
444      (E_OBJECT(d->dia), bluez_pincode_ask_del);
445    e_object_data_set(E_OBJECT(d->dia), d);
446
447    e_dialog_button_focus_num(d->dia, 0);
448    e_widget_focus_set(d->entry, 1);
449
450    e_win_centered_set(d->dia->win, 1);
451    e_dialog_show(d->dia);
452
453    ctxt->agent.pending = eina_list_append(ctxt->agent.pending, d->dia);
454 }
455
456 static DBusMessage *
457 _bluez_request_pincode_cb(E_DBus_Object *obj,
458                           DBusMessage   *msg)
459 {
460    E_Bluez_Module_Context *ctxt = e_dbus_object_data_get(obj);
461    E_Bluez_Element *element;
462    const char *path;
463    const char *alias;
464
465    // TODO: seems that returning NULL is causing pin code rquest to be canceled!
466
467    if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
468                              DBUS_TYPE_INVALID) == FALSE)
469      return NULL;
470
471    element = e_bluez_device_get(path);
472    if (!element)
473      alias = path;
474    else
475      {
476         if (!e_bluez_device_alias_get(element, &alias))
477           {
478              if (!e_bluez_device_name_get(element, &alias))
479                alias = path;
480           }
481      }
482    // TODO: find out why alias == address, then remove debug:
483    fprintf(stderr, ">>> request pin code of '%s' (%s)\n", alias, path);
484    bluez_pincode_ask(_bluez_pincode_ask_cb, msg, alias, ctxt);
485    return NULL;
486 }
487
488 static void
489 _bluez_popup_cb_scan(void       *data,
490                      void *data2 __UNUSED__)
491 {
492    E_Bluez_Instance *inst = data;
493    int ret;
494
495    if (!inst->adapter)
496      ret = 0;
497    else if (inst->discovering)
498      ret = e_bluez_adapter_stop_discovery
499          (inst->adapter, _bluez_discovery_cb, inst);
500    else
501      {
502         inst->last_scan = ecore_loop_time_get();
503
504         _bluez_devices_clear(inst);
505
506         ret = e_bluez_adapter_start_discovery
507             (inst->adapter, _bluez_discovery_cb, inst);
508
509         _bluez_popup_update(inst);
510      }
511
512    if (!ret)
513      ERR("Failed on discovery procedure");
514 }
515
516 static void
517 _bluez_popup_cb_controls(void       *data,
518                          void *data2 __UNUSED__)
519 {
520    E_Bluez_Instance *inst = data;
521    if (inst->popup)
522      _bluez_popup_del(inst);
523    if (inst->conf_dialog)
524      return;
525    if (!inst->adapter)
526      return;
527    inst->conf_dialog = e_bluez_config_dialog_new(NULL, inst);
528 }
529
530 static void
531 _bluez_popup_device_selected(void *data)
532 {
533    E_Bluez_Instance *inst = data;
534    const char *address = inst->address;
535    const char *alias;
536    const char *cap = "DisplayYesNo";
537    const E_Bluez_Instance_Device *d;
538    const Eina_List *l;
539
540    if (inst->popup)
541      _bluez_popup_del(inst);
542
543    if (!address)
544      {
545         ERR("no device selected for pairing.");
546         return;
547      }
548
549    inst->alias = address;
550    EINA_LIST_FOREACH(inst->devices, l, d)
551      {
552         if (address == d->alias)
553           {
554              inst->alias = d->alias;
555              break;
556           }
557      }
558
559    if (!inst->alias)
560      {
561         ERR("device %s does not have an alias.", address);
562         return;
563      }
564
565    alias = eina_stringshare_ref(inst->alias);
566    if (!e_bluez_adapter_create_paired_device
567          (inst->adapter, _e_bluez_agent_path, cap, address,
568          _bluez_create_paired_device_cb, alias))
569      {
570         eina_stringshare_del(alias);
571         return;
572      }
573 }
574
575 static Eina_Bool
576 _bluez_event_devicefound(void       *data,
577                          int type    __UNUSED__,
578                          void *event)
579 {
580    E_Bluez_Module_Context *ctxt = data;
581    E_Bluez_Device_Found *device = event;
582    E_Bluez_Instance *inst;
583    const Eina_List *l_inst;
584    const char *alias;
585
586    // TODO: get properties such as paired, connected, trusted, class, icon...
587    // TODO: check if the adapter contains device->name and if so get path.
588
589    alias = e_bluez_devicefound_alias_get(device);
590
591    EINA_LIST_FOREACH(ctxt->instances, l_inst, inst)
592      {
593         const Eina_List *l_dev;
594         E_Bluez_Instance_Device *dev;
595         Eina_Bool found = EINA_FALSE;
596
597         if (inst->adapter != device->adapter) continue;
598
599         EINA_LIST_FOREACH(inst->devices, l_dev, dev)
600           {
601              if (dev->address == device->name)
602                {
603                   found = EINA_TRUE;
604                   break;
605                }
606           }
607
608         if (found) continue;
609
610         dev = malloc(sizeof(E_Bluez_Instance_Device));
611         if (!dev) continue;
612
613         dev->address = eina_stringshare_ref(device->name);
614         dev->alias = eina_stringshare_ref(alias);
615
616         inst->devices = eina_list_append(inst->devices, dev);
617
618         if (inst->ui.list)
619           {
620              e_widget_ilist_append
621                (inst->ui.list, NULL, dev->alias,
622                _bluez_popup_device_selected, inst, dev->address);
623              e_widget_ilist_go(inst->ui.list);
624           }
625      }
626
627    return 1;
628 }
629
630 static void
631 _bluez_popup_update(E_Bluez_Instance *inst)
632 {
633    Evas_Object *list = inst->ui.list;
634    int selected;
635    const char *label;
636    E_Bluez_Instance_Device *d;
637    Eina_List *l;
638
639    /* TODO: replace this with a scroller + list of edje
640     * objects that are more full of features
641     */
642    selected = e_widget_ilist_selected_get(list);
643    e_widget_ilist_freeze(list);
644    e_widget_ilist_clear(list);
645
646    EINA_LIST_FOREACH(inst->devices, l, d)
647      {
648         e_widget_ilist_append
649           (inst->ui.list, NULL, d->alias,
650           _bluez_popup_device_selected, inst, d->address);
651      }
652
653    if (selected >= 0)
654      {
655         inst->first_selection = EINA_TRUE;
656         e_widget_ilist_selected_set(list, selected);
657      }
658    else
659      inst->first_selection = EINA_FALSE;
660
661    e_widget_ilist_go(list);
662
663    e_widget_check_checked_set(inst->ui.powered, inst->powered);
664    label = inst->discovering ? _("Stop Scan") : _("Start Scan");
665    e_widget_button_label_set(inst->ui.button, label);
666    e_widget_disabled_set(inst->ui.button, !inst->powered);
667 }
668
669 static void
670 _bluez_popup_del(E_Bluez_Instance *inst)
671 {
672    _bluez_popup_input_window_destroy(inst);
673    e_object_del(E_OBJECT(inst->popup));
674    inst->popup = NULL;
675 }
676
677 static void
678 _bluez_popup_new(E_Bluez_Instance *inst)
679 {
680    Evas_Object *ol;
681    Evas *evas;
682    Evas_Coord mw, mh;
683    const char *label;
684    Eina_Bool b, needs_scan = EINA_FALSE;
685
686    if (inst->popup)
687      {
688         e_gadcon_popup_show(inst->popup);
689         return;
690      }
691
692    if (!inst->adapter)
693      {
694         _bluez_operation_error_show(_("No bluetooth adapter."));
695         return;
696      }
697
698    if (!e_bluez_adapter_discovering_get(inst->adapter, &b))
699      {
700         _bluez_operation_error_show(_("Can't get Discovering property"));
701         return;
702      }
703    inst->discovering = b;
704    // maybe auto-scan if did not in the last 30 minutes?
705    // seems scan will hurt things like bluetooth audio playback, so don't do it
706    if ((!inst->discovering) && (inst->last_scan <= 0.0) && (inst->ui.powered))
707      {
708         label = _("Stop Scan");
709         needs_scan = EINA_TRUE;
710      }
711    else
712      label = inst->discovering ? _("Stop Scan") : _("Start Scan");
713
714    inst->popup = e_gadcon_popup_new(inst->gcc);
715    evas = inst->popup->win->evas;
716
717    ol = e_widget_list_add(evas, 0, 0);
718
719    // TODO: get this size from edj
720    inst->ui.list = e_widget_ilist_add(evas, 32, 32, &inst->address);
721    e_widget_size_min_set(inst->ui.list, 180, 100);
722    e_widget_list_object_append(ol, inst->ui.list, 1, 1, 0.5);
723
724    inst->powered = inst->powered;
725    inst->ui.powered = e_widget_check_add(evas, _("Powered"), &inst->powered);
726    e_widget_on_change_hook_set
727      (inst->ui.powered, _bluez_popup_cb_powered_changed, inst);
728    e_widget_list_object_append(ol, inst->ui.powered, 1, 0, 0.5);
729
730    inst->ui.button = e_widget_button_add
731        (evas, label, NULL, _bluez_popup_cb_scan, inst, NULL);
732    e_widget_list_object_append(ol, inst->ui.button, 1, 0, 0.5);
733
734    inst->ui.control = e_widget_button_add
735        (evas, _("Controls"), NULL, _bluez_popup_cb_controls, inst, NULL);
736    e_widget_list_object_append(ol, inst->ui.control, 1, 0, 0.5);
737
738    _bluez_popup_update(inst);
739
740    e_widget_size_min_get(ol, &mw, &mh);
741    if (mh < 200) mh = 200;
742    if (mw < 200) mw = 200;
743    e_widget_size_min_set(ol, mw, mh);
744
745    e_gadcon_popup_content_set(inst->popup, ol);
746    e_gadcon_popup_show(inst->popup);
747    _bluez_popup_input_window_create(inst);
748
749    if (needs_scan) _bluez_popup_cb_scan(inst, NULL);
750 }
751
752 static void
753 _bluez_menu_cb_cfg(void           *data,
754                    E_Menu *menu    __UNUSED__,
755                    E_Menu_Item *mi __UNUSED__)
756 {
757    E_Bluez_Instance *inst = data;
758    if (inst->popup)
759      _bluez_popup_del(inst);
760    if (inst->conf_dialog)
761      return;
762    if (!inst->adapter)
763      return;
764    inst->conf_dialog = e_bluez_config_dialog_new(NULL, inst);
765 }
766
767 static void
768 _bluez_menu_new(E_Bluez_Instance      *inst,
769                 Evas_Event_Mouse_Down *ev)
770 {
771    E_Zone *zone;
772    E_Menu *m;
773    E_Menu_Item *mi;
774    int x, y;
775
776    zone = e_util_zone_current_get(e_manager_current_get());
777
778    m = e_menu_new();
779    mi = e_menu_item_new(m);
780    e_menu_item_label_set(mi, _("Settings"));
781    e_util_menu_item_theme_icon_set(mi, "configure");
782    e_menu_item_callback_set(mi, _bluez_menu_cb_cfg, inst);
783
784    m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
785
786    e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, NULL, NULL);
787    e_menu_activate_mouse(m, zone, x + ev->output.x, y + ev->output.y,
788                          1, 1, E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
789    evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button,
790                             EVAS_BUTTON_NONE, ev->timestamp, NULL);
791 }
792
793 static void
794 _bluez_tip_new(E_Bluez_Instance *inst)
795 {
796    Evas *e;
797
798    inst->tip = e_gadcon_popup_new(inst->gcc);
799    if (!inst->tip) return;
800
801    e = inst->tip->win->evas;
802
803    inst->o_tip = edje_object_add(e);
804    e_theme_edje_object_set(inst->o_tip, "base/theme/modules/bluez/tip",
805                            "e/modules/bluez/tip");
806
807    _bluez_tip_update(inst);
808
809    e_gadcon_popup_content_set(inst->tip, inst->o_tip);
810    e_gadcon_popup_show(inst->tip);
811 }
812
813 static void
814 _bluez_tip_del(E_Bluez_Instance *inst)
815 {
816    evas_object_del(inst->o_tip);
817    e_object_del(E_OBJECT(inst->tip));
818    inst->tip = NULL;
819    inst->o_tip = NULL;
820 }
821
822 static void
823 _bluez_cb_mouse_down(void            *data,
824                      Evas *evas       __UNUSED__,
825                      Evas_Object *obj __UNUSED__,
826                      void            *event)
827 {
828    E_Bluez_Instance *inst;
829    Evas_Event_Mouse_Down *ev;
830
831    inst = data;
832    if (!inst)
833      return;
834
835    ev = event;
836    if (ev->button == 1)
837      {
838         if (!inst->popup)
839           _bluez_popup_new(inst);
840         else
841           _bluez_popup_del(inst);
842      }
843    else if (ev->button == 2)
844      _bluez_toggle_powered(inst);
845    else if (ev->button == 3)
846      _bluez_menu_new(inst, ev);
847 }
848
849 static void
850 _bluez_cb_mouse_in(void            *data,
851                    Evas *evas       __UNUSED__,
852                    Evas_Object *obj __UNUSED__,
853                    void *event      __UNUSED__)
854 {
855    E_Bluez_Instance *inst = data;
856
857    if (inst->tip)
858      return;
859
860    _bluez_tip_new(inst);
861 }
862
863 static void
864 _bluez_cb_mouse_out(void            *data,
865                     Evas *evas       __UNUSED__,
866                     Evas_Object *obj __UNUSED__,
867                     void *event      __UNUSED__)
868 {
869    E_Bluez_Instance *inst = data;
870
871    if (!inst->tip)
872      return;
873
874    _bluez_tip_del(inst);
875 }
876
877 static void
878 _bluez_edje_view_update(E_Bluez_Instance *inst,
879                         Evas_Object      *o)
880 {
881    E_Bluez_Module_Context *ctxt = inst->ctxt;
882    const char *name;
883
884    if (!ctxt->has_manager)
885      {
886         edje_object_part_text_set(o, "e.text.powered", "");
887         edje_object_part_text_set(o, "e.text.status", "");
888         edje_object_signal_emit(o, "e,changed,service,none", "e");
889         edje_object_part_text_set(o, "e.text.name", _("No Bluetooth daemon"));
890         edje_object_signal_emit(o, "e,changed,name", "e");
891         return;
892      }
893
894    if (!inst->adapter)
895      {
896         edje_object_part_text_set(o, "e.text.powered", "");
897         edje_object_part_text_set(o, "e.text.status", "");
898         edje_object_signal_emit(o, "e,changed,off", "e");
899         edje_object_part_text_set(o, "e.text.name", _("No Bluetooth adapter"));
900         edje_object_signal_emit(o, "e,changed,name", "e");
901         return;
902      }
903
904    if (!e_bluez_adapter_name_get(inst->adapter, &name))
905      name = "";
906    edje_object_part_text_set(o, "e.text.name", name);
907    edje_object_signal_emit(o, "e,changed,name", "e");
908
909    if (inst->powered)
910      {
911         if (inst->discoverable)
912           {
913              edje_object_signal_emit(o, "e,changed,powered", "e");
914              edje_object_part_text_set
915                (o, "e.text.status",
916                _("Bluetooth is powered and discoverable."));
917           }
918         else
919           {
920              edje_object_signal_emit(o, "e,changed,hidden", "e");
921              edje_object_part_text_set
922                (o, "e.text.status", _("Bluetooth is powered and hidden."));
923           }
924      }
925    else
926      {
927         edje_object_signal_emit(o, "e,changed,off", "e");
928         edje_object_part_text_set(o, "e.text.status", _("Bluetooth is off."));
929      }
930 }
931
932 static void
933 _bluez_tip_update(E_Bluez_Instance *inst)
934 {
935    _bluez_edje_view_update(inst, inst->o_tip);
936 }
937
938 static void
939 _bluez_gadget_update(E_Bluez_Instance *inst)
940 {
941    E_Bluez_Module_Context *ctxt = inst->ctxt;
942
943    if (inst->popup && ((!ctxt->has_manager) || (!inst->adapter)))
944      _bluez_popup_del(inst);
945
946    if (inst->popup)
947      _bluez_popup_update(inst);
948    if (inst->tip)
949      _bluez_tip_update(inst);
950
951    _bluez_edje_view_update(inst, inst->ui.gadget);
952 }
953
954 /* Gadcon Api Functions */
955
956 static E_Gadcon_Client *
957 _gc_init(E_Gadcon   *gc,
958          const char *name,
959          const char *id,
960          const char *style)
961 {
962    E_Bluez_Instance *inst;
963    E_Bluez_Module_Context *ctxt;
964
965    if (!bluez_mod)
966      return NULL;
967
968    ctxt = bluez_mod->data;
969
970    inst = E_NEW(E_Bluez_Instance, 1);
971    inst->ctxt = ctxt;
972    inst->ui.gadget = edje_object_add(gc->evas);
973    e_theme_edje_object_set(inst->ui.gadget, "base/theme/modules/bluez",
974                            "e/modules/bluez/main");
975
976    inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->ui.gadget);
977    inst->gcc->data = inst;
978
979    evas_object_event_callback_add
980      (inst->ui.gadget, EVAS_CALLBACK_MOUSE_DOWN, _bluez_cb_mouse_down, inst);
981    evas_object_event_callback_add
982      (inst->ui.gadget, EVAS_CALLBACK_MOUSE_IN, _bluez_cb_mouse_in, inst);
983    evas_object_event_callback_add
984      (inst->ui.gadget, EVAS_CALLBACK_MOUSE_OUT, _bluez_cb_mouse_out, inst);
985
986    // TODO: instead of getting the default adapter, get the adapter for
987    // each instance. See the mixer module.
988    if (ctxt->default_adapter)
989      inst->adapter = e_bluez_adapter_get(ctxt->default_adapter);
990    else
991      inst->adapter = NULL;
992
993    if (inst->adapter)
994      {
995         Eina_Bool powered, discoverable, discovering;
996
997         if (e_bluez_adapter_powered_get(inst->adapter, &powered))
998           inst->powered = powered;
999
1000         if (e_bluez_adapter_discoverable_get(inst->adapter, &discoverable))
1001           inst->discoverable = discoverable;
1002
1003         if (e_bluez_adapter_discovering_get(inst->adapter, &discovering))
1004           inst->discovering = discovering;
1005      }
1006
1007    _bluez_gadget_update(inst);
1008
1009    ctxt->instances = eina_list_append(ctxt->instances, inst);
1010
1011    return inst->gcc;
1012 }
1013
1014 static void
1015 _gc_shutdown(E_Gadcon_Client *gcc)
1016 {
1017    E_Bluez_Module_Context *ctxt;
1018    E_Bluez_Instance *inst;
1019
1020    if (!bluez_mod)
1021      return;
1022
1023    ctxt = bluez_mod->data;
1024    if (!ctxt)
1025      return;
1026
1027    inst = gcc->data;
1028    if (!inst)
1029      return;
1030
1031    evas_object_del(inst->ui.gadget);
1032
1033    _bluez_devices_clear(inst);
1034
1035    ctxt->instances = eina_list_remove(ctxt->instances, inst);
1036
1037    E_FREE(inst);
1038 }
1039
1040 static void
1041 _gc_orient(E_Gadcon_Client       *gcc,
1042            E_Gadcon_Orient orient __UNUSED__)
1043 {
1044    e_gadcon_client_aspect_set(gcc, 16, 16);
1045    e_gadcon_client_min_size_set(gcc, 16, 16);
1046 }
1047
1048 static const char *
1049 _gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__)
1050 {
1051    return _(_e_bluez_Name);
1052 }
1053
1054 static Evas_Object *
1055 _gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__,
1056          Evas                               *evas)
1057 {
1058    Evas_Object *o;
1059
1060    o = edje_object_add(evas);
1061    edje_object_file_set(o, e_bluez_theme_path(), "icon");
1062    return o;
1063 }
1064
1065 static const char *
1066 _gc_id_new(const E_Gadcon_Client_Class *client_class __UNUSED__)
1067 {
1068    E_Bluez_Module_Context *ctxt;
1069
1070    if (!bluez_mod)
1071      return NULL;
1072
1073    ctxt = bluez_mod->data;
1074    if (!ctxt)
1075      return NULL;
1076
1077    snprintf(tmpbuf, sizeof(tmpbuf), "bluez.%d",
1078             eina_list_count(ctxt->instances));
1079    return tmpbuf;
1080 }
1081
1082 static const E_Gadcon_Client_Class _gc_class =
1083 {
1084    GADCON_CLIENT_CLASS_VERSION, _e_bluez_name,
1085    {
1086       _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
1087       e_gadcon_site_is_not_toolbar
1088    },
1089    E_GADCON_CLIENT_STYLE_PLAIN
1090 };
1091
1092 EAPI E_Module_Api e_modapi = {E_MODULE_API_VERSION, _e_bluez_Name};
1093
1094 static const char _act_toggle_powered[] = "toggle_powered";
1095 static const char _lbl_toggle_powered[] = "Toggle Powered";
1096
1097 static void
1098 _bluez_actions_register(E_Bluez_Module_Context *ctxt)
1099 {
1100    ctxt->actions.toggle_powered = e_action_add(_act_toggle_powered);
1101    if (ctxt->actions.toggle_powered)
1102      {
1103         ctxt->actions.toggle_powered->func.go =
1104           _bluez_cb_toggle_powered;
1105         e_action_predef_name_set
1106           (_(_e_bluez_Name), _(_lbl_toggle_powered), _act_toggle_powered,
1107           NULL, NULL, 0);
1108      }
1109 }
1110
1111 static void
1112 _bluez_actions_unregister(E_Bluez_Module_Context *ctxt)
1113 {
1114    if (ctxt->actions.toggle_powered)
1115      {
1116         e_action_predef_name_del(_(_e_bluez_Name), _(_lbl_toggle_powered));
1117         e_action_del(_act_toggle_powered);
1118      }
1119 }
1120
1121 static Eina_Bool
1122 _bluez_manager_changed_do(void *data)
1123 {
1124    E_Bluez_Module_Context *ctxt = data;
1125
1126    //FIXME: reload the default adapter maybe?
1127
1128    ctxt->poller.manager_changed = NULL;
1129    return ECORE_CALLBACK_CANCEL;
1130 }
1131
1132 static void
1133 _bluez_manager_changed(void                          *data,
1134                        const E_Bluez_Element *element __UNUSED__)
1135 {
1136    E_Bluez_Module_Context *ctxt = data;
1137    if (ctxt->poller.manager_changed)
1138      ecore_poller_del(ctxt->poller.manager_changed);
1139    ctxt->poller.manager_changed = ecore_poller_add
1140        (ECORE_POLLER_CORE, 1, _bluez_manager_changed_do, ctxt);
1141 }
1142
1143 static void
1144 _properties_sync_callback(void            *data,
1145                           DBusMessage *msg __UNUSED__,
1146                           DBusError       *err)
1147 {
1148    E_Bluez_Instance *inst = data;
1149    Eina_Bool powered;
1150    Eina_Bool discoverable;
1151
1152    if (err && dbus_error_is_set(err))
1153      {
1154         dbus_error_free(err);
1155         return;
1156      }
1157
1158    if (!e_bluez_adapter_powered_get(inst->adapter, &powered))
1159      {
1160         _bluez_operation_error_show(_("Query adapter's powered."));
1161         return;
1162      }
1163
1164    inst->powered = powered;
1165
1166    if (!e_bluez_adapter_discoverable_get(inst->adapter, &discoverable))
1167      {
1168         _bluez_operation_error_show(_("Query adapter's discoverable."));
1169         return;
1170      }
1171
1172    inst->discoverable = discoverable;
1173 }
1174
1175 static void
1176 _default_adapter_callback(void          *data,
1177                           DBusMessage   *msg,
1178                           DBusError *err __UNUSED__)
1179 {
1180    E_Bluez_Module_Context *ctxt = data;
1181    const Eina_List *l;
1182    E_Bluez_Instance *inst;
1183    const char *path;
1184
1185    if (err && dbus_error_is_set(err))
1186      {
1187         dbus_error_free(err);
1188         return;
1189      }
1190
1191    if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1192                              DBUS_TYPE_INVALID) == FALSE)
1193      return;
1194
1195    eina_stringshare_replace(&ctxt->default_adapter, path);
1196
1197    // TODO: instead of getting the default adapter, get the adapter for
1198    // each instance. See the mixer module.
1199    EINA_LIST_FOREACH(ctxt->instances, l, inst)
1200      {
1201         inst->adapter = e_bluez_adapter_get(path);
1202
1203         e_bluez_element_properties_sync_full
1204           (inst->adapter, _properties_sync_callback, inst);
1205      }
1206 }
1207
1208 static Eina_Bool
1209 _bluez_event_manager_in(void       *data,
1210                         int type    __UNUSED__,
1211                         void *event __UNUSED__)
1212 {
1213    E_Bluez_Module_Context *ctxt = data;
1214    E_Bluez_Element *element;
1215    Eina_List *l;
1216    E_Bluez_Instance *inst;
1217
1218    ctxt->has_manager = EINA_TRUE;
1219
1220    element = e_bluez_manager_get();
1221    if (!e_bluez_manager_default_adapter(_default_adapter_callback, ctxt))
1222      return ECORE_CALLBACK_DONE;
1223
1224    e_bluez_element_listener_add(element, _bluez_manager_changed, ctxt, NULL);
1225
1226    EINA_LIST_FOREACH(ctxt->instances, l, inst)
1227      _bluez_gadget_update(inst);
1228
1229    return ECORE_CALLBACK_PASS_ON;
1230 }
1231
1232 static Eina_Bool
1233 _bluez_event_manager_out(void       *data,
1234                          int type    __UNUSED__,
1235                          void *event __UNUSED__)
1236 {
1237    E_Bluez_Module_Context *ctxt = data;
1238    E_Bluez_Instance *inst;
1239    Eina_List *l;
1240
1241    ctxt->has_manager = EINA_FALSE;
1242    eina_stringshare_replace(&ctxt->default_adapter, NULL);
1243
1244    EINA_LIST_FOREACH(ctxt->instances, l, inst)
1245      _bluez_gadget_update(inst);
1246
1247    return ECORE_CALLBACK_PASS_ON;
1248 }
1249
1250 static Eina_Bool
1251 _bluez_event_element_updated(void       *data,
1252                              int type    __UNUSED__,
1253                              void *event __UNUSED__)
1254 {
1255    E_Bluez_Module_Context *ctxt = data;
1256    E_Bluez_Element *element = event;
1257    Eina_Bool powered, discoverable, discovering;
1258    E_Bluez_Instance *inst;
1259    Eina_List *l;
1260
1261    if (!e_bluez_element_is_adapter(element)) return ECORE_CALLBACK_PASS_ON;
1262
1263    if (!e_bluez_adapter_powered_get(element, &powered))
1264      powered = EINA_FALSE;
1265
1266    if (!e_bluez_adapter_discoverable_get(element, &discoverable))
1267      discoverable = EINA_FALSE;
1268
1269    if (!e_bluez_adapter_discovering_get(element, &discovering))
1270      discovering = EINA_FALSE;
1271
1272    EINA_LIST_FOREACH(ctxt->instances, l, inst)
1273      {
1274         if (inst->adapter != element) continue;
1275
1276         inst->powered = powered;
1277         inst->discoverable = discoverable;
1278         inst->discovering = discovering;
1279         _bluez_gadget_update(inst);
1280      }
1281
1282    return ECORE_CALLBACK_PASS_ON;
1283 }
1284
1285 static void
1286 _bluez_events_register(E_Bluez_Module_Context *ctxt)
1287 {
1288    ctxt->event.manager_in = ecore_event_handler_add
1289        (E_BLUEZ_EVENT_MANAGER_IN, _bluez_event_manager_in, ctxt);
1290    ctxt->event.manager_out = ecore_event_handler_add
1291        (E_BLUEZ_EVENT_MANAGER_OUT, _bluez_event_manager_out, ctxt);
1292    ctxt->event.element_updated = ecore_event_handler_add
1293        (E_BLUEZ_EVENT_ELEMENT_UPDATED, _bluez_event_element_updated, ctxt);
1294    ctxt->event.device_found = ecore_event_handler_add
1295        (E_BLUEZ_EVENT_DEVICE_FOUND, _bluez_event_devicefound, ctxt);
1296
1297    // TODO: E_BLUEZ_EVENT_DEVICE_DISAPPEARED
1298 }
1299
1300 static void
1301 _bluez_events_unregister(E_Bluez_Module_Context *ctxt)
1302 {
1303    if (ctxt->event.manager_in)
1304      ecore_event_handler_del(ctxt->event.manager_in);
1305    if (ctxt->event.manager_out)
1306      ecore_event_handler_del(ctxt->event.manager_out);
1307    if (ctxt->event.device_found)
1308      ecore_event_handler_del(ctxt->event.device_found);
1309 }
1310
1311 static void
1312 _bluez_agent_register(E_Bluez_Module_Context *ctxt)
1313 {
1314    E_DBus_Object *o;
1315
1316    ctxt->agent.iface = e_dbus_interface_new("org.bluez.Agent");
1317    if (!ctxt->agent.iface)
1318      return;
1319
1320    o = e_dbus_object_add(ctxt->agent.conn, _e_bluez_agent_path, ctxt);
1321    e_dbus_object_interface_attach(o, ctxt->agent.iface);
1322    e_dbus_interface_method_add
1323      (ctxt->agent.iface, "RequestPinCode", "o", "s", _bluez_request_pincode_cb);
1324    // TODO: RequestPasskey
1325    // TODO: RequestConfirmation
1326    // TODO: Authorize
1327    // TODO: DisplayPasskey
1328    // TODO: ConfirmModeChange
1329    // TODO: Cancel
1330    // TODO: Release
1331
1332    ctxt->agent.obj = o;
1333 }
1334
1335 static void
1336 _bluez_agent_unregister(E_Bluez_Module_Context *ctxt)
1337 {
1338    E_Object *o;
1339
1340    EINA_LIST_FREE(ctxt->agent.pending, o)
1341      e_object_del(o);
1342
1343    e_dbus_object_interface_detach(ctxt->agent.obj, ctxt->agent.iface);
1344    e_dbus_object_free(ctxt->agent.obj);
1345    e_dbus_interface_unref(ctxt->agent.iface);
1346 }
1347
1348 EAPI void *
1349 e_modapi_init(E_Module *m)
1350 {
1351    E_Bluez_Module_Context *ctxt = E_NEW(E_Bluez_Module_Context, 1);
1352    if (!ctxt)
1353      return NULL;
1354
1355    ctxt->agent.conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
1356    if ((!ctxt->agent.conn) || (!e_bluez_system_init(ctxt->agent.conn)))
1357      goto error_bluez_system_init;
1358
1359    bluez_mod = m;
1360
1361    if (_e_bluez_log_dom < 0)
1362      {
1363         _e_bluez_log_dom = eina_log_domain_register("ebluez", EINA_COLOR_ORANGE);
1364         if (_e_bluez_log_dom < 0)
1365           {
1366              //EINA_LOG_CRIT("could not register logging domain ebluez");
1367                goto error_log_domain;
1368           }
1369      }
1370
1371    _bluez_agent_register(ctxt);
1372    _bluez_actions_register(ctxt);
1373    e_gadcon_provider_register(&_gc_class);
1374
1375    _bluez_events_register(ctxt);
1376
1377    return ctxt;
1378
1379 error_log_domain:
1380    _e_bluez_log_dom = -1;
1381    bluez_mod = NULL;
1382    e_bluez_system_shutdown();
1383 error_bluez_system_init:
1384    E_FREE(ctxt);
1385    return NULL;
1386 }
1387
1388 static void
1389 _bluez_instances_free(E_Bluez_Module_Context *ctxt)
1390 {
1391    E_Bluez_Instance *inst;
1392    EINA_LIST_FREE(ctxt->instances, inst)
1393      {
1394         if (inst->popup)
1395           _bluez_popup_del(inst);
1396         if (inst->tip)
1397           _bluez_tip_del(inst);
1398
1399         e_object_del(E_OBJECT(inst->gcc));
1400      }
1401 }
1402
1403 EAPI int
1404 e_modapi_shutdown(E_Module *m)
1405 {
1406    E_Bluez_Module_Context *ctxt = m->data;
1407    E_Bluez_Element *element;
1408
1409    if (!ctxt)
1410      return 0;
1411
1412    element = e_bluez_manager_get();
1413    e_bluez_element_listener_del(element, _bluez_manager_changed, ctxt);
1414
1415    _bluez_events_unregister(ctxt);
1416    _bluez_instances_free(ctxt);
1417
1418    _bluez_actions_unregister(ctxt);
1419    _bluez_agent_unregister(ctxt);
1420    e_gadcon_provider_unregister(&_gc_class);
1421
1422    if (ctxt->poller.manager_changed)
1423      ecore_poller_del(ctxt->poller.manager_changed);
1424
1425    eina_stringshare_del(ctxt->default_adapter);
1426
1427    E_FREE(ctxt);
1428    bluez_mod = NULL;
1429
1430    e_bluez_system_shutdown();
1431
1432    return 1;
1433 }
1434
1435 EAPI int
1436 e_modapi_save(E_Module *m __UNUSED__)
1437 {
1438    return 1;
1439 }
1440