[settings] Add hierarchy navigation in "config" user interface

Allow the user to browse through the settings block hierarchy.

Originally-implemented-by: Glenn Brown <glenn@myri.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/1/head
Michael Brown 2011-03-22 22:58:24 +00:00
parent a04603a070
commit da312ba03b
1 changed files with 260 additions and 199 deletions

View File

@ -47,12 +47,12 @@ FILE_LICENCE ( GPL2_OR_LATER );
#define SETTINGS_LIST_COL 1 #define SETTINGS_LIST_COL 1
#define SETTINGS_LIST_ROWS 16 #define SETTINGS_LIST_ROWS 16
#define INFO_ROW 20 #define INFO_ROW 20
#define ALERT_ROW 20 #define ALERT_ROW 22
#define INSTRUCTION_ROW 22 #define INSTRUCTION_ROW 22
#define INSTRUCTION_PAD " " #define INSTRUCTION_PAD " "
/** Layout of text within a setting widget */ /** Layout of text within a setting widget */
struct setting_row { struct setting_row_text {
char start[0]; char start[0];
char pad1[1]; char pad1[1];
char name[15]; char name[15];
@ -62,15 +62,17 @@ struct setting_row {
char nul; char nul;
} __attribute__ (( packed )); } __attribute__ (( packed ));
/** A setting widget */ /** A setting row widget */
struct setting_widget { struct setting_row_widget {
/** Settings block */ /** Target configuration settings block
*
* Valid only for rows that lead to new settings blocks.
*/
struct settings *settings; struct settings *settings;
/** Number of applicable settings */ /** Configuration setting
unsigned int num_settings; *
/** Index of the first visible setting, for scrolling. */ * Valid only for rows that represent individual settings.
unsigned int first_visible; */
/** Configuration setting */
struct setting *setting; struct setting *setting;
/** Screen row */ /** Screen row */
unsigned int row; unsigned int row;
@ -86,123 +88,142 @@ struct setting_widget {
char value[256]; /* enough size for a DHCP string */ char value[256]; /* enough size for a DHCP string */
}; };
static void load_setting ( struct setting_widget *widget ) __nonnull; /** A settings widget */
static int save_setting ( struct setting_widget *widget ) __nonnull; struct setting_widget {
static void init_widget ( struct setting_widget *widget, /** Settings block */
struct settings *settings ) __nonnull; struct settings *settings;
static void draw_setting ( struct setting_widget *widget ) __nonnull; /** Number of rows */
static int edit_setting ( struct setting_widget *widget, int key ) __nonnull; unsigned int num_rows;
static void select_setting ( struct setting_widget *widget, /** Current row index */
unsigned int index ) __nonnull; unsigned int current;
static void reveal ( struct setting_widget *widget, unsigned int n) __nonnull; /** Index of the first visible row, for scrolling. */
static void vmsg ( unsigned int row, const char *fmt, va_list args ) __nonnull; unsigned int first_visible;
static void msg ( unsigned int row, const char *fmt, ... ) __nonnull; /** Active row */
static void valert ( const char *fmt, va_list args ) __nonnull; struct setting_row_widget row;
static void alert ( const char *fmt, ... ) __nonnull; };
static void draw_info_row ( struct settings *settings,
struct setting *setting ) __nonnull;
static int main_loop ( struct settings *settings ) __nonnull;
/** /**
* Load setting widget value from configuration settings * Select a setting row
* *
* @v widget Setting widget * @v widget Setting widget
* * @v index Index of setting row
* @ret count Number of settings rows
*/ */
static void load_setting ( struct setting_widget *widget ) { static unsigned int select_setting_row ( struct setting_widget *widget,
unsigned int index ) {
struct settings *settings;
struct settings *origin;
struct setting *setting;
unsigned int count = 0;
/* Mark as not editing */ /* Initialise structure */
widget->editing = 0; memset ( &widget->row, 0, sizeof ( widget->row ) );
widget->current = index;
widget->row.row = ( SETTINGS_LIST_ROW + index - widget->first_visible );
widget->row.col = SETTINGS_LIST_COL;
/* Include parent settings block, if applicable */
if ( widget->settings->parent && ( count++ == index ) ) {
widget->row.settings = widget->settings->parent;
snprintf ( widget->row.value, sizeof ( widget->row.value ),
"../" );
}
/* Include any child settings blocks, if applicable */
list_for_each_entry ( settings, &widget->settings->children, siblings ){
if ( count++ == index ) {
widget->row.settings = settings;
snprintf ( widget->row.value,
sizeof ( widget->row.value ), "%s/",
settings->name );
}
}
/* Include any applicable settings */
for_each_table_entry ( setting, SETTINGS ) {
if ( ! setting_applies ( widget->settings, setting ) )
continue;
if ( count++ == index ) {
widget->row.setting = setting;
/* Read current setting value */ /* Read current setting value */
if ( fetchf_setting ( widget->settings, widget->setting, fetchf_setting ( widget->settings, widget->row.setting,
widget->value, sizeof ( widget->value ) ) < 0 ) { widget->row.value,
widget->value[0] = '\0'; sizeof ( widget->row.value ) );
}
/* Check setting's origin */ /* Check setting's origin */
widget->originates_here = origin = fetch_setting_origin ( widget->settings,
( widget->settings == widget->row.setting );
fetch_setting_origin ( widget->settings, widget->setting ) ); widget->row.originates_here =
( origin == widget->settings );
}
}
/* Initialise edit box */ /* Initialise edit box */
init_editbox ( &widget->editbox, widget->value, init_editbox ( &widget->row.editbox, widget->row.value,
sizeof ( widget->value ), NULL, widget->row, sizeof ( widget->row.value ), NULL, widget->row.row,
( widget->col + offsetof ( struct setting_row, value )), ( widget->row.col +
sizeof ( ( ( struct setting_row * ) NULL )->value ), 0); offsetof ( struct setting_row_text, value ) ),
sizeof ( ( ( struct setting_row_text * ) NULL )->value ),
0 );
return count;
}
static size_t string_copy ( char *dest, const char *src, size_t len ) {
size_t src_len;
src_len = strlen ( src );
if ( len > src_len )
len = src_len;
memcpy ( dest, src, len );
return len;
} }
/** /**
* Save setting widget value back to configuration settings * Draw setting row
* *
* @v widget Setting widget * @v widget Setting widget
*/ */
static int save_setting ( struct setting_widget *widget ) { static void draw_setting_row ( struct setting_widget *widget ) {
return storef_setting ( widget->settings, widget->setting, struct setting_row_text text;
widget->value ); unsigned int curs_offset;
}
/**
* Initialise the scrolling setting widget, drawing initial display.
*
* @v widget Setting widget
* @v settings Settings block
*/
static void init_widget ( struct setting_widget *widget,
struct settings *settings ) {
struct setting *setting;
memset ( widget, 0, sizeof ( *widget ) );
widget->settings = settings;
for_each_table_entry ( setting, SETTINGS ) {
if ( setting_applies ( settings, setting ) )
widget->num_settings++;
}
widget->first_visible = SETTINGS_LIST_ROWS;
reveal ( widget, 0 );
}
/**
* Draw setting widget
*
* @v widget Setting widget
*/
static void draw_setting ( struct setting_widget *widget ) {
struct setting_row row;
unsigned int len;
unsigned int curs_col;
char *value; char *value;
/* Fill row with spaces */ /* Fill row with spaces */
memset ( &row, ' ', sizeof ( row ) ); memset ( &text, ' ', sizeof ( text ) );
row.nul = '\0'; text.nul = '\0';
/* Construct row content */
if ( widget->row.settings ) {
/* Construct space-padded name */
curs_offset = ( offsetof ( typeof ( text ), name ) +
string_copy ( text.name, widget->row.value,
sizeof ( text.name ) ) );
} else {
/* Construct dot-padded name */ /* Construct dot-padded name */
memset ( row.name, '.', sizeof ( row.name ) ); memset ( text.name, '.', sizeof ( text.name ) );
len = strlen ( widget->setting->name ); string_copy ( text.name, widget->row.setting->name,
if ( len > sizeof ( row.name ) ) sizeof ( text.name ) );
len = sizeof ( row.name );
memcpy ( row.name, widget->setting->name, len );
/* Construct space-padded value */ /* Construct space-padded value */
value = widget->value; value = widget->row.value;
if ( ! *value ) if ( ! *value )
value = "<not specified>"; value = "<not specified>";
len = strlen ( value ); curs_offset = ( offsetof ( typeof ( text ), value ) +
if ( len > sizeof ( row.value ) ) string_copy ( text.value, value,
len = sizeof ( row.value ); sizeof ( text.value ) ) );
memcpy ( row.value, value, len ); }
curs_col = ( widget->col + offsetof ( typeof ( row ), value )
+ len );
/* Print row */ /* Print row */
if ( widget->originates_here ) if ( widget->row.originates_here || widget->row.settings )
attron ( A_BOLD ); attron ( A_BOLD );
mvprintw ( widget->row, widget->col, "%s", row.start ); mvprintw ( widget->row.row, widget->row.col, "%s", text.start );
attroff ( A_BOLD ); attroff ( A_BOLD );
move ( widget->row, curs_col ); move ( widget->row.row, widget->row.col + curs_offset );
if ( widget->editing )
draw_editbox ( &widget->editbox );
} }
/** /**
@ -213,34 +234,20 @@ static void draw_setting ( struct setting_widget *widget ) {
* @ret key Key returned to application, or zero * @ret key Key returned to application, or zero
*/ */
static int edit_setting ( struct setting_widget *widget, int key ) { static int edit_setting ( struct setting_widget *widget, int key ) {
widget->editing = 1; assert ( widget->row.setting != NULL );
return edit_editbox ( &widget->editbox, key ); widget->row.editing = 1;
return edit_editbox ( &widget->row.editbox, key );
} }
/** /**
* Select a setting for display updates, by index. * Save setting widget value back to configuration settings
* *
* @v widget Setting widget * @v widget Setting widget
* @v settings Settings block
* @v index Index of setting with settings list
*/ */
static void select_setting ( struct setting_widget *widget, static int save_setting ( struct setting_widget *widget ) {
unsigned int index ) { assert ( widget->row.setting != NULL );
unsigned int skip = offsetof ( struct setting_widget, setting ); return storef_setting ( widget->settings, widget->row.setting,
widget->row.value );
/* Reset the widget, preserving static state. */
memset ( ( char * ) widget + skip, 0, sizeof ( *widget ) - skip );
widget->row = SETTINGS_LIST_ROW + index - widget->first_visible;
widget->col = SETTINGS_LIST_COL;
for_each_table_entry ( widget->setting, SETTINGS ) {
if ( ! setting_applies ( widget->settings, widget->setting ) )
continue;
if ( index-- == 0 )
break;
}
/* Read current setting value */
load_setting ( widget );
} }
/** /**
@ -314,173 +321,227 @@ static void alert ( const char *fmt, ... ) {
/** /**
* Draw title row * Draw title row
*
* @v widget Setting widget
*/ */
static void draw_title_row ( void ) { static void draw_title_row ( struct setting_widget *widget ) {
const char *name;
clearmsg ( TITLE_ROW );
name = settings_name ( widget->settings );
attron ( A_BOLD ); attron ( A_BOLD );
msg ( TITLE_ROW, "iPXE option configuration console" ); msg ( TITLE_ROW, "iPXE option configuration%s%s",
( name[0] ? " - " : "" ), name );
attroff ( A_BOLD ); attroff ( A_BOLD );
} }
/** /**
* Draw information row * Draw information row
* *
* @v settings Settings block * @v widget Setting widget
* @v setting Current configuration setting
*/ */
static void draw_info_row ( struct settings *settings, static void draw_info_row ( struct setting_widget *widget ) {
struct setting *setting ) {
struct settings *origin; struct settings *origin;
char buf[32]; char buf[32];
/* Determine a suitable setting name */ /* Draw nothing unless this row represents a setting */
origin = fetch_setting_origin ( settings, setting );
if ( ! origin )
origin = settings;
setting_name ( origin, setting, buf, sizeof ( buf ) );
clearmsg ( INFO_ROW ); clearmsg ( INFO_ROW );
if ( ! widget->row.setting )
return;
/* Determine a suitable setting name */
origin = fetch_setting_origin ( widget->settings, widget->row.setting );
if ( ! origin )
origin = widget->settings;
setting_name ( origin, widget->row.setting, buf, sizeof ( buf ) );
/* Draw row */
attron ( A_BOLD ); attron ( A_BOLD );
msg ( INFO_ROW, "%s - %s", buf, setting->description ); msg ( INFO_ROW, "%s - %s", buf, widget->row.setting->description );
attroff ( A_BOLD ); attroff ( A_BOLD );
} }
/** /**
* Draw instruction row * Draw instruction row
* *
* @v editing Editing in progress flag * @v widget Setting widget
*/ */
static void draw_instruction_row ( int editing ) { static void draw_instruction_row ( struct setting_widget *widget ) {
clearmsg ( INSTRUCTION_ROW ); clearmsg ( INSTRUCTION_ROW );
if ( editing ) { if ( widget->row.editing ) {
msg ( INSTRUCTION_ROW, msg ( INSTRUCTION_ROW,
"Enter - accept changes" INSTRUCTION_PAD "Enter - accept changes" INSTRUCTION_PAD
"Ctrl-C - discard changes" ); "Ctrl-C - discard changes" );
} else { } else {
msg ( INSTRUCTION_ROW, msg ( INSTRUCTION_ROW,
"Ctrl-D - delete setting" INSTRUCTION_PAD "%sCtrl-X - exit configuration utility",
"Ctrl-X - exit configuration utility" ); ( widget->row.setting ?
"Ctrl-D - delete setting" INSTRUCTION_PAD : "" ) );
} }
} }
/** /**
* Reveal a setting by index: Scroll the setting list to reveal the * Reveal setting row
* specified setting.
* *
* @widget The main loop's display widget. * @v widget Setting widget
* @n The index of the setting to reveal. * @v index Index of setting row
*/ */
static void reveal ( struct setting_widget *widget, unsigned int n) static void reveal_setting_row ( struct setting_widget *widget,
{ unsigned int index ) {
unsigned int i; unsigned int i;
/* Simply return if setting N is already on-screen. */ /* Simply return if setting N is already on-screen. */
if ( n - widget->first_visible < SETTINGS_LIST_ROWS ) if ( index - widget->first_visible < SETTINGS_LIST_ROWS )
return; return;
/* Jump scroll to make the specified setting visible. */ /* Jump scroll to make the specified setting row visible. */
while ( widget->first_visible < n ) while ( widget->first_visible < index )
widget->first_visible += SETTINGS_LIST_ROWS; widget->first_visible += SETTINGS_LIST_ROWS;
while ( widget->first_visible > n ) while ( widget->first_visible > index )
widget->first_visible -= SETTINGS_LIST_ROWS; widget->first_visible -= SETTINGS_LIST_ROWS;
/* Draw elipses before and/or after the settings list to /* Draw ellipses before and/or after the settings list to
represent any invisible settings. */ * represent any invisible settings.
*/
mvaddstr ( SETTINGS_LIST_ROW - 1, mvaddstr ( SETTINGS_LIST_ROW - 1,
SETTINGS_LIST_COL + 1, SETTINGS_LIST_COL + 1,
widget->first_visible > 0 ? "..." : " " ); widget->first_visible > 0 ? "..." : " " );
mvaddstr ( SETTINGS_LIST_ROW + SETTINGS_LIST_ROWS, mvaddstr ( SETTINGS_LIST_ROW + SETTINGS_LIST_ROWS,
SETTINGS_LIST_COL + 1, SETTINGS_LIST_COL + 1,
( ( widget->first_visible + SETTINGS_LIST_ROWS ) ( ( widget->first_visible + SETTINGS_LIST_ROWS )
< widget->num_settings ? "..." : " " ) ); < widget->num_rows ? "..." : " " ) );
/* Draw visible settings. */ /* Draw visible settings. */
for ( i = 0; i < SETTINGS_LIST_ROWS; i++ ) { for ( i = 0; i < SETTINGS_LIST_ROWS; i++ ) {
if ( ( widget->first_visible + i ) < widget->num_settings ) { if ( ( widget->first_visible + i ) < widget->num_rows ) {
select_setting ( widget, widget->first_visible + i ); select_setting_row ( widget,
draw_setting ( widget ); widget->first_visible + i );
draw_setting_row ( widget );
} else { } else {
clearmsg ( SETTINGS_LIST_ROW + i ); clearmsg ( SETTINGS_LIST_ROW + i );
} }
} }
}
/* Set the widget to the current row, which will be redrawn /**
appropriately by the main loop. */ * Reveal setting row
select_setting ( widget, n ); *
* @v widget Setting widget
* @v settings Settings block
*/
static void init_widget ( struct setting_widget *widget,
struct settings *settings ) {
widget->settings = settings;
widget->num_rows = select_setting_row ( widget, 0 );
widget->first_visible = SETTINGS_LIST_ROWS;
draw_title_row ( widget );
reveal_setting_row ( widget, 0 );
select_setting_row ( widget, 0 );
} }
static int main_loop ( struct settings *settings ) { static int main_loop ( struct settings *settings ) {
struct setting_widget widget; struct setting_widget widget;
unsigned int current = 0; int redraw = 1;
unsigned int next; unsigned int next;
int key; int key;
int rc; int rc;
/* Print initial screen content */ /* Print initial screen content */
draw_title_row();
color_set ( CPAIR_NORMAL, NULL ); color_set ( CPAIR_NORMAL, NULL );
memset ( &widget, 0, sizeof ( widget ) );
init_widget ( &widget, settings ); init_widget ( &widget, settings );
while ( 1 ) { while ( 1 ) {
/* Redraw information and instruction rows */
draw_info_row ( widget.settings, widget.setting );
draw_instruction_row ( widget.editing );
/* Redraw current setting */ /* Redraw rows if necessary */
color_set ( ( widget.editing ? CPAIR_EDIT : CPAIR_SELECT ), if ( redraw ) {
NULL ); draw_info_row ( &widget );
draw_setting ( &widget ); draw_instruction_row ( &widget );
color_set ( ( widget.row.editing ?
CPAIR_EDIT : CPAIR_SELECT ), NULL );
draw_setting_row ( &widget );
color_set ( CPAIR_NORMAL, NULL );
redraw = 0;
}
if ( widget.row.editing ) {
/* Sanity check */
assert ( widget.row.setting != NULL );
/* Redraw edit box */
color_set ( CPAIR_EDIT, NULL );
draw_editbox ( &widget.row.editbox );
color_set ( CPAIR_NORMAL, NULL ); color_set ( CPAIR_NORMAL, NULL );
key = getkey ( 0 ); /* Process keypress */
if ( widget.editing ) { key = edit_setting ( &widget, getkey ( 0 ) );
key = edit_setting ( &widget, key );
switch ( key ) { switch ( key ) {
case CR: case CR:
case LF: case LF:
if ( ( rc = save_setting ( &widget ) ) != 0 ) { if ( ( rc = save_setting ( &widget ) ) != 0 )
alert ( " Could not set %s: %s ", alert ( " %s ", strerror ( rc ) );
widget.setting->name,
strerror ( rc ) );
}
/* Fall through */ /* Fall through */
case CTRL_C: case CTRL_C:
load_setting ( &widget ); select_setting_row ( &widget, widget.current );
redraw = 1;
break; break;
default: default:
/* Do nothing */ /* Do nothing */
break; break;
} }
} else { } else {
next = current;
/* Process keypress */
key = getkey ( 0 );
next = widget.current;
switch ( key ) { switch ( key ) {
case KEY_DOWN: case KEY_DOWN:
if ( next < ( widget.num_settings - 1 ) ) if ( widget.current < ( widget.num_rows - 1 ) )
reveal ( &widget, ++next ); next++;
break; break;
case KEY_UP: case KEY_UP:
if ( next > 0 ) if ( widget.current > 0 )
reveal ( &widget, --next ) ; next--;
break; break;
case CTRL_D: case CTRL_D:
delete_setting ( widget.settings, if ( ! widget.row.setting )
widget.setting ); break;
select_setting ( &widget, next ); if ( ( rc = delete_setting ( widget.settings,
draw_setting ( &widget ); widget.row.setting ) ) != 0 ) {
alert ( " %s ", strerror ( rc ) );
}
select_setting_row ( &widget, widget.current );
redraw = 1;
break; break;
case CTRL_X: case CTRL_X:
return 0; return 0;
case CR:
case LF:
if ( widget.row.settings ) {
init_widget ( &widget,
widget.row.settings );
redraw = 1;
}
/* Fall through */
default: default:
if ( widget.row.setting ) {
edit_setting ( &widget, key ); edit_setting ( &widget, key );
redraw = 1;
}
break; break;
} }
if ( next != current ) { if ( next != widget.current ) {
draw_setting ( &widget ); draw_setting_row ( &widget );
select_setting ( &widget, next ); redraw = 1;
current = next; reveal_setting_row ( &widget, next );
select_setting_row ( &widget, next );
} }
} }
} }
} }
int settings_ui ( struct settings *settings ) { int settings_ui ( struct settings *settings ) {