2 * Copyright (c) 2017 - 2021 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.
17 #include <Ecore_Wl2.h>
18 #include <appcore_multiwindow_base.h>
20 #include <aul_app_com.h>
21 #include <aul_widget.h>
23 #include <bundle_internal.h>
25 #include <glib-object.h>
27 #include <screen_connector_provider.h>
31 #include <system_info.h>
33 #include <widget_errno.h>
34 #include <widget_instance.h>
36 #include "common/export_private.hh"
37 #include "common/log_private.hh"
38 #include "include/widget_base.h"
40 #define APP_TYPE_WIDGET "widgetapp"
41 #define STATUS_FOREGROUND "fg"
42 #define STATUS_BACKGROUND "bg"
44 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
45 [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
46 [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
47 [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
48 [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
49 = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
50 [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
51 [APP_EVENT_SUSPENDED_STATE_CHANGED]
52 = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
55 struct app_event_info {
56 app_event_type_e type;
60 struct app_event_handler {
61 app_event_type_e type;
67 struct widget_foreach_context {
68 widget_base_instance_cb callback;
72 typedef struct _widget_base_context {
78 } widget_base_context;
80 typedef struct _widget_base_instance_data {
88 char *pending_content;
90 } widget_base_instance_data;
92 static widget_base_context __context;
94 static char *__package_id;
95 static bool __fg_signal;
96 static char *__viewer_endpoint;
97 static bool __is_permanent;
98 static void __call_update_cb(const char *class_id, const char *id, int force,
99 const char *content_raw);
101 static gboolean __timeout_cb(gpointer user_data)
103 widget_base_instance_data *data =
104 (widget_base_instance_data *)user_data;
105 appcore_multiwindow_base_instance_h cxt;
106 const char *class_id;
108 cxt = appcore_multiwindow_base_instance_find(data->id);
111 LOGE("Can't find the instance");
112 return G_SOURCE_REMOVE;
115 if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
116 LOGD("Periodic update!");
117 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
118 __call_update_cb(class_id, data->id, 0, NULL);
120 data->pending_update = true;
121 if (data->periodic_timer) {
122 LOGD("Remove timer!");
123 g_source_remove(data->periodic_timer);
124 data->periodic_timer = 0;
128 return G_SOURCE_CONTINUE;
131 static bool __is_widget_feature_enabled(void)
133 static bool feature = false;
134 static bool retrieved = false;
137 if (retrieved == true)
140 ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
141 if (ret != SYSTEM_INFO_ERROR_NONE) {
142 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
143 return false; /* LCOV_EXCL_LINE */
151 static void __check_empty_instance(void)
153 int cnt = appcore_multiwindow_base_instance_get_cnt();
159 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
161 widget_base_instance_data *data;
163 data = appcore_multiwindow_base_instance_get_extra(instance_h);
164 appcore_multiwindow_base_instance_drop(instance_h);
165 free(data->pending_content);
169 __check_empty_instance();
172 static gint __comp_class(gconstpointer a, gconstpointer b)
174 const widget_base_class *cls = a;
176 return strcmp(cls->id, b);
179 static widget_base_class *__get_class(const char *class_id)
181 widget_base_class *cls;
184 class_node = g_list_find_custom(__context.classes, class_id,
186 if (class_node == NULL) {
187 LOGE("empty classes");
190 cls = (widget_base_class *)class_node->data;
195 static int __send_update_status(const char *class_id, const char *instance_id,
196 int status, int err, bundle *extra)
200 aul_widget_send_status_to_viewer(class_id, instance_id,
201 __viewer_endpoint, status, err, extra);
202 lifecycle = widget_instance_convert_event_to_lifecycle_status(status);
203 if (lifecycle > -1) {
204 aul_widget_send_status_to_service(
205 class_id, instance_id, __package_id, lifecycle);
211 static void __control_create(const char *class_id, const char *id, bundle *b)
213 widget_base_instance_data *data;
214 char *content = NULL;
216 if (appcore_multiwindow_base_instance_find(id)) {
217 LOGE("Already exist id (%s)", id);
221 data = (widget_base_instance_data *)
222 calloc(1, sizeof(widget_base_instance_data));
224 LOGE("Out of memory");
228 data->id = strdup(id);
231 /* call stub create */
232 appcore_multiwindow_base_instance_run(class_id, id, data);
233 if (appcore_multiwindow_base_instance_find(id)) {
235 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
237 data->content = strdup(content);
241 static void __control_resume(const char *class_id, const char *id, bundle *b)
243 appcore_multiwindow_base_instance_h cxt;
245 cxt = appcore_multiwindow_base_instance_find(id);
247 LOGE("context not found: %s", id);
251 /* call stub resume */
252 appcore_multiwindow_base_instance_resume(cxt);
255 static void __control_pause(const char *class_id, const char *id, bundle *b)
257 appcore_multiwindow_base_instance_h instance_h;
259 instance_h = appcore_multiwindow_base_instance_find(id);
262 LOGE("instance not found: %s", id);
266 /* call stub pause */
267 appcore_multiwindow_base_instance_pause(instance_h);
270 static void __control_resize(const char *class_id, const char *id, bundle *b)
272 appcore_multiwindow_base_instance_h instance_h;
279 widget_base_class *cls;
280 const appcore_multiwindow_base_class *raw_cls;
282 instance_h = appcore_multiwindow_base_instance_find(id);
284 LOGE("context not found: %s", id);
288 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
292 cls = __get_class(class_id);
294 LOGE("class not found: %s", class_id);
297 class_data = raw_cls->data;
298 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
299 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
302 w = (int)g_ascii_strtoll(w_str, &remain, 10);
305 h = (int)g_ascii_strtoll(h_str, &remain, 10);
308 cls->ops.resize(instance_h, w, h, class_data);
310 LOGD("%s is resized to %dx%d", id, w, h);
311 __send_update_status(class_id, id,
312 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
315 static void __call_update_cb(const char *class_id, const char *id, int force,
316 const char *content_raw)
319 widget_base_class *cls;
320 const appcore_multiwindow_base_class *raw_cls;
321 appcore_multiwindow_base_instance_h instance_h;
322 bundle *content = NULL;
324 instance_h = appcore_multiwindow_base_instance_find(id);
326 LOGE("context not found: %s", id);
330 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
332 LOGE("class is NULL");
336 class_data = raw_cls->data;
337 cls = __get_class(class_id);
339 LOGE("class not found: %s", class_id);
343 if (!cls->ops.update) {
344 LOGE("update callback is NULL");
349 content = bundle_decode((const bundle_raw *)content_raw,
350 strlen(content_raw));
354 cls->ops.update(instance_h, content, force, class_data);
356 __send_update_status(class_id, id,
357 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
358 LOGD("updated:%s", id);
361 bundle_free(content);
364 static void __update_pending_content(
365 appcore_multiwindow_base_instance_h instance_h,
366 const char *content_raw)
368 widget_base_instance_data *data;
370 data = (widget_base_instance_data *)
371 appcore_multiwindow_base_instance_get_extra(instance_h);
373 if (data->pending_content) {
374 free(data->pending_content);
375 data->pending_content = NULL;
379 data->pending_content = strdup(content_raw);
380 if (data->pending_content == NULL)
381 LOGW("Out of memory");
384 data->pending_update = true;
387 static void __update_process(const char *class_id, const char *id,
388 appcore_multiwindow_base_instance_h instance_h, void *data)
390 char *content_raw = NULL;
391 char *force_str = NULL;
396 LOGE("bundle is NULL");
400 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
402 if (force_str && strcmp(force_str, "true") == 0)
407 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
408 if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
409 __update_pending_content(instance_h, content_raw);
411 __call_update_cb(class_id, id, force, content_raw);
414 static void __control_update(const char *class_id, const char *id, bundle *b)
416 appcore_multiwindow_base_instance_h instance_h;
419 appcore_multiwindow_base_instance_foreach(class_id,
420 __update_process, b);
424 instance_h = appcore_multiwindow_base_instance_find(id);
426 LOGE("context not found: %s", id);
430 __update_process(class_id, id, instance_h, b);
433 static void __control_destroy(const char *class_id, const char *id, bundle *b)
435 appcore_multiwindow_base_instance_h instance_h;
436 widget_base_instance_data *data;
438 instance_h = appcore_multiwindow_base_instance_find(id);
440 LOGE("could not find widget obj: %s, clear amd info", id);
441 aul_widget_instance_del(class_id, id);
445 data = (widget_base_instance_data *)
446 appcore_multiwindow_base_instance_get_extra(instance_h);
449 /* call stub terminate */
450 appcore_multiwindow_base_instance_exit(instance_h);
451 free(data->pending_content);
455 __check_empty_instance();
456 aul_widget_write_log(LOG_TAG,
457 "[%s:%d] instance_id(%s)", __FUNCTION__, __LINE__, id);
460 static void __control_change_period(const char *class_id, const char *id,
463 appcore_multiwindow_base_instance_h instance_h;
464 widget_base_instance_data *data;
465 double *period = NULL;
469 instance_h = appcore_multiwindow_base_instance_find(id);
471 LOGE("context not found: %s", id);
475 data = (widget_base_instance_data *)
476 appcore_multiwindow_base_instance_get_extra(instance_h);
479 LOGE("could not find instance data: %s", id);
483 if (data->periodic_timer) {
484 LOGD("Remove timer!");
485 g_source_remove(data->periodic_timer);
486 data->periodic_timer = 0;
489 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
490 if (ret == BUNDLE_ERROR_NONE)
491 data->period = *period;
493 if (data->period > 0) {
494 LOGD("Restart timer!");
495 data->periodic_timer = g_timeout_add_seconds(data->period,
502 static int __multiwindow_create(void *data)
504 char pkgid[256] = {0, };
507 appcore_multiwindow_base_on_create();
508 app_get_id(&__appid);
509 if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
510 __package_id = strdup(pkgid);
512 if (!__package_id || !__appid) {
513 LOGE("__package_id is NULL");
517 screen_connector_provider_init();
518 if (__context.ops.create)
519 ret = __context.ops.create(data);
521 LOGD("widget base is created");
525 static int __multiwindow_terminate(void *data)
527 if (__context.ops.terminate)
528 __context.ops.terminate(data);
529 screen_connector_provider_fini();
531 if (__viewer_endpoint) {
532 free(__viewer_endpoint);
533 __viewer_endpoint = NULL;
546 appcore_multiwindow_base_on_terminate();
548 LOGD("widget base is terminated");
552 static int __multiwindow_control(bundle *b, void *data)
554 char *class_id = NULL;
556 char *operation = NULL;
558 appcore_multiwindow_base_on_control(b);
559 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
560 /* for previous version compatibility, use appid for default class id */
561 if (class_id == NULL)
564 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
565 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
568 LOGE("operation is NULL");
572 LOGI("app control operation(%s)", operation);
573 if (strcmp(operation, "create") == 0)
574 __control_create(class_id, id, b);
575 else if (strcmp(operation, "resize") == 0)
576 __control_resize(class_id, id, b);
577 else if (strcmp(operation, "update") == 0)
578 __control_update(class_id, id, b);
579 else if (strcmp(operation, "destroy") == 0)
580 __control_destroy(class_id, id, b);
581 else if (strcmp(operation, "resume") == 0)
582 __control_resume(class_id, id, b);
583 else if (strcmp(operation, "pause") == 0)
584 __control_pause(class_id, id, b);
585 else if (strcmp(operation, "terminate") == 0)
586 __control_destroy(class_id, id, b);
587 else if (strcmp(operation, "period") == 0)
588 __control_change_period(class_id, id, b);
593 static void __inst_resume_cb(const char *class_id, const char *id,
594 appcore_multiwindow_base_instance_h cxt, void *data)
596 __control_resume(class_id, id, data);
599 static void __get_content(bundle *b)
601 char *instance_id = NULL;
602 appcore_multiwindow_base_instance_h cxt;
603 widget_base_instance_data * we;
605 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
607 LOGE("instance id is NULL");
611 cxt = appcore_multiwindow_base_instance_find(instance_id);
613 LOGE("could not find widget obj: %s", instance_id);
617 we = appcore_multiwindow_base_instance_get_extra(cxt);
619 LOGE("widget extra is NULL");
624 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
625 LOGD("content info of %s found", instance_id);
627 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
628 LOGD("empty content info added");
632 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
634 appcore_multiwindow_base_on_receive(type, b);
638 appcore_multiwindow_base_instance_foreach_full(
639 __inst_resume_cb, b);
644 case AUL_WIDGET_CONTENT:
654 static void __multiwindow_init(int argc, char **argv, void *data)
656 if (__context.ops.init)
657 __context.ops.init(argc, argv, data);
660 static void __multiwindow_finish(void)
662 if (__context.ops.finish) {
663 __context.ops.finish();
664 /* Check Loader case */
665 if (getenv("AUL_LOADER_INIT")) {
666 unsetenv("AUL_LOADER_INIT");
667 __context.ops.finish();
672 static void __multiwindow_run(void *data)
674 if (__context.ops.run)
675 __context.ops.run(data);
678 static void __multiwindow_exit(void *data)
680 if (__context.ops.exit)
681 __context.ops.exit(data);
684 static void __multiwindow_trim_memory(void *data)
686 if (__context.ops.trim_memory)
687 __context.ops.trim_memory(data);
690 EXPORT_API int widget_base_exit(void)
695 appcore_multiwindow_base_exit();
696 cnt = appcore_multiwindow_base_instance_get_cnt();
697 if (cnt == 0 && __is_permanent)
698 ret = aul_notify_exit();
700 aul_widget_write_log(LOG_TAG,
701 "[%s:%d] exit : ret(%d), cnt(%d), permanent(%d)",
702 __FUNCTION__, __LINE__, ret, cnt, __is_permanent);
707 static gboolean __finish_event_cb(gpointer user_data)
709 appcore_multiwindow_base_instance_h cxt = user_data;
712 const char *class_id;
715 LOGE("user_data is NULL");
719 id = appcore_multiwindow_base_instance_get_id(cxt);
720 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
724 LOGE("Out-of-memory");
728 bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
729 __control_destroy(class_id, id, b);
735 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
737 if (!__is_widget_feature_enabled()) {
738 LOGE("not supported"); /* LCOV_EXCL_LINE */
739 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
743 LOGE("context is null");
744 return WIDGET_ERROR_INVALID_PARAMETER;
747 g_idle_add(__finish_event_cb, context);
749 return WIDGET_ERROR_NONE;
752 static void __inst_full_cb(const char *class_id, const char *id,
753 appcore_multiwindow_base_instance_h cxt, void *data)
755 struct widget_foreach_context *foreach_context = data;
760 if (foreach_context->callback)
761 foreach_context->callback(cxt, foreach_context->data);
764 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
766 struct widget_foreach_context foreach_context;
768 if (!__is_widget_feature_enabled()) {
769 LOGE("not supported"); /* LCOV_EXCL_LINE */
770 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
774 LOGE("callback is NULL");
775 return WIDGET_ERROR_INVALID_PARAMETER;
778 foreach_context.callback = cb;
779 foreach_context.data = data;
780 appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
782 return WIDGET_ERROR_NONE;
785 static int __event_cb(void *event, void *data)
787 app_event_handler_h handler = data;
789 struct app_event_info app_event;
791 app_event.type = handler->type;
792 app_event.value = event;
795 handler->cb(&app_event, handler->data);
800 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
801 app_event_type_e event_type,
802 app_event_cb callback,
807 app_event_handler_h handler;
809 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
811 return WIDGET_BASE_ERROR_FAULT;
814 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
816 if (event_handler == NULL || callback == NULL)
817 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
819 if (event_type < APP_EVENT_LOW_MEMORY
820 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
821 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
823 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
824 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
827 handler = calloc(1, sizeof(struct app_event_handler));
829 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
831 handler->type = event_type;
832 handler->cb = callback;
833 handler->data = user_data;
834 handler->raw = appcore_base_add_event(
835 __app_event_converter[event_type], __event_cb, handler);
836 *event_handler = handler;
838 return WIDGET_BASE_ERROR_NONE;
841 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
846 app_event_type_e type;
848 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
850 return WIDGET_BASE_ERROR_FAULT;
853 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
855 if (event_handler == NULL)
856 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
858 type = event_handler->type;
859 if (type < APP_EVENT_LOW_MEMORY ||
860 type > APP_EVENT_REGION_FORMAT_CHANGED)
861 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
863 r = appcore_base_remove_event(event_handler->raw);
865 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
869 return WIDGET_BASE_ERROR_NONE;
872 EXPORT_API int widget_base_context_set_content_info(
873 widget_base_instance_h context,
874 bundle *content_info)
877 bundle_raw *raw = NULL;
880 const char *class_id;
881 widget_base_instance_data *data;
882 appcore_multiwindow_base_instance_h instance_h;
884 if (!__is_widget_feature_enabled()) {
885 LOGE("not supported"); /* LCOV_EXCL_LINE */
886 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
889 if (!context || !content_info)
890 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
892 instance_h = (appcore_multiwindow_base_instance_h)context;
893 id = appcore_multiwindow_base_instance_get_id(instance_h);
894 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
895 data = appcore_multiwindow_base_instance_get_extra(instance_h);
897 if (!class_id || !id || !data)
898 return WIDGET_BASE_ERROR_FAULT;
900 ret = __send_update_status(class_id, id,
901 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
906 bundle_encode(content_info, &raw, &len);
908 data->content = strdup((const char *)raw);
910 data->content = NULL;
914 /* LCOV_EXCL_START */
915 LOGE("failed to send content info: %s of %s (%d)", id,
917 return WIDGET_BASE_ERROR_IO_ERROR;
921 return WIDGET_BASE_ERROR_NONE;
924 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
926 appcore_multiwindow_base_instance_h instance_h;
927 widget_base_instance_data *data;
929 if (!__is_widget_feature_enabled()) {
930 LOGE("not supported"); /* LCOV_EXCL_LINE */
931 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
934 if (!context || !tag) {
935 LOGE("Invalid parameter");
936 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
939 instance_h = (appcore_multiwindow_base_instance_h)context;
940 data = (widget_base_instance_data *)
941 appcore_multiwindow_base_instance_get_extra(instance_h);
944 LOGE("Invalid parameter");
945 return WIDGET_ERROR_INVALID_PARAMETER;
950 return WIDGET_BASE_ERROR_NONE;
953 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
955 appcore_multiwindow_base_instance_h instance_h;
956 widget_base_instance_data *data;
958 if (!__is_widget_feature_enabled()) {
959 LOGE("not supported"); /* LCOV_EXCL_LINE */
960 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
964 LOGE("Invalid parameter");
965 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
968 instance_h = (appcore_multiwindow_base_instance_h)context;
969 data = (widget_base_instance_data *)
970 appcore_multiwindow_base_instance_get_extra(instance_h);
973 return WIDGET_BASE_ERROR_NONE;
976 EXPORT_API void *widget_base_context_get_user_data(
977 widget_base_instance_h context)
979 appcore_multiwindow_base_instance_h instance_h;
980 widget_base_instance_data *data;
982 if (!__is_widget_feature_enabled()) {
983 LOGE("not supported"); /* LCOV_EXCL_LINE */
984 return NULL; /* LCOV_EXCL_LINE */
988 LOGE("Invalid parameter");
992 instance_h = (appcore_multiwindow_base_instance_h)context;
993 data = (widget_base_instance_data *)
994 appcore_multiwindow_base_instance_get_extra(instance_h);
996 return data->user_data;
1000 EXPORT_API int widget_base_context_set_user_data(
1001 widget_base_instance_h context, void *user_data)
1003 appcore_multiwindow_base_instance_h instance_h;
1004 widget_base_instance_data *data;
1006 if (!__is_widget_feature_enabled()) {
1007 LOGE("not supported"); /* LCOV_EXCL_LINE */
1008 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1012 LOGE("Invalid parameter");
1013 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1016 instance_h = (appcore_multiwindow_base_instance_h)context;
1017 data = (widget_base_instance_data *)
1018 appcore_multiwindow_base_instance_get_extra(instance_h);
1019 data->user_data = user_data;
1021 return WIDGET_BASE_ERROR_NONE;
1024 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1026 appcore_multiwindow_base_instance_h instance_h;
1028 if (!__is_widget_feature_enabled()) {
1029 LOGE("not supported"); /* LCOV_EXCL_LINE */
1030 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1033 instance_h = (appcore_multiwindow_base_instance_h)context;
1034 *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1036 return WIDGET_BASE_ERROR_NONE;
1039 EXPORT_API const char *widget_base_get_viewer_endpoint()
1041 return __viewer_endpoint;
1044 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1048 char *viewer_endpoint = NULL;
1049 appcore_multiwindow_base_ops raw_ops
1050 = appcore_multiwindow_base_get_default_ops();
1052 __context.ops = ops;
1053 __context.argc = argc;
1054 __context.argv = argv;
1055 __context.data = data;
1057 /* override methods */
1058 raw_ops.base.create = __multiwindow_create;
1059 raw_ops.base.control = __multiwindow_control;
1060 raw_ops.base.terminate = __multiwindow_terminate;
1061 raw_ops.base.receive = __multiwindow_receive;
1062 raw_ops.base.init = __multiwindow_init;
1063 raw_ops.base.finish = __multiwindow_finish;
1064 raw_ops.base.run = __multiwindow_run;
1065 raw_ops.base.exit = __multiwindow_exit;
1066 raw_ops.base.trim_memory = __multiwindow_trim_memory;
1068 if (!__is_widget_feature_enabled()) {
1069 LOGE("not supported"); /* LCOV_EXCL_LINE */
1070 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1073 kb = bundle_import_from_argv(argc, argv);
1075 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1076 if (viewer_endpoint) {
1077 LOGD("viewer endpoint :%s", viewer_endpoint);
1078 __viewer_endpoint = strdup(viewer_endpoint);
1080 LOGE("endpoint is missing");
1085 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1086 return WIDGET_ERROR_FAULT;
1089 if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1090 return WIDGET_ERROR_FAULT;
1092 return WIDGET_ERROR_NONE;
1095 static int __on_create(void *data)
1097 return widget_base_on_create();
1100 static int __on_terminate(void *data)
1102 return widget_base_on_terminate();
1105 static void __on_init(int argc, char **argv, void *data)
1107 widget_base_on_init(argc, argv);
1110 static void __on_finish(void)
1112 widget_base_on_finish();
1115 static void __on_run(void *data)
1117 widget_base_on_run();
1120 static void __on_exit(void *data)
1122 widget_base_on_exit();
1125 static void __on_trim_memory(void *data)
1127 widget_base_on_trim_memory();
1130 EXPORT_API int widget_base_on_create(void)
1132 appcore_multiwindow_base_on_create();
1137 EXPORT_API int widget_base_on_terminate(void)
1139 appcore_multiwindow_base_on_terminate();
1144 EXPORT_API int widget_base_on_init(int argc, char **argv)
1149 EXPORT_API void widget_base_on_finish(void)
1153 EXPORT_API void widget_base_on_run(void)
1157 EXPORT_API void widget_base_on_exit(void)
1161 EXPORT_API int widget_base_on_trim_memory(void)
1163 appcore_multiwindow_base_on_trim_memory();
1168 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1170 widget_base_ops ops;
1172 /* override methods */
1173 ops.create = __on_create;
1174 ops.terminate = __on_terminate;
1175 ops.init = __on_init;
1176 ops.finish = __on_finish;
1178 ops.exit = __on_exit;
1179 ops.trim_memory = __on_trim_memory;
1184 static void __free_class(gpointer data)
1186 widget_base_class *cls = data;
1192 EXPORT_API void widget_base_fini(void)
1194 appcore_multiwindow_base_fini();
1195 g_list_free_full(__context.classes, __free_class);
1196 __context.classes = NULL;
1199 EXPORT_API int widget_base_context_window_bind(
1200 widget_base_instance_h instance_h, const char *id,
1201 Ecore_Wl2_Window *wl_win)
1203 struct wl_surface *surface;
1205 surface = ecore_wl2_window_surface_get(wl_win);
1206 if (surface == NULL) {
1207 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1208 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1211 screen_connector_provider_remote_enable(id, surface);
1212 appcore_multiwindow_base_window_bind(instance_h, wl_win);
1214 return WIDGET_BASE_ERROR_NONE;
1217 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1218 int w, int h, void *class_data)
1220 return widget_base_class_on_create(instance_h, content, w, h);
1223 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1225 return widget_base_class_on_resume(instance_h);
1228 static int __class_on_pause(widget_base_instance_h instance_h,
1231 return widget_base_class_on_pause(instance_h);
1234 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1237 return widget_base_class_on_resize(instance_h, w, h);
1240 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1241 int force, void *class_data)
1243 return widget_base_class_on_update(instance_h, content, force);
1246 static int __class_on_destroy(widget_base_instance_h instance_h,
1247 widget_base_destroy_type_e reason, bundle *content,
1250 return widget_base_class_on_destroy(instance_h, reason, content);
1253 static void __multiwindow_instance_create(
1254 appcore_multiwindow_base_instance_h instance_h,
1257 widget_base_instance_data *instance_data;
1259 bundle *content_info = NULL;
1261 char *class_id = NULL;
1262 char *operation = NULL;
1263 char *content = NULL;
1266 char *remain = NULL;
1270 widget_base_class *cls;
1271 double *period = NULL;
1274 appcore_multiwindow_base_class_on_create(instance_h);
1275 instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1276 b = instance_data->args;
1278 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1279 /* for previous version compatibility, use appid for default class id */
1280 if (class_id == NULL)
1283 cls = __get_class(class_id);
1285 LOGE("class not found: %s", class_id);
1289 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1290 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1293 LOGE("no operation provided");
1297 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1298 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1299 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1302 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1305 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1308 content_info = bundle_decode((const bundle_raw *)content,
1311 if (cls->ops.create)
1312 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1315 LOGW("Create callback returns error(%d)", ret);
1316 ret = __send_update_status(class_id, id,
1317 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1319 LOGE("Fail to send abort status (%d) ", ret);
1320 __instance_drop(instance_h);
1322 LOGD("%s is created", id);
1323 aul_widget_instance_add(class_id, id);
1324 ret = __send_update_status(class_id, id,
1325 WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1327 LOGE("Fail to send create status (%d) ", ret);
1329 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1331 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1332 LOGI("set periodic update timer (%lf)", *period);
1333 instance_data->period = *period;
1334 instance_data->periodic_timer = g_timeout_add_seconds(
1335 instance_data->period,
1336 __timeout_cb, instance_data);
1341 bundle_free(content_info);
1344 static void __multiwindow_instance_resume(
1345 appcore_multiwindow_base_instance_h instance_h,
1349 const char *class_id;
1350 widget_base_class *cls;
1351 widget_base_instance_data *data;
1353 appcore_multiwindow_base_class_on_resume(instance_h);
1354 id = appcore_multiwindow_base_instance_get_id(instance_h);
1355 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1356 cls = __get_class(class_id);
1358 LOGE("class not found: %s", class_id);
1362 data = (widget_base_instance_data *)
1363 appcore_multiwindow_base_instance_get_extra(instance_h);
1365 if (data->pending_update) {
1366 LOGD("pending update!");
1367 data->pending_update = false;
1368 __call_update_cb(class_id, data->id, 0, data->pending_content);
1369 if (data->period > 0) {
1370 LOGD("Restart timer!");
1371 data->periodic_timer = g_timeout_add_seconds(
1373 __timeout_cb, data);
1377 if (cls->ops.resume)
1378 cls->ops.resume(instance_h, class_data);
1380 LOGD("%s is resumed", id);
1381 __send_update_status(class_id, id,
1382 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1385 LOGD("Send fg signal to resourceD");
1386 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1391 static void __multiwindow_instance_pause(
1392 appcore_multiwindow_base_instance_h instance_h,
1396 const char *class_id;
1397 widget_base_class *cls;
1399 appcore_multiwindow_base_class_on_pause(instance_h);
1400 id = appcore_multiwindow_base_instance_get_id(instance_h);
1401 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1402 cls = __get_class(class_id);
1404 LOGE("class not found: %s", class_id);
1409 cls->ops.pause(instance_h, class_data);
1411 LOGD("%s is paused", id);
1412 __send_update_status(class_id, id,
1413 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1416 LOGD("Send bg signal to resourceD");
1417 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1418 __fg_signal = false;
1422 static void __multiwindow_instance_terminate(
1423 appcore_multiwindow_base_instance_h instance_h,
1426 widget_base_instance_data *data;
1428 char *operation = NULL;
1429 bundle *content_info;
1430 widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1431 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1433 const char *class_id;
1434 widget_base_class *cls;
1436 id = appcore_multiwindow_base_instance_get_id(instance_h);
1437 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1438 data = appcore_multiwindow_base_instance_get_extra(
1439 (appcore_multiwindow_base_instance_h)instance_h);
1441 cls = __get_class(class_id);
1443 LOGE("class not found: %s", class_id);
1448 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1449 if (operation && strcmp(operation, "destroy") == 0)
1450 reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1454 content_info = bundle_decode((const bundle_raw *)data->content,
1455 strlen(data->content));
1457 content_info = bundle_create();
1459 if (cls->ops.destroy)
1460 cls->ops.destroy(instance_h, reason, content_info, class_data);
1462 LOGW("%s is destroyed %d", id, reason);
1463 if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1464 __is_permanent = true;
1465 event = WIDGET_INSTANCE_EVENT_DESTROY;
1466 aul_widget_instance_del(class_id, id);
1468 __is_permanent = false;
1469 __send_update_status(class_id, id,
1470 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1475 bundle_free(content_info);
1477 if (data->periodic_timer)
1478 g_source_remove(data->periodic_timer);
1480 __send_update_status(class_id, id, event, 0, NULL);
1481 appcore_multiwindow_base_class_on_terminate(instance_h);
1484 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1485 bundle *content, int w, int h)
1487 appcore_multiwindow_base_class_on_create(instance_h);
1492 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1494 appcore_multiwindow_base_class_on_pause(instance_h);
1499 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1501 appcore_multiwindow_base_class_on_resume(instance_h);
1506 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1512 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1513 bundle *content, int force)
1518 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1519 widget_base_destroy_type_e reason, bundle *content)
1521 appcore_multiwindow_base_class_on_terminate(instance_h);
1526 EXPORT_API widget_base_class widget_base_class_get_default(void)
1528 widget_base_class cls;
1530 cls.ops.create = __class_on_create;
1531 cls.ops.resize = __class_on_resize;
1532 cls.ops.update = __class_on_update;
1533 cls.ops.destroy = __class_on_destroy;
1534 cls.ops.pause = __class_on_pause;
1535 cls.ops.resume = __class_on_resume;
1541 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1542 const char *class_id, void *class_data)
1544 widget_base_class *c;
1545 appcore_multiwindow_base_class raw_cls;
1547 if (!__is_widget_feature_enabled()) {
1548 LOGE("not supported");
1549 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1554 LOGE("class id is NULL");
1555 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1559 raw_cls.id = strdup(class_id);
1560 raw_cls.data = class_data;
1561 raw_cls.create = __multiwindow_instance_create;
1562 raw_cls.terminate = __multiwindow_instance_terminate;
1563 raw_cls.pause = __multiwindow_instance_pause;
1564 raw_cls.resume = __multiwindow_instance_resume;
1565 appcore_multiwindow_base_class_add(raw_cls);
1567 c = malloc(sizeof(widget_base_class));
1572 c->id = strdup(class_id);
1573 __context.classes = g_list_append(__context.classes, c);