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 <aul_rsm_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;
416 wc->id = g_strdup(id);
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 %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 aul_rsm_provider_init();
896 elm_init(argc, argv);
898 r = aul_launch_init(__aul_handler, NULL);
900 /* LCOV_EXCL_START */
901 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
903 "Fail to call the aul_launch_init");
907 r = aul_launch_argv_handler(argc, argv);
909 /* LCOV_EXCL_START */
910 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
912 "Fail to call the aul_launch_argv_handler");
916 r = app_get_id(&appid);
917 if (r != APP_ERROR_NONE)
920 name = __get_domain_name(appid);
923 /* LCOV_EXCL_START */
924 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
926 "Fail to call __get_domain_name");
933 /* LCOV_EXCL_START */
934 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
936 "Fail to call _set_i18n");
942 _widget_core_set_appcore_event_cb();
944 class_provider = app_ops->create(app_user_data);
945 if (class_provider == NULL) {
946 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
947 __FUNCTION__, "widget_class is NULL");
950 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
952 return WIDGET_ERROR_NONE;
955 static void __after_loop()
958 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
960 __pause_all(UPDATE_LOCAL);
961 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
963 if (app_ops->terminate)
964 app_ops->terminate(app_user_data);
966 aul_rsm_provider_fini();
968 _widget_app_free_viewer_endpoint();
969 _widget_core_unset_appcore_event_cb();
970 __free_handler_list();
980 static void __on_low_memory(keynode_t *key, void *data)
984 val = vconf_keynode_get_int(key);
985 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
986 app_event_handler_h handler;
987 struct app_event_info event;
989 _I("widget_app_low_memory");
991 event.type = APP_EVENT_LOW_MEMORY;
992 event.value = (void *)&val;
994 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
997 handler = (app_event_handler_h) iter->data;
998 handler->cb(&event, handler->data);
999 iter = g_list_next(iter);
1004 static void __on_low_battery(keynode_t *key, void *data)
1008 val = vconf_keynode_get_int(key);
1009 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1010 app_event_handler_h handler;
1011 struct app_event_info event;
1013 _I("widget_app_low_battery");
1015 event.type = APP_EVENT_LOW_BATTERY;
1016 event.value = (void *)&val;
1018 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1021 handler = (app_event_handler_h) iter->data;
1022 handler->cb(&event, handler->data);
1023 iter = g_list_next(iter);
1028 static void __on_lang_changed(keynode_t *key, void *data)
1033 val = vconf_keynode_get_str(key);
1035 app_event_handler_h handler;
1036 struct app_event_info event;
1038 _I("widget_app_lang_changed");
1040 event.type = APP_EVENT_LANGUAGE_CHANGED;
1041 event.value = (void *)val;
1043 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1046 handler = (app_event_handler_h) iter->data;
1047 handler->cb(&event, handler->data);
1048 iter = g_list_next(iter);
1052 static void __on_region_changed(keynode_t *key, void *data)
1057 val = vconf_keynode_get_str(key);
1059 app_event_handler_h handler;
1060 struct app_event_info event;
1062 _I("widget_app_region_changed");
1064 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1065 event.value = (void *)val;
1067 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1070 handler = (app_event_handler_h) iter->data;
1071 handler->cb(&event, handler->data);
1072 iter = g_list_next(iter);
1076 static void __register_event(int event_type)
1078 switch (event_type) {
1079 case APP_EVENT_LOW_MEMORY:
1080 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1083 case APP_EVENT_LOW_BATTERY:
1084 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1087 case APP_EVENT_LANGUAGE_CHANGED:
1088 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1091 case APP_EVENT_REGION_FORMAT_CHANGED:
1092 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1097 static void __unregister_event(int event_type)
1099 switch (event_type) {
1100 case APP_EVENT_LOW_MEMORY:
1101 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1104 case APP_EVENT_LOW_BATTERY:
1105 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1108 case APP_EVENT_LANGUAGE_CHANGED:
1109 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1112 case APP_EVENT_REGION_FORMAT_CHANGED:
1113 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1118 static void _widget_core_set_appcore_event_cb(void)
1120 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1121 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1124 static void _widget_core_unset_appcore_event_cb(void)
1126 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1127 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1130 EXPORT_API int widget_app_main(int argc, char **argv,
1131 widget_app_lifecycle_callback_s *callback, void *user_data)
1135 if (!_is_widget_feature_enabled()) {
1136 _E("not supported"); /* LCOV_EXCL_LINE */
1137 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1140 if (argc <= 0 || argv == NULL || callback == NULL)
1141 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1142 __FUNCTION__, NULL);
1144 if (callback->create == NULL)
1145 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1147 "widget_app_create_cb() callback must be "
1150 app_user_data = user_data;
1151 r = __before_loop(argc, argv);
1155 ecore_main_loop_begin();
1156 aul_status_update(STATUS_DYING);
1159 return WIDGET_ERROR_NONE;
1162 EXPORT_API int widget_app_exit(void)
1164 if (!_is_widget_feature_enabled()) {
1165 _E("not supported"); /* LCOV_EXCL_LINE */
1166 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1170 return WIDGET_ERROR_NONE;
1174 ecore_main_loop_quit();
1176 return WIDGET_ERROR_NONE;
1179 /* LCOV_EXCL_START */
1180 static gboolean __finish_event_cb(gpointer user_data)
1182 if (user_data == NULL)
1185 widget_context_s *wc = (widget_context_s *)user_data;
1187 switch (wc->state) {
1189 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1191 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1193 __instance_destroy(wc->provider, wc->id,
1194 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1202 /* LCOV_EXCL_STOP */
1204 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1206 if (!_is_widget_feature_enabled()) {
1207 _E("not supported"); /* LCOV_EXCL_LINE */
1208 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1211 if (context == NULL)
1212 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1213 __FUNCTION__, NULL);
1215 g_idle_add(__finish_event_cb, context);
1216 return WIDGET_ERROR_NONE;
1219 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1221 GList *contexts = _widget_app_get_contexts();
1223 widget_context_s *wc;
1225 if (!_is_widget_feature_enabled()) {
1226 _E("not supported"); /* LCOV_EXCL_LINE */
1227 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1231 return WIDGET_ERROR_INVALID_PARAMETER;
1233 list = g_list_first(contexts);
1236 wc = (widget_context_s *)list->data;
1244 return WIDGET_ERROR_NONE;
1247 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1248 app_event_type_e event_type, app_event_cb callback,
1254 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1256 return WIDGET_ERROR_FAULT;
1259 return WIDGET_ERROR_NOT_SUPPORTED;
1261 app_event_handler_h handler;
1263 if (event_handler == NULL || callback == NULL)
1264 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1266 if (event_type < APP_EVENT_LOW_MEMORY
1267 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1268 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1270 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1271 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1273 GList *iter = g_list_first(handler_list[event_type]);
1276 handler = (app_event_handler_h) iter->data;
1278 if (handler->cb == callback)
1279 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1281 iter = g_list_next(iter);
1284 handler = calloc(1, sizeof(struct app_event_handler));
1286 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1288 if (g_list_length(handler_list[event_type]) == 0)
1289 __register_event(event_type);
1291 handler->type = event_type;
1292 handler->cb = callback;
1293 handler->data = user_data;
1294 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1296 *event_handler = handler;
1298 return WIDGET_ERROR_NONE;
1301 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1307 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1309 return WIDGET_ERROR_FAULT;
1312 return WIDGET_ERROR_NOT_SUPPORTED;
1314 app_event_type_e type;
1316 if (event_handler == NULL)
1317 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1319 type = event_handler->type;
1320 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1321 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1323 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1324 free(event_handler);
1326 if (g_list_length(handler_list[type]) == 0)
1327 __unregister_event(type);
1329 return WIDGET_ERROR_NONE;
1332 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1334 if (!_is_widget_feature_enabled()) {
1335 _E("not supported"); /* LCOV_EXCL_LINE */
1336 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1337 return NULL; /* LCOV_EXCL_LINE */
1341 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1345 set_last_result(WIDGET_ERROR_NONE);
1349 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1352 widget_context_s *cxt = (widget_context_s *)context;
1353 Evas_Object *ret_win;
1354 Ecore_Wl_Window *wl_win;
1355 struct wl_surface *surface;
1357 if (!_is_widget_feature_enabled()) {
1358 _E("not supported"); /* LCOV_EXCL_LINE */
1359 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1362 if (context == NULL || win == NULL)
1363 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1364 __FUNCTION__, NULL);
1366 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1367 if (ret_win == NULL) {
1368 _E("failed to create window"); /* LCOV_EXCL_LINE */
1369 goto fault; /* LCOV_EXCL_LINE */
1372 wl_win = elm_win_wl_window_get(ret_win);
1373 if (wl_win == NULL) {
1374 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1378 surface = ecore_wl_window_surface_get(wl_win);
1379 if (surface == NULL) {
1380 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1381 goto fault; /* LCOV_EXCL_LINE */
1384 aul_rsm_provider_remote_enable(cxt->id, surface);
1386 ecore_wl_window_class_name_set(wl_win, cxt->id);
1387 elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1391 cxt->win_id = ecore_wl_window_id_get(wl_win);
1393 _D("window created: %d", cxt->win_id);
1395 return WIDGET_ERROR_NONE;
1398 if (ret_win) /* LCOV_EXCL_LINE */
1399 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1401 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1405 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1406 widget_instance_lifecycle_callback_s callback, void *user_data)
1410 if (!_is_widget_feature_enabled()) {
1411 _E("not supported"); /* LCOV_EXCL_LINE */
1412 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1416 if (class_id == NULL) {
1417 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1421 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1423 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1424 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1425 return NULL; /* LCOV_EXCL_LINE */
1428 wc->classid = strdup(class_id);
1429 wc->user_data = user_data;
1434 set_last_result(WIDGET_ERROR_NONE);
1442 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1443 const char *class_id,
1444 widget_instance_lifecycle_callback_s callback, void *user_data)
1446 return _widget_class_create(widget_class, class_id, callback,
1450 EXPORT_API widget_class_h widget_app_class_create(
1451 widget_instance_lifecycle_callback_s callback, void *user_data)
1453 return _widget_class_create(class_provider, appid, callback, user_data);
1456 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1458 if (!_is_widget_feature_enabled()) {
1459 _E("not supported"); /* LCOV_EXCL_LINE */
1460 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1463 if (context == NULL)
1464 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1465 __FUNCTION__, NULL);
1469 return WIDGET_ERROR_NONE;
1472 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1474 if (!_is_widget_feature_enabled()) {
1475 _E("not supported"); /* LCOV_EXCL_LINE */
1476 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1479 if (context == NULL || tag == NULL)
1480 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1481 __FUNCTION__, NULL);
1483 *tag = context->tag;
1485 return WIDGET_ERROR_NONE;
1488 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1489 bundle *content_info)
1491 const char *class_id = NULL;
1493 bundle_raw *raw = NULL;
1496 if (!_is_widget_feature_enabled()) {
1497 _E("not supported"); /* LCOV_EXCL_LINE */
1498 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1501 if (context == NULL || content_info == NULL)
1502 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1503 __FUNCTION__, NULL);
1505 if (context->provider == NULL)
1506 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1507 __FUNCTION__, NULL);
1509 class_id = context->provider->classid;
1510 if (class_id == NULL)
1511 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1513 ret = __send_update_status(class_id, context->id,
1514 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1516 if (context->content)
1517 free(context->content);
1519 bundle_encode(content_info, &raw, &len);
1521 context->content = strdup((const char *)raw);
1523 context->content = NULL;
1526 /* LCOV_EXCL_START */
1527 _E("failed to send content info: %s of %s (%d)", context->id,
1529 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1531 /* LCOV_EXCL_STOP */
1534 return WIDGET_ERROR_NONE;
1537 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1540 if (!_is_widget_feature_enabled()) {
1541 _E("not supported"); /* LCOV_EXCL_LINE */
1542 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1545 if (!context || !title)
1546 return WIDGET_ERROR_INVALID_PARAMETER;
1549 elm_win_title_set(context->win, title);
1551 return WIDGET_ERROR_NONE;