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.
22 #include <bundle_internal.h>
26 #include <glib-object.h>
28 #include <app_control.h>
29 #include <app_control_internal.h>
30 #include <Elementary.h>
31 #include <widget_errno.h>
32 #include <widget_instance.h>
33 #include <widget_service.h>
34 #include <widget_service_internal.h>
35 #include <aul_app_com.h>
36 #include <Ecore_Wayland.h>
37 #include <system_info.h>
39 #include <vconf-internal-keys.h>
40 #include <screen_connector_provider.h>
42 #include "widget_app.h"
43 #include "widget-log.h"
44 #include "widget-private.h"
45 #include "widget_app_internal.h"
51 #define STR_MAX_BUF 128
52 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
53 #define K_REASON "__WC_K_REASON__"
54 #define APP_TYPE_WIDGET "widgetapp"
55 #define STATUS_FOREGROUND "fg"
56 #define STATUS_BACKGROUND "bg"
58 typedef enum _widget_obj_state_e {
70 struct app_event_handler {
71 app_event_type_e type;
76 struct app_event_info {
77 app_event_type_e type;
81 typedef struct _widget_class widget_class_s;
83 #define WIDGET_APP_EVENT_MAX 5
84 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
86 static int caller_pid;
87 static widget_app_lifecycle_callback_s *app_ops;
88 static void *app_user_data;
90 static widget_class_h class_provider;
91 static int exit_called;
92 static char *package_id;
93 static bool fg_signal;
94 static bool is_permanent;
96 static void _widget_core_set_appcore_event_cb(void);
97 static void _widget_core_unset_appcore_event_cb(void);
98 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content);
100 static void __free_handler_cb(gpointer data)
106 static void __free_handler_list(void)
110 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
111 g_list_free_full(handler_list[i], __free_handler_cb);
112 handler_list[i] = NULL;
116 static inline bool _is_widget_feature_enabled(void)
118 static bool feature = false;
119 static bool retrieved = false;
122 if (retrieved == true)
125 ret = system_info_get_platform_bool(
126 "http://tizen.org/feature/shell.appwidget", &feature);
127 if (ret != SYSTEM_INFO_ERROR_NONE) {
128 _E("failed to get system info"); /* LCOV_EXCL_LINE */
129 return false; /* LCOV_EXCL_LINE */
137 static gint __comp_by_id(gconstpointer a, gconstpointer b)
139 widget_context_s *wc = (widget_context_s *)a;
141 return strcmp(wc->id, (const char *)b);
144 static widget_context_s *__find_context_by_id(const char *id)
147 GList *contexts = _widget_app_get_contexts();
152 ret = g_list_find_custom(contexts, id, __comp_by_id);
159 static gint __comp_by_state(gconstpointer a, gconstpointer b)
161 widget_context_s *wc = (widget_context_s *)a;
163 if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
169 static widget_context_s *__find_context_by_state(widget_obj_state_e state)
172 GList *contexts = _widget_app_get_contexts();
174 ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
181 static gint __comp_by_win(gconstpointer a, gconstpointer b)
183 int win = GPOINTER_TO_INT(b);
184 widget_context_s *wc = (widget_context_s *)a;
186 return (wc && wc->win_id == win) ? 0 : -1;
189 static widget_context_s *__find_context_by_win(int win)
191 GList *contexts = _widget_app_get_contexts();
192 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
200 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
203 bundle *b = bundle_create();
204 char pkgid[256] = {0, };
208 _E("out of memory"); /* LCOV_EXCL_LINE */
209 return -1; /* LCOV_EXCL_LINE */
212 if (package_id == NULL) {
213 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
215 package_id = strdup(pkgid);
218 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
219 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
220 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
222 bundle_add_str(b, AUL_K_PKGID, package_id);
224 _D("send lifecycle %s(%d)", instance_id, status);
225 ret = aul_app_com_send("widget.status", b);
227 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
234 static int __send_update_status(const char *class_id, const char *instance_id,
235 int status, bundle *extra)
239 bundle_raw *raw = NULL;
241 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
245 _E("out of memory"); /* LCOV_EXCL_LINE */
246 return -1; /* LCOV_EXCL_LINE */
249 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
250 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
251 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
254 bundle_encode(extra, &raw, &len);
255 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
256 aul_widget_instance_add(class_id, instance_id);
259 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
260 aul_app_com_send(viewer_endpoint, b);
263 case WIDGET_INSTANCE_EVENT_CREATE:
264 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
266 case WIDGET_INSTANCE_EVENT_DESTROY:
267 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
269 case WIDGET_INSTANCE_EVENT_PAUSE:
270 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
272 case WIDGET_INSTANCE_EVENT_RESUME:
273 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
278 __send_lifecycle_event(class_id, instance_id, lifecycle);
287 static gboolean __timeout_cb(gpointer user_data)
289 widget_context_s *wc = user_data;
291 if (wc->state == WC_RUNNING) {
292 _D("Periodic update!");
293 __instance_update(wc->provider, wc->id, 0, NULL);
294 } else if (wc->state == WC_PAUSED ||
295 wc->state == WC_READY) {
296 wc->pending_update = true;
297 if (wc->periodic_timer) {
299 g_source_remove(wc->periodic_timer);
300 wc->periodic_timer = 0;
304 return G_SOURCE_CONTINUE;
307 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
309 widget_context_s *wc = __find_context_by_id(id);
313 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
314 return -1; /* LCOV_EXCL_LINE */
317 if (wc->state == WC_RUNNING) {
318 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
319 return 0; /* LCOV_EXCL_LINE */
322 if (wc->state == WC_TERMINATED) {
323 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
324 return 0; /* LCOV_EXCL_LINE */
327 if (wc->pending_update) {
328 _D("pending update!");
329 wc->pending_update = false;
330 __instance_update(wc->provider, wc->id, 0, NULL);
332 if (wc->period > 0) {
333 _D("Restart timer!");
334 wc->periodic_timer = g_timeout_add_seconds(wc->period,
339 if (handle->ops.resume)
340 handle->ops.resume(wc, handle->user_data);
342 wc->state = WC_RUNNING;
343 _D("%s is resumed", id);
345 ret = __send_update_status(handle->classid, wc->id,
346 WIDGET_INSTANCE_EVENT_RESUME, NULL);
348 _D("Send fg signal to resourceD");
349 aul_send_app_status_change_signal(getpid(),
363 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
365 widget_context_s *wc = __find_context_by_id(id);
369 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
370 return -1; /* LCOV_EXCL_LINE */
373 if (wc->state == WC_PAUSED) {
374 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
375 return 0; /* LCOV_EXCL_LINE */
378 if (wc->state == WC_TERMINATED) {
379 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
380 return 0; /* LCOV_EXCL_LINE */
383 if (handle->ops.pause)
384 handle->ops.pause(wc, handle->user_data);
386 wc->state = WC_PAUSED;
387 _D("%s is paused", id);
389 ret = __send_update_status(handle->classid, wc->id,
390 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
391 wc = __find_context_by_state(WC_RUNNING);
392 if (!wc && fg_signal) {
393 _D("Send bg signal to resourceD");
394 aul_send_app_status_change_signal(getpid(),
408 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
410 widget_context_s *wc = __find_context_by_id(id);
414 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
415 return -1; /* LCOV_EXCL_LINE */
418 if (handle->ops.resize)
419 handle->ops.resize(wc, w, h, handle->user_data);
421 _D("%s is resized to %dx%d", id, w, h);
422 ret = __send_update_status(handle->classid, wc->id,
423 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
428 /* LCOV_EXCL_START */
429 static int __instance_update_all(widget_class_h handle, int force, const char *content)
431 widget_context_s *wc;
434 GList *context = _widget_app_get_contexts();
437 b = bundle_decode((const bundle_raw *)content, strlen(content));
439 if (handle->ops.update) {
441 wc = (widget_context_s *)context->data;
442 handle->ops.update(wc, b, force, handle->user_data);
443 ret = __send_update_status(handle->classid, wc->id,
444 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
445 _D("updated:%s", wc->id);
446 context = context->next;
457 /* LCOV_EXCL_START */
458 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
460 widget_context_s *wc = __find_context_by_id(id);
464 _E("context not found: %s", id);
469 b = bundle_decode((const bundle_raw *)content, strlen(content));
471 if (handle->ops.update) {
472 handle->ops.update(wc, b, force, handle->user_data);
473 ret = __send_update_status(handle->classid, wc->id,
474 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
475 _D("updated:%s", id);
485 static int __instance_create(widget_class_h handle, const char *id,
486 const char *content, int w, int h, double period)
488 widget_context_s *wc = NULL;
490 bundle *content_info = NULL;
492 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
495 return WIDGET_ERROR_OUT_OF_MEMORY;
498 wc->state = WC_READY;
500 wc->provider = handle;
505 wc->content = strdup(content);
506 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
509 _widget_app_add_context(wc);
511 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
513 _W("Create callback resturns error(%d)", ret);
514 ret = __send_update_status(handle->classid, wc->id,
515 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
516 _widget_app_remove_context(wc);
523 if (_widget_app_get_contexts() == NULL && !exit_called)
526 ret = __send_update_status(handle->classid, wc->id,
527 WIDGET_INSTANCE_EVENT_CREATE, NULL);
532 aul_widget_instance_add(handle->classid, id);
536 bundle_free(content_info);
540 wc->periodic_timer = g_timeout_add_seconds(period,
547 static int __instance_destroy(widget_class_h handle, const char *id,
548 widget_app_destroy_type_e reason, int send_update)
550 widget_context_s *wc = __find_context_by_id(id);
552 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
553 bundle *content_info;
556 _E("could not find widget obj: %s, clear amd info", id); /* LCOV_EXCL_LINE */
557 aul_widget_instance_del(handle->classid, id); /* LCOV_EXCL_LINE */
558 return WIDGET_ERROR_NONE; /* LCOV_EXCL_LINE */
561 wc->state = WC_TERMINATED;
563 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
565 content_info = bundle_create();
567 handle->ops.destroy(wc, reason, content_info,
570 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
572 event = WIDGET_INSTANCE_EVENT_DESTROY;
573 aul_widget_instance_del(handle->classid, id);
575 is_permanent = false;
576 ret = __send_update_status(handle->classid, id,
577 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
581 bundle_free(content_info);
583 ret = __send_update_status(handle->classid, id, event, NULL);
585 _widget_app_remove_context(wc);
593 if (wc->periodic_timer)
594 g_source_remove(wc->periodic_timer);
598 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
604 static int __instance_change_period(const char *id, double period)
606 widget_context_s *wc = __find_context_by_id(id);
609 _E("could not find widget obj: %s", id);
613 if (wc->periodic_timer) {
615 g_source_remove(wc->periodic_timer);
616 wc->periodic_timer = 0;
620 if (wc->period > 0) {
621 _D("Restart timer!");
622 wc->periodic_timer = g_timeout_add_seconds(wc->period,
629 static widget_class_h __find_class_handler(const char *class_id,
630 widget_class_h handle)
632 if (!class_id || !handle)
635 widget_class_h head = handle;
638 if (head->classid && strcmp(head->classid, class_id) == 0)
647 /* LCOV_EXCL_START */
648 static void __resize_window(char *id, int w, int h)
650 widget_context_s *wc = __find_context_by_id(id);
653 _E("can not find instance: %s", id);
658 evas_object_resize(wc->win, w, h);
660 _E("unable to find window of %d", wc->id);
664 static void __control(bundle *b)
666 char *class_id = NULL;
668 char *operation = NULL;
669 char *content = NULL;
676 char *force_str = NULL;
677 double *period = NULL;
678 double update_period = 0;
679 widget_class_h handle = NULL;
683 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
684 /* for previous version compatibility, use appid for default class id */
685 if (class_id == NULL)
688 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
689 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
691 handle = __find_class_handler(class_id, class_provider);
693 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
698 _E("no operation provided");
702 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
704 if (force_str && strcmp(force_str, "true") == 0)
709 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
710 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
711 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
713 w = (int)g_ascii_strtoll(w_str, &remain, 10);
716 h = (int)g_ascii_strtoll(h_str, &remain, 10);
718 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
719 if (ret == BUNDLE_ERROR_NONE)
720 update_period = *period;
722 if (strcmp(operation, "create") == 0) {
723 __instance_create(handle, id, content, w, h, update_period);
724 } else if (strcmp(operation, "resize") == 0) {
725 __resize_window(id, w, h);
726 __instance_resize(handle, id, w, h);
727 } else if (strcmp(operation, "update") == 0) {
729 __instance_update(handle, id, force, content);
731 __instance_update_all(handle, force, content);
733 } else if (strcmp(operation, "destroy") == 0) {
734 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
735 } else if (strcmp(operation, "resume") == 0) {
736 __instance_resume(handle, id, UPDATE_ALL);
737 } else if (strcmp(operation, "pause") == 0) {
738 __instance_pause(handle, id, UPDATE_ALL);
739 } else if (strcmp(operation, "terminate") == 0) {
740 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
741 } else if (strcmp(operation, "period") == 0) {
742 __instance_change_period(id, update_period);
747 LOGD("error on control");
751 static void __pause_all(int send_update)
753 GList *contexts = _widget_app_get_contexts();
754 GList *iter = g_list_first(contexts);
756 while (iter != NULL) {
757 widget_context_s *cxt = (widget_context_s *)iter->data;
759 switch (cxt->state) {
761 __instance_resume(cxt->provider, cxt->id, send_update);
762 __instance_pause(cxt->provider, cxt->id, send_update);
765 __instance_pause(cxt->provider, cxt->id, send_update);
768 LOGD("pause %s", cxt->id);
769 iter = g_list_next(iter);
773 /* LCOV_EXCL_START */
774 static void __resume_all(int send_update)
776 GList *contexts = _widget_app_get_contexts();
777 GList *iter = g_list_first(contexts);
779 while (iter != NULL) {
780 widget_context_s *cxt = (widget_context_s *)iter->data;
782 switch (cxt->state) {
784 __instance_resume(cxt->provider, cxt->id, send_update);
787 __instance_resume(cxt->provider, cxt->id, send_update);
790 iter = g_list_next(iter);
795 static void __destroy_all(int reason, int send_update)
797 GList *contexts = _widget_app_get_contexts();
798 GList *iter = g_list_first(contexts);
800 __pause_all(send_update);
801 while (iter != NULL) {
802 widget_context_s *cxt = (widget_context_s *)iter->data;
803 iter = g_list_next(iter);
804 switch (cxt->state) {
806 LOGD("destroy %d : %s", cxt->state, cxt->id);
807 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
813 static Eina_Bool __visibility_cb(void *data, int type, void *event)
815 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
816 widget_context_s *cxt = __find_context_by_win(ev->win);
818 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
821 LOGE("unknown window error: %d", ev->win);
822 return ECORE_CALLBACK_RENEW;
825 if ((cxt->state == WC_READY || cxt->state == WC_PAUSED)
826 && ev->fully_obscured == 0) {
827 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
828 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
829 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
831 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
834 return ECORE_CALLBACK_RENEW;
837 /* LCOV_EXCL_START */
838 static Eina_Bool __lower_cb(void *data, int type, void *event)
841 return ECORE_CALLBACK_RENEW;
845 static Eina_Bool __configure_cb(void *data, int type, void *event)
847 Ecore_Wl_Event_Window_Configure *ev = event;
848 widget_context_s *cxt = __find_context_by_win(ev->win);
850 LOGD("configure: %d %d", ev->w, ev->h);
853 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
854 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
857 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
858 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
859 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
861 return ECORE_CALLBACK_RENEW;
864 static void __add_climsg()
866 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
867 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
868 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
871 static void __get_content(bundle *b)
873 char *instance_id = NULL;
874 widget_context_s *cxt = NULL;
876 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
880 cxt = __find_context_by_id(instance_id);
882 _E("can not find instance id:%s", instance_id);
887 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
888 _D("content info of %s found", cxt->id);
890 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
891 _D("empty content info added");
895 static int __aul_handler(aul_type type, bundle *b, void *data)
903 bundle_get_str(b, WIDGET_K_CALLER, &caller);
905 caller_pid = g_ascii_strtoll(caller, &remain,
908 /* using caller appid and query pid using caller appid? */
916 __resume_all(UPDATE_ALL);
921 case AUL_WIDGET_CONTENT:
931 static char *__get_domain_name(char *appid)
936 _E("appid is NULL"); /* LCOV_EXCL_LINE */
937 return NULL; /* LCOV_EXCL_LINE */
940 name_token = strrchr(appid, '.');
942 if (name_token == NULL) {
943 _E("appid is invalid"); /* LCOV_EXCL_LINE */
944 return appid; /* LCOV_EXCL_LINE */
952 /* LCOV_EXCL_START */
953 static void __on_poweroff(keynode_t *key, void *data)
957 val = vconf_keynode_get_int(key);
959 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
960 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
961 _I("power off changed: %d", val);
964 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
965 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
973 extern int _set_i18n(const char *name);
975 static int __before_loop(int argc, char **argv)
980 char *viewer_endpoint = NULL;
982 #if !(GLIB_CHECK_VERSION(2, 36, 0))
986 kb = bundle_import_from_argv(argc, argv);
988 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
989 if (viewer_endpoint) {
990 _E("viewer endpoint :%s", viewer_endpoint);
991 _widget_app_set_viewer_endpoint(viewer_endpoint);
993 _E("endpoint is missing");
999 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
1002 screen_connector_provider_init();
1003 elm_init(argc, argv);
1005 r = aul_launch_init(__aul_handler, NULL);
1007 /* LCOV_EXCL_START */
1008 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1010 "Fail to call the aul_launch_init");
1011 /* LCOV_EXCL_STOP */
1014 r = aul_launch_argv_handler(argc, argv);
1016 /* LCOV_EXCL_START */
1017 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1019 "Fail to call the aul_launch_argv_handler");
1020 /* LCOV_EXCL_STOP */
1023 r = app_get_id(&appid);
1024 if (r != APP_ERROR_NONE)
1027 name = __get_domain_name(appid);
1030 /* LCOV_EXCL_START */
1031 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1033 "Fail to call __get_domain_name");
1034 /* LCOV_EXCL_STOP */
1037 r = _set_i18n(name);
1040 /* LCOV_EXCL_START */
1041 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1043 "Fail to call _set_i18n");
1044 /* LCOV_EXCL_STOP */
1049 _widget_core_set_appcore_event_cb();
1051 class_provider = app_ops->create(app_user_data);
1052 if (class_provider == NULL) {
1053 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1054 __FUNCTION__, "widget_class is NULL");
1057 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
1059 return WIDGET_ERROR_NONE;
1062 static void __after_loop()
1065 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
1067 __pause_all(UPDATE_LOCAL);
1068 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1070 if (app_ops->terminate)
1071 app_ops->terminate(app_user_data);
1073 screen_connector_provider_fini();
1075 _widget_app_free_viewer_endpoint();
1076 _widget_core_unset_appcore_event_cb();
1077 __free_handler_list();
1091 /* Check loader case */
1092 if (getenv("AUL_LOADER_INIT")) {
1093 unsetenv("AUL_LOADER_INIT");
1099 static void __on_low_memory(keynode_t *key, void *data)
1103 val = vconf_keynode_get_int(key);
1104 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
1105 app_event_handler_h handler;
1106 struct app_event_info event;
1108 _I("widget_app_low_memory");
1110 event.type = APP_EVENT_LOW_MEMORY;
1111 event.value = (void *)&val;
1113 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
1116 handler = (app_event_handler_h) iter->data;
1117 handler->cb(&event, handler->data);
1118 iter = g_list_next(iter);
1123 static void __on_low_battery(keynode_t *key, void *data)
1127 val = vconf_keynode_get_int(key);
1128 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1129 app_event_handler_h handler;
1130 struct app_event_info event;
1132 _I("widget_app_low_battery");
1134 event.type = APP_EVENT_LOW_BATTERY;
1135 event.value = (void *)&val;
1137 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1140 handler = (app_event_handler_h) iter->data;
1141 handler->cb(&event, handler->data);
1142 iter = g_list_next(iter);
1147 static void __on_lang_changed(keynode_t *key, void *data)
1152 val = vconf_keynode_get_str(key);
1154 app_event_handler_h handler;
1155 struct app_event_info event;
1157 _I("widget_app_lang_changed");
1159 event.type = APP_EVENT_LANGUAGE_CHANGED;
1160 event.value = (void *)val;
1162 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1165 handler = (app_event_handler_h) iter->data;
1166 handler->cb(&event, handler->data);
1167 iter = g_list_next(iter);
1171 static void __on_region_changed(keynode_t *key, void *data)
1176 val = vconf_keynode_get_str(key);
1178 app_event_handler_h handler;
1179 struct app_event_info event;
1181 _I("widget_app_region_changed");
1183 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1184 event.value = (void *)val;
1186 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1189 handler = (app_event_handler_h) iter->data;
1190 handler->cb(&event, handler->data);
1191 iter = g_list_next(iter);
1195 static void __register_event(int event_type)
1197 switch (event_type) {
1198 case APP_EVENT_LOW_MEMORY:
1199 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1202 case APP_EVENT_LOW_BATTERY:
1203 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1206 case APP_EVENT_LANGUAGE_CHANGED:
1207 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1210 case APP_EVENT_REGION_FORMAT_CHANGED:
1211 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1216 static void __unregister_event(int event_type)
1218 switch (event_type) {
1219 case APP_EVENT_LOW_MEMORY:
1220 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1223 case APP_EVENT_LOW_BATTERY:
1224 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1227 case APP_EVENT_LANGUAGE_CHANGED:
1228 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1231 case APP_EVENT_REGION_FORMAT_CHANGED:
1232 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1237 static void _widget_core_set_appcore_event_cb(void)
1239 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1240 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1243 static void _widget_core_unset_appcore_event_cb(void)
1245 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1246 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1249 EXPORT_API int widget_app_main(int argc, char **argv,
1250 widget_app_lifecycle_callback_s *callback, void *user_data)
1254 if (!_is_widget_feature_enabled()) {
1255 _E("not supported"); /* LCOV_EXCL_LINE */
1256 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1259 if (argc <= 0 || argv == NULL || callback == NULL)
1260 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1261 __FUNCTION__, NULL);
1263 if (callback->create == NULL)
1264 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1266 "widget_app_create_cb() callback must be "
1269 app_user_data = user_data;
1270 r = __before_loop(argc, argv);
1280 aul_status_update(STATUS_DYING);
1283 return WIDGET_ERROR_NONE;
1286 EXPORT_API int widget_app_exit(void)
1288 if (!_is_widget_feature_enabled()) {
1289 _E("not supported"); /* LCOV_EXCL_LINE */
1290 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1294 return WIDGET_ERROR_NONE;
1299 if (!_widget_app_get_contexts() && is_permanent)
1302 return WIDGET_ERROR_NONE;
1305 /* LCOV_EXCL_START */
1306 static gboolean __finish_event_cb(gpointer user_data)
1308 if (user_data == NULL)
1311 widget_context_s *wc = (widget_context_s *)user_data;
1313 switch (wc->state) {
1315 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1317 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1319 __instance_destroy(wc->provider, wc->id,
1320 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1328 /* LCOV_EXCL_STOP */
1330 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1332 if (!_is_widget_feature_enabled()) {
1333 _E("not supported"); /* LCOV_EXCL_LINE */
1334 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1337 if (context == NULL)
1338 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1339 __FUNCTION__, NULL);
1341 g_idle_add(__finish_event_cb, context);
1342 return WIDGET_ERROR_NONE;
1345 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1347 GList *contexts = _widget_app_get_contexts();
1349 widget_context_s *wc;
1351 if (!_is_widget_feature_enabled()) {
1352 _E("not supported"); /* LCOV_EXCL_LINE */
1353 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1357 return WIDGET_ERROR_INVALID_PARAMETER;
1359 list = g_list_first(contexts);
1362 wc = (widget_context_s *)list->data;
1370 return WIDGET_ERROR_NONE;
1373 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1374 app_event_type_e event_type, app_event_cb callback,
1380 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1382 return WIDGET_ERROR_FAULT;
1385 return WIDGET_ERROR_NOT_SUPPORTED;
1387 app_event_handler_h handler;
1389 if (event_handler == NULL || callback == NULL)
1390 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1392 if (event_type < APP_EVENT_LOW_MEMORY
1393 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1394 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1396 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1397 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1399 GList *iter = g_list_first(handler_list[event_type]);
1402 handler = (app_event_handler_h) iter->data;
1404 if (handler->cb == callback)
1405 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1407 iter = g_list_next(iter);
1410 handler = calloc(1, sizeof(struct app_event_handler));
1412 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1414 if (g_list_length(handler_list[event_type]) == 0)
1415 __register_event(event_type);
1417 handler->type = event_type;
1418 handler->cb = callback;
1419 handler->data = user_data;
1420 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1422 *event_handler = handler;
1424 return WIDGET_ERROR_NONE;
1427 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1433 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1435 return WIDGET_ERROR_FAULT;
1438 return WIDGET_ERROR_NOT_SUPPORTED;
1440 app_event_type_e type;
1442 if (event_handler == NULL)
1443 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1445 type = event_handler->type;
1446 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1447 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1449 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1450 free(event_handler);
1452 if (g_list_length(handler_list[type]) == 0)
1453 __unregister_event(type);
1455 return WIDGET_ERROR_NONE;
1458 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1460 if (!_is_widget_feature_enabled()) {
1461 _E("not supported"); /* LCOV_EXCL_LINE */
1462 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1463 return NULL; /* LCOV_EXCL_LINE */
1467 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1471 set_last_result(WIDGET_ERROR_NONE);
1475 static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
1477 /* Remove data used in accessibility */
1479 plug_id = evas_object_data_del(obj, "___PLUGID");
1483 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1486 widget_context_s *cxt = (widget_context_s *)context;
1487 Evas_Object *ret_win;
1488 Ecore_Wl_Window *wl_win;
1489 struct wl_surface *surface;
1493 if (!_is_widget_feature_enabled()) {
1494 _E("not supported"); /* LCOV_EXCL_LINE */
1495 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1498 if (context == NULL || win == NULL)
1499 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1500 __FUNCTION__, NULL);
1502 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1503 if (ret_win == NULL) {
1504 _E("failed to create window"); /* LCOV_EXCL_LINE */
1505 goto fault; /* LCOV_EXCL_LINE */
1508 elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
1509 elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
1511 wl_win = elm_win_wl_window_get(ret_win);
1512 if (wl_win == NULL) {
1513 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1517 surface = ecore_wl_window_surface_get(wl_win);
1518 if (surface == NULL) {
1519 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1520 goto fault; /* LCOV_EXCL_LINE */
1522 screen_connector_provider_remote_enable(cxt->id, surface);
1524 ecore_wl_window_class_name_set(wl_win, cxt->id);
1525 elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1529 cxt->win_id = ecore_wl_window_id_get(wl_win);
1531 /* Set data to use in accessibility */
1532 snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
1533 evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
1534 evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
1536 _D("window created: %d", cxt->win_id);
1538 return WIDGET_ERROR_NONE;
1541 if (ret_win) /* LCOV_EXCL_LINE */
1542 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1544 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1548 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1549 widget_instance_lifecycle_callback_s callback, void *user_data)
1553 if (!_is_widget_feature_enabled()) {
1554 _E("not supported"); /* LCOV_EXCL_LINE */
1555 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1559 if (class_id == NULL) {
1560 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1564 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1566 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1567 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1568 return NULL; /* LCOV_EXCL_LINE */
1571 wc->classid = strdup(class_id);
1572 wc->user_data = user_data;
1577 set_last_result(WIDGET_ERROR_NONE);
1585 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1586 const char *class_id,
1587 widget_instance_lifecycle_callback_s callback, void *user_data)
1589 return _widget_class_create(widget_class, class_id, callback,
1593 EXPORT_API widget_class_h widget_app_class_create(
1594 widget_instance_lifecycle_callback_s callback, void *user_data)
1596 return _widget_class_create(class_provider, appid, callback, user_data);
1599 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1601 if (!_is_widget_feature_enabled()) {
1602 _E("not supported"); /* LCOV_EXCL_LINE */
1603 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1606 if (context == NULL)
1607 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1608 __FUNCTION__, NULL);
1612 return WIDGET_ERROR_NONE;
1615 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1617 if (!_is_widget_feature_enabled()) {
1618 _E("not supported"); /* LCOV_EXCL_LINE */
1619 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1622 if (context == NULL || tag == NULL)
1623 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1624 __FUNCTION__, NULL);
1626 *tag = context->tag;
1628 return WIDGET_ERROR_NONE;
1631 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1632 bundle *content_info)
1634 const char *class_id = NULL;
1636 bundle_raw *raw = NULL;
1639 if (!_is_widget_feature_enabled()) {
1640 _E("not supported"); /* LCOV_EXCL_LINE */
1641 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1644 if (context == NULL || content_info == NULL)
1645 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1646 __FUNCTION__, NULL);
1648 if (context->provider == NULL)
1649 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1650 __FUNCTION__, NULL);
1652 class_id = context->provider->classid;
1653 if (class_id == NULL)
1654 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1656 ret = __send_update_status(class_id, context->id,
1657 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1659 if (context->content)
1660 free(context->content);
1662 bundle_encode(content_info, &raw, &len);
1664 context->content = strdup((const char *)raw);
1666 context->content = NULL;
1670 /* LCOV_EXCL_START */
1671 _E("failed to send content info: %s of %s (%d)", context->id,
1673 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1675 /* LCOV_EXCL_STOP */
1678 return WIDGET_ERROR_NONE;
1681 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1684 if (!_is_widget_feature_enabled()) {
1685 _E("not supported"); /* LCOV_EXCL_LINE */
1686 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1689 if (!context || !title)
1690 return WIDGET_ERROR_INVALID_PARAMETER;
1693 elm_win_title_set(context->win, title);
1695 return WIDGET_ERROR_NONE;