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