Remove checking elm init count
[platform/core/appfw/app-core.git] / src / appcore-efl.c
1 /*
2  * Copyright (c) 2000 - 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 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdarg.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #if defined(WAYLAND)
28 #include <Ecore_Wayland.h>
29 #include <wayland-client.h>
30 #include <wayland-tbm-client.h>
31 #include <tizen-extension-client-protocol.h>
32 #elif defined(X11)
33 #include <X11/Xatom.h>
34 #include <X11/Xlib.h>
35 #include <Ecore_X.h>
36 #endif
37
38 #include <Ecore.h>
39 #include <Ecore_Evas.h>
40 #include <Ecore_Input_Evas.h>
41 #include <Elementary.h>
42 #include <glib-object.h>
43 #include <malloc.h>
44 #include <glib.h>
45 #include <gio/gio.h>
46 #include <stdbool.h>
47 #include <aul.h>
48 #include <aul_svc.h>
49 #include <bundle_internal.h>
50 #include <ttrace.h>
51
52 #include "appcore-internal.h"
53 #include "appcore-efl.h"
54
55 static pid_t _pid;
56 static bool resource_reclaiming = TRUE;
57 static int tmp_val = 0;
58
59 struct ui_priv {
60         const char *name;
61         enum app_state state;
62
63         Ecore_Event_Handler *hshow;
64         Ecore_Event_Handler *hhide;
65         Ecore_Event_Handler *hvchange;
66 #if defined(WAYLAND)
67         Ecore_Event_Handler *hlower;
68 #endif
69         Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
70
71         Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
72
73 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
74         struct appcore *app_core;
75         void (*prepare_to_suspend) (void *data);
76         void (*exit_from_suspend) (void *data);
77 #endif
78         struct appcore_ops *ops;
79         void (*mfcb) (void); /* Memory Flushing Callback */
80
81         /* WM_ROTATE */
82         int wm_rot_supported;
83         int rot_started;
84         int (*rot_cb) (void *event_info, enum appcore_rm, void *);
85         void *rot_cb_data;
86         enum appcore_rm rot_mode;
87         bundle *pending_data;
88         char *below_app;
89 };
90
91 static struct ui_priv priv;
92
93 static const char *_ae_name[AE_MAX] = {
94         [AE_UNKNOWN] = "UNKNOWN",
95         [AE_CREATE] = "CREATE",
96         [AE_TERMINATE] = "TERMINATE",
97         [AE_PAUSE] = "PAUSE",
98         [AE_RESUME] = "RESUME",
99         [AE_RESET] = "RESET",
100         [AE_LOWMEM_POST] = "LOWMEM_POST",
101         [AE_MEM_FLUSH] = "MEM_FLUSH",
102 };
103
104 static const char *_as_name[] = {
105         [AS_NONE] = "NONE",
106         [AS_CREATED] = "CREATED",
107         [AS_RUNNING] = "RUNNING",
108         [AS_PAUSED] = "PAUSED",
109         [AS_DYING] = "DYING",
110 };
111
112 static bool b_active = FALSE;
113 static bool first_launch = TRUE;
114
115 struct win_node {
116         unsigned int win;
117 #if defined(WAYLAND)
118         unsigned int surf;
119 #endif
120         bool bfobscured;
121 };
122
123 #if defined(X11)
124 static struct ui_wm_rotate wm_rotate;
125 #endif
126 static Eina_Bool __visibility_cb(void *data, int type, void *event);
127 static GSList *g_winnode_list;
128
129 static struct wl_display *dsp;
130 static struct wl_registry *reg;
131 static struct tizen_policy *tz_policy;
132 static bool bg_state = false;
133
134 static void __wl_listener_cb(void *data, struct wl_registry *reg,
135                 uint32_t id, const char *interface, uint32_t ver)
136 {
137         if (interface && !strcmp(interface, "tizen_policy")) {
138                 if (!tz_policy)
139                         tz_policy = wl_registry_bind(reg, id,
140                                         &tizen_policy_interface, 1);
141         }
142 }
143
144 static void __wl_listener_remove_cb(void *data, struct wl_registry *reg,
145                 unsigned int id)
146 {
147         /* do nothing */
148 }
149
150 static const struct wl_registry_listener reg_listener = {
151         __wl_listener_cb,
152         __wl_listener_remove_cb
153 };
154
155 static int __init_wl(void)
156 {
157         _DBG("initialize wayland");
158         dsp = wl_display_connect(NULL);
159         if (dsp == NULL) {
160                 _ERR("Failed to connect wl display");
161                 return -1;
162         }
163
164         reg = wl_display_get_registry(dsp);
165         if (reg == NULL) {
166                 _ERR("Failed to get registry");
167                 wl_display_disconnect(dsp);
168                 return -1;
169         }
170
171         wl_registry_add_listener(reg, &reg_listener, NULL);
172         wl_display_roundtrip(dsp);
173
174         if (!tz_policy) {
175                 _ERR("Failed to get tizen policy interface");
176                 wl_registry_destroy(reg);
177                 wl_display_disconnect(dsp);
178                 return -1;
179         }
180
181         return 0;
182 }
183
184 static void __finish_wl(void)
185 {
186         if (tz_policy) {
187                 tizen_policy_destroy(tz_policy);
188                 tz_policy = NULL;
189         }
190
191         if (reg) {
192                 wl_registry_destroy(reg);
193                 reg = NULL;
194         }
195
196         if (dsp) {
197                 wl_display_disconnect(dsp);
198                 dsp = NULL;
199         }
200 }
201
202 static void __set_bg_state(void)
203 {
204         if (__init_wl() < 0)
205                 return;
206
207         tizen_policy_set_background_state(tz_policy, getpid());
208         wl_display_roundtrip(dsp);
209         bg_state = true;
210         _DBG("bg state: %d", bg_state);
211 }
212
213 static void __unset_bg_state(void)
214 {
215         if (!tz_policy)
216                 return;
217
218         tizen_policy_unset_background_state(tz_policy, getpid());
219         wl_display_roundtrip(dsp);
220         bg_state = false;
221         _DBG("bg state: %d", bg_state);
222         __finish_wl();
223 }
224
225 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
226 static void __appcore_efl_prepare_to_suspend(void *data)
227 {
228         struct ui_priv *ui = (struct ui_priv *)data;
229         struct sys_op *op = NULL;
230         int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
231
232         if (ui->app_core && !ui->app_core->allowed_bg && !ui->app_core->suspended_state) {
233                 op = &ui->app_core->sops[SE_SUSPENDED_STATE];
234                 if (op && op->func)
235                         op->func((void *)&suspend, op->data); /* calls c-api handler */
236
237                 ui->app_core->suspended_state = true;
238         }
239         _DBG("[__SUSPEND__]");
240 }
241
242 static void __appcore_efl_exit_from_suspend(void *data)
243 {
244         struct ui_priv *ui = (struct ui_priv *)data;
245         struct sys_op *op = NULL;
246         int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
247
248         if (ui->app_core && !ui->app_core->allowed_bg && ui->app_core->suspended_state) {
249                 op = &ui->app_core->sops[SE_SUSPENDED_STATE];
250                 if (op && op->func)
251                         op->func((void *)&suspend, op->data); /* calls c-api handler */
252
253                 ui->app_core->suspended_state = false;
254         }
255         _DBG("[__SUSPEND__]");
256 }
257 #endif
258
259 #if defined(MEMORY_FLUSH_ACTIVATE)
260 static Eina_Bool __appcore_memory_flush_cb(void *data)
261 {
262         struct ui_priv *ui = (struct ui_priv *)data;
263
264         appcore_flush_memory();
265         if (ui)
266                 ui->mftimer = NULL;
267
268 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
269         if (ui && ui->prepare_to_suspend) {
270                 _DBG("[__SUSPEND__] flush case");
271                 ui->prepare_to_suspend(ui);
272         }
273 #endif
274
275         return ECORE_CALLBACK_CANCEL;
276 }
277
278 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
279 {
280         if (ui->state == AS_PAUSED)
281                 appcore_flush_memory();
282         else
283                 malloc_trim(0);
284
285         return 0;
286 }
287
288 static void __appcore_timer_add(struct ui_priv *ui)
289 {
290         ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
291 }
292
293 static void __appcore_timer_del(struct ui_priv *ui)
294 {
295         if (ui->mftimer) {
296                 ecore_timer_del(ui->mftimer);
297                 ui->mftimer = NULL;
298         }
299 }
300
301 #else
302
303 static int __appcore_low_memory_post_cb(ui_priv *ui)
304 {
305         return -1;
306 }
307
308 #define __appcore_timer_add(ui) 0
309 #define __appcore_timer_del(ui) 0
310
311 #endif
312
313 static void __appcore_efl_memory_flush_cb(void)
314 {
315         _DBG("[APP %d]   __appcore_efl_memory_flush_cb()", _pid);
316         elm_cache_all_flush();
317 }
318 #if defined(WAYLAND)
319 static void wl_raise_win(void)
320 {
321         Ecore_Wl_Window *win;
322         unsigned int win_id = appcore_get_main_window();
323
324         _DBG("Raise window: %d", win_id);
325         win = ecore_wl_window_find(win_id);
326         ecore_wl_window_activate(win);
327 }
328
329 static void wl_pause_win(void)
330 {
331         Ecore_Wl_Window *win;
332         GSList *wlist = g_winnode_list;
333         struct win_node *entry = NULL;
334
335         _DBG("Pause window");
336
337         while (wlist) {
338                 entry = wlist->data;
339
340                 _DBG("Pause window: %d", entry->win);
341                 win = ecore_wl_window_find(entry->win);
342                 ecore_wl_window_iconified_set(win, EINA_TRUE);
343
344                 wlist = wlist->next;
345         }
346 }
347
348 #endif
349
350 static void __do_app(enum app_event event, void *data, bundle * b)
351 {
352         int r = -1;
353         struct ui_priv *ui = data;
354         const char *below_app;
355         const char *bg_launch;
356
357         _DBG("[APP %d] Event: %d", _pid, event);
358         _ret_if(ui == NULL || event >= AE_MAX);
359         _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
360              _as_name[ui->state]);
361
362         if (event == AE_MEM_FLUSH) {
363                 ui->mfcb();
364                 return;
365         }
366
367         if (event == AE_LOWMEM_POST) {
368                 if (__appcore_low_memory_post_cb(ui) == 0)
369                         return;
370         }
371
372         if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
373                 __appcore_timer_del(ui);
374
375         if (ui->state == AS_DYING) {
376                 _ERR("Skip the event in dying state");
377                 return;
378         }
379
380         if (event == AE_TERMINATE) {
381                 _DBG("[APP %d] TERMINATE", _pid);
382                 elm_exit();
383                 aul_status_update(STATUS_DYING);
384                 return;
385         }
386
387         if (event == AE_RAISE) {
388 #if defined(X11)
389                 x_raise_win(getpid());
390 #elif defined(WAYLAND)
391                 wl_raise_win();
392 #endif
393                 return;
394         }
395
396         if (event == AE_LOWER) {
397 #if defined(X11)
398                 x_pause_win(getpid());
399 #elif defined(WAYLAND)
400                 wl_pause_win();
401 #endif
402                 return;
403         }
404
405         _ret_if(ui->ops == NULL);
406
407         switch (event) {
408         case AE_RESET:
409                 _DBG("[APP %d] RESET", _pid);
410                 ui->pending_data = bundle_dup(b);
411                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
412
413                 if (ui->below_app) {
414                         free(ui->below_app);
415                         ui->below_app = NULL;
416                 }
417
418                 below_app = bundle_get_val(b, AUL_SVC_K_RELOCATE_BELOW);
419                 if (below_app)
420                         ui->below_app = strdup(below_app);
421 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
422                 if (ui->exit_from_suspend) {
423                         _DBG("[__SUSPEND__] reset case");
424                         ui->exit_from_suspend(ui);
425                 }
426 #endif
427
428                 if (ui->ops->reset) {
429                         traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
430                                         "APPCORE:RESET");
431                         r = ui->ops->reset(b, ui->ops->data);
432                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
433                 }
434                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
435
436                 if (first_launch) {
437 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
438                         if (ui->app_core->allowed_bg)
439                                 __appcore_timer_add(ui);
440 #endif
441                         first_launch = FALSE;
442                 } else {
443                         _INFO("[APP %d] App already running, raise the window", _pid);
444                         if (bg_state) {
445                                 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
446                                 if (!bg_launch || strcmp(bg_launch, "enable"))
447                                         __unset_bg_state();
448                         }
449 #ifdef X11
450                         x_raise_win(getpid());
451 #else
452                         wl_raise_win();
453 #endif
454                 }
455                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
456                     ui->name);
457                 break;
458         case AE_PAUSE:
459                 if (ui->state == AS_RUNNING) {
460                         _DBG("[APP %d] PAUSE", _pid);
461                         if (ui->ops->pause) {
462                                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
463                                                 "APPCORE:PAUSE");
464                                 r = ui->ops->pause(ui->ops->data);
465                                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
466                         }
467                         ui->state = AS_PAUSED;
468                         if (r >= 0 && resource_reclaiming == TRUE)
469                                 __appcore_timer_add(ui);
470 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
471                         else if (r >= 0 && resource_reclaiming == FALSE
472                                         && ui->prepare_to_suspend) {
473                                 _DBG("[__SUSPEND__] pause case");
474                                 ui->prepare_to_suspend(ui);
475                         }
476 #endif
477                 }
478                 /* TODO : rotation stop */
479                 /* r = appcore_pause_rotation_cb(); */
480                 aul_status_update(STATUS_BG);
481                 break;
482         case AE_RESUME:
483                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
484                                 ui->name);
485 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
486                 if (ui->exit_from_suspend) {
487                         _DBG("[__SUSPEND__] resume case");
488                         ui->exit_from_suspend(ui);
489                 }
490                 if (ui->app_core->allowed_bg)
491                         __appcore_timer_del(ui);
492 #endif
493
494                 if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
495                         _DBG("[APP %d] RESUME", _pid);
496
497                         if (ui->state == AS_CREATED) {
498                                 bundle_free(ui->pending_data);
499                                 ui->pending_data = NULL;
500                         }
501
502                         if (ui->ops->resume) {
503                                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
504                                         "APPCORE:RESUME");
505                                 ui->ops->resume(ui->ops->data);
506                                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
507                         }
508                         ui->state = AS_RUNNING;
509
510                         if (ui->below_app) {
511                                 aul_app_group_activate_below(ui->below_app);
512                                 free(ui->below_app);
513                                 ui->below_app = NULL;
514                         }
515                 }
516                 /*TODO : rotation start*/
517                 /* r = appcore_resume_rotation_cb(); */
518                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
519                     ui->name);
520                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
521                     ui->name);
522                 aul_status_update(STATUS_VISIBLE);
523                 break;
524         case AE_TERMINATE_BGAPP:
525                 if (ui->state == AS_PAUSED) {
526                         _DBG("[APP %d] is paused. TERMINATE", _pid);
527                         ui->state = AS_DYING;
528                         aul_status_update(STATUS_DYING);
529                         elm_exit();
530                 } else if (ui->state == AS_RUNNING) {
531                         _DBG("[APP %d] is running.", _pid);
532                 } else {
533                         _DBG("[APP %d] is another state", _pid);
534                 }
535                 break;
536         default:
537                 /* do nothing */
538                 break;
539         }
540 }
541
542 static struct ui_ops efl_ops = {
543         .data = &priv,
544         .cb_app = __do_app,
545 };
546
547 static bool __check_visible(void)
548 {
549         GSList *iter = NULL;
550         struct win_node *entry = NULL;
551
552         _DBG("[EVENT_TEST][EVENT] __check_visible\n");
553
554         for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
555                 entry = iter->data;
556                 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
557                 if (entry->bfobscured == FALSE)
558                         return TRUE;
559         }
560
561         return FALSE;
562 }
563
564 static GSList *__find_win(unsigned int win)
565 {
566         GSList *iter;
567         struct win_node *t;
568
569         for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
570                 t = iter->data;
571                 if (t && t->win == win)
572                         return iter;
573         }
574
575         return NULL;
576 }
577
578 #if defined(X11)
579 static bool __add_win(unsigned int win)
580 {
581         struct win_node *t;
582         GSList *f;
583
584         _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
585
586         f = __find_win(win);
587         if (f) {
588                 errno = ENOENT;
589                 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
590                 return FALSE;
591         }
592
593         t = calloc(1, sizeof(struct win_node));
594         if (t == NULL)
595                 return FALSE;
596
597         t->win = win;
598         t->bfobscured = FALSE;
599
600         g_winnode_list = g_slist_append(g_winnode_list, t);
601
602         return TRUE;
603 }
604 #elif defined(WAYLAND)
605 static bool __add_win(unsigned int win, unsigned int surf)
606 {
607         struct win_node *t;
608         GSList *f;
609
610         _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
611
612         f = __find_win(win);
613         if (f) {
614                 errno = ENOENT;
615                 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
616                 return FALSE;
617         }
618
619         t = calloc(1, sizeof(struct win_node));
620         if (t == NULL)
621                 return FALSE;
622
623         t->win = win;
624         t->surf = surf;
625         t->bfobscured = FALSE;
626
627         g_winnode_list = g_slist_append(g_winnode_list, t);
628
629         return TRUE;
630 }
631 #endif
632
633 static bool __delete_win(unsigned int win)
634 {
635         GSList *f;
636
637         f = __find_win(win);
638         if (!f) {
639                 errno = ENOENT;
640                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
641                                 win);
642                 return FALSE;
643         }
644
645         free(f->data);
646         g_winnode_list = g_slist_delete_link(g_winnode_list, f);
647
648         return TRUE;
649 }
650
651 #if defined(X11)
652 static bool __update_win(unsigned int win, bool bfobscured)
653 {
654         GSList *f;
655         struct win_node *t;
656
657         _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
658              bfobscured);
659
660         f = __find_win(win);
661         if (!f) {
662                 errno = ENOENT;
663                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
664                 return FALSE;
665         }
666
667         g_winnode_list = g_slist_remove_link(g_winnode_list, f);
668
669         t = (struct win_node *)f->data;
670         t->win = win;
671         t->bfobscured = bfobscured;
672
673         g_winnode_list = g_slist_concat(g_winnode_list, f);
674
675         return TRUE;
676 }
677 #elif defined(WAYLAND)
678 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
679 {
680         GSList *f;
681         struct win_node *t;
682
683         _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
684              bfobscured);
685
686         f = __find_win(win);
687         if (!f) {
688                 errno = ENOENT;
689                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
690                 return FALSE;
691         }
692
693         g_winnode_list = g_slist_remove_link(g_winnode_list, f);
694
695         t = (struct win_node *)f->data;
696         t->win = win;
697         if (surf != 0)
698                 t->surf = surf;
699         t->bfobscured = bfobscured;
700
701         g_winnode_list = g_slist_concat(g_winnode_list, f);
702
703         return TRUE;
704 }
705 #endif
706
707 /* WM_ROTATE */
708 #ifdef X11
709 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
710 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
711
712 static int __check_wm_rotation_support(void)
713 {
714         _DBG("Disable window manager rotation");
715         return -1;
716
717         Ecore_X_Window root, win, win2;
718         int ret;
719
720         if (!_WM_WINDOW_ROTATION_SUPPORTED) {
721                 _WM_WINDOW_ROTATION_SUPPORTED =
722                                         ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
723         }
724
725         if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
726                 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
727                                         ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
728         }
729
730         root = ecore_x_window_root_first_get();
731         ret = ecore_x_window_prop_xid_get(root,
732                         _WM_WINDOW_ROTATION_SUPPORTED,
733                         ECORE_X_ATOM_WINDOW,
734                         &win, 1);
735         if ((ret == 1) && (win)) {
736                 ret = ecore_x_window_prop_xid_get(win,
737                                 _WM_WINDOW_ROTATION_SUPPORTED,
738                                 ECORE_X_ATOM_WINDOW,
739                                 &win2, 1);
740                 if ((ret == 1) && (win2 == win))
741                         return 0;
742         }
743
744         return -1;
745 }
746
747 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
748 {
749         GSList *iter = NULL;
750         struct win_node *entry = NULL;
751
752         if (win == 0) {
753                 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
754                         entry = iter->data;
755                         if (entry->win) {
756                                 ecore_x_window_prop_card32_set(entry->win,
757                                                 _WM_WINDOW_ROTATION_SUPPORTED,
758                                                 &set, 1);
759                         }
760                 }
761         } else {
762                 ecore_x_window_prop_card32_set(win,
763                                 _WM_WINDOW_ROTATION_SUPPORTED,
764                                 &set, 1);
765         }
766 }
767 #endif
768
769 static Eina_Bool __show_cb(void *data, int type, void *event)
770 {
771 #if defined(WAYLAND)
772         Ecore_Wl_Event_Window_Show *ev;
773
774         ev = event;
775         if (ev->parent_win != 0) {
776                 /* This is child window. Skip!!! */
777                 return ECORE_CALLBACK_PASS_ON;
778         }
779
780         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
781
782         if (!__find_win((unsigned int)ev->win))
783                 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
784         else
785                 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
786
787 #elif defined(X11)
788         Ecore_X_Event_Window_Show *ev;
789
790         ev = event;
791
792         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
793
794         if (!__find_win((unsigned int)ev->win)) {
795                 /* WM_ROTATE */
796                 if ((priv.wm_rot_supported) && (1 == priv.rot_started))
797                         __set_wm_rotation_support(ev->win, 1);
798                 __add_win((unsigned int)ev->win);
799         } else {
800                 __update_win((unsigned int)ev->win, FALSE);
801         }
802 #endif
803
804         appcore_group_attach();
805         return ECORE_CALLBACK_RENEW;
806 }
807
808 static Eina_Bool __hide_cb(void *data, int type, void *event)
809 {
810 #if defined(WAYLAND)
811         Ecore_Wl_Event_Window_Hide *ev;
812 #elif defined(X11)
813         Ecore_X_Event_Window_Hide *ev;
814 #endif
815         int bvisibility = 0;
816
817         ev = event;
818
819         _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
820
821         if (__find_win((unsigned int)ev->win)) {
822                 __delete_win((unsigned int)ev->win);
823                 bvisibility = __check_visible();
824                 if (!bvisibility && b_active == TRUE) {
825                         _DBG(" Go to Pasue state \n");
826                         b_active = FALSE;
827                         __do_app(AE_PAUSE, data, NULL);
828                 }
829         }
830
831         return ECORE_CALLBACK_RENEW;
832 }
833
834 #if defined(WAYLAND)
835 static Eina_Bool __lower_cb(void *data, int type, void *event)
836 {
837         Ecore_Wl_Event_Window_Lower *ev;
838         ev = event;
839         if (!ev) return ECORE_CALLBACK_RENEW;
840         _DBG("ECORE_WL_EVENT_WINDOW_LOWER window id:%u\n", ev->win);
841         appcore_group_lower();
842         return ECORE_CALLBACK_RENEW;
843 }
844 #endif
845
846 static Eina_Bool __visibility_cb(void *data, int type, void *event)
847 {
848 #if defined(WAYLAND)
849         Ecore_Wl_Event_Window_Visibility_Change *ev;
850         int bvisibility = 0;
851         ev = event;
852         __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
853 #elif defined(X11)
854         Ecore_X_Event_Window_Visibility_Change *ev;
855         int bvisibility = 0;
856
857         ev = event;
858
859         __update_win((unsigned int)ev->win, ev->fully_obscured);
860 #endif
861         bvisibility = __check_visible();
862
863         _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
864
865         if (bvisibility && b_active == FALSE) {
866                 _DBG(" Go to Resume state\n");
867                 b_active = TRUE;
868                 __do_app(AE_RESUME, data, NULL);
869
870         } else if (!bvisibility && b_active == TRUE) {
871                 _DBG(" Go to Pasue state \n");
872                 b_active = FALSE;
873                 __do_app(AE_PAUSE, data, NULL);
874         } else
875                 _DBG(" No change state \n");
876
877         return ECORE_CALLBACK_RENEW;
878
879 }
880
881 #if defined(X11)
882 /* WM_ROTATE */
883 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
884 {
885         struct ui_priv *ui = (struct ui_priv *)data;
886         Ecore_X_Event_Client_Message *e = event;
887
888         if (!ui)
889                 return ECORE_CALLBACK_PASS_ON;
890
891         if (e->format != 32)
892                 return ECORE_CALLBACK_PASS_ON;
893
894         if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
895                 if ((ui->wm_rot_supported == 0)
896                         || (ui->rot_started == 0)
897                         || (ui->rot_cb == NULL)) {
898                         return ECORE_CALLBACK_PASS_ON;
899                 }
900
901                 enum appcore_rm rm;
902                 switch (e->data.l[1]) {
903                 case 0:
904                         rm = APPCORE_RM_PORTRAIT_NORMAL;
905                         break;
906                 case 90:
907                         rm = APPCORE_RM_LANDSCAPE_REVERSE;
908                         break;
909                 case 180:
910                         rm = APPCORE_RM_PORTRAIT_REVERSE;
911                         break;
912                 case 270:
913                         rm = APPCORE_RM_LANDSCAPE_NORMAL;
914                         break;
915                 default:
916                         rm = APPCORE_RM_UNKNOWN;
917                         break;
918                 }
919
920                 ui->rot_mode = rm;
921
922                 if (APPCORE_RM_UNKNOWN != rm)
923                         ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
924         }
925
926         return ECORE_CALLBACK_PASS_ON;
927 }
928 #endif
929
930 static void __add_climsg_cb(struct ui_priv *ui)
931 {
932         _ret_if(ui == NULL);
933 #if defined(WAYLAND)
934         ui->hshow =
935                 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
936         ui->hhide =
937                 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
938         ui->hvchange =
939                 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
940                                 __visibility_cb, ui);
941         ui->hlower =
942                 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER,
943                                 __lower_cb, ui);
944 #elif defined(X11)
945         ui->hshow =
946                 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
947         ui->hhide =
948                 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
949         ui->hvchange =
950                 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
951                                 __visibility_cb, ui);
952
953         /* Add client message callback for WM_ROTATE */
954         if (!__check_wm_rotation_support()) {
955                 ui->hcmsg = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
956                                 __cmsg_cb, ui);
957                 ui->wm_rot_supported = 1;
958                 appcore_set_wm_rotation(&wm_rotate);
959         }
960 #endif
961 }
962
963 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
964 {
965         int r;
966         char *hwacc = NULL;
967 #if _APPFW_FEATURE_BACKGROUND_MANAGEMENT
968         struct appcore *ac = NULL;
969 #endif
970         bundle *b;
971         const char *bg_launch;
972
973         if (argc == NULL || argv == NULL) {
974                 _ERR("argc/argv is NULL");
975                 errno = EINVAL;
976                 return -1;
977         }
978
979 #if !(GLIB_CHECK_VERSION(2, 36, 0))
980         g_type_init();
981 #endif
982         elm_init(*argc, *argv);
983
984         hwacc = getenv("HWACC");
985         if (hwacc == NULL) {
986                 _DBG("elm_config_accel_preference_set is not called");
987         } else if (strcmp(hwacc, "USE") == 0) {
988                 elm_config_accel_preference_set("hw");
989                 _DBG("elm_config_accel_preference_set : hw");
990         } else if (strcmp(hwacc, "NOT_USE") == 0) {
991                 elm_config_accel_preference_set("none");
992                 _DBG("elm_config_accel_preference_set : none");
993         } else {
994                 _DBG("elm_config_accel_preference_set is not called");
995         }
996
997         r = appcore_init(ui->name, &efl_ops, *argc, *argv);
998         _retv_if(r == -1, -1);
999
1000 #if _APPFW_FEATURE_BACKGROUND_MANAGEMENT
1001         appcore_get_app_core(&ac);
1002         ui->app_core = ac;
1003         SECURE_LOGD("[__SUSPEND__] appcore initialized, appcore addr: #%x", ac);
1004 #endif
1005
1006         b = bundle_import_from_argv(*argc, *argv);
1007         if (b) {
1008                 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
1009                 if (bg_launch && strcmp(bg_launch, "enable") == 0)
1010                         __set_bg_state();
1011
1012                 bundle_free(b);
1013         }
1014
1015         LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
1016         if (ui->ops && ui->ops->create) {
1017                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1018                 r = ui->ops->create(ui->ops->data);
1019                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1020                 if (r < 0) {
1021                         _ERR("create() return error");
1022                         appcore_exit();
1023                         if (ui->ops && ui->ops->terminate) {
1024                                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
1025                                         "APPCORE:TERMINATE");
1026                                 ui->ops->terminate(ui->ops->data);
1027                                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1028                         }
1029                         errno = ECANCELED;
1030                         return -1;
1031                 }
1032                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
1033                     ui->name);
1034         }
1035         ui->state = AS_CREATED;
1036
1037         __add_climsg_cb(ui);
1038
1039         return 0;
1040 }
1041
1042 static void __after_loop(struct ui_priv *ui)
1043 {
1044         appcore_unset_rotation_cb();
1045         appcore_exit();
1046
1047         if (ui->state == AS_RUNNING) {
1048                 _DBG("[APP %d] PAUSE before termination", _pid);
1049                 if (ui->ops && ui->ops->pause) {
1050                         traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
1051                                         "APPCORE:PAUSE");
1052                         ui->ops->pause(ui->ops->data);
1053                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1054                 }
1055         }
1056
1057         if (ui->ops && ui->ops->terminate) {
1058                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1059                 ui->ops->terminate(ui->ops->data);
1060                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1061         }
1062
1063         ui->state = AS_DYING;
1064
1065         if (ui->hshow)
1066                 ecore_event_handler_del(ui->hshow);
1067         if (ui->hhide)
1068                 ecore_event_handler_del(ui->hhide);
1069         if (ui->hvchange)
1070                 ecore_event_handler_del(ui->hvchange);
1071 #if defined(WAYLAND)
1072         if (ui->hlower)
1073                 ecore_event_handler_del(ui->hlower);
1074 #endif
1075
1076         __appcore_timer_del(ui);
1077
1078         elm_shutdown();
1079 }
1080
1081 static int __set_data(struct ui_priv *ui, const char *name,
1082                     struct appcore_ops *ops)
1083 {
1084         if (ui->name) {
1085                 _ERR("Mainloop already started");
1086                 errno = EINPROGRESS;
1087                 return -1;
1088         }
1089
1090         if (name == NULL || name[0] == '\0') {
1091                 _ERR("Invalid name");
1092                 errno = EINVAL;
1093                 return -1;
1094         }
1095
1096         if (ops == NULL) {
1097                 _ERR("ops is NULL");
1098                 errno = EINVAL;
1099                 return -1;
1100         }
1101
1102         ui->name = strdup(name);
1103         _retv_if(ui->name == NULL, -1);
1104
1105         ui->ops = ops;
1106         ui->mfcb = __appcore_efl_memory_flush_cb;
1107         _pid = getpid();
1108
1109         /* WM_ROTATE */
1110         ui->wm_rot_supported = 0;
1111         ui->rot_started = 0;
1112         ui->rot_cb = NULL;
1113         ui->rot_cb_data = NULL;
1114         ui->rot_mode = APPCORE_RM_UNKNOWN;
1115
1116 #ifdef  _APPFW_FEATURE_BACKGROUND_MANAGEMENT
1117         ui->app_core = NULL;
1118         ui->prepare_to_suspend = __appcore_efl_prepare_to_suspend;
1119         ui->exit_from_suspend = __appcore_efl_exit_from_suspend;
1120 #endif
1121
1122         return 0;
1123 }
1124
1125 static void __unset_data(struct ui_priv *ui)
1126 {
1127         if (ui->name)
1128                 free((void *)ui->name);
1129
1130         memset(ui, 0, sizeof(struct ui_priv));
1131 }
1132
1133 #if defined(X11)
1134 /* WM_ROTATE */
1135 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
1136 {
1137         if (cb == NULL) {
1138                 errno = EINVAL;
1139                 return -1;
1140         }
1141
1142         if ((priv.wm_rot_supported) && (0 == priv.rot_started))
1143                 __set_wm_rotation_support(0, 1);
1144
1145         priv.rot_cb = cb;
1146         priv.rot_cb_data = data;
1147         priv.rot_started = 1;
1148
1149         return 0;
1150 }
1151
1152 static int __wm_unset_rotation_cb(void)
1153 {
1154         if ((priv.wm_rot_supported) && (1 == priv.rot_started))
1155                 __set_wm_rotation_support(0, 0);
1156
1157         priv.rot_cb = NULL;
1158         priv.rot_cb_data = NULL;
1159         priv.rot_started = 0;
1160
1161         return 0;
1162 }
1163
1164 static int __wm_get_rotation_state(enum appcore_rm *curr)
1165 {
1166         if (curr == NULL) {
1167                 errno = EINVAL;
1168                 return -1;
1169         }
1170
1171         *curr = priv.rot_mode;
1172
1173         return 0;
1174 }
1175
1176 static int __wm_pause_rotation_cb(void)
1177 {
1178         if ((priv.rot_started == 1) && (priv.wm_rot_supported))
1179                 __set_wm_rotation_support(0, 0);
1180
1181         priv.rot_started = 0;
1182
1183         return 0;
1184 }
1185
1186 static int __wm_resume_rotation_cb(void)
1187 {
1188         if ((priv.rot_started == 0) && (priv.wm_rot_supported))
1189                 __set_wm_rotation_support(0, 1);
1190
1191         priv.rot_started = 1;
1192
1193         return 0;
1194 }
1195
1196 static struct ui_wm_rotate wm_rotate = {
1197         __wm_set_rotation_cb,
1198         __wm_unset_rotation_cb,
1199         __wm_get_rotation_state,
1200         __wm_pause_rotation_cb,
1201         __wm_resume_rotation_cb
1202 };
1203 #endif
1204
1205 EXPORT_API int appcore_efl_init(const char *name, int *argc, char ***argv,
1206                      struct appcore_ops *ops)
1207 {
1208         int r;
1209
1210         LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
1211
1212         r = __set_data(&priv, name, ops);
1213         _retv_if(r == -1, -1);
1214
1215         r = __before_loop(&priv, argc, argv);
1216         if (r == -1) {
1217                 aul_status_update(STATUS_DYING);
1218                 __unset_data(&priv);
1219                 return -1;
1220         }
1221
1222         return 0;
1223 }
1224
1225 EXPORT_API void appcore_efl_fini(void)
1226 {
1227         aul_status_update(STATUS_DYING);
1228
1229         __after_loop(&priv);
1230
1231         __unset_data(&priv);
1232 }
1233
1234 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1235                                 struct appcore_ops *ops)
1236 {
1237         int r;
1238
1239         r = appcore_efl_init(name, argc, argv, ops);
1240         _retv_if(r == -1, -1);
1241
1242         elm_run();
1243
1244         appcore_efl_fini();
1245
1246         return 0;
1247 }
1248
1249 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1250 {
1251         resource_reclaiming = enable;
1252
1253         return 0;
1254 }
1255
1256 EXPORT_API int appcore_set_app_state(int state)
1257 {
1258         priv.state = state;
1259
1260         tmp_val = 1;
1261
1262         return 0;
1263 }
1264
1265 EXPORT_API int appcore_set_preinit_window_name(const char *win_name)
1266 {
1267         int ret = -1;
1268         void *preinit_window = NULL;
1269         const Evas *e = NULL;
1270
1271         if (!win_name) {
1272                 _ERR("invalid parameter");
1273                 return ret;
1274         }
1275
1276         preinit_window = elm_win_precreated_object_get();
1277         if (!preinit_window) {
1278                 _ERR("Failed to get preinit window");
1279                 return ret;
1280         }
1281
1282         e = evas_object_evas_get((const Evas_Object *)preinit_window);
1283         if (e) {
1284                 Ecore_Evas *ee = ecore_evas_ecore_evas_get(e);
1285                 if (ee) {
1286                         ecore_evas_name_class_set(ee, win_name, win_name);
1287                         ret = 0;
1288                 }
1289         }
1290
1291         return ret;
1292 }
1293
1294 EXPORT_API unsigned int appcore_get_main_window(void)
1295 {
1296         struct win_node *entry = NULL;
1297
1298         if (g_winnode_list != NULL) {
1299                 entry = g_winnode_list->data;
1300                 return (unsigned int) entry->win;
1301         }
1302
1303         return 0;
1304 }
1305
1306 #if defined(WAYLAND)
1307 EXPORT_API unsigned int appcore_get_main_surface(void)
1308 {
1309         struct win_node *entry = NULL;
1310
1311         if (g_winnode_list != NULL) {
1312                 entry = g_winnode_list->data;
1313                 return (unsigned int) entry->surf;
1314         }
1315
1316         return 0;
1317 }
1318 #endif