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 /* LCOV_EXCL_START */
102 static gboolean __timeout_cb(gpointer user_data)
104 widget_base_instance_data *data =
105 (widget_base_instance_data *)user_data;
106 appcore_multiwindow_base_instance_h cxt;
107 const char *class_id;
109 cxt = appcore_multiwindow_base_instance_find(data->id);
112 LOGE("Can't find the instance");
113 return G_SOURCE_REMOVE;
116 if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
117 LOGD("Periodic update!");
118 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
119 __call_update_cb(class_id, data->id, 0, NULL);
121 data->pending_update = true;
122 if (data->periodic_timer) {
123 LOGD("Remove timer!");
124 g_source_remove(data->periodic_timer);
125 data->periodic_timer = 0;
129 return G_SOURCE_CONTINUE;
132 static bool __is_widget_feature_enabled(void)
134 static bool feature = false;
135 static bool retrieved = false;
138 if (retrieved == true)
141 ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
142 if (ret != SYSTEM_INFO_ERROR_NONE) {
143 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
144 return false; /* LCOV_EXCL_LINE */
152 static void __check_empty_instance(void)
154 int cnt = appcore_multiwindow_base_instance_get_cnt();
160 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
162 widget_base_instance_data *data;
164 data = appcore_multiwindow_base_instance_get_extra(instance_h);
165 appcore_multiwindow_base_instance_drop(instance_h);
166 free(data->pending_content);
170 __check_empty_instance();
173 static gint __comp_class(gconstpointer a, gconstpointer b)
175 const widget_base_class *cls = a;
177 return strcmp(cls->id, b);
180 static widget_base_class *__get_class(const char *class_id)
182 widget_base_class *cls;
185 class_node = g_list_find_custom(__context.classes, class_id,
187 if (class_node == NULL) {
188 LOGE("empty classes");
191 cls = (widget_base_class *)class_node->data;
196 static int __send_update_status(const char *class_id, const char *instance_id,
197 int status, int err, bundle *extra)
201 aul_widget_send_status_to_viewer(class_id, instance_id,
202 __viewer_endpoint, status, err, extra);
203 lifecycle = widget_instance_convert_event_to_lifecycle_status(status);
204 if (lifecycle > -1) {
205 aul_widget_send_status_to_service(
206 class_id, instance_id, __package_id, lifecycle);
212 static void __control_create(const char *class_id, const char *id, bundle *b)
214 widget_base_instance_data *data;
215 char *content = NULL;
217 if (appcore_multiwindow_base_instance_find(id)) {
218 LOGE("Already exist id (%s)", id);
222 data = (widget_base_instance_data *)
223 calloc(1, sizeof(widget_base_instance_data));
225 LOGE("Out of memory");
229 data->id = strdup(id);
232 /* call stub create */
233 appcore_multiwindow_base_instance_run(class_id, id, data);
234 if (appcore_multiwindow_base_instance_find(id)) {
236 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
238 data->content = strdup(content);
242 static void __control_resume(const char *class_id, const char *id, bundle *b)
244 appcore_multiwindow_base_instance_h cxt;
246 cxt = appcore_multiwindow_base_instance_find(id);
248 LOGE("context not found: %s", id);
252 /* call stub resume */
253 appcore_multiwindow_base_instance_resume(cxt);
256 static void __control_pause(const char *class_id, const char *id, bundle *b)
258 appcore_multiwindow_base_instance_h instance_h;
260 instance_h = appcore_multiwindow_base_instance_find(id);
263 LOGE("instance not found: %s", id);
267 /* call stub pause */
268 appcore_multiwindow_base_instance_pause(instance_h);
271 static void __control_resize(const char *class_id, const char *id, bundle *b)
273 appcore_multiwindow_base_instance_h instance_h;
280 widget_base_class *cls;
281 const appcore_multiwindow_base_class *raw_cls;
283 instance_h = appcore_multiwindow_base_instance_find(id);
285 LOGE("context not found: %s", id);
289 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
293 cls = __get_class(class_id);
295 LOGE("class not found: %s", class_id);
298 class_data = raw_cls->data;
299 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
300 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
303 w = (int)g_ascii_strtoll(w_str, &remain, 10);
306 h = (int)g_ascii_strtoll(h_str, &remain, 10);
309 cls->ops.resize(instance_h, w, h, class_data);
311 LOGD("%s is resized to %dx%d", id, w, h);
312 __send_update_status(class_id, id,
313 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
316 static void __call_update_cb(const char *class_id, const char *id, int force,
317 const char *content_raw)
320 widget_base_class *cls;
321 const appcore_multiwindow_base_class *raw_cls;
322 appcore_multiwindow_base_instance_h instance_h;
323 bundle *content = NULL;
325 instance_h = appcore_multiwindow_base_instance_find(id);
327 LOGE("context not found: %s", id);
331 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
333 LOGE("class is NULL");
337 class_data = raw_cls->data;
338 cls = __get_class(class_id);
340 LOGE("class not found: %s", class_id);
344 if (!cls->ops.update) {
345 LOGE("update callback is NULL");
350 content = bundle_decode((const bundle_raw *)content_raw,
351 strlen(content_raw));
355 cls->ops.update(instance_h, content, force, class_data);
357 __send_update_status(class_id, id,
358 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
359 LOGD("updated:%s", id);
362 bundle_free(content);
365 static void __update_pending_content(
366 appcore_multiwindow_base_instance_h instance_h,
367 const char *content_raw)
369 widget_base_instance_data *data;
371 data = (widget_base_instance_data *)
372 appcore_multiwindow_base_instance_get_extra(instance_h);
374 if (data->pending_content) {
375 free(data->pending_content);
376 data->pending_content = NULL;
380 data->pending_content = strdup(content_raw);
381 if (data->pending_content == NULL)
382 LOGW("Out of memory");
385 data->pending_update = true;
388 static void __update_process(const char *class_id, const char *id,
389 appcore_multiwindow_base_instance_h instance_h, void *data)
391 char *content_raw = NULL;
392 char *force_str = NULL;
397 LOGE("bundle is NULL");
401 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
403 if (force_str && strcmp(force_str, "true") == 0)
408 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
409 if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
410 __update_pending_content(instance_h, content_raw);
412 __call_update_cb(class_id, id, force, content_raw);
415 static void __control_update(const char *class_id, const char *id, bundle *b)
417 appcore_multiwindow_base_instance_h instance_h;
420 appcore_multiwindow_base_instance_foreach(class_id,
421 __update_process, b);
425 instance_h = appcore_multiwindow_base_instance_find(id);
427 LOGE("context not found: %s", id);
431 __update_process(class_id, id, instance_h, b);
434 static void __control_destroy(const char *class_id, const char *id, bundle *b)
436 appcore_multiwindow_base_instance_h instance_h;
437 widget_base_instance_data *data;
439 instance_h = appcore_multiwindow_base_instance_find(id);
441 LOGE("could not find widget obj: %s, clear amd info", id);
442 aul_widget_instance_del(class_id, id);
446 data = (widget_base_instance_data *)
447 appcore_multiwindow_base_instance_get_extra(instance_h);
450 /* call stub terminate */
451 appcore_multiwindow_base_instance_exit(instance_h);
452 free(data->pending_content);
456 __check_empty_instance();
457 aul_widget_write_log(LOG_TAG,
458 "[%s:%d] instance_id(%s)", __FUNCTION__, __LINE__, id);
461 static void __control_change_period(const char *class_id, const char *id,
464 appcore_multiwindow_base_instance_h instance_h;
465 widget_base_instance_data *data;
466 double *period = NULL;
470 instance_h = appcore_multiwindow_base_instance_find(id);
472 LOGE("context not found: %s", id);
476 data = (widget_base_instance_data *)
477 appcore_multiwindow_base_instance_get_extra(instance_h);
480 LOGE("could not find instance data: %s", id);
484 if (data->periodic_timer) {
485 LOGD("Remove timer!");
486 g_source_remove(data->periodic_timer);
487 data->periodic_timer = 0;
490 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
491 if (ret == BUNDLE_ERROR_NONE)
492 data->period = *period;
494 if (data->period > 0) {
495 LOGD("Restart timer!");
496 data->periodic_timer = g_timeout_add_seconds(data->period,
503 static int __multiwindow_create(void *data)
505 char pkgid[256] = {0, };
508 appcore_multiwindow_base_on_create();
509 app_get_id(&__appid);
510 if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
511 __package_id = strdup(pkgid);
513 if (!__package_id || !__appid) {
514 LOGE("__package_id is NULL");
518 screen_connector_provider_init();
519 if (__context.ops.create)
520 ret = __context.ops.create(data);
522 LOGD("widget base is created");
526 static int __multiwindow_terminate(void *data)
528 if (__context.ops.terminate)
529 __context.ops.terminate(data);
530 screen_connector_provider_fini();
532 if (__viewer_endpoint) {
533 free(__viewer_endpoint);
534 __viewer_endpoint = NULL;
547 appcore_multiwindow_base_on_terminate();
549 LOGD("widget base is terminated");
553 static int __multiwindow_control(bundle *b, void *data)
555 char *class_id = NULL;
557 char *operation = NULL;
559 appcore_multiwindow_base_on_control(b);
560 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
561 /* for previous version compatibility, use appid for default class id */
562 if (class_id == NULL)
565 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
566 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
569 LOGE("operation is NULL");
573 LOGI("app control operation(%s)", operation);
574 if (strcmp(operation, "create") == 0)
575 __control_create(class_id, id, b);
576 else if (strcmp(operation, "resize") == 0)
577 __control_resize(class_id, id, b);
578 else if (strcmp(operation, "update") == 0)
579 __control_update(class_id, id, b);
580 else if (strcmp(operation, "destroy") == 0)
581 __control_destroy(class_id, id, b);
582 else if (strcmp(operation, "resume") == 0)
583 __control_resume(class_id, id, b);
584 else if (strcmp(operation, "pause") == 0)
585 __control_pause(class_id, id, b);
586 else if (strcmp(operation, "terminate") == 0)
587 __control_destroy(class_id, id, b);
588 else if (strcmp(operation, "period") == 0)
589 __control_change_period(class_id, id, b);
594 static void __inst_resume_cb(const char *class_id, const char *id,
595 appcore_multiwindow_base_instance_h cxt, void *data)
597 __control_resume(class_id, id, data);
600 static void __get_content(bundle *b)
602 char *instance_id = NULL;
603 appcore_multiwindow_base_instance_h cxt;
604 widget_base_instance_data * we;
606 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
608 LOGE("instance id is NULL");
612 cxt = appcore_multiwindow_base_instance_find(instance_id);
614 LOGE("could not find widget obj: %s", instance_id);
618 we = appcore_multiwindow_base_instance_get_extra(cxt);
620 LOGE("widget extra is NULL");
625 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
626 LOGD("content info of %s found", instance_id);
628 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
629 LOGD("empty content info added");
633 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
635 appcore_multiwindow_base_on_receive(type, b);
639 appcore_multiwindow_base_instance_foreach_full(
640 __inst_resume_cb, b);
645 case AUL_WIDGET_CONTENT:
655 static void __multiwindow_init(int argc, char **argv, void *data)
657 if (__context.ops.init)
658 __context.ops.init(argc, argv, data);
661 static void __multiwindow_finish(void)
663 if (__context.ops.finish) {
664 __context.ops.finish();
665 /* Check Loader case */
666 if (getenv("AUL_LOADER_INIT")) {
667 unsetenv("AUL_LOADER_INIT");
668 __context.ops.finish();
673 static void __multiwindow_run(void *data)
675 if (__context.ops.run)
676 __context.ops.run(data);
679 static void __multiwindow_exit(void *data)
681 if (__context.ops.exit)
682 __context.ops.exit(data);
685 static void __multiwindow_trim_memory(void *data)
687 if (__context.ops.trim_memory)
688 __context.ops.trim_memory(data);
691 EXPORT_API int widget_base_exit(void)
696 appcore_multiwindow_base_exit();
697 cnt = appcore_multiwindow_base_instance_get_cnt();
698 if (cnt == 0 && __is_permanent)
699 ret = aul_notify_exit();
701 aul_widget_write_log(LOG_TAG,
702 "[%s:%d] exit : ret(%d), cnt(%d), permanent(%d)",
703 __FUNCTION__, __LINE__, ret, cnt, __is_permanent);
708 static gboolean __finish_event_cb(gpointer user_data)
710 appcore_multiwindow_base_instance_h cxt = user_data;
713 const char *class_id;
716 LOGE("user_data is NULL");
720 id = appcore_multiwindow_base_instance_get_id(cxt);
721 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
725 LOGE("Out-of-memory");
729 bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
730 __control_destroy(class_id, id, b);
736 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
738 if (!__is_widget_feature_enabled()) {
739 LOGE("not supported"); /* LCOV_EXCL_LINE */
740 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
744 LOGE("context is null");
745 return WIDGET_ERROR_INVALID_PARAMETER;
748 g_idle_add(__finish_event_cb, context);
750 return WIDGET_ERROR_NONE;
753 static void __inst_full_cb(const char *class_id, const char *id,
754 appcore_multiwindow_base_instance_h cxt, void *data)
756 struct widget_foreach_context *foreach_context = data;
761 if (foreach_context->callback)
762 foreach_context->callback(cxt, foreach_context->data);
765 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
767 struct widget_foreach_context foreach_context;
769 if (!__is_widget_feature_enabled()) {
770 LOGE("not supported"); /* LCOV_EXCL_LINE */
771 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
775 LOGE("callback is NULL");
776 return WIDGET_ERROR_INVALID_PARAMETER;
779 foreach_context.callback = cb;
780 foreach_context.data = data;
781 appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
783 return WIDGET_ERROR_NONE;
786 static int __event_cb(void *event, void *data)
788 app_event_handler_h handler = data;
790 struct app_event_info app_event;
792 app_event.type = handler->type;
793 app_event.value = event;
796 handler->cb(&app_event, handler->data);
801 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
802 app_event_type_e event_type,
803 app_event_cb callback,
808 app_event_handler_h handler;
810 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
812 return WIDGET_BASE_ERROR_FAULT;
815 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
817 if (event_handler == NULL || callback == NULL)
818 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
820 if (event_type < APP_EVENT_LOW_MEMORY
821 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
822 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
824 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
825 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
828 handler = calloc(1, sizeof(struct app_event_handler));
830 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
832 handler->type = event_type;
833 handler->cb = callback;
834 handler->data = user_data;
835 handler->raw = appcore_base_add_event(
836 __app_event_converter[event_type], __event_cb, handler);
837 *event_handler = handler;
839 return WIDGET_BASE_ERROR_NONE;
842 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
847 app_event_type_e type;
849 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
851 return WIDGET_BASE_ERROR_FAULT;
854 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
856 if (event_handler == NULL)
857 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
859 type = event_handler->type;
860 if (type < APP_EVENT_LOW_MEMORY ||
861 type > APP_EVENT_REGION_FORMAT_CHANGED)
862 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
864 r = appcore_base_remove_event(event_handler->raw);
866 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
870 return WIDGET_BASE_ERROR_NONE;
873 EXPORT_API int widget_base_context_set_content_info(
874 widget_base_instance_h context,
875 bundle *content_info)
878 bundle_raw *raw = NULL;
881 const char *class_id;
882 widget_base_instance_data *data;
883 appcore_multiwindow_base_instance_h instance_h;
885 if (!__is_widget_feature_enabled()) {
886 LOGE("not supported"); /* LCOV_EXCL_LINE */
887 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
890 if (!context || !content_info)
891 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
893 instance_h = (appcore_multiwindow_base_instance_h)context;
894 id = appcore_multiwindow_base_instance_get_id(instance_h);
895 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
896 data = appcore_multiwindow_base_instance_get_extra(instance_h);
898 if (!class_id || !id || !data)
899 return WIDGET_BASE_ERROR_FAULT;
901 ret = __send_update_status(class_id, id,
902 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
907 bundle_encode(content_info, &raw, &len);
909 data->content = strdup((const char *)raw);
911 data->content = NULL;
915 LOGE("failed to send content info: %s of %s (%d)", id,
917 return WIDGET_BASE_ERROR_IO_ERROR;
920 return WIDGET_BASE_ERROR_NONE;
923 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
925 appcore_multiwindow_base_instance_h instance_h;
926 widget_base_instance_data *data;
928 if (!__is_widget_feature_enabled()) {
929 LOGE("not supported"); /* LCOV_EXCL_LINE */
930 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
933 if (!context || !tag) {
934 LOGE("Invalid parameter");
935 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
938 instance_h = (appcore_multiwindow_base_instance_h)context;
939 data = (widget_base_instance_data *)
940 appcore_multiwindow_base_instance_get_extra(instance_h);
943 LOGE("Invalid parameter");
944 return WIDGET_ERROR_INVALID_PARAMETER;
949 return WIDGET_BASE_ERROR_NONE;
952 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
954 appcore_multiwindow_base_instance_h instance_h;
955 widget_base_instance_data *data;
957 if (!__is_widget_feature_enabled()) {
958 LOGE("not supported"); /* LCOV_EXCL_LINE */
959 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
963 LOGE("Invalid parameter");
964 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
967 instance_h = (appcore_multiwindow_base_instance_h)context;
968 data = (widget_base_instance_data *)
969 appcore_multiwindow_base_instance_get_extra(instance_h);
972 return WIDGET_BASE_ERROR_NONE;
975 EXPORT_API void *widget_base_context_get_user_data(
976 widget_base_instance_h context)
978 appcore_multiwindow_base_instance_h instance_h;
979 widget_base_instance_data *data;
981 if (!__is_widget_feature_enabled()) {
982 LOGE("not supported"); /* LCOV_EXCL_LINE */
983 return NULL; /* LCOV_EXCL_LINE */
987 LOGE("Invalid parameter");
991 instance_h = (appcore_multiwindow_base_instance_h)context;
992 data = (widget_base_instance_data *)
993 appcore_multiwindow_base_instance_get_extra(instance_h);
995 return data->user_data;
999 EXPORT_API int widget_base_context_set_user_data(
1000 widget_base_instance_h context, void *user_data)
1002 appcore_multiwindow_base_instance_h instance_h;
1003 widget_base_instance_data *data;
1005 if (!__is_widget_feature_enabled()) {
1006 LOGE("not supported"); /* LCOV_EXCL_LINE */
1007 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1011 LOGE("Invalid parameter");
1012 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1015 instance_h = (appcore_multiwindow_base_instance_h)context;
1016 data = (widget_base_instance_data *)
1017 appcore_multiwindow_base_instance_get_extra(instance_h);
1018 data->user_data = user_data;
1020 return WIDGET_BASE_ERROR_NONE;
1023 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1025 appcore_multiwindow_base_instance_h instance_h;
1027 if (!__is_widget_feature_enabled()) {
1028 LOGE("not supported"); /* LCOV_EXCL_LINE */
1029 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1032 instance_h = (appcore_multiwindow_base_instance_h)context;
1033 *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1035 return WIDGET_BASE_ERROR_NONE;
1038 EXPORT_API const char *widget_base_get_viewer_endpoint()
1040 return __viewer_endpoint;
1043 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1047 char *viewer_endpoint = NULL;
1048 appcore_multiwindow_base_ops raw_ops
1049 = appcore_multiwindow_base_get_default_ops();
1051 __context.ops = ops;
1052 __context.argc = argc;
1053 __context.argv = argv;
1054 __context.data = data;
1056 /* override methods */
1057 raw_ops.base.create = __multiwindow_create;
1058 raw_ops.base.control = __multiwindow_control;
1059 raw_ops.base.terminate = __multiwindow_terminate;
1060 raw_ops.base.receive = __multiwindow_receive;
1061 raw_ops.base.init = __multiwindow_init;
1062 raw_ops.base.finish = __multiwindow_finish;
1063 raw_ops.base.run = __multiwindow_run;
1064 raw_ops.base.exit = __multiwindow_exit;
1065 raw_ops.base.trim_memory = __multiwindow_trim_memory;
1067 if (!__is_widget_feature_enabled()) {
1068 LOGE("not supported"); /* LCOV_EXCL_LINE */
1069 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1072 kb = bundle_import_from_argv(argc, argv);
1074 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1075 if (viewer_endpoint) {
1076 LOGD("viewer endpoint :%s", viewer_endpoint);
1077 __viewer_endpoint = strdup(viewer_endpoint);
1079 LOGE("endpoint is missing");
1084 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1085 return WIDGET_ERROR_FAULT;
1088 if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1089 return WIDGET_ERROR_FAULT;
1091 return WIDGET_ERROR_NONE;
1094 static int __on_create(void *data)
1096 return widget_base_on_create();
1099 static int __on_terminate(void *data)
1101 return widget_base_on_terminate();
1104 static void __on_init(int argc, char **argv, void *data)
1106 widget_base_on_init(argc, argv);
1109 static void __on_finish(void)
1111 widget_base_on_finish();
1114 static void __on_run(void *data)
1116 widget_base_on_run();
1119 static void __on_exit(void *data)
1121 widget_base_on_exit();
1124 static void __on_trim_memory(void *data)
1126 widget_base_on_trim_memory();
1129 EXPORT_API int widget_base_on_create(void)
1131 appcore_multiwindow_base_on_create();
1136 EXPORT_API int widget_base_on_terminate(void)
1138 appcore_multiwindow_base_on_terminate();
1143 EXPORT_API int widget_base_on_init(int argc, char **argv)
1148 EXPORT_API void widget_base_on_finish(void)
1152 EXPORT_API void widget_base_on_run(void)
1156 EXPORT_API void widget_base_on_exit(void)
1160 EXPORT_API int widget_base_on_trim_memory(void)
1162 appcore_multiwindow_base_on_trim_memory();
1167 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1169 widget_base_ops ops;
1171 /* override methods */
1172 ops.create = __on_create;
1173 ops.terminate = __on_terminate;
1174 ops.init = __on_init;
1175 ops.finish = __on_finish;
1177 ops.exit = __on_exit;
1178 ops.trim_memory = __on_trim_memory;
1183 static void __free_class(gpointer data)
1185 widget_base_class *cls = data;
1191 EXPORT_API void widget_base_fini(void)
1193 appcore_multiwindow_base_fini();
1194 g_list_free_full(__context.classes, __free_class);
1195 __context.classes = NULL;
1198 EXPORT_API int widget_base_context_window_bind(
1199 widget_base_instance_h instance_h, const char *id,
1200 Ecore_Wl2_Window *wl_win)
1202 struct wl_surface *surface;
1204 surface = ecore_wl2_window_surface_get(wl_win);
1205 if (surface == NULL) {
1206 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1207 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1210 screen_connector_provider_remote_enable(id, surface);
1211 appcore_multiwindow_base_window_bind(instance_h, wl_win);
1213 return WIDGET_BASE_ERROR_NONE;
1216 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1217 int w, int h, void *class_data)
1219 return widget_base_class_on_create(instance_h, content, w, h);
1222 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1224 return widget_base_class_on_resume(instance_h);
1227 static int __class_on_pause(widget_base_instance_h instance_h,
1230 return widget_base_class_on_pause(instance_h);
1233 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1236 return widget_base_class_on_resize(instance_h, w, h);
1239 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1240 int force, void *class_data)
1242 return widget_base_class_on_update(instance_h, content, force);
1245 static int __class_on_destroy(widget_base_instance_h instance_h,
1246 widget_base_destroy_type_e reason, bundle *content,
1249 return widget_base_class_on_destroy(instance_h, reason, content);
1252 static void __multiwindow_instance_create(
1253 appcore_multiwindow_base_instance_h instance_h,
1256 widget_base_instance_data *instance_data;
1258 bundle *content_info = NULL;
1260 char *class_id = NULL;
1261 char *operation = NULL;
1262 char *content = NULL;
1265 char *remain = NULL;
1269 widget_base_class *cls;
1270 double *period = NULL;
1273 appcore_multiwindow_base_class_on_create(instance_h);
1274 instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1275 b = instance_data->args;
1277 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1278 /* for previous version compatibility, use appid for default class id */
1279 if (class_id == NULL)
1282 cls = __get_class(class_id);
1284 LOGE("class not found: %s", class_id);
1288 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1289 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1292 LOGE("no operation provided");
1296 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1297 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1298 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1301 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1304 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1307 content_info = bundle_decode((const bundle_raw *)content,
1310 if (cls->ops.create)
1311 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1314 LOGW("Create callback returns error(%d)", ret);
1315 ret = __send_update_status(class_id, id,
1316 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1318 LOGE("Fail to send abort status (%d) ", ret);
1319 __instance_drop(instance_h);
1321 LOGD("%s is created", id);
1322 aul_widget_instance_add(class_id, id);
1323 ret = __send_update_status(class_id, id,
1324 WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1326 LOGE("Fail to send create status (%d) ", ret);
1328 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1330 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1331 LOGI("set periodic update timer (%lf)", *period);
1332 instance_data->period = *period;
1333 instance_data->periodic_timer = g_timeout_add_seconds(
1334 instance_data->period,
1335 __timeout_cb, instance_data);
1340 bundle_free(content_info);
1343 static void __multiwindow_instance_resume(
1344 appcore_multiwindow_base_instance_h instance_h,
1348 const char *class_id;
1349 widget_base_class *cls;
1350 widget_base_instance_data *data;
1352 appcore_multiwindow_base_class_on_resume(instance_h);
1353 id = appcore_multiwindow_base_instance_get_id(instance_h);
1354 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1355 cls = __get_class(class_id);
1357 LOGE("class not found: %s", class_id);
1361 data = (widget_base_instance_data *)
1362 appcore_multiwindow_base_instance_get_extra(instance_h);
1364 if (data->pending_update) {
1365 LOGD("pending update!");
1366 data->pending_update = false;
1367 __call_update_cb(class_id, data->id, 0, data->pending_content);
1368 if (data->period > 0) {
1369 LOGD("Restart timer!");
1370 data->periodic_timer = g_timeout_add_seconds(
1372 __timeout_cb, data);
1376 if (cls->ops.resume)
1377 cls->ops.resume(instance_h, class_data);
1379 LOGD("%s is resumed", id);
1380 __send_update_status(class_id, id,
1381 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1384 LOGD("Send fg signal to resourceD");
1385 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1390 static void __multiwindow_instance_pause(
1391 appcore_multiwindow_base_instance_h instance_h,
1395 const char *class_id;
1396 widget_base_class *cls;
1398 appcore_multiwindow_base_class_on_pause(instance_h);
1399 id = appcore_multiwindow_base_instance_get_id(instance_h);
1400 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1401 cls = __get_class(class_id);
1403 LOGE("class not found: %s", class_id);
1408 cls->ops.pause(instance_h, class_data);
1410 LOGD("%s is paused", id);
1411 __send_update_status(class_id, id,
1412 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1415 LOGD("Send bg signal to resourceD");
1416 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1417 __fg_signal = false;
1421 static void __multiwindow_instance_terminate(
1422 appcore_multiwindow_base_instance_h instance_h,
1425 widget_base_instance_data *data;
1427 char *operation = NULL;
1428 bundle *content_info;
1429 widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1430 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1432 const char *class_id;
1433 widget_base_class *cls;
1435 id = appcore_multiwindow_base_instance_get_id(instance_h);
1436 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1437 data = appcore_multiwindow_base_instance_get_extra(
1438 (appcore_multiwindow_base_instance_h)instance_h);
1440 cls = __get_class(class_id);
1442 LOGE("class not found: %s", class_id);
1447 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1448 if (operation && strcmp(operation, "destroy") == 0)
1449 reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1453 content_info = bundle_decode((const bundle_raw *)data->content,
1454 strlen(data->content));
1456 content_info = bundle_create();
1458 if (cls->ops.destroy)
1459 cls->ops.destroy(instance_h, reason, content_info, class_data);
1461 LOGW("%s is destroyed %d", id, reason);
1462 if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1463 __is_permanent = true;
1464 event = WIDGET_INSTANCE_EVENT_DESTROY;
1465 aul_widget_instance_del(class_id, id);
1467 __is_permanent = false;
1468 __send_update_status(class_id, id,
1469 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1474 bundle_free(content_info);
1476 if (data->periodic_timer)
1477 g_source_remove(data->periodic_timer);
1479 __send_update_status(class_id, id, event, 0, NULL);
1480 appcore_multiwindow_base_class_on_terminate(instance_h);
1483 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1484 bundle *content, int w, int h)
1486 appcore_multiwindow_base_class_on_create(instance_h);
1491 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1493 appcore_multiwindow_base_class_on_pause(instance_h);
1498 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1500 appcore_multiwindow_base_class_on_resume(instance_h);
1505 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1511 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1512 bundle *content, int force)
1517 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1518 widget_base_destroy_type_e reason, bundle *content)
1520 appcore_multiwindow_base_class_on_terminate(instance_h);
1525 EXPORT_API widget_base_class widget_base_class_get_default(void)
1527 widget_base_class cls;
1529 cls.ops.create = __class_on_create;
1530 cls.ops.resize = __class_on_resize;
1531 cls.ops.update = __class_on_update;
1532 cls.ops.destroy = __class_on_destroy;
1533 cls.ops.pause = __class_on_pause;
1534 cls.ops.resume = __class_on_resume;
1540 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1541 const char *class_id, void *class_data)
1543 widget_base_class *c;
1544 appcore_multiwindow_base_class raw_cls;
1546 if (!__is_widget_feature_enabled()) {
1547 LOGE("not supported");
1548 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1553 LOGE("class id is NULL");
1554 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1558 raw_cls.id = strdup(class_id);
1559 raw_cls.data = class_data;
1560 raw_cls.create = __multiwindow_instance_create;
1561 raw_cls.terminate = __multiwindow_instance_terminate;
1562 raw_cls.pause = __multiwindow_instance_pause;
1563 raw_cls.resume = __multiwindow_instance_resume;
1564 appcore_multiwindow_base_class_add(raw_cls);
1566 c = malloc(sizeof(widget_base_class));
1571 c->id = strdup(class_id);
1572 __context.classes = g_list_append(__context.classes, c);
1576 /* LCOV_EXCL_STOP */