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