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>
25 #include <glib-object.h>
28 #include <widget_errno.h>
29 #include <widget_instance.h>
30 #include <aul_app_com.h>
31 #include <Ecore_Wayland.h>
32 #include <system_info.h>
34 #include <vconf-internal-keys.h>
35 #include <screen_connector_provider.h>
36 #include <appcore_multiwindow_base.h>
38 #include "widget_base.h"
44 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
45 #define APP_TYPE_WIDGET "widgetapp"
46 #define STATUS_FOREGROUND "fg"
47 #define STATUS_BACKGROUND "bg"
49 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
50 [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
51 [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
52 [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
53 [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
54 = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
55 [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
56 [APP_EVENT_SUSPENDED_STATE_CHANGED]
57 = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
60 struct app_event_info {
61 app_event_type_e type;
65 struct app_event_handler {
66 app_event_type_e type;
72 struct widget_foreach_context {
73 widget_base_instance_cb callback;
77 typedef struct _widget_base_context {
83 } widget_base_context;
85 typedef struct _widget_base_instance_data {
94 } widget_base_instance_data;
96 static widget_base_context __context;
98 static char *__package_id;
99 static bool __fg_signal;
100 static char *__viewer_endpoint;
101 static void __call_update_cb(const char *class_id, const char *id, int force,
102 const char *content_raw);
104 static gboolean __timeout_cb(gpointer user_data)
106 widget_base_instance_data *data =
107 (widget_base_instance_data *)user_data;
108 appcore_multiwindow_base_instance_h cxt;
109 const char *class_id;
111 cxt = appcore_multiwindow_base_instance_find(data->id);
112 if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
113 LOGD("Periodic update!");
114 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
115 __call_update_cb(class_id, data->id, 0, NULL);
117 data->pending_update = true;
118 if (data->periodic_timer) {
119 LOGD("Remove timer!");
120 g_source_remove(data->periodic_timer);
121 data->periodic_timer = 0;
125 return G_SOURCE_CONTINUE;
128 static bool __is_widget_feature_enabled(void)
130 static bool feature = false;
131 static bool retrieved = false;
134 if (retrieved == true)
137 ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
138 if (ret != SYSTEM_INFO_ERROR_NONE) {
139 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
140 return false; /* LCOV_EXCL_LINE */
148 /* LCOV_EXCL_START */
149 static void __on_poweroff(keynode_t *key, void *data)
153 val = vconf_keynode_get_int(key);
155 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
156 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
157 LOGI("power off changed: %d", val);
160 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
161 case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
169 static void __check_empty_instance(void)
171 int cnt = appcore_multiwindow_base_instance_get_cnt();
177 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
179 widget_base_instance_data *data;
181 data = appcore_multiwindow_base_instance_get_extra(instance_h);
182 appcore_multiwindow_base_instance_drop(instance_h);
186 __check_empty_instance();
189 static gint __comp_class(gconstpointer a, gconstpointer b)
191 const widget_base_class *cls = a;
193 return strcmp(cls->id, b);
196 static widget_base_class *__get_class(const char *class_id)
198 widget_base_class *cls;
201 class_node = g_list_find_custom(__context.classes, class_id,
203 if (class_node == NULL) {
204 LOGE("empty classes");
207 cls = (widget_base_class *)class_node->data;
212 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
215 bundle *b = bundle_create();
219 LOGE("out of memory"); /* LCOV_EXCL_LINE */
220 return -1; /* LCOV_EXCL_LINE */
223 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
224 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
225 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
226 bundle_add_str(b, AUL_K_PKGID, __package_id);
228 LOGD("send lifecycle %s(%d)", instance_id, status);
229 ret = aul_app_com_send("widget.status", b);
231 LOGE("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
238 static int __send_update_status(const char *class_id, const char *instance_id,
239 int status, bundle *extra)
243 bundle_raw *raw = NULL;
248 LOGE("out of memory"); /* LCOV_EXCL_LINE */
249 return -1; /* LCOV_EXCL_LINE */
252 bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
253 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
254 bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
257 bundle_encode(extra, &raw, &len);
258 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
259 aul_widget_instance_add(class_id, instance_id);
262 LOGD("send update %s(%d) to %s", instance_id, status, __viewer_endpoint);
263 aul_app_com_send(__viewer_endpoint, b);
266 case WIDGET_INSTANCE_EVENT_CREATE:
267 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
269 case WIDGET_INSTANCE_EVENT_DESTROY:
270 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
272 case WIDGET_INSTANCE_EVENT_PAUSE:
273 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
275 case WIDGET_INSTANCE_EVENT_RESUME:
276 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
281 __send_lifecycle_event(class_id, instance_id, lifecycle);
290 static void __control_create(const char *class_id, const char *id, bundle *b)
292 widget_base_instance_data *data;
293 char *content = NULL;
294 double *period = NULL;
298 data = (widget_base_instance_data *)
299 calloc(1, sizeof(widget_base_instance_data));
301 LOGE("Out of memory");
305 data->id = strdup(id);
307 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
308 if (ret == BUNDLE_ERROR_NONE) {
309 data->period = *period;
310 data->periodic_timer = g_timeout_add_seconds(data->period,
314 /* call stub create */
315 appcore_multiwindow_base_instance_run(class_id, id, data);
317 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
319 data->content = strdup(content);
323 static void __control_resume(const char *class_id, const char *id, bundle *b)
325 appcore_multiwindow_base_instance_h cxt;
327 cxt = appcore_multiwindow_base_instance_find(id);
329 LOGE("context not found: %s", id);
333 /* call stub resume */
334 appcore_multiwindow_base_instance_resume(cxt);
337 static void __control_pause(const char *class_id, const char *id, bundle *b)
339 appcore_multiwindow_base_instance_h instance_h;
341 instance_h = appcore_multiwindow_base_instance_find(id);
344 LOGE("instance not found: %s", id);
348 /* call stub pause */
349 appcore_multiwindow_base_instance_pause(instance_h);
352 static void __control_resize(const char *class_id, const char *id, bundle *b)
354 appcore_multiwindow_base_instance_h instance_h;
361 widget_base_class *cls;
362 const appcore_multiwindow_base_class *raw_cls;
364 instance_h = appcore_multiwindow_base_instance_find(id);
366 LOGE("context not found: %s", id);
370 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
374 cls = __get_class(class_id);
376 LOGE("class not found: %s", class_id);
379 class_data = raw_cls->data;
380 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
381 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
384 w = (int)g_ascii_strtoll(w_str, &remain, 10);
387 h = (int)g_ascii_strtoll(h_str, &remain, 10);
390 cls->ops.resize(instance_h, w, h, class_data);
392 LOGD("%s is resized to %dx%d", id, w, h);
393 __send_update_status(class_id, id,
394 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
397 static void __call_update_cb(const char *class_id, const char *id, int force,
398 const char *content_raw)
401 widget_base_class *cls;
402 const appcore_multiwindow_base_class *raw_cls;
403 appcore_multiwindow_base_instance_h instance_h;
404 bundle *content = NULL;
406 instance_h = appcore_multiwindow_base_instance_find(id);
408 LOGE("context not found: %s", id);
412 raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
414 LOGE("class is NULL");
418 class_data = raw_cls->data;
419 cls = __get_class(class_id);
421 LOGE("class not found: %s", class_id);
425 if (!cls->ops.update) {
426 LOGE("update callback is NULL");
431 content = bundle_decode((const bundle_raw *)content_raw,
432 strlen(content_raw));
436 cls->ops.update(instance_h, content, force, class_data);
438 __send_update_status(class_id, id,
439 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
440 LOGD("updated:%s", id);
443 bundle_free(content);
446 static void __update_process(const char *class_id, const char *id,
447 appcore_multiwindow_base_instance_h instance_h, void *data)
449 char *content_raw = NULL;
450 char *force_str = NULL;
455 LOGE("bundle is NULL");
459 bundle_get_str(b, WIDGET_K_FORCE, &force_str);
461 if (force_str && strcmp(force_str, "true") == 0)
466 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
467 __call_update_cb(class_id, id, force, content_raw);
470 static void __control_update(const char *class_id, const char *id, bundle *b)
472 appcore_multiwindow_base_instance_h instance_h;
475 appcore_multiwindow_base_instance_foreach(class_id,
476 __update_process, b);
480 instance_h = appcore_multiwindow_base_instance_find(id);
482 LOGE("context not found: %s", id);
486 __update_process(class_id, id, instance_h, b);
489 static void __control_destroy(const char *class_id, const char *id, bundle *b)
491 appcore_multiwindow_base_instance_h instance_h;
492 widget_base_instance_data *data;
494 instance_h = appcore_multiwindow_base_instance_find(id);
496 LOGE("could not find widget obj: %s, clear amd info", id);
497 aul_widget_instance_del(class_id, id);
501 data = (widget_base_instance_data *)
502 appcore_multiwindow_base_instance_get_extra(instance_h);
505 /* call stub terminate */
506 appcore_multiwindow_base_instance_exit(instance_h);
510 __check_empty_instance();
513 static void __control_change_period(const char *class_id, const char *id,
516 appcore_multiwindow_base_instance_h instance_h;
517 widget_base_instance_data *data;
518 double *period = NULL;
522 instance_h = appcore_multiwindow_base_instance_find(id);
524 LOGE("context not found: %s", id);
528 data = (widget_base_instance_data *)
529 appcore_multiwindow_base_instance_get_extra(instance_h);
532 LOGE("could not find instance data: %s", id);
536 if (data->periodic_timer) {
537 LOGD("Remove timer!");
538 g_source_remove(data->periodic_timer);
539 data->periodic_timer = 0;
542 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
543 if (ret == BUNDLE_ERROR_NONE)
544 data->period = *period;
546 if (data->period > 0) {
547 LOGD("Restart timer!");
548 data->periodic_timer = g_timeout_add_seconds(data->period,
555 static int __multiwindow_create(void *data)
557 char pkgid[256] = {0, };
560 appcore_multiwindow_base_on_create();
561 app_get_id(&__appid);
562 if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
563 __package_id = strdup(pkgid);
565 if (!__package_id || !__appid) {
566 LOGE("__package_id is NULL");
570 screen_connector_provider_init();
571 vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
572 __on_poweroff, NULL);
575 if (__context.ops.create)
576 ret = __context.ops.create(data);
578 LOGD("widget base is created");
582 static int __multiwindow_terminate(void *data)
584 if (__context.ops.terminate)
585 __context.ops.terminate(data);
587 vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
589 screen_connector_provider_fini();
591 if (__viewer_endpoint) {
592 free(__viewer_endpoint);
593 __viewer_endpoint = NULL;
606 appcore_multiwindow_base_on_terminate();
608 LOGD("widget base is terminated");
612 static int __multiwindow_control(bundle *b, void *data)
614 char *class_id = NULL;
616 char *operation = NULL;
618 appcore_multiwindow_base_on_control(b);
619 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
620 /* for previous version compatibility, use appid for default class id */
621 if (class_id == NULL)
624 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
625 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
628 LOGE("operation is NULL");
632 if (strcmp(operation, "create") == 0)
633 __control_create(class_id, id, b);
634 else if (strcmp(operation, "resize") == 0)
635 __control_resize(class_id, id, b);
636 else if (strcmp(operation, "update") == 0)
637 __control_update(class_id, id, b);
638 else if (strcmp(operation, "destroy") == 0)
639 __control_destroy(class_id, id, b);
640 else if (strcmp(operation, "resume") == 0)
641 __control_resume(class_id, id, b);
642 else if (strcmp(operation, "pause") == 0)
643 __control_pause(class_id, id, b);
644 else if (strcmp(operation, "terminate") == 0)
645 __control_destroy(class_id, id, b);
646 else if (strcmp(operation, "period") == 0)
647 __control_change_period(class_id, id, b);
652 static void __inst_resume_cb(const char *class_id, const char *id,
653 appcore_multiwindow_base_instance_h cxt, void *data)
655 __control_resume(class_id, id, data);
658 static void __get_content(bundle *b)
660 char *instance_id = NULL;
661 appcore_multiwindow_base_instance_h cxt;
662 widget_base_instance_data * we;
664 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
666 LOGE("instance id is NULL");
670 cxt = appcore_multiwindow_base_instance_find(instance_id);
672 LOGE("could not find widget obj: %s", instance_id);
676 we = appcore_multiwindow_base_instance_get_extra(cxt);
678 LOGE("widget extra is NULL");
683 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
684 LOGD("content info of %s found", instance_id);
686 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
687 LOGD("empty content info added");
691 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
693 appcore_multiwindow_base_on_receive(type, b);
697 appcore_multiwindow_base_instance_foreach_full(
698 __inst_resume_cb, b);
703 case AUL_WIDGET_CONTENT:
713 static void __multiwindow_init(int argc, char **argv, void *data)
715 if (__context.ops.init)
716 __context.ops.init(argc, argv, data);
719 static void __multiwindow_finish(void)
721 if (__context.ops.finish) {
722 __context.ops.finish();
723 /* Check Loader case */
724 if (getenv("AUL_LOADER_INIT")) {
725 unsetenv("AUL_LOADER_INIT");
726 __context.ops.finish();
731 static void __multiwindow_run(void *data)
733 if (__context.ops.run)
734 __context.ops.run(data);
737 static void __multiwindow_exit(void *data)
739 if (__context.ops.exit)
740 __context.ops.exit(data);
743 EXPORT_API int widget_base_exit(void)
745 appcore_multiwindow_base_exit();
751 static gboolean __finish_event_cb(gpointer user_data)
753 appcore_multiwindow_base_instance_h cxt = user_data;
756 const char *class_id;
759 LOGE("user_data is NULL");
763 id = appcore_multiwindow_base_instance_get_id(cxt);
764 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
768 LOGE("Out-of-memory");
772 bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
773 __control_destroy(class_id, id, b);
779 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
781 if (!__is_widget_feature_enabled()) {
782 LOGE("not supported"); /* LCOV_EXCL_LINE */
783 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
787 LOGE("context is null");
788 return WIDGET_ERROR_INVALID_PARAMETER;
791 g_idle_add(__finish_event_cb, context);
793 return WIDGET_ERROR_NONE;
796 static void __inst_full_cb(const char *class_id, const char *id,
797 appcore_multiwindow_base_instance_h cxt, void *data)
799 struct widget_foreach_context *foreach_context = data;
804 if (foreach_context->callback)
805 foreach_context->callback(cxt, foreach_context->data);
808 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
810 struct widget_foreach_context foreach_context;
812 if (!__is_widget_feature_enabled()) {
813 LOGE("not supported"); /* LCOV_EXCL_LINE */
814 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
818 LOGE("callback is NULL");
819 return WIDGET_ERROR_INVALID_PARAMETER;
822 foreach_context.callback = cb;
823 foreach_context.data = data;
824 appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
826 return WIDGET_ERROR_NONE;
829 static int __event_cb(void *event, void *data)
831 app_event_handler_h handler = data;
833 struct app_event_info app_event;
835 app_event.type = handler->type;
836 app_event.value = event;
839 handler->cb(&app_event, handler->data);
844 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
845 app_event_type_e event_type,
846 app_event_cb callback,
851 app_event_handler_h handler;
853 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
855 return WIDGET_BASE_ERROR_FAULT;
858 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
860 if (event_handler == NULL || callback == NULL)
861 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
863 if (event_type < APP_EVENT_LOW_MEMORY
864 || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
865 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
867 if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
868 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
871 handler = calloc(1, sizeof(struct app_event_handler));
873 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
875 handler->type = event_type;
876 handler->cb = callback;
877 handler->data = user_data;
878 handler->raw = appcore_base_add_event(
879 __app_event_converter[event_type], __event_cb, handler);
880 *event_handler = handler;
882 return WIDGET_BASE_ERROR_NONE;
885 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
890 app_event_type_e type;
892 r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
894 return WIDGET_BASE_ERROR_FAULT;
897 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
899 if (event_handler == NULL)
900 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
902 type = event_handler->type;
903 if (type < APP_EVENT_LOW_MEMORY ||
904 type > APP_EVENT_REGION_FORMAT_CHANGED)
905 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
907 r = appcore_base_remove_event(event_handler->raw);
909 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
913 return WIDGET_BASE_ERROR_NONE;
916 EXPORT_API int widget_base_context_set_content_info(
917 widget_base_instance_h context,
918 bundle *content_info)
921 bundle_raw *raw = NULL;
924 const char *class_id;
925 widget_base_instance_data *data;
926 appcore_multiwindow_base_instance_h instance_h;
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 || !content_info)
934 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
936 instance_h = (appcore_multiwindow_base_instance_h)context;
937 id = appcore_multiwindow_base_instance_get_id(instance_h);
938 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
939 data = appcore_multiwindow_base_instance_get_extra(instance_h);
941 if (!class_id || !id || !data)
942 return WIDGET_BASE_ERROR_FAULT;
944 ret = __send_update_status(class_id, id,
945 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
950 bundle_encode(content_info, &raw, &len);
952 data->content = strdup((const char *)raw);
954 data->content = NULL;
958 /* LCOV_EXCL_START */
959 LOGE("failed to send content info: %s of %s (%d)", id,
961 return WIDGET_BASE_ERROR_IO_ERROR;
965 return WIDGET_BASE_ERROR_NONE;
968 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
970 appcore_multiwindow_base_instance_h instance_h;
971 widget_base_instance_data *data;
973 if (!__is_widget_feature_enabled()) {
974 LOGE("not supported"); /* LCOV_EXCL_LINE */
975 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
978 if (!context || !tag) {
979 LOGE("Invalid parameter");
980 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
983 instance_h = (appcore_multiwindow_base_instance_h)context;
984 data = (widget_base_instance_data *)
985 appcore_multiwindow_base_instance_get_extra(instance_h);
988 LOGE("Invalid parameter");
989 return WIDGET_ERROR_INVALID_PARAMETER;
994 return WIDGET_BASE_ERROR_NONE;
997 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
999 appcore_multiwindow_base_instance_h instance_h;
1000 widget_base_instance_data *data;
1002 if (!__is_widget_feature_enabled()) {
1003 LOGE("not supported"); /* LCOV_EXCL_LINE */
1004 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1008 LOGE("Invalid parameter");
1009 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1012 instance_h = (appcore_multiwindow_base_instance_h)context;
1013 data = (widget_base_instance_data *)
1014 appcore_multiwindow_base_instance_get_extra(instance_h);
1017 return WIDGET_BASE_ERROR_NONE;
1020 EXPORT_API void *widget_base_context_get_user_data(
1021 widget_base_instance_h context)
1023 appcore_multiwindow_base_instance_h instance_h;
1024 widget_base_instance_data *data;
1026 if (!__is_widget_feature_enabled()) {
1027 LOGE("not supported"); /* LCOV_EXCL_LINE */
1028 return NULL; /* LCOV_EXCL_LINE */
1032 LOGE("Invalid parameter");
1036 instance_h = (appcore_multiwindow_base_instance_h)context;
1037 data = (widget_base_instance_data *)
1038 appcore_multiwindow_base_instance_get_extra(instance_h);
1040 return data->user_data;
1044 EXPORT_API int widget_base_context_set_user_data(
1045 widget_base_instance_h context, void *user_data)
1047 appcore_multiwindow_base_instance_h instance_h;
1048 widget_base_instance_data *data;
1050 if (!__is_widget_feature_enabled()) {
1051 LOGE("not supported"); /* LCOV_EXCL_LINE */
1052 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1056 LOGE("Invalid parameter");
1057 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1060 instance_h = (appcore_multiwindow_base_instance_h)context;
1061 data = (widget_base_instance_data *)
1062 appcore_multiwindow_base_instance_get_extra(instance_h);
1063 data->user_data = user_data;
1065 return WIDGET_BASE_ERROR_NONE;
1068 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1070 appcore_multiwindow_base_instance_h instance_h;
1072 if (!__is_widget_feature_enabled()) {
1073 LOGE("not supported"); /* LCOV_EXCL_LINE */
1074 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1077 instance_h = (appcore_multiwindow_base_instance_h)context;
1078 *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1080 return WIDGET_BASE_ERROR_NONE;
1083 EXPORT_API const char *widget_base_get_viewer_endpoint()
1085 return __viewer_endpoint;
1088 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1092 char *viewer_endpoint = NULL;
1093 appcore_multiwindow_base_ops raw_ops
1094 = appcore_multiwindow_base_get_default_ops();
1096 __context.ops = ops;
1097 __context.argc = argc;
1098 __context.argv = argv;
1099 __context.data = data;
1101 /* override methods */
1102 raw_ops.base.create = __multiwindow_create;
1103 raw_ops.base.control = __multiwindow_control;
1104 raw_ops.base.terminate = __multiwindow_terminate;
1105 raw_ops.base.receive = __multiwindow_receive;
1106 raw_ops.base.init = __multiwindow_init;
1107 raw_ops.base.finish = __multiwindow_finish;
1108 raw_ops.base.run = __multiwindow_run;
1109 raw_ops.base.exit = __multiwindow_exit;
1111 if (!__is_widget_feature_enabled()) {
1112 LOGE("not supported"); /* LCOV_EXCL_LINE */
1113 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1116 kb = bundle_import_from_argv(argc, argv);
1118 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
1119 if (viewer_endpoint) {
1120 LOGD("viewer endpoint :%s", viewer_endpoint);
1121 __viewer_endpoint = strdup(viewer_endpoint);
1123 LOGE("endpoint is missing");
1128 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1129 return WIDGET_ERROR_FAULT;
1132 if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1133 return WIDGET_ERROR_FAULT;
1135 return WIDGET_ERROR_NONE;
1138 static int __on_create(void *data)
1140 return widget_base_on_create();
1143 static int __on_terminate(void *data)
1145 return widget_base_on_terminate();
1148 static void __on_init(int argc, char **argv, void *data)
1150 widget_base_on_init(argc, argv);
1153 static void __on_finish(void)
1155 widget_base_on_finish();
1158 static void __on_run(void *data)
1160 widget_base_on_run();
1163 static void __on_exit(void *data)
1165 widget_base_on_exit();
1168 EXPORT_API int widget_base_on_create(void)
1170 appcore_multiwindow_base_on_create();
1175 EXPORT_API int widget_base_on_terminate(void)
1177 appcore_multiwindow_base_on_terminate();
1182 EXPORT_API int widget_base_on_init(int argc, char **argv)
1187 EXPORT_API void widget_base_on_finish(void)
1191 EXPORT_API void widget_base_on_run(void)
1195 EXPORT_API void widget_base_on_exit(void)
1199 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1201 widget_base_ops ops;
1203 /* override methods */
1204 ops.create = __on_create;
1205 ops.terminate = __on_terminate;
1206 ops.init = __on_init;
1207 ops.finish = __on_finish;
1209 ops.exit = __on_exit;
1214 static void __free_class(gpointer data)
1216 widget_base_class *cls = data;
1222 EXPORT_API void widget_base_fini(void)
1224 g_list_free_full(__context.classes, __free_class);
1225 __context.classes = NULL;
1227 appcore_multiwindow_base_fini();
1230 EXPORT_API int widget_base_context_window_bind(
1231 widget_base_instance_h instance_h, const char *id,
1232 Ecore_Wl_Window *wl_win)
1234 struct wl_surface *surface;
1236 surface = ecore_wl_window_surface_get(wl_win);
1237 if (surface == NULL) {
1238 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1239 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1242 screen_connector_provider_remote_enable(id, surface);
1243 appcore_multiwindow_base_window_bind(instance_h, wl_win);
1245 return WIDGET_BASE_ERROR_NONE;
1248 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1249 int w, int h, void *class_data)
1251 return widget_base_class_on_create(instance_h, content, w, h);
1254 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1256 return widget_base_class_on_resume(instance_h);
1259 static int __class_on_pause(widget_base_instance_h instance_h,
1262 return widget_base_class_on_pause(instance_h);
1265 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1268 return widget_base_class_on_resize(instance_h, w, h);
1271 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1272 int force, void *class_data)
1274 return widget_base_class_on_update(instance_h, content, force);
1277 static int __class_on_destroy(widget_base_instance_h instance_h,
1278 widget_base_destroy_type_e reason, bundle *content,
1281 return widget_base_class_on_destroy(instance_h, reason, content);
1284 static void __multiwindow_instance_create(
1285 appcore_multiwindow_base_instance_h instance_h,
1288 widget_base_instance_data *instance_data;
1290 bundle *content_info = NULL;
1292 char *class_id = NULL;
1293 char *operation = NULL;
1294 char *content = NULL;
1297 char *remain = NULL;
1301 widget_base_class *cls;
1303 appcore_multiwindow_base_class_on_create(instance_h);
1304 instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1305 b = instance_data->args;
1307 bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1308 /* for previous version compatibility, use appid for default class id */
1309 if (class_id == NULL)
1312 cls = __get_class(class_id);
1314 LOGE("class not found: %s", class_id);
1318 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1319 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1322 LOGE("no operation provided");
1326 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1327 bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1328 bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1331 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1334 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1337 content_info = bundle_decode((const bundle_raw *)content,
1340 if (cls->ops.create)
1341 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1344 LOGW("Create callback returns error(%d)", ret);
1345 ret = __send_update_status(class_id, id,
1346 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
1347 __instance_drop(instance_h);
1349 LOGD("%s is created", id);
1350 ret = __send_update_status(class_id, id,
1351 WIDGET_INSTANCE_EVENT_CREATE, NULL);
1353 aul_widget_instance_add(class_id, id);
1357 bundle_free(content_info);
1360 static void __multiwindow_instance_resume(
1361 appcore_multiwindow_base_instance_h instance_h,
1365 const char *class_id;
1366 widget_base_class *cls;
1367 widget_base_instance_data *data;
1369 appcore_multiwindow_base_class_on_resume(instance_h);
1370 id = appcore_multiwindow_base_instance_get_id(instance_h);
1371 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1372 cls = __get_class(class_id);
1374 LOGE("class not found: %s", class_id);
1378 data = (widget_base_instance_data *)
1379 appcore_multiwindow_base_instance_get_extra(instance_h);
1381 if (data->pending_update) {
1382 LOGD("pending update!");
1383 data->pending_update = false;
1384 __control_update(class_id, data->id, data->args);
1385 if (data->period > 0) {
1386 LOGD("Restart timer!");
1387 data->periodic_timer = g_timeout_add_seconds(
1389 __timeout_cb, data);
1393 if (cls->ops.resume)
1394 cls->ops.resume(instance_h, class_data);
1396 LOGD("%s is resumed", id);
1397 __send_update_status(class_id, id,
1398 WIDGET_INSTANCE_EVENT_RESUME, NULL);
1401 LOGD("Send fg signal to resourceD");
1402 aul_send_app_status_change_signal(getpid(),
1411 static void __multiwindow_instance_pause(
1412 appcore_multiwindow_base_instance_h instance_h,
1416 const char *class_id;
1417 widget_base_class *cls;
1419 appcore_multiwindow_base_class_on_pause(instance_h);
1420 id = appcore_multiwindow_base_instance_get_id(instance_h);
1421 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1422 cls = __get_class(class_id);
1424 LOGE("class not found: %s", class_id);
1429 cls->ops.pause(instance_h, class_data);
1431 LOGD("%s is paused", id);
1432 __send_update_status(class_id, id,
1433 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
1436 LOGD("Send bg signal to resourceD");
1437 aul_send_app_status_change_signal(getpid(),
1442 __fg_signal = false;
1446 static void __multiwindow_instance_terminate(
1447 appcore_multiwindow_base_instance_h instance_h,
1450 widget_base_instance_data *data;
1452 char *operation = NULL;
1453 bundle *content_info;
1454 widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1455 int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1457 const char *class_id;
1458 widget_base_class *cls;
1460 id = appcore_multiwindow_base_instance_get_id(instance_h);
1461 class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1462 data = appcore_multiwindow_base_instance_get_extra(
1463 (appcore_multiwindow_base_instance_h)instance_h);
1465 cls = __get_class(class_id);
1467 LOGE("class not found: %s", class_id);
1472 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1473 if (operation && strcmp(operation, "destroy") == 0)
1474 reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1478 content_info = bundle_decode((const bundle_raw *)data->content,
1479 strlen(data->content));
1481 content_info = bundle_create();
1483 if (cls->ops.destroy)
1484 cls->ops.destroy(instance_h, reason, content_info, class_data);
1486 LOGD("%s is destroyed %d", id, reason);
1487 if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1488 event = WIDGET_INSTANCE_EVENT_DESTROY;
1489 aul_widget_instance_del(class_id, id);
1491 __send_update_status(class_id, id,
1492 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED,
1497 bundle_free(content_info);
1499 if (data->periodic_timer)
1500 g_source_remove(data->periodic_timer);
1502 __send_update_status(class_id, id, event, NULL);
1503 appcore_multiwindow_base_class_on_terminate(instance_h);
1506 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1507 bundle *content, int w, int h)
1509 appcore_multiwindow_base_class_on_create(instance_h);
1514 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1516 appcore_multiwindow_base_class_on_pause(instance_h);
1521 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1523 appcore_multiwindow_base_class_on_resume(instance_h);
1528 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1534 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1535 bundle *content, int force)
1540 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1541 widget_base_destroy_type_e reason, bundle *content)
1543 appcore_multiwindow_base_class_on_terminate(instance_h);
1548 EXPORT_API widget_base_class widget_base_class_get_default(void)
1550 widget_base_class cls;
1552 cls.ops.create = __class_on_create;
1553 cls.ops.resize = __class_on_resize;
1554 cls.ops.update = __class_on_update;
1555 cls.ops.destroy = __class_on_destroy;
1556 cls.ops.pause = __class_on_pause;
1557 cls.ops.resume = __class_on_resume;
1562 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1563 const char *class_id, void *class_data)
1565 widget_base_class *c;
1566 appcore_multiwindow_base_class raw_cls;
1568 if (!__is_widget_feature_enabled()) {
1569 LOGE("not supported");
1570 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1575 LOGE("class is is NULL");
1576 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1580 raw_cls.id = strdup(class_id);
1581 raw_cls.data = class_data;
1582 raw_cls.create = __multiwindow_instance_create;
1583 raw_cls.terminate = __multiwindow_instance_terminate;
1584 raw_cls.pause = __multiwindow_instance_pause;
1585 raw_cls.resume = __multiwindow_instance_resume;
1586 appcore_multiwindow_base_class_add(raw_cls);
1588 c = malloc(sizeof(widget_base_class));
1593 c->id = strdup(class_id);
1594 __context.classes = g_list_append(__context.classes, c);