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