Deactivate window for the AUL_PAUSE command
[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         Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
75
76         Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
77
78         struct appcore_ops *ops;
79         void (*mfcb) (void); /* Memory Flushing Callback */
80
81         /* WM_ROTATE */
82         int wm_rot_supported;
83         int rot_started;
84         int (*rot_cb) (void *event_info, enum appcore_rm, void *);
85         void *rot_cb_data;
86         enum appcore_rm rot_mode;
87         bundle *pending_data;
88 };
89
90 static struct ui_priv priv;
91
92 static const char *_ae_name[AE_MAX] = {
93         [AE_UNKNOWN] = "UNKNOWN",
94         [AE_CREATE] = "CREATE",
95         [AE_TERMINATE] = "TERMINATE",
96         [AE_PAUSE] = "PAUSE",
97         [AE_RESUME] = "RESUME",
98         [AE_RESET] = "RESET",
99         [AE_LOWMEM_POST] = "LOWMEM_POST",
100         [AE_MEM_FLUSH] = "MEM_FLUSH",
101 };
102
103 static const char *_as_name[] = {
104         [AS_NONE] = "NONE",
105         [AS_CREATED] = "CREATED",
106         [AS_RUNNING] = "RUNNING",
107         [AS_PAUSED] = "PAUSED",
108         [AS_DYING] = "DYING",
109 };
110
111 static bool b_active = FALSE;
112 static bool first_launch = TRUE;
113
114 struct win_node {
115         unsigned int win;
116 #ifdef WAYLAND
117         unsigned int surf;
118 #endif
119         bool bfobscured;
120 };
121
122 static struct ui_wm_rotate wm_rotate;
123 static Eina_Bool __visibility_cb(void *data, int type, void *event);
124
125 static void _send_to_resourced(enum proc_status_type type)
126 {
127         DBusConnection *conn;
128         DBusMessage* msg;
129         DBusError dbus_error;
130
131         dbus_error_init(&dbus_error);
132
133         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
134         if (!conn) {
135                 _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
136                 dbus_error_free(&dbus_error);
137                 return;
138         }
139
140         msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
141                         "org.tizen.resourced.process",
142                         "ProcStatus");
143         if (!msg) {
144                 _ERR("dbus_message_new_signal is failed");
145                 return;
146         }
147
148         if (!dbus_message_append_args(msg,
149                                 DBUS_TYPE_INT32, &type,
150                                 DBUS_TYPE_INT32, &_pid,
151                                 DBUS_TYPE_INVALID)) {
152                 _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
153                                 type, _pid);
154                 dbus_message_unref(msg);
155                 return;
156         }
157
158         if (!dbus_connection_send(conn, msg, NULL))
159                 _ERR("dbus_connection_send is failed");
160
161         dbus_message_unref(msg);
162 }
163
164 static GSList *g_winnode_list;
165
166 #if defined(MEMORY_FLUSH_ACTIVATE)
167 static Eina_Bool __appcore_memory_flush_cb(void *data)
168 {
169         struct ui_priv *ui = (struct ui_priv *)data;
170
171         appcore_flush_memory();
172         ui->mftimer = NULL;
173
174         return ECORE_CALLBACK_CANCEL;
175 }
176
177 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
178 {
179         if (ui->state == AS_PAUSED)
180                 appcore_flush_memory();
181         else
182                 malloc_trim(0);
183
184         return 0;
185 }
186
187 static void __appcore_timer_add(struct ui_priv *ui)
188 {
189         ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
190 }
191
192 static void __appcore_timer_del(struct ui_priv *ui)
193 {
194         if (ui->mftimer) {
195                 ecore_timer_del(ui->mftimer);
196                 ui->mftimer = NULL;
197         }
198 }
199
200 #else
201
202 static int __appcore_low_memory_post_cb(ui_priv *ui)
203 {
204         return -1;
205 }
206
207 #define __appcore_timer_add(ui) 0
208 #define __appcore_timer_del(ui) 0
209
210 #endif
211
212 static void __appcore_efl_memory_flush_cb(void)
213 {
214         _DBG("[APP %d]   __appcore_efl_memory_flush_cb()", _pid);
215         elm_cache_all_flush();
216 }
217 #ifdef WAYLAND
218 static void wl_raise_win(void)
219 {
220         Ecore_Wl_Window *win;
221         unsigned int win_id = appcore_get_main_window();
222
223         _DBG("Raise window: %d", win_id);
224         win = ecore_wl_window_find(win_id);
225         ecore_wl_window_activate(win);
226 }
227
228 static void wl_pause_win(void)
229 {
230         Ecore_Wl_Window *win;
231         GSList *wlist = g_winnode_list;
232         struct win_node *entry = NULL;
233
234         _DBG("Pause window");
235
236         while (wlist) {
237                 entry = wlist->data;
238
239                 _DBG("Pause window: %d", entry->win);
240                 win = ecore_wl_window_find(entry->win);
241                 ecore_wl_window_iconified_set(win, EINA_TRUE);
242
243                 wlist = wlist->next;
244         }
245 }
246
247 #endif
248
249 static void __do_app(enum app_event event, void *data, bundle * b)
250 {
251         int r = -1;
252         struct ui_priv *ui = data;
253
254         _DBG("[APP %d] Event: %d", _pid, event);
255         _ret_if(ui == NULL || event >= AE_MAX);
256         _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
257              _as_name[ui->state]);
258
259         if (event == AE_MEM_FLUSH) {
260                 ui->mfcb();
261                 return;
262         }
263
264         if (event == AE_LOWMEM_POST) {
265                 if (__appcore_low_memory_post_cb(ui) == 0)
266                         return;
267         }
268
269         if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
270                 __appcore_timer_del(ui);
271
272         if (ui->state == AS_DYING) {
273                 _ERR("Skip the event in dying state");
274                 return;
275         }
276
277         if (event == AE_TERMINATE) {
278                 _DBG("[APP %d] TERMINATE", _pid);
279                 elm_exit();
280                 aul_status_update(STATUS_DYING);
281                 return;
282         }
283
284         if (event == AE_RAISE) {
285 #if defined(X11)
286                 x_raise_win(getpid());
287 #elif defined(WAYLAND)
288                 wl_raise_win();
289 #endif
290                 return;
291         }
292
293         if (event == AE_LOWER) {
294 #if defined(X11)
295                 x_pause_win(getpid());
296 #elif defined(WAYLAND)
297                 wl_pause_win();
298 #endif
299                 return;
300         }
301
302         _ret_if(ui->ops == NULL);
303
304         switch (event) {
305         case AE_RESET:
306                 _DBG("[APP %d] RESET", _pid);
307                 ui->pending_data = bundle_dup(b);
308                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
309                 if (ui->ops->reset)
310                         r = ui->ops->reset(b, ui->ops->data);
311                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
312
313                 if (first_launch) {
314                         first_launch = FALSE;
315                 } else {
316                         _INFO("[APP %d] App already running, raise the window", _pid);
317 #ifdef X11
318                         x_raise_win(getpid());
319 #else
320                         wl_raise_win();
321 #endif
322                 }
323                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
324                     ui->name);
325                 break;
326         case AE_PAUSE:
327                 if (ui->state == AS_RUNNING) {
328                         _DBG("[APP %d] PAUSE", _pid);
329                         if (ui->ops->pause)
330                                 r = ui->ops->pause(ui->ops->data);
331                         ui->state = AS_PAUSED;
332                         if (r >= 0 && resource_reclaiming == TRUE)
333                                 __appcore_timer_add(ui);
334                 }
335                 /* TODO : rotation stop */
336                 /* r = appcore_pause_rotation_cb(); */
337                 aul_status_update(STATUS_BG);
338                 _send_to_resourced(PROC_STATUS_BACKGRD);
339                 break;
340         case AE_RESUME:
341                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
342                                 ui->name);
343                 if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
344                         _DBG("[APP %d] RESUME", _pid);
345
346                         if (ui->state == AS_CREATED) {
347                                 appcore_group_reset(ui->pending_data);
348                                 bundle_free(ui->pending_data);
349                                 ui->pending_data = NULL;
350                         } else {
351                                 appcore_group_resume();
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 #ifdef 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 #else
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 #ifdef 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 #else
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
611 #endif
612
613 static Eina_Bool __show_cb(void *data, int type, void *event)
614 {
615 #if defined(WAYLAND)
616         Ecore_Wl_Event_Window_Show *ev;
617
618         ev = event;
619         if (ev->parent_win != 0) {
620                 /* This is child window. Skip!!! */
621                 return ECORE_CALLBACK_PASS_ON;
622         }
623
624         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
625
626         if (!__find_win((unsigned int)ev->win))
627                 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
628         else
629                 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
630
631 #elif defined(X11)
632         Ecore_X_Event_Window_Show *ev;
633         int ret;
634         Ecore_X_Window parent;
635
636         ev = event;
637
638         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
639
640         if (!__find_win((unsigned int)ev->win)) {
641                 /* WM_ROTATE */
642                 if ((priv.wm_rot_supported) && (1 == priv.rot_started))
643                         __set_wm_rotation_support(ev->win, 1);
644                 __add_win((unsigned int)ev->win);
645         } else {
646                 __update_win((unsigned int)ev->win, FALSE);
647         }
648 #endif
649
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 static Eina_Bool __visibility_cb(void *data, int type, void *event)
680 {
681 #if defined(WAYLAND)
682         Ecore_Wl_Event_Window_Visibility_Change *ev;
683         int bvisibility = 0;
684         ev = event;
685         __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
686 #elif defined(X11)
687         Ecore_X_Event_Window_Visibility_Change *ev;
688         int bvisibility = 0;
689
690         ev = event;
691
692         __update_win((unsigned int)ev->win, ev->fully_obscured);
693 #endif
694         bvisibility = __check_visible();
695
696         _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
697
698         if (bvisibility && b_active == FALSE) {
699                 _DBG(" Go to Resume state\n");
700                 b_active = TRUE;
701                 __do_app(AE_RESUME, data, NULL);
702
703         } else if (!bvisibility && b_active == TRUE) {
704                 _DBG(" Go to Pasue state \n");
705                 b_active = FALSE;
706                 __do_app(AE_PAUSE, data, NULL);
707         } else
708                 _DBG(" No change state \n");
709
710         return ECORE_CALLBACK_RENEW;
711
712 }
713
714 #ifdef X11
715 /* WM_ROTATE */
716 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
717 {
718         struct ui_priv *ui = (struct ui_priv *)data;
719         Ecore_X_Event_Client_Message *e = event;
720
721         if (!ui)
722                 return ECORE_CALLBACK_PASS_ON;
723
724         if (e->format != 32)
725                 return ECORE_CALLBACK_PASS_ON;
726
727         if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
728                 if ((ui->wm_rot_supported == 0)
729                         || (ui->rot_started == 0)
730                         || (ui->rot_cb == NULL)) {
731                         return ECORE_CALLBACK_PASS_ON;
732                 }
733
734                 enum appcore_rm rm;
735                 switch (e->data.l[1]) {
736                 case 0:
737                         rm = APPCORE_RM_PORTRAIT_NORMAL;
738                         break;
739                 case 90:
740                         rm = APPCORE_RM_LANDSCAPE_REVERSE;
741                         break;
742                 case 180:
743                         rm = APPCORE_RM_PORTRAIT_REVERSE;
744                         break;
745                 case 270:
746                         rm = APPCORE_RM_LANDSCAPE_NORMAL;
747                         break;
748                 default:
749                         rm = APPCORE_RM_UNKNOWN;
750                         break;
751                 }
752
753                 ui->rot_mode = rm;
754
755                 if (APPCORE_RM_UNKNOWN != rm)
756                         ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
757         }
758
759         return ECORE_CALLBACK_PASS_ON;
760 }
761 #endif
762
763 static void __add_climsg_cb(struct ui_priv *ui)
764 {
765         _ret_if(ui == NULL);
766 #if defined(WAYLAND)
767         ui->hshow =
768             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
769         ui->hhide =
770             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
771         ui->hvchange =
772             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
773                                     __visibility_cb, ui);
774 #elif defined(X11)
775         ui->hshow =
776             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
777         ui->hhide =
778             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
779         ui->hvchange =
780             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
781                                     __visibility_cb, ui);
782
783         /* Add client message callback for WM_ROTATE */
784         if (!__check_wm_rotation_support()) {
785                 ui->hcmsg = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
786                                                 __cmsg_cb, ui);
787                 ui->wm_rot_supported = 1;
788                 appcore_set_wm_rotation(&wm_rotate);
789         }
790 #endif
791 }
792
793 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
794 {
795         int r;
796         char *hwacc = NULL;
797
798         if (argc == NULL || argv == NULL) {
799                 _ERR("argc/argv is NULL");
800                 errno = EINVAL;
801                 return -1;
802         }
803
804 #if !(GLIB_CHECK_VERSION(2, 36, 0))
805         g_type_init();
806 #endif
807         elm_init(*argc, *argv);
808
809         hwacc = getenv("HWACC");
810         if (hwacc == NULL) {
811                 _DBG("elm_config_preferred_engine_set is not called");
812         } else if (strcmp(hwacc, "USE") == 0) {
813 #if defined(WAYLAND)
814                 elm_config_preferred_engine_set("wayland_egl");
815                 _DBG("elm_config_preferred_engine_set : wayland_egl");
816 #elif defined(X11)
817                 elm_config_preferred_engine_set("opengl_x11");
818                 _DBG("elm_config_preferred_engine_set : opengl_x11");
819 #endif
820         } else if (strcmp(hwacc, "NOT_USE") == 0) {
821 #if defined(WAYLAND)
822                 elm_config_preferred_engine_set("wayland_shm");
823                 _DBG("elm_config_preferred_engine_set : wayland_shm");
824 #elif defined(X11)
825                 elm_config_preferred_engine_set("software_x11");
826                 _DBG("elm_config_preferred_engine_set : software_x11");
827 #endif
828         } else {
829                 _DBG("elm_config_preferred_engine_set is not called");
830         }
831
832         r = appcore_init(ui->name, &efl_ops, *argc, *argv);
833         _retv_if(r == -1, -1);
834
835         LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
836         if (ui->ops && ui->ops->create) {
837                 r = ui->ops->create(ui->ops->data);
838                 if (r < 0) {
839                         _ERR("create() return error");
840                         appcore_exit();
841                         if (ui->ops && ui->ops->terminate)
842                                 ui->ops->terminate(ui->ops->data);
843                         errno = ECANCELED;
844                         return -1;
845                 }
846                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
847                     ui->name);
848         }
849         ui->state = AS_CREATED;
850
851         __add_climsg_cb(ui);
852
853         return 0;
854 }
855
856 static void __after_loop(struct ui_priv *ui)
857 {
858         appcore_unset_rotation_cb();
859         appcore_exit();
860
861         if (ui->state == AS_RUNNING) {
862                 _DBG("[APP %d] PAUSE before termination", _pid);
863                 if (ui->ops && ui->ops->pause)
864                         ui->ops->pause(ui->ops->data);
865         }
866
867         if (ui->ops && ui->ops->terminate)
868                 ui->ops->terminate(ui->ops->data);
869
870         ui->state = AS_DYING;
871
872         if (ui->hshow)
873                 ecore_event_handler_del(ui->hshow);
874         if (ui->hhide)
875                 ecore_event_handler_del(ui->hhide);
876         if (ui->hvchange)
877                 ecore_event_handler_del(ui->hvchange);
878
879         __appcore_timer_del(ui);
880
881         elm_shutdown();
882 }
883
884 static int __set_data(struct ui_priv *ui, const char *name,
885                     struct appcore_ops *ops)
886 {
887         if (ui->name) {
888                 _ERR("Mainloop already started");
889                 errno = EINPROGRESS;
890                 return -1;
891         }
892
893         if (name == NULL || name[0] == '\0') {
894                 _ERR("Invalid name");
895                 errno = EINVAL;
896                 return -1;
897         }
898
899         if (ops == NULL) {
900                 _ERR("ops is NULL");
901                 errno = EINVAL;
902                 return -1;
903         }
904
905         ui->name = strdup(name);
906         _retv_if(ui->name == NULL, -1);
907
908         ui->ops = ops;
909         ui->mfcb = __appcore_efl_memory_flush_cb;
910         _pid = getpid();
911
912         /* WM_ROTATE */
913         ui->wm_rot_supported = 0;
914         ui->rot_started = 0;
915         ui->rot_cb = NULL;
916         ui->rot_cb_data = NULL;
917         ui->rot_mode = APPCORE_RM_UNKNOWN;
918
919         return 0;
920 }
921
922 static void __unset_data(struct ui_priv *ui)
923 {
924         if (ui->name)
925                 free((void *)ui->name);
926
927         memset(ui, 0, sizeof(struct ui_priv));
928 }
929
930 /* WM_ROTATE */
931 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
932 {
933         if (cb == NULL) {
934                 errno = EINVAL;
935                 return -1;
936         }
937
938         if ((priv.wm_rot_supported) && (0 == priv.rot_started))
939                 __set_wm_rotation_support(0, 1);
940
941         priv.rot_cb = cb;
942         priv.rot_cb_data = data;
943         priv.rot_started = 1;
944
945         return 0;
946 }
947
948 static int __wm_unset_rotation_cb(void)
949 {
950         if ((priv.wm_rot_supported) && (1 == priv.rot_started))
951                 __set_wm_rotation_support(0, 0);
952
953         priv.rot_cb = NULL;
954         priv.rot_cb_data = NULL;
955         priv.rot_started = 0;
956
957         return 0;
958 }
959
960 static int __wm_get_rotation_state(enum appcore_rm *curr)
961 {
962         if (curr == NULL) {
963                 errno = EINVAL;
964                 return -1;
965         }
966
967         *curr = priv.rot_mode;
968
969         return 0;
970 }
971
972 static int __wm_pause_rotation_cb(void)
973 {
974         if ((priv.rot_started == 1) && (priv.wm_rot_supported))
975                 __set_wm_rotation_support(0, 0);
976
977         priv.rot_started = 0;
978
979         return 0;
980 }
981
982 static int __wm_resume_rotation_cb(void)
983 {
984         if ((priv.rot_started == 0) && (priv.wm_rot_supported))
985                 __set_wm_rotation_support(0, 1);
986
987         priv.rot_started = 1;
988
989         return 0;
990 }
991
992 static struct ui_wm_rotate wm_rotate = {
993         __wm_set_rotation_cb,
994         __wm_unset_rotation_cb,
995         __wm_get_rotation_state,
996         __wm_pause_rotation_cb,
997         __wm_resume_rotation_cb
998 };
999
1000 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1001                                 struct appcore_ops *ops)
1002 {
1003         int r;
1004
1005         LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
1006
1007         r = __set_data(&priv, name, ops);
1008         _retv_if(r == -1, -1);
1009
1010         r = __before_loop(&priv, argc, argv);
1011         if (r == -1) {
1012                 __unset_data(&priv);
1013                 return -1;
1014         }
1015
1016         elm_run();
1017
1018         aul_status_update(STATUS_DYING);
1019
1020         __after_loop(&priv);
1021
1022         __unset_data(&priv);
1023
1024         return 0;
1025 }
1026
1027 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1028 {
1029         resource_reclaiming = enable;
1030
1031         return 0;
1032 }
1033
1034 EXPORT_API int appcore_set_app_state(int state)
1035 {
1036         priv.state = state;
1037
1038         tmp_val = 1;
1039
1040         return 0;
1041 }
1042
1043 EXPORT_API unsigned int appcore_get_main_window()
1044 {
1045         struct win_node *entry = NULL;
1046
1047         if (g_winnode_list != NULL) {
1048                 entry = g_winnode_list->data;
1049                 return (unsigned int) entry->win;
1050         }
1051
1052         return 0;
1053 }
1054 #ifdef WAYLAND
1055 EXPORT_API unsigned int appcore_get_main_surface()
1056 {
1057         struct win_node *entry = NULL;
1058
1059         if (g_winnode_list != NULL) {
1060                 entry = g_winnode_list->data;
1061                 return (unsigned int) entry->surf;
1062         }
1063
1064         return 0;
1065 }
1066 #endif