iPXE
Data Structures | Macros | Functions
menu_ui.c File Reference

Menu interface. More...

#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/jumpscroll.h>
#include <ipxe/menu.h>

Go to the source code of this file.

Data Structures

struct  menu_ui
 A menu user interface. More...
 

Macros

#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
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static struct menu_itemmenu_item (struct menu *menu, unsigned int index)
 Return a numbered menu item. More...
 
static void draw_menu_item (struct menu_ui *ui, unsigned int index)
 Draw a numbered menu item. More...
 
static void draw_menu_items (struct menu_ui *ui)
 Draw the current block of menu items. More...
 
static int menu_loop (struct menu_ui *ui, struct menu_item **selected)
 Menu main loop. More...
 
int show_menu (struct menu *menu, unsigned long timeout, const char *select, struct menu_item **selected)
 Show menu. More...
 

Detailed Description

Menu interface.

Definition in file menu_ui.c.

Macro Definition Documentation

◆ TITLE_ROW

#define TITLE_ROW   1U

Definition at line 43 of file menu_ui.c.

◆ MENU_ROW

#define MENU_ROW   3U

Definition at line 44 of file menu_ui.c.

◆ MENU_COL

#define MENU_COL   1U

Definition at line 45 of file menu_ui.c.

◆ MENU_ROWS

#define MENU_ROWS   ( LINES - 2U - MENU_ROW )

Definition at line 46 of file menu_ui.c.

◆ MENU_COLS

#define MENU_COLS   ( COLS - 2U )

Definition at line 47 of file menu_ui.c.

◆ MENU_PAD

#define MENU_PAD   2U

Definition at line 48 of file menu_ui.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ menu_item()

static struct menu_item* menu_item ( struct menu menu,
unsigned int  index 
)
static

Return a numbered menu item.

Parameters
menuMenu
indexIndex
Return values
itemMenu item, or NULL

Definition at line 67 of file menu_ui.c.

67  {
68  struct menu_item *item;
69 
70  list_for_each_entry ( item, &menu->items, list ) {
71  if ( index-- == 0 )
72  return item;
73  }
74 
75  return NULL;
76 }
A menu item.
Definition: menu.h:27
struct list_head items
Menu items.
Definition: menu.h:23
struct list_head list
List of menu items.
Definition: menu.h:29
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
A menu.
Definition: menu.h:15
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References index, menu::items, menu_item::list, list_for_each_entry, and NULL.

Referenced by draw_menu_item(), and menu_loop().

◆ draw_menu_item()

static void draw_menu_item ( struct menu_ui ui,
unsigned int  index 
)
static

Draw a numbered menu item.

Parameters
uiMenu user interface
indexIndex

Definition at line 84 of file menu_ui.c.

84  {
85  struct menu_item *item;
86  unsigned int row_offset;
87  char buf[ MENU_COLS + 1 /* NUL */ ];
88  char timeout_buf[6]; /* "(xxx)" + NUL */
89  size_t timeout_len;
90  size_t max_len;
91  size_t len;
92 
93  /* Move to start of row */
94  row_offset = ( index - ui->scroll.first );
95  move ( ( MENU_ROW + row_offset ), MENU_COL );
96 
97  /* Get menu item */
98  item = menu_item ( ui->menu, index );
99  if ( item ) {
100 
101  /* Draw separators in a different colour */
102  if ( ! item->label )
104 
105  /* Highlight if this is the selected item */
106  if ( index == ui->scroll.current ) {
108  attron ( A_BOLD );
109  }
110 
111  /* Construct row */
112  memset ( buf, ' ', ( sizeof ( buf ) - 1 ) );
113  buf[ sizeof ( buf ) -1 ] = '\0';
114  len = strlen ( item->text );
115  max_len = ( sizeof ( buf ) - 1 /* NUL */ - ( 2 * MENU_PAD ) );
116  if ( len > max_len )
117  len = max_len;
118  memcpy ( ( buf + MENU_PAD ), item->text, len );
119 
120  /* Add timeout if applicable */
121  timeout_len =
122  snprintf ( timeout_buf, sizeof ( timeout_buf ), "(%ld)",
123  ( ( ui->timeout + TICKS_PER_SEC - 1 ) /
124  TICKS_PER_SEC ) );
125  if ( ( index == ui->scroll.current ) && ( ui->timeout != 0 ) ) {
126  memcpy ( ( buf + MENU_COLS - MENU_PAD - timeout_len ),
127  timeout_buf, timeout_len );
128  }
129 
130  /* Print row */
131  printw ( "%s", buf );
132 
133  /* Reset attributes */
135  attroff ( A_BOLD );
136 
137  } else {
138  /* Clear row if there is no corresponding menu item */
139  clrtoeol();
140  }
141 
142  /* Move cursor back to start of row */
143  move ( ( MENU_ROW + row_offset ), MENU_COL );
144 }
A menu item.
Definition: menu.h:27
struct menu * menu
Menu.
Definition: menu_ui.c:53
int printw(char *,...)
unsigned long timeout
Timeout (0=indefinite)
Definition: menu_ui.c:57
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
static void size_t size_t max_len
Definition: entropy.h:153
const char * label
Label.
Definition: menu.h:31
static int attroff(int attrs)
Definition: curses.h:508
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
void * memcpy(void *dest, const void *src, size_t len) __nonnull
unsigned int first
First visible item.
Definition: jumpscroll.h:21
static int clrtoeol(void)
Definition: curses.h:553
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition: ansicol.h:46
const char * text
Text.
Definition: menu.h:33
uint32_t len
Length.
Definition: ena.h:14
static int move(int y, int x)
Definition: curses.h:593
#define CPAIR_SELECT
Highlighted text.
Definition: ansicol.h:43
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
#define A_BOLD
Definition: curses.h:138
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
unsigned int current
Currently selected item.
Definition: jumpscroll.h:19
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int attron(int attrs)
Definition: curses.h:512
void * memset(void *dest, int character, size_t len) __nonnull

References A_BOLD, attroff(), attron(), clrtoeol(), color_set, CPAIR_NORMAL, CPAIR_SELECT, CPAIR_SEPARATOR, jump_scroller::current, jump_scroller::first, index, menu_item::label, len, max_len, memcpy(), memset(), menu_ui::menu, MENU_COL, MENU_COLS, menu_item(), MENU_PAD, MENU_ROW, move(), NULL, printw(), menu_ui::scroll, snprintf(), strlen(), menu_item::text, TICKS_PER_SEC, and menu_ui::timeout.

Referenced by draw_menu_items(), menu_loop(), and show_menu().

◆ draw_menu_items()

static void draw_menu_items ( struct menu_ui ui)
static

Draw the current block of menu items.

Parameters
uiMenu user interface

Definition at line 151 of file menu_ui.c.

151  {
152  unsigned int i;
153 
154  /* Draw ellipses before and/or after the list as necessary */
156  mvaddstr ( ( MENU_ROW - 1 ), ( MENU_COL + MENU_PAD ),
157  ( jump_scroll_is_first ( &ui->scroll ) ? " " : "..." ) );
159  ( jump_scroll_is_last ( &ui->scroll ) ? " " : "..." ) );
161 
162  /* Draw visible items */
163  for ( i = 0 ; i < MENU_ROWS ; i++ )
164  draw_menu_item ( ui, ( ui->scroll.first + i ) );
165 }
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
unsigned int first
First visible item.
Definition: jumpscroll.h:21
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition: ansicol.h:46
static int jump_scroll_is_last(struct jump_scroller *scroll)
Check if jump scroller is currently on last page.
Definition: jumpscroll.h:41
static int jump_scroll_is_first(struct jump_scroller *scroll)
Check if jump scroller is currently on first page.
Definition: jumpscroll.h:30
#define color_set(cpno, opts)
Definition: curses.h:240
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int mvaddstr(int y, int x, const char *str)
Definition: curses.h:617

References color_set, CPAIR_NORMAL, CPAIR_SEPARATOR, draw_menu_item(), jump_scroller::first, jump_scroll_is_first(), jump_scroll_is_last(), MENU_COL, MENU_PAD, MENU_ROW, MENU_ROWS, mvaddstr(), NULL, and menu_ui::scroll.

Referenced by menu_loop(), and show_menu().

◆ menu_loop()

static int menu_loop ( struct menu_ui ui,
struct menu_item **  selected 
)
static

Menu main loop.

Parameters
uiMenu user interface
Return values
selectedSelected item
rcReturn status code

Definition at line 174 of file menu_ui.c.

174  {
175  struct menu_item *item;
176  unsigned long timeout;
177  unsigned int previous;
178  int key;
179  int i;
180  int move;
181  int chosen = 0;
182  int rc = 0;
183 
184  do {
185  /* Record current selection */
186  previous = ui->scroll.current;
187 
188  /* Calculate timeout as remainder of current second */
189  timeout = ( ui->timeout % TICKS_PER_SEC );
190  if ( ( timeout == 0 ) && ( ui->timeout != 0 ) )
192  ui->timeout -= timeout;
193 
194  /* Get key */
195  move = 0;
196  key = getkey ( timeout );
197  if ( key < 0 ) {
198  /* Choose default if we finally time out */
199  if ( ui->timeout == 0 )
200  chosen = 1;
201  } else {
202  /* Cancel any timeout */
203  ui->timeout = 0;
204 
205  /* Handle scroll keys */
206  move = jump_scroll_key ( &ui->scroll, key );
207 
208  /* Handle other keys */
209  switch ( key ) {
210  case ESC:
211  case CTRL_C:
212  rc = -ECANCELED;
213  break;
214  case CR:
215  case LF:
216  chosen = 1;
217  break;
218  default:
219  i = 0;
220  list_for_each_entry ( item, &ui->menu->items,
221  list ) {
222  if ( ! ( item->shortcut &&
223  ( item->shortcut == key ) ) ) {
224  i++;
225  continue;
226  }
227  ui->scroll.current = i;
228  if ( item->label ) {
229  chosen = 1;
230  } else {
231  move = +1;
232  }
233  }
234  break;
235  }
236  }
237 
238  /* Move selection, if applicable */
239  while ( move ) {
240  move = jump_scroll_move ( &ui->scroll, move );
241  item = menu_item ( ui->menu, ui->scroll.current );
242  if ( item->label )
243  break;
244  }
245 
246  /* Redraw selection if necessary */
247  if ( ( ui->scroll.current != previous ) || ( timeout != 0 ) ) {
248  draw_menu_item ( ui, previous );
249  if ( jump_scroll ( &ui->scroll ) )
250  draw_menu_items ( ui );
251  draw_menu_item ( ui, ui->scroll.current );
252  }
253 
254  /* Record selection */
255  item = menu_item ( ui->menu, ui->scroll.current );
256  assert ( item != NULL );
257  assert ( item->label != NULL );
258  *selected = item;
259 
260  } while ( ( rc == 0 ) && ! chosen );
261 
262  return rc;
263 }
A menu item.
Definition: menu.h:27
int getkey(unsigned long timeout)
Get single keypress.
Definition: getkey.c:71
struct menu * menu
Menu.
Definition: menu_ui.c:53
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long timeout
Timeout (0=indefinite)
Definition: menu_ui.c:57
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
const char * label
Label.
Definition: menu.h:31
struct list_head items
Menu items.
Definition: menu.h:23
#define ECANCELED
Operation canceled.
Definition: errno.h:343
struct list_head list
List of menu items.
Definition: menu.h:29
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:119
#define CTRL_C
Definition: keys.h:20
#define ESC
Escape character.
Definition: ansiesc.h:92
#define LF
Definition: keys.h:47
static int move(int y, int x)
Definition: curses.h:593
int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition: jumpscroll.c:42
#define CR
Definition: keys.h:48
void timeout(int)
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
unsigned int current
Currently selected item.
Definition: jumpscroll.h:19
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
union @375 key
Sense key.
Definition: scsi.h:18
int jump_scroll_move(struct jump_scroller *scroll, int move)
Move scroller.
Definition: jumpscroll.c:78
int shortcut
Shortcut key.
Definition: menu.h:35

References assert(), CR, CTRL_C, jump_scroller::current, draw_menu_item(), draw_menu_items(), ECANCELED, ESC, getkey(), menu::items, jump_scroll(), jump_scroll_key(), jump_scroll_move(), key, menu_item::label, LF, menu_item::list, list_for_each_entry, menu_ui::menu, menu_item(), move(), NULL, rc, menu_ui::scroll, menu_item::shortcut, TICKS_PER_SEC, menu_ui::timeout, and timeout().

Referenced by show_menu().

◆ show_menu()

int show_menu ( struct menu menu,
unsigned long  timeout,
const char *  select,
struct menu_item **  selected 
)

Show menu.

Parameters
menuMenu
timeoutTimeout period, in ticks (0=indefinite)
Return values
selectedSelected item
rcReturn status code

Definition at line 273 of file menu_ui.c.

274  {
275  struct menu_item *item;
276  struct menu_ui ui;
277  char buf[ MENU_COLS + 1 /* NUL */ ];
278  int labelled_count = 0;
279  int rc;
280 
281  /* Initialise UI */
282  memset ( &ui, 0, sizeof ( ui ) );
283  ui.menu = menu;
284  ui.scroll.rows = MENU_ROWS;
285  ui.timeout = timeout;
286  list_for_each_entry ( item, &menu->items, list ) {
287  if ( item->label ) {
288  if ( ! labelled_count )
289  ui.scroll.current = ui.scroll.count;
290  labelled_count++;
291  if ( select ) {
292  if ( strcmp ( select, item->label ) == 0 )
293  ui.scroll.current = ui.scroll.count;
294  } else {
295  if ( item->is_default )
296  ui.scroll.current = ui.scroll.count;
297  }
298  }
299  ui.scroll.count++;
300  }
301  if ( ! labelled_count ) {
302  /* Menus with no labelled items cannot be selected
303  * from, and will seriously confuse the navigation
304  * logic. Refuse to display any such menus.
305  */
306  return -ENOENT;
307  }
308 
309  /* Initialise screen */
310  initscr();
311  start_color();
313  curs_set ( 0 );
314  erase();
315 
316  /* Draw initial content */
317  attron ( A_BOLD );
318  snprintf ( buf, sizeof ( buf ), "%s", ui.menu->title );
319  mvprintw ( TITLE_ROW, ( ( COLS - strlen ( buf ) ) / 2 ), "%s", buf );
320  attroff ( A_BOLD );
321  jump_scroll ( &ui.scroll );
322  draw_menu_items ( &ui );
323  draw_menu_item ( &ui, ui.scroll.current );
324 
325  /* Enter main loop */
326  rc = menu_loop ( &ui, selected );
327  assert ( *selected );
328 
329  /* Clear screen */
330  endwin();
331 
332  return rc;
333 }
A menu item.
Definition: menu.h:27
struct menu * menu
Menu.
Definition: menu_ui.c:53
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int erase(void)
Completely clear the screen.
Definition: clear.c:97
A menu user interface.
Definition: menu_ui.c:51
const char * label
Label.
Definition: menu.h:31
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
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define mvprintw(y, x, fmt,...)
Definition: curses.h:648
struct list_head items
Menu items.
Definition: menu.h:23
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
WINDOW * initscr(void)
Initialise console environment.
Definition: wininit.c:17
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:119
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition: posix_io.c:229
#define COLS
Definition: curses.h:111
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:157
int is_default
Is default item.
Definition: menu.h:37
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)
#define A_BOLD
Definition: curses.h:138
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int attron(int attrs)
Definition: curses.h:512
void * memset(void *dest, int character, size_t len) __nonnull

References A_BOLD, assert(), attroff(), attron(), color_set, COLS, jump_scroller::count, CPAIR_NORMAL, jump_scroller::current, curs_set(), draw_menu_item(), draw_menu_items(), endwin(), ENOENT, erase(), initscr(), menu_item::is_default, menu::items, jump_scroll(), menu_item::label, list_for_each_entry, memset(), menu_ui::menu, MENU_COLS, menu_loop(), MENU_ROWS, mvprintw, NULL, rc, jump_scroller::rows, menu_ui::scroll, select(), snprintf(), start_color, strcmp(), strlen(), menu_ui::timeout, timeout(), menu::title, and TITLE_ROW.

Referenced by choose_exec().