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 _widget_class {
67 widget_instance_lifecycle_callback_s ops;
69 struct _widget_class *next;
70 struct _widget_class *prev;
73 struct app_event_handler {
74 app_event_type_e type;
79 struct app_event_info {
80 app_event_type_e type;
84 struct _widget_context {
86 struct _widget_class *provider;
92 widget_instance_lifecycle_callback_s ops;
95 typedef struct _widget_class widget_class_s;
96 typedef struct _widget_context widget_context_s;
98 #define WIDGET_APP_EVENT_MAX 5
99 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
101 static int caller_pid;
102 static widget_app_lifecycle_callback_s *app_ops;
103 static void *app_user_data;
105 static widget_class_h class_provider;
106 static GList *contexts;
107 static char *viewer_endpoint;
108 static int exit_called;
110 static void _widget_core_set_appcore_event_cb(void);
111 static void _widget_core_unset_appcore_event_cb(void);
113 static void __free_handler_cb(gpointer data)
119 static void __free_handler_list(void)
123 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
124 g_list_free_full(handler_list[i], __free_handler_cb);
125 handler_list[i] = NULL;
129 static inline bool _is_widget_feature_enabled(void)
131 static bool feature = false;
132 static bool retrieved = false;
135 if (retrieved == true)
138 ret = system_info_get_platform_bool(
139 "http://tizen.org/feature/shell.appwidget", &feature);
140 if (ret != SYSTEM_INFO_ERROR_NONE) {
141 _E("failed to get system info"); /* LCOV_EXCL_LINE */
142 return false; /* LCOV_EXCL_LINE */
150 static gint __comp_by_id(gconstpointer a, gconstpointer b)
152 widget_context_s *wc = (widget_context_s *)a;
154 return strcmp(wc->id, (const char *)b);
157 static widget_context_s *__find_context_by_id(const char *id)
164 ret = g_list_find_custom(contexts, id, __comp_by_id);
171 static gint __comp_by_win(gconstpointer a, gconstpointer b)
173 int win = GPOINTER_TO_INT(b);
174 widget_context_s *wc = (widget_context_s *)a;
176 return (wc && wc->win_id == win) ? 0 : -1;
179 static widget_context_s *__find_context_by_win(int win)
181 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
189 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
192 bundle *b = bundle_create();
196 _E("out of memory"); /* LCOV_EXCL_LINE */
197 return -1; /* LCOV_EXCL_LINE */
200 bundle_add_str(b, WIDGET_K_ID, class_id);
201 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
202 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
204 _D("send lifecycle %s(%d)", instance_id, status);
205 ret = aul_app_com_send("widget.status", b);
207 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
214 static int __send_update_status(const char *class_id, const char *instance_id,
215 int status, bundle *extra)
219 bundle_raw *raw = NULL;
224 _E("out of memory"); /* LCOV_EXCL_LINE */
225 return -1; /* LCOV_EXCL_LINE */
228 bundle_add_str(b, WIDGET_K_ID, class_id);
229 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
230 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
233 bundle_encode(extra, &raw, &len);
234 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
235 aul_widget_instance_add(class_id, instance_id);
238 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
239 aul_app_com_send(viewer_endpoint, b);
242 case WIDGET_INSTANCE_EVENT_CREATE:
243 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
245 case WIDGET_INSTANCE_EVENT_DESTROY:
246 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
248 case WIDGET_INSTANCE_EVENT_PAUSE:
249 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
251 case WIDGET_INSTANCE_EVENT_RESUME:
252 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
257 __send_lifecycle_event(class_id, instance_id, lifecycle);
266 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
268 widget_context_s *wc = __find_context_by_id(id);
272 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
273 return -1; /* LCOV_EXCL_LINE */
276 if (wc->state == WC_RUNNING) {
277 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
278 return 0; /* LCOV_EXCL_LINE */
281 if (wc->state == WC_TERMINATED) {
282 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
283 return 0; /* LCOV_EXCL_LINE */
286 if (handle->ops.resume)
287 handle->ops.resume(wc, handle->user_data);
289 wc->state = WC_RUNNING;
290 _D("%s is resumed", id);
292 ret = __send_update_status(handle->classid, wc->id,
293 WIDGET_INSTANCE_EVENT_RESUME, NULL);
301 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
303 widget_context_s *wc = __find_context_by_id(id);
307 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
308 return -1; /* LCOV_EXCL_LINE */
311 if (wc->state == WC_PAUSED) {
312 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
313 return 0; /* LCOV_EXCL_LINE */
316 if (wc->state == WC_TERMINATED) {
317 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
318 return 0; /* LCOV_EXCL_LINE */
321 if (handle->ops.pause)
322 handle->ops.pause(wc, handle->user_data);
324 wc->state = WC_PAUSED;
325 _D("%s is paused", id);
327 ret = __send_update_status(handle->classid, wc->id,
328 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
336 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
338 widget_context_s *wc = __find_context_by_id(id);
342 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
343 return -1; /* LCOV_EXCL_LINE */
346 if (handle->ops.resize)
347 handle->ops.resize(wc, w, h, handle->user_data);
349 _D("%s is resized to %dx%d", id, w, h);
350 ret = __send_update_status(handle->classid, wc->id,
351 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
356 /* LCOV_EXCL_START */
357 static int __instance_update_all(widget_class_h handle, int force, const char *content)
359 widget_context_s *wc;
362 GList *context = contexts;
365 b = bundle_decode((const bundle_raw *)content, strlen(content));
367 if (handle->ops.update) {
369 wc = (widget_context_s *)context->data;
370 handle->ops.update(wc, b, force, handle->user_data);
371 ret = __send_update_status(handle->classid, wc->id,
372 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
373 _D("updated:%s", wc->id);
374 context = context->next;
385 /* LCOV_EXCL_START */
386 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
388 widget_context_s *wc = __find_context_by_id(id);
392 _E("context not found: %s", id);
397 b = bundle_decode((const bundle_raw *)content, strlen(content));
399 if (handle->ops.update) {
400 handle->ops.update(wc, b, force, handle->user_data);
401 ret = __send_update_status(handle->classid, wc->id,
402 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
403 _D("updated:%s", id);
413 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
415 widget_context_s *wc = NULL;
417 bundle *content_info = NULL;
419 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
421 return WIDGET_ERROR_OUT_OF_MEMORY;
423 wc->state = WC_READY;
424 wc->id = g_strdup(id);
425 wc->provider = handle;
430 wc->content = strdup(content);
431 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
434 contexts = g_list_append(contexts, wc);
436 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
438 /* TODO send abort */
440 ret = __send_update_status(handle->classid, wc->id,
441 WIDGET_INSTANCE_EVENT_CREATE, NULL);
446 aul_widget_instance_add(handle->classid, id);
450 bundle_free(content_info);
455 static int __instance_destroy(widget_class_h handle, const char *id,
456 widget_app_destroy_type_e reason, int send_update)
458 widget_context_s *wc = __find_context_by_id(id);
460 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
461 bundle *content_info;
464 _E("could not find widget obj: %s", id); /* LCOV_EXCL_LINE */
465 return WIDGET_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
468 wc->state = WC_TERMINATED;
470 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
472 content_info = bundle_create();
474 handle->ops.destroy(wc, reason, content_info,
477 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
478 event = WIDGET_INSTANCE_EVENT_DESTROY;
480 ret = __send_update_status(handle->classid, id,
481 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
484 aul_widget_instance_del(handle->classid, id);
487 bundle_free(content_info);
489 ret = __send_update_status(handle->classid, id, event, NULL);
491 contexts = g_list_remove(contexts, wc);
501 if (contexts == NULL && !exit_called) /* all instance destroyed */
507 static widget_class_h __find_class_handler(const char *class_id,
508 widget_class_h handle)
510 if (!class_id || !handle)
513 widget_class_h head = handle;
516 if (head->classid && strcmp(head->classid, class_id) == 0)
525 /* LCOV_EXCL_START */
526 static void __resize_window(char *id, int w, int h)
528 widget_context_s *wc = __find_context_by_id(id);
531 _E("can not find instance: %s", id);
536 evas_object_resize(wc->win, w, h);
538 _E("unable to find window of %d", wc->id);
542 static void __control(bundle *b)
544 char *class_id = NULL;
546 char *operation = NULL;
547 char *content = NULL;
554 char *force_str = NULL;
555 widget_class_h handle = NULL;
557 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
558 /* for previous version compatibility, use appid for default class id */
559 if (class_id == NULL)
562 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
563 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
565 handle = __find_class_handler(class_id, class_provider);
567 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
572 _E("no operation provided");
576 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
578 if (force_str && strcmp(force_str, "true") == 0)
583 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
584 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
585 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
587 w = (int)g_ascii_strtoll(w_str, &remain, 10);
590 h = (int)g_ascii_strtoll(h_str, &remain, 10);
592 if (strcmp(operation, "create") == 0) {
593 __instance_create(handle, id, content, w, h);
594 } else if (strcmp(operation, "resize") == 0) {
595 __resize_window(id, w, h);
596 } else if (strcmp(operation, "update") == 0) {
598 __instance_update(handle, id, force, content);
600 __instance_update_all(handle, force, content);
602 } else if (strcmp(operation, "destroy") == 0) {
603 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
604 } else if (strcmp(operation, "resume") == 0) {
605 __instance_resume(handle, id, UPDATE_ALL);
606 } else if (strcmp(operation, "pause") == 0) {
607 __instance_pause(handle, id, UPDATE_ALL);
608 } else if (strcmp(operation, "terminate") == 0) {
609 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
614 LOGD("error on control");
618 static void __pause_all(int send_update)
620 GList *iter = g_list_first(contexts);
622 while (iter != NULL) {
623 widget_context_s *cxt = (widget_context_s *)iter->data;
625 switch (cxt->state) {
627 __instance_resume(cxt->provider, cxt->id, send_update);
628 __instance_pause(cxt->provider, cxt->id, send_update);
631 __instance_pause(cxt->provider, cxt->id, send_update);
634 iter = g_list_next(iter);
638 /* LCOV_EXCL_START */
639 static void __resume_all(int send_update)
641 GList *iter = g_list_first(contexts);
643 while (iter != NULL) {
644 widget_context_s *cxt = (widget_context_s *)iter->data;
646 switch (cxt->state) {
648 __instance_resume(cxt->provider, cxt->id, send_update);
651 __instance_resume(cxt->provider, cxt->id, send_update);
654 iter = g_list_next(iter);
659 static void __destroy_all(int reason, int send_update)
661 GList *iter = g_list_first(contexts);
663 __pause_all(send_update);
665 while (iter != NULL) {
666 widget_context_s *cxt = (widget_context_s *)iter->data;
668 switch (cxt->state) {
670 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
673 iter = g_list_next(iter);
678 static Eina_Bool __show_cb(void *data, int type, void *event)
680 Ecore_Wl_Event_Window_Show *ev = event;
681 widget_context_s *cxt = __find_context_by_win(ev->win);
683 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
686 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
688 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
690 return ECORE_CALLBACK_RENEW;
693 static Eina_Bool __hide_cb(void *data, int type, void *event)
695 Ecore_Wl_Event_Window_Hide *ev = event;
696 widget_context_s *cxt = __find_context_by_win(ev->win);
699 LOGD("hide %d", (unsigned int)ev->win);
702 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
704 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
706 return ECORE_CALLBACK_RENEW;
709 static Eina_Bool __visibility_cb(void *data, int type, void *event)
711 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
712 widget_context_s *cxt = __find_context_by_win(ev->win);
714 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
717 LOGE("unknown window error: %d", ev->win);
718 return ECORE_CALLBACK_RENEW;
721 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
722 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
723 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
724 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
726 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
729 return ECORE_CALLBACK_RENEW;
732 /* LCOV_EXCL_START */
733 static Eina_Bool __lower_cb(void *data, int type, void *event)
736 return ECORE_CALLBACK_RENEW;
740 static Eina_Bool __configure_cb(void *data, int type, void *event)
742 Ecore_Wl_Event_Window_Configure *ev = event;
743 widget_context_s *cxt = __find_context_by_win(ev->win);
745 LOGD("configure: %d %d", ev->w, ev->h);
748 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
749 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
752 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
753 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
754 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
756 return ECORE_CALLBACK_RENEW;
759 static void __add_climsg()
761 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
762 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
763 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
764 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
765 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
768 static int __aul_handler(aul_type type, bundle *b, void *data)
776 bundle_get_str(b, WIDGET_K_CALLER, &caller);
778 caller_pid = g_ascii_strtoll(caller, &remain,
781 /* using caller appid and query pid using caller appid? */
789 __resume_all(UPDATE_ALL);
801 static char *__get_domain_name(char *appid)
806 _E("appid is NULL"); /* LCOV_EXCL_LINE */
807 return NULL; /* LCOV_EXCL_LINE */
810 name_token = strrchr(appid, '.');
812 if (name_token == NULL) {
813 _E("appid is invalid"); /* LCOV_EXCL_LINE */
814 return appid; /* LCOV_EXCL_LINE */
822 /* LCOV_EXCL_START */
823 static void __on_poweroff(keynode_t *key, void *data)
827 val = vconf_keynode_get_int(key);
829 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
830 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
831 _I("power off changed: %d", val);
834 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
835 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
843 extern int _set_i18n(const char *name);
845 static int __before_loop(int argc, char **argv)
849 char *wayland_display = NULL;
850 char *xdg_runtime_dir = NULL;
853 #if !(GLIB_CHECK_VERSION(2, 36, 0))
857 kb = bundle_import_from_argv(argc, argv);
859 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
860 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
861 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
862 if (viewer_endpoint) {
863 _E("viewer endpoint :%s", viewer_endpoint);
864 viewer_endpoint = strdup(viewer_endpoint);
866 _E("endpoint is missing");
870 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
872 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
875 setenv("WAYLAND_DISPLAY", wayland_display, 1);
877 _D("wayland_display:%s", wayland_display);
882 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
885 elm_init(argc, argv);
887 r = aul_launch_init(__aul_handler, NULL);
889 /* LCOV_EXCL_START */
890 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
892 "Fail to call the aul_launch_init");
896 r = aul_launch_argv_handler(argc, argv);
898 /* LCOV_EXCL_START */
899 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
901 "Fail to call the aul_launch_argv_handler");
905 r = app_get_id(&appid);
906 if (r != APP_ERROR_NONE)
909 name = __get_domain_name(appid);
912 /* LCOV_EXCL_START */
913 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
915 "Fail to call __get_domain_name");
922 /* LCOV_EXCL_START */
923 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
925 "Fail to call _set_i18n");
931 _widget_core_set_appcore_event_cb();
933 class_provider = app_ops->create(app_user_data);
934 if (class_provider == NULL) {
935 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
936 __FUNCTION__, "widget_class is NULL");
939 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
941 return WIDGET_ERROR_NONE;
944 static void __after_loop()
947 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
949 __pause_all(UPDATE_LOCAL);
950 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
952 if (app_ops->terminate)
953 app_ops->terminate(app_user_data);
956 free(viewer_endpoint);
958 _widget_core_unset_appcore_event_cb();
959 __free_handler_list();
963 static void __on_low_memory(keynode_t *key, void *data)
967 val = vconf_keynode_get_int(key);
968 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
969 app_event_handler_h handler;
970 struct app_event_info event;
972 _I("widget_app_low_memory");
974 event.type = APP_EVENT_LOW_MEMORY;
975 event.value = (void *)&val;
977 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
980 handler = (app_event_handler_h) iter->data;
981 handler->cb(&event, handler->data);
982 iter = g_list_next(iter);
987 static void __on_low_battery(keynode_t *key, void *data)
991 val = vconf_keynode_get_int(key);
992 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
993 app_event_handler_h handler;
994 struct app_event_info event;
996 _I("widget_app_low_battery");
998 event.type = APP_EVENT_LOW_BATTERY;
999 event.value = (void *)&val;
1001 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1004 handler = (app_event_handler_h) iter->data;
1005 handler->cb(&event, handler->data);
1006 iter = g_list_next(iter);
1011 static void __on_lang_changed(keynode_t *key, void *data)
1016 val = vconf_keynode_get_str(key);
1018 app_event_handler_h handler;
1019 struct app_event_info event;
1021 _I("widget_app_lang_changed");
1023 event.type = APP_EVENT_LANGUAGE_CHANGED;
1024 event.value = (void *)val;
1026 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1029 handler = (app_event_handler_h) iter->data;
1030 handler->cb(&event, handler->data);
1031 iter = g_list_next(iter);
1035 static void __on_region_changed(keynode_t *key, void *data)
1040 val = vconf_keynode_get_str(key);
1042 app_event_handler_h handler;
1043 struct app_event_info event;
1045 _I("widget_app_region_changed");
1047 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1048 event.value = (void *)val;
1050 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1053 handler = (app_event_handler_h) iter->data;
1054 handler->cb(&event, handler->data);
1055 iter = g_list_next(iter);
1059 static void __register_event(int event_type)
1061 switch (event_type) {
1062 case APP_EVENT_LOW_MEMORY:
1063 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1066 case APP_EVENT_LOW_BATTERY:
1067 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1070 case APP_EVENT_LANGUAGE_CHANGED:
1071 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1074 case APP_EVENT_REGION_FORMAT_CHANGED:
1075 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1080 static void __unregister_event(int event_type)
1082 switch (event_type) {
1083 case APP_EVENT_LOW_MEMORY:
1084 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1087 case APP_EVENT_LOW_BATTERY:
1088 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1091 case APP_EVENT_LANGUAGE_CHANGED:
1092 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1095 case APP_EVENT_REGION_FORMAT_CHANGED:
1096 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1101 static void _widget_core_set_appcore_event_cb(void)
1103 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1104 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1107 static void _widget_core_unset_appcore_event_cb(void)
1109 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1110 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1113 EXPORT_API int widget_app_main(int argc, char **argv,
1114 widget_app_lifecycle_callback_s *callback, void *user_data)
1118 if (!_is_widget_feature_enabled()) {
1119 _E("not supported"); /* LCOV_EXCL_LINE */
1120 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1123 if (argc <= 0 || argv == NULL || callback == NULL)
1124 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1125 __FUNCTION__, NULL);
1127 if (callback->create == NULL)
1128 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1130 "widget_app_create_cb() callback must be "
1134 app_user_data = user_data;
1135 r = __before_loop(argc, argv);
1139 ecore_main_loop_begin();
1140 aul_status_update(STATUS_DYING);
1143 return WIDGET_ERROR_NONE;
1146 EXPORT_API int widget_app_exit(void)
1148 if (!_is_widget_feature_enabled()) {
1149 _E("not supported"); /* LCOV_EXCL_LINE */
1150 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1154 return WIDGET_ERROR_NONE;
1158 ecore_main_loop_quit();
1160 return WIDGET_ERROR_NONE;
1163 /* LCOV_EXCL_START */
1164 static gboolean __finish_event_cb(gpointer user_data)
1166 if (user_data == NULL)
1169 widget_context_s *wc = (widget_context_s *)user_data;
1171 switch (wc->state) {
1173 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1175 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1177 __instance_destroy(wc->provider, wc->id,
1178 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1186 /* LCOV_EXCL_STOP */
1188 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1190 if (!_is_widget_feature_enabled()) {
1191 _E("not supported"); /* LCOV_EXCL_LINE */
1192 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1195 if (context == NULL)
1196 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1197 __FUNCTION__, NULL);
1199 g_idle_add(__finish_event_cb, context);
1200 return WIDGET_ERROR_NONE;
1203 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
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;