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)
159 GList *ret = g_list_find_custom(contexts, id, __comp_by_id);
167 static gint __comp_by_win(gconstpointer a, gconstpointer b)
169 int win = GPOINTER_TO_INT(b);
170 widget_context_s *wc = (widget_context_s *)a;
172 return (wc && wc->win_id == win) ? 0 : -1;
175 static widget_context_s *__find_context_by_win(int win)
177 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
185 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
188 bundle *b = bundle_create();
192 _E("out of memory"); /* LCOV_EXCL_LINE */
193 return -1; /* LCOV_EXCL_LINE */
196 bundle_add_str(b, WIDGET_K_ID, class_id);
197 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
198 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
200 _D("send lifecycle %s(%d)", instance_id, status);
201 ret = aul_app_com_send("widget.status", b);
203 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
210 static int __send_update_status(const char *class_id, const char *instance_id,
211 int status, bundle *extra)
215 bundle_raw *raw = NULL;
220 _E("out of memory"); /* LCOV_EXCL_LINE */
221 return -1; /* LCOV_EXCL_LINE */
224 bundle_add_str(b, WIDGET_K_ID, class_id);
225 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
226 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
229 bundle_encode(extra, &raw, &len);
230 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
233 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
234 aul_app_com_send(viewer_endpoint, b);
237 case WIDGET_INSTANCE_EVENT_CREATE:
238 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
240 case WIDGET_INSTANCE_EVENT_DESTROY:
241 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
243 case WIDGET_INSTANCE_EVENT_PAUSE:
244 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
246 case WIDGET_INSTANCE_EVENT_RESUME:
247 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
252 __send_lifecycle_event(class_id, instance_id, lifecycle);
261 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
263 widget_context_s *wc = __find_context_by_id(id);
267 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
268 return -1; /* LCOV_EXCL_LINE */
271 if (wc->state == WC_RUNNING) {
272 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
273 return 0; /* LCOV_EXCL_LINE */
276 if (wc->state == WC_TERMINATED) {
277 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
278 return 0; /* LCOV_EXCL_LINE */
281 if (handle->ops.resume)
282 handle->ops.resume(wc, handle->user_data);
284 wc->state = WC_RUNNING;
285 _D("%s is resumed", id);
287 ret = __send_update_status(handle->classid, wc->id,
288 WIDGET_INSTANCE_EVENT_RESUME, NULL);
296 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
298 widget_context_s *wc = __find_context_by_id(id);
302 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
303 return -1; /* LCOV_EXCL_LINE */
306 if (wc->state == WC_PAUSED) {
307 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
308 return 0; /* LCOV_EXCL_LINE */
311 if (wc->state == WC_TERMINATED) {
312 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
313 return 0; /* LCOV_EXCL_LINE */
316 if (handle->ops.pause)
317 handle->ops.pause(wc, handle->user_data);
319 wc->state = WC_PAUSED;
320 _D("%s is paused", id);
322 ret = __send_update_status(handle->classid, wc->id,
323 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
331 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
333 widget_context_s *wc = __find_context_by_id(id);
337 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
338 return -1; /* LCOV_EXCL_LINE */
341 if (handle->ops.resize)
342 handle->ops.resize(wc, w, h, handle->user_data);
344 _D("%s is resized to %dx%d", id, w, h);
345 ret = __send_update_status(handle->classid, wc->id,
346 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
351 /* LCOV_EXCL_START */
352 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
354 widget_context_s *wc = __find_context_by_id(id);
358 _E("context not found: %s", id);
363 b = bundle_decode((const bundle_raw *)content, strlen(content));
365 if (handle->ops.update) {
366 handle->ops.update(wc, b, force, handle->user_data);
367 ret = __send_update_status(handle->classid, wc->id,
368 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
369 _D("updated:%s", id);
379 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
381 widget_context_s *wc = NULL;
383 bundle *content_info = NULL;
385 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
387 return WIDGET_ERROR_OUT_OF_MEMORY;
389 wc->state = WC_READY;
390 wc->id = g_strdup(id);
391 wc->provider = handle;
396 wc->content = strdup(content);
397 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
400 contexts = g_list_append(contexts, wc);
402 handle->ops.create(wc, content_info, w, h, handle->user_data);
403 ret = __send_update_status(handle->classid, wc->id,
404 WIDGET_INSTANCE_EVENT_CREATE, NULL);
407 bundle_free(content_info);
412 static int __instance_destroy(widget_class_h handle, const char *id,
413 widget_app_destroy_type_e reason, int send_update)
415 widget_context_s *wc = __find_context_by_id(id);
417 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
418 bundle *content_info;
421 _E("could not find widget obj: %s", id); /* LCOV_EXCL_LINE */
422 return WIDGET_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
425 wc->state = WC_TERMINATED;
427 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
429 content_info = bundle_create();
431 handle->ops.destroy(wc, reason, content_info,
434 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
435 event = WIDGET_INSTANCE_EVENT_DESTROY;
437 ret = __send_update_status(handle->classid, id,
438 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
442 bundle_free(content_info);
444 ret = __send_update_status(handle->classid, id, event, NULL);
446 contexts = g_list_remove(contexts, wc);
456 if (contexts == NULL && !exit_called) /* all instance destroyed */
462 static widget_class_h __find_class_handler(const char *class_id,
463 widget_class_h handle)
465 if (!class_id || !handle)
468 widget_class_h head = handle;
471 if (head->classid && strcmp(head->classid, class_id) == 0)
480 /* LCOV_EXCL_START */
481 static void __resize_window(char *id, int w, int h)
483 widget_context_s *wc = __find_context_by_id(id);
486 _E("can not find instance: %s", id);
491 evas_object_resize(wc->win, w, h);
493 _E("unable to find window of %d", wc->id);
497 static void __control(bundle *b)
499 char *class_id = NULL;
501 char *operation = NULL;
502 char *content = NULL;
509 char *force_str = NULL;
510 widget_class_h handle = NULL;
512 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
513 /* for previous version compatibility, use appid for default class id */
514 if (class_id == NULL)
517 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
518 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
520 handle = __find_class_handler(class_id, class_provider);
522 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
527 _E("no operation provided");
531 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
533 if (force_str && strcmp(force_str, "true") == 0)
538 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
539 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
540 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
542 w = (int)g_ascii_strtoll(w_str, &remain, 10);
545 h = (int)g_ascii_strtoll(h_str, &remain, 10);
547 if (strcmp(operation, "create") == 0) {
548 __instance_create(handle, id, content, w, h);
549 } else if (strcmp(operation, "resize") == 0) {
550 __resize_window(id, w, h);
551 } else if (strcmp(operation, "update") == 0) {
552 __instance_update(handle, id, force, content);
553 } else if (strcmp(operation, "destroy") == 0) {
554 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
555 } else if (strcmp(operation, "resume") == 0) {
556 __instance_resume(handle, id, UPDATE_ALL);
557 } else if (strcmp(operation, "pause") == 0) {
558 __instance_pause(handle, id, UPDATE_ALL);
559 } else if (strcmp(operation, "terminate") == 0) {
560 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
565 LOGD("error on control");
569 static void __pause_all(int send_update)
571 GList *iter = g_list_first(contexts);
573 while (iter != NULL) {
574 widget_context_s *cxt = (widget_context_s *)iter->data;
576 switch (cxt->state) {
578 __instance_resume(cxt->provider, cxt->id, send_update);
579 __instance_pause(cxt->provider, cxt->id, send_update);
582 __instance_pause(cxt->provider, cxt->id, send_update);
585 iter = g_list_next(iter);
589 /* LCOV_EXCL_START */
590 static void __resume_all(int send_update)
592 GList *iter = g_list_first(contexts);
594 while (iter != NULL) {
595 widget_context_s *cxt = (widget_context_s *)iter->data;
597 switch (cxt->state) {
599 __instance_resume(cxt->provider, cxt->id, send_update);
602 __instance_resume(cxt->provider, cxt->id, send_update);
605 iter = g_list_next(iter);
610 static void __destroy_all(int reason, int send_update)
612 GList *iter = g_list_first(contexts);
614 __pause_all(send_update);
616 while (iter != NULL) {
617 widget_context_s *cxt = (widget_context_s *)iter->data;
619 switch (cxt->state) {
621 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
624 iter = g_list_next(iter);
629 static Eina_Bool __show_cb(void *data, int type, void *event)
631 Ecore_Wl_Event_Window_Show *ev = event;
632 widget_context_s *cxt = __find_context_by_win(ev->win);
634 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
637 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
639 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
641 return ECORE_CALLBACK_RENEW;
644 static Eina_Bool __hide_cb(void *data, int type, void *event)
646 Ecore_Wl_Event_Window_Hide *ev = event;
647 widget_context_s *cxt = __find_context_by_win(ev->win);
650 LOGD("hide %d", (unsigned int)ev->win);
653 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
655 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
657 return ECORE_CALLBACK_RENEW;
660 static Eina_Bool __visibility_cb(void *data, int type, void *event)
662 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
663 widget_context_s *cxt = __find_context_by_win(ev->win);
665 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
668 LOGE("unknown window error: %d", ev->win);
669 return ECORE_CALLBACK_RENEW;
672 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
673 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
674 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
675 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
677 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
680 return ECORE_CALLBACK_RENEW;
683 /* LCOV_EXCL_START */
684 static Eina_Bool __lower_cb(void *data, int type, void *event)
687 return ECORE_CALLBACK_RENEW;
691 static Eina_Bool __configure_cb(void *data, int type, void *event)
693 Ecore_Wl_Event_Window_Configure *ev = event;
694 widget_context_s *cxt = __find_context_by_win(ev->win);
696 LOGD("configure: %d %d", ev->w, ev->h);
699 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
700 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
703 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
704 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
705 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
707 return ECORE_CALLBACK_RENEW;
710 static void __add_climsg()
712 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
713 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
714 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
715 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
716 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
719 static int __aul_handler(aul_type type, bundle *b, void *data)
727 bundle_get_str(b, WIDGET_K_CALLER, &caller);
729 caller_pid = g_ascii_strtoll(caller, &remain,
732 /* using caller appid and query pid using caller appid? */
740 __resume_all(UPDATE_ALL);
752 static char *__get_domain_name(char *appid)
757 _E("appid is NULL"); /* LCOV_EXCL_LINE */
758 return NULL; /* LCOV_EXCL_LINE */
761 name_token = strrchr(appid, '.');
763 if (name_token == NULL) {
764 _E("appid is invalid"); /* LCOV_EXCL_LINE */
765 return appid; /* LCOV_EXCL_LINE */
773 /* LCOV_EXCL_START */
774 static void __on_poweroff(keynode_t *key, void *data)
778 val = vconf_keynode_get_int(key);
780 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
781 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
782 _I("power off changed: %d", val);
785 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
786 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
794 extern int _set_i18n(const char *name);
796 static int __before_loop(int argc, char **argv)
800 char *wayland_display = NULL;
801 char *xdg_runtime_dir = NULL;
804 #if !(GLIB_CHECK_VERSION(2, 36, 0))
808 kb = bundle_import_from_argv(argc, argv);
810 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
811 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
812 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
813 if (viewer_endpoint) {
814 _E("viewer endpoint :%s", viewer_endpoint);
815 viewer_endpoint = strdup(viewer_endpoint);
817 _E("endpoint is missing");
821 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
823 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
826 setenv("WAYLAND_DISPLAY", wayland_display, 1);
828 _D("wayland_display:%s", wayland_display);
833 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
836 elm_init(argc, argv);
838 r = aul_launch_init(__aul_handler, NULL);
840 /* LCOV_EXCL_START */
841 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
843 "Fail to call the aul_launch_init");
847 r = aul_launch_argv_handler(argc, argv);
849 /* LCOV_EXCL_START */
850 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
852 "Fail to call the aul_launch_argv_handler");
856 r = app_get_id(&appid);
857 if (r != APP_ERROR_NONE)
860 name = __get_domain_name(appid);
863 /* LCOV_EXCL_START */
864 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
866 "Fail to call __get_domain_name");
873 /* LCOV_EXCL_START */
874 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
876 "Fail to call _set_i18n");
882 _widget_core_set_appcore_event_cb();
884 class_provider = app_ops->create(app_user_data);
885 if (class_provider == NULL) {
886 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
887 __FUNCTION__, "widget_class is NULL");
890 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
892 return WIDGET_ERROR_NONE;
895 static void __after_loop()
898 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
900 __pause_all(UPDATE_LOCAL);
901 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
903 if (app_ops->terminate)
904 app_ops->terminate(app_user_data);
907 free(viewer_endpoint);
909 _widget_core_unset_appcore_event_cb();
910 __free_handler_list();
914 static void __on_low_memory(keynode_t *key, void *data)
918 val = vconf_keynode_get_int(key);
919 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
920 app_event_handler_h handler;
921 struct app_event_info event;
923 _I("widget_app_low_memory");
925 event.type = APP_EVENT_LOW_MEMORY;
926 event.value = (void *)&val;
928 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
931 handler = (app_event_handler_h) iter->data;
932 handler->cb(&event, handler->data);
933 iter = g_list_next(iter);
938 static void __on_low_battery(keynode_t *key, void *data)
942 val = vconf_keynode_get_int(key);
943 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
944 app_event_handler_h handler;
945 struct app_event_info event;
947 _I("widget_app_low_battery");
949 event.type = APP_EVENT_LOW_BATTERY;
950 event.value = (void *)&val;
952 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
955 handler = (app_event_handler_h) iter->data;
956 handler->cb(&event, handler->data);
957 iter = g_list_next(iter);
962 static void __on_lang_changed(keynode_t *key, void *data)
967 val = vconf_keynode_get_str(key);
969 app_event_handler_h handler;
970 struct app_event_info event;
972 _I("widget_app_lang_changed");
974 event.type = APP_EVENT_LANGUAGE_CHANGED;
975 event.value = (void *)val;
977 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
980 handler = (app_event_handler_h) iter->data;
981 handler->cb(&event, handler->data);
982 iter = g_list_next(iter);
986 static void __on_region_changed(keynode_t *key, void *data)
991 val = vconf_keynode_get_str(key);
993 app_event_handler_h handler;
994 struct app_event_info event;
996 _I("widget_app_region_changed");
998 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
999 event.value = (void *)val;
1001 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1004 handler = (app_event_handler_h) iter->data;
1005 handler->cb(&event, handler->data);
1006 iter = g_list_next(iter);
1010 static void __register_event(int event_type)
1012 switch (event_type) {
1013 case APP_EVENT_LOW_MEMORY:
1014 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1017 case APP_EVENT_LOW_BATTERY:
1018 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1021 case APP_EVENT_LANGUAGE_CHANGED:
1022 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1025 case APP_EVENT_REGION_FORMAT_CHANGED:
1026 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1031 static void __unregister_event(int event_type)
1033 switch (event_type) {
1034 case APP_EVENT_LOW_MEMORY:
1035 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1038 case APP_EVENT_LOW_BATTERY:
1039 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1042 case APP_EVENT_LANGUAGE_CHANGED:
1043 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1046 case APP_EVENT_REGION_FORMAT_CHANGED:
1047 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1052 static void _widget_core_set_appcore_event_cb(void)
1054 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1055 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1058 static void _widget_core_unset_appcore_event_cb(void)
1060 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1061 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1064 EXPORT_API int widget_app_main(int argc, char **argv,
1065 widget_app_lifecycle_callback_s *callback, void *user_data)
1069 if (!_is_widget_feature_enabled()) {
1070 _E("not supported"); /* LCOV_EXCL_LINE */
1071 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1074 if (argc <= 0 || argv == NULL || callback == NULL)
1075 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1076 __FUNCTION__, NULL);
1078 if (callback->create == NULL)
1079 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1081 "widget_app_create_cb() callback must be "
1085 app_user_data = user_data;
1086 r = __before_loop(argc, argv);
1090 ecore_main_loop_begin();
1091 aul_status_update(STATUS_DYING);
1094 return WIDGET_ERROR_NONE;
1097 EXPORT_API int widget_app_exit(void)
1099 if (!_is_widget_feature_enabled()) {
1100 _E("not supported"); /* LCOV_EXCL_LINE */
1101 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1105 return WIDGET_ERROR_NONE;
1109 ecore_main_loop_quit();
1111 return WIDGET_ERROR_NONE;
1114 /* LCOV_EXCL_START */
1115 static gboolean __finish_event_cb(gpointer user_data)
1117 if (user_data == NULL)
1120 widget_context_s *wc = (widget_context_s *)user_data;
1122 switch (wc->state) {
1124 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1126 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1128 __instance_destroy(wc->provider, wc->id,
1129 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1137 /* LCOV_EXCL_STOP */
1139 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1141 if (!_is_widget_feature_enabled()) {
1142 _E("not supported"); /* LCOV_EXCL_LINE */
1143 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1146 if (context == NULL)
1147 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1148 __FUNCTION__, NULL);
1150 g_idle_add(__finish_event_cb, context);
1151 return WIDGET_ERROR_NONE;
1154 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1157 widget_context_s *wc;
1159 if (!_is_widget_feature_enabled()) {
1160 _E("not supported"); /* LCOV_EXCL_LINE */
1161 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1165 return WIDGET_ERROR_INVALID_PARAMETER;
1167 list = g_list_first(contexts);
1170 wc = (widget_context_s *)list->data;
1178 return WIDGET_ERROR_NONE;
1181 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1182 app_event_type_e event_type, app_event_cb callback,
1188 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1190 return WIDGET_ERROR_FAULT;
1193 return WIDGET_ERROR_NOT_SUPPORTED;
1195 app_event_handler_h handler;
1197 if (event_handler == NULL || callback == NULL)
1198 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1200 if (event_type < APP_EVENT_LOW_MEMORY
1201 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1202 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1204 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1205 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1207 GList *iter = g_list_first(handler_list[event_type]);
1210 handler = (app_event_handler_h) iter->data;
1212 if (handler->cb == callback)
1213 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1215 iter = g_list_next(iter);
1218 handler = calloc(1, sizeof(struct app_event_handler));
1220 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1222 if (g_list_length(handler_list[event_type]) == 0)
1223 __register_event(event_type);
1225 handler->type = event_type;
1226 handler->cb = callback;
1227 handler->data = user_data;
1228 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1230 *event_handler = handler;
1232 return WIDGET_ERROR_NONE;
1235 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1241 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1243 return WIDGET_ERROR_FAULT;
1246 return WIDGET_ERROR_NOT_SUPPORTED;
1248 app_event_type_e type;
1250 if (event_handler == NULL)
1251 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1253 type = event_handler->type;
1254 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1255 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1257 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1258 free(event_handler);
1260 if (g_list_length(handler_list[type]) == 0)
1261 __unregister_event(type);
1263 return WIDGET_ERROR_NONE;
1266 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1268 if (!_is_widget_feature_enabled()) {
1269 _E("not supported"); /* LCOV_EXCL_LINE */
1270 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1271 return NULL; /* LCOV_EXCL_LINE */
1275 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1279 set_last_result(WIDGET_ERROR_NONE);
1283 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1286 widget_context_s *cxt = (widget_context_s *)context;
1287 Evas_Object *ret_win;
1288 Ecore_Wl_Window *wl_win;
1290 if (!_is_widget_feature_enabled()) {
1291 _E("not supported"); /* LCOV_EXCL_LINE */
1292 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1295 if (context == NULL || win == NULL)
1296 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1297 __FUNCTION__, NULL);
1299 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1300 if (ret_win == NULL) {
1301 _E("failed to create window"); /* LCOV_EXCL_LINE */
1302 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1305 wl_win = elm_win_wl_window_get(ret_win);
1306 if (wl_win == NULL) {
1307 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1308 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1309 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1312 ecore_wl_window_class_name_set(wl_win, cxt->id);
1316 cxt->win_id = ecore_wl_window_id_get(wl_win);
1318 _D("window created: %d", cxt->win_id);
1320 return WIDGET_ERROR_NONE;
1323 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1324 widget_instance_lifecycle_callback_s callback, void *user_data)
1328 if (!_is_widget_feature_enabled()) {
1329 _E("not supported"); /* LCOV_EXCL_LINE */
1330 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1334 if (class_id == NULL) {
1335 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1339 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1341 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1342 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1343 return NULL; /* LCOV_EXCL_LINE */
1346 wc->classid = strdup(class_id);
1347 wc->user_data = user_data;
1352 set_last_result(WIDGET_ERROR_NONE);
1360 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1361 const char *class_id,
1362 widget_instance_lifecycle_callback_s callback, void *user_data)
1364 return _widget_class_create(widget_class, class_id, callback,
1368 EXPORT_API widget_class_h widget_app_class_create(
1369 widget_instance_lifecycle_callback_s callback, void *user_data)
1371 return _widget_class_create(class_provider, appid, callback, user_data);
1374 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1376 if (!_is_widget_feature_enabled()) {
1377 _E("not supported"); /* LCOV_EXCL_LINE */
1378 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1381 if (context == NULL)
1382 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1383 __FUNCTION__, NULL);
1387 return WIDGET_ERROR_NONE;
1390 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1392 if (!_is_widget_feature_enabled()) {
1393 _E("not supported"); /* LCOV_EXCL_LINE */
1394 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1397 if (context == NULL || tag == NULL)
1398 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1399 __FUNCTION__, NULL);
1401 *tag = context->tag;
1403 return WIDGET_ERROR_NONE;
1406 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1407 bundle *content_info)
1409 const char *class_id = NULL;
1411 bundle_raw *raw = NULL;
1414 if (!_is_widget_feature_enabled()) {
1415 _E("not supported"); /* LCOV_EXCL_LINE */
1416 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1419 if (context == NULL || content_info == NULL)
1420 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1421 __FUNCTION__, NULL);
1423 if (context->provider == NULL)
1424 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1425 __FUNCTION__, NULL);
1427 class_id = context->provider->classid;
1428 if (class_id == NULL)
1429 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1431 ret = __send_update_status(class_id, context->id,
1432 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1434 if (context->content)
1435 free(context->content);
1437 bundle_encode(content_info, &raw, &len);
1439 context->content = strdup((const char *)raw);
1441 context->content = NULL;
1444 /* LCOV_EXCL_START */
1445 _E("failed to send content info: %s of %s (%d)", context->id,
1447 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1449 /* LCOV_EXCL_STOP */
1452 return WIDGET_ERROR_NONE;
1455 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1458 if (!_is_widget_feature_enabled()) {
1459 _E("not supported"); /* LCOV_EXCL_LINE */
1460 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1463 if (!context || !title)
1464 return WIDGET_ERROR_INVALID_PARAMETER;
1467 elm_win_title_set(context->win, title);
1469 return WIDGET_ERROR_NONE;