summaryrefslogtreecommitdiffstats
path: root/src/hci/tui/menu_ui.c
blob: 0a9566def15fec4203e0022e92b677797d9611ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

/** @file
 *
 * Menu interface
 *
 */

#include <string.h>
#include <errno.h>
#include <curses.h>
#include <ipxe/keys.h>
#include <ipxe/timer.h>
#include <ipxe/console.h>
#include <ipxe/ansicol.h>
#include <ipxe/menu.h>

/* Screen layout */
#define TITLE_ROW	1U
#define MENU_ROW	3U
#define MENU_COL	1U
#define MENU_ROWS	( LINES - 2U - MENU_ROW )
#define MENU_COLS	( COLS - 2U )
#define MENU_PAD	2U

/** A menu user interface */
struct menu_ui {
	/** Menu */
	struct menu *menu;
	/** Number of menu items */
	unsigned int count;
	/** Currently selected item */
	int selected;
	/** First visible item */
	int first_visible;
	/** Timeout (0=indefinite) */
	unsigned long timeout;
};

/**
 * Return a numbered menu item
 *
 * @v menu		Menu
 * @v index		Index
 * @ret item		Menu item, or NULL
 */
static struct menu_item * menu_item ( struct menu *menu, unsigned int index ) {
	struct menu_item *item;

	list_for_each_entry ( item, &menu->items, list ) {
		if ( index-- == 0 )
			return item;
	}

	return NULL;
}

/**
 * Draw a numbered menu item
 *
 * @v ui		Menu user interface
 * @v index		Index
 */
static void draw_menu_item ( struct menu_ui *ui, int index ) {
	struct menu_item *item;
	unsigned int row_offset;
	char buf[ MENU_COLS + 1 /* NUL */ ];
	char timeout_buf[6]; /* "(xxx)" + NUL */
	size_t timeout_len;
	size_t max_len;
	size_t len;

	/* Move to start of row */
	row_offset = ( index - ui->first_visible );
	move ( ( MENU_ROW + row_offset ), MENU_COL );

	/* Get menu item */
	item = menu_item ( ui->menu, index );
	if ( item ) {

		/* Draw separators in a different colour */
		if ( ! item->label )
			color_set ( CPAIR_SEPARATOR, NULL );

		/* Highlight if this is the selected item */
		if ( index == ui->selected ) {
			color_set ( CPAIR_SELECT, NULL );
			attron ( A_BOLD );
		}

		/* Construct row */
		memset ( buf, ' ', ( sizeof ( buf ) - 1 ) );
		buf[ sizeof ( buf ) -1 ] = '\0';
		len = strlen ( item->text );
		max_len = ( sizeof ( buf ) - 1 /* NUL */ - ( 2 * MENU_PAD ) );
		if ( len > max_len )
			len = max_len;
		memcpy ( ( buf + MENU_PAD ), item->text, len );

		/* Add timeout if applicable */
		timeout_len =
			snprintf ( timeout_buf, sizeof ( timeout_buf ), "(%ld)",
				   ( ( ui->timeout + TICKS_PER_SEC - 1 ) /
				     TICKS_PER_SEC ) );
		if ( ( index == ui->selected ) && ( ui->timeout != 0 ) ) {
			memcpy ( ( buf + MENU_COLS - MENU_PAD - timeout_len ),
				 timeout_buf, timeout_len );
		}

		/* Print row */
		printw ( "%s", buf );

		/* Reset attributes */
		color_set ( CPAIR_NORMAL, NULL );
		attroff ( A_BOLD );

	} else {
		/* Clear row if there is no corresponding menu item */
		clrtoeol();
	}

	/* Move cursor back to start of row */
	move ( ( MENU_ROW + row_offset ), MENU_COL );
}

/**
 * Draw the current block of menu items
 *
 * @v ui		Menu user interface
 */
static void draw_menu_items ( struct menu_ui *ui ) {
	unsigned int i;

	/* Jump scroll to correct point in list */
	while ( ui->first_visible < ui->selected )
		ui->first_visible += MENU_ROWS;
	while ( ui->first_visible > ui->selected )
		ui->first_visible -= MENU_ROWS;

	/* Draw ellipses before and/or after the list as necessary */
	color_set ( CPAIR_SEPARATOR, NULL );
	mvaddstr ( ( MENU_ROW - 1 ), ( MENU_COL + MENU_PAD ),
		   ( ( ui->first_visible > 0 ) ? "..." : "   " ) );
	mvaddstr ( ( MENU_ROW + MENU_ROWS ), ( MENU_COL + MENU_PAD ),
		   ( ( ( ui->first_visible + MENU_ROWS ) < ui->count ) ?
		     "..." : "   " ) );
	color_set ( CPAIR_NORMAL, NULL );

	/* Draw visible items */
	for ( i = 0 ; i < MENU_ROWS ; i++ )
		draw_menu_item ( ui, ( ui->first_visible + i ) );
}

/**
 * Menu main loop
 *
 * @v ui		Menu user interface
 * @ret selected	Selected item
 * @ret rc		Return status code
 */
static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
	struct menu_item *item;
	unsigned long timeout;
	unsigned int delta;
	int current;
	int key;
	int i;
	int move;
	int chosen = 0;
	int rc = 0;

	do {
		/* Record current selection */
		current = ui->selected;

		/* Calculate timeout as remainder of current second */
		timeout = ( ui->timeout % TICKS_PER_SEC );
		if ( ( timeout == 0 ) && ( ui->timeout != 0 ) )
			timeout = TICKS_PER_SEC;
		ui->timeout -= timeout;

		/* Get key */
		move = 0;
		key = getkey ( timeout );
		if ( key < 0 ) {
			/* Choose default if we finally time out */
			if ( ui->timeout == 0 )
				chosen = 1;
		} else {
			/* Cancel any timeout */
			ui->timeout = 0;

			/* Handle key */
			switch ( key ) {
			case KEY_UP:
				move = -1;
				break;
			case KEY_DOWN:
				move = +1;
				break;
			case KEY_PPAGE:
				move = ( ui->first_visible - ui->selected - 1 );
				break;
			case KEY_NPAGE:
				move = ( ui->first_visible - ui->selected
					 + MENU_ROWS );
				break;
			case KEY_HOME:
				move = -ui->count;
				break;
			case KEY_END:
				move = +ui->count;
				break;
			case ESC:
			case CTRL_C:
				rc = -ECANCELED;
				break;
			case CR:
			case LF:
				chosen = 1;
				break;
			default:
				i = 0;
				list_for_each_entry ( item, &ui->menu->items,
						      list ) {
					if ( ! ( item->shortcut &&
						 ( item->shortcut == key ) ) ) {
						i++;
						continue;
					}
					ui->selected = i;
					if ( item->label ) {
						chosen = 1;
					} else {
						move = +1;
					}
				}
				break;
			}
		}

		/* Move selection, if applicable */
		while ( move ) {
			ui->selected += move;
			if ( ui->selected < 0 ) {
				ui->selected = 0;
				move = +1;
			} else if ( ui->selected >= ( int ) ui->count ) {
				ui->selected = ( ui->count - 1 );
				move = -1;
			}
			item = menu_item ( ui->menu, ui->selected );
			if ( item->label )
				break;
			move = ( ( move > 0 ) ? +1 : -1 );
		}

		/* Redraw selection if necessary */
		if ( ( ui->selected != current ) || ( timeout != 0 ) ) {
			draw_menu_item ( ui, current );
			delta = ( ui->selected - ui->first_visible );
			if ( delta >= MENU_ROWS )
				draw_menu_items ( ui );
			draw_menu_item ( ui, ui->selected );
		}

		/* Record selection */
		item = menu_item ( ui->menu, ui->selected );
		assert ( item != NULL );
		assert ( item->label != NULL );
		*selected = item;

	} while ( ( rc == 0 ) && ! chosen );

	return rc;
}

/**
 * Show menu
 *
 * @v menu		Menu
 * @v timeout		Timeout period, in ticks (0=indefinite)
 * @ret selected	Selected item
 * @ret rc		Return status code
 */
int show_menu ( struct menu *menu, unsigned long timeout,
		const char *select, struct menu_item **selected ) {
	struct menu_item *item;
	struct menu_ui ui;
	char buf[ MENU_COLS + 1 /* NUL */ ];
	int labelled_count = 0;
	int rc;

	/* Initialise UI */
	memset ( &ui, 0, sizeof ( ui ) );
	ui.menu = menu;
	ui.timeout = timeout;
	list_for_each_entry ( item, &menu->items, list ) {
		if ( item->label ) {
			if ( ! labelled_count )
				ui.selected = ui.count;
			labelled_count++;
			if ( select ) {
				if ( strcmp ( select, item->label ) == 0 )
					ui.selected = ui.count;
			} else {
				if ( item->is_default )
					ui.selected = ui.count;
			}
		}
		ui.count++;
	}
	if ( ! labelled_count ) {
		/* Menus with no labelled items cannot be selected
		 * from, and will seriously confuse the navigation
		 * logic.  Refuse to display any such menus.
		 */
		return -ENOENT;
	}

	/* Initialise screen */
	initscr();
	start_color();
	color_set ( CPAIR_NORMAL, NULL );
	curs_set ( 0 );
	erase();

	/* Draw initial content */
	attron ( A_BOLD );
	snprintf ( buf, sizeof ( buf ), "%s", ui.menu->title );
	mvprintw ( TITLE_ROW, ( ( COLS - strlen ( buf ) ) / 2 ), "%s", buf );
	attroff ( A_BOLD );
	draw_menu_items ( &ui );
	draw_menu_item ( &ui, ui.selected );

	/* Enter main loop */
	rc = menu_loop ( &ui, selected );
	assert ( *selected );

	/* Clear screen */
	endwin();

	return rc;
}