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 if (handle->ops.resume)
251 handle->ops.resume(wc, handle->user_data);
253 wc->state = WC_RUNNING;
254 _D("%s is resumed", id);
255 ret = __send_update_status(handle->classid, wc->id,
256 WIDGET_INSTANCE_EVENT_RESUME, NULL, 0);
258 _E("context not found: %s", id);
265 static int __instance_pause(widget_class_h handle, const char *id, bundle *b)
267 widget_context_s *wc = __find_context_by_id(id);
271 if (handle->ops.pause)
272 handle->ops.pause(wc, handle->user_data);
274 wc->state = WC_PAUSED;
275 _D("%s is paused", id);
276 ret = __send_update_status(handle->classid, wc->id,
277 WIDGET_INSTANCE_EVENT_PAUSE, NULL, 0);
279 _E("context not found: %s", id);
286 static int __instance_resize(widget_class_h handle, const char *id, bundle *b)
288 widget_context_s *wc = __find_context_by_id(id);
297 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
298 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
301 w = (int)g_ascii_strtoll(w_str, &remain, 10);
306 h = (int)g_ascii_strtoll(h_str, &remain, 10);
310 if (handle->ops.resize)
311 handle->ops.resize(wc, w, h, handle->user_data);
312 _D("%s is resized to %dx%d", id, w, h);
313 ret = __send_update_status(handle->classid, wc->id,
314 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL, 0);
316 _E("context not found: %s", id);
323 static int __instance_update(widget_class_h handle, const char *id, bundle *b)
325 widget_context_s *wc = __find_context_by_id(id);
328 char *force_str = NULL;
331 _E("context not found: %s", id);
335 if (handle->ops.update) {
337 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
339 if (force_str && strcmp(force_str, "true") == 0)
344 handle->ops.update(wc, b, force, handle->user_data);
345 ret = __send_update_status(handle->classid, wc->id,
346 WIDGET_INSTANCE_EVENT_UPDATE, b, 0);
347 _D("updated:%s", id);
353 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
355 widget_context_s *wc = NULL;
357 char *w_str = NULL, *h_str = NULL;
361 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
363 return WIDGET_ERROR_OUT_OF_MEMORY;
365 wc->state = WC_READY;
366 wc->id = g_strdup(id);
367 wc->provider = handle;
371 wc->content = bundle_dup(b);
372 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
373 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
376 w = (int)g_ascii_strtoll(w_str, &remain, 10);
379 h = (int)g_ascii_strtoll(h_str, &remain, 10);
381 contexts = g_list_append(contexts, wc);
383 handle->ops.create(wc, b, w, h, handle->user_data);
384 ret = __send_update_status(handle->classid, wc->id,
385 WIDGET_INSTANCE_EVENT_CREATE, b, 0);
390 static int __instance_destroy(widget_class_h handle, const char *id,
391 widget_destroy_type_e reason, bundle *b)
393 widget_context_s *wc = __find_context_by_id(id);
397 wc->state = WC_TERMINATED;
398 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b,
401 ret = __send_update_status(handle->classid, id,
402 WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
404 contexts = g_list_remove(contexts, wc);
410 _E("could not find widget obj: %s", id);
411 ret = WIDGET_ERROR_INVALID_PARAMETER;
417 static widget_class_h __find_class_handler(const char *class_id,
418 widget_class_h handle)
420 if (!class_id || !handle)
423 widget_class_h head = handle;
426 if (head->classid && strcmp(head->classid, class_id) == 0)
435 static void __control(bundle *b)
437 char *class_id = NULL;
439 char *operation = NULL;
442 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
444 widget_class_h handle = NULL;
445 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
446 /* for previous version compatibility, use appid for default class id */
447 if (class_id == NULL)
450 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
451 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
453 handle = __find_class_handler(class_id, class_provider);
455 _E("no handle provided: %s", class_id);
460 _E("no operation provided");
464 if (strcmp(operation, "create") == 0) {
465 __instance_create(handle, id, b);
466 } else if (strcmp(operation, "resize") == 0) {
467 __instance_resize(handle, id, b);
468 } else if (strcmp(operation, "update") == 0) {
469 __instance_update(handle, id, b);
470 } else if (strcmp(operation, "destroy") == 0) {
471 bundle_get_str(b, WIDGET_K_REASON, &reason);
473 destroy_type = (int)g_ascii_strtoll(reason, &remain,
476 __instance_destroy(handle, id, destroy_type, b);
477 } else if (strcmp(operation, "resume") == 0) {
478 __instance_resume(handle, id, b);
479 } else if (strcmp(operation, "pause") == 0) {
480 __instance_pause(handle, id, b);
485 LOGD("error on control");
489 static void __resume_cb(const char *id, void *data)
491 widget_context_s *cxt = __find_context_by_id(id);
494 __instance_resume(cxt->provider, id, NULL);
496 _E("invalid context id:%s", id);
500 static void __pause_cb(const char *id, void *data)
502 widget_context_s *cxt = __find_context_by_id(id);
505 __instance_pause(cxt->provider, id, NULL);
507 _E("invalid context id:%s", id);
510 static void __pause_all()
512 GList *iter = g_list_first(contexts);
514 while (iter != NULL) {
515 widget_context_s *cxt = (widget_context_s *)iter->data;
516 const char *id = cxt->id;
518 switch (cxt->state) {
520 __resume_cb(id, NULL);
521 __pause_cb(id, NULL);
524 __pause_cb(id, NULL);
527 iter = g_list_next(iter);
531 static void __resume_all()
533 GList *iter = g_list_first(contexts);
535 while (iter != NULL) {
536 widget_context_s *cxt = (widget_context_s *)iter->data;
537 const char *id = cxt->id;
539 switch (cxt->state) {
541 __resume_cb(id, NULL);
544 __resume_cb(id, NULL);
547 iter = g_list_next(iter);
551 static Eina_Bool __show_cb(void *data, int type, void *event)
553 Ecore_Wl_Event_Window_Show *ev = event;
554 widget_context_s *cxt = __find_context_by_win(ev->win);
556 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
559 __instance_resume(cxt->provider, cxt->id, NULL);
561 LOGE("unknown window error: %d", ev->win);
563 return ECORE_CALLBACK_RENEW;
566 static Eina_Bool __hide_cb(void *data, int type, void *event)
568 Ecore_Wl_Event_Window_Hide *ev = event;
569 widget_context_s *cxt = __find_context_by_win(ev->win);
572 LOGD("hide %d", (unsigned int)ev->win);
575 __instance_pause(cxt->provider, cxt->id, NULL);
577 LOGE("unknown window error: %d", ev->win);
579 return ECORE_CALLBACK_RENEW;
582 static Eina_Bool __visibility_cb(void *data, int type, void *event)
584 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
585 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
586 /* this is not working so far*/
587 return ECORE_CALLBACK_RENEW;
590 static Eina_Bool __lower_cb(void *data, int type, void *event)
593 return ECORE_CALLBACK_RENEW;
596 static void __add_climsg()
598 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
599 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
600 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
601 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
604 static int __aul_handler(aul_type type, bundle *b, void *data)
612 bundle_get_str(b, WIDGET_K_CALLER, &caller);
614 caller_pid = g_ascii_strtoll(caller, &remain,
617 /* using caller appid and query pid using caller appid? */
637 static char *__get_domain_name(char *appid)
646 name_token = strrchr(appid, '.');
648 if (name_token == NULL) {
649 _E("appid is invalid");
658 static void __on_poweroff(keynode_t *key, void *data)
662 val = vconf_keynode_get_int(key);
664 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
665 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
666 _I("power off changed: %d", val);
669 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
670 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
677 extern int _set_i18n(const char *name);
679 static int __before_loop(int argc, char **argv)
683 char *wayland_display = NULL;
684 char *xdg_runtime_dir = NULL;
687 #if !(GLIB_CHECK_VERSION(2, 36, 0))
691 kb = bundle_import_from_argv(argc, argv);
693 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
694 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
695 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
696 if (viewer_endpoint) {
697 _E("viewer endpoint :%s", viewer_endpoint);
698 viewer_endpoint = strdup(viewer_endpoint);
700 _E("endpoint is missing");
704 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
707 setenv("WAYLAND_DISPLAY", wayland_display, 1);
712 _E("failed to get launch argv");
715 elm_init(argc, argv);
717 r = aul_launch_init(__aul_handler, NULL);
719 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
721 "Fail to call the aul_launch_init");
724 r = aul_launch_argv_handler(argc, argv);
726 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
728 "Fail to call the aul_launch_argv_handler");
731 r = app_get_id(&appid);
732 if (r != APP_ERROR_NONE)
735 name = __get_domain_name(appid);
738 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
740 "Fail to call __get_domain_name");
746 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
748 "Fail to call _set_i18n");
753 _widget_core_set_appcore_event_cb();
755 class_provider = app_ops->create(app_user_data);
756 if (class_provider == NULL) {
757 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
758 __FUNCTION__, "widget_class is NULL");
761 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
763 return WIDGET_ERROR_NONE;
766 static void __after_loop()
768 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
772 if (app_ops->terminate)
773 app_ops->terminate(app_user_data);
776 free(viewer_endpoint);
778 _widget_core_unset_appcore_event_cb();
779 __free_handler_list();
783 static void __on_low_memory(keynode_t *key, void *data)
787 val = vconf_keynode_get_int(key);
788 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
789 app_event_handler_h handler;
790 struct app_event_info event;
792 _I("widget_app_low_memory");
794 event.type = APP_EVENT_LOW_MEMORY;
795 event.value = (void *)&val;
797 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
800 handler = (app_event_handler_h) iter->data;
801 handler->cb(&event, handler->data);
802 iter = g_list_next(iter);
807 static void __on_low_battery(keynode_t *key, void *data)
811 val = vconf_keynode_get_int(key);
812 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
813 app_event_handler_h handler;
814 struct app_event_info event;
816 _I("widget_app_low_battery");
818 event.type = APP_EVENT_LOW_BATTERY;
819 event.value = (void *)&val;
821 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
824 handler = (app_event_handler_h) iter->data;
825 handler->cb(&event, handler->data);
826 iter = g_list_next(iter);
831 static void __on_lang_changed(keynode_t *key, void *data)
836 val = vconf_keynode_get_str(key);
838 app_event_handler_h handler;
839 struct app_event_info event;
841 _I("widget_app_lang_changed");
843 event.type = APP_EVENT_LANGUAGE_CHANGED;
844 event.value = (void *)val;
846 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
849 handler = (app_event_handler_h) iter->data;
850 handler->cb(&event, handler->data);
851 iter = g_list_next(iter);
855 static void __on_region_changed(keynode_t *key, void *data)
860 val = vconf_keynode_get_str(key);
862 app_event_handler_h handler;
863 struct app_event_info event;
865 _I("widget_app_region_changed");
867 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
868 event.value = (void *)val;
870 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
873 handler = (app_event_handler_h) iter->data;
874 handler->cb(&event, handler->data);
875 iter = g_list_next(iter);
879 static void __register_event(int event_type)
881 switch (event_type) {
882 case APP_EVENT_LOW_MEMORY:
883 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
886 case APP_EVENT_LOW_BATTERY:
887 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
890 case APP_EVENT_LANGUAGE_CHANGED:
891 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
894 case APP_EVENT_REGION_FORMAT_CHANGED:
895 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
900 static void __unregister_event(int event_type)
902 switch (event_type) {
903 case APP_EVENT_LOW_MEMORY:
904 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
907 case APP_EVENT_LOW_BATTERY:
908 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
911 case APP_EVENT_LANGUAGE_CHANGED:
912 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
915 case APP_EVENT_REGION_FORMAT_CHANGED:
916 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
921 static void _widget_core_set_appcore_event_cb(void)
923 __register_event(APP_EVENT_LANGUAGE_CHANGED);
924 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
927 static void _widget_core_unset_appcore_event_cb(void)
929 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
930 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
933 EXPORT_API int widget_app_main(int argc, char **argv,
934 widget_app_lifecycle_callback_s *callback, void *user_data)
938 if (!_is_widget_feature_enabled()) {
940 return WIDGET_ERROR_NOT_SUPPORTED;
943 if (argc <= 0 || argv == NULL || callback == NULL)
944 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
947 if (callback->create == NULL)
948 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
950 "widget_app_create_cb() callback must be "
954 app_user_data = user_data;
955 r = __before_loop(argc, argv);
959 ecore_main_loop_begin();
960 aul_status_update(STATUS_DYING);
963 return WIDGET_ERROR_NONE;
966 EXPORT_API int widget_app_exit(void)
968 if (!_is_widget_feature_enabled()) {
970 return WIDGET_ERROR_NOT_SUPPORTED;
973 ecore_main_loop_quit();
975 return WIDGET_ERROR_NONE;
978 static gboolean __finish_event_cb(gpointer user_data)
980 if (user_data == NULL)
983 widget_context_s *wc = (widget_context_s *)user_data;
1002 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1004 if (!_is_widget_feature_enabled()) {
1005 _E("not supported");
1006 return WIDGET_ERROR_NOT_SUPPORTED;
1009 if (context == NULL)
1010 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1011 __FUNCTION__, NULL);
1013 g_idle_add(__finish_event_cb, context);
1014 return WIDGET_ERROR_NONE;
1017 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1020 widget_context_s *wc;
1022 if (!_is_widget_feature_enabled()) {
1023 _E("not supported");
1024 return WIDGET_ERROR_NOT_SUPPORTED;
1028 return WIDGET_ERROR_INVALID_PARAMETER;
1030 list = g_list_first(contexts);
1033 wc = (widget_context_s *)list->data;
1041 return WIDGET_ERROR_NONE;
1044 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1045 app_event_type_e event_type, app_event_cb callback,
1051 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1053 return WIDGET_ERROR_FAULT;
1056 return WIDGET_ERROR_NOT_SUPPORTED;
1058 app_event_handler_h handler;
1060 if (event_handler == NULL || callback == NULL)
1061 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1063 if (event_type < APP_EVENT_LOW_MEMORY
1064 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1065 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1067 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1068 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1070 GList *iter = g_list_first(handler_list[event_type]);
1073 handler = (app_event_handler_h) iter->data;
1075 if (handler->cb == callback)
1076 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1078 iter = g_list_next(iter);
1081 handler = calloc(1, sizeof(struct app_event_handler));
1083 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
1085 if (g_list_length(handler_list[event_type]) == 0)
1086 __register_event(event_type);
1088 handler->type = event_type;
1089 handler->cb = callback;
1090 handler->data = user_data;
1091 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1093 *event_handler = handler;
1095 return WIDGET_ERROR_NONE;
1098 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1104 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1106 return WIDGET_ERROR_FAULT;
1109 return WIDGET_ERROR_NOT_SUPPORTED;
1111 app_event_type_e type;
1113 if (event_handler == NULL)
1114 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1116 type = event_handler->type;
1117 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1118 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1120 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1121 free(event_handler);
1123 if (g_list_length(handler_list[type]) == 0)
1124 __unregister_event(type);
1126 return WIDGET_ERROR_NONE;
1129 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1131 if (!_is_widget_feature_enabled()) {
1132 _E("not supported");
1133 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1138 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1142 set_last_result(WIDGET_ERROR_NONE);
1146 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1149 widget_context_s *cxt = (widget_context_s *)context;
1150 Evas_Object *ret_win;
1151 Ecore_Wl_Window *wl_win;
1153 if (!_is_widget_feature_enabled()) {
1154 _E("not supported");
1155 return WIDGET_ERROR_NOT_SUPPORTED;
1158 if (context == NULL || win == NULL)
1159 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1160 __FUNCTION__, NULL);
1162 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1163 if (ret_win == NULL) {
1164 _E("failed to create window");
1165 return WIDGET_ERROR_FAULT;
1168 wl_win = elm_win_wl_window_get(ret_win);
1169 if (wl_win == NULL) {
1170 _E("failed to get wayland window");
1171 evas_object_del(ret_win);
1172 return WIDGET_ERROR_FAULT;
1175 ecore_wl_window_class_name_set(wl_win, cxt->id);
1179 cxt->win_id = ecore_wl_window_id_get(wl_win);
1181 _D("window created: %d", cxt->win_id);
1183 return WIDGET_ERROR_NONE;
1186 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1187 widget_instance_lifecycle_callback_s callback, void *user_data)
1191 if (!_is_widget_feature_enabled()) {
1192 _E("not supported");
1193 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1197 if (class_id == NULL) {
1198 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1202 wc = (widget_class_s *)malloc(sizeof(widget_class_s));
1204 _E("failed to malloc : %s", __FUNCTION__);
1205 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
1209 wc->classid = strdup(class_id);
1210 wc->user_data = user_data;
1215 set_last_result(WIDGET_ERROR_NONE);
1223 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1224 const char *class_id,
1225 widget_instance_lifecycle_callback_s callback, void *user_data)
1227 return _widget_class_create(widget_class, class_id, callback,
1231 EXPORT_API widget_class_h widget_app_class_create(
1232 widget_instance_lifecycle_callback_s callback, void *user_data)
1234 return _widget_class_create(class_provider, appid, callback, user_data);
1237 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1239 if (!_is_widget_feature_enabled()) {
1240 _E("not supported");
1241 return WIDGET_ERROR_NOT_SUPPORTED;
1244 if (context == NULL)
1245 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1246 __FUNCTION__, NULL);
1250 return WIDGET_ERROR_NONE;
1253 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1255 if (!_is_widget_feature_enabled()) {
1256 _E("not supported");
1257 return WIDGET_ERROR_NOT_SUPPORTED;
1260 if (context == NULL || tag == NULL)
1261 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1262 __FUNCTION__, NULL);
1264 *tag = context->tag;
1266 return WIDGET_ERROR_NONE;
1269 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1270 bundle *content_info)
1272 const char *class_id = NULL;
1275 if (!_is_widget_feature_enabled()) {
1276 _E("not supported");
1277 return WIDGET_ERROR_NOT_SUPPORTED;
1280 if (context == NULL || content_info == NULL)
1281 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1282 __FUNCTION__, NULL);
1284 if (context->provider == NULL)
1285 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1286 __FUNCTION__, NULL);
1288 class_id = context->provider->classid;
1290 if (class_id == NULL)
1291 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1293 ret = __send_update_status(class_id, context->id,
1294 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info, true);
1297 _E("failed to send content info: %s of %s (%d)", context->id,
1299 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1303 return WIDGET_ERROR_NONE;
1306 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1309 if (!_is_widget_feature_enabled()) {
1310 _E("not supported");
1311 return WIDGET_ERROR_NOT_SUPPORTED;
1314 if (!context || !title)
1315 return WIDGET_ERROR_INVALID_PARAMETER;
1318 elm_win_title_set(context->win, title);
1320 return WIDGET_ERROR_NONE;