Add pause, terminate bgapp event
[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 #ifdef WAYLAND
33 #include <Ecore_Wayland.h>
34 #endif
35
36 #ifdef X11
37 #include <X11/Xatom.h>
38 #include <X11/Xlib.h>
39 #include <Ecore_X.h>
40 #endif
41
42 #include <Ecore.h>
43 #include <Ecore_Evas.h>
44 #include <Ecore_Input_Evas.h>
45 #include <Elementary.h>
46 #include <glib-object.h>
47 #include <malloc.h>
48 #include <glib.h>
49 #include <dbus/dbus.h>
50 #include <stdbool.h>
51 #include <aul.h>
52 #include "appcore-internal.h"
53 #include "appcore-efl.h"
54
55 static pid_t _pid;
56
57 static bool resource_reclaiming = TRUE;
58 static int tmp_val = 0;
59
60 enum proc_status_type { /** cgroup command 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         Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
76
77         Ecore_Timer *mftimer;   /* Ecore Timer for memory flushing */
78
79         struct appcore_ops *ops;
80         void (*mfcb) (void);    /* Memory Flushing Callback */
81
82         /* WM_ROTATE */
83         int wm_rot_supported;
84         int rot_started;
85         int (*rot_cb) (void *event_info, enum appcore_rm, void *);
86         void *rot_cb_data;
87         enum appcore_rm rot_mode;
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         bool bfobscured;
117 };
118
119 static struct ui_wm_rotate wm_rotate;
120 static Eina_Bool __visibility_cb(void *data, int type, void *event);
121
122
123 static void _send_to_resourced(enum proc_status_type type)
124 {
125         DBusConnection *conn;
126         DBusMessage* msg;
127         DBusError dbus_error;
128
129         dbus_error_init(&dbus_error);
130
131         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
132         if (!conn) {
133                 _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
134                 dbus_error_free(&dbus_error);
135                 return;
136         }
137
138         msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
139                         "org.tizen.resourced.process",
140                         "ProcStatus");
141         if (!msg) {
142                 _ERR("dbus_message_new_signal is failed");
143                 return;
144         }
145
146         if (!dbus_message_append_args(msg,
147                                 DBUS_TYPE_INT32, &type,
148                                 DBUS_TYPE_INT32, &_pid,
149                                 DBUS_TYPE_INVALID)) {
150                 _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
151                                 type, _pid);
152                 dbus_message_unref(msg);
153                 return;
154         }
155
156         if (!dbus_connection_send (conn, msg, NULL)) {
157                 _ERR("dbus_connection_send is failed");
158         }
159
160         dbus_message_unref(msg);
161 }
162
163 static GSList *g_winnode_list;
164
165 #if defined(MEMORY_FLUSH_ACTIVATE)
166 static Eina_Bool __appcore_memory_flush_cb(void *data)
167 {
168         struct ui_priv *ui = (struct ui_priv *)data;
169
170         appcore_flush_memory();
171         ui->mftimer = NULL;
172
173         return ECORE_CALLBACK_CANCEL;
174 }
175
176 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
177 {
178         if (ui->state == AS_PAUSED) {
179                 appcore_flush_memory();
180         } else {
181                 malloc_trim(0);
182         }
183
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 unsigned int win_id;
219
220 static void wl_raise_win()
221 {
222         _DBG("Raise window : %d", win_id);
223         Ecore_Wl_Window *win;
224         win = ecore_wl_window_find(win_id);
225         ecore_wl_window_activate(win);
226 }
227 #endif
228
229 static void __do_app(enum app_event event, void *data, bundle * b)
230 {
231         int r = -1;
232         struct ui_priv *ui = data;
233
234         _DBG("[APP %d] Event: %d", _pid, event);
235         _ret_if(ui == NULL || event >= AE_MAX);
236         _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
237              _as_name[ui->state]);
238
239         if (event == AE_MEM_FLUSH) {
240                 ui->mfcb();
241                 return;
242         }
243
244         if (event == AE_LOWMEM_POST) {
245                 if (__appcore_low_memory_post_cb(ui) == 0)
246                         return;
247         }
248
249         if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
250                 __appcore_timer_del(ui);
251
252         if (event == AE_TERMINATE) {
253                 _DBG("[APP %d] TERMINATE", _pid);
254                 ui->state = AS_DYING;
255                 elm_exit();
256                 return;
257         }
258
259         if (event == AE_RAISE) {
260 #ifdef X11
261                 x_raise_win(getpid());
262 #else
263                 wl_raise_win();
264 #endif
265                 return;
266         }
267
268         if (event == AE_LOWER) {
269 #ifdef X11
270                 x_pause_win(getpid());
271                 return;
272 #endif
273                 /* TODO: wayland support */
274         }
275
276         _ret_if(ui->ops == NULL);
277
278         switch (event) {
279         case AE_RESET:
280                 _DBG("[APP %d] RESET", _pid);
281                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
282                 if (ui->ops->reset)
283                         r = ui->ops->reset(b, ui->ops->data);
284                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
285
286                 if (first_launch) {
287                         first_launch = FALSE;
288                         _INFO("[APP %d] Initial Launching, call the resume_cb", _pid);
289                         if (ui->ops->resume)
290                                 r = ui->ops->resume(ui->ops->data);
291                 } else {
292                         _INFO("[APP %d] App already running, raise the window", _pid);
293 #ifdef X11
294                         x_raise_win(getpid());
295 #else
296                         wl_raise_win();
297 #endif
298                         if (ui->state == AS_PAUSED) {
299                                 _INFO("[APP %d] Call the resume_cb", _pid);
300                                 if (ui->ops->resume)
301                                         r = ui->ops->resume(ui->ops->data);
302                         }
303                 }
304
305                 ui->state = AS_RUNNING;
306                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
307                     ui->name);
308                 break;
309         case AE_PAUSE:
310                 if (ui->state == AS_RUNNING) {
311                         _DBG("[APP %d] PAUSE", _pid);
312                         if (ui->ops->pause)
313                                 r = ui->ops->pause(ui->ops->data);
314                         ui->state = AS_PAUSED;
315                         if(r >= 0 && resource_reclaiming == TRUE)
316                                 __appcore_timer_add(ui);
317                 }
318                 /* TODO : rotation stop */
319                 //r = appcore_pause_rotation_cb();
320                 _send_to_resourced(PROC_STATUS_BACKGRD);
321                 break;
322         case AE_RESUME:
323                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
324                     ui->name);
325                 if (ui->state == AS_PAUSED || tmp_val == 1) {
326                         _DBG("[APP %d] RESUME", _pid);
327                         if (ui->ops->resume)
328                                 r = ui->ops->resume(ui->ops->data);
329                         ui->state = AS_RUNNING;
330                          tmp_val = 0;
331                 }
332                 /*TODO : rotation start*/
333                 //r = appcore_resume_rotation_cb();
334                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
335                     ui->name);
336                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
337                     ui->name);
338                 _send_to_resourced(PROC_STATUS_FOREGRD);
339                 break;
340         case AE_TERMINATE_BGAPP:
341                 if (ui->state == AS_PAUSED) {
342                         _DBG("[APP %d] is paused. TERMINATE", _pid);
343                         ui->state = AS_DYING;
344                         aul_status_update(STATUS_DYING);
345                         elm_exit();
346                 } else if (ui->state == AS_RUNNING) {
347                         _DBG("[APP %d] is running.", _pid);
348                 } else {
349                         _DBG("[APP %d] is another state", _pid);
350                 }
351                 break;
352         default:
353                 /* do nothing */
354                 break;
355         }
356 }
357
358 static struct ui_ops efl_ops = {
359         .data = &priv,
360         .cb_app = __do_app,
361 };
362
363
364 static bool __check_visible(void)
365 {
366         GSList *iter = NULL;
367         struct win_node *entry = NULL;
368
369         _DBG("[EVENT_TEST][EVENT] __check_visible\n");
370         
371         for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
372                 entry = iter->data;     
373                 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
374                 if(entry->bfobscured == FALSE)
375                         return TRUE;            
376         }
377         return FALSE;
378 }
379
380 static GSList *__find_win(unsigned int win)
381 {
382         GSList *iter;
383         struct win_node *t;
384
385         for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
386                 t = iter->data;
387                 if (t && t->win == win)
388                         return iter;
389         }
390
391         return NULL;
392 }
393
394 static bool __add_win(unsigned int win)
395 {
396         struct win_node *t;
397         GSList *f;
398
399         _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
400
401         f = __find_win(win);
402         if (f) {
403                 errno = ENOENT;
404                 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
405                 return FALSE;
406         }
407
408         t = calloc(1, sizeof(struct win_node));
409         if (t == NULL)
410                 return FALSE;
411
412         t->win = win;
413         t->bfobscured = FALSE;
414
415         g_winnode_list = g_slist_append(g_winnode_list, t);
416
417         return TRUE;
418 }
419
420 static bool __delete_win(unsigned int win)
421 {
422         GSList *f;
423
424         f = __find_win(win);
425         if (!f) {
426                 errno = ENOENT;
427                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
428                                 win);
429                 return FALSE;
430         }
431
432         g_winnode_list = g_slist_delete_link(g_winnode_list, f);
433
434         free(f->data);
435
436         return TRUE;
437 }
438
439 static bool __update_win(unsigned int win, bool bfobscured)
440 {
441         GSList *f;
442         struct win_node *t;
443
444         _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
445              bfobscured);
446
447         f = __find_win(win);
448         if (!f) {
449                 errno = ENOENT;
450                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
451                 return FALSE;
452         }
453
454         g_winnode_list = g_slist_remove_link(g_winnode_list, f);
455
456         t = f->data;
457         t->win = win;
458         t->bfobscured = bfobscured;
459
460         g_winnode_list = g_slist_concat(g_winnode_list, f);
461
462         return TRUE;
463
464 }
465
466 /* WM_ROTATE */
467 #ifdef X11
468 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
469 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
470
471 static int __check_wm_rotation_support(void)
472 {
473         _DBG("Disable window manager rotation");
474         return -1;
475
476         Ecore_X_Window root, win, win2;
477         int ret;
478
479         if (!_WM_WINDOW_ROTATION_SUPPORTED) {
480                 _WM_WINDOW_ROTATION_SUPPORTED =
481                                         ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
482         }
483
484         if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
485                 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
486                                         ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
487         }
488
489         root = ecore_x_window_root_first_get();
490         ret = ecore_x_window_prop_xid_get(root,
491                         _WM_WINDOW_ROTATION_SUPPORTED,
492                         ECORE_X_ATOM_WINDOW,
493                         &win, 1);
494         if ((ret == 1) && (win))
495         {
496                 ret = ecore_x_window_prop_xid_get(win,
497                                 _WM_WINDOW_ROTATION_SUPPORTED,
498                                 ECORE_X_ATOM_WINDOW,
499                                 &win2, 1);
500                 if ((ret == 1) && (win2 == win))
501                         return 0;
502         }
503
504         return -1;
505 }
506
507 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
508 {
509         GSList *iter = NULL;
510         struct win_node *entry = NULL;
511
512         if (0 == win) {
513                 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
514                         entry = iter->data;
515                         if (entry->win) {
516                                 ecore_x_window_prop_card32_set(entry->win,
517                                                 _WM_WINDOW_ROTATION_SUPPORTED,
518                                                 &set, 1);
519                         }
520                 }
521         } else {
522                 ecore_x_window_prop_card32_set(win,
523                                 _WM_WINDOW_ROTATION_SUPPORTED,
524                                 &set, 1);
525         }
526 }
527
528 #endif
529
530 static Eina_Bool __show_cb(void *data, int type, void *event)
531 {
532 #ifdef WAYLAND
533         Ecore_Wl_Event_Window_Show *ev;
534
535         ev = event;
536
537         if (ev->parent_win != 0)
538         {
539                 // This is child window. Skip!!!
540                 return ECORE_CALLBACK_PASS_ON;
541         }
542
543         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
544
545         if (!__find_win((unsigned int)ev->win))
546                 __add_win((unsigned int)ev->win);
547         else
548                 __update_win((unsigned int)ev->win, FALSE);
549
550         win_id = ev->win;
551 #else
552         Ecore_X_Event_Window_Show *ev;
553         int ret;
554         Ecore_X_Window parent;
555
556         ev = event;
557
558         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
559
560         if (!__find_win((unsigned int)ev->win)) {
561                 /* WM_ROTATE */
562                 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
563                         __set_wm_rotation_support(ev->win, 1);
564                 }
565                 __add_win((unsigned int)ev->win);
566         }
567         else
568                 __update_win((unsigned int)ev->win, FALSE);
569 #endif
570
571         return ECORE_CALLBACK_RENEW;
572 }
573
574 static Eina_Bool __hide_cb(void *data, int type, void *event)
575 {
576 #ifdef WAYLAND
577         Ecore_Wl_Event_Window_Hide *ev;
578 #else
579         Ecore_X_Event_Window_Hide *ev;
580 #endif
581         int bvisibility = 0;
582
583         ev = event;
584
585         _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
586
587         if (__find_win((unsigned int)ev->win)) {
588                 __delete_win((unsigned int)ev->win);
589                 bvisibility = __check_visible();
590                 if (!bvisibility && b_active == TRUE) {
591                         _DBG(" Go to Pasue state \n");
592                         b_active = FALSE;
593                         __do_app(AE_PAUSE, data, NULL);
594                 }
595         }
596
597         return ECORE_CALLBACK_RENEW;
598 }
599
600 static Eina_Bool __visibility_cb(void *data, int type, void *event)
601 {
602 #ifdef WAYLAND
603         Ecore_Wl_Event_Window_Visibility_Change *ev;
604         int bvisibility = 0;
605         ev = event;
606         __update_win((unsigned int)ev->win, ev->fully_obscured);
607 #else
608         Ecore_X_Event_Window_Visibility_Change *ev;
609         int bvisibility = 0;
610
611         ev = event;
612
613         __update_win((unsigned int)ev->win, ev->fully_obscured);
614 #endif
615         bvisibility = __check_visible();
616
617         _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
618
619         if (bvisibility && b_active == FALSE) {
620                 _DBG(" Go to Resume state\n");
621                 b_active = TRUE;
622                 __do_app(AE_RESUME, data, NULL);
623
624         } else if (!bvisibility && b_active == TRUE) {
625                 _DBG(" Go to Pasue state \n");
626                 b_active = FALSE;
627                 __do_app(AE_PAUSE, data, NULL);
628         } else
629                 _DBG(" No change state \n");
630
631         return ECORE_CALLBACK_RENEW;
632
633 }
634
635 #ifdef X11
636 /* WM_ROTATE */
637 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
638 {
639         struct ui_priv *ui = (struct ui_priv *)data;
640         Ecore_X_Event_Client_Message *e = event;
641
642         if (!ui) return ECORE_CALLBACK_PASS_ON;
643         if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
644         if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
645                 if ((0 == ui->wm_rot_supported) ||
646                         (0 == ui->rot_started) ||
647                         (NULL == ui->rot_cb)) {
648                         return ECORE_CALLBACK_PASS_ON;
649                 }
650
651                 enum appcore_rm rm;
652                 switch (e->data.l[1])
653                 {
654                         case   0: rm = APPCORE_RM_PORTRAIT_NORMAL;   break;
655                         case  90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
656                         case 180: rm = APPCORE_RM_PORTRAIT_REVERSE;  break;
657                         case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL;  break;
658                         default:  rm = APPCORE_RM_UNKNOWN;           break;
659                 }
660
661                 ui->rot_mode = rm;
662
663                 if (APPCORE_RM_UNKNOWN != rm) {
664                         ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
665                 }
666         }
667
668         return ECORE_CALLBACK_PASS_ON;
669 }
670 #endif
671
672 static void __add_climsg_cb(struct ui_priv *ui)
673 {
674         _ret_if(ui == NULL);
675 #ifdef WAYLAND
676         ui->hshow =
677             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
678         ui->hhide =
679             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
680         ui->hvchange =
681             ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
682                                     __visibility_cb, ui);
683 #else
684         ui->hshow =
685             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
686         ui->hhide =
687             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
688         ui->hvchange =
689             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
690                                     __visibility_cb, ui);
691
692         /* Add client message callback for WM_ROTATE */
693         if(!__check_wm_rotation_support())
694         {
695                 ui->hcmsg =
696                         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
697                 ui->wm_rot_supported = 1;
698                 appcore_set_wm_rotation(&wm_rotate);
699         }
700 #endif
701 }
702
703 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
704 {
705         int r;
706         char *hwacc = NULL;
707
708         if (argc == NULL || argv == NULL) {
709                 _ERR("argc/argv is NULL");
710                 errno = EINVAL;
711                 return -1;
712         }
713
714         g_type_init();
715         elm_init(*argc, *argv);
716
717         hwacc = getenv("HWACC");
718
719         if(hwacc == NULL) {
720                 _DBG("elm_config_preferred_engine_set is not called");
721         } else if(strcmp(hwacc, "USE") == 0) {
722 #ifdef WAYLAND
723                 elm_config_preferred_engine_set("wayland_egl");
724                 _DBG("elm_config_preferred_engine_set : wayland_egl");
725 #else
726                 elm_config_preferred_engine_set("opengl_x11");
727                 _DBG("elm_config_preferred_engine_set : opengl_x11");
728 #endif
729         } else if(strcmp(hwacc, "NOT_USE") == 0) {
730 #ifdef WAYLAND
731                 elm_config_preferred_engine_set("wayland_shm");
732                 _DBG("elm_config_preferred_engine_set : wayland_shm");
733 #else
734                 elm_config_preferred_engine_set("software_x11");
735                 _DBG("elm_config_preferred_engine_set : software_x11");
736 #endif
737         } else {
738                 _DBG("elm_config_preferred_engine_set is not called");
739         }
740
741         r = appcore_init(ui->name, &efl_ops, *argc, *argv);
742         _retv_if(r == -1, -1);
743
744         LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
745         if (ui->ops && ui->ops->create) {
746                 r = ui->ops->create(ui->ops->data);
747                 if (r == -1) {
748                         _ERR("create() return error");
749                         appcore_exit();
750                         errno = ECANCELED;
751                         return -1;
752                 }
753                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
754                     ui->name);
755         }
756         ui->state = AS_CREATED;
757
758         __add_climsg_cb(ui);
759
760         return 0;
761 }
762
763 static void __after_loop(struct ui_priv *ui)
764 {
765         appcore_unset_rotation_cb();
766         appcore_exit();
767
768         if (ui->state == AS_RUNNING) {
769                 _DBG("[APP %d] PAUSE before termination", _pid);
770                 if (ui->ops && ui->ops->pause)
771                         ui->ops->pause(ui->ops->data);
772         }
773
774         if (ui->ops && ui->ops->terminate)
775                 ui->ops->terminate(ui->ops->data);
776
777         if (ui->hshow)
778                 ecore_event_handler_del(ui->hshow);
779         if (ui->hhide)
780                 ecore_event_handler_del(ui->hhide);
781         if (ui->hvchange)
782                 ecore_event_handler_del(ui->hvchange);
783
784         __appcore_timer_del(ui);
785
786         elm_shutdown();
787 }
788
789 static int __set_data(struct ui_priv *ui, const char *name,
790                     struct appcore_ops *ops)
791 {
792         if (ui->name) {
793                 _ERR("Mainloop already started");
794                 errno = EINPROGRESS;
795                 return -1;
796         }
797
798         if (name == NULL || name[0] == '\0') {
799                 _ERR("Invalid name");
800                 errno = EINVAL;
801                 return -1;
802         }
803
804         if (ops == NULL) {
805                 _ERR("ops is NULL");
806                 errno = EINVAL;
807                 return -1;
808         }
809
810         ui->name = strdup(name);
811         _retv_if(ui->name == NULL, -1);
812
813         ui->ops = ops;
814
815         ui->mfcb = __appcore_efl_memory_flush_cb;
816
817         _pid = getpid();
818
819         /* WM_ROTATE */
820         ui->wm_rot_supported = 0;
821         ui->rot_started = 0;
822         ui->rot_cb = NULL;
823         ui->rot_cb_data = NULL;
824         ui->rot_mode = APPCORE_RM_UNKNOWN;
825
826         return 0;
827 }
828
829 static void __unset_data(struct ui_priv *ui)
830 {
831         if (ui->name)
832                 free((void *)ui->name);
833
834         memset(ui, 0, sizeof(struct ui_priv));
835 }
836
837 /* WM_ROTATE */
838 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
839 {
840         if (cb == NULL) {
841                 errno = EINVAL;
842                 return -1;
843         }
844
845         if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
846                 __set_wm_rotation_support(0, 1);
847         }
848
849         priv.rot_cb = cb;
850         priv.rot_cb_data = data;
851         priv.rot_started = 1;
852
853         return 0;
854 }
855
856 static int __wm_unset_rotation_cb(void)
857 {
858         if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
859                 __set_wm_rotation_support(0, 0);
860         }
861
862         priv.rot_cb = NULL;
863         priv.rot_cb_data = NULL;
864         priv.rot_started = 0;
865
866         return 0;
867 }
868
869 static int __wm_get_rotation_state(enum appcore_rm *curr)
870 {
871         if (curr == NULL) {
872                 errno = EINVAL;
873                 return -1;
874         }
875
876         *curr = priv.rot_mode;
877
878         return 0;
879 }
880
881 static int __wm_pause_rotation_cb(void)
882 {
883         if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
884                 __set_wm_rotation_support(0, 0);
885         }
886
887         priv.rot_started = 0;
888
889         return 0;
890 }
891
892 static int __wm_resume_rotation_cb(void)
893 {
894         if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
895                 __set_wm_rotation_support(0, 1);
896         }
897
898         priv.rot_started = 1;
899
900         return 0;
901 }
902
903 static struct ui_wm_rotate wm_rotate = {
904         __wm_set_rotation_cb,
905         __wm_unset_rotation_cb,
906         __wm_get_rotation_state,
907         __wm_pause_rotation_cb,
908         __wm_resume_rotation_cb
909 };
910
911 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
912                                 struct appcore_ops *ops)
913 {
914         int r;
915
916         LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
917
918         r = __set_data(&priv, name, ops);
919         _retv_if(r == -1, -1);
920
921         r = __before_loop(&priv, argc, argv);
922         if (r == -1) {
923                 __unset_data(&priv);
924                 return -1;
925         }
926
927         elm_run();
928
929         aul_status_update(STATUS_DYING);
930
931         __after_loop(&priv);
932
933         __unset_data(&priv);
934
935         return 0;
936 }
937
938 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
939 {
940         resource_reclaiming = enable;
941
942         return 0;
943 }
944
945 EXPORT_API int appcore_set_app_state(int state)
946 {
947         priv.state = state;
948
949         tmp_val = 1;
950
951         return 0;
952 }