[Menu-screen] Modify the system-info key string
[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  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <ail.h>
21 #include <app.h>
22 #include <aul.h>
23 #include <Elementary.h>
24 #include <stdbool.h>
25 #include <system_info.h>
26 #include <vconf.h>
27
28 #include "conf.h"
29 #include "item.h"
30 #include "layout.h"
31 #include "mapbuf.h"
32 #include "menu_screen.h"
33 #include "mouse.h"
34 #include "page.h"
35 #include "page_scroller.h"
36 #include "util.h"
37
38 #define MENU_SCREEN_ENGINE "file/private/org.tizen.menu-screen/engine"
39
40 #define LAYOUT_EDJE_PORTRAIT EDJEDIR"/layout_portrait.edj"
41 #define LAYOUT_GROUP_NAME "layout"
42
43
44
45 // Define prototype of the "hidden API of AUL"
46 extern int aul_listen_app_dead_signal(int (*func)(int signal, void *data), void *data);
47 static struct {
48         int state;
49         Evas_Object *win;
50         Elm_Theme *theme;
51         bool is_done;
52 } menu_screen_info = {
53         .state = APP_STATE_PAUSE,
54         .win = NULL,
55         .theme = NULL,
56         .is_done = false,
57 };
58
59
60
61 HAPI Evas *menu_screen_get_evas(void)
62 {
63         return evas_object_evas_get(menu_screen_info.win);
64 }
65
66
67 HAPI int menu_screen_get_root_width(void)
68 {
69         int width = 0;
70         elm_win_screen_size_get(menu_screen_get_win(), NULL, NULL, &width, NULL);
71         return width;
72 }
73
74
75
76 HAPI int menu_screen_get_root_height(void)
77 {
78         int height = 0;
79         elm_win_screen_size_get(menu_screen_get_win(), NULL, NULL, NULL, &height);
80         return height;
81 }
82
83
84
85 HAPI Evas_Object *menu_screen_get_win(void)
86 {
87         return menu_screen_info.win;
88 }
89
90
91
92 HAPI Elm_Theme *menu_screen_get_theme(void)
93 {
94         return menu_screen_info.theme;
95 }
96
97
98
99 HAPI bool menu_screen_get_done(void)
100 {
101         return menu_screen_info.is_done;
102 }
103
104
105
106 HAPI void menu_screen_set_done(bool is_done)
107 {
108         menu_screen_info.is_done = is_done;
109 }
110
111
112
113 static menu_screen_error_e _create_canvas(char *name, char *title)
114 {
115
116         menu_screen_info.win = elm_win_add(NULL, name, ELM_WIN_BASIC);
117         retv_if(NULL == menu_screen_info.win, MENU_SCREEN_ERROR_FAIL);
118
119         if (title) {
120                 elm_win_title_set(menu_screen_info.win, title);
121         }
122         elm_win_borderless_set(menu_screen_info.win, EINA_TRUE);
123
124         elm_win_role_set(menu_screen_info.win, "MENU_SCREEN");
125         evas_object_resize(menu_screen_info.win, menu_screen_get_root_width(), menu_screen_get_root_height());
126
127         evas_object_show(menu_screen_info.win);
128
129         return MENU_SCREEN_ERROR_OK;
130 }
131
132
133
134 static void _destroy_canvas(void)
135 {
136         evas_object_del(menu_screen_info.win);
137 }
138
139
140
141 static int _dead_cb(int pid, void *data)
142 {
143         evas_object_show(menu_screen_get_win());
144         return EXIT_SUCCESS;
145 }
146
147
148
149 static void _create_bg(void)
150 {
151         char *buf;
152         Evas_Coord w;
153         Evas_Coord h;
154         Evas_Object *bg;
155         double f, wf, hf;
156         static int trigger = 0;
157         const char *key;
158         int width;
159         int height;
160
161         buf = vconf_get_str(VCONFKEY_BGSET);
162         ret_if(NULL == buf);
163
164         width = menu_screen_get_root_width();
165         height = menu_screen_get_root_height();
166
167         bg = evas_object_data_get(menu_screen_get_win(), "bg");
168         if (NULL == bg) {
169                 Evas_Object *rect;
170
171                 rect = evas_object_rectangle_add(menu_screen_get_evas());
172                 ret_if(NULL == rect);
173                 evas_object_data_set(menu_screen_get_win(), "rect", rect);
174                 evas_object_color_set(rect, 0, 0, 0, 255);
175                 evas_object_size_hint_min_set(rect, width, height);
176                 evas_object_size_hint_max_set(rect, width, height);
177                 evas_object_resize(rect, width, height);
178                 evas_object_show(rect);
179
180                 bg = evas_object_image_add(menu_screen_get_evas());
181                 if (NULL == bg) {
182                         free(buf);
183                         return;
184                 }
185                 evas_object_image_load_orientation_set(bg, EINA_TRUE);
186                 evas_object_data_set(menu_screen_get_win(), "bg", bg);
187         }
188
189         if (trigger == 0) {
190                 key = "/";
191                 trigger = 1;
192         } else {
193                 key = NULL;
194                 trigger = 0;
195         }
196
197         evas_object_image_file_set(bg, buf, key);
198         evas_object_image_size_get(bg, &w, &h);
199         evas_object_image_filled_set(bg, 1);
200
201         wf = (double) width / (double) w;
202         hf = (double) height / (double) h;
203
204         f = wf < hf ? hf : wf;
205
206         w = (int) ((double) f * (double) w);
207         h = (int) ((double) f * (double) h);
208
209         evas_object_image_load_size_set(bg, w, h);
210         evas_object_image_fill_set(bg, 0, 0, w, h);
211         evas_object_move(bg, (width - w) / 2, (height - h) / 2);
212         evas_object_resize(bg, w, h);
213         evas_object_show(bg);
214
215         free(buf);
216 }
217
218
219
220
221 static void _destroy_bg()
222 {
223         Evas_Object *rect;
224         Evas_Object *bg;
225
226         rect = evas_object_data_del(menu_screen_get_win(), "rect");
227         evas_object_del(rect);
228
229         bg = evas_object_data_del(menu_screen_get_win(), "bg");
230         evas_object_del(bg);
231 }
232
233
234
235 static void _change_bg_cb(keynode_t *node, void *data)
236 {
237         _D("Background image is changed.");
238         _create_bg();
239 }
240
241
242
243 static void _init_theme(void)
244 {
245         menu_screen_info.theme = elm_theme_new();
246         elm_theme_ref_set(menu_screen_info.theme, NULL);
247         elm_theme_extension_add(menu_screen_info.theme, EDJEDIR"/index.edj");
248 }
249
250
251
252 static void _fini_theme(void)
253 {
254         elm_theme_extension_del(menu_screen_info.theme, EDJEDIR"/index.edj");
255         elm_theme_free(menu_screen_info.theme);
256         menu_screen_info.theme = NULL;
257
258 }
259
260
261
262 static Evas_Object *_create_conformant(Evas_Object *win)
263 {
264         Evas_Object *conformant;
265
266         conformant = elm_conformant_add(win);
267         retv_if(NULL == conformant, NULL);
268
269         elm_object_style_set(conformant, "nokeypad");
270         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
271         evas_object_data_set(conformant, "win", win);
272         evas_object_show(conformant);
273
274         elm_win_resize_object_add(win, conformant);
275         elm_win_conformant_set(win, EINA_TRUE);
276
277         return conformant;
278 }
279
280
281
282 static void _destroy_conformant(Evas_Object *conformant)
283 {
284         evas_object_data_del(conformant, "win");
285         evas_object_del(conformant);
286 }
287
288
289
290 static bool _create_cb(void *data)
291 {
292         Evas_Object *conformant;
293
294         _init_theme();
295         retv_if(MENU_SCREEN_ERROR_FAIL == _create_canvas("MENU_SCREEN", PACKAGE), false);
296         elm_win_indicator_mode_set(menu_screen_info.win, ELM_WIN_INDICATOR_SHOW);
297
298         if (vconf_notify_key_changed(VCONFKEY_BGSET, _change_bg_cb, NULL) < 0) {
299                 _E("Failed to register a vconf cb for %s\n", VCONFKEY_BGSET);
300         }
301         _create_bg();
302
303         conformant = _create_conformant(menu_screen_info.win);
304         retv_if(NULL == conformant, false);
305         evas_object_data_set(menu_screen_info.win, "conformant", conformant);
306
307         Evas_Object *layout;
308         layout = layout_create(conformant, LAYOUT_EDJE_PORTRAIT,
309                                 LAYOUT_GROUP_NAME, MENU_SCREEN_ROTATE_PORTRAIT);
310         if (NULL == layout) {
311                 _E("Failed to load an edje object");
312                 evas_object_del(menu_screen_info.win);
313                 return false;
314         }
315         evas_object_data_set(menu_screen_info.win, "layout", layout);
316
317         elm_object_content_set(conformant, layout);
318         mouse_register();
319         aul_listen_app_dead_signal(_dead_cb, NULL);
320
321         return true;
322 }
323
324
325
326 static void _terminate_cb(void *data)
327 {
328         Evas_Object *conformant;
329         Evas_Object *layout;
330
331         if (vconf_ignore_key_changed(VCONFKEY_BGSET, _change_bg_cb) < 0) {
332                 _E("Failed to remove bgset [%s]\n", VCONFKEY_BGSET);
333         }
334
335         evas_object_hide(menu_screen_info.win);
336
337         mouse_unregister();
338
339         layout = evas_object_data_del(menu_screen_info.win, "layout");
340         if (layout) layout_destroy(layout);
341
342         conformant = evas_object_data_del(menu_screen_info.win, "conformant");
343         if (conformant) _destroy_conformant(conformant);
344
345         _destroy_bg();
346         _destroy_canvas();
347         _fini_theme();
348         evas_object_del(menu_screen_info.win);
349 }
350
351
352
353 static void _pause_cb(void *data)
354 {
355         _D("Pause start");
356
357         if (vconf_set_int(VCONFKEY_IDLE_SCREEN_TOP, VCONFKEY_IDLE_SCREEN_TOP_FALSE) < 0) {
358                 _E("Failed to set %s to 0", VCONFKEY_IDLE_SCREEN_TOP);
359         }
360
361         menu_screen_info.state = APP_STATE_PAUSE;
362 }
363
364
365
366 static void _resume_cb(void *data)
367 {
368         _D("START RESUME");
369
370         if (vconf_set_int(VCONFKEY_IDLE_SCREEN_TOP, VCONFKEY_IDLE_SCREEN_TOP_TRUE) < 0) {
371                 _E("Failed to set %s to 1", VCONFKEY_IDLE_SCREEN_TOP);
372         }
373
374         evas_object_show(menu_screen_get_win());
375
376         menu_screen_info.state = APP_STATE_RESUME;
377 }
378
379
380
381 static void _service_cb(service_h service, void *data)
382 {
383         _D("START RESET : %d", menu_screen_info.state);
384
385         if (vconf_set_int(VCONFKEY_IDLE_SCREEN_TOP, VCONFKEY_IDLE_SCREEN_TOP_TRUE) < 0) {
386                 _E("Failed to set %s to 1", VCONFKEY_IDLE_SCREEN_TOP);
387         }
388
389         evas_object_show(menu_screen_get_win());
390 }
391
392
393
394 static void _language_changed_cb(void *data)
395 {
396         register unsigned int i;
397         register unsigned int j;
398         unsigned int count;
399         Evas_Object *layout;
400         Evas_Object *all_apps;
401         Evas_Object *scroller;
402         Evas_Object *page;
403         Evas_Object *item;
404         unsigned int page_max_app;
405
406         _D("Language is changed");
407
408         if (false == menu_screen_info.is_done) {
409                 elm_exit();
410         }
411
412         layout = evas_object_data_get(menu_screen_info.win, "layout");
413         ret_if(NULL == layout);
414         all_apps = evas_object_data_get(layout, "all_apps");
415         ret_if(NULL == all_apps);
416         scroller = elm_object_part_content_get(all_apps, "content");
417         ret_if(NULL == scroller);
418
419         count = page_scroller_count_page(scroller);
420         page_max_app = (unsigned int) evas_object_data_get(scroller, "page_max_app");
421         for (i = 0; i < count; i ++) {
422                 page = page_scroller_get_page_at(scroller, i);
423                 if (!page) continue;
424                 if (mapbuf_is_enabled(page)) {
425                         mapbuf_disable(page, 1);
426                 }
427
428                 for (j = 0; j < page_max_app; j ++) {
429                         ail_appinfo_h ai;
430                         char *name;
431
432                         item = page_get_item_at(page, j);
433                         if (!item) continue;
434
435                         if (ail_get_appinfo(item_get_package(item), &ai) < 0) continue;
436                         if (ail_appinfo_get_str(ai, AIL_PROP_NAME_STR, &name) < 0) {
437                                 ail_destroy_appinfo(ai);
438                                 continue;
439                         }
440
441                         if (!name) {
442                                 _D("Failed to get name for %s", item_get_package(item));
443                                 ail_destroy_appinfo(ai);
444                                 continue;
445                         }
446
447                         item_set_name(item, name, 0);
448                         ail_destroy_appinfo(ai);
449                 }
450
451                 mapbuf_enable(page, 1);
452         }
453 }
454
455
456
457 static void _init(app_event_callback_s *event_callback)
458 {
459         event_callback->create = _create_cb;
460         event_callback->terminate = _terminate_cb;
461         event_callback->pause = _pause_cb;
462         event_callback->resume = _resume_cb;
463         event_callback->service = _service_cb;
464         event_callback->low_memory = NULL;
465         event_callback->low_battery = NULL;
466         event_callback->device_orientation = NULL;
467         event_callback->language_changed = _language_changed_cb;
468         event_callback->region_format_changed = NULL;
469 }
470
471
472
473 static void _fini(void)
474 {
475 }
476
477
478
479 #define QP_EMUL_STR             "Emulator"
480 static bool _is_emulator_on(void)
481 {
482         int ret;
483         char *model = NULL;
484
485         ret = system_info_get_platform_string("tizen.org/system/model_name", &model);
486         if (SYSTEM_INFO_ERROR_NONE != ret) {
487                 if (model) {
488                         free(model);
489                 }
490                 return false;
491         }
492
493         if (!strncmp(model, QP_EMUL_STR, strlen(model))) {
494                 _D("This model is on Emulator");
495                 free(model);
496                 return true;
497         }
498
499         _D("This model is NOT on Emulator");
500         free(model);
501         return false;
502 }
503
504
505
506 int main(int argc, char *argv[])
507 {
508         char *buf;
509         app_event_callback_s event_callback;
510
511         if (_is_emulator_on()) {
512                 _D("ELM_ENGINE is set as [software_x11]");
513                 setenv("ELM_ENGINE", "software_x11", 1);
514         } else {
515                 buf = vconf_get_str(MENU_SCREEN_ENGINE);
516                 if (buf) {
517                         _D("ELM_ENGINE is set as [%s]", buf);
518                         setenv("ELM_ENGINE", buf, 1);
519                         free(buf);
520                 } else {
521                         _D("ELM_ENGINE is set as [gl]");
522                         setenv("ELM_ENGINE", "gl", 1);
523                 }
524         }
525
526         _init(&event_callback);
527         app_efl_main(&argc, &argv, &event_callback, NULL);
528         _fini();
529
530         return EXIT_SUCCESS;
531 }
532
533
534
535
536
537 // End of a file