|
@@ -0,0 +1,359 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * Menu interface
|
|
24
|
+ *
|
|
25
|
+ */
|
|
26
|
+
|
|
27
|
+#include <string.h>
|
|
28
|
+#include <errno.h>
|
|
29
|
+#include <curses.h>
|
|
30
|
+#include <ipxe/keys.h>
|
|
31
|
+#include <ipxe/timer.h>
|
|
32
|
+#include <ipxe/console.h>
|
|
33
|
+#include <ipxe/menu.h>
|
|
34
|
+
|
|
35
|
+/* Colour pairs */
|
|
36
|
+#define CPAIR_NORMAL 1
|
|
37
|
+#define CPAIR_SELECT 2
|
|
38
|
+#define CPAIR_SEPARATOR 3
|
|
39
|
+
|
|
40
|
+/* Screen layout */
|
|
41
|
+#define TITLE_ROW 1
|
|
42
|
+#define MENU_ROW 3
|
|
43
|
+#define MENU_COL 1
|
|
44
|
+#define MENU_ROWS 18
|
|
45
|
+#define MENU_COLS 78
|
|
46
|
+#define MENU_PAD 2
|
|
47
|
+
|
|
48
|
+/** A menu user interface */
|
|
49
|
+struct menu_ui {
|
|
50
|
+ /** Menu */
|
|
51
|
+ struct menu *menu;
|
|
52
|
+ /** Number of menu items */
|
|
53
|
+ int count;
|
|
54
|
+ /** Currently selected item */
|
|
55
|
+ int selected;
|
|
56
|
+ /** First visible item */
|
|
57
|
+ int first_visible;
|
|
58
|
+ /** Timeout (0=indefinite) */
|
|
59
|
+ unsigned long timeout;
|
|
60
|
+};
|
|
61
|
+
|
|
62
|
+/**
|
|
63
|
+ * Return a numbered menu item
|
|
64
|
+ *
|
|
65
|
+ * @v menu Menu
|
|
66
|
+ * @v index Index
|
|
67
|
+ * @ret item Menu item, or NULL
|
|
68
|
+ */
|
|
69
|
+static struct menu_item * menu_item ( struct menu *menu, unsigned int index ) {
|
|
70
|
+ struct menu_item *item;
|
|
71
|
+
|
|
72
|
+ list_for_each_entry ( item, &menu->items, list ) {
|
|
73
|
+ if ( index-- == 0 )
|
|
74
|
+ return item;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ return NULL;
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+/**
|
|
81
|
+ * Draw a numbered menu item
|
|
82
|
+ *
|
|
83
|
+ * @v ui Menu user interface
|
|
84
|
+ * @v index Index
|
|
85
|
+ */
|
|
86
|
+static void draw_menu_item ( struct menu_ui *ui, int index ) {
|
|
87
|
+ struct menu_item *item;
|
|
88
|
+ unsigned int row_offset;
|
|
89
|
+ char buf[ MENU_COLS + 1 /* NUL */ ];
|
|
90
|
+ char timeout_buf[6]; /* "(xxx)" + NUL */
|
|
91
|
+ size_t timeout_len;
|
|
92
|
+ size_t max_len;
|
|
93
|
+ size_t len;
|
|
94
|
+
|
|
95
|
+ /* Move to start of row */
|
|
96
|
+ row_offset = ( index - ui->first_visible );
|
|
97
|
+ move ( ( MENU_ROW + row_offset ), MENU_COL );
|
|
98
|
+
|
|
99
|
+ /* Get menu item */
|
|
100
|
+ item = menu_item ( ui->menu, index );
|
|
101
|
+ if ( item ) {
|
|
102
|
+
|
|
103
|
+ /* Draw separators in a different colour */
|
|
104
|
+ if ( ! item->label )
|
|
105
|
+ color_set ( CPAIR_SEPARATOR, NULL );
|
|
106
|
+
|
|
107
|
+ /* Highlight if this is the selected item */
|
|
108
|
+ if ( index == ui->selected ) {
|
|
109
|
+ color_set ( CPAIR_SELECT, NULL );
|
|
110
|
+ attron ( A_BOLD );
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ /* Construct row */
|
|
114
|
+ memset ( buf, ' ', ( sizeof ( buf ) - 1 ) );
|
|
115
|
+ buf[ sizeof ( buf ) -1 ] = '\0';
|
|
116
|
+ len = strlen ( item->text );
|
|
117
|
+ max_len = ( sizeof ( buf ) - 1 /* NUL */ - ( 2 * MENU_PAD ) );
|
|
118
|
+ if ( len > max_len )
|
|
119
|
+ len = max_len;
|
|
120
|
+ memcpy ( ( buf + MENU_PAD ), item->text, len );
|
|
121
|
+
|
|
122
|
+ /* Add timeout if applicable */
|
|
123
|
+ timeout_len =
|
|
124
|
+ snprintf ( timeout_buf, sizeof ( timeout_buf ), "(%ld)",
|
|
125
|
+ ( ( ui->timeout + TICKS_PER_SEC - 1 ) /
|
|
126
|
+ TICKS_PER_SEC ) );
|
|
127
|
+ if ( ( index == ui->selected ) && ( ui->timeout != 0 ) ) {
|
|
128
|
+ memcpy ( ( buf + MENU_COLS - MENU_PAD - timeout_len ),
|
|
129
|
+ timeout_buf, timeout_len );
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ /* Print row */
|
|
133
|
+ printw ( "%s", buf );
|
|
134
|
+
|
|
135
|
+ /* Reset attributes */
|
|
136
|
+ color_set ( CPAIR_NORMAL, NULL );
|
|
137
|
+ attroff ( A_BOLD );
|
|
138
|
+
|
|
139
|
+ } else {
|
|
140
|
+ /* Clear row if there is no corresponding menu item */
|
|
141
|
+ clrtoeol();
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /* Move cursor back to start of row */
|
|
145
|
+ move ( ( MENU_ROW + row_offset ), MENU_COL );
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+/**
|
|
149
|
+ * Draw the current block of menu items
|
|
150
|
+ *
|
|
151
|
+ * @v ui Menu user interface
|
|
152
|
+ */
|
|
153
|
+static void draw_menu_items ( struct menu_ui *ui ) {
|
|
154
|
+ unsigned int i;
|
|
155
|
+
|
|
156
|
+ /* Jump scroll to correct point in list */
|
|
157
|
+ while ( ui->first_visible < ui->selected )
|
|
158
|
+ ui->first_visible += MENU_ROWS;
|
|
159
|
+ while ( ui->first_visible > ui->selected )
|
|
160
|
+ ui->first_visible -= MENU_ROWS;
|
|
161
|
+
|
|
162
|
+ /* Draw ellipses before and/or after the list as necessary */
|
|
163
|
+ color_set ( CPAIR_SEPARATOR, NULL );
|
|
164
|
+ mvaddstr ( ( MENU_ROW - 1 ), ( MENU_COL + MENU_PAD ),
|
|
165
|
+ ( ( ui->first_visible > 0 ) ? "..." : " " ) );
|
|
166
|
+ mvaddstr ( ( MENU_ROW + MENU_ROWS ), ( MENU_COL + MENU_PAD ),
|
|
167
|
+ ( ( ( ui->first_visible + MENU_ROWS ) < ui->count ) ?
|
|
168
|
+ "..." : " " ) );
|
|
169
|
+ color_set ( CPAIR_NORMAL, NULL );
|
|
170
|
+
|
|
171
|
+ /* Draw visible items */
|
|
172
|
+ for ( i = 0 ; i < MENU_ROWS ; i++ )
|
|
173
|
+ draw_menu_item ( ui, ( ui->first_visible + i ) );
|
|
174
|
+}
|
|
175
|
+
|
|
176
|
+/**
|
|
177
|
+ * Menu main loop
|
|
178
|
+ *
|
|
179
|
+ * @v ui Menu user interface
|
|
180
|
+ * @ret selected Selected item
|
|
181
|
+ * @ret rc Return status code
|
|
182
|
+ */
|
|
183
|
+static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
|
|
184
|
+ struct menu_item *item;
|
|
185
|
+ unsigned long timeout;
|
|
186
|
+ unsigned int delta;
|
|
187
|
+ int current;
|
|
188
|
+ int key;
|
|
189
|
+ int i;
|
|
190
|
+ int move;
|
|
191
|
+ int chosen = 0;
|
|
192
|
+ int rc = 0;
|
|
193
|
+
|
|
194
|
+ do {
|
|
195
|
+ /* Record current selection */
|
|
196
|
+ current = ui->selected;
|
|
197
|
+
|
|
198
|
+ /* Calculate timeout as remainder of current second */
|
|
199
|
+ timeout = ( ui->timeout % TICKS_PER_SEC );
|
|
200
|
+ if ( ( timeout == 0 ) && ( ui->timeout != 0 ) )
|
|
201
|
+ timeout = TICKS_PER_SEC;
|
|
202
|
+ ui->timeout -= timeout;
|
|
203
|
+
|
|
204
|
+ /* Get key */
|
|
205
|
+ move = 0;
|
|
206
|
+ key = getkey ( timeout );
|
|
207
|
+ if ( key < 0 ) {
|
|
208
|
+ /* Choose default if we finally time out */
|
|
209
|
+ if ( ui->timeout == 0 )
|
|
210
|
+ chosen = 1;
|
|
211
|
+ } else {
|
|
212
|
+ /* Cancel any timeout */
|
|
213
|
+ ui->timeout = 0;
|
|
214
|
+
|
|
215
|
+ /* Handle key */
|
|
216
|
+ switch ( key ) {
|
|
217
|
+ case KEY_UP:
|
|
218
|
+ move = -1;
|
|
219
|
+ break;
|
|
220
|
+ case KEY_DOWN:
|
|
221
|
+ move = +1;
|
|
222
|
+ break;
|
|
223
|
+ case KEY_PPAGE:
|
|
224
|
+ move = ( ui->first_visible - ui->selected - 1 );
|
|
225
|
+ break;
|
|
226
|
+ case KEY_NPAGE:
|
|
227
|
+ move = ( ui->first_visible - ui->selected
|
|
228
|
+ + MENU_ROWS );
|
|
229
|
+ break;
|
|
230
|
+ case KEY_HOME:
|
|
231
|
+ move = -ui->count;
|
|
232
|
+ break;
|
|
233
|
+ case KEY_END:
|
|
234
|
+ move = +ui->count;
|
|
235
|
+ break;
|
|
236
|
+ case ESC:
|
|
237
|
+ case CTRL_C:
|
|
238
|
+ rc = -ECANCELED;
|
|
239
|
+ break;
|
|
240
|
+ case CR:
|
|
241
|
+ case LF:
|
|
242
|
+ chosen = 1;
|
|
243
|
+ break;
|
|
244
|
+ default:
|
|
245
|
+ i = 0;
|
|
246
|
+ list_for_each_entry ( item, &ui->menu->items,
|
|
247
|
+ list ) {
|
|
248
|
+ if ( item->shortcut == key ) {
|
|
249
|
+ ui->selected = i;
|
|
250
|
+ chosen = 1;
|
|
251
|
+ break;
|
|
252
|
+ }
|
|
253
|
+ i++;
|
|
254
|
+ }
|
|
255
|
+ break;
|
|
256
|
+ }
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ /* Move selection, if applicable */
|
|
260
|
+ while ( move ) {
|
|
261
|
+ ui->selected += move;
|
|
262
|
+ if ( ui->selected < 0 ) {
|
|
263
|
+ ui->selected = 0;
|
|
264
|
+ move = +1;
|
|
265
|
+ } else if ( ui->selected >= ui->count ) {
|
|
266
|
+ ui->selected = ( ui->count - 1 );
|
|
267
|
+ move = -1;
|
|
268
|
+ }
|
|
269
|
+ item = menu_item ( ui->menu, ui->selected );
|
|
270
|
+ if ( item->label )
|
|
271
|
+ break;
|
|
272
|
+ move = ( ( move > 0 ) ? +1 : -1 );
|
|
273
|
+ }
|
|
274
|
+
|
|
275
|
+ /* Redraw selection if necessary */
|
|
276
|
+ if ( ( ui->selected != current ) || ( timeout != 0 ) ) {
|
|
277
|
+ draw_menu_item ( ui, current );
|
|
278
|
+ delta = ( ui->selected - ui->first_visible );
|
|
279
|
+ if ( delta >= MENU_ROWS )
|
|
280
|
+ draw_menu_items ( ui );
|
|
281
|
+ draw_menu_item ( ui, ui->selected );
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+ /* Refuse to choose unlabelled items (i.e. separators) */
|
|
285
|
+ item = menu_item ( ui->menu, ui->selected );
|
|
286
|
+ if ( ! item->label )
|
|
287
|
+ chosen = 0;
|
|
288
|
+
|
|
289
|
+ /* Record selection */
|
|
290
|
+ *selected = item;
|
|
291
|
+
|
|
292
|
+ } while ( ( rc == 0 ) && ! chosen );
|
|
293
|
+
|
|
294
|
+ return rc;
|
|
295
|
+}
|
|
296
|
+
|
|
297
|
+/**
|
|
298
|
+ * Show menu
|
|
299
|
+ *
|
|
300
|
+ * @v menu Menu
|
|
301
|
+ * @v wait_ms Time to wait, in milliseconds (0=indefinite)
|
|
302
|
+ * @ret selected Selected item
|
|
303
|
+ * @ret rc Return status code
|
|
304
|
+ */
|
|
305
|
+int show_menu ( struct menu *menu, unsigned int timeout_ms,
|
|
306
|
+ struct menu_item **selected ) {
|
|
307
|
+ struct menu_item *item;
|
|
308
|
+ struct menu_ui ui;
|
|
309
|
+ int labelled_count = 0;
|
|
310
|
+ int rc;
|
|
311
|
+
|
|
312
|
+ /* Initialise UI */
|
|
313
|
+ memset ( &ui, 0, sizeof ( ui ) );
|
|
314
|
+ ui.menu = menu;
|
|
315
|
+ ui.timeout = ( ( timeout_ms * TICKS_PER_SEC ) / 1000 );
|
|
316
|
+ list_for_each_entry ( item, &menu->items, list ) {
|
|
317
|
+ if ( item->label ) {
|
|
318
|
+ labelled_count++;
|
|
319
|
+ if ( ! ui.selected )
|
|
320
|
+ ui.selected = ui.count;
|
|
321
|
+ if ( item->is_default )
|
|
322
|
+ ui.selected = ui.count;
|
|
323
|
+ }
|
|
324
|
+ ui.count++;
|
|
325
|
+ }
|
|
326
|
+ if ( ! labelled_count ) {
|
|
327
|
+ /* Menus with no labelled items cannot be selected
|
|
328
|
+ * from, and will seriously confuse the navigation
|
|
329
|
+ * logic. Refuse to display any such menus.
|
|
330
|
+ */
|
|
331
|
+ return -ENOENT;
|
|
332
|
+ }
|
|
333
|
+
|
|
334
|
+ /* Initialise screen */
|
|
335
|
+ initscr();
|
|
336
|
+ start_color();
|
|
337
|
+ init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLUE );
|
|
338
|
+ init_pair ( CPAIR_SELECT, COLOR_WHITE, COLOR_RED );
|
|
339
|
+ init_pair ( CPAIR_SEPARATOR, COLOR_CYAN, COLOR_BLUE );
|
|
340
|
+ color_set ( CPAIR_NORMAL, NULL );
|
|
341
|
+ erase();
|
|
342
|
+
|
|
343
|
+ /* Draw initial content */
|
|
344
|
+ attron ( A_BOLD );
|
|
345
|
+ mvprintw ( TITLE_ROW, ( ( COLS - strlen ( ui.menu->title ) ) / 2 ),
|
|
346
|
+ "%s", ui.menu->title );
|
|
347
|
+ attroff ( A_BOLD );
|
|
348
|
+ draw_menu_items ( &ui );
|
|
349
|
+ draw_menu_item ( &ui, ui.selected );
|
|
350
|
+
|
|
351
|
+ /* Enter main loop */
|
|
352
|
+ rc = menu_loop ( &ui, selected );
|
|
353
|
+ assert ( *selected );
|
|
354
|
+
|
|
355
|
+ /* Clear screen */
|
|
356
|
+ endwin();
|
|
357
|
+
|
|
358
|
+ return rc;
|
|
359
|
+}
|