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