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