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;
88 static void _widget_core_set_appcore_event_cb(void);
89 static void _widget_core_unset_appcore_event_cb(void);
91 static void __free_handler_cb(gpointer data)
97 static void __free_handler_list(void)
101 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
102 g_list_free_full(handler_list[i], __free_handler_cb);
103 handler_list[i] = NULL;
107 static inline bool _is_widget_feature_enabled(void)
109 static bool feature = false;
110 static bool retrieved = false;
113 if (retrieved == true)
116 ret = system_info_get_platform_bool(
117 "http://tizen.org/feature/shell.appwidget", &feature);
118 if (ret != SYSTEM_INFO_ERROR_NONE) {
119 _E("failed to get system info"); /* LCOV_EXCL_LINE */
120 return false; /* LCOV_EXCL_LINE */
128 static gint __comp_by_id(gconstpointer a, gconstpointer b)
130 widget_context_s *wc = (widget_context_s *)a;
132 return strcmp(wc->id, (const char *)b);
135 static widget_context_s *__find_context_by_id(const char *id)
138 GList *contexts = _widget_app_get_contexts();
143 ret = g_list_find_custom(contexts, id, __comp_by_id);
150 static gint __comp_by_win(gconstpointer a, gconstpointer b)
152 int win = GPOINTER_TO_INT(b);
153 widget_context_s *wc = (widget_context_s *)a;
155 return (wc && wc->win_id == win) ? 0 : -1;
158 static widget_context_s *__find_context_by_win(int win)
160 GList *contexts = _widget_app_get_contexts();
161 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
169 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
172 bundle *b = bundle_create();
176 _E("out of memory"); /* LCOV_EXCL_LINE */
177 return -1; /* LCOV_EXCL_LINE */
180 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
181 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
182 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
184 _D("send lifecycle %s(%d)", instance_id, status);
185 ret = aul_app_com_send("widget.status", b);
187 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
194 static int __send_update_status(const char *class_id, const char *instance_id,
195 int status, bundle *extra)
199 bundle_raw *raw = NULL;
201 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
205 _E("out of memory"); /* LCOV_EXCL_LINE */
206 return -1; /* LCOV_EXCL_LINE */
209 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
210 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
211 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
214 bundle_encode(extra, &raw, &len);
215 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
216 aul_widget_instance_add(class_id, instance_id);
219 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
220 aul_app_com_send(viewer_endpoint, b);
223 case WIDGET_INSTANCE_EVENT_CREATE:
224 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
226 case WIDGET_INSTANCE_EVENT_DESTROY:
227 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
229 case WIDGET_INSTANCE_EVENT_PAUSE:
230 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
232 case WIDGET_INSTANCE_EVENT_RESUME:
233 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
238 __send_lifecycle_event(class_id, instance_id, lifecycle);
247 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
249 widget_context_s *wc = __find_context_by_id(id);
253 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
254 return -1; /* LCOV_EXCL_LINE */
257 if (wc->state == WC_RUNNING) {
258 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
259 return 0; /* LCOV_EXCL_LINE */
262 if (wc->state == WC_TERMINATED) {
263 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
264 return 0; /* LCOV_EXCL_LINE */
267 if (handle->ops.resume)
268 handle->ops.resume(wc, handle->user_data);
270 wc->state = WC_RUNNING;
271 _D("%s is resumed", id);
273 ret = __send_update_status(handle->classid, wc->id,
274 WIDGET_INSTANCE_EVENT_RESUME, NULL);
282 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
284 widget_context_s *wc = __find_context_by_id(id);
288 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
289 return -1; /* LCOV_EXCL_LINE */
292 if (wc->state == WC_PAUSED) {
293 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
294 return 0; /* LCOV_EXCL_LINE */
297 if (wc->state == WC_TERMINATED) {
298 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
299 return 0; /* LCOV_EXCL_LINE */
302 if (handle->ops.pause)
303 handle->ops.pause(wc, handle->user_data);
305 wc->state = WC_PAUSED;
306 _D("%s is paused", id);
308 ret = __send_update_status(handle->classid, wc->id,
309 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
317 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
319 widget_context_s *wc = __find_context_by_id(id);
323 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
324 return -1; /* LCOV_EXCL_LINE */
327 if (handle->ops.resize)
328 handle->ops.resize(wc, w, h, handle->user_data);
330 _D("%s is resized to %dx%d", id, w, h);
331 ret = __send_update_status(handle->classid, wc->id,
332 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
337 /* LCOV_EXCL_START */
338 static int __instance_update_all(widget_class_h handle, int force, const char *content)
340 widget_context_s *wc;
343 GList *context = _widget_app_get_contexts();
346 b = bundle_decode((const bundle_raw *)content, strlen(content));
348 if (handle->ops.update) {
350 wc = (widget_context_s *)context->data;
351 handle->ops.update(wc, b, force, handle->user_data);
352 ret = __send_update_status(handle->classid, wc->id,
353 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
354 _D("updated:%s", wc->id);
355 context = context->next;
366 /* LCOV_EXCL_START */
367 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
369 widget_context_s *wc = __find_context_by_id(id);
373 _E("context not found: %s", id);
378 b = bundle_decode((const bundle_raw *)content, strlen(content));
380 if (handle->ops.update) {
381 handle->ops.update(wc, b, force, handle->user_data);
382 ret = __send_update_status(handle->classid, wc->id,
383 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
384 _D("updated:%s", id);
394 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
396 widget_context_s *wc = NULL;
398 bundle *content_info = NULL;
400 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
402 return WIDGET_ERROR_OUT_OF_MEMORY;
404 wc->state = WC_READY;
405 wc->id = g_strdup(id);
406 wc->provider = handle;
411 wc->content = strdup(content);
412 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
415 _widget_app_add_context(wc);
417 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
419 /* TODO send abort */
421 ret = __send_update_status(handle->classid, wc->id,
422 WIDGET_INSTANCE_EVENT_CREATE, NULL);
427 aul_widget_instance_add(handle->classid, id);
431 bundle_free(content_info);
436 static int __instance_destroy(widget_class_h handle, const char *id,
437 widget_app_destroy_type_e reason, int send_update)
439 widget_context_s *wc = __find_context_by_id(id);
441 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
442 bundle *content_info;
446 _E("could not find widget obj: %s", id); /* LCOV_EXCL_LINE */
447 return WIDGET_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
450 wc->state = WC_TERMINATED;
452 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
454 content_info = bundle_create();
456 handle->ops.destroy(wc, reason, content_info,
459 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
460 event = WIDGET_INSTANCE_EVENT_DESTROY;
462 ret = __send_update_status(handle->classid, id,
463 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
466 aul_widget_instance_del(handle->classid, id);
469 bundle_free(content_info);
471 ret = __send_update_status(handle->classid, id, event, NULL);
473 _widget_app_remove_context(wc);
483 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
489 static widget_class_h __find_class_handler(const char *class_id,
490 widget_class_h handle)
492 if (!class_id || !handle)
495 widget_class_h head = handle;
498 if (head->classid && strcmp(head->classid, class_id) == 0)
507 /* LCOV_EXCL_START */
508 static void __resize_window(char *id, int w, int h)
510 widget_context_s *wc = __find_context_by_id(id);
513 _E("can not find instance: %s", id);
518 evas_object_resize(wc->win, w, h);
520 _E("unable to find window of %d", wc->id);
524 static void __control(bundle *b)
526 char *class_id = NULL;
528 char *operation = NULL;
529 char *content = NULL;
536 char *force_str = NULL;
537 widget_class_h handle = NULL;
539 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
540 /* for previous version compatibility, use appid for default class id */
541 if (class_id == NULL)
544 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
545 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
547 handle = __find_class_handler(class_id, class_provider);
549 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
554 _E("no operation provided");
558 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
560 if (force_str && strcmp(force_str, "true") == 0)
565 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
566 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
567 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
569 w = (int)g_ascii_strtoll(w_str, &remain, 10);
572 h = (int)g_ascii_strtoll(h_str, &remain, 10);
574 if (strcmp(operation, "create") == 0) {
575 __instance_create(handle, id, content, w, h);
576 } else if (strcmp(operation, "resize") == 0) {
577 __resize_window(id, w, h);
578 } else if (strcmp(operation, "update") == 0) {
580 __instance_update(handle, id, force, content);
582 __instance_update_all(handle, force, content);
584 } else if (strcmp(operation, "destroy") == 0) {
585 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
586 } else if (strcmp(operation, "resume") == 0) {
587 __instance_resume(handle, id, UPDATE_ALL);
588 } else if (strcmp(operation, "pause") == 0) {
589 __instance_pause(handle, id, UPDATE_ALL);
590 } else if (strcmp(operation, "terminate") == 0) {
591 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
596 LOGD("error on control");
600 static void __pause_all(int send_update)
602 GList *contexts = _widget_app_get_contexts();
603 GList *iter = g_list_first(contexts);
605 while (iter != NULL) {
606 widget_context_s *cxt = (widget_context_s *)iter->data;
608 switch (cxt->state) {
610 __instance_resume(cxt->provider, cxt->id, send_update);
611 __instance_pause(cxt->provider, cxt->id, send_update);
614 __instance_pause(cxt->provider, cxt->id, send_update);
617 iter = g_list_next(iter);
621 /* LCOV_EXCL_START */
622 static void __resume_all(int send_update)
624 GList *contexts = _widget_app_get_contexts();
625 GList *iter = g_list_first(contexts);
627 while (iter != NULL) {
628 widget_context_s *cxt = (widget_context_s *)iter->data;
630 switch (cxt->state) {
632 __instance_resume(cxt->provider, cxt->id, send_update);
635 __instance_resume(cxt->provider, cxt->id, send_update);
638 iter = g_list_next(iter);
643 static void __destroy_all(int reason, int send_update)
645 GList *contexts = _widget_app_get_contexts();
646 GList *iter = g_list_first(contexts);
648 __pause_all(send_update);
650 while (iter != NULL) {
651 widget_context_s *cxt = (widget_context_s *)iter->data;
653 switch (cxt->state) {
655 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
658 iter = g_list_next(iter);
663 static Eina_Bool __show_cb(void *data, int type, void *event)
665 Ecore_Wl_Event_Window_Show *ev = event;
666 widget_context_s *cxt = __find_context_by_win(ev->win);
668 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
671 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
673 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
675 return ECORE_CALLBACK_RENEW;
678 static Eina_Bool __hide_cb(void *data, int type, void *event)
680 Ecore_Wl_Event_Window_Hide *ev = event;
681 widget_context_s *cxt = __find_context_by_win(ev->win);
684 LOGD("hide %d", (unsigned int)ev->win);
687 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
689 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
691 return ECORE_CALLBACK_RENEW;
694 static Eina_Bool __visibility_cb(void *data, int type, void *event)
696 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
697 widget_context_s *cxt = __find_context_by_win(ev->win);
699 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
702 LOGE("unknown window error: %d", ev->win);
703 return ECORE_CALLBACK_RENEW;
706 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
707 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
708 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
709 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
711 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
714 return ECORE_CALLBACK_RENEW;
717 /* LCOV_EXCL_START */
718 static Eina_Bool __lower_cb(void *data, int type, void *event)
721 return ECORE_CALLBACK_RENEW;
725 static Eina_Bool __configure_cb(void *data, int type, void *event)
727 Ecore_Wl_Event_Window_Configure *ev = event;
728 widget_context_s *cxt = __find_context_by_win(ev->win);
730 LOGD("configure: %d %d", ev->w, ev->h);
733 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
734 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
737 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
738 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
739 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
741 return ECORE_CALLBACK_RENEW;
744 static void __add_climsg()
746 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
747 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
748 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
749 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
750 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
753 static int __aul_handler(aul_type type, bundle *b, void *data)
761 bundle_get_str(b, WIDGET_K_CALLER, &caller);
763 caller_pid = g_ascii_strtoll(caller, &remain,
766 /* using caller appid and query pid using caller appid? */
774 __resume_all(UPDATE_ALL);
786 static char *__get_domain_name(char *appid)
791 _E("appid is NULL"); /* LCOV_EXCL_LINE */
792 return NULL; /* LCOV_EXCL_LINE */
795 name_token = strrchr(appid, '.');
797 if (name_token == NULL) {
798 _E("appid is invalid"); /* LCOV_EXCL_LINE */
799 return appid; /* LCOV_EXCL_LINE */
807 /* LCOV_EXCL_START */
808 static void __on_poweroff(keynode_t *key, void *data)
812 val = vconf_keynode_get_int(key);
814 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
815 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
816 _I("power off changed: %d", val);
819 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
820 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
828 extern int _set_i18n(const char *name);
830 static int __before_loop(int argc, char **argv)
834 char *wayland_display = NULL;
835 char *xdg_runtime_dir = NULL;
837 char *viewer_endpoint;
839 #if !(GLIB_CHECK_VERSION(2, 36, 0))
843 kb = bundle_import_from_argv(argc, argv);
845 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
846 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
847 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
848 if (viewer_endpoint) {
849 _E("viewer endpoint :%s", viewer_endpoint);
850 _widget_app_set_viewer_endpoint(viewer_endpoint);
852 _E("endpoint is missing");
856 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
858 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
861 setenv("WAYLAND_DISPLAY", wayland_display, 1);
863 _D("wayland_display:%s", wayland_display);
868 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
871 elm_init(argc, argv);
873 r = aul_launch_init(__aul_handler, NULL);
875 /* LCOV_EXCL_START */
876 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
878 "Fail to call the aul_launch_init");
882 r = aul_launch_argv_handler(argc, argv);
884 /* LCOV_EXCL_START */
885 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
887 "Fail to call the aul_launch_argv_handler");
891 r = app_get_id(&appid);
892 if (r != APP_ERROR_NONE)
895 name = __get_domain_name(appid);
898 /* LCOV_EXCL_START */
899 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
901 "Fail to call __get_domain_name");
908 /* LCOV_EXCL_START */
909 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
911 "Fail to call _set_i18n");
917 _widget_core_set_appcore_event_cb();
919 class_provider = app_ops->create(app_user_data);
920 if (class_provider == NULL) {
921 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
922 __FUNCTION__, "widget_class is NULL");
925 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
927 return WIDGET_ERROR_NONE;
930 static void __after_loop()
933 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
935 __pause_all(UPDATE_LOCAL);
936 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
938 if (app_ops->terminate)
939 app_ops->terminate(app_user_data);
941 _widget_app_free_viewer_endpoint();
942 _widget_core_unset_appcore_event_cb();
943 __free_handler_list();
947 static void __on_low_memory(keynode_t *key, void *data)
951 val = vconf_keynode_get_int(key);
952 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
953 app_event_handler_h handler;
954 struct app_event_info event;
956 _I("widget_app_low_memory");
958 event.type = APP_EVENT_LOW_MEMORY;
959 event.value = (void *)&val;
961 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
964 handler = (app_event_handler_h) iter->data;
965 handler->cb(&event, handler->data);
966 iter = g_list_next(iter);
971 static void __on_low_battery(keynode_t *key, void *data)
975 val = vconf_keynode_get_int(key);
976 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
977 app_event_handler_h handler;
978 struct app_event_info event;
980 _I("widget_app_low_battery");
982 event.type = APP_EVENT_LOW_BATTERY;
983 event.value = (void *)&val;
985 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
988 handler = (app_event_handler_h) iter->data;
989 handler->cb(&event, handler->data);
990 iter = g_list_next(iter);
995 static void __on_lang_changed(keynode_t *key, void *data)
1000 val = vconf_keynode_get_str(key);
1002 app_event_handler_h handler;
1003 struct app_event_info event;
1005 _I("widget_app_lang_changed");
1007 event.type = APP_EVENT_LANGUAGE_CHANGED;
1008 event.value = (void *)val;
1010 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1013 handler = (app_event_handler_h) iter->data;
1014 handler->cb(&event, handler->data);
1015 iter = g_list_next(iter);
1019 static void __on_region_changed(keynode_t *key, void *data)
1024 val = vconf_keynode_get_str(key);
1026 app_event_handler_h handler;
1027 struct app_event_info event;
1029 _I("widget_app_region_changed");
1031 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1032 event.value = (void *)val;
1034 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1037 handler = (app_event_handler_h) iter->data;
1038 handler->cb(&event, handler->data);
1039 iter = g_list_next(iter);
1043 static void __register_event(int event_type)
1045 switch (event_type) {
1046 case APP_EVENT_LOW_MEMORY:
1047 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1050 case APP_EVENT_LOW_BATTERY:
1051 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1054 case APP_EVENT_LANGUAGE_CHANGED:
1055 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1058 case APP_EVENT_REGION_FORMAT_CHANGED:
1059 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1064 static void __unregister_event(int event_type)
1066 switch (event_type) {
1067 case APP_EVENT_LOW_MEMORY:
1068 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1071 case APP_EVENT_LOW_BATTERY:
1072 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1075 case APP_EVENT_LANGUAGE_CHANGED:
1076 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1079 case APP_EVENT_REGION_FORMAT_CHANGED:
1080 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1085 static void _widget_core_set_appcore_event_cb(void)
1087 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1088 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1091 static void _widget_core_unset_appcore_event_cb(void)
1093 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1094 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1097 EXPORT_API int widget_app_main(int argc, char **argv,
1098 widget_app_lifecycle_callback_s *callback, void *user_data)
1102 if (!_is_widget_feature_enabled()) {
1103 _E("not supported"); /* LCOV_EXCL_LINE */
1104 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1107 if (argc <= 0 || argv == NULL || callback == NULL)
1108 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1109 __FUNCTION__, NULL);
1111 if (callback->create == NULL)
1112 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1114 "widget_app_create_cb() callback must be "
1118 app_user_data = user_data;
1119 r = __before_loop(argc, argv);
1123 ecore_main_loop_begin();
1124 aul_status_update(STATUS_DYING);
1127 return WIDGET_ERROR_NONE;
1130 EXPORT_API int widget_app_exit(void)
1132 if (!_is_widget_feature_enabled()) {
1133 _E("not supported"); /* LCOV_EXCL_LINE */
1134 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1138 return WIDGET_ERROR_NONE;
1142 ecore_main_loop_quit();
1144 return WIDGET_ERROR_NONE;
1147 /* LCOV_EXCL_START */
1148 static gboolean __finish_event_cb(gpointer user_data)
1150 if (user_data == NULL)
1153 widget_context_s *wc = (widget_context_s *)user_data;
1155 switch (wc->state) {
1157 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1159 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1161 __instance_destroy(wc->provider, wc->id,
1162 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1170 /* LCOV_EXCL_STOP */
1172 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1174 if (!_is_widget_feature_enabled()) {
1175 _E("not supported"); /* LCOV_EXCL_LINE */
1176 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1179 if (context == NULL)
1180 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1181 __FUNCTION__, NULL);
1183 g_idle_add(__finish_event_cb, context);
1184 return WIDGET_ERROR_NONE;
1187 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1189 GList *contexts = _widget_app_get_contexts();
1191 widget_context_s *wc;
1193 if (!_is_widget_feature_enabled()) {
1194 _E("not supported"); /* LCOV_EXCL_LINE */
1195 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1199 return WIDGET_ERROR_INVALID_PARAMETER;
1201 list = g_list_first(contexts);
1204 wc = (widget_context_s *)list->data;
1212 return WIDGET_ERROR_NONE;
1215 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1216 app_event_type_e event_type, app_event_cb callback,
1222 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1224 return WIDGET_ERROR_FAULT;
1227 return WIDGET_ERROR_NOT_SUPPORTED;
1229 app_event_handler_h handler;
1231 if (event_handler == NULL || callback == NULL)
1232 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1234 if (event_type < APP_EVENT_LOW_MEMORY
1235 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1236 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1238 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1239 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1241 GList *iter = g_list_first(handler_list[event_type]);
1244 handler = (app_event_handler_h) iter->data;
1246 if (handler->cb == callback)
1247 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1249 iter = g_list_next(iter);
1252 handler = calloc(1, sizeof(struct app_event_handler));
1254 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1256 if (g_list_length(handler_list[event_type]) == 0)
1257 __register_event(event_type);
1259 handler->type = event_type;
1260 handler->cb = callback;
1261 handler->data = user_data;
1262 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1264 *event_handler = handler;
1266 return WIDGET_ERROR_NONE;
1269 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1275 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1277 return WIDGET_ERROR_FAULT;
1280 return WIDGET_ERROR_NOT_SUPPORTED;
1282 app_event_type_e type;
1284 if (event_handler == NULL)
1285 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1287 type = event_handler->type;
1288 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1289 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1291 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1292 free(event_handler);
1294 if (g_list_length(handler_list[type]) == 0)
1295 __unregister_event(type);
1297 return WIDGET_ERROR_NONE;
1300 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1302 if (!_is_widget_feature_enabled()) {
1303 _E("not supported"); /* LCOV_EXCL_LINE */
1304 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1305 return NULL; /* LCOV_EXCL_LINE */
1309 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1313 set_last_result(WIDGET_ERROR_NONE);
1317 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1320 widget_context_s *cxt = (widget_context_s *)context;
1321 Evas_Object *ret_win;
1322 Ecore_Wl_Window *wl_win;
1324 if (!_is_widget_feature_enabled()) {
1325 _E("not supported"); /* LCOV_EXCL_LINE */
1326 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1329 if (context == NULL || win == NULL)
1330 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1331 __FUNCTION__, NULL);
1333 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1334 if (ret_win == NULL) {
1335 _E("failed to create window"); /* LCOV_EXCL_LINE */
1336 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1339 wl_win = elm_win_wl_window_get(ret_win);
1340 if (wl_win == NULL) {
1341 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1342 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1343 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1346 ecore_wl_window_class_name_set(wl_win, cxt->id);
1350 cxt->win_id = ecore_wl_window_id_get(wl_win);
1352 _D("window created: %d", cxt->win_id);
1354 return WIDGET_ERROR_NONE;
1357 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1358 widget_instance_lifecycle_callback_s callback, void *user_data)
1362 if (!_is_widget_feature_enabled()) {
1363 _E("not supported"); /* LCOV_EXCL_LINE */
1364 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1368 if (class_id == NULL) {
1369 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1373 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1375 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1376 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1377 return NULL; /* LCOV_EXCL_LINE */
1380 wc->classid = strdup(class_id);
1381 wc->user_data = user_data;
1386 set_last_result(WIDGET_ERROR_NONE);
1394 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1395 const char *class_id,
1396 widget_instance_lifecycle_callback_s callback, void *user_data)
1398 return _widget_class_create(widget_class, class_id, callback,
1402 EXPORT_API widget_class_h widget_app_class_create(
1403 widget_instance_lifecycle_callback_s callback, void *user_data)
1405 return _widget_class_create(class_provider, appid, callback, user_data);
1408 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1410 if (!_is_widget_feature_enabled()) {
1411 _E("not supported"); /* LCOV_EXCL_LINE */
1412 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1415 if (context == NULL)
1416 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1417 __FUNCTION__, NULL);
1421 return WIDGET_ERROR_NONE;
1424 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1426 if (!_is_widget_feature_enabled()) {
1427 _E("not supported"); /* LCOV_EXCL_LINE */
1428 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1431 if (context == NULL || tag == NULL)
1432 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1433 __FUNCTION__, NULL);
1435 *tag = context->tag;
1437 return WIDGET_ERROR_NONE;
1440 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1441 bundle *content_info)
1443 const char *class_id = NULL;
1445 bundle_raw *raw = NULL;
1448 if (!_is_widget_feature_enabled()) {
1449 _E("not supported"); /* LCOV_EXCL_LINE */
1450 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1453 if (context == NULL || content_info == NULL)
1454 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1455 __FUNCTION__, NULL);
1457 if (context->provider == NULL)
1458 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1459 __FUNCTION__, NULL);
1461 class_id = context->provider->classid;
1462 if (class_id == NULL)
1463 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1465 ret = __send_update_status(class_id, context->id,
1466 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1468 if (context->content)
1469 free(context->content);
1471 bundle_encode(content_info, &raw, &len);
1473 context->content = strdup((const char *)raw);
1475 context->content = NULL;
1478 /* LCOV_EXCL_START */
1479 _E("failed to send content info: %s of %s (%d)", context->id,
1481 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1483 /* LCOV_EXCL_STOP */
1486 return WIDGET_ERROR_NONE;
1489 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1492 if (!_is_widget_feature_enabled()) {
1493 _E("not supported"); /* LCOV_EXCL_LINE */
1494 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1497 if (!context || !title)
1498 return WIDGET_ERROR_INVALID_PARAMETER;
1501 elm_win_title_set(context->win, title);
1503 return WIDGET_ERROR_NONE;