iPXE
menu_ui.c
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Menu interface
29  *
30  */
31 
32 #include <string.h>
33 #include <errno.h>
34 #include <curses.h>
35 #include <ipxe/keys.h>
36 #include <ipxe/timer.h>
37 #include <ipxe/console.h>
38 #include <ipxe/ansicol.h>
39 #include <ipxe/jumpscroll.h>
40 #include <ipxe/dynui.h>
41 
42 /* Screen layout */
43 #define TITLE_ROW 1U
44 #define MENU_ROW 3U
45 #define MENU_COL 1U
46 #define MENU_ROWS ( LINES - 2U - MENU_ROW )
47 #define MENU_COLS ( COLS - 2U )
48 #define MENU_PAD 2U
49 
50 /** A menu user interface */
51 struct menu_ui {
52  /** Dynamic user interface */
53  struct dynamic_ui *dynui;
54  /** Jump scroller */
56  /** Remaining timeout (0=indefinite) */
57  unsigned long timeout;
58  /** Post-activity timeout (0=indefinite) */
59  unsigned long retimeout;
60 };
61 
62 /**
63  * Draw a numbered menu item
64  *
65  * @v ui Menu user interface
66  * @v index Index
67  */
68 static void draw_menu_item ( struct menu_ui *ui, unsigned int index ) {
69  struct dynamic_item *item;
70  unsigned int row_offset;
71  char buf[ MENU_COLS + 1 /* NUL */ ];
72  char timeout_buf[6]; /* "(xxx)" + NUL */
73  size_t timeout_len;
74  size_t max_len;
75  size_t len;
76 
77  /* Move to start of row */
78  row_offset = ( index - ui->scroll.first );
79  move ( ( MENU_ROW + row_offset ), MENU_COL );
80 
81  /* Get menu item */
82  item = dynui_item ( ui->dynui, index );
83  if ( item ) {
84 
85  /* Draw separators in a different colour */
86  if ( ! item->name )
88 
89  /* Highlight if this is the selected item */
90  if ( index == ui->scroll.current ) {
92  attron ( A_BOLD );
93  }
94 
95  /* Construct row */
96  memset ( buf, ' ', ( sizeof ( buf ) - 1 ) );
97  buf[ sizeof ( buf ) -1 ] = '\0';
98  len = strlen ( item->text );
99  max_len = ( sizeof ( buf ) - 1 /* NUL */ - ( 2 * MENU_PAD ) );
100  if ( len > max_len )
101  len = max_len;
102  memcpy ( ( buf + MENU_PAD ), item->text, len );
103 
104  /* Add timeout if applicable */
105  timeout_len =
106  snprintf ( timeout_buf, sizeof ( timeout_buf ), "(%ld)",
107  ( ( ui->timeout + TICKS_PER_SEC - 1 ) /
108  TICKS_PER_SEC ) );
109  if ( ( index == ui->scroll.current ) && ( ui->timeout != 0 ) ) {
110  memcpy ( ( buf + MENU_COLS - MENU_PAD - timeout_len ),
111  timeout_buf, timeout_len );
112  }
113 
114  /* Print row */
115  printw ( "%s", buf );
116 
117  /* Reset attributes */
119  attroff ( A_BOLD );
120 
121  } else {
122  /* Clear row if there is no corresponding menu item */
123  clrtoeol();
124  }
125 
126  /* Move cursor back to start of row */
127  move ( ( MENU_ROW + row_offset ), MENU_COL );
128 }
129 
130 /**
131  * Draw the current block of menu items
132  *
133  * @v ui Menu user interface
134  */
135 static void draw_menu_items ( struct menu_ui *ui ) {
136  unsigned int i;
137 
138  /* Draw ellipses before and/or after the list as necessary */
140  mvaddstr ( ( MENU_ROW - 1 ), ( MENU_COL + MENU_PAD ),
141  ( jump_scroll_is_first ( &ui->scroll ) ? " " : "..." ) );
143  ( jump_scroll_is_last ( &ui->scroll ) ? " " : "..." ) );
145 
146  /* Draw visible items */
147  for ( i = 0 ; i < MENU_ROWS ; i++ )
148  draw_menu_item ( ui, ( ui->scroll.first + i ) );
149 }
150 
151 /**
152  * Menu main loop
153  *
154  * @v ui Menu user interface
155  * @ret selected Selected item
156  * @ret rc Return status code
157  */
158 static int menu_loop ( struct menu_ui *ui, struct dynamic_item **selected ) {
159  struct dynamic_item *item;
160  unsigned long timeout;
161  unsigned int previous;
162  unsigned int move;
163  int key;
164  int chosen = 0;
165  int rc = 0;
166 
167  do {
168  /* Record current selection */
169  previous = ui->scroll.current;
170 
171  /* Calculate timeout as remainder of current second */
172  timeout = ( ui->timeout % TICKS_PER_SEC );
173  if ( ( timeout == 0 ) && ( ui->timeout != 0 ) )
175  ui->timeout -= timeout;
176 
177  /* Get key */
178  move = SCROLL_NONE;
179  key = getkey ( timeout );
180  if ( key < 0 ) {
181  /* Choose default if we finally time out */
182  if ( ui->timeout == 0 )
183  chosen = 1;
184  } else {
185  /* Reset timeout after activity */
186  ui->timeout = ui->retimeout;
187 
188  /* Handle scroll keys */
189  move = jump_scroll_key ( &ui->scroll, key );
190 
191  /* Handle other keys */
192  switch ( key ) {
193  case ESC:
194  case CTRL_C:
195  rc = -ECANCELED;
196  break;
197  case CR:
198  case LF:
199  chosen = 1;
200  break;
201  default:
202  item = dynui_shortcut ( ui->dynui, key );
203  if ( item ) {
204  ui->scroll.current = item->index;
205  if ( item->name ) {
206  chosen = 1;
207  } else {
208  move = SCROLL_DOWN;
209  }
210  }
211  break;
212  }
213  }
214 
215  /* Move selection, if applicable */
216  while ( move ) {
217  move = jump_scroll_move ( &ui->scroll, move );
218  item = dynui_item ( ui->dynui, ui->scroll.current );
219  if ( item->name )
220  break;
221  }
222 
223  /* Redraw selection if necessary */
224  if ( ( ui->scroll.current != previous ) || ( timeout != 0 ) ) {
225  draw_menu_item ( ui, previous );
226  if ( jump_scroll ( &ui->scroll ) )
227  draw_menu_items ( ui );
228  draw_menu_item ( ui, ui->scroll.current );
229  }
230 
231  /* Record selection */
232  item = dynui_item ( ui->dynui, ui->scroll.current );
233  assert ( item != NULL );
234  assert ( item->name != NULL );
235  *selected = item;
236 
237  } while ( ( rc == 0 ) && ! chosen );
238 
239  return rc;
240 }
241 
242 /**
243  * Show menu
244  *
245  * @v dynui Dynamic user interface
246  * @v timeout Initial timeout period, in ticks (0=indefinite)
247  * @v retimeout Post-activity timeout period, in ticks (0=indefinite)
248  * @ret selected Selected item
249  * @ret rc Return status code
250  */
251 int show_menu ( struct dynamic_ui *dynui, unsigned long timeout,
252  unsigned long retimeout, const char *select,
253  struct dynamic_item **selected ) {
254  struct dynamic_item *item;
255  struct menu_ui ui;
256  char buf[ MENU_COLS + 1 /* NUL */ ];
257  int named_count = 0;
258  int rc;
259 
260  /* Initialise UI */
261  memset ( &ui, 0, sizeof ( ui ) );
262  ui.dynui = dynui;
263  ui.scroll.rows = MENU_ROWS;
264  ui.timeout = timeout;
265  ui.retimeout = retimeout;
266 
267  list_for_each_entry ( item, &dynui->items, list ) {
268  if ( item->name ) {
269  if ( ! named_count )
270  ui.scroll.current = ui.scroll.count;
271  named_count++;
272  if ( select ) {
273  if ( strcmp ( select, item->name ) == 0 )
274  ui.scroll.current = ui.scroll.count;
275  } else {
276  if ( item->flags & DYNUI_DEFAULT )
277  ui.scroll.current = ui.scroll.count;
278  }
279  }
280  ui.scroll.count++;
281  }
282  if ( ! named_count ) {
283  /* Menus with no named items cannot be selected from,
284  * and will seriously confuse the navigation logic.
285  * Refuse to display any such menus.
286  */
287  return -ENOENT;
288  }
289 
290  /* Initialise screen */
291  initscr();
292  start_color();
294  curs_set ( 0 );
295  erase();
296 
297  /* Draw initial content */
298  attron ( A_BOLD );
299  snprintf ( buf, sizeof ( buf ), "%s", ui.dynui->title );
300  mvprintw ( TITLE_ROW, ( ( COLS - strlen ( buf ) ) / 2 ), "%s", buf );
301  attroff ( A_BOLD );
302  jump_scroll ( &ui.scroll );
303  draw_menu_items ( &ui );
304  draw_menu_item ( &ui, ui.scroll.current );
305 
306  /* Enter main loop */
307  rc = menu_loop ( &ui, selected );
308  assert ( *selected );
309 
310  /* Clear screen */
311  endwin();
312 
313  return rc;
314 }
int getkey(unsigned long timeout)
Get single keypress.
Definition: getkey.c:71
MuCurses header file.
const char * title
Title.
Definition: dynui.h:21
int printw(char *,...)
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long timeout
Remaining timeout (0=indefinite)
Definition: menu_ui.c:57
A dynamic user interface item.
Definition: dynui.h:29
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
int erase(void)
Completely clear the screen.
Definition: clear.c:97
A menu user interface.
Definition: menu_ui.c:51
Error codes.
static int attroff(int attrs)
Definition: curses.h:508
#define start_color()
Definition: curses.h:396
int endwin(void)
Finalise console environment.
Definition: wininit.c:31
long index
Definition: bigint.h:62
#define SCROLL_NONE
Do not scroll.
Definition: jumpscroll.h:47
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define mvprintw(y, x, fmt,...)
Definition: curses.h:648
iPXE timers
A jump scroller.
Definition: jumpscroll.h:15
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
WINDOW * initscr(void)
Initialise console environment.
Definition: wininit.c:17
const char * name
Name.
Definition: dynui.h:33
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct dynamic_item * dynui_shortcut(struct dynamic_ui *dynui, int key)
Find dynamic user interface item by shortcut key.
Definition: dynui.c:211
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
unsigned int first
First visible item.
Definition: jumpscroll.h:23
unsigned int count
Total number of items.
Definition: jumpscroll.h:19
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:140
#define COLS
Definition: vga.h:27
#define CTRL_C
Definition: keys.h:20
#define ESC
Escape character.
Definition: ansiesc.h:92
User interaction.
unsigned int jump_scroll_move(struct jump_scroller *scroll, unsigned int move)
Move scroller.
Definition: jumpscroll.c:92
static int clrtoeol(void)
Definition: curses.h:553
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition: posix_io.c:229
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition: ansicol.h:46
#define SCROLL_DOWN
Scroll down by one line.
Definition: jumpscroll.h:53
#define LF
Definition: keys.h:47
A dynamic user interface.
Definition: dynui.h:15
static int jump_scroll_is_last(struct jump_scroller *scroll)
Check if jump scroller is currently on last page.
Definition: jumpscroll.h:72
int curs_set(int visibility)
Set cursor visibility.
Definition: mucurses.c:153
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
const char * text
Text.
Definition: dynui.h:35
#define DYNUI_DEFAULT
Dynamic user interface item is default selection.
Definition: dynui.h:45
Key definitions.
static int jump_scroll_is_first(struct jump_scroller *scroll)
Check if jump scroller is currently on first page.
Definition: jumpscroll.h:61
static int move(int y, int x)
Definition: curses.h:593
unsigned long retimeout
Post-activity timeout (0=indefinite)
Definition: menu_ui.c:59
#define CPAIR_SELECT
Highlighted text.
Definition: ansicol.h:43
Jump scrolling.
#define CR
Definition: keys.h:48
unsigned int rows
Maximum number of visible rows.
Definition: jumpscroll.h:17
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
#define color_set(cpno, opts)
Definition: curses.h:240
void timeout(int)
Dynamic user interfaces.
#define A_BOLD
Definition: curses.h:138
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
unsigned int flags
Flags.
Definition: dynui.h:39
struct dynamic_ui * dynui
Dynamic user interface.
Definition: menu_ui.c:53
unsigned int current
Currently selected item.
Definition: jumpscroll.h:21
unsigned int index
Index.
Definition: dynui.h:37
struct dynamic_item * dynui_item(struct dynamic_ui *dynui, unsigned int index)
Find dynamic user interface item by index.
Definition: dynui.c:192
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
unsigned int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition: jumpscroll.c:42
static int mvaddstr(int y, int x, const char *str)
Definition: curses.h:617
union @383 key
Sense key.
Definition: scsi.h:18
ANSI colours.
static int attron(int attrs)
Definition: curses.h:512
void * memset(void *dest, int character, size_t len) __nonnull