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(struct win_node *a, struct win_node *b)
83 return (int)((a->win)-(b->win));
86 static struct win_node *win_h;
87 GSList *g_winnode_list;
90 #if defined(MEMORY_FLUSH_ACTIVATE)
91 static Eina_Bool __appcore_memory_flush_cb(struct ui_priv *ui)
93 appcore_flush_memory();
96 return ECORE_CALLBACK_CANCEL;
99 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
101 if (ui->state == AS_PAUSED) {
102 appcore_flush_memory();
110 static void __appcore_timer_add(struct ui_priv *ui)
112 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
115 static void __appcore_timer_del(struct ui_priv *ui)
118 ecore_timer_del(ui->mftimer);
125 static int __appcore_low_memory_post_cb(ui_priv *ui)
130 #define __appcore_timer_add(ui) 0
131 #define __appcore_timer_del(ui) 0
135 static void __appcore_efl_memory_flush_cb(void)
137 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
141 static void __do_app(enum app_event event, void *data, bundle * b)
144 struct ui_priv *ui = data;
146 _DBG("[APP %d] Event: %d", _pid, event);
147 _ret_if(ui == NULL || event >= AE_MAX);
148 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
149 _as_name[ui->state]);
151 if (event == AE_MEM_FLUSH) {
156 if (event == AE_LOWMEM_POST) {
157 if (__appcore_low_memory_post_cb(ui) == 0)
161 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
162 __appcore_timer_del(ui);
164 if (event == AE_TERMINATE) {
165 _DBG("[APP %d] TERMINATE", _pid);
166 ui->state = AS_DYING;
171 _ret_if(ui->ops == NULL);
175 _DBG("[APP %d] RESET", _pid);
176 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
179 r = ui->ops->reset(b, ui->ops->data);
180 ui->state = AS_RUNNING;
181 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
185 if (ui->state == AS_RUNNING) {
186 _DBG("[APP %d] PAUSE", _pid);
188 r = ui->ops->pause(ui->ops->data);
189 ui->state = AS_PAUSED;
191 __appcore_timer_add(ui);
193 /* TODO : rotation stop */
194 r = appcore_pause_rotation_cb();
196 sysman_inform_backgrd();
199 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
201 if (ui->state == AS_PAUSED) {
202 _DBG("[APP %d] RESUME", _pid);
204 r = ui->ops->resume(ui->ops->data);
205 ui->state = AS_RUNNING;
207 /*TODO : rotation start*/
208 r = appcore_resume_rotation_cb();
209 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
211 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
213 sysman_inform_foregrd();
222 static struct ui_ops efl_ops = {
228 static bool __check_visible(void)
231 struct win_node *entry = NULL;
233 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
235 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
237 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
238 if(entry->bfobscured == FALSE)
244 static bool __exist_win(unsigned int win)
246 struct win_node temp;
251 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
260 static bool __add_win(unsigned int win)
265 t = calloc(1, sizeof(struct win_node));
270 t->bfobscured = FALSE;
272 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
274 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
278 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
283 g_winnode_list = g_slist_append(g_winnode_list, t);
289 static bool __delete_win(unsigned int win)
291 struct win_node temp;
296 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
299 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
304 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
309 static bool __update_win(unsigned int win, bool bfobscured)
311 struct win_node temp;
316 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
321 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
325 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
329 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
333 t = calloc(1, sizeof(struct win_node));
338 t->bfobscured = bfobscured;
340 g_winnode_list = g_slist_append(g_winnode_list, t);
346 static Eina_Bool __show_cb(void *data, int type, void *event)
348 Ecore_X_Event_Window_Show *ev;
352 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
354 if (!__exist_win((unsigned int)ev->win))
355 __add_win((unsigned int)ev->win);
357 __update_win((unsigned int)ev->win, FALSE);
359 return ECORE_CALLBACK_RENEW;
362 static Eina_Bool __hide_cb(void *data, int type, void *event)
364 Ecore_X_Event_Window_Hide *ev;
369 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
371 if (__exist_win((unsigned int)ev->win)) {
372 __delete_win((unsigned int)ev->win);
374 bvisibility = __check_visible();
375 if (!bvisibility && b_active == 1) {
376 _DBG(" Go to Pasue state \n");
378 __do_app(AE_PAUSE, data, NULL);
382 return ECORE_CALLBACK_RENEW;
385 static Eina_Bool __visibility_cb(void *data, int type, void *event)
387 Ecore_X_Event_Window_Visibility_Change *ev;
392 __update_win((unsigned int)ev->win, ev->fully_obscured);
393 bvisibility = __check_visible();
395 if (bvisibility && b_active == 0) {
396 _DBG(" Go to Resume state\n");
398 __do_app(AE_RESUME, data, NULL);
400 } else if (!bvisibility && b_active == 1) {
401 _DBG(" Go to Pasue state \n");
403 __do_app(AE_PAUSE, data, NULL);
405 _DBG(" No change state \n");
407 return ECORE_CALLBACK_RENEW;
411 static void __add_climsg_cb(struct ui_priv *ui)
416 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
418 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
420 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
421 __visibility_cb, ui);
425 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
429 if (argc == NULL || argv == NULL) {
430 _ERR("argc/argv is NULL");
436 elm_init(*argc, *argv);
438 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
439 _retv_if(r == -1, -1);
441 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
442 if (ui->ops && ui->ops->create) {
443 r = ui->ops->create(ui->ops->data);
445 _ERR("create() return error");
450 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
453 ui->state = AS_CREATED;
460 static void __after_loop(struct ui_priv *ui)
462 appcore_unset_rotation_cb();
465 if (ui->ops && ui->ops->terminate)
466 ui->ops->terminate(ui->ops->data);
469 ecore_event_handler_del(ui->hshow);
471 ecore_event_handler_del(ui->hhide);
473 ecore_event_handler_del(ui->hvchange);
475 __appcore_timer_del(ui);
480 static int __set_data(struct ui_priv *ui, const char *name,
481 struct appcore_ops *ops)
484 _ERR("Mainloop already started");
489 if (name == NULL || name[0] == '\0') {
490 _ERR("Invalid name");
501 ui->name = strdup(name);
502 _retv_if(ui->name == NULL, -1);
506 ui->mfcb = __appcore_efl_memory_flush_cb;
513 static void __unset_data(struct ui_priv *ui)
516 free((void *)ui->name);
518 memset(ui, 0, sizeof(struct ui_priv));
521 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
522 struct appcore_ops *ops)
526 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
528 r = __set_data(&priv, name, ops);
529 _retv_if(r == -1, -1);
531 r = __before_loop(&priv, argc, argv);