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/dynui.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 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 dynamic_item **selected)
 Menu main loop. More...
 
int show_menu (struct dynamic_ui *dynui, unsigned long timeout, const char *select, struct dynamic_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  )

◆ 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 66 of file menu_ui.c.

66  {
67  struct dynamic_item *item;
68  unsigned int row_offset;
69  char buf[ MENU_COLS + 1 /* NUL */ ];
70  char timeout_buf[6]; /* "(xxx)" + NUL */
71  size_t timeout_len;
72  size_t max_len;
73  size_t len;
74 
75  /* Move to start of row */
76  row_offset = ( index - ui->scroll.first );
77  move ( ( MENU_ROW + row_offset ), MENU_COL );
78 
79  /* Get menu item */
80  item = dynui_item ( ui->dynui, index );
81  if ( item ) {
82 
83  /* Draw separators in a different colour */
84  if ( ! item->name )
86 
87  /* Highlight if this is the selected item */
88  if ( index == ui->scroll.current ) {
90  attron ( A_BOLD );
91  }
92 
93  /* Construct row */
94  memset ( buf, ' ', ( sizeof ( buf ) - 1 ) );
95  buf[ sizeof ( buf ) -1 ] = '\0';
96  len = strlen ( item->text );
97  max_len = ( sizeof ( buf ) - 1 /* NUL */ - ( 2 * MENU_PAD ) );
98  if ( len > max_len )
99  len = max_len;
100  memcpy ( ( buf + MENU_PAD ), item->text, len );
101 
102  /* Add timeout if applicable */
103  timeout_len =
104  snprintf ( timeout_buf, sizeof ( timeout_buf ), "(%ld)",
105  ( ( ui->timeout + TICKS_PER_SEC - 1 ) /
106  TICKS_PER_SEC ) );
107  if ( ( index == ui->scroll.current ) && ( ui->timeout != 0 ) ) {
108  memcpy ( ( buf + MENU_COLS - MENU_PAD - timeout_len ),
109  timeout_buf, timeout_len );
110  }
111 
112  /* Print row */
113  printw ( "%s", buf );
114 
115  /* Reset attributes */
117  attroff ( A_BOLD );
118 
119  } else {
120  /* Clear row if there is no corresponding menu item */
121  clrtoeol();
122  }
123 
124  /* Move cursor back to start of row */
125  move ( ( MENU_ROW + row_offset ), MENU_COL );
126 }
int printw(char *,...)
unsigned long timeout
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
uint16_t max_len
Maximum length (in bytes)
Definition: ntlm.h:18
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
const char * name
Name.
Definition: dynui.h:33
unsigned int first
First visible item.
Definition: jumpscroll.h:23
static int clrtoeol(void)
Definition: curses.h:553
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
#define CPAIR_SEPARATOR
Unselectable text (e.g.
Definition: ansicol.h:46
uint32_t len
Length.
Definition: ena.h:14
const char * text
Text.
Definition: dynui.h:35
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
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
#define A_BOLD
Definition: curses.h:138
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
struct dynamic_ui * dynui
Dynamic user interface.
Definition: menu_ui.c:53
unsigned int current
Currently selected item.
Definition: jumpscroll.h:21
struct dynamic_item * dynui_item(struct dynamic_ui *dynui, unsigned int index)
Find dynamic user interface item by index.
Definition: dynui.c:192
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
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, menu_ui::dynui, dynui_item(), jump_scroller::first, index, len, max_len, memcpy(), memset(), MENU_COL, MENU_COLS, MENU_PAD, MENU_ROW, move(), dynamic_item::name, NULL, printw(), menu_ui::scroll, snprintf(), strlen(), dynamic_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 133 of file menu_ui.c.

133  {
134  unsigned int i;
135 
136  /* Draw ellipses before and/or after the list as necessary */
138  mvaddstr ( ( MENU_ROW - 1 ), ( MENU_COL + MENU_PAD ),
139  ( jump_scroll_is_first ( &ui->scroll ) ? " " : "..." ) );
141  ( jump_scroll_is_last ( &ui->scroll ) ? " " : "..." ) );
143 
144  /* Draw visible items */
145  for ( i = 0 ; i < MENU_ROWS ; i++ )
146  draw_menu_item ( ui, ( ui->scroll.first + i ) );
147 }
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
unsigned int first
First visible item.
Definition: jumpscroll.h:23
#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:72
static int jump_scroll_is_first(struct jump_scroller *scroll)
Check if jump scroller is currently on first page.
Definition: jumpscroll.h:61
#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:321
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 dynamic_item **  selected 
)
static

Menu main loop.

Parameters
uiMenu user interface
Return values
selectedSelected item
rcReturn status code

Definition at line 156 of file menu_ui.c.

156  {
157  struct dynamic_item *item;
158  unsigned long timeout;
159  unsigned int previous;
160  unsigned int move;
161  int key;
162  int chosen = 0;
163  int rc = 0;
164 
165  do {
166  /* Record current selection */
167  previous = ui->scroll.current;
168 
169  /* Calculate timeout as remainder of current second */
170  timeout = ( ui->timeout % TICKS_PER_SEC );
171  if ( ( timeout == 0 ) && ( ui->timeout != 0 ) )
173  ui->timeout -= timeout;
174 
175  /* Get key */
176  move = SCROLL_NONE;
177  key = getkey ( timeout );
178  if ( key < 0 ) {
179  /* Choose default if we finally time out */
180  if ( ui->timeout == 0 )
181  chosen = 1;
182  } else {
183  /* Cancel any timeout */
184  ui->timeout = 0;
185 
186  /* Handle scroll keys */
187  move = jump_scroll_key ( &ui->scroll, key );
188 
189  /* Handle other keys */
190  switch ( key ) {
191  case ESC:
192  case CTRL_C:
193  rc = -ECANCELED;
194  break;
195  case CR:
196  case LF:
197  chosen = 1;
198  break;
199  default:
200  item = dynui_shortcut ( ui->dynui, key );
201  if ( item ) {
202  ui->scroll.current = item->index;
203  if ( item->name ) {
204  chosen = 1;
205  } else {
206  move = SCROLL_DOWN;
207  }
208  }
209  break;
210  }
211  }
212 
213  /* Move selection, if applicable */
214  while ( move ) {
215  move = jump_scroll_move ( &ui->scroll, move );
216  item = dynui_item ( ui->dynui, ui->scroll.current );
217  if ( item->name )
218  break;
219  }
220 
221  /* Redraw selection if necessary */
222  if ( ( ui->scroll.current != previous ) || ( timeout != 0 ) ) {
223  draw_menu_item ( ui, previous );
224  if ( jump_scroll ( &ui->scroll ) )
225  draw_menu_items ( ui );
226  draw_menu_item ( ui, ui->scroll.current );
227  }
228 
229  /* Record selection */
230  item = dynui_item ( ui->dynui, ui->scroll.current );
231  assert ( item != NULL );
232  assert ( item->name != NULL );
233  *selected = item;
234 
235  } while ( ( rc == 0 ) && ! chosen );
236 
237  return rc;
238 }
int getkey(unsigned long timeout)
Get single keypress.
Definition: getkey.c:71
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long timeout
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
#define SCROLL_NONE
Do not scroll.
Definition: jumpscroll.h:47
#define ECANCELED
Operation canceled.
Definition: errno.h:343
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
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:140
#define CTRL_C
Definition: keys.h:20
#define ESC
Escape character.
Definition: ansiesc.h:92
unsigned int jump_scroll_move(struct jump_scroller *scroll, unsigned int move)
Move scroller.
Definition: jumpscroll.c:92
#define SCROLL_DOWN
Scroll down by one line.
Definition: jumpscroll.h:53
#define LF
Definition: keys.h:47
static int move(int y, int x)
Definition: curses.h:593
#define CR
Definition: keys.h:48
void timeout(int)
struct jump_scroller scroll
Jump scroller.
Definition: menu_ui.c:55
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
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
unsigned int jump_scroll_key(struct jump_scroller *scroll, int key)
Jump scrolling.
Definition: jumpscroll.c:42
union @382 key
Sense key.
Definition: crypto.h:284

References assert(), CR, CTRL_C, jump_scroller::current, draw_menu_item(), draw_menu_items(), menu_ui::dynui, dynui_item(), dynui_shortcut(), ECANCELED, ESC, getkey(), dynamic_item::index, jump_scroll(), jump_scroll_key(), jump_scroll_move(), key, LF, move(), dynamic_item::name, NULL, rc, menu_ui::scroll, SCROLL_DOWN, SCROLL_NONE, TICKS_PER_SEC, menu_ui::timeout, and timeout().

Referenced by show_menu().

◆ show_menu()

int show_menu ( struct dynamic_ui dynui,
unsigned long  timeout,
const char *  select,
struct dynamic_item **  selected 
)

Show menu.

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

Definition at line 248 of file menu_ui.c.

249  {
250  struct dynamic_item *item;
251  struct menu_ui ui;
252  char buf[ MENU_COLS + 1 /* NUL */ ];
253  int named_count = 0;
254  int rc;
255 
256  /* Initialise UI */
257  memset ( &ui, 0, sizeof ( ui ) );
258  ui.dynui = dynui;
259  ui.scroll.rows = MENU_ROWS;
260  ui.timeout = timeout;
261  list_for_each_entry ( item, &dynui->items, list ) {
262  if ( item->name ) {
263  if ( ! named_count )
264  ui.scroll.current = ui.scroll.count;
265  named_count++;
266  if ( select ) {
267  if ( strcmp ( select, item->name ) == 0 )
268  ui.scroll.current = ui.scroll.count;
269  } else {
270  if ( item->flags & DYNUI_DEFAULT )
271  ui.scroll.current = ui.scroll.count;
272  }
273  }
274  ui.scroll.count++;
275  }
276  if ( ! named_count ) {
277  /* Menus with no named items cannot be selected from,
278  * and will seriously confuse the navigation logic.
279  * Refuse to display any such menus.
280  */
281  return -ENOENT;
282  }
283 
284  /* Initialise screen */
285  initscr();
286  start_color();
288  curs_set ( 0 );
289  erase();
290 
291  /* Draw initial content */
292  attron ( A_BOLD );
293  snprintf ( buf, sizeof ( buf ), "%s", ui.dynui->title );
294  mvprintw ( TITLE_ROW, ( ( COLS - strlen ( buf ) ) / 2 ), "%s", buf );
295  attroff ( A_BOLD );
296  jump_scroll ( &ui.scroll );
297  draw_menu_items ( &ui );
298  draw_menu_item ( &ui, ui.scroll.current );
299 
300  /* Enter main loop */
301  rc = menu_loop ( &ui, selected );
302  assert ( *selected );
303 
304  /* Clear screen */
305  endwin();
306 
307  return rc;
308 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A dynamic user interface item.
Definition: dynui.h:29
int erase(void)
Completely clear the screen.
Definition: clear.c:97
A menu user interface.
Definition: menu_ui.c:51
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
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
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)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:140
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 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:173
#define DYNUI_DEFAULT
Dynamic user interface item is default selection.
Definition: dynui.h:45
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
unsigned int flags
Flags.
Definition: dynui.h:39
struct dynamic_ui * dynui
Dynamic user interface.
Definition: menu_ui.c:53
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
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(), menu_ui::dynui, DYNUI_DEFAULT, endwin(), ENOENT, erase(), dynamic_item::flags, initscr(), dynamic_ui::items, jump_scroll(), list_for_each_entry, memset(), MENU_COLS, menu_loop(), MENU_ROWS, mvprintw, dynamic_item::name, NULL, rc, jump_scroller::rows, menu_ui::scroll, select(), snprintf(), start_color, strcmp(), strlen(), menu_ui::timeout, timeout(), dynamic_ui::title, and TITLE_ROW.

Referenced by choose_exec().