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