[hci] Provide a general concept of a text widget set

Create a generic abstraction of a text widget, refactor the existing
editable text box widget to use this abstraction, add an
implementation of a non-editable text label widget, and generalise the
login user interface to use this generic widget abstraction.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-05-15 14:10:33 +01:00
parent d7e58c5a81
commit dc118c5369
9 changed files with 509 additions and 140 deletions

View File

@@ -11,51 +11,39 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <curses.h>
#include <ipxe/editstring.h>
#include <ipxe/widget.h>
/** An editable text box widget */
struct edit_box {
/** Text widget */
struct widget widget;
/** Editable string */
struct edit_string string;
/** Containing window */
WINDOW *win;
/** Row */
unsigned int row;
/** Starting column */
unsigned int col;
/** Width */
unsigned int width;
/** First displayed character */
unsigned int first;
/** Flags */
unsigned int flags;
};
/** Editable text box widget flags */
enum edit_box_flags {
/** Show stars instead of contents (for password widgets) */
EDITBOX_STARS = 0x0001,
};
extern void init_editbox ( struct edit_box *box, char **buf,
WINDOW *win, unsigned int row, unsigned int col,
unsigned int width, unsigned int flags )
__attribute__ (( nonnull (1, 2) ));
extern void draw_editbox ( struct edit_box *box ) __nonnull;
static inline int edit_editbox ( struct edit_box *box, int key ) __nonnull;
extern struct widget_operations editbox_operations;
/**
* Edit text box widget
* Initialise text box widget
*
* @v box Editable text box widget
* @v key Key pressed by user
* @ret key Key returned to application, or zero
*
* You must call draw_editbox() to update the display after calling
* edit_editbox().
*
* @v row Row
* @v col Starting column
* @v width Width
* @v flags Flags
* @v buf Dynamically allocated string buffer
*/
static inline int edit_editbox ( struct edit_box *box, int key ) {
return edit_string ( &box->string, key );
static inline __attribute__ (( always_inline )) void
init_editbox ( struct edit_box *box, unsigned int row, unsigned int col,
unsigned int width, unsigned int flags, char **buf ) {
init_widget ( &box->widget, &editbox_operations, row, col,
width, ( flags | WIDGET_EDITABLE ) );
init_editstring ( &box->string, buf );
if ( *buf )
box->string.cursor = strlen ( *buf );
}
#endif /* _IPXE_EDITBOX_H */

View File

@@ -417,6 +417,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#define ERRFILE_x25519 ( ERRFILE_OTHER | 0x005f0000 )
#define ERRFILE_des ( ERRFILE_OTHER | 0x00600000 )
#define ERRFILE_editstring ( ERRFILE_OTHER | 0x00610000 )
#define ERRFILE_widget_ui ( ERRFILE_OTHER | 0x00620000 )
/** @} */

42
src/include/ipxe/label.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef _IPXE_LABEL_H
#define _IPXE_LABEL_H
/** @file
*
* Text label widget
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <curses.h>
#include <ipxe/widget.h>
/** A text label widget */
struct label {
/** Text widget */
struct widget widget;
/** Label text */
const char *text;
};
extern struct widget_operations label_operations;
/**
* Initialise text label widget
*
* @v label Text label widget
* @v row Row
* @v col Starting column
* @v width Width
* @v text Label text
*/
static inline __attribute__ (( always_inline )) void
init_label ( struct label *label, unsigned int row, unsigned int col,
unsigned int width, const char *text ) {
init_widget ( &label->widget, &label_operations, row, col, width, 0 );
label->text = text;
}
#endif /* _IPXE_LABEL_H */

151
src/include/ipxe/widget.h Normal file
View File

@@ -0,0 +1,151 @@
#ifndef _IPXE_WIDGET_H
#define _IPXE_WIDGET_H
/** @file
*
* Text widgets
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <curses.h>
#include <ipxe/list.h>
/** A text widget set */
struct widgets {
/** List of widgets (in tab order) */
struct list_head list;
/** Containing window */
WINDOW *win;
};
/** A text widget */
struct widget {
/** List of widgets (in tab order) */
struct list_head list;
/** Widget operations */
struct widget_operations *op;
/** Row */
unsigned int row;
/** Starting column */
unsigned int col;
/** Width */
unsigned int width;
/** Flags */
unsigned int flags;
};
/** Text widget flags */
enum widget_flags {
/** Widget may have input focus */
WIDGET_EDITABLE = 0x0001,
/** Widget contains a secret */
WIDGET_SECRET = 0x0002,
};
/** Text widget operations */
struct widget_operations {
/**
* Draw widget
*
* @v widgets Text widget set
* @v widget Text widget
*/
void ( * draw ) ( struct widgets *widgets, struct widget *widget );
/**
* Edit widget
*
* @v widgets Text widget set
* @v widget Text widget
* @v key Key pressed by user
* @ret key Key returned to application, or zero
*
* This will not update the display: you must call the draw()
* method to ensure that any changes to an editable widget are
* displayed to the user.
*/
int ( * edit ) ( struct widgets *widgets, struct widget *widget,
int key );
};
/**
* Initialise text widget set
*
* @v widgets Text widget set
* @v win Containing window
*/
static inline __attribute__ (( always_inline )) void
init_widgets ( struct widgets *widgets, WINDOW *win ) {
INIT_LIST_HEAD ( &widgets->list );
widgets->win = ( win ? win : stdscr );
}
/**
* Initialise text widget
*
* @v widget Text widget
* @v op Text widget operations
* @v row Row
* @v col Starting column
* @v width Width
*/
static inline __attribute__ (( always_inline )) void
init_widget ( struct widget *widget, struct widget_operations *op,
unsigned int row, unsigned int col, unsigned int width,
unsigned int flags ) {
widget->op = op;
widget->row = row;
widget->col = col;
widget->width = width;
widget->flags = flags;
}
/**
* Append text widget
*
* @v widgets Text widget set
* @v widget Text widget
*/
static inline __attribute__ (( always_inline )) void
add_widget ( struct widgets *widgets, struct widget *widget ) {
list_add_tail ( &widget->list, &widgets->list );
}
/**
* Draw text widget
*
* @v widgets Text widget set
* @v widget Text widget
*/
static inline __attribute__ (( always_inline )) void
draw_widget ( struct widgets *widgets, struct widget *widget ) {
widget->op->draw ( widgets, widget );
}
/**
* Edit text widget
*
* @v widgets Text widget set
* @v widget Text widget
* @v key Key pressed by user
* @ret key Key returned to application, or zero
*
* This will not update the display: you must call draw_widget() to
* ensure that any changes to an editable widget are displayed to the
* user.
*/
static inline __attribute__ (( always_inline )) int
edit_widget ( struct widgets *widgets, struct widget *widget, int key ) {
return widget->op->edit ( widgets, widget, key );
}
extern int widget_ui ( struct widgets *widgets );
#endif /* _IPXE_WIDGET_H */