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>
40 #include "widget_app.h"
41 #include "widget-log.h"
42 #include "widget-private.h"
43 #include "widget_app_internal.h"
49 #define STR_MAX_BUF 128
50 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
51 #define K_REASON "__WC_K_REASON__"
53 typedef enum _widget_obj_state_e {
65 struct app_event_handler {
66 app_event_type_e type;
71 struct app_event_info {
72 app_event_type_e type;
76 typedef struct _widget_class widget_class_s;
78 #define WIDGET_APP_EVENT_MAX 5
79 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
81 static int caller_pid;
82 static widget_app_lifecycle_callback_s *app_ops;
83 static void *app_user_data;
85 static widget_class_h class_provider;
86 static int exit_called;
87 static char *package_id;
89 static void _widget_core_set_appcore_event_cb(void);
90 static void _widget_core_unset_appcore_event_cb(void);
92 static void __free_handler_cb(gpointer data)
98 static void __free_handler_list(void)
102 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
103 g_list_free_full(handler_list[i], __free_handler_cb);
104 handler_list[i] = NULL;
108 static inline bool _is_widget_feature_enabled(void)
110 static bool feature = false;
111 static bool retrieved = false;
114 if (retrieved == true)
117 ret = system_info_get_platform_bool(
118 "http://tizen.org/feature/shell.appwidget", &feature);
119 if (ret != SYSTEM_INFO_ERROR_NONE) {
120 _E("failed to get system info"); /* LCOV_EXCL_LINE */
121 return false; /* LCOV_EXCL_LINE */
129 static gint __comp_by_id(gconstpointer a, gconstpointer b)
131 widget_context_s *wc = (widget_context_s *)a;
133 return strcmp(wc->id, (const char *)b);
136 static widget_context_s *__find_context_by_id(const char *id)
139 GList *contexts = _widget_app_get_contexts();
144 ret = g_list_find_custom(contexts, id, __comp_by_id);
151 static gint __comp_by_win(gconstpointer a, gconstpointer b)
153 int win = GPOINTER_TO_INT(b);
154 widget_context_s *wc = (widget_context_s *)a;
156 return (wc && wc->win_id == win) ? 0 : -1;
159 static widget_context_s *__find_context_by_win(int win)
161 GList *contexts = _widget_app_get_contexts();
162 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
170 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
173 bundle *b = bundle_create();
174 char pkgid[256] = {0, };
178 _E("out of memory"); /* LCOV_EXCL_LINE */
179 return -1; /* LCOV_EXCL_LINE */
182 if (package_id == NULL) {
183 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
185 package_id = strdup(pkgid);
188 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
189 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
190 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
192 bundle_add_str(b, AUL_K_PKGID, package_id);
194 _D("send lifecycle %s(%d)", instance_id, status);
195 ret = aul_app_com_send("widget.status", b);
197 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
204 static int __send_update_status(const char *class_id, const char *instance_id,
205 int status, bundle *extra)
209 bundle_raw *raw = NULL;
211 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
215 _E("out of memory"); /* LCOV_EXCL_LINE */
216 return -1; /* LCOV_EXCL_LINE */
219 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
220 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
221 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
224 bundle_encode(extra, &raw, &len);
225 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
226 aul_widget_instance_add(class_id, instance_id);
229 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
230 aul_app_com_send(viewer_endpoint, b);
233 case WIDGET_INSTANCE_EVENT_CREATE:
234 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
236 case WIDGET_INSTANCE_EVENT_DESTROY:
237 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
239 case WIDGET_INSTANCE_EVENT_PAUSE:
240 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
242 case WIDGET_INSTANCE_EVENT_RESUME:
243 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
248 __send_lifecycle_event(class_id, instance_id, lifecycle);
257 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
259 widget_context_s *wc = __find_context_by_id(id);
263 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
264 return -1; /* LCOV_EXCL_LINE */
267 if (wc->state == WC_RUNNING) {
268 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
269 return 0; /* LCOV_EXCL_LINE */
272 if (wc->state == WC_TERMINATED) {
273 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
274 return 0; /* LCOV_EXCL_LINE */
277 if (handle->ops.resume)
278 handle->ops.resume(wc, handle->user_data);
280 wc->state = WC_RUNNING;
281 _D("%s is resumed", id);
283 ret = __send_update_status(handle->classid, wc->id,
284 WIDGET_INSTANCE_EVENT_RESUME, NULL);
292 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
294 widget_context_s *wc = __find_context_by_id(id);
298 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
299 return -1; /* LCOV_EXCL_LINE */
302 if (wc->state == WC_PAUSED) {
303 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
304 return 0; /* LCOV_EXCL_LINE */
307 if (wc->state == WC_TERMINATED) {
308 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
309 return 0; /* LCOV_EXCL_LINE */
312 if (handle->ops.pause)
313 handle->ops.pause(wc, handle->user_data);
315 wc->state = WC_PAUSED;
316 _D("%s is paused", id);
318 ret = __send_update_status(handle->classid, wc->id,
319 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
327 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
329 widget_context_s *wc = __find_context_by_id(id);
333 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
334 return -1; /* LCOV_EXCL_LINE */
337 if (handle->ops.resize)
338 handle->ops.resize(wc, w, h, handle->user_data);
340 _D("%s is resized to %dx%d", id, w, h);
341 ret = __send_update_status(handle->classid, wc->id,
342 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
347 /* LCOV_EXCL_START */
348 static int __instance_update_all(widget_class_h handle, int force, const char *content)
350 widget_context_s *wc;
353 GList *context = _widget_app_get_contexts();
356 b = bundle_decode((const bundle_raw *)content, strlen(content));
358 if (handle->ops.update) {
360 wc = (widget_context_s *)context->data;
361 handle->ops.update(wc, b, force, handle->user_data);
362 ret = __send_update_status(handle->classid, wc->id,
363 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
364 _D("updated:%s", wc->id);
365 context = context->next;
376 /* LCOV_EXCL_START */
377 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
379 widget_context_s *wc = __find_context_by_id(id);
383 _E("context not found: %s", id);
388 b = bundle_decode((const bundle_raw *)content, strlen(content));
390 if (handle->ops.update) {
391 handle->ops.update(wc, b, force, handle->user_data);
392 ret = __send_update_status(handle->classid, wc->id,
393 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
394 _D("updated:%s", id);
404 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
406 widget_context_s *wc = NULL;
408 bundle *content_info = NULL;
410 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
412 return WIDGET_ERROR_OUT_OF_MEMORY;
414 wc->state = WC_READY;
415 wc->id = g_strdup(id);
416 wc->provider = handle;
421 wc->content = strdup(content);
422 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
425 _widget_app_add_context(wc);
427 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
429 /* TODO send abort */
431 ret = __send_update_status(handle->classid, wc->id,
432 WIDGET_INSTANCE_EVENT_CREATE, NULL);
437 aul_widget_instance_add(handle->classid, id);
441 bundle_free(content_info);
446 static int __instance_destroy(widget_class_h handle, const char *id,
447 widget_app_destroy_type_e reason, int send_update)
449 widget_context_s *wc = __find_context_by_id(id);
451 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
452 bundle *content_info;
455 _E("could not find widget obj: %s, clear amd info", id); /* LCOV_EXCL_LINE */
456 aul_widget_instance_del(handle->classid, id); /* LCOV_EXCL_LINE */
457 return WIDGET_ERROR_NONE; /* LCOV_EXCL_LINE */
460 wc->state = WC_TERMINATED;
462 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
464 content_info = bundle_create();
466 handle->ops.destroy(wc, reason, content_info,
469 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
470 event = WIDGET_INSTANCE_EVENT_DESTROY;
471 aul_widget_instance_del(handle->classid, id);
473 ret = __send_update_status(handle->classid, id,
474 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
478 bundle_free(content_info);
480 ret = __send_update_status(handle->classid, id, event, NULL);
482 _widget_app_remove_context(wc);
492 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
498 static widget_class_h __find_class_handler(const char *class_id,
499 widget_class_h handle)
501 if (!class_id || !handle)
504 widget_class_h head = handle;
507 if (head->classid && strcmp(head->classid, class_id) == 0)
516 /* LCOV_EXCL_START */
517 static void __resize_window(char *id, int w, int h)
519 widget_context_s *wc = __find_context_by_id(id);
522 _E("can not find instance: %s", id);
527 evas_object_resize(wc->win, w, h);
529 _E("unable to find window of %d", wc->id);
533 static void __control(bundle *b)
535 char *class_id = NULL;
537 char *operation = NULL;
538 char *content = NULL;
545 char *force_str = NULL;
546 widget_class_h handle = NULL;
548 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
549 /* for previous version compatibility, use appid for default class id */
550 if (class_id == NULL)
553 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
554 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
556 handle = __find_class_handler(class_id, class_provider);
558 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
563 _E("no operation provided");
567 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
569 if (force_str && strcmp(force_str, "true") == 0)
574 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
575 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
576 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
578 w = (int)g_ascii_strtoll(w_str, &remain, 10);
581 h = (int)g_ascii_strtoll(h_str, &remain, 10);
583 if (strcmp(operation, "create") == 0) {
584 __instance_create(handle, id, content, w, h);
585 } else if (strcmp(operation, "resize") == 0) {
586 __resize_window(id, w, h);
587 } else if (strcmp(operation, "update") == 0) {
589 __instance_update(handle, id, force, content);
591 __instance_update_all(handle, force, content);
593 } else if (strcmp(operation, "destroy") == 0) {
594 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
595 } else if (strcmp(operation, "resume") == 0) {
596 __instance_resume(handle, id, UPDATE_ALL);
597 } else if (strcmp(operation, "pause") == 0) {
598 __instance_pause(handle, id, UPDATE_ALL);
599 } else if (strcmp(operation, "terminate") == 0) {
600 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
605 LOGD("error on control");
609 static void __pause_all(int send_update)
611 GList *contexts = _widget_app_get_contexts();
612 GList *iter = g_list_first(contexts);
614 while (iter != NULL) {
615 widget_context_s *cxt = (widget_context_s *)iter->data;
617 switch (cxt->state) {
619 __instance_resume(cxt->provider, cxt->id, send_update);
620 __instance_pause(cxt->provider, cxt->id, send_update);
623 __instance_pause(cxt->provider, cxt->id, send_update);
626 LOGD("pause %s", cxt->id);
627 iter = g_list_next(iter);
631 /* LCOV_EXCL_START */
632 static void __resume_all(int send_update)
634 GList *contexts = _widget_app_get_contexts();
635 GList *iter = g_list_first(contexts);
637 while (iter != NULL) {
638 widget_context_s *cxt = (widget_context_s *)iter->data;
640 switch (cxt->state) {
642 __instance_resume(cxt->provider, cxt->id, send_update);
645 __instance_resume(cxt->provider, cxt->id, send_update);
648 iter = g_list_next(iter);
653 static void __destroy_all(int reason, int send_update)
655 GList *contexts = _widget_app_get_contexts();
656 GList *iter = g_list_first(contexts);
658 __pause_all(send_update);
659 while (iter != NULL) {
660 widget_context_s *cxt = (widget_context_s *)iter->data;
661 iter = g_list_next(iter);
662 switch (cxt->state) {
664 LOGD("destroy %s", cxt->state, cxt->id);
665 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
672 static Eina_Bool __show_cb(void *data, int type, void *event)
674 Ecore_Wl_Event_Window_Show *ev = event;
675 widget_context_s *cxt = __find_context_by_win(ev->win);
677 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
680 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
682 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
684 return ECORE_CALLBACK_RENEW;
687 static Eina_Bool __hide_cb(void *data, int type, void *event)
689 Ecore_Wl_Event_Window_Hide *ev = event;
690 widget_context_s *cxt = __find_context_by_win(ev->win);
693 LOGD("hide %d", (unsigned int)ev->win);
696 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
698 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
700 return ECORE_CALLBACK_RENEW;
703 static Eina_Bool __visibility_cb(void *data, int type, void *event)
705 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
706 widget_context_s *cxt = __find_context_by_win(ev->win);
708 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
711 LOGE("unknown window error: %d", ev->win);
712 return ECORE_CALLBACK_RENEW;
715 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
716 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
717 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
718 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
720 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
723 return ECORE_CALLBACK_RENEW;
726 /* LCOV_EXCL_START */
727 static Eina_Bool __lower_cb(void *data, int type, void *event)
730 return ECORE_CALLBACK_RENEW;
734 static Eina_Bool __configure_cb(void *data, int type, void *event)
736 Ecore_Wl_Event_Window_Configure *ev = event;
737 widget_context_s *cxt = __find_context_by_win(ev->win);
739 LOGD("configure: %d %d", ev->w, ev->h);
742 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
743 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
746 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
747 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
748 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
750 return ECORE_CALLBACK_RENEW;
753 static void __add_climsg()
755 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
756 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
757 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
758 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
759 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
762 static int __aul_handler(aul_type type, bundle *b, void *data)
770 bundle_get_str(b, WIDGET_K_CALLER, &caller);
772 caller_pid = g_ascii_strtoll(caller, &remain,
775 /* using caller appid and query pid using caller appid? */
783 __resume_all(UPDATE_ALL);
795 static char *__get_domain_name(char *appid)
800 _E("appid is NULL"); /* LCOV_EXCL_LINE */
801 return NULL; /* LCOV_EXCL_LINE */
804 name_token = strrchr(appid, '.');
806 if (name_token == NULL) {
807 _E("appid is invalid"); /* LCOV_EXCL_LINE */
808 return appid; /* LCOV_EXCL_LINE */
816 /* LCOV_EXCL_START */
817 static void __on_poweroff(keynode_t *key, void *data)
821 val = vconf_keynode_get_int(key);
823 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
824 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
825 _I("power off changed: %d", val);
828 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
829 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
837 extern int _set_i18n(const char *name);
839 static int __before_loop(int argc, char **argv)
843 char *wayland_display = NULL;
844 char *xdg_runtime_dir = NULL;
846 char *viewer_endpoint = NULL;
848 #if !(GLIB_CHECK_VERSION(2, 36, 0))
852 kb = bundle_import_from_argv(argc, argv);
854 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
855 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
856 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
857 if (viewer_endpoint) {
858 _E("viewer endpoint :%s", viewer_endpoint);
859 _widget_app_set_viewer_endpoint(viewer_endpoint);
861 _E("endpoint is missing");
865 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
867 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
870 setenv("WAYLAND_DISPLAY", wayland_display, 1);
872 _D("wayland_display:%s", wayland_display);
877 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
880 elm_init(argc, argv);
882 r = aul_launch_init(__aul_handler, NULL);
884 /* LCOV_EXCL_START */
885 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
887 "Fail to call the aul_launch_init");
891 r = aul_launch_argv_handler(argc, argv);
893 /* LCOV_EXCL_START */
894 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
896 "Fail to call the aul_launch_argv_handler");
900 r = app_get_id(&appid);
901 if (r != APP_ERROR_NONE)
904 name = __get_domain_name(appid);
907 /* LCOV_EXCL_START */
908 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
910 "Fail to call __get_domain_name");
917 /* LCOV_EXCL_START */
918 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
920 "Fail to call _set_i18n");
926 _widget_core_set_appcore_event_cb();
928 class_provider = app_ops->create(app_user_data);
929 if (class_provider == NULL) {
930 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
931 __FUNCTION__, "widget_class is NULL");
934 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
936 return WIDGET_ERROR_NONE;
939 static void __after_loop()
942 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
944 __pause_all(UPDATE_LOCAL);
945 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
947 if (app_ops->terminate)
948 app_ops->terminate(app_user_data);
950 _widget_app_free_viewer_endpoint();
951 _widget_core_unset_appcore_event_cb();
952 __free_handler_list();
962 static void __on_low_memory(keynode_t *key, void *data)
966 val = vconf_keynode_get_int(key);
967 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
968 app_event_handler_h handler;
969 struct app_event_info event;
971 _I("widget_app_low_memory");
973 event.type = APP_EVENT_LOW_MEMORY;
974 event.value = (void *)&val;
976 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
979 handler = (app_event_handler_h) iter->data;
980 handler->cb(&event, handler->data);
981 iter = g_list_next(iter);
986 static void __on_low_battery(keynode_t *key, void *data)
990 val = vconf_keynode_get_int(key);
991 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
992 app_event_handler_h handler;
993 struct app_event_info event;
995 _I("widget_app_low_battery");
997 event.type = APP_EVENT_LOW_BATTERY;
998 event.value = (void *)&val;
1000 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1003 handler = (app_event_handler_h) iter->data;
1004 handler->cb(&event, handler->data);
1005 iter = g_list_next(iter);
1010 static void __on_lang_changed(keynode_t *key, void *data)
1015 val = vconf_keynode_get_str(key);
1017 app_event_handler_h handler;
1018 struct app_event_info event;
1020 _I("widget_app_lang_changed");
1022 event.type = APP_EVENT_LANGUAGE_CHANGED;
1023 event.value = (void *)val;
1025 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1028 handler = (app_event_handler_h) iter->data;
1029 handler->cb(&event, handler->data);
1030 iter = g_list_next(iter);
1034 static void __on_region_changed(keynode_t *key, void *data)
1039 val = vconf_keynode_get_str(key);
1041 app_event_handler_h handler;
1042 struct app_event_info event;
1044 _I("widget_app_region_changed");
1046 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1047 event.value = (void *)val;
1049 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1052 handler = (app_event_handler_h) iter->data;
1053 handler->cb(&event, handler->data);
1054 iter = g_list_next(iter);
1058 static void __register_event(int event_type)
1060 switch (event_type) {
1061 case APP_EVENT_LOW_MEMORY:
1062 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1065 case APP_EVENT_LOW_BATTERY:
1066 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1069 case APP_EVENT_LANGUAGE_CHANGED:
1070 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1073 case APP_EVENT_REGION_FORMAT_CHANGED:
1074 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1079 static void __unregister_event(int event_type)
1081 switch (event_type) {
1082 case APP_EVENT_LOW_MEMORY:
1083 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1086 case APP_EVENT_LOW_BATTERY:
1087 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1090 case APP_EVENT_LANGUAGE_CHANGED:
1091 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1094 case APP_EVENT_REGION_FORMAT_CHANGED:
1095 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1100 static void _widget_core_set_appcore_event_cb(void)
1102 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1103 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1106 static void _widget_core_unset_appcore_event_cb(void)
1108 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1109 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1112 EXPORT_API int widget_app_main(int argc, char **argv,
1113 widget_app_lifecycle_callback_s *callback, void *user_data)
1117 if (!_is_widget_feature_enabled()) {
1118 _E("not supported"); /* LCOV_EXCL_LINE */
1119 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1122 if (argc <= 0 || argv == NULL || callback == NULL)
1123 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1124 __FUNCTION__, NULL);
1126 if (callback->create == NULL)
1127 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1129 "widget_app_create_cb() callback must be "
1133 app_user_data = user_data;
1134 r = __before_loop(argc, argv);
1138 ecore_main_loop_begin();
1139 aul_status_update(STATUS_DYING);
1142 return WIDGET_ERROR_NONE;
1145 EXPORT_API int widget_app_exit(void)
1147 if (!_is_widget_feature_enabled()) {
1148 _E("not supported"); /* LCOV_EXCL_LINE */
1149 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1153 return WIDGET_ERROR_NONE;
1157 ecore_main_loop_quit();
1159 return WIDGET_ERROR_NONE;
1162 /* LCOV_EXCL_START */
1163 static gboolean __finish_event_cb(gpointer user_data)
1165 if (user_data == NULL)
1168 widget_context_s *wc = (widget_context_s *)user_data;
1170 switch (wc->state) {
1172 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1174 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1176 __instance_destroy(wc->provider, wc->id,
1177 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1185 /* LCOV_EXCL_STOP */
1187 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1189 if (!_is_widget_feature_enabled()) {
1190 _E("not supported"); /* LCOV_EXCL_LINE */
1191 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1194 if (context == NULL)
1195 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1196 __FUNCTION__, NULL);
1198 g_idle_add(__finish_event_cb, context);
1199 return WIDGET_ERROR_NONE;
1202 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1204 GList *contexts = _widget_app_get_contexts();
1206 widget_context_s *wc;
1208 if (!_is_widget_feature_enabled()) {
1209 _E("not supported"); /* LCOV_EXCL_LINE */
1210 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1214 return WIDGET_ERROR_INVALID_PARAMETER;
1216 list = g_list_first(contexts);
1219 wc = (widget_context_s *)list->data;
1227 return WIDGET_ERROR_NONE;
1230 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1231 app_event_type_e event_type, app_event_cb callback,
1237 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1239 return WIDGET_ERROR_FAULT;
1242 return WIDGET_ERROR_NOT_SUPPORTED;
1244 app_event_handler_h handler;
1246 if (event_handler == NULL || callback == NULL)
1247 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1249 if (event_type < APP_EVENT_LOW_MEMORY
1250 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1251 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1253 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1254 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1256 GList *iter = g_list_first(handler_list[event_type]);
1259 handler = (app_event_handler_h) iter->data;
1261 if (handler->cb == callback)
1262 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1264 iter = g_list_next(iter);
1267 handler = calloc(1, sizeof(struct app_event_handler));
1269 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1271 if (g_list_length(handler_list[event_type]) == 0)
1272 __register_event(event_type);
1274 handler->type = event_type;
1275 handler->cb = callback;
1276 handler->data = user_data;
1277 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1279 *event_handler = handler;
1281 return WIDGET_ERROR_NONE;
1284 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1290 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1292 return WIDGET_ERROR_FAULT;
1295 return WIDGET_ERROR_NOT_SUPPORTED;
1297 app_event_type_e type;
1299 if (event_handler == NULL)
1300 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1302 type = event_handler->type;
1303 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1304 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1306 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1307 free(event_handler);
1309 if (g_list_length(handler_list[type]) == 0)
1310 __unregister_event(type);
1312 return WIDGET_ERROR_NONE;
1315 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1317 if (!_is_widget_feature_enabled()) {
1318 _E("not supported"); /* LCOV_EXCL_LINE */
1319 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1320 return NULL; /* LCOV_EXCL_LINE */
1324 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1328 set_last_result(WIDGET_ERROR_NONE);
1332 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1335 widget_context_s *cxt = (widget_context_s *)context;
1336 Evas_Object *ret_win;
1337 Ecore_Wl_Window *wl_win;
1339 if (!_is_widget_feature_enabled()) {
1340 _E("not supported"); /* LCOV_EXCL_LINE */
1341 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1344 if (context == NULL || win == NULL)
1345 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1346 __FUNCTION__, NULL);
1348 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1349 if (ret_win == NULL) {
1350 _E("failed to create window"); /* LCOV_EXCL_LINE */
1351 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1354 wl_win = elm_win_wl_window_get(ret_win);
1355 if (wl_win == NULL) {
1356 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1357 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1358 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1361 ecore_wl_window_class_name_set(wl_win, cxt->id);
1365 cxt->win_id = ecore_wl_window_id_get(wl_win);
1367 _D("window created: %d", cxt->win_id);
1369 return WIDGET_ERROR_NONE;
1372 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1373 widget_instance_lifecycle_callback_s callback, void *user_data)
1377 if (!_is_widget_feature_enabled()) {
1378 _E("not supported"); /* LCOV_EXCL_LINE */
1379 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1383 if (class_id == NULL) {
1384 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1388 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1390 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1391 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1392 return NULL; /* LCOV_EXCL_LINE */
1395 wc->classid = strdup(class_id);
1396 wc->user_data = user_data;
1401 set_last_result(WIDGET_ERROR_NONE);
1409 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1410 const char *class_id,
1411 widget_instance_lifecycle_callback_s callback, void *user_data)
1413 return _widget_class_create(widget_class, class_id, callback,
1417 EXPORT_API widget_class_h widget_app_class_create(
1418 widget_instance_lifecycle_callback_s callback, void *user_data)
1420 return _widget_class_create(class_provider, appid, callback, user_data);
1423 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1425 if (!_is_widget_feature_enabled()) {
1426 _E("not supported"); /* LCOV_EXCL_LINE */
1427 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1430 if (context == NULL)
1431 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1432 __FUNCTION__, NULL);
1436 return WIDGET_ERROR_NONE;
1439 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1441 if (!_is_widget_feature_enabled()) {
1442 _E("not supported"); /* LCOV_EXCL_LINE */
1443 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1446 if (context == NULL || tag == NULL)
1447 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1448 __FUNCTION__, NULL);
1450 *tag = context->tag;
1452 return WIDGET_ERROR_NONE;
1455 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1456 bundle *content_info)
1458 const char *class_id = NULL;
1460 bundle_raw *raw = NULL;
1463 if (!_is_widget_feature_enabled()) {
1464 _E("not supported"); /* LCOV_EXCL_LINE */
1465 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1468 if (context == NULL || content_info == NULL)
1469 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1470 __FUNCTION__, NULL);
1472 if (context->provider == NULL)
1473 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1474 __FUNCTION__, NULL);
1476 class_id = context->provider->classid;
1477 if (class_id == NULL)
1478 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1480 ret = __send_update_status(class_id, context->id,
1481 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1483 if (context->content)
1484 free(context->content);
1486 bundle_encode(content_info, &raw, &len);
1488 context->content = strdup((const char *)raw);
1490 context->content = NULL;
1493 /* LCOV_EXCL_START */
1494 _E("failed to send content info: %s of %s (%d)", context->id,
1496 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1498 /* LCOV_EXCL_STOP */
1501 return WIDGET_ERROR_NONE;
1504 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1507 if (!_is_widget_feature_enabled()) {
1508 _E("not supported"); /* LCOV_EXCL_LINE */
1509 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1512 if (!context || !title)
1513 return WIDGET_ERROR_INVALID_PARAMETER;
1516 elm_win_title_set(context->win, title);
1518 return WIDGET_ERROR_NONE;