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 <aul_widget.h>
37 #include <Ecore_Wayland.h>
38 #include <system_info.h>
40 #include <vconf-internal-keys.h>
41 #include <screen_connector_provider.h>
43 #include "widget_app.h"
44 #include "widget-log.h"
45 #include "widget-private.h"
46 #include "widget_app_internal.h"
52 #define STR_MAX_BUF 128
53 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
54 #define K_REASON "__WC_K_REASON__"
55 #define APP_TYPE_WIDGET "widgetapp"
56 #define STATUS_FOREGROUND "fg"
57 #define STATUS_BACKGROUND "bg"
59 typedef enum _widget_obj_state_e {
71 struct app_event_handler {
72 app_event_type_e type;
77 struct app_event_info {
78 app_event_type_e type;
82 typedef struct _widget_class widget_class_s;
84 #define WIDGET_APP_EVENT_MAX 5
85 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
87 static int caller_pid;
88 static widget_app_lifecycle_callback_s *app_ops;
89 static void *app_user_data;
91 static widget_class_h class_provider;
92 static int exit_called;
93 static char *package_id;
94 static bool fg_signal;
95 static bool is_permanent;
97 static void _widget_core_set_appcore_event_cb(void);
98 static void _widget_core_unset_appcore_event_cb(void);
99 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content);
101 static void __free_handler_cb(gpointer data)
107 static void __free_handler_list(void)
111 for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
112 g_list_free_full(handler_list[i], __free_handler_cb);
113 handler_list[i] = NULL;
117 static inline bool _is_widget_feature_enabled(void)
119 static bool feature = false;
120 static bool retrieved = false;
123 if (retrieved == true)
126 ret = system_info_get_platform_bool(
127 "http://tizen.org/feature/shell.appwidget", &feature);
128 if (ret != SYSTEM_INFO_ERROR_NONE) {
129 _E("failed to get system info"); /* LCOV_EXCL_LINE */
130 return false; /* LCOV_EXCL_LINE */
138 static gint __comp_by_id(gconstpointer a, gconstpointer b)
140 widget_context_s *wc = (widget_context_s *)a;
142 return strcmp(wc->id, (const char *)b);
145 static widget_context_s *__find_context_by_id(const char *id)
148 GList *contexts = _widget_app_get_contexts();
153 ret = g_list_find_custom(contexts, id, __comp_by_id);
160 static gint __comp_by_state(gconstpointer a, gconstpointer b)
162 widget_context_s *wc = (widget_context_s *)a;
164 if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
170 static widget_context_s *__find_context_by_state(widget_obj_state_e state)
173 GList *contexts = _widget_app_get_contexts();
175 ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
182 static gint __comp_by_win(gconstpointer a, gconstpointer b)
184 int win = GPOINTER_TO_INT(b);
185 widget_context_s *wc = (widget_context_s *)a;
187 return (wc && wc->win_id == win) ? 0 : -1;
190 static widget_context_s *__find_context_by_win(int win)
192 GList *contexts = _widget_app_get_contexts();
193 GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
201 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
204 bundle *b = bundle_create();
205 char pkgid[256] = {0, };
209 _E("out of memory"); /* LCOV_EXCL_LINE */
210 return -1; /* LCOV_EXCL_LINE */
213 if (package_id == NULL) {
214 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
216 package_id = strdup(pkgid);
219 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
220 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
221 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
223 bundle_add_str(b, AUL_K_PKGID, package_id);
225 _D("send lifecycle %s(%d)", instance_id, status);
226 ret = aul_app_com_send("widget.status", b);
228 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
235 static int __send_update_status(const char *class_id, const char *instance_id,
236 int status, int err, bundle *extra)
240 bundle_raw *raw = NULL;
242 char *viewer_endpoint = _widget_app_get_viewer_endpoint();
247 _E("out of memory"); /* LCOV_EXCL_LINE */
248 return -1; /* LCOV_EXCL_LINE */
252 snprintf(err_str, sizeof(err_str), "%d", err);
253 bundle_add_str(b, AUL_K_WIDGET_ERROR_CODE, err_str);
256 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
257 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
258 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
261 bundle_encode(extra, &raw, &len);
262 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
263 aul_widget_instance_add(class_id, instance_id);
266 _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
267 aul_app_com_send(viewer_endpoint, b);
270 case WIDGET_INSTANCE_EVENT_CREATE:
271 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
273 case WIDGET_INSTANCE_EVENT_DESTROY:
274 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
276 case WIDGET_INSTANCE_EVENT_PAUSE:
277 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
279 case WIDGET_INSTANCE_EVENT_RESUME:
280 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
285 __send_lifecycle_event(class_id, instance_id, lifecycle);
294 static gboolean __timeout_cb(gpointer user_data)
296 widget_context_s *wc = user_data;
298 if (wc->state == WC_RUNNING) {
299 _D("Periodic update!");
300 __instance_update(wc->provider, wc->id, 0, NULL);
301 } else if (wc->state == WC_PAUSED ||
302 wc->state == WC_READY) {
303 wc->pending_update = true;
304 if (wc->periodic_timer) {
306 g_source_remove(wc->periodic_timer);
307 wc->periodic_timer = 0;
311 return G_SOURCE_CONTINUE;
314 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
316 widget_context_s *wc = __find_context_by_id(id);
320 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
321 return -1; /* LCOV_EXCL_LINE */
324 if (wc->state == WC_RUNNING) {
325 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
326 return 0; /* LCOV_EXCL_LINE */
329 if (wc->state == WC_TERMINATED) {
330 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
331 return 0; /* LCOV_EXCL_LINE */
334 wc->state = WC_RUNNING;
335 if (wc->pending_update) {
336 _D("pending update!");
337 wc->pending_update = false;
338 __instance_update(wc->provider, wc->id, 0, wc->pending_content);
340 if (wc->period > 0) {
341 _D("Restart timer!");
342 wc->periodic_timer = g_timeout_add_seconds(wc->period,
347 if (handle->ops.resume)
348 handle->ops.resume(wc, handle->user_data);
350 _D("%s is resumed", id);
352 ret = __send_update_status(handle->classid, wc->id,
353 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
355 _D("Send fg signal to resourceD");
356 aul_send_app_status_change_signal(getpid(),
370 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
372 widget_context_s *wc = __find_context_by_id(id);
376 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
377 return -1; /* LCOV_EXCL_LINE */
380 if (wc->state == WC_PAUSED) {
381 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
382 return 0; /* LCOV_EXCL_LINE */
385 if (wc->state == WC_TERMINATED) {
386 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
387 return 0; /* LCOV_EXCL_LINE */
390 if (handle->ops.pause)
391 handle->ops.pause(wc, handle->user_data);
393 wc->state = WC_PAUSED;
394 _D("%s is paused", id);
396 ret = __send_update_status(handle->classid, wc->id,
397 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
398 wc = __find_context_by_state(WC_RUNNING);
399 if (!wc && fg_signal) {
400 _D("Send bg signal to resourceD");
401 aul_send_app_status_change_signal(getpid(),
415 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
417 widget_context_s *wc = __find_context_by_id(id);
421 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
422 return -1; /* LCOV_EXCL_LINE */
425 if (handle->ops.resize)
426 handle->ops.resize(wc, w, h, handle->user_data);
428 _D("%s is resized to %dx%d", id, w, h);
429 ret = __send_update_status(handle->classid, wc->id,
430 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
435 static void __update_pending_content(widget_context_s *wc, const char *content)
437 if (wc->pending_content) {
438 free(wc->pending_content);
439 wc->pending_content = NULL;
443 wc->pending_content = strdup(content);
444 if (wc->pending_content == NULL)
449 /* LCOV_EXCL_START */
450 static int __instance_update_all(widget_class_h handle, int force, const char *content)
452 widget_context_s *wc;
455 GList *context = _widget_app_get_contexts();
458 b = bundle_decode((const bundle_raw *)content, strlen(content));
460 if (handle->ops.update) {
462 wc = (widget_context_s *)context->data;
463 context = context->next;
464 if (wc->state != WC_RUNNING && !force) {
465 __update_pending_content(wc, content);
466 wc->pending_update = true;
470 handle->ops.update(wc, b, force, handle->user_data);
471 ret = __send_update_status(handle->classid, wc->id,
472 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
473 _D("updated:%s", wc->id);
484 /* LCOV_EXCL_START */
485 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
487 widget_context_s *wc = __find_context_by_id(id);
491 _E("context not found: %s", id);
495 if (wc->state != WC_RUNNING && !force) {
496 __update_pending_content(wc, content);
497 wc->pending_update = true;
502 b = bundle_decode((const bundle_raw *)content, strlen(content));
504 if (handle->ops.update) {
505 handle->ops.update(wc, b, force, handle->user_data);
506 ret = __send_update_status(handle->classid, wc->id,
507 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
508 _D("updated:%s", id);
518 static int __instance_create(widget_class_h handle, const char *id,
519 const char *content, int w, int h, double period)
521 widget_context_s *wc = NULL;
524 bundle *content_info = NULL;
526 wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
529 return WIDGET_ERROR_OUT_OF_MEMORY;
532 wc->state = WC_READY;
534 wc->provider = handle;
539 wc->content = strdup(content);
540 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
543 _widget_app_add_context(wc);
545 ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
547 _W("Create callback resturns error(%d)", ret);
548 send_ret = __send_update_status(handle->classid, wc->id,
549 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
551 _E("Fail to send abort status (%d)", send_ret);
553 _widget_app_remove_context(wc);
560 if (_widget_app_get_contexts() == NULL && !exit_called)
563 ret = __send_update_status(handle->classid, wc->id,
564 WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
569 aul_widget_instance_add(handle->classid, id);
573 wc->periodic_timer = g_timeout_add_seconds(period,
578 aul_widget_write_log(LOG_TAG, "[%s:%d] %s ret : %d",
579 __FUNCTION__, __LINE__, id, ret);
582 bundle_free(content_info);
587 static int __instance_destroy(widget_class_h handle, const char *id,
588 widget_app_destroy_type_e reason, int send_update)
590 widget_context_s *wc = __find_context_by_id(id);
592 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
593 bundle *content_info;
596 _E("could not find widget obj: %s, clear amd info", id); /* LCOV_EXCL_LINE */
597 aul_widget_instance_del(handle->classid, id); /* LCOV_EXCL_LINE */
598 return WIDGET_ERROR_NONE; /* LCOV_EXCL_LINE */
601 wc->state = WC_TERMINATED;
603 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
605 content_info = bundle_create();
607 handle->ops.destroy(wc, reason, content_info,
610 if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
612 event = WIDGET_INSTANCE_EVENT_DESTROY;
613 aul_widget_instance_del(handle->classid, id);
615 is_permanent = false;
616 ret = __send_update_status(handle->classid, id,
617 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
620 aul_widget_write_log(LOG_TAG, "[%s:%d] id : %s",
621 __FUNCTION__, __LINE__, id);
624 bundle_free(content_info);
626 ret = __send_update_status(handle->classid, id, event, 0, NULL);
628 _widget_app_remove_context(wc);
636 if (wc->periodic_timer)
637 g_source_remove(wc->periodic_timer);
639 if (wc->pending_content)
640 free(wc->pending_content);
644 if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
650 static int __instance_change_period(const char *id, double period)
652 widget_context_s *wc = __find_context_by_id(id);
655 _E("could not find widget obj: %s", id);
659 if (wc->periodic_timer) {
661 g_source_remove(wc->periodic_timer);
662 wc->periodic_timer = 0;
666 if (wc->period > 0) {
667 _D("Restart timer!");
668 wc->periodic_timer = g_timeout_add_seconds(wc->period,
675 static widget_class_h __find_class_handler(const char *class_id,
676 widget_class_h handle)
678 if (!class_id || !handle)
681 widget_class_h head = handle;
684 if (head->classid && strcmp(head->classid, class_id) == 0)
693 /* LCOV_EXCL_START */
694 static void __resize_window(char *id, int w, int h)
696 widget_context_s *wc = __find_context_by_id(id);
699 _E("can not find instance: %s", id);
704 evas_object_resize(wc->win, w, h);
706 _E("unable to find window of %d", wc->id);
710 static void __control(bundle *b)
712 char *class_id = NULL;
714 char *operation = NULL;
715 char *content = NULL;
722 char *force_str = NULL;
723 double *period = NULL;
724 double update_period = 0;
725 widget_class_h handle = NULL;
729 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
730 /* for previous version compatibility, use appid for default class id */
731 if (class_id == NULL)
734 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
735 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
737 handle = __find_class_handler(class_id, class_provider);
739 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
744 _E("no operation provided");
748 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
750 if (force_str && strcmp(force_str, "true") == 0)
755 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
756 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
757 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
759 w = (int)g_ascii_strtoll(w_str, &remain, 10);
762 h = (int)g_ascii_strtoll(h_str, &remain, 10);
764 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
765 if (ret == BUNDLE_ERROR_NONE)
766 update_period = *period;
768 if (strcmp(operation, "create") == 0) {
769 __instance_create(handle, id, content, w, h, update_period);
770 } else if (strcmp(operation, "resize") == 0) {
771 __resize_window(id, w, h);
772 __instance_resize(handle, id, w, h);
773 } else if (strcmp(operation, "update") == 0) {
775 __instance_update(handle, id, force, content);
777 __instance_update_all(handle, force, content);
779 } else if (strcmp(operation, "destroy") == 0) {
780 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
781 } else if (strcmp(operation, "resume") == 0) {
782 __instance_resume(handle, id, UPDATE_ALL);
783 } else if (strcmp(operation, "pause") == 0) {
784 __instance_pause(handle, id, UPDATE_ALL);
785 } else if (strcmp(operation, "terminate") == 0) {
786 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
787 } else if (strcmp(operation, "period") == 0) {
788 __instance_change_period(id, update_period);
793 LOGD("error on control");
797 static void __pause_all(int send_update)
799 GList *contexts = _widget_app_get_contexts();
800 GList *iter = g_list_first(contexts);
802 while (iter != NULL) {
803 widget_context_s *cxt = (widget_context_s *)iter->data;
805 switch (cxt->state) {
807 __instance_resume(cxt->provider, cxt->id, send_update);
808 __instance_pause(cxt->provider, cxt->id, send_update);
811 __instance_pause(cxt->provider, cxt->id, send_update);
814 LOGD("pause %s", cxt->id);
815 iter = g_list_next(iter);
819 /* LCOV_EXCL_START */
820 static void __resume_all(int send_update)
822 GList *contexts = _widget_app_get_contexts();
823 GList *iter = g_list_first(contexts);
825 while (iter != NULL) {
826 widget_context_s *cxt = (widget_context_s *)iter->data;
828 switch (cxt->state) {
830 __instance_resume(cxt->provider, cxt->id, send_update);
833 __instance_resume(cxt->provider, cxt->id, send_update);
836 iter = g_list_next(iter);
841 static void __destroy_all(int reason, int send_update)
843 GList *contexts = _widget_app_get_contexts();
844 GList *iter = g_list_first(contexts);
846 __pause_all(send_update);
847 while (iter != NULL) {
848 widget_context_s *cxt = (widget_context_s *)iter->data;
849 iter = g_list_next(iter);
850 switch (cxt->state) {
852 LOGD("destroy %d : %s", cxt->state, cxt->id);
853 __instance_destroy(cxt->provider, cxt->id, reason, send_update);
859 static Eina_Bool __visibility_cb(void *data, int type, void *event)
861 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
862 widget_context_s *cxt = __find_context_by_win(ev->win);
864 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
867 LOGE("unknown window error: %d", ev->win);
868 return ECORE_CALLBACK_RENEW;
871 if ((cxt->state == WC_READY || cxt->state == WC_PAUSED)
872 && ev->fully_obscured == 0) {
873 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
874 } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
875 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
877 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
880 return ECORE_CALLBACK_RENEW;
883 /* LCOV_EXCL_START */
884 static Eina_Bool __lower_cb(void *data, int type, void *event)
887 return ECORE_CALLBACK_RENEW;
891 static Eina_Bool __configure_cb(void *data, int type, void *event)
893 Ecore_Wl_Event_Window_Configure *ev = event;
894 widget_context_s *cxt = __find_context_by_win(ev->win);
896 LOGD("configure: %d %d", ev->w, ev->h);
899 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
900 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
903 if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
904 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
905 LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
907 return ECORE_CALLBACK_RENEW;
910 static void __add_climsg()
912 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
913 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
914 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
917 static void __get_content(bundle *b)
919 char *instance_id = NULL;
920 widget_context_s *cxt = NULL;
922 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
926 cxt = __find_context_by_id(instance_id);
928 _E("can not find instance id:%s", instance_id);
933 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
934 _D("content info of %s found", cxt->id);
936 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
937 _D("empty content info added");
941 static int __aul_handler(aul_type type, bundle *b, void *data)
949 bundle_get_str(b, WIDGET_K_CALLER, &caller);
951 caller_pid = g_ascii_strtoll(caller, &remain,
954 /* using caller appid and query pid using caller appid? */
962 __resume_all(UPDATE_ALL);
967 case AUL_WIDGET_CONTENT:
977 static char *__get_domain_name(char *appid)
982 _E("appid is NULL"); /* LCOV_EXCL_LINE */
983 return NULL; /* LCOV_EXCL_LINE */
986 name_token = strrchr(appid, '.');
988 if (name_token == NULL) {
989 _E("appid is invalid"); /* LCOV_EXCL_LINE */
990 return appid; /* LCOV_EXCL_LINE */
998 /* LCOV_EXCL_START */
999 static void __on_poweroff(keynode_t *key, void *data)
1003 val = vconf_keynode_get_int(key);
1005 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
1006 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
1007 _I("power off changed: %d", val);
1010 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
1011 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
1017 /* LCOV_EXCL_STOP */
1019 extern int _set_i18n(const char *name);
1020 extern void aul_finalize();
1022 static int __before_loop(int argc, char **argv)
1027 char *viewer_endpoint = NULL;
1029 #if !(GLIB_CHECK_VERSION(2, 36, 0))
1033 kb = bundle_import_from_argv(argc, argv);
1035 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
1036 if (viewer_endpoint) {
1037 _E("viewer endpoint :%s", viewer_endpoint);
1038 _widget_app_set_viewer_endpoint(viewer_endpoint);
1040 _E("endpoint is missing");
1046 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
1049 screen_connector_provider_init();
1050 elm_init(argc, argv);
1052 r = aul_launch_init(__aul_handler, NULL);
1054 /* LCOV_EXCL_START */
1055 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1057 "Fail to call the aul_launch_init");
1058 /* LCOV_EXCL_STOP */
1061 r = aul_launch_argv_handler(argc, argv);
1063 /* LCOV_EXCL_START */
1064 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1066 "Fail to call the aul_launch_argv_handler");
1067 /* LCOV_EXCL_STOP */
1070 r = app_get_id(&appid);
1071 if (r != APP_ERROR_NONE)
1074 name = __get_domain_name(appid);
1077 /* LCOV_EXCL_START */
1078 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1080 "Fail to call __get_domain_name");
1081 /* LCOV_EXCL_STOP */
1084 r = _set_i18n(name);
1087 /* LCOV_EXCL_START */
1088 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1090 "Fail to call _set_i18n");
1091 /* LCOV_EXCL_STOP */
1096 _widget_core_set_appcore_event_cb();
1098 class_provider = app_ops->create(app_user_data);
1099 if (class_provider == NULL) {
1100 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1101 __FUNCTION__, "widget_class is NULL");
1103 aul_widget_write_log(LOG_TAG, "[%s:%d]", __FUNCTION__, __LINE__);
1105 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
1107 return WIDGET_ERROR_NONE;
1110 static void __after_loop()
1113 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
1115 __pause_all(UPDATE_LOCAL);
1116 __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1118 if (app_ops->terminate)
1119 app_ops->terminate(app_user_data);
1120 aul_widget_write_log(LOG_TAG, "[%s:%d]", __FUNCTION__, __LINE__);
1123 screen_connector_provider_fini();
1125 _widget_app_free_viewer_endpoint();
1126 _widget_core_unset_appcore_event_cb();
1127 __free_handler_list();
1140 /* Check loader case */
1141 if (getenv("AUL_LOADER_INIT")) {
1142 unsetenv("AUL_LOADER_INIT");
1148 static void __on_low_memory(keynode_t *key, void *data)
1152 val = vconf_keynode_get_int(key);
1153 if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
1154 app_event_handler_h handler;
1155 struct app_event_info event;
1157 _I("widget_app_low_memory");
1159 event.type = APP_EVENT_LOW_MEMORY;
1160 event.value = (void *)&val;
1162 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
1165 handler = (app_event_handler_h) iter->data;
1166 handler->cb(&event, handler->data);
1167 iter = g_list_next(iter);
1172 static void __on_low_battery(keynode_t *key, void *data)
1176 val = vconf_keynode_get_int(key);
1177 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1178 app_event_handler_h handler;
1179 struct app_event_info event;
1181 _I("widget_app_low_battery");
1183 event.type = APP_EVENT_LOW_BATTERY;
1184 event.value = (void *)&val;
1186 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1189 handler = (app_event_handler_h) iter->data;
1190 handler->cb(&event, handler->data);
1191 iter = g_list_next(iter);
1196 static void __on_lang_changed(keynode_t *key, void *data)
1201 val = vconf_keynode_get_str(key);
1203 app_event_handler_h handler;
1204 struct app_event_info event;
1206 _I("widget_app_lang_changed");
1208 event.type = APP_EVENT_LANGUAGE_CHANGED;
1209 event.value = (void *)val;
1211 GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1214 handler = (app_event_handler_h) iter->data;
1215 handler->cb(&event, handler->data);
1216 iter = g_list_next(iter);
1220 static void __on_region_changed(keynode_t *key, void *data)
1225 val = vconf_keynode_get_str(key);
1227 app_event_handler_h handler;
1228 struct app_event_info event;
1230 _I("widget_app_region_changed");
1232 event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1233 event.value = (void *)val;
1235 GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1238 handler = (app_event_handler_h) iter->data;
1239 handler->cb(&event, handler->data);
1240 iter = g_list_next(iter);
1244 static void __register_event(int event_type)
1246 switch (event_type) {
1247 case APP_EVENT_LOW_MEMORY:
1248 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1251 case APP_EVENT_LOW_BATTERY:
1252 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1255 case APP_EVENT_LANGUAGE_CHANGED:
1256 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1259 case APP_EVENT_REGION_FORMAT_CHANGED:
1260 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1265 static void __unregister_event(int event_type)
1267 switch (event_type) {
1268 case APP_EVENT_LOW_MEMORY:
1269 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1272 case APP_EVENT_LOW_BATTERY:
1273 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1276 case APP_EVENT_LANGUAGE_CHANGED:
1277 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1280 case APP_EVENT_REGION_FORMAT_CHANGED:
1281 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1286 static void _widget_core_set_appcore_event_cb(void)
1288 __register_event(APP_EVENT_LANGUAGE_CHANGED);
1289 __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1292 static void _widget_core_unset_appcore_event_cb(void)
1294 __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1295 __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1298 EXPORT_API int widget_app_main(int argc, char **argv,
1299 widget_app_lifecycle_callback_s *callback, void *user_data)
1303 if (!_is_widget_feature_enabled()) {
1304 _E("not supported"); /* LCOV_EXCL_LINE */
1305 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1308 if (argc <= 0 || argv == NULL || callback == NULL)
1309 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1310 __FUNCTION__, NULL);
1312 if (callback->create == NULL)
1313 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1315 "widget_app_create_cb() callback must be "
1318 app_user_data = user_data;
1319 r = __before_loop(argc, argv);
1329 aul_status_update(STATUS_DYING);
1332 return WIDGET_ERROR_NONE;
1335 EXPORT_API int widget_app_exit(void)
1337 if (!_is_widget_feature_enabled()) {
1338 _E("not supported"); /* LCOV_EXCL_LINE */
1339 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1343 return WIDGET_ERROR_NONE;
1348 if (!_widget_app_get_contexts() && is_permanent)
1351 return WIDGET_ERROR_NONE;
1354 /* LCOV_EXCL_START */
1355 static gboolean __finish_event_cb(gpointer user_data)
1357 if (user_data == NULL)
1360 widget_context_s *wc = (widget_context_s *)user_data;
1362 switch (wc->state) {
1364 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1366 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1368 __instance_destroy(wc->provider, wc->id,
1369 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1377 /* LCOV_EXCL_STOP */
1379 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1381 if (!_is_widget_feature_enabled()) {
1382 _E("not supported"); /* LCOV_EXCL_LINE */
1383 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1386 if (context == NULL)
1387 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1388 __FUNCTION__, NULL);
1390 g_idle_add(__finish_event_cb, context);
1391 return WIDGET_ERROR_NONE;
1394 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1396 GList *contexts = _widget_app_get_contexts();
1398 widget_context_s *wc;
1400 if (!_is_widget_feature_enabled()) {
1401 _E("not supported"); /* LCOV_EXCL_LINE */
1402 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1406 return WIDGET_ERROR_INVALID_PARAMETER;
1408 list = g_list_first(contexts);
1411 wc = (widget_context_s *)list->data;
1419 return WIDGET_ERROR_NONE;
1422 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1423 app_event_type_e event_type, app_event_cb callback,
1429 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1431 return WIDGET_ERROR_FAULT;
1434 return WIDGET_ERROR_NOT_SUPPORTED;
1436 app_event_handler_h handler;
1438 if (event_handler == NULL || callback == NULL)
1439 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1441 if (event_type < APP_EVENT_LOW_MEMORY
1442 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1443 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1445 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1446 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1448 GList *iter = g_list_first(handler_list[event_type]);
1451 handler = (app_event_handler_h) iter->data;
1453 if (handler->cb == callback)
1454 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1456 iter = g_list_next(iter);
1459 handler = calloc(1, sizeof(struct app_event_handler));
1461 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1463 if (g_list_length(handler_list[event_type]) == 0)
1464 __register_event(event_type);
1466 handler->type = event_type;
1467 handler->cb = callback;
1468 handler->data = user_data;
1469 handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1471 *event_handler = handler;
1473 return WIDGET_ERROR_NONE;
1476 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1482 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1484 return WIDGET_ERROR_FAULT;
1487 return WIDGET_ERROR_NOT_SUPPORTED;
1489 app_event_type_e type;
1491 if (event_handler == NULL)
1492 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1494 type = event_handler->type;
1495 if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1496 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1498 handler_list[type] = g_list_remove(handler_list[type], event_handler);
1499 free(event_handler);
1501 if (g_list_length(handler_list[type]) == 0)
1502 __unregister_event(type);
1504 return WIDGET_ERROR_NONE;
1507 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1509 if (!_is_widget_feature_enabled()) {
1510 _E("not supported"); /* LCOV_EXCL_LINE */
1511 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1512 return NULL; /* LCOV_EXCL_LINE */
1516 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1520 set_last_result(WIDGET_ERROR_NONE);
1524 static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
1526 /* Remove data used in accessibility */
1528 plug_id = evas_object_data_del(obj, "___PLUGID");
1532 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1535 widget_context_s *cxt = (widget_context_s *)context;
1536 Evas_Object *ret_win;
1537 Ecore_Wl_Window *wl_win;
1538 struct wl_surface *surface;
1542 if (!_is_widget_feature_enabled()) {
1543 _E("not supported"); /* LCOV_EXCL_LINE */
1544 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1547 if (context == NULL || win == NULL)
1548 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1549 __FUNCTION__, NULL);
1551 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1552 if (ret_win == NULL) {
1553 _E("failed to create window"); /* LCOV_EXCL_LINE */
1554 goto fault; /* LCOV_EXCL_LINE */
1557 elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
1558 elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
1560 wl_win = elm_win_wl_window_get(ret_win);
1561 if (wl_win == NULL) {
1562 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1566 surface = ecore_wl_window_surface_get(wl_win);
1567 if (surface == NULL) {
1568 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1569 goto fault; /* LCOV_EXCL_LINE */
1571 screen_connector_provider_remote_enable(cxt->id, surface);
1573 ecore_wl_window_class_name_set(wl_win, cxt->id);
1574 elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1578 cxt->win_id = ecore_wl_window_id_get(wl_win);
1580 /* Set data to use in accessibility */
1581 snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
1582 evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
1583 evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
1585 _D("window created: %d", cxt->win_id);
1587 return WIDGET_ERROR_NONE;
1590 if (ret_win) /* LCOV_EXCL_LINE */
1591 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1593 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1597 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1598 widget_instance_lifecycle_callback_s callback, void *user_data)
1602 if (!_is_widget_feature_enabled()) {
1603 _E("not supported"); /* LCOV_EXCL_LINE */
1604 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1608 if (class_id == NULL) {
1609 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1613 wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1615 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1616 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1617 return NULL; /* LCOV_EXCL_LINE */
1620 wc->classid = strdup(class_id);
1621 wc->user_data = user_data;
1626 set_last_result(WIDGET_ERROR_NONE);
1634 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1635 const char *class_id,
1636 widget_instance_lifecycle_callback_s callback, void *user_data)
1638 return _widget_class_create(widget_class, class_id, callback,
1642 EXPORT_API widget_class_h widget_app_class_create(
1643 widget_instance_lifecycle_callback_s callback, void *user_data)
1645 return _widget_class_create(class_provider, appid, callback, user_data);
1648 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1650 if (!_is_widget_feature_enabled()) {
1651 _E("not supported"); /* LCOV_EXCL_LINE */
1652 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1655 if (context == NULL)
1656 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1657 __FUNCTION__, NULL);
1661 return WIDGET_ERROR_NONE;
1664 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1666 if (!_is_widget_feature_enabled()) {
1667 _E("not supported"); /* LCOV_EXCL_LINE */
1668 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1671 if (context == NULL || tag == NULL)
1672 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1673 __FUNCTION__, NULL);
1675 *tag = context->tag;
1677 return WIDGET_ERROR_NONE;
1680 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1681 bundle *content_info)
1683 const char *class_id = NULL;
1685 bundle_raw *raw = NULL;
1688 if (!_is_widget_feature_enabled()) {
1689 _E("not supported"); /* LCOV_EXCL_LINE */
1690 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1693 if (context == NULL || content_info == NULL)
1694 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1695 __FUNCTION__, NULL);
1697 if (context->provider == NULL)
1698 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1699 __FUNCTION__, NULL);
1701 class_id = context->provider->classid;
1702 if (class_id == NULL)
1703 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1705 ret = __send_update_status(class_id, context->id,
1706 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
1708 if (context->content)
1709 free(context->content);
1711 bundle_encode(content_info, &raw, &len);
1713 context->content = strdup((const char *)raw);
1715 context->content = NULL;
1719 /* LCOV_EXCL_START */
1720 _E("failed to send content info: %s of %s (%d)", context->id,
1722 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1724 /* LCOV_EXCL_STOP */
1727 return WIDGET_ERROR_NONE;
1730 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1733 if (!_is_widget_feature_enabled()) {
1734 _E("not supported"); /* LCOV_EXCL_LINE */
1735 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1738 if (!context || !title)
1739 return WIDGET_ERROR_INVALID_PARAMETER;
1742 elm_win_title_set(context->win, title);
1744 return WIDGET_ERROR_NONE;