iPXE
dynui.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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/** @file
28 *
29 * Dynamic user interfaces
30 *
31 */
32
33#include <stdlib.h>
34#include <string.h>
35#include <assert.h>
36#include <ipxe/list.h>
37#include <ipxe/dynui.h>
38
39/** List of all dynamic user interfaces */
40static LIST_HEAD ( dynamic_uis );
41
42/**
43 * Create dynamic user interface
44 *
45 * @v name User interface name, or NULL
46 * @v title User interface title, or NULL
47 * @ret dynui Dynamic user interface, or NULL on failure
48 */
49struct dynamic_ui * create_dynui ( const char *name, const char *title ) {
50 struct dynamic_ui *dynui;
51 size_t name_len;
52 size_t title_len;
53 size_t len;
54 char *name_copy;
55 char *title_copy;
56
57 /* Destroy any existing user interface of this name */
58 dynui = find_dynui ( name );
59 if ( dynui )
60 destroy_dynui ( dynui );
61
62 /* Use empty title if none given */
63 if ( ! title )
64 title = "";
65
66 /* Allocate user interface */
67 name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
68 title_len = ( strlen ( title ) + 1 /* NUL */ );
69 len = ( sizeof ( *dynui ) + name_len + title_len );
70 dynui = zalloc ( len );
71 if ( ! dynui )
72 return NULL;
73 name_copy = ( ( void * ) ( dynui + 1 ) );
74 title_copy = ( name_copy + name_len );
75
76 /* Initialise user interface */
77 if ( name ) {
78 strcpy ( name_copy, name );
79 dynui->name = name_copy;
80 }
81 strcpy ( title_copy, title );
82 dynui->title = title_copy;
83 INIT_LIST_HEAD ( &dynui->items );
84
85 /* Add to list of user interfaces */
86 list_add_tail ( &dynui->list, &dynamic_uis );
87
88 DBGC ( dynui, "DYNUI %s created with title \"%s\"\n",
89 dynui->name, dynui->title );
90
91 return dynui;
92}
93
94/**
95 * Add dynamic user interface item
96 *
97 * @v dynui Dynamic user interface
98 * @v name Name, or NULL
99 * @v text Text, or NULL
100 * @v flags Flags
101 * @v shortcut Shortcut key
102 * @ret item User interface item, or NULL on failure
103 */
104struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
105 const char *name, const char *text,
106 unsigned int flags, int shortcut ) {
107 struct dynamic_item *item;
108 size_t name_len;
109 size_t text_len;
110 size_t len;
111 char *name_copy;
112 char *text_copy;
113
114 /* Use empty text if none given */
115 if ( ! text )
116 text = "";
117
118 /* Allocate item */
119 name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
120 text_len = ( strlen ( text ) + 1 /* NUL */ );
121 len = ( sizeof ( *item ) + name_len + text_len );
122 item = zalloc ( len );
123 if ( ! item )
124 return NULL;
125 name_copy = ( ( void * ) ( item + 1 ) );
126 text_copy = ( name_copy + name_len );
127
128 /* Initialise item */
129 if ( name ) {
130 strcpy ( name_copy, name );
131 item->name = name_copy;
132 }
133 strcpy ( text_copy, text );
134 item->text = text_copy;
135 item->index = dynui->count++;
136 item->flags = flags;
137 item->shortcut = shortcut;
138
139 /* Add to list of items */
140 list_add_tail ( &item->list, &dynui->items );
141
142 return item;
143}
144
145/**
146 * Destroy dynamic user interface
147 *
148 * @v dynui Dynamic user interface
149 */
150void destroy_dynui ( struct dynamic_ui *dynui ) {
151 struct dynamic_item *item;
152 struct dynamic_item *tmp;
153
154 /* Remove from list of user interfaces */
155 list_del ( &dynui->list );
156
157 /* Free items */
158 list_for_each_entry_safe ( item, tmp, &dynui->items, list ) {
159 list_del ( &item->list );
160 free ( item );
161 }
162
163 /* Free user interface */
164 free ( dynui );
165}
166
167/**
168 * Find dynamic user interface
169 *
170 * @v name User interface name, or NULL
171 * @ret dynui Dynamic user interface, or NULL if not found
172 */
173struct dynamic_ui * find_dynui ( const char *name ) {
174 struct dynamic_ui *dynui;
175
176 list_for_each_entry ( dynui, &dynamic_uis, list ) {
177 if ( ( dynui->name == name ) ||
178 ( strcmp ( dynui->name, name ) == 0 ) ) {
179 return dynui;
180 }
181 }
182
183 return NULL;
184}
185
186/**
187 * Find dynamic user interface item by index
188 *
189 * @v dynui Dynamic user interface
190 * @v index Index
191 * @ret item User interface item, or NULL if not found
192 */
193struct dynamic_item * dynui_item ( struct dynamic_ui *dynui,
194 unsigned int index ) {
195 struct dynamic_item *item;
196
197 list_for_each_entry ( item, &dynui->items, list ) {
198 if ( index-- == 0 )
199 return item;
200 }
201
202 return NULL;
203}
204
205/**
206 * Find dynamic user interface item by shortcut key
207 *
208 * @v dynui Dynamic user interface
209 * @v key Shortcut key
210 * @ret item User interface item, or NULL if not found
211 */
212struct dynamic_item * dynui_shortcut ( struct dynamic_ui *dynui, int key ) {
213 struct dynamic_item *item;
214
215 list_for_each_entry ( item, &dynui->items, list ) {
216 if ( key && ( key == item->shortcut ) )
217 return item;
218 }
219
220 return NULL;
221}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
long index
Definition bigint.h:65
Assertions.
const char * name
Definition ath9k_hw.c:1986
ring len
Length.
Definition dwmac.h:226
struct dynamic_ui * find_dynui(const char *name)
Find dynamic user interface.
Definition dynui.c:173
void destroy_dynui(struct dynamic_ui *dynui)
Destroy dynamic user interface.
Definition dynui.c:150
struct dynamic_ui * create_dynui(const char *name, const char *title)
Create dynamic user interface.
Definition dynui.c:49
struct dynamic_item * dynui_item(struct dynamic_ui *dynui, unsigned int index)
Find dynamic user interface item by index.
Definition dynui.c:193
struct dynamic_item * dynui_shortcut(struct dynamic_ui *dynui, int key)
Find dynamic user interface item by shortcut key.
Definition dynui.c:212
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.
Definition dynui.c:104
Dynamic user interfaces.
uint8_t flags
Flags.
Definition ena.h:7
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
String functions.
unsigned long tmp
Definition linux_pci.h:65
Linked lists.
#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:459
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition list.h:46
#define LIST_HEAD(list)
Declare a static list head.
Definition list.h:38
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:347
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A dynamic user interface item.
Definition dynui.h:30
struct list_head list
List of dynamic user interface items.
Definition dynui.h:32
int shortcut
Shortcut key.
Definition dynui.h:42
unsigned int index
Index.
Definition dynui.h:38
const char * text
Text.
Definition dynui.h:36
unsigned int flags
Flags.
Definition dynui.h:40
const char * name
Name.
Definition dynui.h:34
A dynamic user interface.
Definition dynui.h:16
const char * title
Title.
Definition dynui.h:22
struct list_head list
List of dynamic user interfaces.
Definition dynui.h:18
const char * name
Name.
Definition dynui.h:20
unsigned int count
Number of user interface items.
Definition dynui.h:26
struct list_head items
Dynamic user interface items.
Definition dynui.h:24