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>
36 #include "widget_app.h"
37 #include "widget-log.h"
38 #include "widget-private.h"
39 #include "widget_app_internal.h"
45 #define STR_MAX_BUF 128
46 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
47 #define K_REASON "__WC_K_REASON__"
49 typedef enum _widget_obj_state_e {
56 struct app_event_handler {
57 app_event_type_e type;
62 struct app_event_info {
63 app_event_type_e type;
67 struct _widget_class {
69 widget_instance_lifecycle_callback_s ops;
71 struct _widget_class *next;
72 struct _widget_class *prev;
75 struct _widget_context {
77 struct _widget_class *provider;
82 widget_instance_lifecycle_callback_s ops;
85 typedef struct _widget_class widget_class_s;
86 typedef struct _widget_context widget_context_s;
88 static int caller_pid = 0;
89 static widget_app_lifecycle_callback_s *app_ops;
90 static void *app_user_data = NULL;
91 static char *appid = NULL;
92 static widget_class_h class_provider = NULL;
93 static GList *contexts = NULL;
94 static char *viewer_endpoint = NULL;
96 static gint __comp_by_id(gconstpointer a, gconstpointer b)
98 widget_context_s *wc = (widget_context_s*)a;
100 return strcmp(wc->id, (const char*)b);
103 static widget_context_s* __find_context_by_id(const char *id)
105 GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
113 static int __send_update_status(const char *class_id, const char *instance_id,
114 int status, bundle *extra, int internal_only)
121 bundle_add_str(b, WIDGET_K_ID, class_id);
122 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
123 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
125 aul_app_com_send(viewer_endpoint, b);
133 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
135 widget_context_s *wc = NULL;
137 char *w_str = NULL, *h_str = NULL;
141 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
143 return WIDGET_ERROR_OUT_OF_MEMORY;
145 wc->state = WC_READY;
146 wc->id = g_strdup(id);
147 wc->provider = handle;
150 wc->content = bundle_dup(b);
151 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
152 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
155 w = (int)g_ascii_strtoll(w_str, &remain, 10);
159 h = (int)g_ascii_strtoll(h_str, &remain, 10);
162 contexts = g_list_append(contexts, wc);
164 handle->ops.create(wc, b, w, h, handle->user_data);
166 ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
171 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
173 widget_context_s *wc = __find_context_by_id(id);
177 wc->state = WC_TERMINATED;
178 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
180 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
182 contexts = g_list_remove(contexts, wc);
188 _E("could not find widget obj: %s", id);
189 ret = WIDGET_ERROR_INVALID_PARAMETER;
195 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
197 if (!class_id || !handle)
200 widget_class_h head = handle;
203 if (head->classid && strcmp(head->classid, class_id) == 0)
212 static void __control(bundle *b)
214 char *class_id = NULL;
216 char *operation = NULL;
219 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
221 widget_class_h handle = NULL;
222 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
223 /* for previous version compatibility, use appid for default class id */
224 if (class_id == NULL)
227 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
228 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
230 handle = __find_class_handler(class_id, class_provider);
232 _E("no handle provided: %s", class_id);
237 _E("no operation provided");
241 if (strcmp(operation, "create") == 0) {
242 __instance_create(handle, id, b);
243 } else if (strcmp(operation, "resize") == 0) {
245 } else if (strcmp(operation, "update") == 0) {
247 } else if (strcmp(operation, "destroy") == 0) {
248 bundle_get_str(b, WIDGET_K_REASON, &reason);
250 destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
252 __instance_destroy(handle, id, destroy_type, b);
253 } else if (strcmp(operation, "resume") == 0) {
255 } else if (strcmp(operation, "pause") == 0) {
261 LOGD("error on control");
265 static void __show_all()
270 static int __aul_handler(aul_type type, bundle *b, void *data)
278 bundle_get_str(b, WIDGET_K_CALLER, &caller);
280 caller_pid = g_ascii_strtoll(caller, &remain, 10);
282 /* using caller appid and query pid using caller appid? */
303 static int __before_loop(int argc, char **argv)
307 char *wayland_display = NULL;
308 char *xdg_runtime_dir = NULL;
310 #if !(GLIB_CHECK_VERSION(2, 36, 0))
314 kb = bundle_import_from_argv(argc, argv);
316 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
317 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
318 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
321 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
324 setenv("WAYLAND_DISPLAY", wayland_display, 1);
329 _E("failed to get launch argv");
332 elm_init(argc, argv);
334 r = aul_launch_init(__aul_handler, NULL);
336 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
337 "Fail to call the aul_launch_init");
340 r = aul_launch_argv_handler(argc, argv);
342 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
343 "Fail to call the aul_launch_argv_handler");
346 r = app_get_id(&appid);
347 if (r != APP_ERROR_NONE)
350 class_provider = app_ops->create(app_user_data);
351 if (class_provider == NULL) {
352 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
353 "widget_class is NULL");
356 return WIDGET_ERROR_NONE;
359 static void __after_loop()
361 if (app_ops->terminate)
362 app_ops->terminate(app_user_data);
367 EXPORT_API int widget_app_main(int argc, char **argv,
368 widget_app_lifecycle_callback_s *callback, void *user_data)
372 if (argc <= 0 || argv == NULL || callback == NULL)
373 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
375 if (callback->create == NULL)
376 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
379 app_user_data = user_data;
380 r = __before_loop(argc, argv);
384 ecore_main_loop_begin();
385 aul_status_update(STATUS_DYING);
388 return WIDGET_ERROR_NONE;
391 EXPORT_API int widget_app_exit(void)
393 ecore_main_loop_quit();
395 return WIDGET_ERROR_NONE;
398 static gboolean __finish_event_cb(gpointer user_data)
400 if (user_data == NULL)
403 widget_context_s *wc = (widget_context_s *)user_data;
423 EXPORT_API int widget_app_terminate_context(widget_context_h context)
426 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
428 g_idle_add(__finish_event_cb, context);
429 return WIDGET_ERROR_NONE;
432 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
435 return WIDGET_ERROR_INVALID_PARAMETER;
437 return WIDGET_ERROR_NONE;
440 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
441 app_event_type_e event_type, app_event_cb callback,
445 if (!event_handler || !callback)
446 return WIDGET_ERROR_INVALID_PARAMETER;
448 switch (event_type) {
449 case APP_EVENT_LOW_MEMORY:
450 case APP_EVENT_LOW_BATTERY:
451 case APP_EVENT_LANGUAGE_CHANGED:
452 case APP_EVENT_DEVICE_ORIENTATION_CHANGED:
453 case APP_EVENT_REGION_FORMAT_CHANGED:
454 case APP_EVENT_SUSPENDED_STATE_CHANGED:
458 return WIDGET_ERROR_INVALID_PARAMETER;
461 return WIDGET_ERROR_NONE;
464 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
469 return WIDGET_ERROR_INVALID_PARAMETER;
471 return WIDGET_ERROR_NONE;
474 EXPORT_API const char* widget_app_get_id(widget_context_h context)
477 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
484 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
487 if (context == NULL || win == NULL)
488 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
490 widget_context_s *cxt = (widget_context_s*)context;
491 Evas_Object *ret_win;
492 Ecore_Wl_Window *wl_win;
494 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
495 if (ret_win == NULL) {
496 _E("failed to create window");
497 return WIDGET_ERROR_FAULT;
500 wl_win = elm_win_wl_window_get(ret_win);
501 if (wl_win == NULL) {
502 _E("failed to get wayland window");
503 evas_object_del(ret_win);
504 return WIDGET_ERROR_FAULT;
507 ecore_wl_window_class_name_set(wl_win, cxt->id);
512 return WIDGET_ERROR_NONE;
515 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
516 widget_instance_lifecycle_callback_s callback, void *user_data)
518 widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
521 _E("failed to malloc : %s", __FUNCTION__);
522 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
526 wc->classid = strdup(class_id);
527 wc->user_data = user_data;
532 set_last_result(WIDGET_ERROR_NONE);
540 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
541 widget_instance_lifecycle_callback_s callback, void *user_data)
543 if (class_id == NULL) {
544 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
548 return _widget_class_create(widget_class, class_id, callback, user_data);
551 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
553 return _widget_class_create(class_provider, appid, callback, user_data);
556 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
559 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
563 return WIDGET_ERROR_NONE;
566 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
568 if (context == NULL || tag == NULL)
569 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
573 return WIDGET_ERROR_NONE;
576 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
578 const char *class_id = NULL;
580 if (context == NULL || content_info == NULL)
581 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
583 if (context->provider == NULL)
584 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
586 class_id = context->provider->classid;
588 if (class_id == NULL)
589 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
591 ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
594 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
595 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
598 return WIDGET_ERROR_NONE;
601 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
604 call elm_win_title_set()
607 if (!context || !title)
608 return WIDGET_ERROR_INVALID_PARAMETER;
610 return WIDGET_ERROR_NONE;