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