diff mbox

[23/31] petitboot: Update item text on edit

Message ID 20090708001136.497804114@am.sony.com
State New
Headers show

Commit Message

Geoff Levand July 8, 2009, 12:11 a.m. UTC
Update the boot option menu item text on edit.

Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
---
 ui/ncurses/nc-cui.c  |   59 ++++++++++++++++++++++++++++---------
 ui/ncurses/nc-menu.c |   80 ++++++++++++++++++++++++++++++++++++++++++---------
 ui/ncurses/nc-menu.h |    1 
 3 files changed, 112 insertions(+), 28 deletions(-)
diff mbox

Patch

--- a/ui/ncurses/nc-cui.c
+++ b/ui/ncurses/nc-cui.c
@@ -60,6 +60,35 @@  void cui_resize(struct cui *cui)
 }
 
 /**
+ * cui_make_item_name - Format the menu item name srting.
+ *
+ * Returns a talloc string.
+ */
+
+static char *cui_make_item_name(struct pmenu_item *i, struct cui_opt_data *cod)
+{
+	char *name;
+
+	assert(cod->name);
+	assert(cod->kd);
+
+	name = talloc_asprintf(i, "%s:", cod->name);
+
+	if (cod->kd->image)
+		name = talloc_asprintf_append(name, " %s", cod->kd->image);
+
+	if (cod->kd->initrd)
+		name = talloc_asprintf_append(name, " initrd=%s",
+			cod->kd->initrd);
+
+	if (cod->kd->args)
+		name = talloc_asprintf_append(name, " %s", cod->kd->args);
+
+	DBGS("@%s@\n", name);
+	return name;
+}
+
+/**
  * cui_on_exit - A generic main menu ESC callback.
  */
 
@@ -143,6 +172,7 @@  static void cui_ked_on_exit(struct ked *
 	if (ked_result == ked_update) {
 		struct pmenu_item *i = pmenu_find_selected(cui->main);
 		struct cui_opt_data *cod = cod_from_item(i);
+		char *name;
 
 		assert(kd);
 
@@ -150,6 +180,12 @@  static void cui_ked_on_exit(struct ked *
 		talloc_free(cod->kd);
 		cod->kd = kd;
 
+		name = cui_make_item_name(i, cod);
+		pmenu_item_replace(i, name);
+
+		/* FIXME: need to make item visible somehow */
+		set_current_item(cui->main->ncm, i->nci);
+
 		pb_log("%s: updating opt '%s'\n", __func__, cod->name);
 		pb_log(" image  '%s'\n", cod->kd->image);
 		pb_log(" initrd '%s'\n", cod->kd->initrd);
@@ -164,10 +200,10 @@  static void cui_ked_on_exit(struct ked *
 int cui_ked_run(struct pmenu_item *item)
 {
 	struct cui *cui = cui_from_item(item);
+	struct cui_opt_data *cod = cod_from_item(item);
 	struct ked *ked;
 
-	ked = ked_init(cui, cod_from_item(item)->kd, cui_ked_on_exit);
-
+	ked = ked_init(cui, cod->kd, cui_ked_on_exit);
 	cui_set_current(cui, &ked->scr);
 
 	return 0;
@@ -270,7 +306,6 @@  void cui_on_open(struct pmenu *menu)
 	unsigned int insert_pt;
 	struct pmenu_item *i;
 	struct cui_opt_data *cod;
-	char *name;
 
 	menu->scr.unpost(&menu->scr);
 
@@ -283,23 +318,21 @@  void cui_on_open(struct pmenu *menu)
 	insert_pt = pmenu_grow(menu, 1);
 	i = pmenu_item_alloc(menu);
 
-	name = talloc_asprintf(i, "User item %u:", insert_pt);
-	pmenu_item_setup(menu, i, insert_pt, name);
-
 	i->on_edit = cui_ked_run;
 	i->on_execute = cui_run_kexec;
 	i->data = cod = talloc_zero(i, struct cui_opt_data);
 
+	cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
 	cod->kd = talloc_zero(i, struct pb_kexec_data);
-	cod->name = name;
+
+	pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
 
 	/* Re-attach the items array. */
 
 	set_menu_items(menu->ncm, menu->items);
 
-	set_current_item(menu->ncm, i->nci);
 	menu->scr.post(&menu->scr);
-	pos_menu_cursor(menu->ncm);
+	set_current_item(menu->ncm, i->nci);
 
 	i->on_edit(i);
 }
@@ -351,11 +384,6 @@  static int cui_device_add(struct device 
 
 		opt->ui_info = i = pmenu_item_alloc(cui->main);
 
-		name = talloc_asprintf(i, "%s: %s", opt->name,
-			opt->description);
-
-		pmenu_item_setup(cui->main, i, insert_pt, name);
-
 		i->on_edit = cui_ked_run;
 		i->on_execute = cui_run_kexec;
 		i->data = cod = talloc(i, struct cui_opt_data);
@@ -370,6 +398,9 @@  static int cui_device_add(struct device 
 		cod->kd->initrd = talloc_strdup(cod->kd, opt->initrd_file);
 		cod->kd->args = talloc_strdup(cod->kd, opt->boot_args);
 
+		name = cui_make_item_name(i, cod);
+		pmenu_item_setup(cui->main, i, insert_pt, name);
+
 		insert_pt++;
 
 		pb_log("%s: adding opt '%s'\n", __func__, cod->name);
--- a/ui/ncurses/nc-menu.c
+++ b/ui/ncurses/nc-menu.c
@@ -94,6 +94,7 @@  struct pmenu_item *pmenu_item_setup(stru
 	unsigned int index, const char *name)
 {
 	assert(i);
+	assert(name);
 
 	if (!i)
 		return NULL;
@@ -114,6 +115,67 @@  struct pmenu_item *pmenu_item_setup(stru
 	return i;
 }
 
+static int pmenu_item_get_index(const struct pmenu_item *item)
+{
+	unsigned int i;
+
+	for (i = 0; i < item->pmenu->item_count; i++)
+		if (item->pmenu->items[i] == item->nci)
+			return i;
+
+	pb_log("%s: not found: %p %s\n", __func__, item,
+		(item ? item->nci->name.str : "(null)"));
+	return -1;
+}
+
+/**
+ * pmenu_item_replace - Replace the menu item with a new one.
+ *
+ * Use this routine to change a menu item's text.
+ */
+
+int pmenu_item_replace(struct pmenu_item *i, const char *name)
+{
+	struct pmenu *menu;
+	ITEM *nci;
+	int index;
+
+	assert(name);
+	assert(i->nci);
+
+	menu = i->pmenu;
+	index = pmenu_item_get_index(i);
+
+	if (index < 0) {
+		assert(0 && "get_index failed");
+		return -1;
+	}
+
+	nci = new_item(name, NULL);
+
+	if (!nci) {
+		assert(0 && "new_item failed");
+		return -1;
+	}
+
+	set_item_userptr(nci, i);
+
+	menu->scr.unpost(&menu->scr);
+	set_menu_items(menu->ncm, NULL);
+
+	// FIXME: need to assure item name is a talloc string.
+	/* talloc_free((char *)item_name(i->nci)); */
+
+	free_item(i->nci);
+	menu->items[index] = nci;
+	i->nci = nci;
+
+	set_menu_items(menu->ncm, menu->items);
+	menu->scr.post(&menu->scr);
+
+	return 0;
+}
+
 /**
  * pmenu_move_cursor - Move the cursor.
  * @req: An ncurses request or char to send to menu_driver().
@@ -230,19 +292,6 @@  unsigned int pmenu_grow(struct pmenu *me
 	return tmp;
 }
 
-static int pmenu_item_get_index(const struct pmenu_item *item)
-{
-	unsigned int i;
-
-	for (i = 0; i < item->pmenu->item_count; i++)
-		if (item->pmenu->items[i] == item->nci)
-			return i;
-
-	pb_log("%s: not found: %p %s\n", __func__, item,
-		(item ? item->nci->name.str : "(null)"));
-	return -1;
-}
-
 /**
  * pmenu_remove - Remove an item from the item array.
  *
@@ -264,6 +313,9 @@  int pmenu_remove(struct pmenu *menu, str
 	if (index < 0)
 		return -1;
 
+	free_item(item->nci);
+	talloc_free(item);
+
 	/* Note that items array has a null terminator. */
 
 	menu->insert_pt--;
@@ -351,7 +403,7 @@  void pmenu_delete(struct pmenu *menu)
 	menu->scr.sig = pb_removed_sig;
 
 	for (i = item_count(menu->ncm); i; i--)
-		free_item(menu->items[i]);
+		free_item(menu->items[i - 1]);
 
 	free_menu(menu->ncm);
 	delwin(menu->scr.sub_ncw);
--- a/ui/ncurses/nc-menu.h
+++ b/ui/ncurses/nc-menu.h
@@ -46,6 +46,7 @@  struct pmenu_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);
+int pmenu_item_replace(struct pmenu_item *i, const char *name);
 void pmenu_item_delete(struct pmenu_item *item);
 
 static inline struct pmenu_item *pmenu_item_from_arg(void *arg)