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