Handle widget resize event
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdlib.h>
19 #include <stdbool.h>
20
21 #include <bundle.h>
22 #include <bundle_internal.h>
23 #include <aul.h>
24 #include <dlog.h>
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <stdlib.h>
28 #include <app_control.h>
29 #include <app_control_internal.h>
30 #include <Elementary.h>
31 #include <widget_errno.h>
32 #include <widget_instance.h>
33 #include <widget_service.h>
34 #include <widget_service_internal.h>
35 #include <aul_app_com.h>
36 #include <Ecore_Wayland.h>
37 #include <system_info.h>
38 #include <vconf.h>
39 #include <vconf-internal-keys.h>
40 #include <screen_connector_provider.h>
41
42 #include "widget_app.h"
43 #include "widget-log.h"
44 #include "widget-private.h"
45 #include "widget_app_internal.h"
46
47 #ifdef LOG_TAG
48 #undef LOG_TAG
49 #endif
50
51 #define STR_MAX_BUF 128
52 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
53 #define K_REASON    "__WC_K_REASON__"
54 #define APP_TYPE_WIDGET "widgetapp"
55 #define STATUS_FOREGROUND "fg"
56 #define STATUS_BACKGROUND "bg"
57
58 typedef enum _widget_obj_state_e {
59         WC_READY = 0,
60         WC_RUNNING = 1,
61         WC_PAUSED = 2,
62         WC_TERMINATED = 3
63 } widget_obj_state_e;
64
65 enum {
66         UPDATE_LOCAL = 0,
67         UPDATE_ALL = 1,
68 };
69
70 struct app_event_handler {
71         app_event_type_e type;
72         app_event_cb cb;
73         void *data;
74 };
75
76 struct app_event_info {
77         app_event_type_e type;
78         void *value;
79 };
80
81 typedef struct _widget_class widget_class_s;
82
83 #define WIDGET_APP_EVENT_MAX 5
84 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
85
86 static int caller_pid;
87 static widget_app_lifecycle_callback_s *app_ops;
88 static void *app_user_data;
89 static char *appid;
90 static widget_class_h class_provider;
91 static int exit_called;
92 static char *package_id;
93 static bool fg_signal;
94
95 static void _widget_core_set_appcore_event_cb(void);
96 static void _widget_core_unset_appcore_event_cb(void);
97
98 static void __free_handler_cb(gpointer data)
99 {
100         if (data)
101                 free(data);
102 }
103
104 static void __free_handler_list(void)
105 {
106         int i;
107
108         for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
109                 g_list_free_full(handler_list[i], __free_handler_cb);
110                 handler_list[i] = NULL;
111         }
112 }
113
114 static inline bool _is_widget_feature_enabled(void)
115 {
116         static bool feature = false;
117         static bool retrieved = false;
118         int ret;
119
120         if (retrieved == true)
121                 return feature;
122
123         ret = system_info_get_platform_bool(
124                         "http://tizen.org/feature/shell.appwidget", &feature);
125         if (ret != SYSTEM_INFO_ERROR_NONE) {
126                 _E("failed to get system info"); /* LCOV_EXCL_LINE */
127                 return false; /* LCOV_EXCL_LINE */
128         }
129
130         retrieved = true;
131
132         return feature;
133 }
134
135 static gint __comp_by_id(gconstpointer a, gconstpointer b)
136 {
137         widget_context_s *wc = (widget_context_s *)a;
138
139         return strcmp(wc->id, (const char *)b);
140 }
141
142 static widget_context_s *__find_context_by_id(const char *id)
143 {
144         GList *ret;
145         GList *contexts = _widget_app_get_contexts();
146
147         if (id == NULL)
148                 return NULL;
149
150         ret = g_list_find_custom(contexts, id, __comp_by_id);
151         if (ret == NULL)
152                 return NULL;
153
154         return ret->data;
155 }
156
157 static gint __comp_by_state(gconstpointer a, gconstpointer b)
158 {
159         widget_context_s *wc = (widget_context_s *)a;
160
161         if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
162                 return 0;
163
164         return -1;
165 }
166
167 static widget_context_s *__find_context_by_state(widget_obj_state_e state)
168 {
169         GList *ret;
170         GList *contexts = _widget_app_get_contexts();
171
172         ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
173         if (ret == NULL)
174                 return NULL;
175
176         return ret->data;
177 }
178
179 static gint __comp_by_win(gconstpointer a, gconstpointer b)
180 {
181         int win = GPOINTER_TO_INT(b);
182         widget_context_s *wc = (widget_context_s *)a;
183
184         return (wc && wc->win_id == win) ? 0 : -1;
185 }
186
187 static widget_context_s *__find_context_by_win(int win)
188 {
189         GList *contexts = _widget_app_get_contexts();
190         GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
191
192         if (ret == NULL)
193                 return NULL;
194
195         return ret->data;
196 }
197
198 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
199         int status)
200 {
201         bundle *b = bundle_create();
202         char pkgid[256] = {0, };
203         int ret;
204
205         if (b == NULL) {
206                 _E("out of memory"); /* LCOV_EXCL_LINE */
207                 return -1; /* LCOV_EXCL_LINE */
208         }
209
210         if (package_id == NULL) {
211                 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
212                 if (ret == 0)
213                         package_id = strdup(pkgid);
214         }
215
216         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
217         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
218         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
219         if (package_id)
220                 bundle_add_str(b, AUL_K_PKGID, package_id);
221
222         _D("send lifecycle %s(%d)", instance_id, status);
223         ret = aul_app_com_send("widget.status", b);
224         if (ret < 0)
225                 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
226
227         bundle_free(b);
228
229         return ret;
230 }
231
232 static int __send_update_status(const char *class_id, const char *instance_id,
233         int status, bundle *extra)
234 {
235         bundle *b;
236         int lifecycle = -1;
237         bundle_raw *raw = NULL;
238         int len;
239         char *viewer_endpoint = _widget_app_get_viewer_endpoint();
240
241         b = bundle_create();
242         if (!b) {
243                 _E("out of memory"); /* LCOV_EXCL_LINE */
244                 return -1; /* LCOV_EXCL_LINE */
245         }
246
247         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
248         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
249         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
250
251         if (extra) {
252                 bundle_encode(extra, &raw, &len);
253                 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
254                 aul_widget_instance_add(class_id, instance_id);
255         }
256
257         _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
258         aul_app_com_send(viewer_endpoint, b);
259
260         switch (status) {
261         case WIDGET_INSTANCE_EVENT_CREATE:
262                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
263                 break;
264         case WIDGET_INSTANCE_EVENT_DESTROY:
265                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
266                 break;
267         case WIDGET_INSTANCE_EVENT_PAUSE:
268                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
269                 break;
270         case WIDGET_INSTANCE_EVENT_RESUME:
271                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
272                 break;
273         }
274
275         if (lifecycle > -1)
276                 __send_lifecycle_event(class_id, instance_id, lifecycle);
277
278         bundle_free(b);
279         if (raw)
280                 free(raw);
281
282         return 0;
283 }
284
285 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
286 {
287         widget_context_s *wc = __find_context_by_id(id);
288         int ret;
289
290         if (!wc) {
291                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
292                 return -1; /* LCOV_EXCL_LINE */
293         }
294
295         if (wc->state == WC_RUNNING) {
296                 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
297                 return 0; /* LCOV_EXCL_LINE */
298         }
299
300         if (wc->state == WC_TERMINATED) {
301                 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
302                 return 0; /* LCOV_EXCL_LINE */
303         }
304
305         if (handle->ops.resume)
306                 handle->ops.resume(wc, handle->user_data);
307
308         wc->state = WC_RUNNING;
309         _D("%s is resumed", id);
310         if (send_update) {
311                 ret = __send_update_status(handle->classid, wc->id,
312                         WIDGET_INSTANCE_EVENT_RESUME, NULL);
313                 if (!fg_signal) {
314                         _D("Send fg signal to resourceD");
315                         aul_send_app_status_change_signal(getpid(),
316                                         appid,
317                                         package_id,
318                                         STATUS_FOREGROUND,
319                                         APP_TYPE_WIDGET);
320                         fg_signal = true;
321                 }
322         } else {
323                 ret = 0;
324         }
325
326         return ret;
327 }
328
329 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
330 {
331         widget_context_s *wc = __find_context_by_id(id);
332         int ret;
333
334         if (!wc) {
335                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
336                 return -1; /* LCOV_EXCL_LINE */
337         }
338
339         if (wc->state == WC_PAUSED) {
340                 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
341                 return 0; /* LCOV_EXCL_LINE */
342         }
343
344         if (wc->state == WC_TERMINATED) {
345                 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
346                 return 0; /* LCOV_EXCL_LINE */
347         }
348
349         if (handle->ops.pause)
350                 handle->ops.pause(wc, handle->user_data);
351
352         wc->state = WC_PAUSED;
353         _D("%s is paused", id);
354         if (send_update) {
355                 ret = __send_update_status(handle->classid, wc->id,
356                         WIDGET_INSTANCE_EVENT_PAUSE, NULL);
357                 wc = __find_context_by_state(WC_RUNNING);
358                 if (!wc && fg_signal) {
359                         _D("Send bg signal to resourceD");
360                         aul_send_app_status_change_signal(getpid(),
361                                         appid,
362                                         package_id,
363                                         STATUS_BACKGROUND,
364                                         APP_TYPE_WIDGET);
365                         fg_signal = false;
366                 }
367         } else {
368                 ret = 0;
369         }
370
371         return ret;
372 }
373
374 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
375 {
376         widget_context_s *wc = __find_context_by_id(id);
377         int ret;
378
379         if (!wc) {
380                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
381                 return -1; /* LCOV_EXCL_LINE */
382         }
383
384         if (handle->ops.resize)
385                 handle->ops.resize(wc, w, h, handle->user_data);
386
387         _D("%s is resized to %dx%d", id, w, h);
388         ret = __send_update_status(handle->classid, wc->id,
389                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
390
391         return ret;
392 }
393
394 /* LCOV_EXCL_START */
395 static int __instance_update_all(widget_class_h handle, int force, const char *content)
396 {
397         widget_context_s *wc;
398         int ret = 0;
399         bundle *b = NULL;
400         GList *context = _widget_app_get_contexts();
401
402         if (content)
403                 b = bundle_decode((const bundle_raw *)content, strlen(content));
404
405         if (handle->ops.update) {
406                 while (context) {
407                         wc = (widget_context_s *)context->data;
408                         handle->ops.update(wc, b, force, handle->user_data);
409                         ret = __send_update_status(handle->classid, wc->id,
410                                 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
411                         _D("updated:%s", wc->id);
412                         context = context->next;
413                 }
414         }
415
416         if (b)
417                 bundle_free(b);
418
419         return ret;
420 }
421 /* LCOV_EXCL_STOP */
422
423 /* LCOV_EXCL_START */
424 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
425 {
426         widget_context_s *wc = __find_context_by_id(id);
427         int ret = 0;
428         bundle *b = NULL;
429         if (!wc) {
430                 _E("context not found: %s", id);
431                 return -1;
432         }
433
434         if (content)
435                 b = bundle_decode((const bundle_raw *)content, strlen(content));
436
437         if (handle->ops.update) {
438                 handle->ops.update(wc, b, force, handle->user_data);
439                 ret = __send_update_status(handle->classid, wc->id,
440                         WIDGET_INSTANCE_EVENT_UPDATE, NULL);
441                 _D("updated:%s", id);
442         }
443
444         if (b)
445                 bundle_free(b);
446
447         return ret;
448 }
449 /* LCOV_EXCL_STOP */
450
451 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
452 {
453         widget_context_s *wc = NULL;
454         int ret = 0;
455         bundle *content_info = NULL;
456
457         wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
458         if (!wc) {
459                 _E("Out of memory");
460                 return WIDGET_ERROR_OUT_OF_MEMORY;
461         }
462
463         wc->state = WC_READY;
464         wc->id = strdup(id);
465         wc->provider = handle;
466         wc->win = NULL;
467         wc->win_id = -1;
468
469         if (content) {
470                 wc->content = strdup(content);
471                 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
472         }
473
474         _widget_app_add_context(wc);
475
476         ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
477         if (ret < 0) {
478                 _W("Create callback resturns error(%d)", ret);
479                 /* TODO send abort */
480         } else {
481                 ret = __send_update_status(handle->classid, wc->id,
482                         WIDGET_INSTANCE_EVENT_CREATE, NULL);
483
484                 if (content == NULL)
485                         content = "NULL";
486
487                 aul_widget_instance_add(handle->classid, id);
488         }
489
490         if (content_info)
491                 bundle_free(content_info);
492
493         return ret;
494 }
495
496 static int __instance_destroy(widget_class_h handle, const char *id,
497                 widget_app_destroy_type_e reason, int send_update)
498 {
499         widget_context_s *wc = __find_context_by_id(id);
500         int ret = 0;
501         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
502         bundle *content_info;
503
504         if (!wc) {
505                 _E("could not find widget obj: %s, clear amd info", id);        /* LCOV_EXCL_LINE */
506                 aul_widget_instance_del(handle->classid, id);                   /* LCOV_EXCL_LINE */
507                 return WIDGET_ERROR_NONE;                                       /* LCOV_EXCL_LINE */
508         }
509
510         wc->state = WC_TERMINATED;
511         if (wc->content)
512                 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
513         else
514                 content_info = bundle_create();
515
516         handle->ops.destroy(wc, reason, content_info,
517                         handle->user_data);
518
519         if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
520                 event = WIDGET_INSTANCE_EVENT_DESTROY;
521                 aul_widget_instance_del(handle->classid, id);
522         } else {
523                 ret = __send_update_status(handle->classid, id,
524                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
525         }
526
527         if (content_info)
528                 bundle_free(content_info);
529
530         ret = __send_update_status(handle->classid, id, event, NULL);
531
532         _widget_app_remove_context(wc);
533
534         if (wc->id)
535                 free(wc->id);
536
537         if (wc->content)
538                 free(wc->content);
539
540         free(wc);
541
542         if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
543                 widget_app_exit();
544
545         return ret;
546 }
547
548 static widget_class_h __find_class_handler(const char *class_id,
549                 widget_class_h handle)
550 {
551         if (!class_id || !handle)
552                 return NULL;
553
554         widget_class_h head = handle;
555
556         while (head) {
557                 if (head->classid && strcmp(head->classid, class_id) == 0)
558                         return head;
559
560                 head = head->next;
561         }
562
563         return NULL;
564 }
565
566 /* LCOV_EXCL_START */
567 static void __resize_window(char *id, int w, int h)
568 {
569         widget_context_s *wc = __find_context_by_id(id);
570
571         if (!wc) {
572                 _E("can not find instance: %s", id);
573                 return;
574         }
575
576         if (wc->win)
577                 evas_object_resize(wc->win, w, h);
578         else
579                 _E("unable to find window of %d", wc->id);
580 }
581 /* LCOV_EXCL_STOP */
582
583 static void __control(bundle *b)
584 {
585         char *class_id = NULL;
586         char *id = NULL;
587         char *operation = NULL;
588         char *content = NULL;
589         char *w_str = NULL;
590         char *h_str = NULL;
591         int w = 0;
592         int h = 0;
593         char *remain = NULL;
594         int force;
595         char *force_str = NULL;
596         widget_class_h handle = NULL;
597
598         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
599         /* for previous version compatibility, use appid for default class id */
600         if (class_id == NULL)
601                 class_id = appid;
602
603         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
604         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
605
606         handle = __find_class_handler(class_id, class_provider);
607         if (!handle) {
608                 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
609                 goto error;
610         }
611
612         if (!operation) {
613                 _E("no operation provided");
614                 goto error;
615         }
616
617         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
618
619         if (force_str && strcmp(force_str, "true") == 0)
620                 force = 1;
621         else
622                 force = 0;
623
624         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
625         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
626         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
627         if (w_str)
628                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
629
630         if (h_str)
631                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
632
633         if (strcmp(operation, "create") == 0) {
634                 __instance_create(handle, id, content, w, h);
635         } else if (strcmp(operation, "resize") == 0) {
636                 __resize_window(id, w, h);
637                 __instance_resize(handle, id, w, h);
638         } else if (strcmp(operation, "update") == 0) {
639                 if (id)
640                         __instance_update(handle, id, force, content);
641                 else
642                         __instance_update_all(handle, force, content);
643
644         } else if (strcmp(operation, "destroy") == 0) {
645                 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
646         } else if (strcmp(operation, "resume") == 0) {
647                 __instance_resume(handle, id, UPDATE_ALL);
648         } else if (strcmp(operation, "pause") == 0) {
649                 __instance_pause(handle, id, UPDATE_ALL);
650         } else if (strcmp(operation, "terminate") == 0) {
651                 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
652         }
653
654         return;
655 error:
656         LOGD("error on control");
657         return;
658 }
659
660 static void __pause_all(int send_update)
661 {
662         GList *contexts = _widget_app_get_contexts();
663         GList *iter = g_list_first(contexts);
664
665         while (iter != NULL) {
666                 widget_context_s *cxt = (widget_context_s *)iter->data;
667
668                 switch (cxt->state) {
669                 case WC_READY:
670                         __instance_resume(cxt->provider, cxt->id, send_update);
671                         __instance_pause(cxt->provider, cxt->id, send_update);
672                         break;
673                 case WC_RUNNING:
674                         __instance_pause(cxt->provider, cxt->id, send_update);
675                         break;
676                 }
677                 LOGD("pause %s", cxt->id);
678                 iter = g_list_next(iter);
679         }
680 }
681
682 /* LCOV_EXCL_START */
683 static void __resume_all(int send_update)
684 {
685         GList *contexts = _widget_app_get_contexts();
686         GList *iter = g_list_first(contexts);
687
688         while (iter != NULL) {
689                 widget_context_s *cxt = (widget_context_s *)iter->data;
690
691                 switch (cxt->state) {
692                 case WC_READY:
693                         __instance_resume(cxt->provider, cxt->id, send_update);
694                         break;
695                 case WC_PAUSED:
696                         __instance_resume(cxt->provider, cxt->id, send_update);
697                         break;
698                 }
699                 iter = g_list_next(iter);
700         }
701 }
702 /* LCOV_EXCL_STOP */
703
704 static void __destroy_all(int reason, int send_update)
705 {
706         GList *contexts = _widget_app_get_contexts();
707         GList *iter = g_list_first(contexts);
708
709         __pause_all(send_update);
710         while (iter != NULL) {
711                 widget_context_s *cxt = (widget_context_s *)iter->data;
712                 iter = g_list_next(iter);
713                 switch (cxt->state) {
714                 case WC_PAUSED:
715                         LOGD("destroy %d : %s", cxt->state, cxt->id);
716                         __instance_destroy(cxt->provider, cxt->id, reason, send_update);
717                         break;
718                 }
719         }
720 }
721
722
723 static Eina_Bool __show_cb(void *data, int type, void *event)
724 {
725         Ecore_Wl_Event_Window_Show *ev = event;
726         widget_context_s *cxt = __find_context_by_win(ev->win);
727
728         LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
729
730         if (cxt)
731                 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
732         else
733                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
734
735         return ECORE_CALLBACK_RENEW;
736 }
737
738 static Eina_Bool __hide_cb(void *data, int type, void *event)
739 {
740         Ecore_Wl_Event_Window_Hide *ev = event;
741         widget_context_s *cxt = __find_context_by_win(ev->win);
742
743
744         LOGD("hide %d", (unsigned int)ev->win);
745
746         if (cxt)
747                 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
748         else
749                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
750
751         return ECORE_CALLBACK_RENEW;
752 }
753
754 static Eina_Bool __visibility_cb(void *data, int type, void *event)
755 {
756         Ecore_Wl_Event_Window_Visibility_Change *ev = event;
757         widget_context_s *cxt = __find_context_by_win(ev->win);
758
759         LOGD("visiblity change: %d %d", (unsigned int)ev->win,  (unsigned int)ev->fully_obscured);
760
761         if (!cxt) {
762                 LOGE("unknown window error: %d", ev->win);
763                 return ECORE_CALLBACK_RENEW;
764         }
765
766         if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
767                 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
768         } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
769                 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
770         } else {
771                 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
772         }
773
774         return ECORE_CALLBACK_RENEW;
775 }
776
777 /* LCOV_EXCL_START */
778 static Eina_Bool __lower_cb(void *data, int type, void *event)
779 {
780         LOGD("lower");
781         return ECORE_CALLBACK_RENEW;
782 }
783 /* LCOV_EXCL_STOP */
784
785 static Eina_Bool __configure_cb(void *data, int type, void *event)
786 {
787         Ecore_Wl_Event_Window_Configure *ev = event;
788         widget_context_s *cxt = __find_context_by_win(ev->win);
789
790         LOGD("configure: %d %d", ev->w, ev->h);
791
792         if (!cxt) {
793                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
794                 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
795         }
796
797         if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
798                 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
799         LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
800
801         return ECORE_CALLBACK_RENEW;
802 }
803
804 static void __add_climsg()
805 {
806         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
807         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
808         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
809         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
810         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
811 }
812
813 static void __get_content(bundle *b)
814 {
815         char *instance_id = NULL;
816         widget_context_s *cxt = NULL;
817
818         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
819         if (!instance_id)
820                 return;
821
822         cxt = __find_context_by_id(instance_id);
823         if (!cxt) {
824                 _E("can not find instance id:%s", instance_id);
825                 return;
826         }
827
828         if (cxt->content) {
829                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
830                 _D("content info of %s found", cxt->id);
831         } else {
832                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
833                 _D("empty content info added");
834         }
835 }
836
837 static int __aul_handler(aul_type type, bundle *b, void *data)
838 {
839         char *caller = NULL;
840         char *remain = NULL;
841
842         switch (type) {
843         case AUL_START:
844                 if (b) {
845                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
846                         if (caller) {
847                                 caller_pid = g_ascii_strtoll(caller, &remain,
848                                                 10);
849                         } else {
850                                 /* using caller appid and query pid using caller appid? */
851                                 _E("no caller pid");
852                         }
853                 }
854
855                 __control(b);
856                 break;
857         case AUL_RESUME:
858                 __resume_all(UPDATE_ALL);
859                 break;
860         case AUL_TERMINATE:
861                 widget_app_exit();
862                 break;
863         case AUL_WIDGET_CONTENT:
864                 __get_content(b);
865                 break;
866         default:
867                 break;
868         }
869
870         return 0;
871 }
872
873 static char *__get_domain_name(char *appid)
874 {
875         char *name_token;
876
877         if (appid == NULL) {
878                 _E("appid is NULL"); /* LCOV_EXCL_LINE */
879                 return NULL; /* LCOV_EXCL_LINE */
880         }
881
882         name_token = strrchr(appid, '.');
883
884         if (name_token == NULL) {
885                 _E("appid is invalid"); /* LCOV_EXCL_LINE */
886                 return appid; /* LCOV_EXCL_LINE */
887         }
888
889         name_token++;
890
891         return name_token;
892 }
893
894 /* LCOV_EXCL_START */
895 static void __on_poweroff(keynode_t *key, void *data)
896 {
897         int val;
898
899         val = vconf_keynode_get_int(key);
900         switch (val) {
901         case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
902         case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
903                 _I("power off changed: %d", val);
904                 widget_app_exit();
905                 break;
906         case VCONFKEY_SYSMAN_POWER_OFF_NONE:
907         case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
908         default:
909                 /* DO NOTHING */
910                 break;
911         }
912 }
913 /* LCOV_EXCL_STOP */
914
915 extern int _set_i18n(const char *name);
916
917 static int __before_loop(int argc, char **argv)
918 {
919         int r;
920         bundle *kb = NULL;
921         char *name;
922         char *viewer_endpoint = NULL;
923
924 #if !(GLIB_CHECK_VERSION(2, 36, 0))
925         g_type_init();
926 #endif
927
928         kb = bundle_import_from_argv(argc, argv);
929         if (kb) {
930                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
931                 if (viewer_endpoint) {
932                         _E("viewer endpoint :%s", viewer_endpoint);
933                         _widget_app_set_viewer_endpoint(viewer_endpoint);
934                 } else {
935                         _E("endpoint is missing");
936                 }
937
938                 bundle_free(kb);
939                 kb = NULL;
940         } else {
941                 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
942         }
943
944         screen_connector_provider_init();
945         elm_init(argc, argv);
946
947         r = aul_launch_init(__aul_handler, NULL);
948         if (r < 0) {
949                 /* LCOV_EXCL_START */
950                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
951                                 __FUNCTION__,
952                                 "Fail to call the aul_launch_init");
953                 /* LCOV_EXCL_STOP */
954         }
955
956         r = aul_launch_argv_handler(argc, argv);
957         if (r < 0) {
958                 /* LCOV_EXCL_START */
959                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
960                                 __FUNCTION__,
961                                 "Fail to call the aul_launch_argv_handler");
962                 /* LCOV_EXCL_STOP */
963         }
964
965         r = app_get_id(&appid);
966         if (r != APP_ERROR_NONE)
967                 return r;
968
969         name = __get_domain_name(appid);
970
971         if (name == NULL) {
972                 /* LCOV_EXCL_START */
973                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
974                                 __FUNCTION__,
975                                 "Fail to call __get_domain_name");
976                 /* LCOV_EXCL_STOP */
977         }
978
979         r = _set_i18n(name);
980
981         if (r < 0) {
982                 /* LCOV_EXCL_START */
983                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
984                                 __FUNCTION__,
985                                 "Fail to call _set_i18n");
986                 /* LCOV_EXCL_STOP */
987         }
988
989         __add_climsg();
990
991         _widget_core_set_appcore_event_cb();
992
993         class_provider = app_ops->create(app_user_data);
994         if (class_provider == NULL) {
995                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
996                                 __FUNCTION__, "widget_class is NULL");
997         }
998
999         vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
1000
1001         return WIDGET_ERROR_NONE;
1002 }
1003
1004 static void __after_loop()
1005 {
1006         exit_called = 1;
1007         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
1008
1009         __pause_all(UPDATE_LOCAL);
1010         __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1011
1012         if (app_ops->terminate)
1013                 app_ops->terminate(app_user_data);
1014
1015         screen_connector_provider_fini();
1016
1017         _widget_app_free_viewer_endpoint();
1018         _widget_core_unset_appcore_event_cb();
1019         __free_handler_list();
1020
1021         if (package_id) {
1022                 free(package_id);
1023                 package_id = NULL;
1024         }
1025
1026         if (appid) {
1027                 free(appid);
1028                 appid = NULL;
1029         }
1030
1031         elm_shutdown();
1032 }
1033
1034 static void __on_low_memory(keynode_t *key, void *data)
1035 {
1036         int val;
1037
1038         val = vconf_keynode_get_int(key);
1039         if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
1040                 app_event_handler_h handler;
1041                 struct app_event_info event;
1042
1043                 _I("widget_app_low_memory");
1044
1045                 event.type = APP_EVENT_LOW_MEMORY;
1046                 event.value = (void *)&val;
1047
1048                 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
1049
1050                 while (iter) {
1051                         handler = (app_event_handler_h) iter->data;
1052                         handler->cb(&event, handler->data);
1053                         iter = g_list_next(iter);
1054                 }
1055         }
1056 }
1057
1058 static void __on_low_battery(keynode_t *key, void *data)
1059 {
1060         int val;
1061
1062         val = vconf_keynode_get_int(key);
1063         if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
1064                 app_event_handler_h handler;
1065                 struct app_event_info event;
1066
1067                 _I("widget_app_low_battery");
1068
1069                 event.type = APP_EVENT_LOW_BATTERY;
1070                 event.value = (void *)&val;
1071
1072                 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1073
1074                 while (iter) {
1075                         handler = (app_event_handler_h) iter->data;
1076                         handler->cb(&event, handler->data);
1077                         iter = g_list_next(iter);
1078                 }
1079         }
1080 }
1081
1082 static void __on_lang_changed(keynode_t *key, void *data)
1083 {
1084         char *val;
1085
1086         _update_lang();
1087         val = vconf_keynode_get_str(key);
1088
1089         app_event_handler_h handler;
1090         struct app_event_info event;
1091
1092         _I("widget_app_lang_changed");
1093
1094         event.type = APP_EVENT_LANGUAGE_CHANGED;
1095         event.value = (void *)val;
1096
1097         GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1098
1099         while (iter) {
1100                 handler = (app_event_handler_h) iter->data;
1101                 handler->cb(&event, handler->data);
1102                 iter = g_list_next(iter);
1103         }
1104 }
1105
1106 static void __on_region_changed(keynode_t *key, void *data)
1107 {
1108         char *val;
1109
1110         _update_region();
1111         val = vconf_keynode_get_str(key);
1112
1113         app_event_handler_h handler;
1114         struct app_event_info event;
1115
1116         _I("widget_app_region_changed");
1117
1118         event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1119         event.value = (void *)val;
1120
1121         GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1122
1123         while (iter) {
1124                 handler = (app_event_handler_h) iter->data;
1125                 handler->cb(&event, handler->data);
1126                 iter = g_list_next(iter);
1127         }
1128 }
1129
1130 static void __register_event(int event_type)
1131 {
1132         switch (event_type) {
1133         case APP_EVENT_LOW_MEMORY:
1134                 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1135                 break;
1136
1137         case APP_EVENT_LOW_BATTERY:
1138                 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1139                 break;
1140
1141         case APP_EVENT_LANGUAGE_CHANGED:
1142                 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1143                 break;
1144
1145         case APP_EVENT_REGION_FORMAT_CHANGED:
1146                 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1147                 break;
1148         }
1149 }
1150
1151 static void __unregister_event(int event_type)
1152 {
1153         switch (event_type) {
1154         case APP_EVENT_LOW_MEMORY:
1155                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1156                 break;
1157
1158         case APP_EVENT_LOW_BATTERY:
1159                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1160                 break;
1161
1162         case APP_EVENT_LANGUAGE_CHANGED:
1163                 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1164                 break;
1165
1166         case APP_EVENT_REGION_FORMAT_CHANGED:
1167                 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1168                 break;
1169         }
1170 }
1171
1172 static void _widget_core_set_appcore_event_cb(void)
1173 {
1174         __register_event(APP_EVENT_LANGUAGE_CHANGED);
1175         __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1176 }
1177
1178 static void _widget_core_unset_appcore_event_cb(void)
1179 {
1180         __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1181         __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1182 }
1183
1184 EXPORT_API int widget_app_main(int argc, char **argv,
1185                 widget_app_lifecycle_callback_s *callback, void *user_data)
1186 {
1187         int r;
1188
1189         if (!_is_widget_feature_enabled()) {
1190                 _E("not supported"); /* LCOV_EXCL_LINE */
1191                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1192         }
1193
1194         if (argc <= 0 || argv == NULL || callback == NULL)
1195                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1196                                 __FUNCTION__, NULL);
1197
1198         if (callback->create == NULL)
1199                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1200                                 __FUNCTION__,
1201                                 "widget_app_create_cb() callback must be "
1202                                 "registered");
1203         app_ops = callback;
1204         app_user_data = user_data;
1205         r = __before_loop(argc, argv);
1206         if (r < 0) {
1207                 if (appid) {
1208                         free(appid);
1209                         appid = NULL;
1210                 }
1211                 return r;
1212         }
1213
1214         ecore_main_loop_begin();
1215         aul_status_update(STATUS_DYING);
1216         __after_loop();
1217
1218         return WIDGET_ERROR_NONE;
1219 }
1220
1221 EXPORT_API int widget_app_exit(void)
1222 {
1223         if (!_is_widget_feature_enabled()) {
1224                 _E("not supported"); /* LCOV_EXCL_LINE */
1225                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1226         }
1227
1228         if (exit_called)
1229                 return WIDGET_ERROR_NONE;
1230
1231         exit_called = 1;
1232
1233         ecore_main_loop_quit();
1234
1235         return WIDGET_ERROR_NONE;
1236 }
1237
1238 /* LCOV_EXCL_START */
1239 static gboolean __finish_event_cb(gpointer user_data)
1240 {
1241         if (user_data == NULL)
1242                 return FALSE;
1243
1244         widget_context_s *wc = (widget_context_s *)user_data;
1245
1246         switch (wc->state) {
1247         case WC_READY:
1248                 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1249         case WC_RUNNING:
1250                 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1251         case WC_PAUSED:
1252                 __instance_destroy(wc->provider, wc->id,
1253                                 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1254                 break;
1255         default:
1256                 break;
1257         }
1258
1259         return FALSE;
1260 }
1261 /* LCOV_EXCL_STOP */
1262
1263 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1264 {
1265         if (!_is_widget_feature_enabled()) {
1266                 _E("not supported"); /* LCOV_EXCL_LINE */
1267                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1268         }
1269
1270         if (context == NULL)
1271                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1272                                 __FUNCTION__, NULL);
1273
1274         g_idle_add(__finish_event_cb, context);
1275         return WIDGET_ERROR_NONE;
1276 }
1277
1278 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1279 {
1280         GList *contexts = _widget_app_get_contexts();
1281         GList *list;
1282         widget_context_s *wc;
1283
1284         if (!_is_widget_feature_enabled()) {
1285                 _E("not supported"); /* LCOV_EXCL_LINE */
1286                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1287         }
1288
1289         if (!cb)
1290                 return WIDGET_ERROR_INVALID_PARAMETER;
1291
1292         list = g_list_first(contexts);
1293
1294         while (list) {
1295                 wc = (widget_context_s *)list->data;
1296                 if (wc) {
1297                         if (!cb(wc, data))
1298                                 break;
1299                 }
1300                 list = list->next;
1301         }
1302
1303         return WIDGET_ERROR_NONE;
1304 }
1305
1306 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1307                                         app_event_type_e event_type, app_event_cb callback,
1308                                         void *user_data)
1309 {
1310         int r;
1311         bool feature;
1312
1313         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1314         if (r < 0)
1315                 return WIDGET_ERROR_FAULT;
1316
1317         if (!feature)
1318                 return WIDGET_ERROR_NOT_SUPPORTED;
1319
1320         app_event_handler_h handler;
1321
1322         if (event_handler == NULL || callback == NULL)
1323                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1324
1325         if (event_type < APP_EVENT_LOW_MEMORY
1326             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1327                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1328
1329         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1330                 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1331
1332         GList *iter = g_list_first(handler_list[event_type]);
1333
1334         while (iter) {
1335                 handler = (app_event_handler_h) iter->data;
1336
1337                 if (handler->cb == callback)
1338                         return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1339
1340                 iter = g_list_next(iter);
1341         }
1342
1343         handler = calloc(1, sizeof(struct app_event_handler));
1344         if (!handler)
1345                 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1346
1347         if (g_list_length(handler_list[event_type]) == 0)
1348                 __register_event(event_type);
1349
1350         handler->type = event_type;
1351         handler->cb = callback;
1352         handler->data = user_data;
1353         handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1354
1355         *event_handler = handler;
1356
1357         return WIDGET_ERROR_NONE;
1358 }
1359
1360 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1361                                                 event_handler)
1362 {
1363         int r;
1364         bool feature;
1365
1366         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1367         if (r < 0)
1368                 return WIDGET_ERROR_FAULT;
1369
1370         if (!feature)
1371                 return WIDGET_ERROR_NOT_SUPPORTED;
1372
1373         app_event_type_e type;
1374
1375         if (event_handler == NULL)
1376                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1377
1378         type = event_handler->type;
1379         if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1380                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1381
1382         handler_list[type] = g_list_remove(handler_list[type], event_handler);
1383         free(event_handler);
1384
1385         if (g_list_length(handler_list[type]) == 0)
1386                 __unregister_event(type);
1387
1388         return WIDGET_ERROR_NONE;
1389 }
1390
1391 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1392 {
1393         if (!_is_widget_feature_enabled()) {
1394                 _E("not supported"); /* LCOV_EXCL_LINE */
1395                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1396                 return NULL; /* LCOV_EXCL_LINE */
1397         }
1398
1399         if (!context) {
1400                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1401                 return NULL;
1402         }
1403
1404         set_last_result(WIDGET_ERROR_NONE);
1405         return context->id;
1406 }
1407
1408 static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
1409 {
1410        /* Remove data used in accessibility */
1411         char *plug_id;
1412         plug_id = evas_object_data_del(obj, "___PLUGID");
1413         free(plug_id);
1414 }
1415
1416 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1417                                         Evas_Object **win)
1418 {
1419         widget_context_s *cxt = (widget_context_s *)context;
1420         Evas_Object *ret_win;
1421         Ecore_Wl_Window *wl_win;
1422         struct wl_surface *surface;
1423         char buffer[256];
1424         int rots[3] = {0};
1425
1426         if (!_is_widget_feature_enabled()) {
1427                 _E("not supported"); /* LCOV_EXCL_LINE */
1428                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1429         }
1430
1431         if (context == NULL || win == NULL)
1432                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1433                                 __FUNCTION__, NULL);
1434
1435         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1436         if (ret_win == NULL) {
1437                 _E("failed to create window"); /* LCOV_EXCL_LINE */
1438                 goto fault; /* LCOV_EXCL_LINE */
1439         }
1440
1441         elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
1442         elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
1443
1444         wl_win = elm_win_wl_window_get(ret_win);
1445         if (wl_win == NULL) {
1446                 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1447                 goto fault;
1448         }
1449
1450         surface = ecore_wl_window_surface_get(wl_win);
1451         if (surface == NULL) {
1452                 _E("failed to get surface"); /* LCOV_EXCL_LINE */
1453                 goto fault; /* LCOV_EXCL_LINE */
1454         }
1455         screen_connector_provider_remote_enable(cxt->id, surface);
1456
1457         ecore_wl_window_class_name_set(wl_win, cxt->id);
1458         elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
1459
1460         *win = ret_win;
1461         cxt->win = ret_win;
1462         cxt->win_id = ecore_wl_window_id_get(wl_win);
1463
1464         /* Set data to use in accessibility */
1465         snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
1466         evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
1467         evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
1468
1469         _D("window created: %d", cxt->win_id);
1470
1471         return WIDGET_ERROR_NONE;
1472
1473 fault:
1474         if (ret_win)    /* LCOV_EXCL_LINE */
1475                 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1476
1477         return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1478
1479 }
1480
1481 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1482                 widget_instance_lifecycle_callback_s callback, void *user_data)
1483 {
1484         widget_class_s *wc;
1485
1486         if (!_is_widget_feature_enabled()) {
1487                 _E("not supported"); /* LCOV_EXCL_LINE */
1488                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1489                 return NULL;
1490         }
1491
1492         if (class_id == NULL) {
1493                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1494                 return NULL;
1495         }
1496
1497         wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1498         if (wc == NULL) {
1499                 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1500                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1501                 return NULL; /* LCOV_EXCL_LINE */
1502         }
1503
1504         wc->classid = strdup(class_id);
1505         wc->user_data = user_data;
1506         wc->ops = callback;
1507         wc->next = prev;
1508         wc->prev = NULL;
1509
1510         set_last_result(WIDGET_ERROR_NONE);
1511
1512         if (prev)
1513                 prev->prev = wc;
1514
1515         return wc;
1516 }
1517
1518 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1519                 const char *class_id,
1520                 widget_instance_lifecycle_callback_s callback, void *user_data)
1521 {
1522         return _widget_class_create(widget_class, class_id, callback,
1523                         user_data);
1524 }
1525
1526 EXPORT_API widget_class_h widget_app_class_create(
1527                 widget_instance_lifecycle_callback_s callback, void *user_data)
1528 {
1529         return _widget_class_create(class_provider, appid, callback, user_data);
1530 }
1531
1532 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1533 {
1534         if (!_is_widget_feature_enabled()) {
1535                 _E("not supported"); /* LCOV_EXCL_LINE */
1536                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1537         }
1538
1539         if (context == NULL)
1540                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1541                                 __FUNCTION__, NULL);
1542
1543         context->tag = tag;
1544
1545         return WIDGET_ERROR_NONE;
1546 }
1547
1548 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1549 {
1550         if (!_is_widget_feature_enabled()) {
1551                 _E("not supported"); /* LCOV_EXCL_LINE */
1552                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1553         }
1554
1555         if (context == NULL || tag == NULL)
1556                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1557                                 __FUNCTION__, NULL);
1558
1559         *tag = context->tag;
1560
1561         return WIDGET_ERROR_NONE;
1562 }
1563
1564 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1565                 bundle *content_info)
1566 {
1567         const char *class_id = NULL;
1568         int ret = 0;
1569         bundle_raw *raw = NULL;
1570         int len;
1571
1572         if (!_is_widget_feature_enabled()) {
1573                 _E("not supported"); /* LCOV_EXCL_LINE */
1574                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1575         }
1576
1577         if (context == NULL || content_info == NULL)
1578                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1579                                 __FUNCTION__, NULL);
1580
1581         if (context->provider == NULL)
1582                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1583                                 __FUNCTION__, NULL);
1584
1585         class_id = context->provider->classid;
1586         if (class_id == NULL)
1587                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1588
1589         ret = __send_update_status(class_id, context->id,
1590                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1591
1592         if (context->content)
1593                 free(context->content);
1594
1595         bundle_encode(content_info, &raw, &len);
1596         if (raw)
1597                 context->content = strdup((const char *)raw);
1598         else
1599                 context->content = NULL;
1600
1601         free(raw);
1602         if (ret < 0) {
1603                 /* LCOV_EXCL_START */
1604                 _E("failed to send content info: %s of %s (%d)", context->id,
1605                                 class_id, ret);
1606                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1607                                 NULL);
1608                 /* LCOV_EXCL_STOP */
1609         }
1610
1611         return WIDGET_ERROR_NONE;
1612 }
1613
1614 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1615                 const char *title)
1616 {
1617         if (!_is_widget_feature_enabled()) {
1618                 _E("not supported"); /* LCOV_EXCL_LINE */
1619                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1620         }
1621
1622         if (!context || !title)
1623                 return WIDGET_ERROR_INVALID_PARAMETER;
1624
1625         if (context->win)
1626                 elm_win_title_set(context->win, title);
1627
1628         return WIDGET_ERROR_NONE;
1629 }