]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[dynui] Generalise mechanisms for looking up user interface items
authorMichael Brown <mcb30@ipxe.org>
Thu, 20 Jun 2024 21:16:18 +0000 (14:16 -0700)
committerMichael Brown <mcb30@ipxe.org>
Thu, 20 Jun 2024 21:51:28 +0000 (14:51 -0700)
Generalise the ability to look up a dynamic user interface item by
index or by shortcut key, to allow for reuse of this code for
interactive forms.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/dynui.c
src/hci/tui/menu_ui.c
src/include/ipxe/dynui.h

index 6e053b905b3c0ae3c47f25fe19ad8fca433943f9..33218f5982f42d530ed1ab261812a260ff3b0153 100644 (file)
@@ -131,6 +131,7 @@ struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
        }
        strcpy ( text_copy, text );
        item->text = text_copy;
+       item->index = dynui->count++;
        item->shortcut = shortcut;
        item->is_default = is_default;
 
@@ -180,3 +181,40 @@ struct dynamic_ui * find_dynui ( const char *name ) {
 
        return NULL;
 }
+
+/**
+ * Find dynamic user interface item by index
+ *
+ * @v dynui            Dynamic user interface
+ * @v index            Index
+ * @ret item           User interface item, or NULL if not found
+ */
+struct dynamic_item * dynui_item ( struct dynamic_ui *dynui,
+                                  unsigned int index ) {
+       struct dynamic_item *item;
+
+       list_for_each_entry ( item, &dynui->items, list ) {
+               if ( index-- == 0 )
+                       return item;
+       }
+
+       return NULL;
+}
+
+/**
+ * Find dynamic user interface item by shortcut key
+ *
+ * @v dynui            Dynamic user interface
+ * @v key              Shortcut key
+ * @ret item           User interface item, or NULL if not found
+ */
+struct dynamic_item * dynui_shortcut ( struct dynamic_ui *dynui, int key ) {
+       struct dynamic_item *item;
+
+       list_for_each_entry ( item, &dynui->items, list ) {
+               if ( key && ( key == item->shortcut ) )
+                       return item;
+       }
+
+       return NULL;
+}
index ab4e602a7288a434202ea75dd61b1bae9619ebbe..00cdab8df6fd228f5b96f04ddf3ec382bd04475a 100644 (file)
@@ -57,25 +57,6 @@ struct menu_ui {
        unsigned long timeout;
 };
 
-/**
- * Return a numbered menu item
- *
- * @v dynui            Dynamic user interface
- * @v index            Index
- * @ret item           Menu item, or NULL
- */
-static struct dynamic_item * menu_item ( struct dynamic_ui *dynui,
-                                        unsigned int index ) {
-       struct dynamic_item *item;
-
-       list_for_each_entry ( item, &dynui->items, list ) {
-               if ( index-- == 0 )
-                       return item;
-       }
-
-       return NULL;
-}
-
 /**
  * Draw a numbered menu item
  *
@@ -96,7 +77,7 @@ static void draw_menu_item ( struct menu_ui *ui, unsigned int index ) {
        move ( ( MENU_ROW + row_offset ), MENU_COL );
 
        /* Get menu item */
-       item = menu_item ( ui->dynui, index );
+       item = dynui_item ( ui->dynui, index );
        if ( item ) {
 
                /* Draw separators in a different colour */
@@ -178,7 +159,6 @@ static int menu_loop ( struct menu_ui *ui, struct dynamic_item **selected ) {
        unsigned int previous;
        unsigned int move;
        int key;
-       int i;
        int chosen = 0;
        int rc = 0;
 
@@ -217,15 +197,9 @@ static int menu_loop ( struct menu_ui *ui, struct dynamic_item **selected ) {
                                chosen = 1;
                                break;
                        default:
-                               i = 0;
-                               list_for_each_entry ( item, &ui->dynui->items,
-                                                     list ) {
-                                       if ( ! ( item->shortcut &&
-                                                ( item->shortcut == key ) ) ) {
-                                               i++;
-                                               continue;
-                                       }
-                                       ui->scroll.current = i;
+                               item = dynui_shortcut ( ui->dynui, key );
+                               if ( item ) {
+                                       ui->scroll.current = item->index;
                                        if ( item->name ) {
                                                chosen = 1;
                                        } else {
@@ -239,7 +213,7 @@ static int menu_loop ( struct menu_ui *ui, struct dynamic_item **selected ) {
                /* Move selection, if applicable */
                while ( move ) {
                        move = jump_scroll_move ( &ui->scroll, move );
-                       item = menu_item ( ui->dynui, ui->scroll.current );
+                       item = dynui_item ( ui->dynui, ui->scroll.current );
                        if ( item->name )
                                break;
                }
@@ -253,7 +227,7 @@ static int menu_loop ( struct menu_ui *ui, struct dynamic_item **selected ) {
                }
 
                /* Record selection */
-               item = menu_item ( ui->dynui, ui->scroll.current );
+               item = dynui_item ( ui->dynui, ui->scroll.current );
                assert ( item != NULL );
                assert ( item->name != NULL );
                *selected = item;
index 25124a3af3c68c1c7f11ff3e6fd7f87ddc6fe2ee..f38d448250d87bae449a2629f62d79564b035d4e 100644 (file)
@@ -21,6 +21,8 @@ struct dynamic_ui {
        const char *title;
        /** Dynamic user interface items */
        struct list_head items;
+       /** Number of user interface items */
+       unsigned int count;
 };
 
 /** A dynamic user interface item */
@@ -31,6 +33,8 @@ struct dynamic_item {
        const char *name;
        /** Text */
        const char *text;
+       /** Index */
+       unsigned int index;
        /** Shortcut key */
        int shortcut;
        /** Is default item */
@@ -44,6 +48,10 @@ extern struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
                                              int is_default );
 extern void destroy_dynui ( struct dynamic_ui *dynui );
 extern struct dynamic_ui * find_dynui ( const char *name );
+extern struct dynamic_item * dynui_item ( struct dynamic_ui *dynui,
+                                         unsigned int index );
+extern struct dynamic_item * dynui_shortcut ( struct dynamic_ui *dynui,
+                                             int key );
 extern int show_menu ( struct dynamic_ui *dynui, unsigned long timeout,
                       const char *select, struct dynamic_item **selected );