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