iPXE
Functions
dynui.c File Reference

Dynamic user interfaces. More...

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/list.h>
#include <ipxe/dynui.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static LIST_HEAD (dynamic_uis)
 List of all dynamic user interfaces. More...
 
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...
 

Detailed Description

Dynamic user interfaces.

Definition in file dynui.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ LIST_HEAD()

static LIST_HEAD ( dynamic_uis  )
static

List of all dynamic user interfaces.

◆ 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().