iPXE
Data Structures | Macros | Functions
dynui.h File Reference

Dynamic user interfaces. More...

#include <ipxe/list.h>

Go to the source code of this file.

Data Structures

struct  dynamic_ui
 A dynamic user interface. More...
 
struct  dynamic_item
 A dynamic user interface item. More...
 

Macros

#define DYNUI_DEFAULT   0x0001
 Dynamic user interface item is default selection. More...
 
#define DYNUI_SECRET   0x0002
 Dynamic user interface item represents a secret. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct dynamic_uicreate_dynui (const char *name, const char *title)
 Create dynamic user interface. More...
 
struct dynamic_itemadd_dynui_item (struct dynamic_ui *dynui, const char *name, const char *text, unsigned int flags, int shortcut)
 Add dynamic user interface item. More...
 
void destroy_dynui (struct dynamic_ui *dynui)
 Destroy dynamic user interface. More...
 
struct dynamic_uifind_dynui (const char *name)
 Find dynamic user interface. More...
 
struct dynamic_itemdynui_item (struct dynamic_ui *dynui, unsigned int index)
 Find dynamic user interface item by index. More...
 
struct dynamic_itemdynui_shortcut (struct dynamic_ui *dynui, int key)
 Find dynamic user interface item by shortcut key. More...
 
int show_menu (struct dynamic_ui *dynui, unsigned long timeout, const char *select, struct dynamic_item **selected)
 Show menu. More...
 
int show_form (struct dynamic_ui *dynui)
 Show form. More...
 

Detailed Description

Dynamic user interfaces.

Definition in file dynui.h.

Macro Definition Documentation

◆ DYNUI_DEFAULT

#define DYNUI_DEFAULT   0x0001

Dynamic user interface item is default selection.

Definition at line 45 of file dynui.h.

◆ DYNUI_SECRET

#define DYNUI_SECRET   0x0002

Dynamic user interface item represents a secret.

Definition at line 48 of file dynui.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ create_dynui()

struct dynamic_ui* create_dynui ( const char *  name,
const char *  title 
)

Create dynamic user interface.

Parameters
nameUser interface name, or NULL
titleUser interface title, or NULL
Return values
dynuiDynamic user interface, or NULL on failure

Definition at line 48 of file dynui.c.

48  {
49  struct dynamic_ui *dynui;
50  size_t name_len;
51  size_t title_len;
52  size_t len;
53  char *name_copy;
54  char *title_copy;
55 
56  /* Destroy any existing user interface of this name */
57  dynui = find_dynui ( name );
58  if ( dynui )
59  destroy_dynui ( dynui );
60 
61  /* Use empty title if none given */
62  if ( ! title )
63  title = "";
64 
65  /* Allocate user interface */
66  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
67  title_len = ( strlen ( title ) + 1 /* NUL */ );
68  len = ( sizeof ( *dynui ) + name_len + title_len );
69  dynui = zalloc ( len );
70  if ( ! dynui )
71  return NULL;
72  name_copy = ( ( void * ) ( dynui + 1 ) );
73  title_copy = ( name_copy + name_len );
74 
75  /* Initialise user interface */
76  if ( name ) {
77  strcpy ( name_copy, name );
78  dynui->name = name_copy;
79  }
80  strcpy ( title_copy, title );
81  dynui->title = title_copy;
82  INIT_LIST_HEAD ( &dynui->items );
83 
84  /* Add to list of user interfaces */
85  list_add_tail ( &dynui->list, &dynamic_uis );
86 
87  DBGC ( dynui, "DYNUI %s created with title \"%s\"\n",
88  dynui->name, dynui->title );
89 
90  return dynui;
91 }
const char * title
Title.
Definition: dynui.h:21
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
struct dynamic_ui * find_dynui(const char *name)
Find dynamic user interface.
Definition: dynui.c:172
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
A dynamic user interface.
Definition: dynui.h:15
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:17
const char * name
Name.
Definition: dynui.h:19
void destroy_dynui(struct dynamic_ui *dynui)
Destroy dynamic user interface.
Definition: dynui.c:149
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References DBGC, destroy_dynui(), find_dynui(), INIT_LIST_HEAD, dynamic_ui::items, len, dynamic_ui::list, list_add_tail, dynamic_ui::name, name, NULL, strcpy(), strlen(), dynamic_ui::title, and zalloc().

Referenced by dynui_exec().

◆ add_dynui_item()

struct dynamic_item* add_dynui_item ( struct dynamic_ui dynui,
const char *  name,
const char *  text,
unsigned int  flags,
int  shortcut 
)

Add dynamic user interface item.

Parameters
dynuiDynamic user interface
nameName, or NULL
textText, or NULL
flagsFlags
shortcutShortcut key
Return values
itemUser interface item, or NULL on failure

Definition at line 103 of file dynui.c.

105  {
106  struct dynamic_item *item;
107  size_t name_len;
108  size_t text_len;
109  size_t len;
110  char *name_copy;
111  char *text_copy;
112 
113  /* Use empty text if none given */
114  if ( ! text )
115  text = "";
116 
117  /* Allocate item */
118  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
119  text_len = ( strlen ( text ) + 1 /* NUL */ );
120  len = ( sizeof ( *item ) + name_len + text_len );
121  item = zalloc ( len );
122  if ( ! item )
123  return NULL;
124  name_copy = ( ( void * ) ( item + 1 ) );
125  text_copy = ( name_copy + name_len );
126 
127  /* Initialise item */
128  if ( name ) {
129  strcpy ( name_copy, name );
130  item->name = name_copy;
131  }
132  strcpy ( text_copy, text );
133  item->text = text_copy;
134  item->index = dynui->count++;
135  item->flags = flags;
136  item->shortcut = shortcut;
137 
138  /* Add to list of items */
139  list_add_tail ( &item->list, &dynui->items );
140 
141  return item;
142 }
const char * name
Definition: ath9k_hw.c:1984
A dynamic user interface item.
Definition: dynui.h:29
unsigned int count
Number of user interface items.
Definition: dynui.h:25
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
const char * name
Name.
Definition: dynui.h:33
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
uint8_t flags
Flags.
Definition: ena.h:18
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
int shortcut
Shortcut key.
Definition: dynui.h:41
const char * text
Text.
Definition: dynui.h:35
unsigned int flags
Flags.
Definition: dynui.h:39
unsigned int index
Index.
Definition: dynui.h:37
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:31
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References dynamic_ui::count, flags, dynamic_item::flags, dynamic_item::index, dynamic_ui::items, len, dynamic_item::list, list_add_tail, dynamic_item::name, name, NULL, dynamic_item::shortcut, strcpy(), strlen(), dynamic_item::text, and zalloc().

Referenced by item_exec().

◆ destroy_dynui()

void destroy_dynui ( struct dynamic_ui dynui)

Destroy dynamic user interface.

Parameters
dynuiDynamic user interface

Definition at line 149 of file dynui.c.

149  {
150  struct dynamic_item *item;
151  struct dynamic_item *tmp;
152 
153  /* Remove from list of user interfaces */
154  list_del ( &dynui->list );
155 
156  /* Free items */
157  list_for_each_entry_safe ( item, tmp, &dynui->items, list ) {
158  list_del ( &item->list );
159  free ( item );
160  }
161 
162  /* Free user interface */
163  free ( dynui );
164 }
A dynamic user interface item.
Definition: dynui.h:29
unsigned long tmp
Definition: linux_pci.h:63
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:458
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:17
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:31

References free, dynamic_ui::items, dynamic_item::list, dynamic_ui::list, list_del, list_for_each_entry_safe, and tmp.

Referenced by choose_exec(), create_dynui(), dynui_exec(), and present_exec().

◆ find_dynui()

struct dynamic_ui* find_dynui ( const char *  name)

Find dynamic user interface.

Parameters
nameUser interface name, or NULL
Return values
dynuiDynamic user interface, or NULL if not found

Definition at line 172 of file dynui.c.

172  {
173  struct dynamic_ui *dynui;
174 
175  list_for_each_entry ( dynui, &dynamic_uis, list ) {
176  if ( ( dynui->name == name ) ||
177  ( strcmp ( dynui->name, name ) == 0 ) ) {
178  return dynui;
179  }
180  }
181 
182  return NULL;
183 }
const char * name
Definition: ath9k_hw.c:1984
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
A dynamic user interface.
Definition: dynui.h:15
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:17
const char * name
Name.
Definition: dynui.h:19
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References dynamic_ui::list, list_for_each_entry, dynamic_ui::name, name, NULL, and strcmp().

Referenced by create_dynui(), and parse_dynui().

◆ dynui_item()

struct dynamic_item* dynui_item ( struct dynamic_ui dynui,
unsigned int  index 
)

Find dynamic user interface item by index.

Parameters
dynuiDynamic user interface
indexIndex
Return values
itemUser interface item, or NULL if not found

Definition at line 192 of file dynui.c.

193  {
194  struct dynamic_item *item;
195 
196  list_for_each_entry ( item, &dynui->items, list ) {
197  if ( index-- == 0 )
198  return item;
199  }
200 
201  return NULL;
202 }
A dynamic user interface item.
Definition: dynui.h:29
long index
Definition: bigint.h:61
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References index, dynamic_ui::items, dynamic_item::list, list_for_each_entry, and NULL.

Referenced by draw_menu_item(), and menu_loop().

◆ dynui_shortcut()

struct dynamic_item* dynui_shortcut ( struct dynamic_ui dynui,
int  key 
)

Find dynamic user interface item by shortcut key.

Parameters
dynuiDynamic user interface
keyShortcut key
Return values
itemUser interface item, or NULL if not found

Definition at line 211 of file dynui.c.

211  {
212  struct dynamic_item *item;
213 
214  list_for_each_entry ( item, &dynui->items, list ) {
215  if ( key && ( key == item->shortcut ) )
216  return item;
217  }
218 
219  return NULL;
220 }
A dynamic user interface item.
Definition: dynui.h:29
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
int shortcut
Shortcut key.
Definition: dynui.h:41
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
union @383 key
Sense key.
Definition: scsi.h:18

References dynamic_ui::items, key, dynamic_item::list, list_for_each_entry, NULL, and dynamic_item::shortcut.

Referenced by form_loop(), and menu_loop().

◆ 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
#define COLS
Definition: vga.h:27
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
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().

◆ show_form()

int show_form ( struct dynamic_ui dynui)

Show form.

Parameters
dynuiDynamic user interface
Return values
rcReturn status code

Definition at line 507 of file form_ui.c.

507  {
508  struct form *form;
509  int rc;
510 
511  /* Allocate and initialise structure */
512  form = alloc_form ( dynui );
513  if ( ! form ) {
514  rc = -ENOMEM;
515  goto err_alloc;
516  }
517 
518  /* Parse setting names and load current values */
519  if ( ( rc = parse_names ( form ) ) != 0 )
520  goto err_parse_names;
521  load_values ( form );
522 
523  /* Lay out form on screen */
524  if ( ( rc = layout_form ( form ) ) != 0 )
525  goto err_layout;
526 
527  /* Draw initial form */
528  initscr();
529  start_color();
530  draw_form ( form );
531 
532  /* Run main loop */
533  if ( ( rc = form_loop ( form ) ) != 0 )
534  goto err_loop;
535 
536  err_loop:
538  endwin();
539  err_layout:
540  err_parse_names:
541  free_form ( form );
542  err_alloc:
543  return rc;
544 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void draw_form(struct form *form)
Draw form.
Definition: form_ui.c:252
static int parse_names(struct form *form)
Parse setting names.
Definition: form_ui.c:320
#define start_color()
Definition: curses.h:396
int endwin(void)
Finalise console environment.
Definition: wininit.c:31
static int layout_form(struct form *form)
Assign form rows.
Definition: form_ui.c:158
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
#define ENOMEM
Not enough space.
Definition: errno.h:534
A form.
Definition: form_ui.c:64
static void free_form(struct form *form)
Free form.
Definition: form_ui.c:141
WINDOW * initscr(void)
Initialise console environment.
Definition: wininit.c:17
static void load_values(struct form *form)
Load current input values.
Definition: form_ui.c:357
static struct form * alloc_form(struct dynamic_ui *dynui)
Allocate form.
Definition: form_ui.c:99
static int form_loop(struct form *form)
Form main loop.
Definition: form_ui.c:422
#define color_set(cpno, opts)
Definition: curses.h:240
struct dynamic_ui * dynui
Dynamic user interface.
Definition: form_ui.c:66
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References alloc_form(), color_set, CPAIR_NORMAL, draw_form(), form::dynui, endwin(), ENOMEM, form_loop(), free_form(), initscr(), layout_form(), load_values(), NULL, parse_names(), rc, and start_color.

Referenced by login_ui(), and present_exec().