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_internal.h>
33 #include <aul_app_com.h>
34 #include <Ecore_Wayland.h>
35 #include <system_info.h>
37 #include "widget_app.h"
38 #include "widget-log.h"
39 #include "widget-private.h"
40 #include "widget_app_internal.h"
46 #define STR_MAX_BUF 128
47 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
48 #define K_REASON "__WC_K_REASON__"
50 typedef enum _widget_obj_state_e {
57 struct app_event_handler {
58 app_event_type_e type;
63 struct app_event_info {
64 app_event_type_e type;
68 struct _widget_class {
70 widget_instance_lifecycle_callback_s ops;
72 struct _widget_class *next;
73 struct _widget_class *prev;
76 struct _widget_context {
78 struct _widget_class *provider;
83 widget_instance_lifecycle_callback_s ops;
86 typedef struct _widget_class widget_class_s;
87 typedef struct _widget_context widget_context_s;
89 static int caller_pid = 0;
90 static widget_app_lifecycle_callback_s *app_ops;
91 static void *app_user_data = NULL;
92 static char *appid = NULL;
93 static widget_class_h class_provider = NULL;
94 static GList *contexts = NULL;
95 static char *viewer_endpoint = NULL;
97 static inline bool _is_widget_feature_enabled(void)
99 static bool feature = false;
100 static bool retrieved = false;
103 if (retrieved == true)
106 ret = system_info_get_platform_bool(
107 "http://tizen.org/feature/shell.appwidget", &feature);
108 if (ret != SYSTEM_INFO_ERROR_NONE) {
109 _E("failed to get system info");
118 static gint __comp_by_id(gconstpointer a, gconstpointer b)
120 widget_context_s *wc = (widget_context_s *)a;
122 return strcmp(wc->id, (const char *)b);
125 static widget_context_s *__find_context_by_id(const char *id)
127 GList *ret = g_list_find_custom(contexts, id, __comp_by_id);
135 static int __send_update_status(const char *class_id, const char *instance_id,
136 int status, bundle *extra, int internal_only)
143 bundle_add_str(b, WIDGET_K_ID, class_id);
144 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
145 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
147 _E("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
148 aul_app_com_send(viewer_endpoint, b);
156 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
158 widget_context_s *wc = NULL;
160 char *w_str = NULL, *h_str = NULL;
164 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
166 return WIDGET_ERROR_OUT_OF_MEMORY;
168 wc->state = WC_READY;
169 wc->id = g_strdup(id);
170 wc->provider = handle;
173 wc->content = bundle_dup(b);
174 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
175 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
178 w = (int)g_ascii_strtoll(w_str, &remain, 10);
181 h = (int)g_ascii_strtoll(h_str, &remain, 10);
183 contexts = g_list_append(contexts, wc);
185 handle->ops.create(wc, b, w, h, handle->user_data);
186 ret = __send_update_status(handle->classid, wc->id,
187 WIDGET_INSTANCE_EVENT_CREATE, b, 0);
192 static int __instance_destroy(widget_class_h handle, const char *id,
193 widget_destroy_type_e reason, bundle *b)
195 widget_context_s *wc = __find_context_by_id(id);
199 wc->state = WC_TERMINATED;
200 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b,
203 ret = __send_update_status(handle->classid, id,
204 WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
206 contexts = g_list_remove(contexts, wc);
212 _E("could not find widget obj: %s", id);
213 ret = WIDGET_ERROR_INVALID_PARAMETER;
219 static widget_class_h __find_class_handler(const char *class_id,
220 widget_class_h handle)
222 if (!class_id || !handle)
225 widget_class_h head = handle;
228 if (head->classid && strcmp(head->classid, class_id) == 0)
237 static void __control(bundle *b)
239 char *class_id = NULL;
241 char *operation = NULL;
244 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
246 widget_class_h handle = NULL;
247 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
248 /* for previous version compatibility, use appid for default class id */
249 if (class_id == NULL)
252 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
253 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
255 handle = __find_class_handler(class_id, class_provider);
257 _E("no handle provided: %s", class_id);
262 _E("no operation provided");
266 if (strcmp(operation, "create") == 0) {
267 __instance_create(handle, id, b);
268 } else if (strcmp(operation, "resize") == 0) {
270 } else if (strcmp(operation, "update") == 0) {
272 } else if (strcmp(operation, "destroy") == 0) {
273 bundle_get_str(b, WIDGET_K_REASON, &reason);
275 destroy_type = (int)g_ascii_strtoll(reason, &remain,
278 __instance_destroy(handle, id, destroy_type, b);
279 } else if (strcmp(operation, "resume") == 0) {
281 } else if (strcmp(operation, "pause") == 0) {
287 LOGD("error on control");
291 static void __show_all()
296 static Eina_Bool __show_cb(void *data, int type, void *event)
298 Ecore_Wl_Event_Window_Show *ev = event;
299 LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
300 return ECORE_CALLBACK_RENEW;
303 static Eina_Bool __hide_cb(void *data, int type, void *event)
305 Ecore_Wl_Event_Window_Hide *ev = event;
306 LOGD("hide %d", (unsigned int)ev->win);
307 return ECORE_CALLBACK_RENEW;
310 static Eina_Bool __visibility_cb(void *data, int type, void *event)
312 Ecore_Wl_Event_Window_Visibility_Change *ev = event;
313 LOGD("visiblity change: %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
314 return ECORE_CALLBACK_RENEW;
317 static Eina_Bool __lower_cb(void *data, int type, void *event)
320 return ECORE_CALLBACK_RENEW;
323 static void __add_climsg()
325 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
326 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
327 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
328 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
331 static int __aul_handler(aul_type type, bundle *b, void *data)
339 bundle_get_str(b, WIDGET_K_CALLER, &caller);
341 caller_pid = g_ascii_strtoll(caller, &remain,
344 /* using caller appid and query pid using caller appid? */
364 static char *__get_domain_name(char *appid)
373 name_token = strrchr(appid, '.');
375 if (name_token == NULL) {
376 _E("appid is invalid");
385 extern int _set_i18n(const char *name);
387 static int __before_loop(int argc, char **argv)
391 char *wayland_display = NULL;
392 char *xdg_runtime_dir = NULL;
395 #if !(GLIB_CHECK_VERSION(2, 36, 0))
399 kb = bundle_import_from_argv(argc, argv);
401 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
402 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
403 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
404 if (viewer_endpoint) {
405 _E("viewer endpoint :%s", viewer_endpoint);
406 viewer_endpoint = strdup(viewer_endpoint);
408 _E("endpoint is missing");
412 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
415 setenv("WAYLAND_DISPLAY", wayland_display, 1);
420 _E("failed to get launch argv");
423 elm_init(argc, argv);
425 r = aul_launch_init(__aul_handler, NULL);
427 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
429 "Fail to call the aul_launch_init");
432 r = aul_launch_argv_handler(argc, argv);
434 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
436 "Fail to call the aul_launch_argv_handler");
439 r = app_get_id(&appid);
440 if (r != APP_ERROR_NONE)
443 name = __get_domain_name(appid);
446 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
448 "Fail to call __get_domain_name");
454 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
456 "Fail to call _set_i18n");
461 class_provider = app_ops->create(app_user_data);
462 if (class_provider == NULL) {
463 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
464 __FUNCTION__, "widget_class is NULL");
467 return WIDGET_ERROR_NONE;
470 static void __after_loop()
472 if (app_ops->terminate)
473 app_ops->terminate(app_user_data);
476 free(viewer_endpoint);
481 EXPORT_API int widget_app_main(int argc, char **argv,
482 widget_app_lifecycle_callback_s *callback, void *user_data)
486 if (!_is_widget_feature_enabled()) {
488 return WIDGET_ERROR_NOT_SUPPORTED;
491 if (argc <= 0 || argv == NULL || callback == NULL)
492 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
495 if (callback->create == NULL)
496 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
498 "widget_app_create_cb() callback must be "
502 app_user_data = user_data;
503 r = __before_loop(argc, argv);
507 ecore_main_loop_begin();
508 aul_status_update(STATUS_DYING);
511 return WIDGET_ERROR_NONE;
514 EXPORT_API int widget_app_exit(void)
516 if (!_is_widget_feature_enabled()) {
518 return WIDGET_ERROR_NOT_SUPPORTED;
521 ecore_main_loop_quit();
523 return WIDGET_ERROR_NONE;
526 static gboolean __finish_event_cb(gpointer user_data)
528 if (user_data == NULL)
531 widget_context_s *wc = (widget_context_s *)user_data;
551 EXPORT_API int widget_app_terminate_context(widget_context_h context)
553 if (!_is_widget_feature_enabled()) {
555 return WIDGET_ERROR_NOT_SUPPORTED;
559 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
562 g_idle_add(__finish_event_cb, context);
563 return WIDGET_ERROR_NONE;
566 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
569 widget_context_s *wc;
571 if (!_is_widget_feature_enabled()) {
573 return WIDGET_ERROR_NOT_SUPPORTED;
577 return WIDGET_ERROR_INVALID_PARAMETER;
579 list = g_list_first(contexts);
582 wc = (widget_context_s *)list->data;
590 return WIDGET_ERROR_NONE;
593 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
594 app_event_type_e event_type, app_event_cb callback,
597 if (!_is_widget_feature_enabled()) {
599 return WIDGET_ERROR_NOT_SUPPORTED;
603 if (!event_handler || !callback)
604 return WIDGET_ERROR_INVALID_PARAMETER;
606 switch (event_type) {
607 case APP_EVENT_LOW_MEMORY:
608 case APP_EVENT_LOW_BATTERY:
609 case APP_EVENT_LANGUAGE_CHANGED:
610 case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
611 case APP_EVENT_REGION_FORMAT_CHANGED:
612 case APP_EVENT_SUSPENDED_STATE_CHANGED:
616 return WIDGET_ERROR_INVALID_PARAMETER;
619 return WIDGET_ERROR_NONE;
622 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
625 if (!_is_widget_feature_enabled()) {
627 return WIDGET_ERROR_NOT_SUPPORTED;
632 return WIDGET_ERROR_INVALID_PARAMETER;
634 return WIDGET_ERROR_NONE;
637 EXPORT_API const char *widget_app_get_id(widget_context_h context)
639 if (!_is_widget_feature_enabled()) {
641 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
646 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
650 set_last_result(WIDGET_ERROR_NONE);
654 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
657 widget_context_s *cxt = (widget_context_s *)context;
658 Evas_Object *ret_win;
659 Ecore_Wl_Window *wl_win;
661 if (!_is_widget_feature_enabled()) {
663 return WIDGET_ERROR_NOT_SUPPORTED;
666 if (context == NULL || win == NULL)
667 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
670 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
671 if (ret_win == NULL) {
672 _E("failed to create window");
673 return WIDGET_ERROR_FAULT;
676 wl_win = elm_win_wl_window_get(ret_win);
677 if (wl_win == NULL) {
678 _E("failed to get wayland window");
679 evas_object_del(ret_win);
680 return WIDGET_ERROR_FAULT;
683 ecore_wl_window_class_name_set(wl_win, cxt->id);
688 return WIDGET_ERROR_NONE;
691 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
692 widget_instance_lifecycle_callback_s callback, void *user_data)
696 if (!_is_widget_feature_enabled()) {
698 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
702 if (class_id == NULL) {
703 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
707 wc = (widget_class_s *)malloc(sizeof(widget_class_s));
709 _E("failed to malloc : %s", __FUNCTION__);
710 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
714 wc->classid = strdup(class_id);
715 wc->user_data = user_data;
720 set_last_result(WIDGET_ERROR_NONE);
728 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
729 const char *class_id,
730 widget_instance_lifecycle_callback_s callback, void *user_data)
732 return _widget_class_create(widget_class, class_id, callback,
736 EXPORT_API widget_class_h widget_app_class_create(
737 widget_instance_lifecycle_callback_s callback, void *user_data)
739 return _widget_class_create(class_provider, appid, callback, user_data);
742 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
744 if (!_is_widget_feature_enabled()) {
746 return WIDGET_ERROR_NOT_SUPPORTED;
750 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
755 return WIDGET_ERROR_NONE;
758 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
760 if (!_is_widget_feature_enabled()) {
762 return WIDGET_ERROR_NOT_SUPPORTED;
765 if (context == NULL || tag == NULL)
766 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
771 return WIDGET_ERROR_NONE;
774 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
775 bundle *content_info)
777 const char *class_id = NULL;
780 if (!_is_widget_feature_enabled()) {
782 return WIDGET_ERROR_NOT_SUPPORTED;
785 if (context == NULL || content_info == NULL)
786 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
789 if (context->provider == NULL)
790 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
793 class_id = context->provider->classid;
795 if (class_id == NULL)
796 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
798 ret = __send_update_status(class_id, context->id,
799 WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
802 _E("failed to send content info: %s of %s (%d)", context->id,
804 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
808 return WIDGET_ERROR_NONE;
811 EXPORT_API int widget_app_context_set_title(widget_context_h context,
814 if (!_is_widget_feature_enabled()) {
816 return WIDGET_ERROR_NOT_SUPPORTED;
820 call elm_win_title_set()
823 if (!context || !title)
824 return WIDGET_ERROR_INVALID_PARAMETER;
826 return WIDGET_ERROR_NONE;