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