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.
21 #include <bundle_internal.h>
25 #include <glib-object.h>
27 #include <app_control.h>
28 #include <app_control_internal.h>
29 #include <Elementary.h>
30 #include <widget_errno.h>
31 #include <widget_instance.h>
32 #include <widget_service.h>
33 #include <widget_service_internal.h>
34 #include <aul_app_com.h>
35 #include <Ecore_Wayland.h>
36 #include <system_info.h>
38 #include <vconf-internal-keys.h>
39 #include <screen_connector_provider.h>
41 #include "widget_app.h"
42 #include "widget-log.h"
43 #include "widget-private.h"
44 #include "widget_app_internal.h"
50 #define STR_MAX_BUF 128
51 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
52 #define K_REASON "__WC_K_REASON__"
54 typedef enum _widget_obj_state_e {
66 struct app_event_handler {
67 app_event_type_e type;
72 struct app_event_info {
73 app_event_type_e type;
77 typedef struct _widget_class widget_class_s;
79 #define WIDGET_APP_EVENT_MAX 5
80 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
82 static int caller_pid;
83 static widget_app_lifecycle_callback_s *app_ops;
84 static void *app_user_data;
86 static widget_class_h class_provider;
87 static int exit_called;
88 static char *package_id;
90 static void _widget_core_set_appcore_event_cb(void);
91 static void _widget_core_unset_appcore_event_cb(void);
93 static void __free_handler_cb(gpointer data)
99 static void __free_handler_list(void)
103 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
104 g_list_free_full(handler_list[i], __free_handler_cb);
105 handler_list[i] = NULL;
109 static inline bool _is_widget_feature_enabled(void)
111 static bool feature = false;
112 static bool retrieved = false;
115 if (retrieved == true)
118 ret = system_info_get_platform_bool(
119 "http://tizen.org/feature/shell.appwidget", &feature);
120 if (ret != SYSTEM_INFO_ERROR_NONE) {
121 _E("failed to get system info"); /* LCOV_EXCL_LINE */
122 return false; /* LCOV_EXCL_LINE */
130 static gint __comp_by_id(gconstpointer a, gconstpointer b)
132 widget_context_s *wc = (widget_context_s *)a;
134 return strcmp(wc->id, (const char *)b);
137 static widget_context_s *__find_context_by_id(const char *id)
140 GList *contexts = _widget_app_get_contexts();
145 ret = g_list_find_custom(contexts, id, __comp_by_id);
152 static gint __comp_by_win(gconstpointer a, gconstpointer b)
154 int win = GPOINTER_TO_INT(b);
155 widget_context_s *wc = (widget_context_s *)a;
157 return (wc && wc->win_id == win) ? 0 : -1;
160 static widget_context_s *__find_context_by_win(int win)
162 GList *contexts = _widget_app_get_contexts();
163 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
171 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
174 bundle *b = bundle_create();
175 char pkgid[256] = {0, };
179 _E("out of memory"); /* LCOV_EXCL_LINE */
180 return -1; /* LCOV_EXCL_LINE */
183 if (package_id == NULL) {
184 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
186 package_id = strdup(pkgid);
189 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
190 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
191 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
193 bundle_add_str(b, AUL_K_PKGID, package_id);
195 _D("send lifecycle %s(%d)", instance_id, status);
196 ret = aul_app_com_send("widget.status", b);
198 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
205 static int __send_update_status(const char *class_id, const char *instance_id,
206 int status, bundle *extra)
210 bundle_raw *raw = NULL;
212 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
216 _E("out of memory"); /* LCOV_EXCL_LINE */
217 return -1; /* LCOV_EXCL_LINE */
220 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
221 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
222 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
225 bundle_encode(extra, &raw, &len);
226 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
227 aul_widget_instance_add(class_id, instance_id);
230 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
231 aul_app_com_send(viewer_endpoint, b);
234 case WIDGET_INSTANCE_EVENT_CREATE:
235 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
237 case WIDGET_INSTANCE_EVENT_DESTROY:
238 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
240 case WIDGET_INSTANCE_EVENT_PAUSE:
241 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
243 case WIDGET_INSTANCE_EVENT_RESUME:
244 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
249 __send_lifecycle_event(class_id, instance_id, lifecycle);
258 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
260 widget_context_s *wc = __find_context_by_id(id);
264 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
265 return -1; /* LCOV_EXCL_LINE */
268 if (wc->state == WC_RUNNING) {
269 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
270 return 0; /* LCOV_EXCL_LINE */
273 if (wc->state == WC_TERMINATED) {
274 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
275 return 0; /* LCOV_EXCL_LINE */
278 if (handle->ops.resume)
279 handle->ops.resume(wc, handle->user_data);
281 wc->state = WC_RUNNING;
282 _D("%s is resumed", id);
284 ret = __send_update_status(handle->classid, wc->id,
285 WIDGET_INSTANCE_EVENT_RESUME, NULL);
293 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
295 widget_context_s *wc = __find_context_by_id(id);
299 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
300 return -1; /* LCOV_EXCL_LINE */
303 if (wc->state == WC_PAUSED) {
304 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
305 return 0; /* LCOV_EXCL_LINE */
308 if (wc->state == WC_TERMINATED) {
309 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
310 return 0; /* LCOV_EXCL_LINE */
313 if (handle->ops.pause)
314 handle->ops.pause(wc, handle->user_data);
316 wc->state = WC_PAUSED;
317 _D("%s is paused", id);
319 ret = __send_update_status(handle->classid, wc->id,
320 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
328 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
330 widget_context_s *wc = __find_context_by_id(id);
334 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
335 return -1; /* LCOV_EXCL_LINE */
338 if (handle->ops.resize)
339 handle->ops.resize(wc, w, h, handle->user_data);
341 _D("%s is resized to %dx%d", id, w, h);
342 ret = __send_update_status(handle->classid, wc->id,
343 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
348 /* LCOV_EXCL_START */
349 static int __instance_update_all(widget_class_h handle, int force, const char *content)
351 widget_context_s *wc;
354 GList *context = _widget_app_get_contexts();
357 b = bundle_decode((const bundle_raw *)content, strlen(content));
359 if (handle->ops.update) {
361 wc = (widget_context_s *)context->data;
362 handle->ops.update(wc, b, force, handle->user_data);
363 ret = __send_update_status(handle->classid, wc->id,
364 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
365 _D("updated:%s", wc->id);
366 context = context->next;
377 /* LCOV_EXCL_START */
378 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
380 widget_context_s *wc = __find_context_by_id(id);
384 _E("context not found: %s", id);
389 b = bundle_decode((const bundle_raw *)content, strlen(content));
391 if (handle->ops.update) {
392 handle->ops.update(wc, b, force, handle->user_data);
393 ret = __send_update_status(handle->classid, wc->id,
394 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
395 _D("updated:%s", id);
405 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
407 widget_context_s *wc = NULL;
409 bundle *content_info = NULL;
411 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
413 return WIDGET_ERROR_OUT_OF_MEMORY;
415 wc->state = WC_READY;
417 wc->provider = handle;
422 wc->content = strdup(content);
423 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
426 _widget_app_add_context(wc);
428 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
430 /* TODO send abort */
432 ret = __send_update_status(handle->classid, wc->id,
433 WIDGET_INSTANCE_EVENT_CREATE, NULL);
438 aul_widget_instance_add(handle->classid, id);
442 bundle_free(content_info);
447 static int __instance_destroy(widget_class_h handle, const char *id,
448 widget_app_destroy_type_e reason, int send_update)
450 widget_context_s *wc = __find_context_by_id(id);
452 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
453 bundle *content_info;
456 _E("could not find widget obj: %s, clear amd info", id); /* LCOV_EXCL_LINE */
457 aul_widget_instance_del(handle->classid, id); /* LCOV_EXCL_LINE */
458 return WIDGET_ERROR_NONE; /* LCOV_EXCL_LINE */
461 wc->state = WC_TERMINATED;
463 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
465 content_info = bundle_create();
467 handle->ops.destroy(wc, reason, content_info,
470 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
471 event = WIDGET_INSTANCE_EVENT_DESTROY;
472 aul_widget_instance_del(handle->classid, id);
474 ret = __send_update_status(handle->classid, id,
475 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
479 bundle_free(content_info);
481 ret = __send_update_status(handle->classid, id, event, NULL);
483 _widget_app_remove_context(wc);
493 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
499 static widget_class_h __find_class_handler(const char *class_id,
500 widget_class_h handle)
502 if (!class_id || !handle)
505 widget_class_h head = handle;
508 if (head->classid && strcmp(head->classid, class_id) == 0)
517 /* LCOV_EXCL_START */
518 static void __resize_window(char *id, int w, int h)
520 widget_context_s *wc = __find_context_by_id(id);
523 _E("can not find instance: %s", id);
528 evas_object_resize(wc->win, w, h);
530 _E("unable to find window of %d", wc->id);
534 static void __control(bundle *b)
536 char *class_id = NULL;
538 char *operation = NULL;
539 char *content = NULL;
546 char *force_str = NULL;
547 widget_class_h handle = NULL;
549 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
550 /* for previous version compatibility, use appid for default class id */
551 if (class_id == NULL)
554 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
555 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
557 handle = __find_class_handler(class_id, class_provider);
559 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
564 _E("no operation provided");
568 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
570 if (force_str && strcmp(force_str, "true") == 0)
575 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
576 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
577 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
579 w = (int)g_ascii_strtoll(w_str, &remain, 10);
582 h = (int)g_ascii_strtoll(h_str, &remain, 10);
584 if (strcmp(operation, "create") == 0) {
585 __instance_create(handle, id, content, w, h);
586 } else if (strcmp(operation, "resize") == 0) {
587 __resize_window(id, w, h);
588 } else if (strcmp(operation, "update") == 0) {
590 __instance_update(handle, id, force, content);
592 __instance_update_all(handle, force, content);
594 } else if (strcmp(operation, "destroy") == 0) {
595 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
596 } else if (strcmp(operation, "resume") == 0) {
597 __instance_resume(handle, id, UPDATE_ALL);
598 } else if (strcmp(operation, "pause") == 0) {
599 __instance_pause(handle, id, UPDATE_ALL);
600 } else if (strcmp(operation, "terminate") == 0) {
601 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
606 LOGD("error on control");
610 static void __pause_all(int send_update)
612 GList *contexts = _widget_app_get_contexts();
613 GList *iter = g_list_first(contexts);
615 while (iter != NULL) {
616 widget_context_s *cxt = (widget_context_s *)iter->data;
618 switch (cxt->state) {
620 __instance_resume(cxt->provider, cxt->id, send_update);
621 __instance_pause(cxt->provider, cxt->id, send_update);
624 __instance_pause(cxt->provider, cxt->id, send_update);
627 LOGD("pause %s", cxt->id);
628 iter = g_list_next(iter);
632 /* LCOV_EXCL_START */
633 static void __resume_all(int send_update)
635 GList *contexts = _widget_app_get_contexts();
636 GList *iter = g_list_first(contexts);
638 while (iter != NULL) {
639 widget_context_s *cxt = (widget_context_s *)iter->data;
641 switch (cxt->state) {
643 __instance_resume(cxt->provider, cxt->id, send_update);
646 __instance_resume(cxt->provider, cxt->id, send_update);
649 iter = g_list_next(iter);
654 static void __destroy_all(int reason, int send_update)
656 GList *contexts = _widget_app_get_contexts();
657 GList *iter = g_list_first(contexts);
659 __pause_all(send_update);
660 while (iter != NULL) {
661 widget_context_s *cxt = (widget_context_s *)iter->data;
662 iter = g_list_next(iter);
663 switch (cxt->state) {
665 LOGD("destroy %d : %s", cxt->state, cxt->id);
666 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
673 static Eina_Bool __show_cb(void *data, int type, void *event)
675 Ecore_Wl_Event_Window_Show *ev = event;
676 widget_context_s *cxt = __find_context_by_win(ev->win);
678 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
681 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
683 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
685 return ECORE_CALLBACK_RENEW;
688 static Eina_Bool __hide_cb(void *data, int type, void *event)
690 Ecore_Wl_Event_Window_Hide *ev = event;
691 widget_context_s *cxt = __find_context_by_win(ev->win);
694 LOGD("hide %d", (unsigned int)ev->win);
697 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
699 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
701 return ECORE_CALLBACK_RENEW;
704 static Eina_Bool __visibility_cb(void *data, int type, void *event)
706 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
707 widget_context_s *cxt = __find_context_by_win(ev->win);
709 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
712 LOGE("unknown window error: %d", ev->win);
713 return ECORE_CALLBACK_RENEW;
716 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
717 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
718 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
719 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
721 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
724 return ECORE_CALLBACK_RENEW;
727 /* LCOV_EXCL_START */
728 static Eina_Bool __lower_cb(void *data, int type, void *event)
731 return ECORE_CALLBACK_RENEW;
735 static Eina_Bool __configure_cb(void *data, int type, void *event)
737 Ecore_Wl_Event_Window_Configure *ev = event;
738 widget_context_s *cxt = __find_context_by_win(ev->win);
740 LOGD("configure: %d %d", ev->w, ev->h);
743 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
744 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
747 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
748 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
749 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
751 return ECORE_CALLBACK_RENEW;
754 static void __add_climsg()
756 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
757 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
758 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
759 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
760 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
763 static void __get_content(bundle *b)
765 char *instance_id = NULL;
766 widget_context_s *cxt = NULL;
768 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
772 cxt = __find_context_by_id(instance_id);
774 _E("can not find instance id:%s", instance_id);
779 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
780 _D("content info of %s found", cxt->id);
782 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
783 _D("empty content info added");
787 static int __aul_handler(aul_type type, bundle *b, void *data)
795 bundle_get_str(b, WIDGET_K_CALLER, &caller);
797 caller_pid = g_ascii_strtoll(caller, &remain,
800 /* using caller appid and query pid using caller appid? */
808 __resume_all(UPDATE_ALL);
813 case AUL_WIDGET_CONTENT:
823 static char *__get_domain_name(char *appid)
828 _E("appid is NULL"); /* LCOV_EXCL_LINE */
829 return NULL; /* LCOV_EXCL_LINE */
832 name_token = strrchr(appid, '.');
834 if (name_token == NULL) {
835 _E("appid is invalid"); /* LCOV_EXCL_LINE */
836 return appid; /* LCOV_EXCL_LINE */
844 /* LCOV_EXCL_START */
845 static void __on_poweroff(keynode_t *key, void *data)
849 val = vconf_keynode_get_int(key);
851 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
852 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
853 _I("power off changed: %d", val);
856 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
857 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
865 extern int _set_i18n(const char *name);
867 static int __before_loop(int argc, char **argv)
872 char *viewer_endpoint = NULL;
874 #if !(GLIB_CHECK_VERSION(2, 36, 0))
878 kb = bundle_import_from_argv(argc, argv);
880 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
881 if (viewer_endpoint) {
882 _E("viewer endpoint :%s", viewer_endpoint);
883 _widget_app_set_viewer_endpoint(viewer_endpoint);
885 _E("endpoint is missing");
891 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
894 screen_connector_provider_init();
895 elm_init(argc, argv);
897 r = aul_launch_init(__aul_handler, NULL);
899 /* LCOV_EXCL_START */
900 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
902 "Fail to call the aul_launch_init");
906 r = aul_launch_argv_handler(argc, argv);
908 /* LCOV_EXCL_START */
909 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
911 "Fail to call the aul_launch_argv_handler");
915 r = app_get_id(&appid);
916 if (r != APP_ERROR_NONE)
919 name = __get_domain_name(appid);
922 /* LCOV_EXCL_START */
923 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
925 "Fail to call __get_domain_name");
932 /* LCOV_EXCL_START */
933 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
935 "Fail to call _set_i18n");
941 _widget_core_set_appcore_event_cb();
943 class_provider = app_ops->create(app_user_data);
944 if (class_provider == NULL) {
945 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
946 __FUNCTION__, "widget_class is NULL");
949 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
951 return WIDGET_ERROR_NONE;
954 static void __after_loop()
957 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
959 __pause_all(UPDATE_LOCAL);
960 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
962 if (app_ops->terminate)
963 app_ops->terminate(app_user_data);
965 screen_connector_provider_fini();
967 _widget_app_free_viewer_endpoint();
968 _widget_core_unset_appcore_event_cb();
969 __free_handler_list();
984 static void __on_low_memory(keynode_t *key, void *data)
988 val = vconf_keynode_get_int(key);
989 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
990 app_event_handler_h handler;
991 struct app_event_info event;
993 _I("widget_app_low_memory");
995 event.type = APP_EVENT_LOW_MEMORY;
996 event.value = (void *)&val;
998 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
1001 handler = (app_event_handler_h) iter->data;
1002 handler->cb(&event, handler->data);
1003 iter = g_list_next(iter);
1008 static void __on_low_battery(keynode_t *key, void *data)
1012 val = vconf_keynode_get_int(key);
1013 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1014 app_event_handler_h handler;
1015 struct app_event_info event;
1017 _I("widget_app_low_battery");
1019 event.type = APP_EVENT_LOW_BATTERY;
1020 event.value = (void *)&val;
1022 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1025 handler = (app_event_handler_h) iter->data;
1026 handler->cb(&event, handler->data);
1027 iter = g_list_next(iter);
1032 static void __on_lang_changed(keynode_t *key, void *data)
1037 val = vconf_keynode_get_str(key);
1039 app_event_handler_h handler;
1040 struct app_event_info event;
1042 _I("widget_app_lang_changed");
1044 event.type = APP_EVENT_LANGUAGE_CHANGED;
1045 event.value = (void *)val;
1047 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1050 handler = (app_event_handler_h) iter->data;
1051 handler->cb(&event, handler->data);
1052 iter = g_list_next(iter);
1056 static void __on_region_changed(keynode_t *key, void *data)
1061 val = vconf_keynode_get_str(key);
1063 app_event_handler_h handler;
1064 struct app_event_info event;
1066 _I("widget_app_region_changed");
1068 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1069 event.value = (void *)val;
1071 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1074 handler = (app_event_handler_h) iter->data;
1075 handler->cb(&event, handler->data);
1076 iter = g_list_next(iter);
1080 static void __register_event(int event_type)
1082 switch (event_type) {
1083 case APP_EVENT_LOW_MEMORY:
1084 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1087 case APP_EVENT_LOW_BATTERY:
1088 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1091 case APP_EVENT_LANGUAGE_CHANGED:
1092 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1095 case APP_EVENT_REGION_FORMAT_CHANGED:
1096 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1101 static void __unregister_event(int event_type)
1103 switch (event_type) {
1104 case APP_EVENT_LOW_MEMORY:
1105 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1108 case APP_EVENT_LOW_BATTERY:
1109 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1112 case APP_EVENT_LANGUAGE_CHANGED:
1113 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1116 case APP_EVENT_REGION_FORMAT_CHANGED:
1117 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1122 static void _widget_core_set_appcore_event_cb(void)
1124 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1125 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1128 static void _widget_core_unset_appcore_event_cb(void)
1130 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1131 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1134 EXPORT_API int widget_app_main(int argc, char **argv,
1135 widget_app_lifecycle_callback_s *callback, void *user_data)
1139 if (!_is_widget_feature_enabled()) {
1140 _E("not supported"); /* LCOV_EXCL_LINE */
1141 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1144 if (argc <= 0 || argv == NULL || callback == NULL)
1145 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1146 __FUNCTION__, NULL);
1148 if (callback->create == NULL)
1149 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1151 "widget_app_create_cb() callback must be "
1154 app_user_data = user_data;
1155 r = __before_loop(argc, argv);
1164 ecore_main_loop_begin();
1165 aul_status_update(STATUS_DYING);
1168 return WIDGET_ERROR_NONE;
1171 EXPORT_API int widget_app_exit(void)
1173 if (!_is_widget_feature_enabled()) {
1174 _E("not supported"); /* LCOV_EXCL_LINE */
1175 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1179 return WIDGET_ERROR_NONE;
1183 ecore_main_loop_quit();
1185 return WIDGET_ERROR_NONE;
1188 /* LCOV_EXCL_START */
1189 static gboolean __finish_event_cb(gpointer user_data)
1191 if (user_data == NULL)
1194 widget_context_s *wc = (widget_context_s *)user_data;
1196 switch (wc->state) {
1198 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1200 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1202 __instance_destroy(wc->provider, wc->id,
1203 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1211 /* LCOV_EXCL_STOP */
1213 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1215 if (!_is_widget_feature_enabled()) {
1216 _E("not supported"); /* LCOV_EXCL_LINE */
1217 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1220 if (context == NULL)
1221 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1222 __FUNCTION__, NULL);
1224 g_idle_add(__finish_event_cb, context);
1225 return WIDGET_ERROR_NONE;
1228 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1230 GList *contexts = _widget_app_get_contexts();
1232 widget_context_s *wc;
1234 if (!_is_widget_feature_enabled()) {
1235 _E("not supported"); /* LCOV_EXCL_LINE */
1236 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1240 return WIDGET_ERROR_INVALID_PARAMETER;
1242 list = g_list_first(contexts);
1245 wc = (widget_context_s *)list->data;
1253 return WIDGET_ERROR_NONE;
1256 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1257 app_event_type_e event_type, app_event_cb callback,
1263 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1265 return WIDGET_ERROR_FAULT;
1268 return WIDGET_ERROR_NOT_SUPPORTED;
1270 app_event_handler_h handler;
1272 if (event_handler == NULL || callback == NULL)
1273 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1275 if (event_type < APP_EVENT_LOW_MEMORY
1276 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1277 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1279 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1280 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1282 GList *iter = g_list_first(handler_list[event_type]);
1285 handler = (app_event_handler_h) iter->data;
1287 if (handler->cb == callback)
1288 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1290 iter = g_list_next(iter);
1293 handler = calloc(1, sizeof(struct app_event_handler));
1295 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1297 if (g_list_length(handler_list[event_type]) == 0)
1298 __register_event(event_type);
1300 handler->type = event_type;
1301 handler->cb = callback;
1302 handler->data = user_data;
1303 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1305 *event_handler = handler;
1307 return WIDGET_ERROR_NONE;
1310 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1316 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1318 return WIDGET_ERROR_FAULT;
1321 return WIDGET_ERROR_NOT_SUPPORTED;
1323 app_event_type_e type;
1325 if (event_handler == NULL)
1326 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1328 type = event_handler->type;
1329 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1330 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1332 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1333 free(event_handler);
1335 if (g_list_length(handler_list[type]) == 0)
1336 __unregister_event(type);
1338 return WIDGET_ERROR_NONE;
1341 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1343 if (!_is_widget_feature_enabled()) {
1344 _E("not supported"); /* LCOV_EXCL_LINE */
1345 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1346 return NULL; /* LCOV_EXCL_LINE */
1350 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1354 set_last_result(WIDGET_ERROR_NONE);
1358 static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
1360 /* Remove data used in accessibility */
1362 plug_id = evas_object_data_del(obj, "___PLUGID");
1366 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1369 widget_context_s *cxt = (widget_context_s *)context;
1370 Evas_Object *ret_win;
1371 Ecore_Wl_Window *wl_win;
1372 struct wl_surface *surface;
1376 if (!_is_widget_feature_enabled()) {
1377 _E("not supported"); /* LCOV_EXCL_LINE */
1378 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1381 if (context == NULL || win == NULL)
1382 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1383 __FUNCTION__, NULL);
1385 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1386 if (ret_win == NULL) {
1387 _E("failed to create window"); /* LCOV_EXCL_LINE */
1388 goto fault; /* LCOV_EXCL_LINE */
1391 elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
1392 elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
1394 wl_win = elm_win_wl_window_get(ret_win);
1395 if (wl_win == NULL) {
1396 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1400 surface = ecore_wl_window_surface_get(wl_win);
1401 if (surface == NULL) {
1402 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1403 goto fault; /* LCOV_EXCL_LINE */
1405 screen_connector_provider_remote_enable(cxt->id, surface);
1407 ecore_wl_window_class_name_set(wl_win, cxt->id);
1408 elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1412 cxt->win_id = ecore_wl_window_id_get(wl_win);
1414 /* Set data to use in accessibility */
1415 snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
1416 evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
1417 evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
1419 _D("window created: %d", cxt->win_id);
1421 return WIDGET_ERROR_NONE;
1424 if (ret_win) /* LCOV_EXCL_LINE */
1425 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1427 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1431 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1432 widget_instance_lifecycle_callback_s callback, void *user_data)
1436 if (!_is_widget_feature_enabled()) {
1437 _E("not supported"); /* LCOV_EXCL_LINE */
1438 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1442 if (class_id == NULL) {
1443 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1447 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1449 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1450 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1451 return NULL; /* LCOV_EXCL_LINE */
1454 wc->classid = strdup(class_id);
1455 wc->user_data = user_data;
1460 set_last_result(WIDGET_ERROR_NONE);
1468 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1469 const char *class_id,
1470 widget_instance_lifecycle_callback_s callback, void *user_data)
1472 return _widget_class_create(widget_class, class_id, callback,
1476 EXPORT_API widget_class_h widget_app_class_create(
1477 widget_instance_lifecycle_callback_s callback, void *user_data)
1479 return _widget_class_create(class_provider, appid, callback, user_data);
1482 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1484 if (!_is_widget_feature_enabled()) {
1485 _E("not supported"); /* LCOV_EXCL_LINE */
1486 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1489 if (context == NULL)
1490 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1491 __FUNCTION__, NULL);
1495 return WIDGET_ERROR_NONE;
1498 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1500 if (!_is_widget_feature_enabled()) {
1501 _E("not supported"); /* LCOV_EXCL_LINE */
1502 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1505 if (context == NULL || tag == NULL)
1506 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1507 __FUNCTION__, NULL);
1509 *tag = context->tag;
1511 return WIDGET_ERROR_NONE;
1514 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1515 bundle *content_info)
1517 const char *class_id = NULL;
1519 bundle_raw *raw = NULL;
1522 if (!_is_widget_feature_enabled()) {
1523 _E("not supported"); /* LCOV_EXCL_LINE */
1524 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1527 if (context == NULL || content_info == NULL)
1528 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1529 __FUNCTION__, NULL);
1531 if (context->provider == NULL)
1532 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1533 __FUNCTION__, NULL);
1535 class_id = context->provider->classid;
1536 if (class_id == NULL)
1537 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1539 ret = __send_update_status(class_id, context->id,
1540 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1542 if (context->content)
1543 free(context->content);
1545 bundle_encode(content_info, &raw, &len);
1547 context->content = strdup((const char *)raw);
1549 context->content = NULL;
1553 /* LCOV_EXCL_START */
1554 _E("failed to send content info: %s of %s (%d)", context->id,
1556 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1558 /* LCOV_EXCL_STOP */
1561 return WIDGET_ERROR_NONE;
1564 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1567 if (!_is_widget_feature_enabled()) {
1568 _E("not supported"); /* LCOV_EXCL_LINE */
1569 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1572 if (!context || !title)
1573 return WIDGET_ERROR_INVALID_PARAMETER;
1576 elm_win_title_set(context->win, title);
1578 return WIDGET_ERROR_NONE;