6 * - check why return NULL from method call triggers cancel error
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)
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
21 #include "e_mod_main.h"
23 static E_Module *bluez_mod = NULL;
24 static char tmpbuf[4096]; /* general purpose buffer, just use immediately */
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;
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);
35 struct bluez_pincode_data
37 void (*cb)(struct bluez_pincode_data *d);
39 E_Bluez_Module_Context *ctxt;
48 e_bluez_theme_path(void)
50 #define TF "/e-module-bluez.edj"
53 dirlen = strlen(bluez_mod->dir);
54 if (dirlen >= sizeof(tmpbuf) - sizeof(TF))
57 memcpy(tmpbuf, bluez_mod->dir, dirlen);
58 memcpy(tmpbuf + dirlen, TF, sizeof(TF));
65 _bluez_devices_clear(E_Bluez_Instance *inst)
67 E_Bluez_Instance_Device *d;
68 EINA_LIST_FREE(inst->devices, d)
70 eina_stringshare_del(d->address);
71 eina_stringshare_del(d->alias);
79 _bluez_discovery_cb(void *data,
80 DBusMessage *msg __UNUSED__,
83 E_Bluez_Instance *inst = data;
86 if (error && dbus_error_is_set(error))
88 _bluez_dbus_error_show(_("Cannot change adapter's discovery."), error);
89 dbus_error_free(error);
93 inst->discovering = !inst->discovering;
95 label = !inst->discovering ? _("Start Scan") : _("Stop Scan");
96 e_widget_button_label_set(inst->ui.button, label);
100 _bluez_create_paired_device_cb(void *data,
101 DBusMessage *msg __UNUSED__,
104 const char *alias = data;
106 if (error && dbus_error_is_set(error))
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);
116 (_("Bluetooth Manager"), _("Device '%s' successfully paired."), alias);
117 eina_stringshare_del(alias);
121 _bluez_toggle_powered_cb(void *data,
122 DBusMessage *msg __UNUSED__,
125 E_Bluez_Instance *inst = data;
127 if ((!error) || (!dbus_error_is_set(error)))
129 inst->powered_pending = EINA_FALSE;
130 inst->powered = !inst->powered;
134 _bluez_devices_clear(inst);
137 _bluez_popup_update(inst);
140 _bluez_gadget_update(inst);
144 _bluez_dbus_error_show(_("Cannot toggle adapter's powered."), error);
145 dbus_error_free(error);
149 _bluez_toggle_powered(E_Bluez_Instance *inst)
153 if ((!inst) || (!inst->ctxt->has_manager))
155 _bluez_operation_error_show(_("BlueZ Daemon is not running."));
161 _bluez_operation_error_show(_("No bluetooth adapter."));
165 if (!e_bluez_adapter_powered_get(inst->adapter, &powered))
167 _bluez_operation_error_show(_("Query adapter's powered."));
173 if (!e_bluez_adapter_powered_set
174 (inst->adapter, powered, _bluez_toggle_powered_cb, inst))
176 _bluez_operation_error_show(_("Query adapter's powered."));
182 _bluez_cb_toggle_powered(E_Object *obj __UNUSED__,
183 const char *params __UNUSED__)
185 E_Bluez_Module_Context *ctxt;
187 E_Bluez_Instance *inst;
192 ctxt = bluez_mod->data;
193 EINA_LIST_FOREACH(ctxt->instances, l, inst)
194 if (inst->adapter) _bluez_toggle_powered(inst);
197 static void _bluez_popup_del(E_Bluez_Instance *inst);
200 _bluez_popup_input_window_mouse_up_cb(void *data,
204 Ecore_Event_Mouse_Button *ev = event;
205 E_Bluez_Instance *inst = data;
207 if (ev->window != inst->ui.input.win)
208 return ECORE_CALLBACK_PASS_ON;
210 _bluez_popup_del(inst);
212 return ECORE_CALLBACK_PASS_ON;
216 _bluez_popup_input_window_key_down_cb(void *data,
220 Ecore_Event_Key *ev = event;
221 E_Bluez_Instance *inst = data;
224 if (ev->window != inst->ui.input.win)
225 return ECORE_CALLBACK_PASS_ON;
228 if (strcmp(keysym, "Escape") == 0)
229 _bluez_popup_del(inst);
231 return ECORE_CALLBACK_PASS_ON;
235 _bluez_popup_input_window_destroy(E_Bluez_Instance *inst)
237 ecore_x_window_free(inst->ui.input.win);
238 inst->ui.input.win = 0;
240 ecore_event_handler_del(inst->ui.input.mouse_up);
241 inst->ui.input.mouse_up = NULL;
243 ecore_event_handler_del(inst->ui.input.key_down);
244 inst->ui.input.key_down = NULL;
248 _bluez_popup_input_window_create(E_Bluez_Instance *inst)
250 Ecore_X_Window_Configure_Mask mask;
251 Ecore_X_Window w, popup_w;
254 man = e_manager_current_get();
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);
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);
268 inst->ui.input.key_down =
269 ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
270 _bluez_popup_input_window_key_down_cb, inst);
272 inst->ui.input.win = w;
276 _bluez_popup_cb_powered_changed(void *data,
279 E_Bluez_Instance *inst = data;
280 E_Bluez_Module_Context *ctxt = inst->ctxt;
281 Eina_Bool powered = e_widget_check_checked_get(obj);
283 if ((!ctxt) || (!ctxt->has_manager))
285 _bluez_operation_error_show(_("BlueZ Daemon is not running."));
291 _bluez_operation_error_show(_("No bluetooth adapter."));
295 if (!e_bluez_adapter_powered_set
296 (inst->adapter, powered, _bluez_toggle_powered_cb, inst))
298 _bluez_operation_error_show
299 (_("Cannot toggle adapter's powered."));
303 inst->powered_pending = EINA_TRUE;
307 _bluez_pincode_ask_cb(struct bluez_pincode_data *d)
313 e_util_dialog_show(_("Bluetooth Manager"), _("Invalid Pin Code."));
317 reply = dbus_message_new_method_return(d->msg);
318 dbus_message_append_args
319 (reply, DBUS_TYPE_STRING, &d->pincode, DBUS_TYPE_INVALID);
321 dbus_message_set_no_reply(reply, EINA_TRUE);
322 e_dbus_message_send(d->ctxt->agent.conn, reply, NULL, -1, NULL);
326 bluez_pincode_ask_ok(void *data,
329 struct bluez_pincode_data *d = data;
330 d->canceled = EINA_FALSE;
331 e_object_del(E_OBJECT(dia));
335 bluez_pincode_ask_cancel(void *data,
338 struct bluez_pincode_data *d = data;
339 d->canceled = EINA_TRUE;
340 e_object_del(E_OBJECT(dia));
344 bluez_pincode_ask_del(void *data)
346 E_Dialog *dia = data;
347 struct bluez_pincode_data *d = e_object_data_get(E_OBJECT(dia));
352 d->ctxt->agent.pending = eina_list_remove(d->ctxt->agent.pending, dia);
355 dbus_message_unref(d->msg);
356 eina_stringshare_del(d->alias);
361 bluez_pincode_ask_key_down(void *data,
363 Evas_Object *o __UNUSED__,
366 Evas_Event_Key_Down *ev = event;
367 struct bluez_pincode_data *d = data;
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);
376 bluez_pincode_ask(void (*cb)(struct bluez_pincode_data *),
379 E_Bluez_Module_Context *ctxt)
381 struct bluez_pincode_data *d;
382 Evas_Object *list, *o;
390 d = E_NEW(struct bluez_pincode_data, 1);
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");
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");
406 evas = d->dia->win->evas;
408 list = e_widget_list_add(evas, 0, 0);
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);
420 e_widget_list_object_append(list, o, 1, 1, 0.5);
422 d->entry = o = e_widget_entry_add(evas, &d->pincode, NULL, NULL, NULL);
423 e_widget_entry_password_set(o, 0);
425 e_widget_list_object_append(list, o, 1, 0, 0.0);
427 e_widget_size_min_get(list, &mw, &mh);
432 e_dialog_content_set(d->dia, list, mw, mh);
435 (d->dia, _("Ok"), NULL, bluez_pincode_ask_ok, d);
437 (d->dia, _("Cancel"), NULL, bluez_pincode_ask_cancel, d);
439 evas_object_event_callback_add
440 (d->dia->bg_object, EVAS_CALLBACK_KEY_DOWN,
441 bluez_pincode_ask_key_down, d);
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);
447 e_dialog_button_focus_num(d->dia, 0);
448 e_widget_focus_set(d->entry, 1);
450 e_win_centered_set(d->dia->win, 1);
451 e_dialog_show(d->dia);
453 ctxt->agent.pending = eina_list_append(ctxt->agent.pending, d->dia);
457 _bluez_request_pincode_cb(E_DBus_Object *obj,
460 E_Bluez_Module_Context *ctxt = e_dbus_object_data_get(obj);
461 E_Bluez_Element *element;
465 // TODO: seems that returning NULL is causing pin code rquest to be canceled!
467 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
468 DBUS_TYPE_INVALID) == FALSE)
471 element = e_bluez_device_get(path);
476 if (!e_bluez_device_alias_get(element, &alias))
478 if (!e_bluez_device_name_get(element, &alias))
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);
489 _bluez_popup_cb_scan(void *data,
490 void *data2 __UNUSED__)
492 E_Bluez_Instance *inst = data;
497 else if (inst->discovering)
498 ret = e_bluez_adapter_stop_discovery
499 (inst->adapter, _bluez_discovery_cb, inst);
502 inst->last_scan = ecore_loop_time_get();
504 _bluez_devices_clear(inst);
506 ret = e_bluez_adapter_start_discovery
507 (inst->adapter, _bluez_discovery_cb, inst);
509 _bluez_popup_update(inst);
513 ERR("Failed on discovery procedure");
517 _bluez_popup_cb_controls(void *data,
518 void *data2 __UNUSED__)
520 E_Bluez_Instance *inst = data;
522 _bluez_popup_del(inst);
523 if (inst->conf_dialog)
527 inst->conf_dialog = e_bluez_config_dialog_new(NULL, inst);
531 _bluez_popup_device_selected(void *data)
533 E_Bluez_Instance *inst = data;
534 const char *address = inst->address;
536 const char *cap = "DisplayYesNo";
537 const E_Bluez_Instance_Device *d;
541 _bluez_popup_del(inst);
545 ERR("no device selected for pairing.");
549 inst->alias = address;
550 EINA_LIST_FOREACH(inst->devices, l, d)
552 if (address == d->alias)
554 inst->alias = d->alias;
561 ERR("device %s does not have an alias.", address);
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))
570 eina_stringshare_del(alias);
576 _bluez_event_devicefound(void *data,
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;
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.
589 alias = e_bluez_devicefound_alias_get(device);
591 EINA_LIST_FOREACH(ctxt->instances, l_inst, inst)
593 const Eina_List *l_dev;
594 E_Bluez_Instance_Device *dev;
595 Eina_Bool found = EINA_FALSE;
597 if (inst->adapter != device->adapter) continue;
599 EINA_LIST_FOREACH(inst->devices, l_dev, dev)
601 if (dev->address == device->name)
610 dev = malloc(sizeof(E_Bluez_Instance_Device));
613 dev->address = eina_stringshare_ref(device->name);
614 dev->alias = eina_stringshare_ref(alias);
616 inst->devices = eina_list_append(inst->devices, dev);
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);
631 _bluez_popup_update(E_Bluez_Instance *inst)
633 Evas_Object *list = inst->ui.list;
636 E_Bluez_Instance_Device *d;
639 /* TODO: replace this with a scroller + list of edje
640 * objects that are more full of features
642 selected = e_widget_ilist_selected_get(list);
643 e_widget_ilist_freeze(list);
644 e_widget_ilist_clear(list);
646 EINA_LIST_FOREACH(inst->devices, l, d)
648 e_widget_ilist_append
649 (inst->ui.list, NULL, d->alias,
650 _bluez_popup_device_selected, inst, d->address);
655 inst->first_selection = EINA_TRUE;
656 e_widget_ilist_selected_set(list, selected);
659 inst->first_selection = EINA_FALSE;
661 e_widget_ilist_go(list);
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);
670 _bluez_popup_del(E_Bluez_Instance *inst)
672 _bluez_popup_input_window_destroy(inst);
673 e_object_del(E_OBJECT(inst->popup));
678 _bluez_popup_new(E_Bluez_Instance *inst)
684 Eina_Bool b, needs_scan = EINA_FALSE;
688 e_gadcon_popup_show(inst->popup);
694 _bluez_operation_error_show(_("No bluetooth adapter."));
698 if (!e_bluez_adapter_discovering_get(inst->adapter, &b))
700 _bluez_operation_error_show(_("Can't get Discovering property"));
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))
708 label = _("Stop Scan");
709 needs_scan = EINA_TRUE;
712 label = inst->discovering ? _("Stop Scan") : _("Start Scan");
714 inst->popup = e_gadcon_popup_new(inst->gcc);
715 evas = inst->popup->win->evas;
717 ol = e_widget_list_add(evas, 0, 0);
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);
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);
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);
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);
738 _bluez_popup_update(inst);
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);
745 e_gadcon_popup_content_set(inst->popup, ol);
746 e_gadcon_popup_show(inst->popup);
747 _bluez_popup_input_window_create(inst);
749 if (needs_scan) _bluez_popup_cb_scan(inst, NULL);
753 _bluez_menu_cb_cfg(void *data,
754 E_Menu *menu __UNUSED__,
755 E_Menu_Item *mi __UNUSED__)
757 E_Bluez_Instance *inst = data;
759 _bluez_popup_del(inst);
760 if (inst->conf_dialog)
764 inst->conf_dialog = e_bluez_config_dialog_new(NULL, inst);
768 _bluez_menu_new(E_Bluez_Instance *inst,
769 Evas_Event_Mouse_Down *ev)
776 zone = e_util_zone_current_get(e_manager_current_get());
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);
784 m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
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);
794 _bluez_tip_new(E_Bluez_Instance *inst)
798 inst->tip = e_gadcon_popup_new(inst->gcc);
799 if (!inst->tip) return;
801 e = inst->tip->win->evas;
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");
807 _bluez_tip_update(inst);
809 e_gadcon_popup_content_set(inst->tip, inst->o_tip);
810 e_gadcon_popup_show(inst->tip);
814 _bluez_tip_del(E_Bluez_Instance *inst)
816 evas_object_del(inst->o_tip);
817 e_object_del(E_OBJECT(inst->tip));
823 _bluez_cb_mouse_down(void *data,
824 Evas *evas __UNUSED__,
825 Evas_Object *obj __UNUSED__,
828 E_Bluez_Instance *inst;
829 Evas_Event_Mouse_Down *ev;
839 _bluez_popup_new(inst);
841 _bluez_popup_del(inst);
843 else if (ev->button == 2)
844 _bluez_toggle_powered(inst);
845 else if (ev->button == 3)
846 _bluez_menu_new(inst, ev);
850 _bluez_cb_mouse_in(void *data,
851 Evas *evas __UNUSED__,
852 Evas_Object *obj __UNUSED__,
853 void *event __UNUSED__)
855 E_Bluez_Instance *inst = data;
860 _bluez_tip_new(inst);
864 _bluez_cb_mouse_out(void *data,
865 Evas *evas __UNUSED__,
866 Evas_Object *obj __UNUSED__,
867 void *event __UNUSED__)
869 E_Bluez_Instance *inst = data;
874 _bluez_tip_del(inst);
878 _bluez_edje_view_update(E_Bluez_Instance *inst,
881 E_Bluez_Module_Context *ctxt = inst->ctxt;
884 if (!ctxt->has_manager)
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");
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");
904 if (!e_bluez_adapter_name_get(inst->adapter, &name))
906 edje_object_part_text_set(o, "e.text.name", name);
907 edje_object_signal_emit(o, "e,changed,name", "e");
911 if (inst->discoverable)
913 edje_object_signal_emit(o, "e,changed,powered", "e");
914 edje_object_part_text_set
916 _("Bluetooth is powered and discoverable."));
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."));
927 edje_object_signal_emit(o, "e,changed,off", "e");
928 edje_object_part_text_set(o, "e.text.status", _("Bluetooth is off."));
933 _bluez_tip_update(E_Bluez_Instance *inst)
935 _bluez_edje_view_update(inst, inst->o_tip);
939 _bluez_gadget_update(E_Bluez_Instance *inst)
941 E_Bluez_Module_Context *ctxt = inst->ctxt;
943 if (inst->popup && ((!ctxt->has_manager) || (!inst->adapter)))
944 _bluez_popup_del(inst);
947 _bluez_popup_update(inst);
949 _bluez_tip_update(inst);
951 _bluez_edje_view_update(inst, inst->ui.gadget);
954 /* Gadcon Api Functions */
956 static E_Gadcon_Client *
957 _gc_init(E_Gadcon *gc,
962 E_Bluez_Instance *inst;
963 E_Bluez_Module_Context *ctxt;
968 ctxt = bluez_mod->data;
970 inst = E_NEW(E_Bluez_Instance, 1);
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");
976 inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->ui.gadget);
977 inst->gcc->data = inst;
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);
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);
991 inst->adapter = NULL;
995 Eina_Bool powered, discoverable, discovering;
997 if (e_bluez_adapter_powered_get(inst->adapter, &powered))
998 inst->powered = powered;
1000 if (e_bluez_adapter_discoverable_get(inst->adapter, &discoverable))
1001 inst->discoverable = discoverable;
1003 if (e_bluez_adapter_discovering_get(inst->adapter, &discovering))
1004 inst->discovering = discovering;
1007 _bluez_gadget_update(inst);
1009 ctxt->instances = eina_list_append(ctxt->instances, inst);
1015 _gc_shutdown(E_Gadcon_Client *gcc)
1017 E_Bluez_Module_Context *ctxt;
1018 E_Bluez_Instance *inst;
1023 ctxt = bluez_mod->data;
1031 evas_object_del(inst->ui.gadget);
1033 _bluez_devices_clear(inst);
1035 ctxt->instances = eina_list_remove(ctxt->instances, inst);
1041 _gc_orient(E_Gadcon_Client *gcc,
1042 E_Gadcon_Orient orient __UNUSED__)
1044 e_gadcon_client_aspect_set(gcc, 16, 16);
1045 e_gadcon_client_min_size_set(gcc, 16, 16);
1049 _gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__)
1051 return _(_e_bluez_Name);
1054 static Evas_Object *
1055 _gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__,
1060 o = edje_object_add(evas);
1061 edje_object_file_set(o, e_bluez_theme_path(), "icon");
1066 _gc_id_new(const E_Gadcon_Client_Class *client_class __UNUSED__)
1068 E_Bluez_Module_Context *ctxt;
1073 ctxt = bluez_mod->data;
1077 snprintf(tmpbuf, sizeof(tmpbuf), "bluez.%d",
1078 eina_list_count(ctxt->instances));
1082 static const E_Gadcon_Client_Class _gc_class =
1084 GADCON_CLIENT_CLASS_VERSION, _e_bluez_name,
1086 _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
1087 e_gadcon_site_is_not_toolbar
1089 E_GADCON_CLIENT_STYLE_PLAIN
1092 EAPI E_Module_Api e_modapi = {E_MODULE_API_VERSION, _e_bluez_Name};
1094 static const char _act_toggle_powered[] = "toggle_powered";
1095 static const char _lbl_toggle_powered[] = "Toggle Powered";
1098 _bluez_actions_register(E_Bluez_Module_Context *ctxt)
1100 ctxt->actions.toggle_powered = e_action_add(_act_toggle_powered);
1101 if (ctxt->actions.toggle_powered)
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,
1112 _bluez_actions_unregister(E_Bluez_Module_Context *ctxt)
1114 if (ctxt->actions.toggle_powered)
1116 e_action_predef_name_del(_(_e_bluez_Name), _(_lbl_toggle_powered));
1117 e_action_del(_act_toggle_powered);
1122 _bluez_manager_changed_do(void *data)
1124 E_Bluez_Module_Context *ctxt = data;
1126 //FIXME: reload the default adapter maybe?
1128 ctxt->poller.manager_changed = NULL;
1129 return ECORE_CALLBACK_CANCEL;
1133 _bluez_manager_changed(void *data,
1134 const E_Bluez_Element *element __UNUSED__)
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);
1144 _properties_sync_callback(void *data,
1145 DBusMessage *msg __UNUSED__,
1148 E_Bluez_Instance *inst = data;
1150 Eina_Bool discoverable;
1152 if (err && dbus_error_is_set(err))
1154 dbus_error_free(err);
1158 if (!e_bluez_adapter_powered_get(inst->adapter, &powered))
1160 _bluez_operation_error_show(_("Query adapter's powered."));
1164 inst->powered = powered;
1166 if (!e_bluez_adapter_discoverable_get(inst->adapter, &discoverable))
1168 _bluez_operation_error_show(_("Query adapter's discoverable."));
1172 inst->discoverable = discoverable;
1176 _default_adapter_callback(void *data,
1178 DBusError *err __UNUSED__)
1180 E_Bluez_Module_Context *ctxt = data;
1182 E_Bluez_Instance *inst;
1185 if (err && dbus_error_is_set(err))
1187 dbus_error_free(err);
1191 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1192 DBUS_TYPE_INVALID) == FALSE)
1195 eina_stringshare_replace(&ctxt->default_adapter, path);
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)
1201 inst->adapter = e_bluez_adapter_get(path);
1203 e_bluez_element_properties_sync_full
1204 (inst->adapter, _properties_sync_callback, inst);
1209 _bluez_event_manager_in(void *data,
1210 int type __UNUSED__,
1211 void *event __UNUSED__)
1213 E_Bluez_Module_Context *ctxt = data;
1214 E_Bluez_Element *element;
1216 E_Bluez_Instance *inst;
1218 ctxt->has_manager = EINA_TRUE;
1220 element = e_bluez_manager_get();
1221 if (!e_bluez_manager_default_adapter(_default_adapter_callback, ctxt))
1222 return ECORE_CALLBACK_DONE;
1224 e_bluez_element_listener_add(element, _bluez_manager_changed, ctxt, NULL);
1226 EINA_LIST_FOREACH(ctxt->instances, l, inst)
1227 _bluez_gadget_update(inst);
1229 return ECORE_CALLBACK_PASS_ON;
1233 _bluez_event_manager_out(void *data,
1234 int type __UNUSED__,
1235 void *event __UNUSED__)
1237 E_Bluez_Module_Context *ctxt = data;
1238 E_Bluez_Instance *inst;
1241 ctxt->has_manager = EINA_FALSE;
1242 eina_stringshare_replace(&ctxt->default_adapter, NULL);
1244 EINA_LIST_FOREACH(ctxt->instances, l, inst)
1245 _bluez_gadget_update(inst);
1247 return ECORE_CALLBACK_PASS_ON;
1251 _bluez_event_element_updated(void *data,
1252 int type __UNUSED__,
1253 void *event __UNUSED__)
1255 E_Bluez_Module_Context *ctxt = data;
1256 E_Bluez_Element *element = event;
1257 Eina_Bool powered, discoverable, discovering;
1258 E_Bluez_Instance *inst;
1261 if (!e_bluez_element_is_adapter(element)) return ECORE_CALLBACK_PASS_ON;
1263 if (!e_bluez_adapter_powered_get(element, &powered))
1264 powered = EINA_FALSE;
1266 if (!e_bluez_adapter_discoverable_get(element, &discoverable))
1267 discoverable = EINA_FALSE;
1269 if (!e_bluez_adapter_discovering_get(element, &discovering))
1270 discovering = EINA_FALSE;
1272 EINA_LIST_FOREACH(ctxt->instances, l, inst)
1274 if (inst->adapter != element) continue;
1276 inst->powered = powered;
1277 inst->discoverable = discoverable;
1278 inst->discovering = discovering;
1279 _bluez_gadget_update(inst);
1282 return ECORE_CALLBACK_PASS_ON;
1286 _bluez_events_register(E_Bluez_Module_Context *ctxt)
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);
1297 // TODO: E_BLUEZ_EVENT_DEVICE_DISAPPEARED
1301 _bluez_events_unregister(E_Bluez_Module_Context *ctxt)
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);
1312 _bluez_agent_register(E_Bluez_Module_Context *ctxt)
1316 ctxt->agent.iface = e_dbus_interface_new("org.bluez.Agent");
1317 if (!ctxt->agent.iface)
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
1327 // TODO: DisplayPasskey
1328 // TODO: ConfirmModeChange
1332 ctxt->agent.obj = o;
1336 _bluez_agent_unregister(E_Bluez_Module_Context *ctxt)
1340 EINA_LIST_FREE(ctxt->agent.pending, o)
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);
1349 e_modapi_init(E_Module *m)
1351 E_Bluez_Module_Context *ctxt = E_NEW(E_Bluez_Module_Context, 1);
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;
1361 if (_e_bluez_log_dom < 0)
1363 _e_bluez_log_dom = eina_log_domain_register("ebluez", EINA_COLOR_ORANGE);
1364 if (_e_bluez_log_dom < 0)
1366 //EINA_LOG_CRIT("could not register logging domain ebluez");
1367 goto error_log_domain;
1371 _bluez_agent_register(ctxt);
1372 _bluez_actions_register(ctxt);
1373 e_gadcon_provider_register(&_gc_class);
1375 _bluez_events_register(ctxt);
1380 _e_bluez_log_dom = -1;
1382 e_bluez_system_shutdown();
1383 error_bluez_system_init:
1389 _bluez_instances_free(E_Bluez_Module_Context *ctxt)
1391 E_Bluez_Instance *inst;
1392 EINA_LIST_FREE(ctxt->instances, inst)
1395 _bluez_popup_del(inst);
1397 _bluez_tip_del(inst);
1399 e_object_del(E_OBJECT(inst->gcc));
1404 e_modapi_shutdown(E_Module *m)
1406 E_Bluez_Module_Context *ctxt = m->data;
1407 E_Bluez_Element *element;
1412 element = e_bluez_manager_get();
1413 e_bluez_element_listener_del(element, _bluez_manager_changed, ctxt);
1415 _bluez_events_unregister(ctxt);
1416 _bluez_instances_free(ctxt);
1418 _bluez_actions_unregister(ctxt);
1419 _bluez_agent_unregister(ctxt);
1420 e_gadcon_provider_unregister(&_gc_class);
1422 if (ctxt->poller.manager_changed)
1423 ecore_poller_del(ctxt->poller.manager_changed);
1425 eina_stringshare_del(ctxt->default_adapter);
1430 e_bluez_system_shutdown();
1436 e_modapi_save(E_Module *m __UNUSED__)