4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <X11/Xatom.h>
29 #include <Elementary.h>
30 #include <glib-object.h>
35 #include "appcore-internal.h"
36 #include "appcore-efl.h"
40 static bool resource_reclaiming = TRUE;
46 Ecore_Event_Handler *hshow;
47 Ecore_Event_Handler *hhide;
48 Ecore_Event_Handler *hvchange;
50 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
52 struct appcore_ops *ops;
53 void (*mfcb) (void); /* Memory Flushing Callback */
56 static struct ui_priv priv;
58 static const char *_ae_name[AE_MAX] = {
59 [AE_UNKNOWN] = "UNKNOWN",
60 [AE_CREATE] = "CREATE",
61 [AE_TERMINATE] = "TERMINATE",
63 [AE_RESUME] = "RESUME",
65 [AE_LOWMEM_POST] = "LOWMEM_POST",
66 [AE_MEM_FLUSH] = "MEM_FLUSH",
69 static const char *_as_name[] = {
71 [AS_CREATED] = "CREATED",
72 [AS_RUNNING] = "RUNNING",
73 [AS_PAUSED] = "PAUSED",
77 static bool b_active = 1;
83 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
85 struct win_node *a = (struct win_node *)data1;
86 struct win_node *b = (struct win_node *)data2;
87 return (int)((a->win)-(b->win));
90 GSList *g_winnode_list;
92 #if defined(MEMORY_FLUSH_ACTIVATE)
93 static Eina_Bool __appcore_memory_flush_cb(void *data)
95 struct ui_priv *ui = (struct ui_priv *)data;
97 appcore_flush_memory();
101 return ECORE_CALLBACK_CANCEL;
104 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
106 if (ui->state == AS_PAUSED) {
107 appcore_flush_memory();
115 static void __appcore_timer_add(struct ui_priv *ui)
117 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
120 static void __appcore_timer_del(struct ui_priv *ui)
123 ecore_timer_del(ui->mftimer);
130 static int __appcore_low_memory_post_cb(ui_priv *ui)
135 #define __appcore_timer_add(ui) 0
136 #define __appcore_timer_del(ui) 0
140 static void __appcore_efl_memory_flush_cb(void)
142 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
143 elm_cache_all_flush();
146 static void __do_app(enum app_event event, void *data, bundle * b)
149 struct ui_priv *ui = data;
151 _DBG("[APP %d] Event: %d", _pid, event);
152 _ret_if(ui == NULL || event >= AE_MAX);
153 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
154 _as_name[ui->state]);
156 if (event == AE_MEM_FLUSH) {
161 if (event == AE_LOWMEM_POST) {
162 if (__appcore_low_memory_post_cb(ui) == 0)
166 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
167 __appcore_timer_del(ui);
169 if (event == AE_TERMINATE) {
170 _DBG("[APP %d] TERMINATE", _pid);
171 ui->state = AS_DYING;
176 _ret_if(ui->ops == NULL);
180 _DBG("[APP %d] RESET", _pid);
181 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
184 r = ui->ops->reset(b, ui->ops->data);
185 ui->state = AS_RUNNING;
186 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
190 if (ui->state == AS_RUNNING) {
191 _DBG("[APP %d] PAUSE", _pid);
193 r = ui->ops->pause(ui->ops->data);
194 ui->state = AS_PAUSED;
195 if(r >= 0 && resource_reclaiming == TRUE)
196 __appcore_timer_add(ui);
198 /* TODO : rotation stop */
199 //r = appcore_pause_rotation_cb();
201 sysman_inform_backgrd();
204 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
206 if (ui->state == AS_PAUSED) {
207 _DBG("[APP %d] RESUME", _pid);
209 r = ui->ops->resume(ui->ops->data);
210 ui->state = AS_RUNNING;
212 /*TODO : rotation start*/
213 //r = appcore_resume_rotation_cb();
214 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
216 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
218 sysman_inform_foregrd();
227 static struct ui_ops efl_ops = {
233 static bool __check_visible(void)
236 struct win_node *entry = NULL;
238 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
240 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
242 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
243 if(entry->bfobscured == FALSE)
249 static bool __exist_win(unsigned int win)
251 struct win_node temp;
256 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
265 static bool __add_win(unsigned int win)
270 t = calloc(1, sizeof(struct win_node));
275 t->bfobscured = FALSE;
277 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
279 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
283 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
288 g_winnode_list = g_slist_append(g_winnode_list, t);
294 static bool __delete_win(unsigned int win)
296 struct win_node temp;
301 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
304 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
309 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
316 static bool __update_win(unsigned int win, bool bfobscured)
318 struct win_node temp;
323 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
328 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
332 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
336 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
340 t = calloc(1, sizeof(struct win_node));
345 t->bfobscured = bfobscured;
347 g_winnode_list = g_slist_append(g_winnode_list, t);
353 Ecore_X_Atom atom_parent;
355 static Eina_Bool __show_cb(void *data, int type, void *event)
357 Ecore_X_Event_Window_Show *ev;
359 Ecore_X_Window parent;
363 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
366 // This is child window. Skip!!!
367 return ECORE_CALLBACK_PASS_ON;
370 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
372 if (!__exist_win((unsigned int)ev->win))
373 __add_win((unsigned int)ev->win);
375 __update_win((unsigned int)ev->win, FALSE);
377 return ECORE_CALLBACK_RENEW;
380 static Eina_Bool __hide_cb(void *data, int type, void *event)
382 Ecore_X_Event_Window_Hide *ev;
387 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
389 if (__exist_win((unsigned int)ev->win)) {
390 __delete_win((unsigned int)ev->win);
392 bvisibility = __check_visible();
393 if (!bvisibility && b_active == 1) {
394 _DBG(" Go to Pasue state \n");
396 __do_app(AE_PAUSE, data, NULL);
400 return ECORE_CALLBACK_RENEW;
403 static Eina_Bool __visibility_cb(void *data, int type, void *event)
405 Ecore_X_Event_Window_Visibility_Change *ev;
410 __update_win((unsigned int)ev->win, ev->fully_obscured);
411 bvisibility = __check_visible();
413 if (bvisibility && b_active == 0) {
414 _DBG(" Go to Resume state\n");
416 __do_app(AE_RESUME, data, NULL);
418 } else if (!bvisibility && b_active == 1) {
419 _DBG(" Go to Pasue state \n");
421 __do_app(AE_PAUSE, data, NULL);
423 _DBG(" No change state \n");
425 return ECORE_CALLBACK_RENEW;
429 static void __add_climsg_cb(struct ui_priv *ui)
433 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
440 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
442 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
444 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
445 __visibility_cb, ui);
449 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
453 if (argc == NULL || argv == NULL) {
454 _ERR("argc/argv is NULL");
460 elm_init(*argc, *argv);
462 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
463 _retv_if(r == -1, -1);
465 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
466 if (ui->ops && ui->ops->create) {
467 r = ui->ops->create(ui->ops->data);
469 _ERR("create() return error");
474 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
477 ui->state = AS_CREATED;
484 static void __after_loop(struct ui_priv *ui)
486 appcore_unset_rotation_cb();
489 if (ui->ops && ui->ops->terminate)
490 ui->ops->terminate(ui->ops->data);
493 ecore_event_handler_del(ui->hshow);
495 ecore_event_handler_del(ui->hhide);
497 ecore_event_handler_del(ui->hvchange);
499 __appcore_timer_del(ui);
504 static int __set_data(struct ui_priv *ui, const char *name,
505 struct appcore_ops *ops)
508 _ERR("Mainloop already started");
513 if (name == NULL || name[0] == '\0') {
514 _ERR("Invalid name");
525 ui->name = strdup(name);
526 _retv_if(ui->name == NULL, -1);
530 ui->mfcb = __appcore_efl_memory_flush_cb;
537 static void __unset_data(struct ui_priv *ui)
540 free((void *)ui->name);
542 memset(ui, 0, sizeof(struct ui_priv));
545 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
546 struct appcore_ops *ops)
550 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
552 r = __set_data(&priv, name, ops);
553 _retv_if(r == -1, -1);
555 r = __before_loop(&priv, argc, argv);
570 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
572 resource_reclaiming = enable;