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