Merge 2.4 to 3.0
[apps/native/menu-screen.git] / src / menu_screen.c
1 /*
2  * MENU-SCREEN
3  *
4  * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Jin Yoon <jinny.yoon@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <ail.h>
24 #include <app.h>
25 #include <appcore-efl.h>
26 #include <aul.h>
27 //#include <Ecore_X.h>
28 #include <Elementary.h>
29 #include <stdbool.h>
30 #include <system_info.h>
31 #include <vconf.h>
32 #include <app_preference.h>
33 //#include <system_info_internal.h>
34 #include <system_settings.h>
35
36 #include "conf.h"
37 #include "item.h"
38 #include "key.h"
39 #include "layout.h"
40 #include "mapbuf.h"
41 #include "menu_screen.h"
42 #include "mouse.h"
43 #include "page.h"
44 #include "page_scroller.h"
45 #include "pkgmgr.h"
46 #include "util.h"
47
48 #define MENU_SCREEN_ENGINE "file/private/org.tizen.menu-screen/engine"
49
50 #define LAYOUT_EDJE_PORTRAIT EDJEDIR"/layout_portrait.edj"
51 #define LAYOUT_GROUP_NAME "layout"
52
53
54
55 // Define prototype of the "hidden API of AUL"
56 extern int aul_listen_app_dead_signal(int (*func)(int signal, void *data), void *data);
57 static struct {
58         int state;
59         int root_width;
60         int root_height;
61         int is_tts;
62         int booting_state;
63         Evas *evas;
64         Ecore_Evas *ee;
65         Evas_Object *win;
66         Elm_Theme *theme;
67         bool is_done;
68 } menu_screen_info = {
69         .state = APP_STATE_PAUSE,
70         .is_tts = false,
71         .booting_state = 0,
72         .evas = NULL,
73         .ee = NULL,
74         .win = NULL,
75         .theme = NULL,
76         .is_done = false,
77 };
78
79
80
81 HAPI Evas *menu_screen_get_evas(void)
82 {
83         return menu_screen_info.evas;
84 }
85
86
87
88 HAPI int menu_screen_get_root_width(void)
89 {
90         return menu_screen_info.root_width;
91 }
92
93
94
95 HAPI int menu_screen_get_root_height(void)
96 {
97         return menu_screen_info.root_height;
98 }
99
100
101
102 HAPI Evas_Object *menu_screen_get_win(void)
103 {
104         return menu_screen_info.win;
105 }
106
107
108
109 HAPI Elm_Theme *menu_screen_get_theme(void)
110 {
111         return menu_screen_info.theme;
112 }
113
114
115
116 HAPI bool menu_screen_get_done(void)
117 {
118         return menu_screen_info.is_done;
119 }
120
121
122
123 HAPI void menu_screen_set_done(bool is_done)
124 {
125         menu_screen_info.is_done = is_done;
126 }
127
128
129
130 HAPI int menu_screen_get_state(void)
131 {
132         return menu_screen_info.state;
133 }
134
135
136
137 HAPI int menu_screen_is_tts(void)
138 {
139         return menu_screen_info.is_tts;
140 }
141
142
143 static Eina_Bool _appcore_flush_cb(void *data)
144 {
145         if (APP_STATE_PAUSE != menu_screen_info.state) return ECORE_CALLBACK_CANCEL;
146         if (0 != appcore_flush_memory()) _E("Cannot flush memory");
147         return ECORE_CALLBACK_CANCEL;
148 }
149
150
151
152 HAPI void menu_screen_inc_booting_state(void)
153 {
154         menu_screen_info.booting_state++;
155         if (BOOTING_STATE_DONE > menu_screen_info.booting_state) return;
156
157         menu_screen_error_e ret = MENU_SCREEN_ERROR_OK;
158         do {
159                 ret = pkgmgr_reserve_list_pop_request();
160         } while (MENU_SCREEN_ERROR_OK == ret);
161
162         /*  Cache memory is cleared when the application paused (every time, after 5 seconds (in appcore)),
163         *  but after running in a minimized mode (HIDE_LAUNCH) application have status AS_RUNNING.
164         *  Application have status AS_PAUSED only after change of visibility to hidden condition by user (on hiding window)
165         *  Cleaning must be performed only once after application loading in hidden condition
166         *  (and stay in a hidden condition at time of cleaning).
167         */
168         if (APP_STATE_PAUSE == menu_screen_info.state)
169                 ecore_timer_add(5, _appcore_flush_cb, NULL);
170 }
171
172
173
174 HAPI void menu_screen_dec_booting_state(void)
175 {
176         menu_screen_info.booting_state --;
177 }
178
179
180
181 HAPI int menu_screen_get_booting_state(void)
182 {
183         return menu_screen_info.booting_state;
184 }
185
186
187
188 static bool _is_emulator_on(void)
189 {
190         int ret;
191         char *model;
192
193         ret = system_info_get_platform_string("tizen.org/system/model_name", &model);
194         if (SYSTEM_INFO_ERROR_NONE != ret) {
195                 if (model) {
196                         free(model);
197                 }
198                 return false;
199         }
200
201         if (!strncmp(model, "Emulator", strlen(model))) {
202                 _D("This model is on Emulator");
203                 free(model);
204                 return true;
205         }
206
207         _D("This model is NOT on Emulator");
208         free(model);
209         return false;
210 }
211
212
213
214 static menu_screen_error_e _create_canvas(char *name, char *title)
215 {
216         //Ecore_X_Atom ATOM_WM_WINDOW_ROLE;
217         char *buf;
218
219         if (_is_emulator_on()) {
220                 _D("ELM_ENGINE is set as [software_x11]");
221                 elm_config_accel_preference_set("opengl");
222         } else {
223                 buf = vconf_get_str(MENU_SCREEN_ENGINE);
224                 if (buf) {
225                         _D("ELM_ENGINE is set as [%s]", buf);
226                         elm_config_accel_preference_set(buf);
227                         free(buf);
228                 } else {
229                         _D("ELM_ENGINE is set as [gl]");
230                         elm_config_accel_preference_set("gl");
231                 }
232         }
233
234
235         menu_screen_info.win = elm_win_add(NULL, name, ELM_WIN_BASIC);
236         retv_if(NULL == menu_screen_info.win, MENU_SCREEN_ERROR_FAIL);
237
238         if (title) {
239                 elm_win_title_set(menu_screen_info.win, title);
240         }
241         elm_win_borderless_set(menu_screen_info.win, EINA_TRUE);
242         elm_win_screen_size_get(menu_screen_info.win, NULL, NULL, &menu_screen_info.root_width, &menu_screen_info.root_height);
243
244         //ecore_x_icccm_name_class_set(elm_win_xwindow_get(menu_screen_info.win), "MENU_SCREEN", "MENU_SCREEN");
245         //ATOM_WM_WINDOW_ROLE = ecore_x_atom_get("WM_WINDOW_ROLE");
246         //if (ATOM_WM_WINDOW_ROLE) {
247 //              ecore_x_window_prop_string_set(elm_win_xwindow_get(menu_screen_info.win), ATOM_WM_WINDOW_ROLE, "MENU_SCREEN");
248 //      } else {
249 //              _D("Failed to set the window role as MENU_SCREEN");
250 //      }
251
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());
254
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__);
258         }
259
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__);
263         }
264
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);
269
270         return MENU_SCREEN_ERROR_OK;
271 }
272
273
274
275 static void _destroy_canvas(void)
276 {
277         evas_object_del(menu_screen_info.win);
278 }
279
280
281
282 static int _dead_cb(int pid, void *data)
283 {
284         return EXIT_SUCCESS;
285 }
286
287
288
289 static void _create_bg(void)
290 {
291         char *buf;
292         Evas_Coord w;
293         Evas_Coord h;
294         Evas_Object *bg;
295         double f, wf, hf;
296         static int trigger = 0;
297         const char *key;
298         int width;
299         int height;
300
301         system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &buf);
302         ret_if(NULL == buf);
303
304         width = menu_screen_get_root_width();
305         height = menu_screen_get_root_height();
306
307         bg = evas_object_data_get(menu_screen_get_win(), "bg");
308         if (NULL == bg) {
309                 Evas_Object *rect;
310
311                 rect = evas_object_rectangle_add(menu_screen_get_evas());
312                 ret_if(NULL == rect);
313                 evas_object_data_set(menu_screen_get_win(), "rect", rect);
314                 evas_object_color_set(rect, 0, 0, 0, 255);
315                 evas_object_size_hint_min_set(rect, width, height);
316                 evas_object_size_hint_max_set(rect, width, height);
317                 elm_win_resize_object_add(menu_screen_get_win(), rect);
318                 evas_object_show(rect);
319
320                 bg = evas_object_image_add(menu_screen_get_evas());
321                 if (NULL == bg) {
322                         free(buf);
323                         return;
324                 }
325                 evas_object_image_load_orientation_set(bg, EINA_TRUE);
326                 evas_object_data_set(menu_screen_get_win(), "bg", bg);
327         }
328
329         if (trigger == 0) {
330                 key = "/";
331                 trigger = 1;
332         } else {
333                 key = NULL;
334                 trigger = 0;
335         }
336
337         evas_object_image_file_set(bg, buf, key);
338         evas_object_image_size_get(bg, &w, &h);
339         evas_object_image_filled_set(bg, 1);
340
341         wf = (double) width / (double) w;
342         hf = (double) height / (double) h;
343
344         f = wf < hf ? hf : wf;
345
346         w = (int) ((double) f * (double) w);
347         h = (int) ((double) f * (double) h);
348
349         evas_object_image_load_size_set(bg, w, h);
350         evas_object_image_fill_set(bg, 0, 0, w, h);
351         elm_win_resize_object_add(menu_screen_get_win(), bg);
352         evas_object_show(bg);
353
354         free(buf);
355 }
356
357
358
359
360 static void _destroy_bg()
361 {
362         Evas_Object *rect;
363         Evas_Object *bg;
364
365         rect = evas_object_data_del(menu_screen_get_win(), "rect");
366         evas_object_del(rect);
367
368         bg = evas_object_data_del(menu_screen_get_win(), "bg");
369         evas_object_del(bg);
370 }
371
372
373
374 static void _change_bg_cb(keynode_t *node, void *data)
375 {
376         _D("Background image is changed.");
377         _create_bg();
378 }
379
380
381
382 static void _init_theme(void)
383 {
384         menu_screen_info.theme = elm_theme_new();
385         elm_theme_ref_set(menu_screen_info.theme, NULL);
386         elm_theme_extension_add(menu_screen_info.theme, EDJEDIR"/index.edj");
387 }
388
389
390
391 static void _fini_theme(void)
392 {
393         elm_theme_extension_del(menu_screen_info.theme, EDJEDIR"/index.edj");
394         elm_theme_free(menu_screen_info.theme);
395         menu_screen_info.theme = NULL;
396
397 }
398
399
400
401 static Evas_Object *_create_conformant(Evas_Object *win)
402 {
403         Evas_Object *conformant;
404
405         conformant = elm_conformant_add(win);
406         retv_if(NULL == conformant, NULL);
407
408         elm_win_indicator_opacity_set(win, ELM_WIN_INDICATOR_TRANSLUCENT);
409         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
410         elm_win_indicator_mode_set(menu_screen_info.win, ELM_WIN_INDICATOR_SHOW);
411         evas_object_data_set(conformant, "win", win);
412         evas_object_show(conformant);
413
414         elm_win_resize_object_add(win, conformant);
415         elm_win_conformant_set(win, EINA_TRUE);
416
417         return conformant;
418 }
419
420
421
422 static void _destroy_conformant(Evas_Object *conformant)
423 {
424         evas_object_data_del(conformant, "win");
425         evas_object_del(conformant);
426 }
427
428
429 static char *_replace_str(char *str, const char *before, const char *after)
430 {
431         retv_if(NULL == str, NULL);
432         retv_if(NULL == before, NULL);
433         retv_if(NULL == after, NULL);
434
435         size_t before_len = strlen(before);
436         retv_if(before_len < 1, str);
437
438         size_t after_len = strlen(after);
439         size_t i, count = 0;
440         if (after_len != before_len) {
441                 for (i = 0; str[i] != '\0';) {
442                         if (0 == memcmp(&str[i], before, before_len)) {
443                                 count++;
444                                 i += before_len;
445                         } else {
446                                 i++;
447                         }
448                 }
449         } else {
450                 i = strlen(str);
451         }
452
453         char *result;
454         result = malloc(i + 1 + count * (after_len - before_len));
455         retv_if(result == NULL, NULL);
456
457         char *sr;
458         sr = result;
459         while (*str) {
460                 if (0 == memcmp(str, before, before_len)) {
461                         memcpy(sr, after, after_len);
462                         sr += after_len;
463                         str += before_len;
464                 } else {
465                         *sr++ = *str++;
466                 }
467         }
468         *sr = '\0';
469
470         return result;
471 }
472
473
474
475 static bool _create_cb(void *data)
476 {
477         Evas_Object *conformant;
478
479         _init_theme();
480         retv_if(MENU_SCREEN_ERROR_FAIL == _create_canvas(PACKAGE, PACKAGE), false);
481
482         //if (system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, _change_bg_cb, NULL) < 0) {
483         //      _E("Failed to register a settings change cb for %s\n", SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
484         //}
485         _create_bg();
486
487         conformant = _create_conformant(menu_screen_info.win);
488         retv_if(NULL == conformant, false);
489         evas_object_data_set(menu_screen_info.win, "conformant", conformant);
490
491         Evas_Object *layout;
492         layout = layout_create(conformant, LAYOUT_EDJE_PORTRAIT,
493                                 LAYOUT_GROUP_NAME, MENU_SCREEN_ROTATE_PORTRAIT);
494         if (NULL == layout) {
495                 _E("Failed to load an edje object");
496                 evas_object_del(menu_screen_info.win);
497                 return false;
498         }
499         evas_object_data_set(menu_screen_info.win, "layout", layout);
500
501         elm_object_content_set(conformant, layout);
502
503         mouse_register();
504         aul_listen_app_dead_signal(_dead_cb, NULL);
505         key_register();
506
507         // FIXME : This will be enabled after rebuilding the routine for appid <-> pkgid.
508         pkgmgr_init();
509
510         return true;
511 }
512
513
514
515 static void _terminate_cb(void *data)
516 {
517         Evas_Object *conformant;
518         Evas_Object *layout;
519
520         // FIXME : This will be enabled after rebuilding the routine for appid <-> pkgid.
521         pkgmgr_fini();
522
523         if (system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN) < 0) {
524                 _E("Failed to remove bgset [%s]\n", SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
525         }
526
527         evas_object_hide(menu_screen_info.win);
528
529         key_unregister();
530         mouse_unregister();
531
532         layout = evas_object_data_del(menu_screen_info.win, "layout");
533         if (layout) layout_destroy(layout);
534
535         conformant = evas_object_data_del(menu_screen_info.win, "conformant");
536         if (conformant) _destroy_conformant(conformant);
537
538         _destroy_bg();
539         _destroy_canvas();
540         _fini_theme();
541         evas_object_del(menu_screen_info.win);
542 }
543
544
545
546 static void _pause_cb(void *data)
547 {
548         _D("Pause start");
549
550         menu_screen_info.state = APP_STATE_PAUSE;
551 }
552
553
554
555 static void _resume_cb(void *data)
556 {
557         _D("START RESUME");
558
559         do { // Focus
560                 Evas_Object *layout = evas_object_data_get(menu_screen_info.win, "layout");
561                 break_if(NULL == layout);
562
563                 Evas_Object *all_apps = evas_object_data_get(layout, "all_apps");
564                 break_if(NULL == all_apps);
565
566                 Evas_Object *scroller = elm_object_part_content_get(all_apps, "content");
567                 break_if(NULL == scroller);
568
569                 page_scroller_focus(scroller);
570         } while (0);
571
572         menu_screen_info.state = APP_STATE_RESUME;
573 }
574
575
576
577 static void _app_control_cb(app_control_h service, void *data)
578 {
579         _D("START RESET : %d", menu_screen_info.state);
580
581         do { // Focus
582                 Evas_Object *layout = evas_object_data_get(menu_screen_info.win, "layout");
583                 break_if(NULL == layout);
584
585                 Evas_Object *all_apps = evas_object_data_get(layout, "all_apps");
586                 break_if(NULL == all_apps);
587
588                 Evas_Object *scroller = elm_object_part_content_get(all_apps, "content");
589                 break_if(NULL == scroller);
590
591                 page_scroller_focus(scroller);
592         } while (0);
593 }
594
595
596
597 static void _language_changed_cb(app_event_info_h event_info, void *data)
598 {
599         register unsigned int i;
600         register unsigned int j;
601         unsigned int count;
602         Evas_Object *layout;
603         Evas_Object *all_apps;
604         Evas_Object *scroller;
605         Evas_Object *page;
606         Evas_Object *item;
607         unsigned int page_max_app;
608
609         _D("Language is changed");
610
611         if (false == menu_screen_info.is_done) {
612                 elm_exit();
613         }
614
615         layout = evas_object_data_get(menu_screen_info.win, "layout");
616         ret_if(NULL == layout);
617         all_apps = evas_object_data_get(layout, "all_apps");
618         ret_if(NULL == all_apps);
619         scroller = elm_object_part_content_get(all_apps, "content");
620         ret_if(NULL == scroller);
621
622         count = page_scroller_count_page(scroller);
623         page_max_app = (unsigned int) evas_object_data_get(scroller, "page_max_app");
624         for (i = 0; i < count; i ++) {
625                 page = page_scroller_get_page_at(scroller, i);
626                 if (!page) continue;
627                 if (mapbuf_is_enabled(page)) {
628                         mapbuf_disable(page, 1);
629                 }
630
631                 for (j = 0; j < page_max_app; j ++) {
632                         ail_appinfo_h ai;
633                         char *name;
634
635                         item = page_get_item_at(page, j);
636                         if (!item) continue;
637
638                         if (ail_get_appinfo(item_get_package(item), &ai) < 0) {
639                                 ail_destroy_appinfo(ai);
640                                 continue;
641                         }
642                         if (ail_appinfo_get_str(ai, AIL_PROP_NAME_STR, &name) < 0) {
643                                 ail_destroy_appinfo(ai);
644                                 continue;
645                         }
646
647                         if (!name) {
648                                 _D("Failed to get name for %s", item_get_package(item));
649                                 ail_destroy_appinfo(ai);
650                                 continue;
651                         }
652
653                         item_set_name(item, name, 0);
654                         ail_destroy_appinfo(ai);
655                 }
656
657                 mapbuf_enable(page, 1);
658         }
659 }
660
661
662
663 static void _init(ui_app_lifecycle_callback_s *lifecycle_callback, app_event_handler_h *event_handlers)
664 {
665         lifecycle_callback->create = _create_cb;
666         lifecycle_callback->terminate = _terminate_cb;
667         lifecycle_callback->pause = _pause_cb;
668         lifecycle_callback->resume = _resume_cb;
669         lifecycle_callback->app_control = _app_control_cb;
670
671         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
672         ui_app_add_event_handler(&event_handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, NULL);
673         ui_app_add_event_handler(&event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
674         ui_app_add_event_handler(&event_handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _language_changed_cb, NULL);
675         ui_app_add_event_handler(&event_handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, NULL);
676
677 }
678
679
680
681 static void _fini(app_event_handler_h *event_handlers)
682 {
683         ui_app_remove_event_handler(event_handlers[APP_EVENT_LOW_BATTERY]);
684         ui_app_remove_event_handler(event_handlers[APP_EVENT_LOW_MEMORY]);
685         ui_app_remove_event_handler(event_handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED]);
686         ui_app_remove_event_handler(event_handlers[APP_EVENT_LANGUAGE_CHANGED]);
687         ui_app_remove_event_handler(event_handlers[APP_EVENT_REGION_FORMAT_CHANGED]);
688
689 }
690
691
692
693 static void _init_reference_value(void)
694 {
695         preference_set_string(MENU_SCREEN_ENGINE, "gl");
696 }
697
698
699 int main(int argc, char *argv[])
700 {
701         char *buf;
702         ui_app_lifecycle_callback_s lifecycle_callback = {0, };
703         app_event_handler_h event_handlers[5] = {NULL, };
704
705         _init(&lifecycle_callback, event_handlers);
706         ui_app_main(argc, argv, &lifecycle_callback, NULL);
707         _fini(event_handlers);
708
709         return EXIT_SUCCESS;
710 }
711
712
713
714 // End of a file