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");
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();
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);
210 static int __send_update_status(const char *class_id, const char *instance_id,
211 int status, bundle *extra)
215 bundle_raw *raw = NULL;
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);
271 if (wc->state == WC_RUNNING) {
272 _D("%s is already in running state", id);
276 if (wc->state == WC_TERMINATED) {
277 _D("%s is in terminated state", id);
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);
306 if (wc->state == WC_PAUSED) {
307 _D("%s is already in paused state", id);
311 if (wc->state == WC_TERMINATED) {
312 _D("%s is in terminated state", id);
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);
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 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
353 widget_context_s *wc = __find_context_by_id(id);
357 _E("context not found: %s", id);
362 b = bundle_decode((const bundle_raw *)content, strlen(content));
364 if (handle->ops.update) {
365 handle->ops.update(wc, b, force, handle->user_data);
366 ret = __send_update_status(handle->classid, wc->id,
367 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
368 _D("updated:%s", id);
377 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
379 widget_context_s *wc = NULL;
381 bundle *content_info = NULL;
383 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
385 return WIDGET_ERROR_OUT_OF_MEMORY;
387 wc->state = WC_READY;
388 wc->id = g_strdup(id);
389 wc->provider = handle;
394 wc->content = strdup(content);
395 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
398 contexts = g_list_append(contexts, wc);
400 handle->ops.create(wc, content_info, w, h, handle->user_data);
401 ret = __send_update_status(handle->classid, wc->id,
402 WIDGET_INSTANCE_EVENT_CREATE, NULL);
405 bundle_free(content_info);
410 static int __instance_destroy(widget_class_h handle, const char *id,
411 widget_app_destroy_type_e reason, int send_update)
413 widget_context_s *wc = __find_context_by_id(id);
415 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
416 bundle *content_info;
419 _E("could not find widget obj: %s", id);
420 return WIDGET_ERROR_INVALID_PARAMETER;
423 wc->state = WC_TERMINATED;
425 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
427 content_info = bundle_create();
429 handle->ops.destroy(wc, reason, content_info,
432 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
433 event = WIDGET_INSTANCE_EVENT_DESTROY;
435 ret = __send_update_status(handle->classid, id,
436 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
440 bundle_free(content_info);
442 ret = __send_update_status(handle->classid, id, event, NULL);
444 contexts = g_list_remove(contexts, wc);
454 if (contexts == NULL && !exit_called) /* all instance destroyed */
460 static widget_class_h __find_class_handler(const char *class_id,
461 widget_class_h handle)
463 if (!class_id || !handle)
466 widget_class_h head = handle;
469 if (head->classid && strcmp(head->classid, class_id) == 0)
478 static void __resize_window(char *id, int w, int h)
480 widget_context_s *wc = __find_context_by_id(id);
483 _E("can not find instance: %s", id);
488 evas_object_resize(wc->win, w, h);
490 _E("unable to find window of %d", wc->id);
493 static void __control(bundle *b)
495 char *class_id = NULL;
497 char *operation = NULL;
498 char *content = NULL;
505 char *force_str = NULL;
506 widget_class_h handle = NULL;
508 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
509 /* for previous version compatibility, use appid for default class id */
510 if (class_id == NULL)
513 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
514 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
516 handle = __find_class_handler(class_id, class_provider);
518 _E("no handle provided: %s", class_id);
523 _E("no operation provided");
527 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
529 if (force_str && strcmp(force_str, "true") == 0)
534 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
535 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
536 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
538 w = (int)g_ascii_strtoll(w_str, &remain, 10);
541 h = (int)g_ascii_strtoll(h_str, &remain, 10);
543 if (strcmp(operation, "create") == 0) {
544 __instance_create(handle, id, content, w, h);
545 } else if (strcmp(operation, "resize") == 0) {
546 __resize_window(id, w, h);
547 } else if (strcmp(operation, "update") == 0) {
548 __instance_update(handle, id, force, content);
549 } else if (strcmp(operation, "destroy") == 0) {
550 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
551 } else if (strcmp(operation, "resume") == 0) {
552 __instance_resume(handle, id, UPDATE_ALL);
553 } else if (strcmp(operation, "pause") == 0) {
554 __instance_pause(handle, id, UPDATE_ALL);
555 } else if (strcmp(operation, "terminate") == 0) {
556 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
561 LOGD("error on control");
565 static void __pause_all(int send_update)
567 GList *iter = g_list_first(contexts);
569 while (iter != NULL) {
570 widget_context_s *cxt = (widget_context_s *)iter->data;
572 switch (cxt->state) {
574 __instance_resume(cxt->provider, cxt->id, send_update);
575 __instance_pause(cxt->provider, cxt->id, send_update);
578 __instance_pause(cxt->provider, cxt->id, send_update);
581 iter = g_list_next(iter);
585 static void __resume_all(int send_update)
587 GList *iter = g_list_first(contexts);
589 while (iter != NULL) {
590 widget_context_s *cxt = (widget_context_s *)iter->data;
592 switch (cxt->state) {
594 __instance_resume(cxt->provider, cxt->id, send_update);
597 __instance_resume(cxt->provider, cxt->id, send_update);
600 iter = g_list_next(iter);
604 static void __destroy_all(int reason, int send_update)
606 GList *iter = g_list_first(contexts);
608 __pause_all(send_update);
610 while (iter != NULL) {
611 widget_context_s *cxt = (widget_context_s *)iter->data;
613 switch (cxt->state) {
615 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
618 iter = g_list_next(iter);
623 static Eina_Bool __show_cb(void *data, int type, void *event)
625 Ecore_Wl_Event_Window_Show *ev = event;
626 widget_context_s *cxt = __find_context_by_win(ev->win);
628 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
631 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
633 LOGE("unknown window error: %d", ev->win);
635 return ECORE_CALLBACK_RENEW;
638 static Eina_Bool __hide_cb(void *data, int type, void *event)
640 Ecore_Wl_Event_Window_Hide *ev = event;
641 widget_context_s *cxt = __find_context_by_win(ev->win);
644 LOGD("hide %d", (unsigned int)ev->win);
647 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
649 LOGE("unknown window error: %d", ev->win);
651 return ECORE_CALLBACK_RENEW;
654 static Eina_Bool __visibility_cb(void *data, int type, void *event)
656 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
657 widget_context_s *cxt = __find_context_by_win(ev->win);
659 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
662 LOGE("unknown window error: %d", ev->win);
663 return ECORE_CALLBACK_RENEW;
666 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
667 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
668 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
669 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
671 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
674 return ECORE_CALLBACK_RENEW;
677 static Eina_Bool __lower_cb(void *data, int type, void *event)
680 return ECORE_CALLBACK_RENEW;
683 static Eina_Bool __configure_cb(void *data, int type, void *event)
685 Ecore_Wl_Event_Window_Configure *ev = event;
686 widget_context_s *cxt = __find_context_by_win(ev->win);
688 LOGD("configure: %d %d", ev->w, ev->h);
691 LOGE("unknown window error: %d", ev->win);
692 return ECORE_CALLBACK_RENEW;
695 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
696 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
697 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
699 return ECORE_CALLBACK_RENEW;
702 static void __add_climsg()
704 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
705 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
706 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
707 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
708 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
711 static int __aul_handler(aul_type type, bundle *b, void *data)
719 bundle_get_str(b, WIDGET_K_CALLER, &caller);
721 caller_pid = g_ascii_strtoll(caller, &remain,
724 /* using caller appid and query pid using caller appid? */
732 __resume_all(UPDATE_ALL);
744 static char *__get_domain_name(char *appid)
753 name_token = strrchr(appid, '.');
755 if (name_token == NULL) {
756 _E("appid is invalid");
765 static void __on_poweroff(keynode_t *key, void *data)
769 val = vconf_keynode_get_int(key);
771 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
772 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
773 _I("power off changed: %d", val);
776 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
777 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
784 extern int _set_i18n(const char *name);
786 static int __before_loop(int argc, char **argv)
790 char *wayland_display = NULL;
791 char *xdg_runtime_dir = NULL;
794 #if !(GLIB_CHECK_VERSION(2, 36, 0))
798 kb = bundle_import_from_argv(argc, argv);
800 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
801 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
802 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
803 if (viewer_endpoint) {
804 _E("viewer endpoint :%s", viewer_endpoint);
805 viewer_endpoint = strdup(viewer_endpoint);
807 _E("endpoint is missing");
811 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
813 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
816 setenv("WAYLAND_DISPLAY", wayland_display, 1);
818 _D("wayland_display:%s", wayland_display);
823 _E("failed to get launch argv");
826 elm_init(argc, argv);
828 r = aul_launch_init(__aul_handler, NULL);
830 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
832 "Fail to call the aul_launch_init");
835 r = aul_launch_argv_handler(argc, argv);
837 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
839 "Fail to call the aul_launch_argv_handler");
842 r = app_get_id(&appid);
843 if (r != APP_ERROR_NONE)
846 name = __get_domain_name(appid);
849 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
851 "Fail to call __get_domain_name");
857 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
859 "Fail to call _set_i18n");
864 _widget_core_set_appcore_event_cb();
866 class_provider = app_ops->create(app_user_data);
867 if (class_provider == NULL) {
868 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
869 __FUNCTION__, "widget_class is NULL");
872 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
874 return WIDGET_ERROR_NONE;
877 static void __after_loop()
880 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
882 __pause_all(UPDATE_LOCAL);
883 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
885 if (app_ops->terminate)
886 app_ops->terminate(app_user_data);
889 free(viewer_endpoint);
891 _widget_core_unset_appcore_event_cb();
892 __free_handler_list();
896 static void __on_low_memory(keynode_t *key, void *data)
900 val = vconf_keynode_get_int(key);
901 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
902 app_event_handler_h handler;
903 struct app_event_info event;
905 _I("widget_app_low_memory");
907 event.type = APP_EVENT_LOW_MEMORY;
908 event.value = (void *)&val;
910 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
913 handler = (app_event_handler_h) iter->data;
914 handler->cb(&event, handler->data);
915 iter = g_list_next(iter);
920 static void __on_low_battery(keynode_t *key, void *data)
924 val = vconf_keynode_get_int(key);
925 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
926 app_event_handler_h handler;
927 struct app_event_info event;
929 _I("widget_app_low_battery");
931 event.type = APP_EVENT_LOW_BATTERY;
932 event.value = (void *)&val;
934 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
937 handler = (app_event_handler_h) iter->data;
938 handler->cb(&event, handler->data);
939 iter = g_list_next(iter);
944 static void __on_lang_changed(keynode_t *key, void *data)
949 val = vconf_keynode_get_str(key);
951 app_event_handler_h handler;
952 struct app_event_info event;
954 _I("widget_app_lang_changed");
956 event.type = APP_EVENT_LANGUAGE_CHANGED;
957 event.value = (void *)val;
959 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
962 handler = (app_event_handler_h) iter->data;
963 handler->cb(&event, handler->data);
964 iter = g_list_next(iter);
968 static void __on_region_changed(keynode_t *key, void *data)
973 val = vconf_keynode_get_str(key);
975 app_event_handler_h handler;
976 struct app_event_info event;
978 _I("widget_app_region_changed");
980 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
981 event.value = (void *)val;
983 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
986 handler = (app_event_handler_h) iter->data;
987 handler->cb(&event, handler->data);
988 iter = g_list_next(iter);
992 static void __register_event(int event_type)
994 switch (event_type) {
995 case APP_EVENT_LOW_MEMORY:
996 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
999 case APP_EVENT_LOW_BATTERY:
1000 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1003 case APP_EVENT_LANGUAGE_CHANGED:
1004 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1007 case APP_EVENT_REGION_FORMAT_CHANGED:
1008 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1013 static void __unregister_event(int event_type)
1015 switch (event_type) {
1016 case APP_EVENT_LOW_MEMORY:
1017 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1020 case APP_EVENT_LOW_BATTERY:
1021 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1024 case APP_EVENT_LANGUAGE_CHANGED:
1025 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1028 case APP_EVENT_REGION_FORMAT_CHANGED:
1029 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1034 static void _widget_core_set_appcore_event_cb(void)
1036 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1037 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1040 static void _widget_core_unset_appcore_event_cb(void)
1042 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1043 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1046 EXPORT_API int widget_app_main(int argc, char **argv,
1047 widget_app_lifecycle_callback_s *callback, void *user_data)
1051 if (!_is_widget_feature_enabled()) {
1052 _E("not supported");
1053 return WIDGET_ERROR_NOT_SUPPORTED;
1056 if (argc <= 0 || argv == NULL || callback == NULL)
1057 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1058 __FUNCTION__, NULL);
1060 if (callback->create == NULL)
1061 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1063 "widget_app_create_cb() callback must be "
1067 app_user_data = user_data;
1068 r = __before_loop(argc, argv);
1072 ecore_main_loop_begin();
1073 aul_status_update(STATUS_DYING);
1076 return WIDGET_ERROR_NONE;
1079 EXPORT_API int widget_app_exit(void)
1081 if (!_is_widget_feature_enabled()) {
1082 _E("not supported");
1083 return WIDGET_ERROR_NOT_SUPPORTED;
1087 return WIDGET_ERROR_NONE;
1091 ecore_main_loop_quit();
1093 return WIDGET_ERROR_NONE;
1096 static gboolean __finish_event_cb(gpointer user_data)
1098 if (user_data == NULL)
1101 widget_context_s *wc = (widget_context_s *)user_data;
1103 switch (wc->state) {
1105 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1107 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1109 __instance_destroy(wc->provider, wc->id,
1110 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1119 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1121 if (!_is_widget_feature_enabled()) {
1122 _E("not supported");
1123 return WIDGET_ERROR_NOT_SUPPORTED;
1126 if (context == NULL)
1127 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1128 __FUNCTION__, NULL);
1130 g_idle_add(__finish_event_cb, context);
1131 return WIDGET_ERROR_NONE;
1134 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1137 widget_context_s *wc;
1139 if (!_is_widget_feature_enabled()) {
1140 _E("not supported");
1141 return WIDGET_ERROR_NOT_SUPPORTED;
1145 return WIDGET_ERROR_INVALID_PARAMETER;
1147 list = g_list_first(contexts);
1150 wc = (widget_context_s *)list->data;
1158 return WIDGET_ERROR_NONE;
1161 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1162 app_event_type_e event_type, app_event_cb callback,
1168 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1170 return WIDGET_ERROR_FAULT;
1173 return WIDGET_ERROR_NOT_SUPPORTED;
1175 app_event_handler_h handler;
1177 if (event_handler == NULL || callback == NULL)
1178 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1180 if (event_type < APP_EVENT_LOW_MEMORY
1181 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1182 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1184 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1185 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1187 GList *iter = g_list_first(handler_list[event_type]);
1190 handler = (app_event_handler_h) iter->data;
1192 if (handler->cb == callback)
1193 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1195 iter = g_list_next(iter);
1198 handler = calloc(1, sizeof(struct app_event_handler));
1200 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
1202 if (g_list_length(handler_list[event_type]) == 0)
1203 __register_event(event_type);
1205 handler->type = event_type;
1206 handler->cb = callback;
1207 handler->data = user_data;
1208 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1210 *event_handler = handler;
1212 return WIDGET_ERROR_NONE;
1215 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1221 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1223 return WIDGET_ERROR_FAULT;
1226 return WIDGET_ERROR_NOT_SUPPORTED;
1228 app_event_type_e type;
1230 if (event_handler == NULL)
1231 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1233 type = event_handler->type;
1234 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1235 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1237 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1238 free(event_handler);
1240 if (g_list_length(handler_list[type]) == 0)
1241 __unregister_event(type);
1243 return WIDGET_ERROR_NONE;
1246 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1248 if (!_is_widget_feature_enabled()) {
1249 _E("not supported");
1250 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1255 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1259 set_last_result(WIDGET_ERROR_NONE);
1263 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1266 widget_context_s *cxt = (widget_context_s *)context;
1267 Evas_Object *ret_win;
1268 Ecore_Wl_Window *wl_win;
1270 if (!_is_widget_feature_enabled()) {
1271 _E("not supported");
1272 return WIDGET_ERROR_NOT_SUPPORTED;
1275 if (context == NULL || win == NULL)
1276 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1277 __FUNCTION__, NULL);
1279 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1280 if (ret_win == NULL) {
1281 _E("failed to create window");
1282 return WIDGET_ERROR_FAULT;
1285 wl_win = elm_win_wl_window_get(ret_win);
1286 if (wl_win == NULL) {
1287 _E("failed to get wayland window");
1288 evas_object_del(ret_win);
1289 return WIDGET_ERROR_FAULT;
1292 ecore_wl_window_class_name_set(wl_win, cxt->id);
1296 cxt->win_id = ecore_wl_window_id_get(wl_win);
1298 _D("window created: %d", cxt->win_id);
1300 return WIDGET_ERROR_NONE;
1303 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1304 widget_instance_lifecycle_callback_s callback, void *user_data)
1308 if (!_is_widget_feature_enabled()) {
1309 _E("not supported");
1310 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1314 if (class_id == NULL) {
1315 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1319 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1321 _E("failed to calloc : %s", __FUNCTION__);
1322 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
1326 wc->classid = strdup(class_id);
1327 wc->user_data = user_data;
1332 set_last_result(WIDGET_ERROR_NONE);
1340 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1341 const char *class_id,
1342 widget_instance_lifecycle_callback_s callback, void *user_data)
1344 return _widget_class_create(widget_class, class_id, callback,
1348 EXPORT_API widget_class_h widget_app_class_create(
1349 widget_instance_lifecycle_callback_s callback, void *user_data)
1351 return _widget_class_create(class_provider, appid, callback, user_data);
1354 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1356 if (!_is_widget_feature_enabled()) {
1357 _E("not supported");
1358 return WIDGET_ERROR_NOT_SUPPORTED;
1361 if (context == NULL)
1362 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1363 __FUNCTION__, NULL);
1367 return WIDGET_ERROR_NONE;
1370 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1372 if (!_is_widget_feature_enabled()) {
1373 _E("not supported");
1374 return WIDGET_ERROR_NOT_SUPPORTED;
1377 if (context == NULL || tag == NULL)
1378 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1379 __FUNCTION__, NULL);
1381 *tag = context->tag;
1383 return WIDGET_ERROR_NONE;
1386 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1387 bundle *content_info)
1389 const char *class_id = NULL;
1391 bundle_raw *raw = NULL;
1394 if (!_is_widget_feature_enabled()) {
1395 _E("not supported");
1396 return WIDGET_ERROR_NOT_SUPPORTED;
1399 if (context == NULL || content_info == NULL)
1400 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1401 __FUNCTION__, NULL);
1403 if (context->provider == NULL)
1404 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1405 __FUNCTION__, NULL);
1407 class_id = context->provider->classid;
1409 if (class_id == NULL)
1410 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1412 ret = __send_update_status(class_id, context->id,
1413 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1415 if (context->content)
1416 free(context->content);
1418 bundle_encode(content_info, &raw, &len);
1420 context->content = strdup((const char *)raw);
1422 context->content = NULL;
1425 _E("failed to send content info: %s of %s (%d)", context->id,
1427 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1431 return WIDGET_ERROR_NONE;
1434 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1437 if (!_is_widget_feature_enabled()) {
1438 _E("not supported");
1439 return WIDGET_ERROR_NOT_SUPPORTED;
1442 if (!context || !title)
1443 return WIDGET_ERROR_INVALID_PARAMETER;
1446 elm_win_title_set(context->win, title);
1448 return WIDGET_ERROR_NONE;