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.
24 #include <glib-object.h>
26 #include <app_control.h>
27 #include <app_control_internal.h>
28 #include <Elementary.h>
29 #include <widget_errno.h>
30 #include <widget_instance.h>
31 #include <widget_service_internal.h>
33 #include "widget_app.h"
34 #include "widget-log.h"
35 #include "widget-private.h"
36 #include "widget_app_internal.h"
42 #define STR_MAX_BUF 128
43 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
44 #define K_REASON "__WC_K_REASON__"
46 typedef enum _widget_obj_state_e {
53 struct app_event_handler {
54 app_event_type_e type;
59 struct app_event_info {
60 app_event_type_e type;
64 struct _widget_class {
66 widget_instance_lifecycle_callback_s ops;
68 struct _widget_class *next;
69 struct _widget_class *prev;
72 struct _widget_context {
74 struct _widget_class *provider;
79 widget_instance_lifecycle_callback_s ops;
82 typedef struct _widget_class widget_class_s;
83 typedef struct _widget_context widget_context_s;
85 static int caller_pid = 0;
86 static widget_app_lifecycle_callback_s *app_ops;
87 static void *app_user_data = NULL;
88 static char *appid = NULL;
89 static widget_class_h class_provider = NULL;
90 static GList *contexts = NULL;
91 static char *viewer_endpoint = NULL;
93 static gint __comp_by_id(gconstpointer a, gconstpointer b)
95 widget_context_s *wc = (widget_context_s*)a;
97 return strcmp(wc->id, (const char*)b);
100 static widget_context_s* __find_context_by_id(const char *id)
102 GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
110 static int __send_update_status(const char *class_id, const char *instance_id,
111 int status, bundle *extra, int internal_only)
118 bundle_add_str(b, WIDGET_K_ID, class_id);
119 bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
120 bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
122 aul_app_com_send(viewer_endpoint, b);
128 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
130 widget_context_s *wc = NULL;
132 char *w_str = NULL, *h_str = NULL;
136 wc = (widget_context_s *)malloc(sizeof(widget_context_s));
138 return WIDGET_ERROR_OUT_OF_MEMORY;
140 wc->state = WC_READY;
141 wc->id = g_strdup(id);
142 wc->provider = handle;
145 wc->content = bundle_dup(b);
146 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
147 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
150 w = (int)g_ascii_strtoll(w_str, &remain, 10);
154 h = (int)g_ascii_strtoll(h_str, &remain, 10);
157 contexts = g_list_append(contexts, wc);
159 handle->ops.create(wc, b, w, h, handle->user_data);
161 ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
166 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
168 widget_context_s *wc = __find_context_by_id(id);
172 wc->state = WC_TERMINATED;
173 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
175 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
177 contexts = g_list_remove(contexts, wc);
183 _E("could not find widget obj: %s", id);
184 ret = WIDGET_ERROR_INVALID_PARAMETER;
190 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
192 if (!class_id || !handle)
195 widget_class_h head = handle;
198 if (head->classid && strcmp(head->classid, class_id) == 0)
207 static void __control(bundle *b)
209 char *class_id = NULL;
211 char *operation = NULL;
214 int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
216 widget_class_h handle = NULL;
217 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
218 /* for previous version compatibility, use appid for default class id */
219 if (class_id == NULL)
222 bundle_get_str(b, WIDGET_K_INSTANCE, &id);
223 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
225 handle = __find_class_handler(class_id, class_provider);
227 _E("no handle provided: %s", class_id);
232 _E("no operation provided");
236 if (strcmp(operation, "create") == 0) {
237 __instance_create(handle, id, b);
238 } else if (strcmp(operation, "resize") == 0) {
240 } else if (strcmp(operation, "update") == 0) {
242 } else if (strcmp(operation, "destroy") == 0) {
243 bundle_get_str(b, WIDGET_K_REASON, &reason);
245 destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
247 __instance_destroy(handle, id, destroy_type, b);
248 } else if (strcmp(operation, "resume") == 0) {
250 } else if (strcmp(operation, "pause") == 0) {
256 LOGD("error on control");
260 static void __show_all()
265 static int __aul_handler(aul_type type, bundle *b, void *data)
273 bundle_get_str(b, WIDGET_K_CALLER, &caller);
275 caller_pid = g_ascii_strtoll(caller, &remain, 10);
277 /* using caller appid and query pid using caller appid? */
298 static int __before_loop(int argc, char **argv)
302 char *wayland_display = NULL;
303 char *xdg_runtime_dir = NULL;
305 #if !(GLIB_CHECK_VERSION(2, 36, 0))
309 kb = bundle_import_from_argv(argc, argv);
311 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
312 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
313 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
316 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
319 setenv("WAYLAND_DISPLAY", wayland_display, 1);
324 _E("failed to get launch argv");
327 elm_init(argc, argv);
329 r = aul_launch_init(__aul_handler, NULL);
331 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
332 "Fail to call the aul_launch_init");
335 r = aul_launch_argv_handler(argc, argv);
337 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
338 "Fail to call the aul_launch_argv_handler");
341 r = app_get_id(&appid);
342 if (r != APP_ERROR_NONE)
345 class_provider = app_ops->create(app_user_data);
346 if (class_provider == NULL) {
347 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
348 "widget_class is NULL");
351 return WIDGET_ERROR_NONE;
354 static void __after_loop()
356 if (app_ops->terminate)
357 app_ops->terminate(app_user_data);
362 EXPORT_API int widget_app_main(int argc, char **argv,
363 widget_app_lifecycle_callback_s *callback, void *user_data)
367 if (argc <= 0 || argv == NULL || callback == NULL)
368 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
370 if (callback->create == NULL)
371 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
374 app_user_data = user_data;
375 r = __before_loop(argc, argv);
379 ecore_main_loop_begin();
380 aul_status_update(STATUS_DYING);
383 return WIDGET_ERROR_NONE;
386 EXPORT_API int widget_app_exit(void)
388 ecore_main_loop_quit();
390 return WIDGET_ERROR_NONE;
393 static gboolean __finish_event_cb(gpointer user_data)
395 if (user_data == NULL)
398 widget_context_s *wc = (widget_context_s *)user_data;
418 EXPORT_API int widget_app_terminate_context(widget_context_h context)
421 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
423 g_idle_add(__finish_event_cb, context);
424 return WIDGET_ERROR_NONE;
427 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
429 return WIDGET_ERROR_NONE;
432 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
433 app_event_type_e event_type, app_event_cb callback,
440 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
447 EXPORT_API const char* widget_app_get_id(widget_context_h context)
452 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
455 if (context == NULL || win == NULL)
456 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
458 widget_context_s *cxt = (widget_context_s*)context;
459 Evas_Object *ret_win = NULL;
461 ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
462 if (ret_win == NULL) {
463 _E("failed to create window");
464 return WIDGET_ERROR_FAULT;
466 elm_win_title_set(ret_win, cxt->id);
471 return WIDGET_ERROR_NONE;
474 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
475 widget_instance_lifecycle_callback_s callback, void *user_data)
477 widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
480 _E("failed to malloc : %s", __FUNCTION__);
481 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
485 wc->classid = strdup(class_id);
486 wc->user_data = user_data;
491 set_last_result(WIDGET_ERROR_NONE);
499 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
500 widget_instance_lifecycle_callback_s callback, void *user_data)
502 if (class_id == NULL) {
503 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
507 return _widget_class_create(widget_class, class_id, callback, user_data);
510 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
512 return _widget_class_create(class_provider, appid, callback, user_data);
515 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
518 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
522 return WIDGET_ERROR_NONE;
525 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
527 if (context == NULL || tag == NULL)
528 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
532 return WIDGET_ERROR_NONE;
535 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
537 const char *class_id = NULL;
539 if (context == NULL || content_info == NULL)
540 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
542 if (context->provider == NULL)
543 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
545 class_id = context->provider->classid;
547 if (class_id == NULL)
548 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
550 ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
553 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
554 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
557 return WIDGET_ERROR_NONE;
560 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
563 may use widget status update, or use surface title set.