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