Clone a tag rather than SeaBIOS stable branch HEAD
[coreboot.git] / payloads / bayou / menu.c
1 /*
2  * This file is part of the bayou project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <libpayload.h>
21 #include <curses.h>
22 #include "bayou.h"
23
24 #define SCREEN_X 80
25 #define SCREEN_Y 25
26
27 static int menu_width = 0;
28 static struct payload *mpayloads[BAYOU_MAX_ENTRIES];
29 static int m_entries = 0;
30 static unsigned int selected = 0;
31 static WINDOW *menuwin, *status;
32
33 void create_menu(void)
34 {
35         int i;
36
37         for (i = 0; i < bayoucfg.n_entries; i++) {
38                 struct payload *p = &(bayoucfg.entries[i]);
39                 char *name;
40
41                 if ((p->pentry.parent != 0) ||
42                     (p->pentry.flags & BPT_FLAG_NOSHOW))
43                         continue;
44
45                 mpayloads[m_entries++] = p;
46
47                 name = payload_get_name(p);
48
49                 if (strlen(name) > menu_width)
50                         menu_width = strlen(name);
51         }
52
53         menu_width += 4;
54
55         if (menu_width < 30)
56                 menu_width = 30;
57
58         menuwin = newwin(m_entries + 3, menu_width,
59                          (SCREEN_Y - (m_entries + 3)) / 2,
60                          (SCREEN_X - menu_width) / 2);
61 }
62
63 void draw_menu(void)
64 {
65         struct payload *s;
66         int i;
67
68         wattrset(menuwin, COLOR_PAIR(3));
69         wclear(menuwin);
70         wborder(menuwin, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
71                 ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
72
73         wattrset(menuwin, COLOR_PAIR(4) | A_BOLD);
74         mvwprintw(menuwin, 0, (menu_width - 17) / 2, " Payload Chooser ");
75
76         wattrset(menuwin, COLOR_PAIR(3));
77
78         for (i = 0; i < m_entries; i++) {
79                 char *name = payload_get_name(mpayloads[i]);
80                 int col = (menu_width - (2 + strlen(name))) / 2;
81
82                 if (i == selected)
83                         wattrset(menuwin, COLOR_PAIR(5) | A_BOLD);
84                 else
85                         wattrset(menuwin, COLOR_PAIR(3));
86
87                 mvwprintw(menuwin, 2 + i, col, name);
88         }
89
90         s = mpayloads[selected];
91
92         wclear(status);
93
94         if (s->params[BAYOU_PARAM_DESC] != NULL) {
95                 char buf[66];
96                 int len = strnlen(s->params[BAYOU_PARAM_DESC], 65);
97
98                 snprintf(buf, 65, s->params[BAYOU_PARAM_DESC]);
99                 buf[65] = 0;
100
101                 mvwprintw(status, 0, (80 - len) / 2, buf);
102         }
103
104         wrefresh(menuwin);
105         wrefresh(status);
106 }
107
108 void loop(void)
109 {
110         int key;
111
112         while (1) {
113                 key = getch();
114
115                 if (key == ERR)
116                         continue;
117
118                 if (key == KEY_DOWN)
119                         selected = (selected + 1) % m_entries;
120                 else if (key == KEY_UP)
121                         selected = (selected - 1) % m_entries;
122                 else if (key == KEY_ENTER) {
123                         run_payload(mpayloads[selected]);
124                         clear();
125                         refresh();
126                 } else
127                         continue;
128
129                 draw_menu();
130         }
131 }
132
133 void menu(void)
134 {
135         initscr();
136
137         init_pair(1, COLOR_WHITE, COLOR_RED);
138         init_pair(2, COLOR_BLACK, COLOR_WHITE);
139         init_pair(3, COLOR_BLACK, COLOR_WHITE);
140         init_pair(4, COLOR_CYAN, COLOR_WHITE);
141         init_pair(5, COLOR_WHITE, COLOR_RED);
142
143         wattrset(stdscr, COLOR_PAIR(1));
144         wclear(stdscr);
145
146         status = newwin(1, 80, 24, 0);
147         wattrset(status, COLOR_PAIR(2));
148         wclear(status);
149
150         refresh();
151
152         create_menu();
153         draw_menu();
154
155         loop();
156 }