2 * Copyright (c) 2015 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 aul_app_com_send(viewer_endpoint, b);
155 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
157 widget_context_s *wc = NULL;
159 char *w_str = NULL, *h_str = NULL;
163 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
165 return WIDGET_ERROR_OUT_OF_MEMORY;
167 wc->state = WC_READY;
168 wc->id = g_strdup(id);
169 wc->provider = handle;
172 wc->content = bundle_dup(b);
173 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
174 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
177 w = (int)g_ascii_strtoll(w_str, &remain, 10);
180 h = (int)g_ascii_strtoll(h_str, &remain, 10);
182 contexts = g_list_append(contexts, wc);
184 handle->ops.create(wc, b, w, h, handle->user_data);
186 ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
191 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
193 widget_context_s *wc = __find_context_by_id(id);
197 wc->state = WC_TERMINATED;
198 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
200 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
202 contexts = g_list_remove(contexts, wc);
208 _E("could not find widget obj: %s", id);
209 ret = WIDGET_ERROR_INVALID_PARAMETER;
215 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
217 if (!class_id || !handle)
220 widget_class_h head = handle;
223 if (head->classid && strcmp(head->classid, class_id) == 0)
232 static void __control(bundle *b)
234 char *class_id = NULL;
236 char *operation = NULL;
239 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
241 widget_class_h handle = NULL;
242 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
243 /* for previous version compatibility, use appid for default class id */
244 if (class_id == NULL)
247 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
248 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
250 handle = __find_class_handler(class_id, class_provider);
252 _E("no handle provided: %s", class_id);
257 _E("no operation provided");
261 if (strcmp(operation, "create") == 0) {
262 __instance_create(handle, id, b);
263 } else if (strcmp(operation, "resize") == 0) {
265 } else if (strcmp(operation, "update") == 0) {
267 } else if (strcmp(operation, "destroy") == 0) {
268 bundle_get_str(b, WIDGET_K_REASON, &reason);
270 destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
272 __instance_destroy(handle, id, destroy_type, b);
273 } else if (strcmp(operation, "resume") == 0) {
275 } else if (strcmp(operation, "pause") == 0) {
281 LOGD("error on control");
285 static void __show_all()
290 static int __aul_handler(aul_type type, bundle *b, void *data)
298 bundle_get_str(b, WIDGET_K_CALLER, &caller);
300 caller_pid = g_ascii_strtoll(caller, &remain, 10);
302 /* using caller appid and query pid using caller appid? */
323 static int __before_loop(int argc, char **argv)
327 char *wayland_display = NULL;
328 char *xdg_runtime_dir = NULL;
330 #if !(GLIB_CHECK_VERSION(2, 36, 0))
334 kb = bundle_import_from_argv(argc, argv);
336 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
337 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
338 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
341 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
344 setenv("WAYLAND_DISPLAY", wayland_display, 1);
349 _E("failed to get launch argv");
352 elm_init(argc, argv);
354 r = aul_launch_init(__aul_handler, NULL);
356 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
357 "Fail to call the aul_launch_init");
360 r = aul_launch_argv_handler(argc, argv);
362 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
363 "Fail to call the aul_launch_argv_handler");
366 r = app_get_id(&appid);
367 if (r != APP_ERROR_NONE)
370 class_provider = app_ops->create(app_user_data);
371 if (class_provider == NULL) {
372 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
373 "widget_class is NULL");
376 return WIDGET_ERROR_NONE;
379 static void __after_loop()
381 if (app_ops->terminate)
382 app_ops->terminate(app_user_data);
387 EXPORT_API int widget_app_main(int argc, char **argv,
388 widget_app_lifecycle_callback_s *callback, void *user_data)
392 if (!_is_widget_feature_enabled()) {
394 return WIDGET_ERROR_NOT_SUPPORTED;
397 if (argc <= 0 || argv == NULL || callback == NULL)
398 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
400 if (callback->create == NULL)
401 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
404 app_user_data = user_data;
405 r = __before_loop(argc, argv);
409 ecore_main_loop_begin();
410 aul_status_update(STATUS_DYING);
413 return WIDGET_ERROR_NONE;
416 EXPORT_API int widget_app_exit(void)
418 if (!_is_widget_feature_enabled()) {
420 return WIDGET_ERROR_NOT_SUPPORTED;
423 ecore_main_loop_quit();
425 return WIDGET_ERROR_NONE;
428 static gboolean __finish_event_cb(gpointer user_data)
430 if (user_data == NULL)
433 widget_context_s *wc = (widget_context_s *)user_data;
453 EXPORT_API int widget_app_terminate_context(widget_context_h context)
455 if (!_is_widget_feature_enabled()) {
457 return WIDGET_ERROR_NOT_SUPPORTED;
461 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
463 g_idle_add(__finish_event_cb, context);
464 return WIDGET_ERROR_NONE;
467 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
469 if (!_is_widget_feature_enabled()) {
471 return WIDGET_ERROR_NOT_SUPPORTED;
475 return WIDGET_ERROR_INVALID_PARAMETER;
477 return WIDGET_ERROR_NONE;
480 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
481 app_event_type_e event_type, app_event_cb callback,
484 if (!_is_widget_feature_enabled()) {
486 return WIDGET_ERROR_NOT_SUPPORTED;
490 if (!event_handler || !callback)
491 return WIDGET_ERROR_INVALID_PARAMETER;
493 switch (event_type) {
494 case APP_EVENT_LOW_MEMORY:
495 case APP_EVENT_LOW_BATTERY:
496 case APP_EVENT_LANGUAGE_CHANGED:
497 case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
498 case APP_EVENT_REGION_FORMAT_CHANGED:
499 case APP_EVENT_SUSPENDED_STATE_CHANGED:
503 return WIDGET_ERROR_INVALID_PARAMETER;
506 return WIDGET_ERROR_NONE;
509 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
512 if (!_is_widget_feature_enabled()) {
514 return WIDGET_ERROR_NOT_SUPPORTED;
519 return WIDGET_ERROR_INVALID_PARAMETER;
521 return WIDGET_ERROR_NONE;
524 EXPORT_API const char* widget_app_get_id(widget_context_h context)
526 if (!_is_widget_feature_enabled()) {
528 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
533 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
540 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
543 widget_context_s *cxt = (widget_context_s*)context;
544 Evas_Object *ret_win;
545 Ecore_Wl_Window *wl_win;
547 if (!_is_widget_feature_enabled()) {
549 return WIDGET_ERROR_NOT_SUPPORTED;
552 if (context == NULL || win == NULL)
553 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
555 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
556 if (ret_win == NULL) {
557 _E("failed to create window");
558 return WIDGET_ERROR_FAULT;
561 wl_win = elm_win_wl_window_get(ret_win);
562 if (wl_win == NULL) {
563 _E("failed to get wayland window");
564 evas_object_del(ret_win);
565 return WIDGET_ERROR_FAULT;
568 ecore_wl_window_class_name_set(wl_win, cxt->id);
573 return WIDGET_ERROR_NONE;
576 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
577 widget_instance_lifecycle_callback_s callback, void *user_data)
581 if (!_is_widget_feature_enabled()) {
583 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
587 if (class_id == NULL) {
588 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
592 wc = (widget_class_s *)malloc(sizeof(widget_class_s));
594 _E("failed to malloc : %s", __FUNCTION__);
595 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
599 wc->classid = strdup(class_id);
600 wc->user_data = user_data;
605 set_last_result(WIDGET_ERROR_NONE);
613 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
614 widget_instance_lifecycle_callback_s callback, void *user_data)
616 return _widget_class_create(widget_class, class_id, callback, user_data);
619 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
621 return _widget_class_create(class_provider, appid, callback, user_data);
624 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
626 if (!_is_widget_feature_enabled()) {
628 return WIDGET_ERROR_NOT_SUPPORTED;
632 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
636 return WIDGET_ERROR_NONE;
639 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
641 if (!_is_widget_feature_enabled()) {
643 return WIDGET_ERROR_NOT_SUPPORTED;
646 if (context == NULL || tag == NULL)
647 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
651 return WIDGET_ERROR_NONE;
654 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
656 const char *class_id = NULL;
659 if (!_is_widget_feature_enabled()) {
661 return WIDGET_ERROR_NOT_SUPPORTED;
664 if (context == NULL || content_info == NULL)
665 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
667 if (context->provider == NULL)
668 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
670 class_id = context->provider->classid;
672 if (class_id == NULL)
673 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
675 ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
678 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
679 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
682 return WIDGET_ERROR_NONE;
685 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
687 if (!_is_widget_feature_enabled()) {
689 return WIDGET_ERROR_NOT_SUPPORTED;
693 call elm_win_title_set()
696 if (!context || !title)
697 return WIDGET_ERROR_INVALID_PARAMETER;
699 return WIDGET_ERROR_NONE;