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"
44 Ecore_Event_Handler *hshow;
45 Ecore_Event_Handler *hhide;
46 Ecore_Event_Handler *hvchange;
48 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
50 struct appcore_ops *ops;
51 void (*mfcb) (void); /* Memory Flushing Callback */
54 static struct ui_priv priv;
56 static const char *_ae_name[AE_MAX] = {
57 [AE_UNKNOWN] = "UNKNOWN",
58 [AE_CREATE] = "CREATE",
59 [AE_TERMINATE] = "TERMINATE",
61 [AE_RESUME] = "RESUME",
63 [AE_LOWMEM_POST] = "LOWMEM_POST",
64 [AE_MEM_FLUSH] = "MEM_FLUSH",
67 static const char *_as_name[] = {
69 [AS_CREATED] = "CREATED",
70 [AS_RUNNING] = "RUNNING",
71 [AS_PAUSED] = "PAUSED",
75 static bool b_active = 1;
81 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
83 struct win_node *a = (struct win_node *)data1;
84 struct win_node *b = (struct win_node *)data2;
85 return (int)((a->win)-(b->win));
88 GSList *g_winnode_list;
90 #if defined(MEMORY_FLUSH_ACTIVATE)
91 static Eina_Bool __appcore_memory_flush_cb(void *data)
93 struct ui_priv *ui = (struct ui_priv *)data;
95 appcore_flush_memory();
98 return ECORE_CALLBACK_CANCEL;
101 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
103 if (ui->state == AS_PAUSED) {
104 appcore_flush_memory();
112 static void __appcore_timer_add(struct ui_priv *ui)
114 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
117 static void __appcore_timer_del(struct ui_priv *ui)
120 ecore_timer_del(ui->mftimer);
127 static int __appcore_low_memory_post_cb(ui_priv *ui)
132 #define __appcore_timer_add(ui) 0
133 #define __appcore_timer_del(ui) 0
137 static void __appcore_efl_memory_flush_cb(void)
139 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
140 elm_cache_all_flush();
143 static void __do_app(enum app_event event, void *data, bundle * b)
146 struct ui_priv *ui = data;
148 _DBG("[APP %d] Event: %d", _pid, event);
149 _ret_if(ui == NULL || event >= AE_MAX);
150 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
151 _as_name[ui->state]);
153 if (event == AE_MEM_FLUSH) {
158 if (event == AE_LOWMEM_POST) {
159 if (__appcore_low_memory_post_cb(ui) == 0)
163 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
164 __appcore_timer_del(ui);
166 if (event == AE_TERMINATE) {
167 _DBG("[APP %d] TERMINATE", _pid);
168 ui->state = AS_DYING;
173 _ret_if(ui->ops == NULL);
177 _DBG("[APP %d] RESET", _pid);
178 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
181 r = ui->ops->reset(b, ui->ops->data);
182 ui->state = AS_RUNNING;
183 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
187 if (ui->state == AS_RUNNING) {
188 _DBG("[APP %d] PAUSE", _pid);
190 r = ui->ops->pause(ui->ops->data);
191 ui->state = AS_PAUSED;
193 __appcore_timer_add(ui);
195 /* TODO : rotation stop */
196 r = appcore_pause_rotation_cb();
198 sysman_inform_backgrd();
201 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
203 if (ui->state == AS_PAUSED) {
204 _DBG("[APP %d] RESUME", _pid);
206 r = ui->ops->resume(ui->ops->data);
207 ui->state = AS_RUNNING;
209 /*TODO : rotation start*/
210 r = appcore_resume_rotation_cb();
211 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
213 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
215 sysman_inform_foregrd();
224 static struct ui_ops efl_ops = {
230 static bool __check_visible(void)
233 struct win_node *entry = NULL;
235 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
237 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
239 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
240 if(entry->bfobscured == FALSE)
246 static bool __exist_win(unsigned int win)
248 struct win_node temp;
253 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
262 static bool __add_win(unsigned int win)
267 t = calloc(1, sizeof(struct win_node));
272 t->bfobscured = FALSE;
274 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
276 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
280 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
285 g_winnode_list = g_slist_append(g_winnode_list, t);
291 static bool __delete_win(unsigned int win)
293 struct win_node temp;
298 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
301 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
306 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
313 static bool __update_win(unsigned int win, bool bfobscured)
315 struct win_node temp;
320 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
325 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
329 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
333 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
337 t = calloc(1, sizeof(struct win_node));
342 t->bfobscured = bfobscured;
344 g_winnode_list = g_slist_append(g_winnode_list, t);
350 static Eina_Bool __show_cb(void *data, int type, void *event)
352 Ecore_X_Event_Window_Show *ev;
356 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
358 if (!__exist_win((unsigned int)ev->win))
359 __add_win((unsigned int)ev->win);
361 __update_win((unsigned int)ev->win, FALSE);
363 return ECORE_CALLBACK_RENEW;
366 static Eina_Bool __hide_cb(void *data, int type, void *event)
368 Ecore_X_Event_Window_Hide *ev;
373 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
375 if (__exist_win((unsigned int)ev->win)) {
376 __delete_win((unsigned int)ev->win);
378 bvisibility = __check_visible();
379 if (!bvisibility && b_active == 1) {
380 _DBG(" Go to Pasue state \n");
382 __do_app(AE_PAUSE, data, NULL);
386 return ECORE_CALLBACK_RENEW;
389 static Eina_Bool __visibility_cb(void *data, int type, void *event)
391 Ecore_X_Event_Window_Visibility_Change *ev;
396 __update_win((unsigned int)ev->win, ev->fully_obscured);
397 bvisibility = __check_visible();
399 if (bvisibility && b_active == 0) {
400 _DBG(" Go to Resume state\n");
402 __do_app(AE_RESUME, data, NULL);
404 } else if (!bvisibility && b_active == 1) {
405 _DBG(" Go to Pasue state \n");
407 __do_app(AE_PAUSE, data, NULL);
409 _DBG(" No change state \n");
411 return ECORE_CALLBACK_RENEW;
415 static void __add_climsg_cb(struct ui_priv *ui)
420 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
422 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
424 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
425 __visibility_cb, ui);
429 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
433 if (argc == NULL || argv == NULL) {
434 _ERR("argc/argv is NULL");
440 elm_init(*argc, *argv);
442 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
443 _retv_if(r == -1, -1);
445 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
446 if (ui->ops && ui->ops->create) {
447 r = ui->ops->create(ui->ops->data);
449 _ERR("create() return error");
454 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
457 ui->state = AS_CREATED;
464 static void __after_loop(struct ui_priv *ui)
466 appcore_unset_rotation_cb();
469 if (ui->ops && ui->ops->terminate)
470 ui->ops->terminate(ui->ops->data);
473 ecore_event_handler_del(ui->hshow);
475 ecore_event_handler_del(ui->hhide);
477 ecore_event_handler_del(ui->hvchange);
479 __appcore_timer_del(ui);
484 static int __set_data(struct ui_priv *ui, const char *name,
485 struct appcore_ops *ops)
488 _ERR("Mainloop already started");
493 if (name == NULL || name[0] == '\0') {
494 _ERR("Invalid name");
505 ui->name = strdup(name);
506 _retv_if(ui->name == NULL, -1);
510 ui->mfcb = __appcore_efl_memory_flush_cb;
517 static void __unset_data(struct ui_priv *ui)
520 free((void *)ui->name);
522 memset(ui, 0, sizeof(struct ui_priv));
525 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
526 struct appcore_ops *ops)
530 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
532 r = __set_data(&priv, name, ops);
533 _retv_if(r == -1, -1);
535 r = __before_loop(&priv, argc, argv);