2 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <bundle_internal.h>
26 #include <glib-object.h>
28 #include <app_control.h>
29 #include <app_control_internal.h>
30 #include <Elementary.h>
31 #include <widget_errno.h>
32 #include <widget_instance.h>
33 #include <widget_service.h>
34 #include <widget_service_internal.h>
35 #include <aul_app_com.h>
36 #include <Ecore_Wayland.h>
37 #include <system_info.h>
39 #include <vconf-internal-keys.h>
40 #include <screen_connector_provider.h>
42 #include "widget_app.h"
43 #include "widget-log.h"
44 #include "widget-private.h"
45 #include "widget_app_internal.h"
51 #define STR_MAX_BUF 128
52 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
53 #define K_REASON "__WC_K_REASON__"
54 #define APP_TYPE_WIDGET "widgetapp"
55 #define STATUS_FOREGROUND "fg"
56 #define STATUS_BACKGROUND "bg"
58 typedef enum _widget_obj_state_e {
70 struct app_event_handler {
71 app_event_type_e type;
76 struct app_event_info {
77 app_event_type_e type;
81 typedef struct _widget_class widget_class_s;
83 #define WIDGET_APP_EVENT_MAX 5
84 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
86 static int caller_pid;
87 static widget_app_lifecycle_callback_s *app_ops;
88 static void *app_user_data;
90 static widget_class_h class_provider;
91 static int exit_called;
92 static char *package_id;
93 static bool fg_signal;
95 static void _widget_core_set_appcore_event_cb(void);
96 static void _widget_core_unset_appcore_event_cb(void);
98 static void __free_handler_cb(gpointer data)
104 static void __free_handler_list(void)
108 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
109 g_list_free_full(handler_list[i], __free_handler_cb);
110 handler_list[i] = NULL;
114 static inline bool _is_widget_feature_enabled(void)
116 static bool feature = false;
117 static bool retrieved = false;
120 if (retrieved == true)
123 ret = system_info_get_platform_bool(
124 "http://tizen.org/feature/shell.appwidget", &feature);
125 if (ret != SYSTEM_INFO_ERROR_NONE) {
126 _E("failed to get system info"); /* LCOV_EXCL_LINE */
127 return false; /* LCOV_EXCL_LINE */
135 static gint __comp_by_id(gconstpointer a, gconstpointer b)
137 widget_context_s *wc = (widget_context_s *)a;
139 return strcmp(wc->id, (const char *)b);
142 static widget_context_s *__find_context_by_id(const char *id)
145 GList *contexts = _widget_app_get_contexts();
150 ret = g_list_find_custom(contexts, id, __comp_by_id);
157 static gint __comp_by_state(gconstpointer a, gconstpointer b)
159 widget_context_s *wc = (widget_context_s *)a;
161 if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
167 static widget_context_s *__find_context_by_state(widget_obj_state_e state)
170 GList *contexts = _widget_app_get_contexts();
172 ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
179 static gint __comp_by_win(gconstpointer a, gconstpointer b)
181 int win = GPOINTER_TO_INT(b);
182 widget_context_s *wc = (widget_context_s *)a;
184 return (wc && wc->win_id == win) ? 0 : -1;
187 static widget_context_s *__find_context_by_win(int win)
189 GList *contexts = _widget_app_get_contexts();
190 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
198 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
201 bundle *b = bundle_create();
202 char pkgid[256] = {0, };
206 _E("out of memory"); /* LCOV_EXCL_LINE */
207 return -1; /* LCOV_EXCL_LINE */
210 if (package_id == NULL) {
211 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
213 package_id = strdup(pkgid);
216 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
217 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
218 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
220 bundle_add_str(b, AUL_K_PKGID, package_id);
222 _D("send lifecycle %s(%d)", instance_id, status);
223 ret = aul_app_com_send("widget.status", b);
225 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
232 static int __send_update_status(const char *class_id, const char *instance_id,
233 int status, bundle *extra)
237 bundle_raw *raw = NULL;
239 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
243 _E("out of memory"); /* LCOV_EXCL_LINE */
244 return -1; /* LCOV_EXCL_LINE */
247 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
248 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
249 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
252 bundle_encode(extra, &raw, &len);
253 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
254 aul_widget_instance_add(class_id, instance_id);
257 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
258 aul_app_com_send(viewer_endpoint, b);
261 case WIDGET_INSTANCE_EVENT_CREATE:
262 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
264 case WIDGET_INSTANCE_EVENT_DESTROY:
265 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
267 case WIDGET_INSTANCE_EVENT_PAUSE:
268 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
270 case WIDGET_INSTANCE_EVENT_RESUME:
271 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
276 __send_lifecycle_event(class_id, instance_id, lifecycle);
285 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
287 widget_context_s *wc = __find_context_by_id(id);
291 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
292 return -1; /* LCOV_EXCL_LINE */
295 if (wc->state == WC_RUNNING) {
296 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
297 return 0; /* LCOV_EXCL_LINE */
300 if (wc->state == WC_TERMINATED) {
301 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
302 return 0; /* LCOV_EXCL_LINE */
305 if (handle->ops.resume)
306 handle->ops.resume(wc, handle->user_data);
308 wc->state = WC_RUNNING;
309 _D("%s is resumed", id);
311 ret = __send_update_status(handle->classid, wc->id,
312 WIDGET_INSTANCE_EVENT_RESUME, NULL);
314 _D("Send fg signal to resourceD");
315 aul_send_app_status_change_signal(getpid(),
329 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
331 widget_context_s *wc = __find_context_by_id(id);
335 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
336 return -1; /* LCOV_EXCL_LINE */
339 if (wc->state == WC_PAUSED) {
340 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
341 return 0; /* LCOV_EXCL_LINE */
344 if (wc->state == WC_TERMINATED) {
345 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
346 return 0; /* LCOV_EXCL_LINE */
349 if (handle->ops.pause)
350 handle->ops.pause(wc, handle->user_data);
352 wc->state = WC_PAUSED;
353 _D("%s is paused", id);
355 ret = __send_update_status(handle->classid, wc->id,
356 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
357 wc = __find_context_by_state(WC_RUNNING);
358 if (!wc && fg_signal) {
359 _D("Send bg signal to resourceD");
360 aul_send_app_status_change_signal(getpid(),
374 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
376 widget_context_s *wc = __find_context_by_id(id);
380 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
381 return -1; /* LCOV_EXCL_LINE */
384 if (handle->ops.resize)
385 handle->ops.resize(wc, w, h, handle->user_data);
387 _D("%s is resized to %dx%d", id, w, h);
388 ret = __send_update_status(handle->classid, wc->id,
389 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
394 /* LCOV_EXCL_START */
395 static int __instance_update_all(widget_class_h handle, int force, const char *content)
397 widget_context_s *wc;
400 GList *context = _widget_app_get_contexts();
403 b = bundle_decode((const bundle_raw *)content, strlen(content));
405 if (handle->ops.update) {
407 wc = (widget_context_s *)context->data;
408 handle->ops.update(wc, b, force, handle->user_data);
409 ret = __send_update_status(handle->classid, wc->id,
410 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
411 _D("updated:%s", wc->id);
412 context = context->next;
423 /* LCOV_EXCL_START */
424 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
426 widget_context_s *wc = __find_context_by_id(id);
430 _E("context not found: %s", id);
435 b = bundle_decode((const bundle_raw *)content, strlen(content));
437 if (handle->ops.update) {
438 handle->ops.update(wc, b, force, handle->user_data);
439 ret = __send_update_status(handle->classid, wc->id,
440 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
441 _D("updated:%s", id);
451 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
453 widget_context_s *wc = NULL;
455 bundle *content_info = NULL;
457 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
460 return WIDGET_ERROR_OUT_OF_MEMORY;
463 wc->state = WC_READY;
465 wc->provider = handle;
470 wc->content = strdup(content);
471 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
474 _widget_app_add_context(wc);
476 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
478 _W("Create callback resturns error(%d)", ret);
479 ret = __send_update_status(handle->classid, wc->id,
480 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
481 _widget_app_remove_context(wc);
488 if (_widget_app_get_contexts() == NULL && !exit_called)
491 ret = __send_update_status(handle->classid, wc->id,
492 WIDGET_INSTANCE_EVENT_CREATE, NULL);
497 aul_widget_instance_add(handle->classid, id);
501 bundle_free(content_info);
506 static int __instance_destroy(widget_class_h handle, const char *id,
507 widget_app_destroy_type_e reason, int send_update)
509 widget_context_s *wc = __find_context_by_id(id);
511 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
512 bundle *content_info;
515 _E("could not find widget obj: %s, clear amd info", id); /* LCOV_EXCL_LINE */
516 aul_widget_instance_del(handle->classid, id); /* LCOV_EXCL_LINE */
517 return WIDGET_ERROR_NONE; /* LCOV_EXCL_LINE */
520 wc->state = WC_TERMINATED;
522 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
524 content_info = bundle_create();
526 handle->ops.destroy(wc, reason, content_info,
529 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
530 event = WIDGET_INSTANCE_EVENT_DESTROY;
531 aul_widget_instance_del(handle->classid, id);
533 ret = __send_update_status(handle->classid, id,
534 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
538 bundle_free(content_info);
540 ret = __send_update_status(handle->classid, id, event, NULL);
542 _widget_app_remove_context(wc);
552 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
558 static widget_class_h __find_class_handler(const char *class_id,
559 widget_class_h handle)
561 if (!class_id || !handle)
564 widget_class_h head = handle;
567 if (head->classid && strcmp(head->classid, class_id) == 0)
576 /* LCOV_EXCL_START */
577 static void __resize_window(char *id, int w, int h)
579 widget_context_s *wc = __find_context_by_id(id);
582 _E("can not find instance: %s", id);
587 evas_object_resize(wc->win, w, h);
589 _E("unable to find window of %d", wc->id);
593 static void __control(bundle *b)
595 char *class_id = NULL;
597 char *operation = NULL;
598 char *content = NULL;
605 char *force_str = NULL;
606 widget_class_h handle = NULL;
608 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
609 /* for previous version compatibility, use appid for default class id */
610 if (class_id == NULL)
613 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
614 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
616 handle = __find_class_handler(class_id, class_provider);
618 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
623 _E("no operation provided");
627 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
629 if (force_str && strcmp(force_str, "true") == 0)
634 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
635 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
636 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
638 w = (int)g_ascii_strtoll(w_str, &remain, 10);
641 h = (int)g_ascii_strtoll(h_str, &remain, 10);
643 if (strcmp(operation, "create") == 0) {
644 __instance_create(handle, id, content, w, h);
645 } else if (strcmp(operation, "resize") == 0) {
646 __resize_window(id, w, h);
647 } else if (strcmp(operation, "update") == 0) {
649 __instance_update(handle, id, force, content);
651 __instance_update_all(handle, force, content);
653 } else if (strcmp(operation, "destroy") == 0) {
654 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
655 } else if (strcmp(operation, "resume") == 0) {
656 __instance_resume(handle, id, UPDATE_ALL);
657 } else if (strcmp(operation, "pause") == 0) {
658 __instance_pause(handle, id, UPDATE_ALL);
659 } else if (strcmp(operation, "terminate") == 0) {
660 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
665 LOGD("error on control");
669 static void __pause_all(int send_update)
671 GList *contexts = _widget_app_get_contexts();
672 GList *iter = g_list_first(contexts);
674 while (iter != NULL) {
675 widget_context_s *cxt = (widget_context_s *)iter->data;
677 switch (cxt->state) {
679 __instance_resume(cxt->provider, cxt->id, send_update);
680 __instance_pause(cxt->provider, cxt->id, send_update);
683 __instance_pause(cxt->provider, cxt->id, send_update);
686 LOGD("pause %s", cxt->id);
687 iter = g_list_next(iter);
691 /* LCOV_EXCL_START */
692 static void __resume_all(int send_update)
694 GList *contexts = _widget_app_get_contexts();
695 GList *iter = g_list_first(contexts);
697 while (iter != NULL) {
698 widget_context_s *cxt = (widget_context_s *)iter->data;
700 switch (cxt->state) {
702 __instance_resume(cxt->provider, cxt->id, send_update);
705 __instance_resume(cxt->provider, cxt->id, send_update);
708 iter = g_list_next(iter);
713 static void __destroy_all(int reason, int send_update)
715 GList *contexts = _widget_app_get_contexts();
716 GList *iter = g_list_first(contexts);
718 __pause_all(send_update);
719 while (iter != NULL) {
720 widget_context_s *cxt = (widget_context_s *)iter->data;
721 iter = g_list_next(iter);
722 switch (cxt->state) {
724 LOGD("destroy %d : %s", cxt->state, cxt->id);
725 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
732 static Eina_Bool __show_cb(void *data, int type, void *event)
734 Ecore_Wl_Event_Window_Show *ev = event;
735 widget_context_s *cxt = __find_context_by_win(ev->win);
737 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
740 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
742 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
744 return ECORE_CALLBACK_RENEW;
747 static Eina_Bool __hide_cb(void *data, int type, void *event)
749 Ecore_Wl_Event_Window_Hide *ev = event;
750 widget_context_s *cxt = __find_context_by_win(ev->win);
753 LOGD("hide %d", (unsigned int)ev->win);
756 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
758 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
760 return ECORE_CALLBACK_RENEW;
763 static Eina_Bool __visibility_cb(void *data, int type, void *event)
765 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
766 widget_context_s *cxt = __find_context_by_win(ev->win);
768 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
771 LOGE("unknown window error: %d", ev->win);
772 return ECORE_CALLBACK_RENEW;
775 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
776 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
777 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
778 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
780 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
783 return ECORE_CALLBACK_RENEW;
786 /* LCOV_EXCL_START */
787 static Eina_Bool __lower_cb(void *data, int type, void *event)
790 return ECORE_CALLBACK_RENEW;
794 static Eina_Bool __configure_cb(void *data, int type, void *event)
796 Ecore_Wl_Event_Window_Configure *ev = event;
797 widget_context_s *cxt = __find_context_by_win(ev->win);
799 LOGD("configure: %d %d", ev->w, ev->h);
802 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
803 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
806 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
807 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
808 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
810 return ECORE_CALLBACK_RENEW;
813 static void __add_climsg()
815 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
816 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
817 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
818 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
819 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
822 static void __get_content(bundle *b)
824 char *instance_id = NULL;
825 widget_context_s *cxt = NULL;
827 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
831 cxt = __find_context_by_id(instance_id);
833 _E("can not find instance id:%s", instance_id);
838 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
839 _D("content info of %s found", cxt->id);
841 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
842 _D("empty content info added");
846 static int __aul_handler(aul_type type, bundle *b, void *data)
854 bundle_get_str(b, WIDGET_K_CALLER, &caller);
856 caller_pid = g_ascii_strtoll(caller, &remain,
859 /* using caller appid and query pid using caller appid? */
867 __resume_all(UPDATE_ALL);
872 case AUL_WIDGET_CONTENT:
882 static char *__get_domain_name(char *appid)
887 _E("appid is NULL"); /* LCOV_EXCL_LINE */
888 return NULL; /* LCOV_EXCL_LINE */
891 name_token = strrchr(appid, '.');
893 if (name_token == NULL) {
894 _E("appid is invalid"); /* LCOV_EXCL_LINE */
895 return appid; /* LCOV_EXCL_LINE */
903 /* LCOV_EXCL_START */
904 static void __on_poweroff(keynode_t *key, void *data)
908 val = vconf_keynode_get_int(key);
910 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
911 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
912 _I("power off changed: %d", val);
915 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
916 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
924 extern int _set_i18n(const char *name);
926 static int __before_loop(int argc, char **argv)
931 char *viewer_endpoint = NULL;
933 #if !(GLIB_CHECK_VERSION(2, 36, 0))
937 kb = bundle_import_from_argv(argc, argv);
939 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
940 if (viewer_endpoint) {
941 _E("viewer endpoint :%s", viewer_endpoint);
942 _widget_app_set_viewer_endpoint(viewer_endpoint);
944 _E("endpoint is missing");
950 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
953 screen_connector_provider_init();
954 elm_init(argc, argv);
956 r = aul_launch_init(__aul_handler, NULL);
958 /* LCOV_EXCL_START */
959 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
961 "Fail to call the aul_launch_init");
965 r = aul_launch_argv_handler(argc, argv);
967 /* LCOV_EXCL_START */
968 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
970 "Fail to call the aul_launch_argv_handler");
974 r = app_get_id(&appid);
975 if (r != APP_ERROR_NONE)
978 name = __get_domain_name(appid);
981 /* LCOV_EXCL_START */
982 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
984 "Fail to call __get_domain_name");
991 /* LCOV_EXCL_START */
992 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
994 "Fail to call _set_i18n");
1000 _widget_core_set_appcore_event_cb();
1002 class_provider = app_ops->create(app_user_data);
1003 if (class_provider == NULL) {
1004 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1005 __FUNCTION__, "widget_class is NULL");
1008 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
1010 return WIDGET_ERROR_NONE;
1013 static void __after_loop()
1016 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
1018 __pause_all(UPDATE_LOCAL);
1019 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1021 if (app_ops->terminate)
1022 app_ops->terminate(app_user_data);
1024 screen_connector_provider_fini();
1026 _widget_app_free_viewer_endpoint();
1027 _widget_core_unset_appcore_event_cb();
1028 __free_handler_list();
1043 static void __on_low_memory(keynode_t *key, void *data)
1047 val = vconf_keynode_get_int(key);
1048 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
1049 app_event_handler_h handler;
1050 struct app_event_info event;
1052 _I("widget_app_low_memory");
1054 event.type = APP_EVENT_LOW_MEMORY;
1055 event.value = (void *)&val;
1057 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
1060 handler = (app_event_handler_h) iter->data;
1061 handler->cb(&event, handler->data);
1062 iter = g_list_next(iter);
1067 static void __on_low_battery(keynode_t *key, void *data)
1071 val = vconf_keynode_get_int(key);
1072 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1073 app_event_handler_h handler;
1074 struct app_event_info event;
1076 _I("widget_app_low_battery");
1078 event.type = APP_EVENT_LOW_BATTERY;
1079 event.value = (void *)&val;
1081 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1084 handler = (app_event_handler_h) iter->data;
1085 handler->cb(&event, handler->data);
1086 iter = g_list_next(iter);
1091 static void __on_lang_changed(keynode_t *key, void *data)
1096 val = vconf_keynode_get_str(key);
1098 app_event_handler_h handler;
1099 struct app_event_info event;
1101 _I("widget_app_lang_changed");
1103 event.type = APP_EVENT_LANGUAGE_CHANGED;
1104 event.value = (void *)val;
1106 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1109 handler = (app_event_handler_h) iter->data;
1110 handler->cb(&event, handler->data);
1111 iter = g_list_next(iter);
1115 static void __on_region_changed(keynode_t *key, void *data)
1120 val = vconf_keynode_get_str(key);
1122 app_event_handler_h handler;
1123 struct app_event_info event;
1125 _I("widget_app_region_changed");
1127 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1128 event.value = (void *)val;
1130 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1133 handler = (app_event_handler_h) iter->data;
1134 handler->cb(&event, handler->data);
1135 iter = g_list_next(iter);
1139 static void __register_event(int event_type)
1141 switch (event_type) {
1142 case APP_EVENT_LOW_MEMORY:
1143 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1146 case APP_EVENT_LOW_BATTERY:
1147 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1150 case APP_EVENT_LANGUAGE_CHANGED:
1151 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1154 case APP_EVENT_REGION_FORMAT_CHANGED:
1155 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1160 static void __unregister_event(int event_type)
1162 switch (event_type) {
1163 case APP_EVENT_LOW_MEMORY:
1164 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1167 case APP_EVENT_LOW_BATTERY:
1168 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1171 case APP_EVENT_LANGUAGE_CHANGED:
1172 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1175 case APP_EVENT_REGION_FORMAT_CHANGED:
1176 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1181 static void _widget_core_set_appcore_event_cb(void)
1183 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1184 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1187 static void _widget_core_unset_appcore_event_cb(void)
1189 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1190 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1193 EXPORT_API int widget_app_main(int argc, char **argv,
1194 widget_app_lifecycle_callback_s *callback, void *user_data)
1198 if (!_is_widget_feature_enabled()) {
1199 _E("not supported"); /* LCOV_EXCL_LINE */
1200 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1203 if (argc <= 0 || argv == NULL || callback == NULL)
1204 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1205 __FUNCTION__, NULL);
1207 if (callback->create == NULL)
1208 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1210 "widget_app_create_cb() callback must be "
1213 app_user_data = user_data;
1214 r = __before_loop(argc, argv);
1223 ecore_main_loop_begin();
1224 aul_status_update(STATUS_DYING);
1227 return WIDGET_ERROR_NONE;
1230 EXPORT_API int widget_app_exit(void)
1232 if (!_is_widget_feature_enabled()) {
1233 _E("not supported"); /* LCOV_EXCL_LINE */
1234 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1238 return WIDGET_ERROR_NONE;
1242 ecore_main_loop_quit();
1244 return WIDGET_ERROR_NONE;
1247 /* LCOV_EXCL_START */
1248 static gboolean __finish_event_cb(gpointer user_data)
1250 if (user_data == NULL)
1253 widget_context_s *wc = (widget_context_s *)user_data;
1255 switch (wc->state) {
1257 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1259 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1261 __instance_destroy(wc->provider, wc->id,
1262 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1270 /* LCOV_EXCL_STOP */
1272 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1274 if (!_is_widget_feature_enabled()) {
1275 _E("not supported"); /* LCOV_EXCL_LINE */
1276 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1279 if (context == NULL)
1280 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1281 __FUNCTION__, NULL);
1283 g_idle_add(__finish_event_cb, context);
1284 return WIDGET_ERROR_NONE;
1287 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1289 GList *contexts = _widget_app_get_contexts();
1291 widget_context_s *wc;
1293 if (!_is_widget_feature_enabled()) {
1294 _E("not supported"); /* LCOV_EXCL_LINE */
1295 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1299 return WIDGET_ERROR_INVALID_PARAMETER;
1301 list = g_list_first(contexts);
1304 wc = (widget_context_s *)list->data;
1312 return WIDGET_ERROR_NONE;
1315 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1316 app_event_type_e event_type, app_event_cb callback,
1322 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1324 return WIDGET_ERROR_FAULT;
1327 return WIDGET_ERROR_NOT_SUPPORTED;
1329 app_event_handler_h handler;
1331 if (event_handler == NULL || callback == NULL)
1332 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1334 if (event_type < APP_EVENT_LOW_MEMORY
1335 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1336 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1338 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1339 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1341 GList *iter = g_list_first(handler_list[event_type]);
1344 handler = (app_event_handler_h) iter->data;
1346 if (handler->cb == callback)
1347 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1349 iter = g_list_next(iter);
1352 handler = calloc(1, sizeof(struct app_event_handler));
1354 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1356 if (g_list_length(handler_list[event_type]) == 0)
1357 __register_event(event_type);
1359 handler->type = event_type;
1360 handler->cb = callback;
1361 handler->data = user_data;
1362 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1364 *event_handler = handler;
1366 return WIDGET_ERROR_NONE;
1369 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1375 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1377 return WIDGET_ERROR_FAULT;
1380 return WIDGET_ERROR_NOT_SUPPORTED;
1382 app_event_type_e type;
1384 if (event_handler == NULL)
1385 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1387 type = event_handler->type;
1388 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1389 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1391 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1392 free(event_handler);
1394 if (g_list_length(handler_list[type]) == 0)
1395 __unregister_event(type);
1397 return WIDGET_ERROR_NONE;
1400 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1402 if (!_is_widget_feature_enabled()) {
1403 _E("not supported"); /* LCOV_EXCL_LINE */
1404 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1405 return NULL; /* LCOV_EXCL_LINE */
1409 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1413 set_last_result(WIDGET_ERROR_NONE);
1417 static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
1419 /* Remove data used in accessibility */
1421 plug_id = evas_object_data_del(obj, "___PLUGID");
1425 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1428 widget_context_s *cxt = (widget_context_s *)context;
1429 Evas_Object *ret_win;
1430 Ecore_Wl_Window *wl_win;
1431 struct wl_surface *surface;
1435 if (!_is_widget_feature_enabled()) {
1436 _E("not supported"); /* LCOV_EXCL_LINE */
1437 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1440 if (context == NULL || win == NULL)
1441 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1442 __FUNCTION__, NULL);
1444 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1445 if (ret_win == NULL) {
1446 _E("failed to create window"); /* LCOV_EXCL_LINE */
1447 goto fault; /* LCOV_EXCL_LINE */
1450 elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
1451 elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
1453 wl_win = elm_win_wl_window_get(ret_win);
1454 if (wl_win == NULL) {
1455 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1459 surface = ecore_wl_window_surface_get(wl_win);
1460 if (surface == NULL) {
1461 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1462 goto fault; /* LCOV_EXCL_LINE */
1464 screen_connector_provider_remote_enable(cxt->id, surface);
1466 ecore_wl_window_class_name_set(wl_win, cxt->id);
1467 elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1471 cxt->win_id = ecore_wl_window_id_get(wl_win);
1473 /* Set data to use in accessibility */
1474 snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
1475 evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
1476 evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
1478 _D("window created: %d", cxt->win_id);
1480 return WIDGET_ERROR_NONE;
1483 if (ret_win) /* LCOV_EXCL_LINE */
1484 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1486 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1490 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1491 widget_instance_lifecycle_callback_s callback, void *user_data)
1495 if (!_is_widget_feature_enabled()) {
1496 _E("not supported"); /* LCOV_EXCL_LINE */
1497 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1501 if (class_id == NULL) {
1502 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1506 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1508 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1509 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1510 return NULL; /* LCOV_EXCL_LINE */
1513 wc->classid = strdup(class_id);
1514 wc->user_data = user_data;
1519 set_last_result(WIDGET_ERROR_NONE);
1527 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1528 const char *class_id,
1529 widget_instance_lifecycle_callback_s callback, void *user_data)
1531 return _widget_class_create(widget_class, class_id, callback,
1535 EXPORT_API widget_class_h widget_app_class_create(
1536 widget_instance_lifecycle_callback_s callback, void *user_data)
1538 return _widget_class_create(class_provider, appid, callback, user_data);
1541 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1543 if (!_is_widget_feature_enabled()) {
1544 _E("not supported"); /* LCOV_EXCL_LINE */
1545 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1548 if (context == NULL)
1549 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1550 __FUNCTION__, NULL);
1554 return WIDGET_ERROR_NONE;
1557 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1559 if (!_is_widget_feature_enabled()) {
1560 _E("not supported"); /* LCOV_EXCL_LINE */
1561 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1564 if (context == NULL || tag == NULL)
1565 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1566 __FUNCTION__, NULL);
1568 *tag = context->tag;
1570 return WIDGET_ERROR_NONE;
1573 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1574 bundle *content_info)
1576 const char *class_id = NULL;
1578 bundle_raw *raw = NULL;
1581 if (!_is_widget_feature_enabled()) {
1582 _E("not supported"); /* LCOV_EXCL_LINE */
1583 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1586 if (context == NULL || content_info == NULL)
1587 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1588 __FUNCTION__, NULL);
1590 if (context->provider == NULL)
1591 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1592 __FUNCTION__, NULL);
1594 class_id = context->provider->classid;
1595 if (class_id == NULL)
1596 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1598 ret = __send_update_status(class_id, context->id,
1599 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1601 if (context->content)
1602 free(context->content);
1604 bundle_encode(content_info, &raw, &len);
1606 context->content = strdup((const char *)raw);
1608 context->content = NULL;
1612 /* LCOV_EXCL_START */
1613 _E("failed to send content info: %s of %s (%d)", context->id,
1615 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1617 /* LCOV_EXCL_STOP */
1620 return WIDGET_ERROR_NONE;
1623 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1626 if (!_is_widget_feature_enabled()) {
1627 _E("not supported"); /* LCOV_EXCL_LINE */
1628 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1631 if (!context || !title)
1632 return WIDGET_ERROR_INVALID_PARAMETER;
1635 elm_win_title_set(context->win, title);
1637 return WIDGET_ERROR_NONE;