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