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);
448 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
449 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
452 w = (int)g_ascii_strtoll(w_str, &remain, 10);
454 _E("unable to get width");
459 h = (int)g_ascii_strtoll(h_str, &remain, 10);
461 _E("unable to get height");
466 evas_object_resize(wc->win, w, h);
468 _E("unable to find window of %d", wc->id);
471 static void __control(bundle *b)
473 char *class_id = NULL;
475 char *operation = NULL;
478 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
480 widget_class_h handle = NULL;
481 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
482 /* for previous version compatibility, use appid for default class id */
483 if (class_id == NULL)
486 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
487 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
489 handle = __find_class_handler(class_id, class_provider);
491 _E("no handle provided: %s", class_id);
496 _E("no operation provided");
500 if (strcmp(operation, "create") == 0) {
501 __instance_create(handle, id, b);
502 } else if (strcmp(operation, "resize") == 0) {
503 __resize_window(id, b);
504 } else if (strcmp(operation, "update") == 0) {
505 __instance_update(handle, id, b);
506 } else if (strcmp(operation, "destroy") == 0) {
507 bundle_get_str(b, WIDGET_K_REASON, &reason);
509 destroy_type = (int)g_ascii_strtoll(reason, &remain,
512 __instance_destroy(handle, id, destroy_type, b);
513 } else if (strcmp(operation, "resume") == 0) {
514 __instance_resume(handle, id, b);
515 } else if (strcmp(operation, "pause") == 0) {
516 __instance_pause(handle, id, b);
521 LOGD("error on control");
525 static void __resume_cb(const char *id, void *data)
527 widget_context_s *cxt = __find_context_by_id(id);
530 _E("invalid context id:%s", id);
534 __instance_resume(cxt->provider, id, NULL);
537 static void __pause_cb(const char *id, void *data)
539 widget_context_s *cxt = __find_context_by_id(id);
542 _E("invalid context id:%s", id);
546 __instance_pause(cxt->provider, id, NULL);
549 static void __pause_all()
551 GList *iter = g_list_first(contexts);
553 while (iter != NULL) {
554 widget_context_s *cxt = (widget_context_s *)iter->data;
555 const char *id = cxt->id;
557 switch (cxt->state) {
559 __resume_cb(id, NULL);
560 __pause_cb(id, NULL);
563 __pause_cb(id, NULL);
566 iter = g_list_next(iter);
570 static void __resume_all()
572 GList *iter = g_list_first(contexts);
574 while (iter != NULL) {
575 widget_context_s *cxt = (widget_context_s *)iter->data;
576 const char *id = cxt->id;
578 switch (cxt->state) {
580 __resume_cb(id, NULL);
583 __resume_cb(id, NULL);
586 iter = g_list_next(iter);
590 static Eina_Bool __show_cb(void *data, int type, void *event)
592 Ecore_Wl_Event_Window_Show *ev = event;
593 widget_context_s *cxt = __find_context_by_win(ev->win);
595 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
598 __instance_resume(cxt->provider, cxt->id, NULL);
600 LOGE("unknown window error: %d", ev->win);
602 return ECORE_CALLBACK_RENEW;
605 static Eina_Bool __hide_cb(void *data, int type, void *event)
607 Ecore_Wl_Event_Window_Hide *ev = event;
608 widget_context_s *cxt = __find_context_by_win(ev->win);
611 LOGD("hide %d", (unsigned int)ev->win);
614 __instance_pause(cxt->provider, cxt->id, NULL);
616 LOGE("unknown window error: %d", ev->win);
618 return ECORE_CALLBACK_RENEW;
621 static Eina_Bool __visibility_cb(void *data, int type, void *event)
623 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
624 widget_context_s *cxt = __find_context_by_win(ev->win);
626 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
629 LOGE("unknown window error: %d", ev->win);
630 return ECORE_CALLBACK_RENEW;
633 if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
634 __instance_resume(cxt->provider, cxt->id, NULL);
635 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
636 __instance_pause(cxt->provider, cxt->id, NULL);
638 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
641 return ECORE_CALLBACK_RENEW;
644 static Eina_Bool __lower_cb(void *data, int type, void *event)
647 return ECORE_CALLBACK_RENEW;
650 static Eina_Bool __configure_cb(void *data, int type, void *event)
652 Ecore_Wl_Event_Window_Configure *ev = event;
653 widget_context_s *cxt = __find_context_by_win(ev->win);
655 LOGD("configure: %d %d", ev->w, ev->h);
658 LOGE("unknown window error: %d", ev->win);
659 return ECORE_CALLBACK_RENEW;
662 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
663 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h, NULL);
664 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
666 return ECORE_CALLBACK_RENEW;
669 static void __add_climsg()
671 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
672 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
673 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
674 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
675 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
678 static int __aul_handler(aul_type type, bundle *b, void *data)
686 bundle_get_str(b, WIDGET_K_CALLER, &caller);
688 caller_pid = g_ascii_strtoll(caller, &remain,
691 /* using caller appid and query pid using caller appid? */
711 static char *__get_domain_name(char *appid)
720 name_token = strrchr(appid, '.');
722 if (name_token == NULL) {
723 _E("appid is invalid");
732 static void __on_poweroff(keynode_t *key, void *data)
736 val = vconf_keynode_get_int(key);
738 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
739 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
740 _I("power off changed: %d", val);
743 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
744 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
751 extern int _set_i18n(const char *name);
753 static int __before_loop(int argc, char **argv)
757 char *wayland_display = NULL;
758 char *xdg_runtime_dir = NULL;
761 #if !(GLIB_CHECK_VERSION(2, 36, 0))
765 kb = bundle_import_from_argv(argc, argv);
767 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
768 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
769 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
770 if (viewer_endpoint) {
771 _E("viewer endpoint :%s", viewer_endpoint);
772 viewer_endpoint = strdup(viewer_endpoint);
774 _E("endpoint is missing");
778 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
780 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
783 setenv("WAYLAND_DISPLAY", wayland_display, 1);
785 _D("wayland_display:%s", wayland_display);
790 _E("failed to get launch argv");
793 elm_init(argc, argv);
795 r = aul_launch_init(__aul_handler, NULL);
797 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
799 "Fail to call the aul_launch_init");
802 r = aul_launch_argv_handler(argc, argv);
804 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
806 "Fail to call the aul_launch_argv_handler");
809 r = app_get_id(&appid);
810 if (r != APP_ERROR_NONE)
813 name = __get_domain_name(appid);
816 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
818 "Fail to call __get_domain_name");
824 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
826 "Fail to call _set_i18n");
831 _widget_core_set_appcore_event_cb();
833 class_provider = app_ops->create(app_user_data);
834 if (class_provider == NULL) {
835 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
836 __FUNCTION__, "widget_class is NULL");
839 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
841 return WIDGET_ERROR_NONE;
844 static void __after_loop()
846 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
850 if (app_ops->terminate)
851 app_ops->terminate(app_user_data);
854 free(viewer_endpoint);
856 _widget_core_unset_appcore_event_cb();
857 __free_handler_list();
861 static void __on_low_memory(keynode_t *key, void *data)
865 val = vconf_keynode_get_int(key);
866 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
867 app_event_handler_h handler;
868 struct app_event_info event;
870 _I("widget_app_low_memory");
872 event.type = APP_EVENT_LOW_MEMORY;
873 event.value = (void *)&val;
875 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
878 handler = (app_event_handler_h) iter->data;
879 handler->cb(&event, handler->data);
880 iter = g_list_next(iter);
885 static void __on_low_battery(keynode_t *key, void *data)
889 val = vconf_keynode_get_int(key);
890 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
891 app_event_handler_h handler;
892 struct app_event_info event;
894 _I("widget_app_low_battery");
896 event.type = APP_EVENT_LOW_BATTERY;
897 event.value = (void *)&val;
899 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
902 handler = (app_event_handler_h) iter->data;
903 handler->cb(&event, handler->data);
904 iter = g_list_next(iter);
909 static void __on_lang_changed(keynode_t *key, void *data)
914 val = vconf_keynode_get_str(key);
916 app_event_handler_h handler;
917 struct app_event_info event;
919 _I("widget_app_lang_changed");
921 event.type = APP_EVENT_LANGUAGE_CHANGED;
922 event.value = (void *)val;
924 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
927 handler = (app_event_handler_h) iter->data;
928 handler->cb(&event, handler->data);
929 iter = g_list_next(iter);
933 static void __on_region_changed(keynode_t *key, void *data)
938 val = vconf_keynode_get_str(key);
940 app_event_handler_h handler;
941 struct app_event_info event;
943 _I("widget_app_region_changed");
945 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
946 event.value = (void *)val;
948 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
951 handler = (app_event_handler_h) iter->data;
952 handler->cb(&event, handler->data);
953 iter = g_list_next(iter);
957 static void __register_event(int event_type)
959 switch (event_type) {
960 case APP_EVENT_LOW_MEMORY:
961 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
964 case APP_EVENT_LOW_BATTERY:
965 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
968 case APP_EVENT_LANGUAGE_CHANGED:
969 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
972 case APP_EVENT_REGION_FORMAT_CHANGED:
973 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
978 static void __unregister_event(int event_type)
980 switch (event_type) {
981 case APP_EVENT_LOW_MEMORY:
982 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
985 case APP_EVENT_LOW_BATTERY:
986 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
989 case APP_EVENT_LANGUAGE_CHANGED:
990 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
993 case APP_EVENT_REGION_FORMAT_CHANGED:
994 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
999 static void _widget_core_set_appcore_event_cb(void)
1001 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1002 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1005 static void _widget_core_unset_appcore_event_cb(void)
1007 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1008 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1011 EXPORT_API int widget_app_main(int argc, char **argv,
1012 widget_app_lifecycle_callback_s *callback, void *user_data)
1016 if (!_is_widget_feature_enabled()) {
1017 _E("not supported");
1018 return WIDGET_ERROR_NOT_SUPPORTED;
1021 if (argc <= 0 || argv == NULL || callback == NULL)
1022 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1023 __FUNCTION__, NULL);
1025 if (callback->create == NULL)
1026 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1028 "widget_app_create_cb() callback must be "
1032 app_user_data = user_data;
1033 r = __before_loop(argc, argv);
1037 ecore_main_loop_begin();
1038 aul_status_update(STATUS_DYING);
1041 return WIDGET_ERROR_NONE;
1044 EXPORT_API int widget_app_exit(void)
1046 if (!_is_widget_feature_enabled()) {
1047 _E("not supported");
1048 return WIDGET_ERROR_NOT_SUPPORTED;
1051 ecore_main_loop_quit();
1053 return WIDGET_ERROR_NONE;
1056 static gboolean __finish_event_cb(gpointer user_data)
1058 if (user_data == NULL)
1061 widget_context_s *wc = (widget_context_s *)user_data;
1063 switch (wc->state) {
1080 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1082 if (!_is_widget_feature_enabled()) {
1083 _E("not supported");
1084 return WIDGET_ERROR_NOT_SUPPORTED;
1087 if (context == NULL)
1088 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1089 __FUNCTION__, NULL);
1091 g_idle_add(__finish_event_cb, context);
1092 return WIDGET_ERROR_NONE;
1095 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1098 widget_context_s *wc;
1100 if (!_is_widget_feature_enabled()) {
1101 _E("not supported");
1102 return WIDGET_ERROR_NOT_SUPPORTED;
1106 return WIDGET_ERROR_INVALID_PARAMETER;
1108 list = g_list_first(contexts);
1111 wc = (widget_context_s *)list->data;
1119 return WIDGET_ERROR_NONE;
1122 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1123 app_event_type_e event_type, app_event_cb callback,
1129 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1131 return WIDGET_ERROR_FAULT;
1134 return WIDGET_ERROR_NOT_SUPPORTED;
1136 app_event_handler_h handler;
1138 if (event_handler == NULL || callback == NULL)
1139 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1141 if (event_type < APP_EVENT_LOW_MEMORY
1142 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1143 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1145 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1146 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1148 GList *iter = g_list_first(handler_list[event_type]);
1151 handler = (app_event_handler_h) iter->data;
1153 if (handler->cb == callback)
1154 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1156 iter = g_list_next(iter);
1159 handler = calloc(1, sizeof(struct app_event_handler));
1161 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
1163 if (g_list_length(handler_list[event_type]) == 0)
1164 __register_event(event_type);
1166 handler->type = event_type;
1167 handler->cb = callback;
1168 handler->data = user_data;
1169 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1171 *event_handler = handler;
1173 return WIDGET_ERROR_NONE;
1176 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1182 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1184 return WIDGET_ERROR_FAULT;
1187 return WIDGET_ERROR_NOT_SUPPORTED;
1189 app_event_type_e type;
1191 if (event_handler == NULL)
1192 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1194 type = event_handler->type;
1195 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1196 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1198 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1199 free(event_handler);
1201 if (g_list_length(handler_list[type]) == 0)
1202 __unregister_event(type);
1204 return WIDGET_ERROR_NONE;
1207 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1209 if (!_is_widget_feature_enabled()) {
1210 _E("not supported");
1211 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1216 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1220 set_last_result(WIDGET_ERROR_NONE);
1224 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1227 widget_context_s *cxt = (widget_context_s *)context;
1228 Evas_Object *ret_win;
1229 Ecore_Wl_Window *wl_win;
1231 if (!_is_widget_feature_enabled()) {
1232 _E("not supported");
1233 return WIDGET_ERROR_NOT_SUPPORTED;
1236 if (context == NULL || win == NULL)
1237 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1238 __FUNCTION__, NULL);
1240 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1241 if (ret_win == NULL) {
1242 _E("failed to create window");
1243 return WIDGET_ERROR_FAULT;
1246 wl_win = elm_win_wl_window_get(ret_win);
1247 if (wl_win == NULL) {
1248 _E("failed to get wayland window");
1249 evas_object_del(ret_win);
1250 return WIDGET_ERROR_FAULT;
1253 ecore_wl_window_class_name_set(wl_win, cxt->id);
1257 cxt->win_id = ecore_wl_window_id_get(wl_win);
1259 _D("window created: %d", cxt->win_id);
1261 return WIDGET_ERROR_NONE;
1264 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1265 widget_instance_lifecycle_callback_s callback, void *user_data)
1269 if (!_is_widget_feature_enabled()) {
1270 _E("not supported");
1271 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1275 if (class_id == NULL) {
1276 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1280 wc = (widget_class_s *)malloc(sizeof(widget_class_s));
1282 _E("failed to malloc : %s", __FUNCTION__);
1283 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
1287 wc->classid = strdup(class_id);
1288 wc->user_data = user_data;
1293 set_last_result(WIDGET_ERROR_NONE);
1301 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1302 const char *class_id,
1303 widget_instance_lifecycle_callback_s callback, void *user_data)
1305 return _widget_class_create(widget_class, class_id, callback,
1309 EXPORT_API widget_class_h widget_app_class_create(
1310 widget_instance_lifecycle_callback_s callback, void *user_data)
1312 return _widget_class_create(class_provider, appid, callback, user_data);
1315 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1317 if (!_is_widget_feature_enabled()) {
1318 _E("not supported");
1319 return WIDGET_ERROR_NOT_SUPPORTED;
1322 if (context == NULL)
1323 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1324 __FUNCTION__, NULL);
1328 return WIDGET_ERROR_NONE;
1331 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1333 if (!_is_widget_feature_enabled()) {
1334 _E("not supported");
1335 return WIDGET_ERROR_NOT_SUPPORTED;
1338 if (context == NULL || tag == NULL)
1339 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1340 __FUNCTION__, NULL);
1342 *tag = context->tag;
1344 return WIDGET_ERROR_NONE;
1347 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1348 bundle *content_info)
1350 const char *class_id = NULL;
1353 if (!_is_widget_feature_enabled()) {
1354 _E("not supported");
1355 return WIDGET_ERROR_NOT_SUPPORTED;
1358 if (context == NULL || content_info == NULL)
1359 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1360 __FUNCTION__, NULL);
1362 if (context->provider == NULL)
1363 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1364 __FUNCTION__, NULL);
1366 class_id = context->provider->classid;
1368 if (class_id == NULL)
1369 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1371 ret = __send_update_status(class_id, context->id,
1372 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info, true);
1375 _E("failed to send content info: %s of %s (%d)", context->id,
1377 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1381 return WIDGET_ERROR_NONE;
1384 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1387 if (!_is_widget_feature_enabled()) {
1388 _E("not supported");
1389 return WIDGET_ERROR_NOT_SUPPORTED;
1392 if (!context || !title)
1393 return WIDGET_ERROR_INVALID_PARAMETER;
1396 elm_win_title_set(context->win, title);
1398 return WIDGET_ERROR_NONE;