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