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