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