4 * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Jin Yoon <jinny.yoon@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
25 #include <appcore-efl.h>
27 #include <Elementary.h>
29 #include <system_info.h>
31 #include <app_preference.h>
32 #include <system_settings.h>
33 #include <pkgmgr-info.h>
40 #include "menu_screen.h"
43 #include "page_scroller.h"
47 #define MENU_SCREEN_ENGINE "file/private/org.tizen.menu-screen/engine"
49 #define LAYOUT_EDJE_PORTRAIT EDJEDIR"/layout_portrait.edj"
50 #define LAYOUT_GROUP_NAME "layout"
54 // Define prototype of the "hidden API of AUL"
55 extern int aul_listen_app_dead_signal(int (*func)(int signal, void *data), void *data);
67 } menu_screen_info = {
68 .state = APP_STATE_PAUSE,
80 HAPI Evas *menu_screen_get_evas(void)
82 return menu_screen_info.evas;
87 HAPI int menu_screen_get_root_width(void)
89 return menu_screen_info.root_width;
94 HAPI int menu_screen_get_root_height(void)
96 return menu_screen_info.root_height;
101 HAPI Evas_Object *menu_screen_get_win(void)
103 return menu_screen_info.win;
108 HAPI Elm_Theme *menu_screen_get_theme(void)
110 return menu_screen_info.theme;
115 HAPI bool menu_screen_get_done(void)
117 return menu_screen_info.is_done;
122 HAPI void menu_screen_set_done(bool is_done)
124 menu_screen_info.is_done = is_done;
129 HAPI int menu_screen_get_state(void)
131 return menu_screen_info.state;
136 HAPI int menu_screen_is_tts(void)
138 return menu_screen_info.is_tts;
142 static Eina_Bool _appcore_flush_cb(void *data)
144 if (APP_STATE_PAUSE != menu_screen_info.state) return ECORE_CALLBACK_CANCEL;
145 if (0 != appcore_flush_memory()) _E("Cannot flush memory");
146 return ECORE_CALLBACK_CANCEL;
151 HAPI void menu_screen_inc_booting_state(void)
153 menu_screen_info.booting_state++;
154 if (BOOTING_STATE_DONE > menu_screen_info.booting_state) return;
156 menu_screen_error_e ret = MENU_SCREEN_ERROR_OK;
158 ret = pkgmgr_reserve_list_pop_request();
159 } while (MENU_SCREEN_ERROR_OK == ret);
161 /* Cache memory is cleared when the application paused (every time, after 5 seconds (in appcore)),
162 * but after running in a minimized mode (HIDE_LAUNCH) application have status AS_RUNNING.
163 * Application have status AS_PAUSED only after change of visibility to hidden condition by user (on hiding window)
164 * Cleaning must be performed only once after application loading in hidden condition
165 * (and stay in a hidden condition at time of cleaning).
167 if (APP_STATE_PAUSE == menu_screen_info.state)
168 ecore_timer_add(5, _appcore_flush_cb, NULL);
173 HAPI void menu_screen_dec_booting_state(void)
175 menu_screen_info.booting_state --;
180 HAPI int menu_screen_get_booting_state(void)
182 return menu_screen_info.booting_state;
187 static bool _is_emulator_on(void)
192 ret = system_info_get_platform_string("tizen.org/system/model_name", &model);
193 if (SYSTEM_INFO_ERROR_NONE != ret) {
200 if (!strncmp(model, "Emulator", strlen(model))) {
201 _D("This model is on Emulator");
206 _D("This model is NOT on Emulator");
213 static menu_screen_error_e _create_canvas(char *name, char *title)
215 //Ecore_X_Atom ATOM_WM_WINDOW_ROLE;
218 if (_is_emulator_on()) {
219 _D("ELM_ENGINE is set as [software_x11]");
220 elm_config_accel_preference_set("opengl");
222 buf = vconf_get_str(MENU_SCREEN_ENGINE);
224 _D("ELM_ENGINE is set as [%s]", buf);
225 elm_config_accel_preference_set(buf);
228 _D("ELM_ENGINE is set as [gl]");
229 elm_config_accel_preference_set("gl");
233 menu_screen_info.win = elm_win_add(NULL, name, ELM_WIN_BASIC);
234 retv_if(NULL == menu_screen_info.win, MENU_SCREEN_ERROR_FAIL);
237 elm_win_title_set(menu_screen_info.win, title);
239 elm_win_borderless_set(menu_screen_info.win, EINA_TRUE);
240 elm_win_screen_size_get(menu_screen_info.win, NULL, NULL, &menu_screen_info.root_width, &menu_screen_info.root_height);
243 ecore_x_icccm_name_class_set(elm_win_xwindow_get(menu_screen_info.win), "MENU_SCREEN", "MENU_SCREEN");
244 ATOM_WM_WINDOW_ROLE = ecore_x_atom_get("WM_WINDOW_ROLE");
245 if (ATOM_WM_WINDOW_ROLE) {
246 ecore_x_window_prop_string_set(elm_win_xwindow_get(menu_screen_info.win), ATOM_WM_WINDOW_ROLE, "MENU_SCREEN");
248 _D("Failed to set the window role as MENU_SCREEN");
252 elm_win_role_set(menu_screen_info.win, "MENU_SCREEN");
253 evas_object_resize(menu_screen_info.win, menu_screen_get_root_width(), menu_screen_get_root_height());
255 menu_screen_info.evas = evas_object_evas_get(menu_screen_info.win);
256 if (!menu_screen_info.evas) {
257 _E("[%s] Failed to get the evas object", __func__);
260 menu_screen_info.ee = ecore_evas_ecore_evas_get(menu_screen_info.evas);
261 if (!menu_screen_info.ee) {
262 _E("[%s] Failed to get ecore_evas object", __func__);
265 evas_object_size_hint_min_set(menu_screen_info.win, menu_screen_info.root_width, menu_screen_info.root_height);
266 evas_object_size_hint_max_set(menu_screen_info.win, menu_screen_info.root_width, menu_screen_info.root_height);
267 evas_object_resize(menu_screen_info.win, menu_screen_info.root_width, menu_screen_info.root_height);
268 evas_object_show(menu_screen_info.win);
270 return MENU_SCREEN_ERROR_OK;
275 static void _destroy_canvas(void)
277 evas_object_del(menu_screen_info.win);
282 static int _dead_cb(int pid, void *data)
289 static void _create_bg(void)
297 static int trigger = 0;
301 int ret = SYSTEM_SETTINGS_ERROR_NONE;
303 if (system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &buf) < 0) {
304 _E("Failed to get a wallpaper: %d\n", SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
307 width = menu_screen_get_root_width();
308 height = menu_screen_get_root_height();
310 bg = evas_object_data_get(menu_screen_get_win(), "bg");
313 rect = evas_object_rectangle_add(menu_screen_get_evas());
314 goto_if(!rect, ERROR);
315 evas_object_data_set(menu_screen_get_win(), "rect", rect);
316 evas_object_color_set(rect, 0, 0, 0, 255);
317 evas_object_size_hint_min_set(rect, width, height);
318 evas_object_size_hint_max_set(rect, width, height);
319 elm_win_resize_object_add(menu_screen_get_win(), rect);
320 evas_object_show(rect);
322 bg = evas_object_image_add(menu_screen_get_evas());
325 evas_object_del(rect);
329 evas_object_image_load_orientation_set(bg, EINA_TRUE);
330 evas_object_data_set(menu_screen_get_win(), "bg", bg);
341 evas_object_image_file_set(bg, buf, key);
342 evas_object_image_size_get(bg, &w, &h);
343 if (w == 0 || h == 0) {
344 _E("There's something wrong, width or height is ZERO");
347 evas_object_image_filled_set(bg, 1);
349 wf = (double) width / (double) w;
350 hf = (double) height / (double) h;
352 f = wf < hf ? hf : wf;
354 w = (int) ((double) f * (double) w);
355 h = (int) ((double) f * (double) h);
357 evas_object_image_load_size_set(bg, w, h);
358 evas_object_image_fill_set(bg, 0, 0, w, h);
359 elm_win_resize_object_add(menu_screen_get_win(), bg);
360 evas_object_show(bg);
371 static void _destroy_bg()
376 rect = evas_object_data_del(menu_screen_get_win(), "rect");
377 evas_object_del(rect);
379 bg = evas_object_data_del(menu_screen_get_win(), "bg");
385 static void _change_bg_cb(system_settings_key_e key, void *data)
387 _D("Background image is changed.");
393 static void _init_theme(void)
395 menu_screen_info.theme = elm_theme_new();
396 elm_theme_ref_set(menu_screen_info.theme, NULL);
397 elm_theme_extension_add(menu_screen_info.theme, EDJEDIR"/index.edj");
402 static void _fini_theme(void)
404 elm_theme_extension_del(menu_screen_info.theme, EDJEDIR"/index.edj");
405 elm_theme_free(menu_screen_info.theme);
406 menu_screen_info.theme = NULL;
412 static Evas_Object *_create_conformant(Evas_Object *win)
414 Evas_Object *conformant;
416 conformant = elm_conformant_add(win);
417 retv_if(NULL == conformant, NULL);
419 evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
420 elm_win_indicator_mode_set(menu_screen_info.win, ELM_WIN_INDICATOR_SHOW);
421 elm_object_signal_emit(conformant, "elm,state,indicator,overlap", "elm");
422 evas_object_data_set(conformant, "win", win);
423 evas_object_show(conformant);
425 elm_win_resize_object_add(win, conformant);
426 elm_win_conformant_set(win, EINA_TRUE);
433 static void _destroy_conformant(Evas_Object *conformant)
435 evas_object_data_del(conformant, "win");
436 evas_object_del(conformant);
440 static char *_replace_str(char *str, const char *before, const char *after)
442 retv_if(NULL == str, NULL);
443 retv_if(NULL == before, NULL);
444 retv_if(NULL == after, NULL);
446 size_t before_len = strlen(before);
447 retv_if(before_len < 1, str);
449 size_t after_len = strlen(after);
451 if (after_len != before_len) {
452 for (i = 0; str[i] != '\0';) {
453 if (0 == memcmp(&str[i], before, before_len)) {
465 result = malloc(i + 1 + count * (after_len - before_len));
466 retv_if(result == NULL, NULL);
471 if (0 == memcmp(str, before, before_len)) {
472 memcpy(sr, after, after_len);
486 static bool _create_cb(void *data)
488 Evas_Object *conformant;
491 retv_if(MENU_SCREEN_ERROR_FAIL == _create_canvas(PACKAGE, PACKAGE), false);
493 if (system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, _change_bg_cb, NULL) < 0) {
494 _E("Failed to register a settings change cb for %d\n", SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
498 conformant = _create_conformant(menu_screen_info.win);
499 retv_if(NULL == conformant, false);
500 evas_object_data_set(menu_screen_info.win, "conformant", conformant);
503 layout = layout_create(conformant, LAYOUT_EDJE_PORTRAIT,
504 LAYOUT_GROUP_NAME, MENU_SCREEN_ROTATE_PORTRAIT);
505 if (NULL == layout) {
506 _E("Failed to load an edje object");
507 evas_object_del(menu_screen_info.win);
510 evas_object_data_set(menu_screen_info.win, "layout", layout);
512 elm_object_content_set(conformant, layout);
515 aul_listen_app_dead_signal(_dead_cb, NULL);
518 // FIXME : This will be enabled after rebuilding the routine for appid <-> pkgid.
526 static void _terminate_cb(void *data)
528 Evas_Object *conformant;
531 // FIXME : This will be enabled after rebuilding the routine for appid <-> pkgid.
534 if (system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN) < 0) {
535 _E("Failed to remove bgset [%s]\n", SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
538 evas_object_hide(menu_screen_info.win);
543 layout = evas_object_data_del(menu_screen_info.win, "layout");
544 if (layout) layout_destroy(layout);
546 conformant = evas_object_data_del(menu_screen_info.win, "conformant");
547 if (conformant) _destroy_conformant(conformant);
552 evas_object_del(menu_screen_info.win);
557 static void _pause_cb(void *data)
561 menu_screen_info.state = APP_STATE_PAUSE;
566 static void _resume_cb(void *data)
571 Evas_Object *layout = evas_object_data_get(menu_screen_info.win, "layout");
572 break_if(NULL == layout);
574 Evas_Object *all_apps = evas_object_data_get(layout, "all_apps");
575 break_if(NULL == all_apps);
577 Evas_Object *scroller = elm_object_part_content_get(all_apps, "content");
578 break_if(NULL == scroller);
580 page_scroller_focus(scroller);
583 menu_screen_info.state = APP_STATE_RESUME;
588 static void _app_control_cb(app_control_h service, void *data)
590 _D("START RESET : %d", menu_screen_info.state);
593 Evas_Object *layout = evas_object_data_get(menu_screen_info.win, "layout");
594 break_if(NULL == layout);
596 Evas_Object *all_apps = evas_object_data_get(layout, "all_apps");
597 break_if(NULL == all_apps);
599 Evas_Object *scroller = elm_object_part_content_get(all_apps, "content");
600 break_if(NULL == scroller);
602 page_scroller_focus(scroller);
608 static void _language_changed_cb(app_event_info_h event_info, void *data)
610 register unsigned int i;
611 register unsigned int j;
614 Evas_Object *all_apps;
615 Evas_Object *scroller;
618 unsigned int page_max_app;
620 _D("Language is changed");
622 if (false == menu_screen_info.is_done) {
626 layout = evas_object_data_get(menu_screen_info.win, "layout");
627 ret_if(NULL == layout);
628 all_apps = evas_object_data_get(layout, "all_apps");
629 ret_if(NULL == all_apps);
630 scroller = elm_object_part_content_get(all_apps, "content");
631 ret_if(NULL == scroller);
633 count = page_scroller_count_page(scroller);
634 page_max_app = (unsigned int) evas_object_data_get(scroller, "page_max_app");
635 for (i = 0; i < count; i ++) {
636 page = page_scroller_get_page_at(scroller, i);
638 if (mapbuf_is_enabled(page)) {
639 mapbuf_disable(page, 1);
642 for (j = 0; j < page_max_app; j ++) {
643 pkgmgrinfo_appinfo_h appinfo_h = NULL;
646 item = page_get_item_at(page, j);
649 if (pkgmgrinfo_appinfo_get_appinfo(item_get_package(item), &appinfo_h) < 0) {
650 pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
653 if (pkgmgrinfo_appinfo_get_label(appinfo_h, &name) < 0) {
654 pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
659 _D("Failed to get name for %s", item_get_package(item));
660 pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
664 item_set_name(item, name, 0);
665 pkgmgrinfo_appinfo_destroy_appinfo(appinfo_h);
668 mapbuf_enable(page, 1);
674 static void _init(ui_app_lifecycle_callback_s *lifecycle_callback, app_event_handler_h *event_handlers)
676 lifecycle_callback->create = _create_cb;
677 lifecycle_callback->terminate = _terminate_cb;
678 lifecycle_callback->pause = _pause_cb;
679 lifecycle_callback->resume = _resume_cb;
680 lifecycle_callback->app_control = _app_control_cb;
682 ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
683 ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, NULL);
684 ui_app_add_event_handler(&event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
685 ui_app_add_event_handler(&event_handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _language_changed_cb, NULL);
686 ui_app_add_event_handler(&event_handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, NULL);
692 static void _fini(app_event_handler_h *event_handlers)
694 ui_app_remove_event_handler(event_handlers[APP_EVENT_LOW_BATTERY]);
695 ui_app_remove_event_handler(event_handlers[APP_EVENT_LOW_MEMORY]);
696 ui_app_remove_event_handler(event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED]);
697 ui_app_remove_event_handler(event_handlers[APP_EVENT_LANGUAGE_CHANGED]);
698 ui_app_remove_event_handler(event_handlers[APP_EVENT_REGION_FORMAT_CHANGED]);
704 static void _init_reference_value(void)
706 preference_set_string(MENU_SCREEN_ENGINE, "gl");
710 int main(int argc, char *argv[])
713 ui_app_lifecycle_callback_s lifecycle_callback = {0, };
714 app_event_handler_h event_handlers[5] = {NULL, };
716 _init(&lifecycle_callback, event_handlers);
717 ui_app_main(argc, argv, &lifecycle_callback, NULL);
718 _fini(event_handlers);