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