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 = 0;
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();
100 return ECORE_CALLBACK_CANCEL;
103 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
105 if (ui->state == AS_PAUSED) {
106 appcore_flush_memory();
114 static void __appcore_timer_add(struct ui_priv *ui)
116 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
119 static void __appcore_timer_del(struct ui_priv *ui)
122 ecore_timer_del(ui->mftimer);
129 static int __appcore_low_memory_post_cb(ui_priv *ui)
134 #define __appcore_timer_add(ui) 0
135 #define __appcore_timer_del(ui) 0
139 static void __appcore_efl_memory_flush_cb(void)
141 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
142 elm_cache_all_flush();
145 static void __do_app(enum app_event event, void *data, bundle * b)
148 struct ui_priv *ui = data;
150 _DBG("[APP %d] Event: %d", _pid, event);
151 _ret_if(ui == NULL || event >= AE_MAX);
152 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
153 _as_name[ui->state]);
155 if (event == AE_MEM_FLUSH) {
160 if (event == AE_LOWMEM_POST) {
161 if (__appcore_low_memory_post_cb(ui) == 0)
165 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
166 __appcore_timer_del(ui);
168 if (event == AE_TERMINATE) {
169 _DBG("[APP %d] TERMINATE", _pid);
170 ui->state = AS_DYING;
175 _ret_if(ui->ops == NULL);
179 _DBG("[APP %d] RESET", _pid);
180 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
183 r = ui->ops->reset(b, ui->ops->data);
184 ui->state = AS_RUNNING;
185 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
189 if (ui->state == AS_RUNNING) {
190 _DBG("[APP %d] PAUSE", _pid);
192 r = ui->ops->pause(ui->ops->data);
193 ui->state = AS_PAUSED;
194 if(r >= 0 && resource_reclaiming == TRUE)
195 __appcore_timer_add(ui);
197 /* TODO : rotation stop */
198 //r = appcore_pause_rotation_cb();
200 sysman_inform_backgrd();
203 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
205 if (ui->state == AS_PAUSED) {
206 _DBG("[APP %d] RESUME", _pid);
208 r = ui->ops->resume(ui->ops->data);
209 ui->state = AS_RUNNING;
211 /*TODO : rotation start*/
212 //r = appcore_resume_rotation_cb();
213 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
215 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
217 sysman_inform_foregrd();
226 static struct ui_ops efl_ops = {
232 static bool __check_visible(void)
235 struct win_node *entry = NULL;
237 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
239 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
241 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
242 if(entry->bfobscured == FALSE)
248 static bool __exist_win(unsigned int win)
250 struct win_node temp;
255 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
264 static bool __add_win(unsigned int win)
269 t = calloc(1, sizeof(struct win_node));
274 t->bfobscured = FALSE;
276 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
278 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
282 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
287 g_winnode_list = g_slist_append(g_winnode_list, t);
293 static bool __delete_win(unsigned int win)
295 struct win_node temp;
300 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
303 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
308 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
315 static bool __update_win(unsigned int win, bool bfobscured)
317 struct win_node temp;
322 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
327 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
331 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
335 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
339 t = calloc(1, sizeof(struct win_node));
344 t->bfobscured = bfobscured;
346 g_winnode_list = g_slist_append(g_winnode_list, t);
352 Ecore_X_Atom atom_parent;
354 static Eina_Bool __show_cb(void *data, int type, void *event)
356 Ecore_X_Event_Window_Show *ev;
358 Ecore_X_Window parent;
362 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
365 // This is child window. Skip!!!
366 return ECORE_CALLBACK_PASS_ON;
369 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
371 if (!__exist_win((unsigned int)ev->win))
372 __add_win((unsigned int)ev->win);
374 __update_win((unsigned int)ev->win, FALSE);
376 return ECORE_CALLBACK_RENEW;
379 static Eina_Bool __hide_cb(void *data, int type, void *event)
381 Ecore_X_Event_Window_Hide *ev;
386 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
388 if (__exist_win((unsigned int)ev->win)) {
389 __delete_win((unsigned int)ev->win);
391 bvisibility = __check_visible();
392 if (!bvisibility && b_active == 1) {
393 _DBG(" Go to Pasue state \n");
395 __do_app(AE_PAUSE, data, NULL);
399 return ECORE_CALLBACK_RENEW;
402 static Eina_Bool __visibility_cb(void *data, int type, void *event)
404 Ecore_X_Event_Window_Visibility_Change *ev;
409 __update_win((unsigned int)ev->win, ev->fully_obscured);
410 bvisibility = __check_visible();
412 if (bvisibility && b_active == 0) {
413 _DBG(" Go to Resume state\n");
415 __do_app(AE_RESUME, data, NULL);
417 } else if (!bvisibility && b_active == 1) {
418 _DBG(" Go to Pasue state \n");
420 __do_app(AE_PAUSE, data, NULL);
422 _DBG(" No change state \n");
424 return ECORE_CALLBACK_RENEW;
428 static void __add_climsg_cb(struct ui_priv *ui)
432 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
439 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
441 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
443 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
444 __visibility_cb, ui);
448 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
452 if (argc == NULL || argv == NULL) {
453 _ERR("argc/argv is NULL");
459 elm_init(*argc, *argv);
461 if(getenv("opengl_x11")) {
462 elm_config_preferred_engine_set("opengl_x11");
463 _DBG("elm_config_preferred_engine_set");
465 _DBG("opengl_x11 is not set");
468 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
469 _retv_if(r == -1, -1);
471 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
472 if (ui->ops && ui->ops->create) {
473 r = ui->ops->create(ui->ops->data);
475 _ERR("create() return error");
480 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
483 ui->state = AS_CREATED;
490 static void __after_loop(struct ui_priv *ui)
492 appcore_unset_rotation_cb();
495 if (ui->ops && ui->ops->terminate)
496 ui->ops->terminate(ui->ops->data);
499 ecore_event_handler_del(ui->hshow);
501 ecore_event_handler_del(ui->hhide);
503 ecore_event_handler_del(ui->hvchange);
505 __appcore_timer_del(ui);
510 static int __set_data(struct ui_priv *ui, const char *name,
511 struct appcore_ops *ops)
514 _ERR("Mainloop already started");
519 if (name == NULL || name[0] == '\0') {
520 _ERR("Invalid name");
531 ui->name = strdup(name);
532 _retv_if(ui->name == NULL, -1);
536 ui->mfcb = __appcore_efl_memory_flush_cb;
543 static void __unset_data(struct ui_priv *ui)
546 free((void *)ui->name);
548 memset(ui, 0, sizeof(struct ui_priv));
551 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
552 struct appcore_ops *ops)
556 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
558 r = __set_data(&priv, name, ops);
559 _retv_if(r == -1, -1);
561 r = __before_loop(&priv, argc, argv);
576 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
578 resource_reclaiming = enable;