Refactor GUI code, add grid layout by Dave Ball
[qi-bootmenu.git] / gui.c
blob34149b5aafb803ec0ca7b698476f05914a5f19a7
1 #include <stdbool.h>
2 #include <Eina.h>
3 #include <Evas.h>
4 #include <Ecore.h>
5 #include <Ecore_Evas.h>
6 #include "config.h"
7 #include "kexec.h"
8 #include "util.h"
10 /* XXX: replace hardcoded values with runtime information */
11 #define SCREEN_WIDTH 480
12 #define SCREEN_HEIGHT 640
14 /* drawing related stuff */
15 static Ecore_Evas *ee;
16 static Evas *evas;
18 static void poweroff(void *data, Evas *evas, Evas_Object *obj, void *event);
19 static void bootitem_clicked(void *data, Evas *evas, Evas_Object *item, void *event);
21 typedef struct {
22 const char *text;
23 const char *logo;
24 void(*callback)(void*, Evas*, Evas_Object*, void *);
25 } MenuItem;
27 MenuItem menu[] = {
28 { "Power Off", POWEROFF_LOGO, poweroff },
31 #include "gui-list.c"
32 #include "gui-grid.c"
34 typedef struct {
35 char option;
36 void(*show)(Eina_List *systems);
37 void(*select)(Evas_Object *item);
38 } Gui;
40 Gui guis[] = {
41 { 'l', gui_list, gui_list_select_item },
42 { 'g', gui_grid, NULL },
45 /* if no option is passed in then use the first entry */
46 static Gui *gui = &guis[0];
48 static void poweroff(void *data, Evas *evas, Evas_Object *item, void *event) {
49 if (gui->select)
50 gui->select(item);
51 system("poweroff");
54 static void bootitem_clicked(void *data, Evas *evas, Evas_Object *item, void *event) {
55 if (gui->select)
56 gui->select(item);
57 boot_kernel((BootItem*)data);
58 /* XXX: shouldn't be reached, display an error message? */
61 static bool gui_init(){
62 if (!ecore_init() || !ecore_evas_init())
63 return false;
65 /* XXX: fixed dimensions */
66 ee = ecore_evas_new(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL);
68 if (!ee)
69 return false;
71 ecore_evas_title_set(ee, APPNAME);
72 ecore_evas_borderless_set(ee, 0);
73 ecore_evas_show(ee);
75 evas = ecore_evas_get(ee);
76 if (!evas)
77 return false;
79 evas_font_path_append(evas, FONT_PATH);
81 return true;
84 int gui_show(int argc, char **argv) {
86 if (argc > 1 && argv[1][0] == '-') {
87 int i;
88 for (i = 0; i < countof(guis); i++) {
89 if (argv[1][1] == guis[i].option)
90 gui = &guis[i];
94 if (!gui_init()) {
95 eprint("Couldn't init GUI\n");
96 return 1;
99 /* search for system images to boot and display them */
100 gui->show(scan_system());
102 ecore_main_loop_begin();
104 return 0;