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