4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Taeyoung Kim <ty317.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <appcore-efl.h>
28 #include "usb-syspopup.h"
30 #define SYSPOPUP_PARAM_LEN 3
31 #define USB_SYSPOPUP_MESSAGE_DOMAIN \
33 #define USB_NOTICE_SYSPOPUP_FAIL \
34 "USB system popup failed."
36 char *matchedApps[MAX_NUM_OF_MATCHED_APPS];
37 Elm_Genlist_Item_Class itc;
39 Elm_Object_Item *item;
41 syspopup_handler handler = {
43 .def_timeout_fn = NULL
46 int ipc_socket_client_init(int *sock_remote)
49 if (!sock_remote) return -1;
51 struct sockaddr_un remote;
52 char str[SOCK_STR_LEN];
54 if (((*sock_remote) = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
56 USB_LOG(USB_LOG_VERBOSE, "FAIL: socket(AF_UNIX, SOCK_STREAM, 0)");
59 remote.sun_family = AF_UNIX;
60 strncpy(remote.sun_path, SOCK_PATH, strlen(SOCK_PATH)+1);
61 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
63 if (connect((*sock_remote), (struct sockaddr *)&remote, len) == -1) {
65 USB_LOG(USB_LOG_VERBOSE, "FAIL: connect((*sock_remote), (struct sockaddr *)&remote, len)");
72 int ipc_socket_client_close(int *sock_remote)
75 if (!sock_remote) return -1;
80 static void __win_del(void *data, Evas_Object * obj, void *event)
87 static Evas_Object *__create_win(const char *name)
96 eo = elm_win_add(NULL, name, ELM_WIN_DIALOG_BASIC);
98 elm_win_title_set(eo, name);
99 elm_win_borderless_set(eo, EINA_TRUE);
100 elm_win_alpha_set(eo, EINA_TRUE);
102 evas_object_smart_callback_add(eo, "delete,request",
104 ecore_x_window_size_get(ecore_x_window_root_first_get(),
106 evas_object_resize(eo, w, h);
114 static void usb_chgdet_cb(void *data)
118 if(device_get_property(DEVTYPE_JACK,JACK_PROP_USB_ONLINE,&val)==0)
120 USB_LOG(USB_LOG_VERBOSE, "device_get_property(): %d\n", val);
123 USB_LOG(USB_LOG_VERBOSE, "Calling elm_exit()\n");
130 static int register_heynoti(void *data)
135 struct appdata *ad = data;
137 /* Registering heynoti to recognize the change of the status of USB cable connection */
138 ad->noti_fd = heynoti_init();
141 USB_LOG(USB_LOG_VERBOSE, "FAIL: heynoti_init()\n");
145 if(heynoti_attach_handler(ad->noti_fd) < 0)
147 USB_LOG(USB_LOG_VERBOSE, "FAIL: heynoti_attach_handler()\n");
151 if (heynoti_subscribe(ad->noti_fd,"device_usb_chgdet", usb_chgdet_cb, NULL) != 0)
153 USB_LOG(USB_LOG_VERBOSE, "FAIL: heynoti_subscribe()\n");
161 static void deregister_heynoti(void *data)
166 struct appdata *ad = data;
168 /* Closing heynoti */
169 if (heynoti_unsubscribe(ad->noti_fd, "device_usb_chgdet", usb_chgdet_cb) < 0)
171 USB_LOG(USB_LOG_VERBOSE, "ERROR: heynoti_unsubscribe() \n");
173 if (heynoti_detach_handler(ad->noti_fd) < 0)
175 USB_LOG(USB_LOG_VERBOSE, "ERROR: heynoti_detach_handler() \n");
177 heynoti_close(ad->noti_fd);
183 static int __app_create(void *data)
188 struct appdata *ad = data;
192 register_heynoti(ad);
194 /* When USB cable is removed before registering heynoti, syspopup should be terminated */
197 /* init internationalization */
198 r = appcore_set_i18n(PACKAGE, LOCALEDIR);
201 USB_LOG(USB_LOG_VERBOSE, "FAIL: appcore_set_i18n(PACKAGE, LOCALEDIR)\n");
210 static void unload_popup(void *data)
215 struct appdata *ad = data;
218 evas_object_del(ad->lbtn);
222 evas_object_del(ad->rbtn);
226 evas_object_del(ad->popup);
230 evas_object_del(ad->win);
237 static int __app_terminate(void *data)
242 struct appdata *ad = data;
245 USB_LOG(USB_LOG_VERBOSE, "[SYSPOPUP] %s, %d\n", __func__, __LINE__);
247 deregister_heynoti(ad);
252 ret = bundle_free(ad->b);
255 USB_LOG(USB_LOG_VERBOSE, "FAIL: bundle_free(ad->b)\n");
264 static int __app_pause(void *data)
271 static int __app_resume(void *data)
278 int request_to_usb_server(int request, char *pkgName, char *answer)
282 char str[SOCK_STR_LEN];
284 ret = ipc_socket_client_init(&sock_remote);
286 USB_LOG(USB_LOG_VERBOSE, "FAIL: ipc_socket_client_init(&sock_remote)");
289 if(LAUNCH_APP_FOR_ACC == request) {
290 snprintf(str, SOCK_STR_LEN, "%d|%s", request, pkgName);
292 snprintf(str, SOCK_STR_LEN, "%d|", request);
294 USB_LOG(USB_LOG_VERBOSE, "request: %s", str);
295 if (send (sock_remote, str, strlen(str)+1, 0) == -1) {
296 USB_LOG(USB_LOG_VERBOSE, "FAIL: send (sock_remote, str, strlen(str), 0)");
297 ipc_socket_client_close(&sock_remote);
300 if ((t = recv(sock_remote, answer, SOCK_STR_LEN, 0)) > 0) {
302 USB_LOG(USB_LOG_VERBOSE, "[CLIENT] Received value: %s", answer);
304 USB_LOG(USB_LOG_VERBOSE, "FAIL: recv(sock_remote, str, SOCK_STR_LEN, 0)");
307 ipc_socket_client_close(&sock_remote);
312 static void load_connection_failed_popup_ok_response_cb(void *data,
313 Evas_Object * obj, void *event_info)
318 struct appdata *ad = data;
321 evas_object_smart_callback_del(ad->lbtn, "clicked", load_connection_failed_popup_ok_response_cb);
322 char buf[SOCK_STR_LEN];
323 int ret = request_to_usb_server(ERROR_POPUP_OK_BTN, NULL, buf);
325 USB_LOG(USB_LOG_VERBOSE, "FAIL: request_to_usb_server(ERROR_POPUP_OK_BTN, NULL, buf)\n");
335 static void request_perm_popup_yes_response_cb(void *data,
336 Evas_Object * obj, void *event_info)
340 struct appdata *ad = (struct appdata *)data;
342 evas_object_smart_callback_del(ad->lbtn, "clicked", request_perm_popup_yes_response_cb);
343 evas_object_smart_callback_del(ad->rbtn, "clicked", request_perm_popup_no_response_cb);
345 char buf[SOCK_STR_LEN];
346 int ret = request_to_usb_server(REQ_ACC_PERM_NOTI_YES_BTN, NULL, buf);
348 USB_LOG(USB_LOG_VERBOSE, "FAIL: request_to_usb_server(NOTICE_YES_BTN, NULL, buf)\n");
355 static void request_perm_popup_no_response_cb(void *data,
356 Evas_Object * obj, void *event_info)
360 struct appdata *ad = (struct appdata *)data;
362 evas_object_smart_callback_del(ad->lbtn, "clicked", request_perm_popup_yes_response_cb);
363 evas_object_smart_callback_del(ad->rbtn, "clicked", request_perm_popup_no_response_cb);
365 char buf[SOCK_STR_LEN];
366 int ret = request_to_usb_server(REQ_ACC_PERM_NOTI_NO_BTN, NULL, buf);
368 USB_LOG(USB_LOG_VERBOSE, "FAIL: request_to_usb_server(NOTICE_NO_BTN, NULL, buf)\n");
375 static void load_connection_failed_popup(void *data)
380 struct appdata *ad = data;
385 ad->win = __create_win(PACKAGE);
389 ret = syspopup_create(ad->b, &handler, ad->win, ad);
392 ad->content = dgettext(USB_SYSPOPUP_MESSAGE_DOMAIN, "IDS_USB_POP_USB_CONNECTION_FAILED");
393 USB_LOG(USB_LOG_VERBOSE, "ad->content is (%s)\n", ad->content);
395 evas_object_show(ad->win);
396 ad->popup = elm_popup_add(ad->win);
397 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
398 elm_object_text_set(ad->popup, ad->content);
401 ad->lbtn = elm_button_add(ad->popup);
402 elm_object_text_set(ad->lbtn, dgettext("sys_string","IDS_COM_SK_OK"));
403 elm_object_part_content_set(ad->popup, "button1", ad->lbtn);
404 evas_object_smart_callback_add(ad->lbtn, "clicked",
405 load_connection_failed_popup_ok_response_cb, ad);
407 evas_object_show(ad->popup);
411 USB_LOG(USB_LOG_VERBOSE, "syspopup_create() returns an integer which is not 0\n");
417 static int get_accessory_matched(struct appdata* ad)
421 matchedApps[0]=strdup("acc_test");
429 static char *_gl_text_get(void *data, Evas_Object *obj, const char *part)
432 int index = (int)data;
433 USB_LOG(USB_LOG_VERBOSE, "App name: %s\n", matchedApps[index]);
435 return strdup(matchedApps[index]);
438 static void select_app_popup_cancel_response_cb(void *data, Evas_Object * obj, void *event_info)
442 struct appdata *ad = (struct appdata *)data;
448 static int send_sel_pkg_to_usb_server(struct appdata *ad)
452 char answer[SOCK_STR_LEN];
453 ret = request_to_usb_server(LAUNCH_APP_FOR_ACC, ad->selPkg, answer);
455 USB_LOG(USB_LOG_VERBOSE, "FAIL: request_to_usb_server(LAUNCH_APP, ad->selPkg, answer)");
458 USB_LOG(USB_LOG_VERBOSE, "Launching app result is %s\n", answer);
463 static void select_app_popup_gl_select_cb(void *data, Evas_Object *obj, void *event_info)
466 if (!event_info) return;
468 struct appdata *ad = (struct appdata *)data;
470 Elm_Object_Item *item = (Elm_Object_Item *)event_info;
472 index = (int)elm_object_item_data_get(item);
473 USB_LOG(USB_LOG_VERBOSE, "Selected Item: %d: %s\n", index, matchedApps[index]);
474 snprintf(ad->selPkg, PKG_NAME_LEN, "%s", matchedApps[index]);
477 int ret = send_sel_pkg_to_usb_server(ad);
478 if ( 0!= ret ) USB_LOG(USB_LOG_VERBOSE,"FAIL: send_sel_pkg_to_usb_server(ad)");
484 static void load_popup_to_select_app(struct appdata *ad, int numOfApps)
494 win = __create_win(PACKAGE);
499 ret = syspopup_create(ad->b, &handler, ad->win, ad);
500 USB_LOG(USB_LOG_VERBOSE, "ret: %d\n", ret);
502 evas_object_show(ad->win);
503 ad->popup = elm_popup_add(ad->win);
504 elm_object_style_set(ad->popup,"menustyle");
505 elm_object_part_text_set(ad->popup, "title,text", "Select app to launch");
506 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
507 ad->lbtn = elm_button_add(ad->popup);
508 elm_object_text_set(ad->lbtn, "Cancel");
509 elm_object_part_content_set(ad->popup, "button1", ad->lbtn);
510 evas_object_smart_callback_add(ad->lbtn, "clicked", select_app_popup_cancel_response_cb, ad);
512 itc.item_style = "1text";
513 itc.func.text_get = _gl_text_get;
514 itc.func.content_get = NULL;
515 itc.func.state_get = NULL;
517 genlist = elm_genlist_add(ad->popup);
518 evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
519 evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
520 for (index = 0; index < numOfApps; index++) {
521 USB_LOG(USB_LOG_VERBOSE, "%d\n", numOfApps);
522 item = elm_genlist_item_append(genlist, &itc, (void *) index, NULL,
523 ELM_GENLIST_ITEM_NONE, select_app_popup_gl_select_cb, ad);
524 if (NULL == item) USB_LOG(USB_LOG_VERBOSE, "NULL ==item\n");
526 elm_object_content_set(ad->popup, genlist);
527 evas_object_show(ad->popup);
532 static void load_popup_to_confirm_uri(struct appdata *ad)
538 static int get_accessory_info(struct appdata *ad)
543 char device[ACC_INFO_NUM][ACC_ELEMENT_LEN];
544 char key[SYSPOPUP_PARAM_LEN];
545 for (i = 0; i < ACC_INFO_NUM; i++) {
546 snprintf(key, SYSPOPUP_PARAM_LEN, "%d", 1 + i);
547 const char* type = bundle_get_val(ad->b, (const char *)key);
549 USB_LOG(USB_LOG_VERBOSE, "ERROR: bundle_get_val(b)\n");
552 USB_LOG(USB_LOG_VERBOSE, "%d: %s\n", i, type);
553 snprintf(device[i], ACC_ELEMENT_LEN, "%s", type);
557 USB_LOG(USB_LOG_VERBOSE, "Get USB Acc info\n");
558 snprintf(ad->usbAcc->manufacturer, ACC_ELEMENT_LEN, "%s", device[ACC_MANUFACTURER]);
559 snprintf(ad->usbAcc->model, ACC_ELEMENT_LEN, "%s", device[ACC_MODEL]);
560 snprintf(ad->usbAcc->description, ACC_ELEMENT_LEN, "%s", device[ACC_DESCRIPTION]);
561 snprintf(ad->usbAcc->version, ACC_ELEMENT_LEN, "%s", device[ACC_VERSION]);
562 snprintf(ad->usbAcc->uri, ACC_ELEMENT_LEN, "%s", device[ACC_URI]);
563 snprintf(ad->usbAcc->serial, ACC_ELEMENT_LEN, "%s", device[ACC_SERIAL]);
565 USB_LOG(USB_LOG_VERBOSE, "Print USB acc info\n");
566 USB_LOG(USB_LOG_VERBOSE, "** USB ACCESSORY INFO **");
567 USB_LOG(USB_LOG_VERBOSE, "* Manufacturer: %s *", ad->usbAcc->manufacturer);
568 USB_LOG(USB_LOG_VERBOSE, "* Model : %s *", ad->usbAcc->model);
569 USB_LOG(USB_LOG_VERBOSE, "* Description : %s *", ad->usbAcc->description);
570 USB_LOG(USB_LOG_VERBOSE, "* Version : %s *", ad->usbAcc->version);
571 USB_LOG(USB_LOG_VERBOSE, "* URI : %s *", ad->usbAcc->uri);
572 USB_LOG(USB_LOG_VERBOSE, "* SERIAL : %s *", ad->usbAcc->serial);
573 USB_LOG(USB_LOG_VERBOSE, "************************");
578 static void load_select_pkg_for_acc_popup(struct appdata *ad)
584 memset(&usbAcc, 0x0, sizeof(UsbAccessory));
585 ad->usbAcc = &usbAcc;
586 ret = get_accessory_info(ad);
588 USB_LOG(USB_LOG_VERBOSE, "FAIL: get_accessory_info(ad)");
592 int numOfApps = get_accessory_matched(ad);
594 USB_LOG(USB_LOG_VERBOSE, "number of apps matched: %d\n", numOfApps);
595 load_popup_to_select_app(ad, numOfApps);
597 USB_LOG(USB_LOG_VERBOSE, "number of apps matched is 0\n");
598 load_popup_to_confirm_uri(ad);
603 void load_request_perm_popup(struct appdata *ad)
614 win = __create_win(PACKAGE);
619 ret = syspopup_create(ad->b, &handler, ad->win, ad);
622 ad->content = dgettext(USB_SYSPOPUP_MESSAGE_DOMAIN,
623 "IDS_COM_POP_ALLOW_APPLICATION_P1SS_TO_ACCESS_USB_ACCESSORY_Q_ABB");
624 USB_LOG(USB_LOG_VERBOSE, "ad->content is (%s)\n", ad->content);
626 evas_object_show(ad->win);
627 ad->popup = elm_popup_add(ad->win);
628 evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
629 elm_object_text_set(ad->popup, ad->content);
632 ad->lbtn = elm_button_add(ad->popup);
633 elm_object_text_set(ad->lbtn, dgettext("sys_string","IDS_COM_SK_YES"));
634 elm_object_part_content_set(ad->popup, "button1", ad->lbtn);
635 evas_object_smart_callback_add(ad->lbtn, "clicked", request_perm_popup_yes_response_cb, ad);
638 ad->rbtn = elm_button_add(ad->popup);
639 elm_object_text_set(ad->rbtn, dgettext("sys_string","IDS_COM_SK_NO"));
640 elm_object_part_content_set(ad->popup, "button2", ad->rbtn);
641 evas_object_smart_callback_add(ad->rbtn, "clicked", request_perm_popup_no_response_cb, ad);
643 evas_object_show(ad->popup);
647 USB_LOG(USB_LOG_VERBOSE, "syspopup_create() returns an integer which is not 0\n");
653 static int __app_reset(bundle *b, void *data)
658 struct appdata *ad = data;
659 char key[SYSPOPUP_PARAM_LEN];
662 ad->b = bundle_dup(b);
664 /* When syspopup is already loaded, remove the popup and load new popup */
665 if (syspopup_has_popup(b)) {
666 USB_LOG(USB_LOG_VERBOSE, "syspopup_has_popup(b) returns 1\n");
668 /* Resetting all proporties of syspopup */
672 snprintf(key, SYSPOPUP_PARAM_LEN, "%d", SYSPOPUP_TYPE);
673 const char* type = bundle_get_val(b, (const char *)key);
675 USB_LOG(USB_LOG_VERBOSE, "ERROR: Non existing type of popup\n");
678 ad->type = atoi(type);
679 USB_LOG(USB_LOG_VERBOSE, "ad->type is (%d)\n", ad->type);
684 USB_LOG(USB_LOG_VERBOSE, "Connection failed popup is loaded\n");
685 load_connection_failed_popup(ad);
687 case SELECT_PKG_FOR_ACC_POPUP:
688 USB_LOG(USB_LOG_VERBOSE, "Select pkg for acc popup is loaded\n");
689 load_select_pkg_for_acc_popup(ad);
691 case REQ_ACC_PERM_POPUP:
692 USB_LOG(USB_LOG_VERBOSE, "Request Permission popup is loaded\n");
693 load_request_perm_popup(ad);
695 USB_LOG(USB_LOG_VERBOSE, "ERROR: The popup type(%d) does not exist\n", ad->type);
704 int main(int argc, char *argv[])
709 struct appcore_ops ops = {
710 .create = __app_create,
711 .terminate = __app_terminate,
712 .pause = __app_pause,
713 .resume = __app_resume,
714 .reset = __app_reset,
717 memset(&ad, 0x0, sizeof(struct appdata));
721 return appcore_efl_main(PACKAGE, &argc, &argv, &ops);