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