summaryrefslogtreecommitdiffstats
path: root/ui/ncurses
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2014-03-12 14:59:03 +0800
committerJeremy Kerr <jk@ozlabs.org>2014-04-10 11:18:24 +0800
commit1bbd230d618162ccd5ea97540a413c766a07c0cb (patch)
tree8bf596660165d015328d54930ba2f91ed4c4effd /ui/ncurses
parente07bd59ad5f4ba9cd113f8dfdcc72e6ca2e69819 (diff)
downloadtalos-petitboot-1bbd230d618162ccd5ea97540a413c766a07c0cb.tar.gz
talos-petitboot-1bbd230d618162ccd5ea97540a413c766a07c0cb.zip
ui/ncurses: Separate menu item creation & initialisation from insertion
Currently, the menu item creation is has two main functions: pmenu_item_alloc and pmenu_item_setup. The latter does initialisation (it sets item->name), and inserts the item into the menu. We have pmenu_item_init to combine this into one, but that means we need to do further initialisation (eg, to set on_execute) after the item has been added to the menu. Instead, this change use a more direct _create and _insert interface. Create does the allocation and initialisation, while _insert does the actual insertion. This means new_item failures will be detected at creation time, rather than during pmenu_insert. Also, we're now insert a completely-populated item into the menu, rather than populating on_edit, on_execute and data after insertion. Because we can detect errors from creation (ie, from new_item failing), we add handling code to cui_boot_option_add and cui_boot_editor_on_exit. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'ui/ncurses')
-rw-r--r--ui/ncurses/generic-main.c19
-rw-r--r--ui/ncurses/nc-cui.c55
-rw-r--r--ui/ncurses/nc-menu.c44
-rw-r--r--ui/ncurses/nc-menu.h19
4 files changed, 67 insertions, 70 deletions
diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c
index 67b0b61..0d0a38a 100644
--- a/ui/ncurses/generic-main.c
+++ b/ui/ncurses/generic-main.c
@@ -201,16 +201,25 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
"Enter=accept, e=edit, n=new, x=exit, h=help");
m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
- i = pmenu_item_init(m, 0, " ");
+ i = pmenu_item_create(m, " ");
item_opts_off(i->nci, O_SELECTABLE);
- i = pmenu_item_init(m, 1, "System information");
+ pmenu_item_insert(m, i, 0);
+
+ i = pmenu_item_create(m, "System information");
i->on_execute = pmenu_sysinfo;
- i = pmenu_item_init(m, 2, "System configuration");
+ pmenu_item_insert(m, i, 1);
+
+ i = pmenu_item_create(m, "System configuration");
i->on_execute = pmenu_config;
- i = pmenu_item_init(m, 3, "Rescan devices");
+ pmenu_item_insert(m, i, 2);
+
+ i = pmenu_item_create(m, "Rescan devices");
i->on_execute = pmenu_reinit;
- i = pmenu_item_init(m, 4, "Exit to shell");
+ pmenu_item_insert(m, i, 3);
+
+ i = pmenu_item_create(m, "Exit to shell");
i->on_execute = pmenu_exit_cb;
+ pmenu_item_insert(m, i, 4);
result = pmenu_setup(m);
diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c
index e8aaf9a..3a89869 100644
--- a/ui/ncurses/nc-cui.c
+++ b/ui/ncurses/nc-cui.c
@@ -175,22 +175,27 @@ static void cui_boot_editor_on_exit(struct cui *cui,
if (!item) {
int insert_pt;
+ cod = talloc_zero(NULL, struct cui_opt_data);
+ cod->name = talloc_asprintf(cod, "User item %u", ++user_idx);
+
+ item = pmenu_item_create(menu, cod->name);
+ if (!item) {
+ talloc_free(cod);
+ goto out;
+ }
+
+ item->on_edit = cui_item_edit;
+ item->on_execute = cui_boot;
+ item->data = cod;
+
+ talloc_steal(item, cod);
+
/* Detach the items array. */
set_menu_items(menu->ncm, NULL);
/* Insert new item at insert_pt. */
insert_pt = pmenu_grow(menu, 1);
- item = pmenu_item_alloc(menu);
- item->on_edit = cui_item_edit;
- item->on_execute = cui_boot;
- item->data = cod = talloc_zero(item, struct cui_opt_data);
-
- cod->name = talloc_asprintf(cod, "User item %u:", ++user_idx);
- if (pmenu_item_setup(menu, item, insert_pt,
- talloc_strdup(item, cod->name)) == NULL) {
- talloc_free(item);
- item = NULL;
- }
+ pmenu_item_insert(menu, item, insert_pt);
/* Re-attach the items array. */
set_menu_items(menu->ncm, menu->items);
@@ -201,8 +206,8 @@ static void cui_boot_editor_on_exit(struct cui *cui,
cod->bd = talloc_steal(cod, bd);
- if (item)
- set_current_item(item->pmenu->ncm, item->nci);
+ set_current_item(item->pmenu->ncm, item->nci);
+out:
cui_set_current(cui, &cui->main->scr);
talloc_free(cui->boot_editor);
cui->boot_editor = NULL;
@@ -407,19 +412,11 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
if (cui->current == &cui->main->scr)
nc_scr_unpost(cui->current);
- /* This disconnects items array from menu. */
-
- result = set_menu_items(cui->main->ncm, NULL);
-
- if (result)
- pb_log("%s: set_menu_items failed: %d\n", __func__, result);
-
- /* Insert new items at insert_pt. */
- insert_pt = pmenu_grow(cui->main, 1);
-
/* Save the item in opt->ui_info for cui_device_remove() */
- opt->ui_info = i = pmenu_item_alloc(cui->main);
+ opt->ui_info = i = pmenu_item_create(cui->main, opt->name);
+ if (!i)
+ return -1;
i->on_edit = cui_item_edit;
i->on_execute = cui_boot;
@@ -436,7 +433,15 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
- pmenu_item_setup(cui->main, i, insert_pt, cod->name);
+ /* This disconnects items array from menu. */
+ result = set_menu_items(cui->main->ncm, NULL);
+
+ if (result)
+ pb_log("%s: set_menu_items failed: %d\n", __func__, result);
+
+ /* Insert new items at insert_pt. */
+ insert_pt = pmenu_grow(cui->main, 1);
+ pmenu_item_insert(cui->main, i, insert_pt);
pb_log("%s: adding opt '%s'\n", __func__, cod->name);
pb_log(" image '%s'\n", cod->bd->image);
diff --git a/ui/ncurses/nc-menu.c b/ui/ncurses/nc-menu.c
index ef00ff2..5691f42 100644
--- a/ui/ncurses/nc-menu.c
+++ b/ui/ncurses/nc-menu.c
@@ -77,43 +77,39 @@ static void pmenu_resize(struct nc_scr *scr)
}
/**
- * pmenu_item_init - Allocate and initialize a new pmenu_item instance.
+ * pmenu_item_create - Allocate and initialize a new pmenu_item instance.
*
* Returns a pointer the the initialized struct pmenu_item instance or NULL
* on error. The caller is responsible for calling talloc_free() for the
* returned instance.
*/
-
-struct pmenu_item *pmenu_item_alloc(struct pmenu *menu)
+struct pmenu_item *pmenu_item_create(struct pmenu *menu, const char *name)
{
- /* Items go with the menu, not the pointer array. */
-
- struct pmenu_item *i = talloc_zero(menu, struct pmenu_item);
+ struct pmenu_item *item = talloc_zero(menu, struct pmenu_item);
- return i;
-}
-
-struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
- unsigned int index, const char *name)
-{
- assert(i);
- assert(name);
+ item->i_sig = pb_item_sig;
+ item->pmenu = menu;
+ item->nci = new_item(name, NULL);
- if (!i)
+ if (!item->nci) {
+ talloc_free(item);
return NULL;
+ }
- i->i_sig = pb_item_sig;
- i->pmenu = menu;
- i->nci = new_item(name, NULL);
-
- if (!i->nci)
- return NULL;
+ set_item_userptr(item->nci, item);
- set_item_userptr(i->nci, i);
+ return item;
+}
- menu->items[index] = i->nci;
+void pmenu_item_insert(struct pmenu *menu, struct pmenu_item *item,
+ unsigned int index)
+{
+ assert(item);
+ assert(index < menu->item_count);
+ assert(menu->items[index] == NULL);
+ assert(menu_items(menu->ncm) == NULL);
- return i;
+ menu->items[index] = item->nci;
}
static int pmenu_item_get_index(const struct pmenu_item *item)
diff --git a/ui/ncurses/nc-menu.h b/ui/ncurses/nc-menu.h
index 63da2e5..8e9c56b 100644
--- a/ui/ncurses/nc-menu.h
+++ b/ui/ncurses/nc-menu.h
@@ -53,9 +53,9 @@ struct pmenu_item {
int (*on_execute)(struct pmenu_item *item);
};
-struct pmenu_item *pmenu_item_alloc(struct pmenu *menu);
-struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
- unsigned int index, const char *name);
+struct pmenu_item *pmenu_item_create(struct pmenu *menu, const char *name);
+void pmenu_item_insert(struct pmenu *menu, struct pmenu_item *item,
+ unsigned int index);
void pmenu_item_delete(struct pmenu_item *item);
static inline struct pmenu_item *pmenu_item_from_arg(void *arg)
@@ -71,19 +71,6 @@ static inline struct cui_opt_data *cod_from_item(struct pmenu_item *item)
return item->data;
}
-static inline struct pmenu_item *pmenu_item_init(struct pmenu *menu,
- unsigned int index, const char *name)
-{
- struct pmenu_item *item = pmenu_item_alloc(menu);
-
- if (pmenu_item_setup(menu, item, index, name)) {
- talloc_free(item);
- item = NULL;
- }
-
- return item;
-}
-
/**
* struct pmenu - Data structure defining complete menu.
* @insert_pt: Index in nc item array.
OpenPOWER on IntegriCloud