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 {
60 struct _widget_class {
62 widget_instance_lifecycle_callback_s ops;
64 struct _widget_class *next;
65 struct _widget_class *prev;
68 struct app_event_handler {
69 app_event_type_e type;
74 struct app_event_info {
75 app_event_type_e type;
79 struct _widget_context {
81 struct _widget_class *provider;
87 widget_instance_lifecycle_callback_s ops;
90 typedef struct _widget_class widget_class_s;
91 typedef struct _widget_context widget_context_s;
93 #define WIDGET_APP_EVENT_MAX 5
94 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
96 static int caller_pid = 0;
97 static widget_app_lifecycle_callback_s *app_ops;
98 static void *app_user_data = NULL;
99 static char *appid = NULL;
100 static widget_class_h class_provider = NULL;
101 static GList *contexts = NULL;
102 static char *viewer_endpoint = NULL;
104 static void _widget_core_set_appcore_event_cb(void);
105 static void _widget_core_unset_appcore_event_cb(void);
107 static void __free_handler_cb(gpointer data)
113 static void __free_handler_list(void)
117 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
118 g_list_free_full(handler_list[i], __free_handler_cb);
119 handler_list[i] = NULL;
123 static inline bool _is_widget_feature_enabled(void)
125 static bool feature = false;
126 static bool retrieved = false;
129 if (retrieved == true)
132 ret = system_info_get_platform_bool(
133 "http://tizen.org/feature/shell.appwidget", &feature);
134 if (ret != SYSTEM_INFO_ERROR_NONE) {
135 _E("failed to get system info");
144 static gint __comp_by_id(gconstpointer a, gconstpointer b)
146 widget_context_s *wc = (widget_context_s *)a;
148 return strcmp(wc->id, (const char *)b);
151 static widget_context_s *__find_context_by_id(const char *id)
153 GList *ret = g_list_find_custom(contexts, id, __comp_by_id);
161 static gint __comp_by_win(gconstpointer a, gconstpointer b)
163 int win = GPOINTER_TO_INT(b);
164 widget_context_s *wc = (widget_context_s *)a;
166 return (wc && wc->win_id == win) ? 0 : -1;
169 static widget_context_s *__find_context_by_win(int win)
171 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
179 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
182 bundle *b = bundle_create();
190 bundle_add_str(b, WIDGET_K_ID, class_id);
191 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
192 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
194 _D("send lifecycle %s(%d)", instance_id, status);
195 ret = aul_app_com_send("widget.status", b);
197 _E("send lifecycle error:%d", ret);
204 static int __send_update_status(const char *class_id, const char *instance_id,
205 int status, bundle *extra, int internal_only)
213 bundle_add_str(b, WIDGET_K_ID, class_id);
214 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
215 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
217 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
218 aul_app_com_send(viewer_endpoint, b);
221 case WIDGET_INSTANCE_EVENT_CREATE:
222 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
224 case WIDGET_INSTANCE_EVENT_DESTROY:
225 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
227 case WIDGET_INSTANCE_EVENT_PAUSE:
228 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
230 case WIDGET_INSTANCE_EVENT_RESUME:
231 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
236 __send_lifecycle_event(class_id, instance_id, lifecycle);
244 static int __instance_resume(widget_class_h handle, const char *id, bundle *b)
246 widget_context_s *wc = __find_context_by_id(id);
250 _E("context not found: %s", id);
254 if (wc->state == WC_RUNNING) {
255 _D("%s is already in running state", id);
259 if (wc->state == WC_TERMINATED) {
260 _D("%s is in terminated state", id);
264 if (handle->ops.resume)
265 handle->ops.resume(wc, handle->user_data);
267 wc->state = WC_RUNNING;
268 _D("%s is resumed", id);
269 ret = __send_update_status(handle->classid, wc->id,
270 WIDGET_INSTANCE_EVENT_RESUME, NULL, 0);
275 static int __instance_pause(widget_class_h handle, const char *id, bundle *b)
277 widget_context_s *wc = __find_context_by_id(id);
281 _E("context not found: %s", id);
285 if (wc->state == WC_PAUSED) {
286 _D("%s is already in paused state", id);
290 if (wc->state == WC_TERMINATED) {
291 _D("%s is in terminated state", id);
295 if (handle->ops.pause)
296 handle->ops.pause(wc, handle->user_data);
298 wc->state = WC_PAUSED;
299 _D("%s is paused", id);
300 ret = __send_update_status(handle->classid, wc->id,
301 WIDGET_INSTANCE_EVENT_PAUSE, NULL, 0);
306 static int __instance_resize(widget_class_h handle, const char *id, int w, int h, bundle *b)
308 widget_context_s *wc = __find_context_by_id(id);
312 _E("context not found: %s", id);
316 if (handle->ops.resize)
317 handle->ops.resize(wc, w, h, handle->user_data);
319 _D("%s is resized to %dx%d", id, w, h);
320 ret = __send_update_status(handle->classid, wc->id,
321 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL, 0);
326 static int __instance_update(widget_class_h handle, const char *id, bundle *b)
328 widget_context_s *wc = __find_context_by_id(id);
331 char *force_str = NULL;
334 _E("context not found: %s", id);
338 if (handle->ops.update) {
340 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
342 if (force_str && strcmp(force_str, "true") == 0)
347 handle->ops.update(wc, b, force, handle->user_data);
348 ret = __send_update_status(handle->classid, wc->id,
349 WIDGET_INSTANCE_EVENT_UPDATE, b, 0);
350 _D("updated:%s", id);
356 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
358 widget_context_s *wc = NULL;
360 char *w_str = NULL, *h_str = NULL;
364 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
366 return WIDGET_ERROR_OUT_OF_MEMORY;
368 wc->state = WC_READY;
369 wc->id = g_strdup(id);
370 wc->provider = handle;
374 wc->content = bundle_dup(b);
375 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
376 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
379 w = (int)g_ascii_strtoll(w_str, &remain, 10);
382 h = (int)g_ascii_strtoll(h_str, &remain, 10);
384 contexts = g_list_append(contexts, wc);
386 handle->ops.create(wc, b, w, h, handle->user_data);
387 ret = __send_update_status(handle->classid, wc->id,
388 WIDGET_INSTANCE_EVENT_CREATE, b, 0);
393 static int __instance_destroy(widget_class_h handle, const char *id,
394 widget_destroy_type_e reason, bundle *b)
396 widget_context_s *wc = __find_context_by_id(id);
400 _E("could not find widget obj: %s", id);
401 return WIDGET_ERROR_INVALID_PARAMETER;
404 wc->state = WC_TERMINATED;
405 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b,
408 ret = __send_update_status(handle->classid, id,
409 WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
411 contexts = g_list_remove(contexts, wc);
421 static widget_class_h __find_class_handler(const char *class_id,
422 widget_class_h handle)
424 if (!class_id || !handle)
427 widget_class_h head = handle;
430 if (head->classid && strcmp(head->classid, class_id) == 0)
439 static void __resize_window(char *id, bundle *b)
441 widget_context_s *wc = __find_context_by_id(id);
449 _E("can not find instance: %s", id);
453 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
454 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
457 w = (int)g_ascii_strtoll(w_str, &remain, 10);
459 _E("unable to get width");
464 h = (int)g_ascii_strtoll(h_str, &remain, 10);
466 _E("unable to get height");
471 evas_object_resize(wc->win, w, h);
473 _E("unable to find window of %d", wc->id);
476 static void __control(bundle *b)
478 char *class_id = NULL;
480 char *operation = NULL;
483 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
485 widget_class_h handle = NULL;
486 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
487 /* for previous version compatibility, use appid for default class id */
488 if (class_id == NULL)
491 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
492 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
494 handle = __find_class_handler(class_id, class_provider);
496 _E("no handle provided: %s", class_id);
501 _E("no operation provided");
505 if (strcmp(operation, "create") == 0) {
506 __instance_create(handle, id, b);
507 } else if (strcmp(operation, "resize") == 0) {
508 __resize_window(id, b);
509 } else if (strcmp(operation, "update") == 0) {
510 __instance_update(handle, id, b);
511 } else if (strcmp(operation, "destroy") == 0) {
512 bundle_get_str(b, WIDGET_K_REASON, &reason);
514 destroy_type = (int)g_ascii_strtoll(reason, &remain,
517 __instance_destroy(handle, id, destroy_type, b);
518 } else if (strcmp(operation, "resume") == 0) {
519 __instance_resume(handle, id, b);
520 } else if (strcmp(operation, "pause") == 0) {
521 __instance_pause(handle, id, b);
526 LOGD("error on control");
530 static void __resume_cb(const char *id, void *data)
532 widget_context_s *cxt = __find_context_by_id(id);
535 _E("invalid context id:%s", id);
539 __instance_resume(cxt->provider, id, NULL);
542 static void __pause_cb(const char *id, void *data)
544 widget_context_s *cxt = __find_context_by_id(id);
547 _E("invalid context id:%s", id);
551 __instance_pause(cxt->provider, id, NULL);
554 static void __pause_all()
556 GList *iter = g_list_first(contexts);
558 while (iter != NULL) {
559 widget_context_s *cxt = (widget_context_s *)iter->data;
560 const char *id = cxt->id;
562 switch (cxt->state) {
564 __resume_cb(id, NULL);
565 __pause_cb(id, NULL);
568 __pause_cb(id, NULL);
571 iter = g_list_next(iter);
575 static void __resume_all()
577 GList *iter = g_list_first(contexts);
579 while (iter != NULL) {
580 widget_context_s *cxt = (widget_context_s *)iter->data;
581 const char *id = cxt->id;
583 switch (cxt->state) {
585 __resume_cb(id, NULL);
588 __resume_cb(id, NULL);
591 iter = g_list_next(iter);
595 static Eina_Bool __show_cb(void *data, int type, void *event)
597 Ecore_Wl_Event_Window_Show *ev = event;
598 widget_context_s *cxt = __find_context_by_win(ev->win);
600 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
603 __instance_resume(cxt->provider, cxt->id, NULL);
605 LOGE("unknown window error: %d", ev->win);
607 return ECORE_CALLBACK_RENEW;
610 static Eina_Bool __hide_cb(void *data, int type, void *event)
612 Ecore_Wl_Event_Window_Hide *ev = event;
613 widget_context_s *cxt = __find_context_by_win(ev->win);
616 LOGD("hide %d", (unsigned int)ev->win);
619 __instance_pause(cxt->provider, cxt->id, NULL);
621 LOGE("unknown window error: %d", ev->win);
623 return ECORE_CALLBACK_RENEW;
626 static Eina_Bool __visibility_cb(void *data, int type, void *event)
628 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
629 widget_context_s *cxt = __find_context_by_win(ev->win);
631 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
634 LOGE("unknown window error: %d", ev->win);
635 return ECORE_CALLBACK_RENEW;
638 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
639 __instance_resume(cxt->provider, cxt->id, NULL);
640 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
641 __instance_pause(cxt->provider, cxt->id, NULL);
643 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
646 return ECORE_CALLBACK_RENEW;
649 static Eina_Bool __lower_cb(void *data, int type, void *event)
652 return ECORE_CALLBACK_RENEW;
655 static Eina_Bool __configure_cb(void *data, int type, void *event)
657 Ecore_Wl_Event_Window_Configure *ev = event;
658 widget_context_s *cxt = __find_context_by_win(ev->win);
660 LOGD("configure: %d %d", ev->w, ev->h);
663 LOGE("unknown window error: %d", ev->win);
664 return ECORE_CALLBACK_RENEW;
667 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
668 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h, NULL);
669 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
671 return ECORE_CALLBACK_RENEW;
674 static void __add_climsg()
676 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
677 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
678 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
679 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
680 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
683 static int __aul_handler(aul_type type, bundle *b, void *data)
691 bundle_get_str(b, WIDGET_K_CALLER, &caller);
693 caller_pid = g_ascii_strtoll(caller, &remain,
696 /* using caller appid and query pid using caller appid? */
716 static char *__get_domain_name(char *appid)
725 name_token = strrchr(appid, '.');
727 if (name_token == NULL) {
728 _E("appid is invalid");
737 static void __on_poweroff(keynode_t *key, void *data)
741 val = vconf_keynode_get_int(key);
743 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
744 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
745 _I("power off changed: %d", val);
748 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
749 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
756 extern int _set_i18n(const char *name);
758 static int __before_loop(int argc, char **argv)
762 char *wayland_display = NULL;
763 char *xdg_runtime_dir = NULL;
766 #if !(GLIB_CHECK_VERSION(2, 36, 0))
770 kb = bundle_import_from_argv(argc, argv);
772 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
773 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
774 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
775 if (viewer_endpoint) {
776 _E("viewer endpoint :%s", viewer_endpoint);
777 viewer_endpoint = strdup(viewer_endpoint);
779 _E("endpoint is missing");
783 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
785 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
788 setenv("WAYLAND_DISPLAY", wayland_display, 1);
790 _D("wayland_display:%s", wayland_display);
795 _E("failed to get launch argv");
798 elm_init(argc, argv);
800 r = aul_launch_init(__aul_handler, NULL);
802 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
804 "Fail to call the aul_launch_init");
807 r = aul_launch_argv_handler(argc, argv);
809 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
811 "Fail to call the aul_launch_argv_handler");
814 r = app_get_id(&appid);
815 if (r != APP_ERROR_NONE)
818 name = __get_domain_name(appid);
821 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
823 "Fail to call __get_domain_name");
829 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
831 "Fail to call _set_i18n");
836 _widget_core_set_appcore_event_cb();
838 class_provider = app_ops->create(app_user_data);
839 if (class_provider == NULL) {
840 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
841 __FUNCTION__, "widget_class is NULL");
844 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
846 return WIDGET_ERROR_NONE;
849 static void __after_loop()
851 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
855 if (app_ops->terminate)
856 app_ops->terminate(app_user_data);
859 free(viewer_endpoint);
861 _widget_core_unset_appcore_event_cb();
862 __free_handler_list();
866 static void __on_low_memory(keynode_t *key, void *data)
870 val = vconf_keynode_get_int(key);
871 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
872 app_event_handler_h handler;
873 struct app_event_info event;
875 _I("widget_app_low_memory");
877 event.type = APP_EVENT_LOW_MEMORY;
878 event.value = (void *)&val;
880 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
883 handler = (app_event_handler_h) iter->data;
884 handler->cb(&event, handler->data);
885 iter = g_list_next(iter);
890 static void __on_low_battery(keynode_t *key, void *data)
894 val = vconf_keynode_get_int(key);
895 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
896 app_event_handler_h handler;
897 struct app_event_info event;
899 _I("widget_app_low_battery");
901 event.type = APP_EVENT_LOW_BATTERY;
902 event.value = (void *)&val;
904 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
907 handler = (app_event_handler_h) iter->data;
908 handler->cb(&event, handler->data);
909 iter = g_list_next(iter);
914 static void __on_lang_changed(keynode_t *key, void *data)
919 val = vconf_keynode_get_str(key);
921 app_event_handler_h handler;
922 struct app_event_info event;
924 _I("widget_app_lang_changed");
926 event.type = APP_EVENT_LANGUAGE_CHANGED;
927 event.value = (void *)val;
929 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
932 handler = (app_event_handler_h) iter->data;
933 handler->cb(&event, handler->data);
934 iter = g_list_next(iter);
938 static void __on_region_changed(keynode_t *key, void *data)
943 val = vconf_keynode_get_str(key);
945 app_event_handler_h handler;
946 struct app_event_info event;
948 _I("widget_app_region_changed");
950 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
951 event.value = (void *)val;
953 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
956 handler = (app_event_handler_h) iter->data;
957 handler->cb(&event, handler->data);
958 iter = g_list_next(iter);
962 static void __register_event(int event_type)
964 switch (event_type) {
965 case APP_EVENT_LOW_MEMORY:
966 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
969 case APP_EVENT_LOW_BATTERY:
970 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
973 case APP_EVENT_LANGUAGE_CHANGED:
974 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
977 case APP_EVENT_REGION_FORMAT_CHANGED:
978 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
983 static void __unregister_event(int event_type)
985 switch (event_type) {
986 case APP_EVENT_LOW_MEMORY:
987 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
990 case APP_EVENT_LOW_BATTERY:
991 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
994 case APP_EVENT_LANGUAGE_CHANGED:
995 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
998 case APP_EVENT_REGION_FORMAT_CHANGED:
999 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1004 static void _widget_core_set_appcore_event_cb(void)
1006 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1007 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1010 static void _widget_core_unset_appcore_event_cb(void)
1012 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1013 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1016 EXPORT_API int widget_app_main(int argc, char **argv,
1017 widget_app_lifecycle_callback_s *callback, void *user_data)
1021 if (!_is_widget_feature_enabled()) {
1022 _E("not supported");
1023 return WIDGET_ERROR_NOT_SUPPORTED;
1026 if (argc <= 0 || argv == NULL || callback == NULL)
1027 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1028 __FUNCTION__, NULL);
1030 if (callback->create == NULL)
1031 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1033 "widget_app_create_cb() callback must be "
1037 app_user_data = user_data;
1038 r = __before_loop(argc, argv);
1042 ecore_main_loop_begin();
1043 aul_status_update(STATUS_DYING);
1046 return WIDGET_ERROR_NONE;
1049 EXPORT_API int widget_app_exit(void)
1051 if (!_is_widget_feature_enabled()) {
1052 _E("not supported");
1053 return WIDGET_ERROR_NOT_SUPPORTED;
1056 ecore_main_loop_quit();
1058 return WIDGET_ERROR_NONE;
1061 static gboolean __finish_event_cb(gpointer user_data)
1063 if (user_data == NULL)
1066 widget_context_s *wc = (widget_context_s *)user_data;
1068 switch (wc->state) {
1085 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1087 if (!_is_widget_feature_enabled()) {
1088 _E("not supported");
1089 return WIDGET_ERROR_NOT_SUPPORTED;
1092 if (context == NULL)
1093 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1094 __FUNCTION__, NULL);
1096 g_idle_add(__finish_event_cb, context);
1097 return WIDGET_ERROR_NONE;
1100 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1103 widget_context_s *wc;
1105 if (!_is_widget_feature_enabled()) {
1106 _E("not supported");
1107 return WIDGET_ERROR_NOT_SUPPORTED;
1111 return WIDGET_ERROR_INVALID_PARAMETER;
1113 list = g_list_first(contexts);
1116 wc = (widget_context_s *)list->data;
1124 return WIDGET_ERROR_NONE;
1127 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1128 app_event_type_e event_type, app_event_cb callback,
1134 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1136 return WIDGET_ERROR_FAULT;
1139 return WIDGET_ERROR_NOT_SUPPORTED;
1141 app_event_handler_h handler;
1143 if (event_handler == NULL || callback == NULL)
1144 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1146 if (event_type < APP_EVENT_LOW_MEMORY
1147 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1148 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1150 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1151 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1153 GList *iter = g_list_first(handler_list[event_type]);
1156 handler = (app_event_handler_h) iter->data;
1158 if (handler->cb == callback)
1159 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1161 iter = g_list_next(iter);
1164 handler = calloc(1, sizeof(struct app_event_handler));
1166 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
1168 if (g_list_length(handler_list[event_type]) == 0)
1169 __register_event(event_type);
1171 handler->type = event_type;
1172 handler->cb = callback;
1173 handler->data = user_data;
1174 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1176 *event_handler = handler;
1178 return WIDGET_ERROR_NONE;
1181 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1187 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1189 return WIDGET_ERROR_FAULT;
1192 return WIDGET_ERROR_NOT_SUPPORTED;
1194 app_event_type_e type;
1196 if (event_handler == NULL)
1197 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1199 type = event_handler->type;
1200 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1201 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1203 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1204 free(event_handler);
1206 if (g_list_length(handler_list[type]) == 0)
1207 __unregister_event(type);
1209 return WIDGET_ERROR_NONE;
1212 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1214 if (!_is_widget_feature_enabled()) {
1215 _E("not supported");
1216 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1221 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1225 set_last_result(WIDGET_ERROR_NONE);
1229 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1232 widget_context_s *cxt = (widget_context_s *)context;
1233 Evas_Object *ret_win;
1234 Ecore_Wl_Window *wl_win;
1236 if (!_is_widget_feature_enabled()) {
1237 _E("not supported");
1238 return WIDGET_ERROR_NOT_SUPPORTED;
1241 if (context == NULL || win == NULL)
1242 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1243 __FUNCTION__, NULL);
1245 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1246 if (ret_win == NULL) {
1247 _E("failed to create window");
1248 return WIDGET_ERROR_FAULT;
1251 wl_win = elm_win_wl_window_get(ret_win);
1252 if (wl_win == NULL) {
1253 _E("failed to get wayland window");
1254 evas_object_del(ret_win);
1255 return WIDGET_ERROR_FAULT;
1258 ecore_wl_window_class_name_set(wl_win, cxt->id);
1262 cxt->win_id = ecore_wl_window_id_get(wl_win);
1264 _D("window created: %d", cxt->win_id);
1266 return WIDGET_ERROR_NONE;
1269 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1270 widget_instance_lifecycle_callback_s callback, void *user_data)
1274 if (!_is_widget_feature_enabled()) {
1275 _E("not supported");
1276 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1280 if (class_id == NULL) {
1281 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1285 wc = (widget_class_s *)malloc(sizeof(widget_class_s));
1287 _E("failed to malloc : %s", __FUNCTION__);
1288 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
1292 wc->classid = strdup(class_id);
1293 wc->user_data = user_data;
1298 set_last_result(WIDGET_ERROR_NONE);
1306 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1307 const char *class_id,
1308 widget_instance_lifecycle_callback_s callback, void *user_data)
1310 return _widget_class_create(widget_class, class_id, callback,
1314 EXPORT_API widget_class_h widget_app_class_create(
1315 widget_instance_lifecycle_callback_s callback, void *user_data)
1317 return _widget_class_create(class_provider, appid, callback, user_data);
1320 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1322 if (!_is_widget_feature_enabled()) {
1323 _E("not supported");
1324 return WIDGET_ERROR_NOT_SUPPORTED;
1327 if (context == NULL)
1328 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1329 __FUNCTION__, NULL);
1333 return WIDGET_ERROR_NONE;
1336 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1338 if (!_is_widget_feature_enabled()) {
1339 _E("not supported");
1340 return WIDGET_ERROR_NOT_SUPPORTED;
1343 if (context == NULL || tag == NULL)
1344 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1345 __FUNCTION__, NULL);
1347 *tag = context->tag;
1349 return WIDGET_ERROR_NONE;
1352 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1353 bundle *content_info)
1355 const char *class_id = NULL;
1358 if (!_is_widget_feature_enabled()) {
1359 _E("not supported");
1360 return WIDGET_ERROR_NOT_SUPPORTED;
1363 if (context == NULL || content_info == NULL)
1364 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1365 __FUNCTION__, NULL);
1367 if (context->provider == NULL)
1368 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1369 __FUNCTION__, NULL);
1371 class_id = context->provider->classid;
1373 if (class_id == NULL)
1374 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1376 ret = __send_update_status(class_id, context->id,
1377 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info, true);
1380 _E("failed to send content info: %s of %s (%d)", context->id,
1382 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1386 return WIDGET_ERROR_NONE;
1389 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1392 if (!_is_widget_feature_enabled()) {
1393 _E("not supported");
1394 return WIDGET_ERROR_NOT_SUPPORTED;
1397 if (!context || !title)
1398 return WIDGET_ERROR_INVALID_PARAMETER;
1401 elm_win_title_set(context->win, title);
1403 return WIDGET_ERROR_NONE;