2 * Copyright (c) 2017 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>
23 #include <aul_widget.h>
26 #include <glib-object.h>
29 #include <widget_errno.h>
30 #include <widget_instance.h>
31 #include <aul_app_com.h>
32 #include <Ecore_Wl2.h>
33 #include <system_info.h>
35 #include <vconf-internal-keys.h>
36 #include <screen_connector_provider.h>
37 #include <appcore_multiwindow_base.h>
39 #include "widget_base.h"
45 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
46 #define APP_TYPE_WIDGET "widgetapp"
47 #define STATUS_FOREGROUND "fg"
48 #define STATUS_BACKGROUND "bg"
50 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
51 [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
52 [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
53 [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
54 [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
55 = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
56 [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
57 [APP_EVENT_SUSPENDED_STATE_CHANGED]
58 = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
61 struct app_event_info {
62 app_event_type_e type;
66 struct app_event_handler {
67 app_event_type_e type;
73 struct widget_foreach_context {
74 widget_base_instance_cb callback;
78 typedef struct _widget_base_context {
84 } widget_base_context;
86 typedef struct _widget_base_instance_data {
94 char *pending_content;
96 } widget_base_instance_data;
98 static widget_base_context __context;
100 static char *__package_id;
101 static bool __fg_signal;
102 static char *__viewer_endpoint;
103 static bool __is_permanent;
104 static void __call_update_cb(const char *class_id, const char *id, int force,
105 const char *content_raw);
107 static gboolean __timeout_cb(gpointer user_data)
109 widget_base_instance_data *data =
110 (widget_base_instance_data *)user_data;
111 appcore_multiwindow_base_instance_h cxt;
112 const char *class_id;
114 cxt = appcore_multiwindow_base_instance_find(data->id);
117 LOGE("Can't find the instance");
118 return G_SOURCE_REMOVE;
121 if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
122 LOGD("Periodic update!");
123 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
124 __call_update_cb(class_id, data->id, 0, NULL);
126 data->pending_update = true;
127 if (data->periodic_timer) {
128 LOGD("Remove timer!");
129 g_source_remove(data->periodic_timer);
130 data->periodic_timer = 0;
134 return G_SOURCE_CONTINUE;
137 static bool __is_widget_feature_enabled(void)
139 static bool feature = false;
140 static bool retrieved = false;
143 if (retrieved == true)
146 ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
147 if (ret != SYSTEM_INFO_ERROR_NONE) {
148 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
149 return false; /* LCOV_EXCL_LINE */
157 /* LCOV_EXCL_START */
158 static void __on_poweroff(keynode_t *key, void *data)
162 val = vconf_keynode_get_int(key);
164 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
165 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
166 LOGI("power off changed: %d", val);
169 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
170 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
178 static void __check_empty_instance(void)
180 int cnt = appcore_multiwindow_base_instance_get_cnt();
186 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
188 widget_base_instance_data *data;
190 data = appcore_multiwindow_base_instance_get_extra(instance_h);
191 appcore_multiwindow_base_instance_drop(instance_h);
192 free(data->pending_content);
196 __check_empty_instance();
199 static gint __comp_class(gconstpointer a, gconstpointer b)
201 const widget_base_class *cls = a;
203 return strcmp(cls->id, b);
206 static widget_base_class *__get_class(const char *class_id)
208 widget_base_class *cls;
211 class_node = g_list_find_custom(__context.classes, class_id,
213 if (class_node == NULL) {
214 LOGE("empty classes");
217 cls = (widget_base_class *)class_node->data;
222 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
225 bundle *b = bundle_create();
229 LOGE("out of memory"); /* LCOV_EXCL_LINE */
230 return -1; /* LCOV_EXCL_LINE */
233 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
234 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
235 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
236 bundle_add_str(b, AUL_K_PKGID, __package_id);
238 LOGD("send lifecycle %s(%d)", instance_id, status);
239 ret = aul_app_com_send("widget.status", b);
241 LOGE("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
248 static int __send_update_status(const char *class_id, const char *instance_id,
249 int status, int err, bundle *extra)
253 bundle_raw *raw = NULL;
259 LOGE("out of memory"); /* LCOV_EXCL_LINE */
260 return -1; /* LCOV_EXCL_LINE */
264 snprintf(err_str, sizeof(err_str), "%d", err);
265 bundle_add_str(b, AUL_K_WIDGET_ERROR_CODE, err_str);
268 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
269 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
270 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
273 bundle_encode(extra, &raw, &len);
274 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
275 aul_widget_instance_add(class_id, instance_id);
278 LOGD("send update %s(%d) to %s", instance_id, status, __viewer_endpoint);
279 aul_app_com_send(__viewer_endpoint, b);
282 case WIDGET_INSTANCE_EVENT_CREATE:
283 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
285 case WIDGET_INSTANCE_EVENT_DESTROY:
286 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
288 case WIDGET_INSTANCE_EVENT_PAUSE:
289 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
291 case WIDGET_INSTANCE_EVENT_RESUME:
292 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
297 __send_lifecycle_event(class_id, instance_id, lifecycle);
306 static void __control_create(const char *class_id, const char *id, bundle *b)
308 widget_base_instance_data *data;
309 char *content = NULL;
311 if (appcore_multiwindow_base_instance_find(id)) {
312 LOGE("Already exist id (%s)", id);
316 data = (widget_base_instance_data *)
317 calloc(1, sizeof(widget_base_instance_data));
319 LOGE("Out of memory");
323 data->id = strdup(id);
326 /* call stub create */
327 appcore_multiwindow_base_instance_run(class_id, id, data);
329 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
331 data->content = strdup(content);
335 static void __control_resume(const char *class_id, const char *id, bundle *b)
337 appcore_multiwindow_base_instance_h cxt;
339 cxt = appcore_multiwindow_base_instance_find(id);
341 LOGE("context not found: %s", id);
345 /* call stub resume */
346 appcore_multiwindow_base_instance_resume(cxt);
349 static void __control_pause(const char *class_id, const char *id, bundle *b)
351 appcore_multiwindow_base_instance_h instance_h;
353 instance_h = appcore_multiwindow_base_instance_find(id);
356 LOGE("instance not found: %s", id);
360 /* call stub pause */
361 appcore_multiwindow_base_instance_pause(instance_h);
364 static void __control_resize(const char *class_id, const char *id, bundle *b)
366 appcore_multiwindow_base_instance_h instance_h;
373 widget_base_class *cls;
374 const appcore_multiwindow_base_class *raw_cls;
376 instance_h = appcore_multiwindow_base_instance_find(id);
378 LOGE("context not found: %s", id);
382 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
386 cls = __get_class(class_id);
388 LOGE("class not found: %s", class_id);
391 class_data = raw_cls->data;
392 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
393 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
396 w = (int)g_ascii_strtoll(w_str, &remain, 10);
399 h = (int)g_ascii_strtoll(h_str, &remain, 10);
402 cls->ops.resize(instance_h, w, h, class_data);
404 LOGD("%s is resized to %dx%d", id, w, h);
405 __send_update_status(class_id, id,
406 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
409 static void __call_update_cb(const char *class_id, const char *id, int force,
410 const char *content_raw)
413 widget_base_class *cls;
414 const appcore_multiwindow_base_class *raw_cls;
415 appcore_multiwindow_base_instance_h instance_h;
416 bundle *content = NULL;
418 instance_h = appcore_multiwindow_base_instance_find(id);
420 LOGE("context not found: %s", id);
424 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
426 LOGE("class is NULL");
430 class_data = raw_cls->data;
431 cls = __get_class(class_id);
433 LOGE("class not found: %s", class_id);
437 if (!cls->ops.update) {
438 LOGE("update callback is NULL");
443 content = bundle_decode((const bundle_raw *)content_raw,
444 strlen(content_raw));
448 cls->ops.update(instance_h, content, force, class_data);
450 __send_update_status(class_id, id,
451 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
452 LOGD("updated:%s", id);
455 bundle_free(content);
458 static void __update_pending_content(
459 appcore_multiwindow_base_instance_h instance_h,
460 const char *content_raw)
462 widget_base_instance_data *data;
464 data = (widget_base_instance_data *)
465 appcore_multiwindow_base_instance_get_extra(instance_h);
467 if (data->pending_content) {
468 free(data->pending_content);
469 data->pending_content = NULL;
473 data->pending_content = strdup(content_raw);
474 if (data->pending_content == NULL)
475 LOGW("Out of memory");
478 data->pending_update = true;
481 static void __update_process(const char *class_id, const char *id,
482 appcore_multiwindow_base_instance_h instance_h, void *data)
484 char *content_raw = NULL;
485 char *force_str = NULL;
490 LOGE("bundle is NULL");
494 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
496 if (force_str && strcmp(force_str, "true") == 0)
501 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
502 if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
503 __update_pending_content(instance_h, content_raw);
505 __call_update_cb(class_id, id, force, content_raw);
508 static void __control_update(const char *class_id, const char *id, bundle *b)
510 appcore_multiwindow_base_instance_h instance_h;
513 appcore_multiwindow_base_instance_foreach(class_id,
514 __update_process, b);
518 instance_h = appcore_multiwindow_base_instance_find(id);
520 LOGE("context not found: %s", id);
524 __update_process(class_id, id, instance_h, b);
527 static void __control_destroy(const char *class_id, const char *id, bundle *b)
529 appcore_multiwindow_base_instance_h instance_h;
530 widget_base_instance_data *data;
532 instance_h = appcore_multiwindow_base_instance_find(id);
534 LOGE("could not find widget obj: %s, clear amd info", id);
535 aul_widget_instance_del(class_id, id);
539 data = (widget_base_instance_data *)
540 appcore_multiwindow_base_instance_get_extra(instance_h);
543 /* call stub terminate */
544 appcore_multiwindow_base_instance_exit(instance_h);
545 free(data->pending_content);
549 __check_empty_instance();
552 static void __control_change_period(const char *class_id, const char *id,
555 appcore_multiwindow_base_instance_h instance_h;
556 widget_base_instance_data *data;
557 double *period = NULL;
561 instance_h = appcore_multiwindow_base_instance_find(id);
563 LOGE("context not found: %s", id);
567 data = (widget_base_instance_data *)
568 appcore_multiwindow_base_instance_get_extra(instance_h);
571 LOGE("could not find instance data: %s", id);
575 if (data->periodic_timer) {
576 LOGD("Remove timer!");
577 g_source_remove(data->periodic_timer);
578 data->periodic_timer = 0;
581 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
582 if (ret == BUNDLE_ERROR_NONE)
583 data->period = *period;
585 if (data->period > 0) {
586 LOGD("Restart timer!");
587 data->periodic_timer = g_timeout_add_seconds(data->period,
594 static int __multiwindow_create(void *data)
596 char pkgid[256] = {0, };
599 appcore_multiwindow_base_on_create();
600 app_get_id(&__appid);
601 if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
602 __package_id = strdup(pkgid);
604 if (!__package_id || !__appid) {
605 LOGE("__package_id is NULL");
609 screen_connector_provider_init();
610 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
611 __on_poweroff, NULL);
614 if (__context.ops.create)
615 ret = __context.ops.create(data);
617 LOGD("widget base is created");
621 static int __multiwindow_terminate(void *data)
623 if (__context.ops.terminate)
624 __context.ops.terminate(data);
626 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
628 screen_connector_provider_fini();
630 if (__viewer_endpoint) {
631 free(__viewer_endpoint);
632 __viewer_endpoint = NULL;
645 appcore_multiwindow_base_on_terminate();
647 LOGD("widget base is terminated");
651 static int __multiwindow_control(bundle *b, void *data)
653 char *class_id = NULL;
655 char *operation = NULL;
657 appcore_multiwindow_base_on_control(b);
658 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
659 /* for previous version compatibility, use appid for default class id */
660 if (class_id == NULL)
663 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
664 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
667 LOGE("operation is NULL");
671 if (strcmp(operation, "create") == 0)
672 __control_create(class_id, id, b);
673 else if (strcmp(operation, "resize") == 0)
674 __control_resize(class_id, id, b);
675 else if (strcmp(operation, "update") == 0)
676 __control_update(class_id, id, b);
677 else if (strcmp(operation, "destroy") == 0)
678 __control_destroy(class_id, id, b);
679 else if (strcmp(operation, "resume") == 0)
680 __control_resume(class_id, id, b);
681 else if (strcmp(operation, "pause") == 0)
682 __control_pause(class_id, id, b);
683 else if (strcmp(operation, "terminate") == 0)
684 __control_destroy(class_id, id, b);
685 else if (strcmp(operation, "period") == 0)
686 __control_change_period(class_id, id, b);
691 static void __inst_resume_cb(const char *class_id, const char *id,
692 appcore_multiwindow_base_instance_h cxt, void *data)
694 __control_resume(class_id, id, data);
697 static void __get_content(bundle *b)
699 char *instance_id = NULL;
700 appcore_multiwindow_base_instance_h cxt;
701 widget_base_instance_data * we;
703 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
705 LOGE("instance id is NULL");
709 cxt = appcore_multiwindow_base_instance_find(instance_id);
711 LOGE("could not find widget obj: %s", instance_id);
715 we = appcore_multiwindow_base_instance_get_extra(cxt);
717 LOGE("widget extra is NULL");
722 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
723 LOGD("content info of %s found", instance_id);
725 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
726 LOGD("empty content info added");
730 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
732 appcore_multiwindow_base_on_receive(type, b);
736 appcore_multiwindow_base_instance_foreach_full(
737 __inst_resume_cb, b);
742 case AUL_WIDGET_CONTENT:
752 static void __multiwindow_init(int argc, char **argv, void *data)
754 if (__context.ops.init)
755 __context.ops.init(argc, argv, data);
758 static void __multiwindow_finish(void)
760 if (__context.ops.finish) {
761 __context.ops.finish();
762 /* Check Loader case */
763 if (getenv("AUL_LOADER_INIT")) {
764 unsetenv("AUL_LOADER_INIT");
765 __context.ops.finish();
770 static void __multiwindow_run(void *data)
772 if (__context.ops.run)
773 __context.ops.run(data);
776 static void __multiwindow_exit(void *data)
778 if (__context.ops.exit)
779 __context.ops.exit(data);
782 static void __multiwindow_trim_memory(void *data)
784 if (__context.ops.trim_memory)
785 __context.ops.trim_memory(data);
788 EXPORT_API int widget_base_exit(void)
792 appcore_multiwindow_base_exit();
793 if (appcore_multiwindow_base_instance_get_cnt() == 0 && __is_permanent) {
794 ret = aul_notify_exit();
795 aul_widget_write_log(LOG_TAG, "[%s:%d] permanent exit : %d",
796 __FUNCTION__, __LINE__, ret);
802 static gboolean __finish_event_cb(gpointer user_data)
804 appcore_multiwindow_base_instance_h cxt = user_data;
807 const char *class_id;
810 LOGE("user_data is NULL");
814 id = appcore_multiwindow_base_instance_get_id(cxt);
815 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
819 LOGE("Out-of-memory");
823 bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
824 __control_destroy(class_id, id, b);
830 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
832 if (!__is_widget_feature_enabled()) {
833 LOGE("not supported"); /* LCOV_EXCL_LINE */
834 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
838 LOGE("context is null");
839 return WIDGET_ERROR_INVALID_PARAMETER;
842 g_idle_add(__finish_event_cb, context);
844 return WIDGET_ERROR_NONE;
847 static void __inst_full_cb(const char *class_id, const char *id,
848 appcore_multiwindow_base_instance_h cxt, void *data)
850 struct widget_foreach_context *foreach_context = data;
855 if (foreach_context->callback)
856 foreach_context->callback(cxt, foreach_context->data);
859 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
861 struct widget_foreach_context foreach_context;
863 if (!__is_widget_feature_enabled()) {
864 LOGE("not supported"); /* LCOV_EXCL_LINE */
865 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
869 LOGE("callback is NULL");
870 return WIDGET_ERROR_INVALID_PARAMETER;
873 foreach_context.callback = cb;
874 foreach_context.data = data;
875 appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
877 return WIDGET_ERROR_NONE;
880 static int __event_cb(void *event, void *data)
882 app_event_handler_h handler = data;
884 struct app_event_info app_event;
886 app_event.type = handler->type;
887 app_event.value = event;
890 handler->cb(&app_event, handler->data);
895 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
896 app_event_type_e event_type,
897 app_event_cb callback,
902 app_event_handler_h handler;
904 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
906 return WIDGET_BASE_ERROR_FAULT;
909 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
911 if (event_handler == NULL || callback == NULL)
912 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
914 if (event_type < APP_EVENT_LOW_MEMORY
915 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
916 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
918 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
919 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
922 handler = calloc(1, sizeof(struct app_event_handler));
924 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
926 handler->type = event_type;
927 handler->cb = callback;
928 handler->data = user_data;
929 handler->raw = appcore_base_add_event(
930 __app_event_converter[event_type], __event_cb, handler);
931 *event_handler = handler;
933 return WIDGET_BASE_ERROR_NONE;
936 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
941 app_event_type_e type;
943 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
945 return WIDGET_BASE_ERROR_FAULT;
948 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
950 if (event_handler == NULL)
951 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
953 type = event_handler->type;
954 if (type < APP_EVENT_LOW_MEMORY ||
955 type > APP_EVENT_REGION_FORMAT_CHANGED)
956 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
958 r = appcore_base_remove_event(event_handler->raw);
960 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
964 return WIDGET_BASE_ERROR_NONE;
967 EXPORT_API int widget_base_context_set_content_info(
968 widget_base_instance_h context,
969 bundle *content_info)
972 bundle_raw *raw = NULL;
975 const char *class_id;
976 widget_base_instance_data *data;
977 appcore_multiwindow_base_instance_h instance_h;
979 if (!__is_widget_feature_enabled()) {
980 LOGE("not supported"); /* LCOV_EXCL_LINE */
981 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
984 if (!context || !content_info)
985 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
987 instance_h = (appcore_multiwindow_base_instance_h)context;
988 id = appcore_multiwindow_base_instance_get_id(instance_h);
989 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
990 data = appcore_multiwindow_base_instance_get_extra(instance_h);
992 if (!class_id || !id || !data)
993 return WIDGET_BASE_ERROR_FAULT;
995 ret = __send_update_status(class_id, id,
996 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
1001 bundle_encode(content_info, &raw, &len);
1003 data->content = strdup((const char *)raw);
1005 data->content = NULL;
1009 /* LCOV_EXCL_START */
1010 LOGE("failed to send content info: %s of %s (%d)", id,
1012 return WIDGET_BASE_ERROR_IO_ERROR;
1013 /* LCOV_EXCL_STOP */
1016 return WIDGET_BASE_ERROR_NONE;
1019 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
1021 appcore_multiwindow_base_instance_h instance_h;
1022 widget_base_instance_data *data;
1024 if (!__is_widget_feature_enabled()) {
1025 LOGE("not supported"); /* LCOV_EXCL_LINE */
1026 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1029 if (!context || !tag) {
1030 LOGE("Invalid parameter");
1031 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1034 instance_h = (appcore_multiwindow_base_instance_h)context;
1035 data = (widget_base_instance_data *)
1036 appcore_multiwindow_base_instance_get_extra(instance_h);
1039 LOGE("Invalid parameter");
1040 return WIDGET_ERROR_INVALID_PARAMETER;
1045 return WIDGET_BASE_ERROR_NONE;
1048 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1050 appcore_multiwindow_base_instance_h instance_h;
1051 widget_base_instance_data *data;
1053 if (!__is_widget_feature_enabled()) {
1054 LOGE("not supported"); /* LCOV_EXCL_LINE */
1055 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1059 LOGE("Invalid parameter");
1060 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1063 instance_h = (appcore_multiwindow_base_instance_h)context;
1064 data = (widget_base_instance_data *)
1065 appcore_multiwindow_base_instance_get_extra(instance_h);
1068 return WIDGET_BASE_ERROR_NONE;
1071 EXPORT_API void *widget_base_context_get_user_data(
1072 widget_base_instance_h context)
1074 appcore_multiwindow_base_instance_h instance_h;
1075 widget_base_instance_data *data;
1077 if (!__is_widget_feature_enabled()) {
1078 LOGE("not supported"); /* LCOV_EXCL_LINE */
1079 return NULL; /* LCOV_EXCL_LINE */
1083 LOGE("Invalid parameter");
1087 instance_h = (appcore_multiwindow_base_instance_h)context;
1088 data = (widget_base_instance_data *)
1089 appcore_multiwindow_base_instance_get_extra(instance_h);
1091 return data->user_data;
1095 EXPORT_API int widget_base_context_set_user_data(
1096 widget_base_instance_h context, void *user_data)
1098 appcore_multiwindow_base_instance_h instance_h;
1099 widget_base_instance_data *data;
1101 if (!__is_widget_feature_enabled()) {
1102 LOGE("not supported"); /* LCOV_EXCL_LINE */
1103 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1107 LOGE("Invalid parameter");
1108 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1111 instance_h = (appcore_multiwindow_base_instance_h)context;
1112 data = (widget_base_instance_data *)
1113 appcore_multiwindow_base_instance_get_extra(instance_h);
1114 data->user_data = user_data;
1116 return WIDGET_BASE_ERROR_NONE;
1119 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1121 appcore_multiwindow_base_instance_h instance_h;
1123 if (!__is_widget_feature_enabled()) {
1124 LOGE("not supported"); /* LCOV_EXCL_LINE */
1125 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1128 instance_h = (appcore_multiwindow_base_instance_h)context;
1129 *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1131 return WIDGET_BASE_ERROR_NONE;
1134 EXPORT_API const char *widget_base_get_viewer_endpoint()
1136 return __viewer_endpoint;
1139 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1143 char *viewer_endpoint = NULL;
1144 appcore_multiwindow_base_ops raw_ops
1145 = appcore_multiwindow_base_get_default_ops();
1147 __context.ops = ops;
1148 __context.argc = argc;
1149 __context.argv = argv;
1150 __context.data = data;
1152 /* override methods */
1153 raw_ops.base.create = __multiwindow_create;
1154 raw_ops.base.control = __multiwindow_control;
1155 raw_ops.base.terminate = __multiwindow_terminate;
1156 raw_ops.base.receive = __multiwindow_receive;
1157 raw_ops.base.init = __multiwindow_init;
1158 raw_ops.base.finish = __multiwindow_finish;
1159 raw_ops.base.run = __multiwindow_run;
1160 raw_ops.base.exit = __multiwindow_exit;
1161 raw_ops.base.trim_memory = __multiwindow_trim_memory;
1163 if (!__is_widget_feature_enabled()) {
1164 LOGE("not supported"); /* LCOV_EXCL_LINE */
1165 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1168 kb = bundle_import_from_argv(argc, argv);
1170 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1171 if (viewer_endpoint) {
1172 LOGD("viewer endpoint :%s", viewer_endpoint);
1173 __viewer_endpoint = strdup(viewer_endpoint);
1175 LOGE("endpoint is missing");
1180 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1181 return WIDGET_ERROR_FAULT;
1184 if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1185 return WIDGET_ERROR_FAULT;
1187 return WIDGET_ERROR_NONE;
1190 static int __on_create(void *data)
1192 return widget_base_on_create();
1195 static int __on_terminate(void *data)
1197 return widget_base_on_terminate();
1200 static void __on_init(int argc, char **argv, void *data)
1202 widget_base_on_init(argc, argv);
1205 static void __on_finish(void)
1207 widget_base_on_finish();
1210 static void __on_run(void *data)
1212 widget_base_on_run();
1215 static void __on_exit(void *data)
1217 widget_base_on_exit();
1220 static void __on_trim_memory(void *data)
1222 widget_base_on_trim_memory();
1225 EXPORT_API int widget_base_on_create(void)
1227 appcore_multiwindow_base_on_create();
1232 EXPORT_API int widget_base_on_terminate(void)
1234 appcore_multiwindow_base_on_terminate();
1239 EXPORT_API int widget_base_on_init(int argc, char **argv)
1244 EXPORT_API void widget_base_on_finish(void)
1248 EXPORT_API void widget_base_on_run(void)
1252 EXPORT_API void widget_base_on_exit(void)
1256 EXPORT_API int widget_base_on_trim_memory(void)
1258 appcore_multiwindow_base_on_trim_memory();
1263 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1265 widget_base_ops ops;
1267 /* override methods */
1268 ops.create = __on_create;
1269 ops.terminate = __on_terminate;
1270 ops.init = __on_init;
1271 ops.finish = __on_finish;
1273 ops.exit = __on_exit;
1274 ops.trim_memory = __on_trim_memory;
1279 static void __free_class(gpointer data)
1281 widget_base_class *cls = data;
1287 EXPORT_API void widget_base_fini(void)
1289 appcore_multiwindow_base_fini();
1290 g_list_free_full(__context.classes, __free_class);
1291 __context.classes = NULL;
1294 EXPORT_API int widget_base_context_window_bind(
1295 widget_base_instance_h instance_h, const char *id,
1296 Ecore_Wl2_Window *wl_win)
1298 struct wl_surface *surface;
1300 surface = ecore_wl2_window_surface_get(wl_win);
1301 if (surface == NULL) {
1302 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1303 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1306 screen_connector_provider_remote_enable(id, surface);
1307 appcore_multiwindow_base_window_bind(instance_h, wl_win);
1309 return WIDGET_BASE_ERROR_NONE;
1312 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1313 int w, int h, void *class_data)
1315 return widget_base_class_on_create(instance_h, content, w, h);
1318 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1320 return widget_base_class_on_resume(instance_h);
1323 static int __class_on_pause(widget_base_instance_h instance_h,
1326 return widget_base_class_on_pause(instance_h);
1329 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1332 return widget_base_class_on_resize(instance_h, w, h);
1335 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1336 int force, void *class_data)
1338 return widget_base_class_on_update(instance_h, content, force);
1341 static int __class_on_destroy(widget_base_instance_h instance_h,
1342 widget_base_destroy_type_e reason, bundle *content,
1345 return widget_base_class_on_destroy(instance_h, reason, content);
1348 static void __multiwindow_instance_create(
1349 appcore_multiwindow_base_instance_h instance_h,
1352 widget_base_instance_data *instance_data;
1354 bundle *content_info = NULL;
1356 char *class_id = NULL;
1357 char *operation = NULL;
1358 char *content = NULL;
1361 char *remain = NULL;
1365 widget_base_class *cls;
1366 double *period = NULL;
1369 appcore_multiwindow_base_class_on_create(instance_h);
1370 instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1371 b = instance_data->args;
1373 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1374 /* for previous version compatibility, use appid for default class id */
1375 if (class_id == NULL)
1378 cls = __get_class(class_id);
1380 LOGE("class not found: %s", class_id);
1384 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1385 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1388 LOGE("no operation provided");
1392 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1393 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1394 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1397 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1400 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1403 content_info = bundle_decode((const bundle_raw *)content,
1406 if (cls->ops.create)
1407 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1410 LOGW("Create callback returns error(%d)", ret);
1411 ret = __send_update_status(class_id, id,
1412 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1414 LOGE("Fail to send abort status (%d) ", ret);
1415 __instance_drop(instance_h);
1417 LOGD("%s is created", id);
1418 ret = __send_update_status(class_id, id,
1419 WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1421 LOGE("Fail to send create status (%d) ", ret);
1423 aul_widget_instance_add(class_id, id);
1425 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1427 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1428 LOGI("set periodic update timer (%lf)", *period);
1429 instance_data->period = *period;
1430 instance_data->periodic_timer = g_timeout_add_seconds(
1431 instance_data->period,
1432 __timeout_cb, instance_data);
1437 bundle_free(content_info);
1440 static void __multiwindow_instance_resume(
1441 appcore_multiwindow_base_instance_h instance_h,
1445 const char *class_id;
1446 widget_base_class *cls;
1447 widget_base_instance_data *data;
1449 appcore_multiwindow_base_class_on_resume(instance_h);
1450 id = appcore_multiwindow_base_instance_get_id(instance_h);
1451 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1452 cls = __get_class(class_id);
1454 LOGE("class not found: %s", class_id);
1458 data = (widget_base_instance_data *)
1459 appcore_multiwindow_base_instance_get_extra(instance_h);
1461 if (data->pending_update) {
1462 LOGD("pending update!");
1463 data->pending_update = false;
1464 __call_update_cb(class_id, data->id, 0, data->pending_content);
1465 if (data->period > 0) {
1466 LOGD("Restart timer!");
1467 data->periodic_timer = g_timeout_add_seconds(
1469 __timeout_cb, data);
1473 if (cls->ops.resume)
1474 cls->ops.resume(instance_h, class_data);
1476 LOGD("%s is resumed", id);
1477 __send_update_status(class_id, id,
1478 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1481 LOGD("Send fg signal to resourceD");
1482 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1487 static void __multiwindow_instance_pause(
1488 appcore_multiwindow_base_instance_h instance_h,
1492 const char *class_id;
1493 widget_base_class *cls;
1495 appcore_multiwindow_base_class_on_pause(instance_h);
1496 id = appcore_multiwindow_base_instance_get_id(instance_h);
1497 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1498 cls = __get_class(class_id);
1500 LOGE("class not found: %s", class_id);
1505 cls->ops.pause(instance_h, class_data);
1507 LOGD("%s is paused", id);
1508 __send_update_status(class_id, id,
1509 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1512 LOGD("Send bg signal to resourceD");
1513 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1514 __fg_signal = false;
1518 static void __multiwindow_instance_terminate(
1519 appcore_multiwindow_base_instance_h instance_h,
1522 widget_base_instance_data *data;
1524 char *operation = NULL;
1525 bundle *content_info;
1526 widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1527 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1529 const char *class_id;
1530 widget_base_class *cls;
1532 id = appcore_multiwindow_base_instance_get_id(instance_h);
1533 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1534 data = appcore_multiwindow_base_instance_get_extra(
1535 (appcore_multiwindow_base_instance_h)instance_h);
1537 cls = __get_class(class_id);
1539 LOGE("class not found: %s", class_id);
1544 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1545 if (operation && strcmp(operation, "destroy") == 0)
1546 reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1550 content_info = bundle_decode((const bundle_raw *)data->content,
1551 strlen(data->content));
1553 content_info = bundle_create();
1555 if (cls->ops.destroy)
1556 cls->ops.destroy(instance_h, reason, content_info, class_data);
1558 LOGW("%s is destroyed %d", id, reason);
1559 if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1560 __is_permanent = true;
1561 event = WIDGET_INSTANCE_EVENT_DESTROY;
1562 aul_widget_instance_del(class_id, id);
1564 __is_permanent = false;
1565 __send_update_status(class_id, id,
1566 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1571 bundle_free(content_info);
1573 if (data->periodic_timer)
1574 g_source_remove(data->periodic_timer);
1576 __send_update_status(class_id, id, event, 0, NULL);
1577 appcore_multiwindow_base_class_on_terminate(instance_h);
1580 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1581 bundle *content, int w, int h)
1583 appcore_multiwindow_base_class_on_create(instance_h);
1588 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1590 appcore_multiwindow_base_class_on_pause(instance_h);
1595 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1597 appcore_multiwindow_base_class_on_resume(instance_h);
1602 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1608 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1609 bundle *content, int force)
1614 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1615 widget_base_destroy_type_e reason, bundle *content)
1617 appcore_multiwindow_base_class_on_terminate(instance_h);
1622 EXPORT_API widget_base_class widget_base_class_get_default(void)
1624 widget_base_class cls;
1626 cls.ops.create = __class_on_create;
1627 cls.ops.resize = __class_on_resize;
1628 cls.ops.update = __class_on_update;
1629 cls.ops.destroy = __class_on_destroy;
1630 cls.ops.pause = __class_on_pause;
1631 cls.ops.resume = __class_on_resume;
1637 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1638 const char *class_id, void *class_data)
1640 widget_base_class *c;
1641 appcore_multiwindow_base_class raw_cls;
1643 if (!__is_widget_feature_enabled()) {
1644 LOGE("not supported");
1645 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1650 LOGE("class is is NULL");
1651 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1655 raw_cls.id = strdup(class_id);
1656 raw_cls.data = class_data;
1657 raw_cls.create = __multiwindow_instance_create;
1658 raw_cls.terminate = __multiwindow_instance_terminate;
1659 raw_cls.pause = __multiwindow_instance_pause;
1660 raw_cls.resume = __multiwindow_instance_resume;
1661 appcore_multiwindow_base_class_add(raw_cls);
1663 c = malloc(sizeof(widget_base_class));
1668 c->id = strdup(class_id);
1669 __context.classes = g_list_append(__context.classes, c);