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