diff mbox

[20/31] petitboot: CUI add empty item

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

Commit Message

Geoff Levand July 8, 2009, 12:11 a.m. UTC
Add a CUI hot-key 'o' that opens a new empty menu item.

Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
---
 ui/ncurses/nc-cui.c  |   52 +++++++++++++++++++++++++++++++++++++++++++++++----
 ui/ncurses/nc-cui.h  |    6 ++++-
 ui/ncurses/nc-ked.c  |    5 ++--
 ui/ncurses/nc-menu.c |    7 ++++--
 ui/ncurses/nc-menu.h |    1 
 ui/ncurses/pb-cui.c  |    6 +++--
 ui/ncurses/ps3-cui.c |    9 +++++---
 7 files changed, 72 insertions(+), 14 deletions(-)
diff mbox

Patch

--- a/ui/ncurses/nc-cui.c
+++ b/ui/ncurses/nc-cui.c
@@ -108,8 +108,8 @@  static int cui_run_kexec(struct pmenu_it
 	assert(cui->current == &cui->main->scr);
 	assert(cui->on_kexec);
 
-	pb_log("%s: %s\n", __func__, cod->dev->name, cod->opt->name);
-	nc_scr_status_printf(cui->current, "Booting %s...", cod->opt->name);
+	pb_log("%s: %s\n", __func__, cod->name);
+	nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
 
 	def_prog_mode();
 
@@ -150,7 +150,7 @@  static void cui_ked_on_exit(struct ked *
 		talloc_free(cod->kd);
 		cod->kd = kd;
 
-		pb_log("%s: updating opt '%s'\n", __func__, cod->opt->name);
+		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);
 		pb_log(" args   '%s'\n", cod->kd->args);
@@ -267,6 +267,49 @@  static void cui_handle_resize(struct cui
 }
 
 /**
+ * cui_on_open - Open new item callback.
+ */
+
+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);
+
+	/* This disconnects items array from menu. */
+
+	set_menu_items(menu->ncm, NULL);
+
+	/* Insert new items at insert_pt. */
+
+	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, NULL);
+
+	i->on_edit = cui_ked_run;
+	i->on_execute = cui_run_kexec;
+	i->data = cod = talloc_zero(i, struct cui_opt_data);
+
+	cod->kd = talloc_zero(i, struct pb_kexec_data);
+	cod->name = 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);
+
+	i->on_edit(i);
+}
+
+/**
  * cui_device_add - Client device_add callback.
  *
  * Creates menu_items for all the device boot_options and inserts those
@@ -332,6 +375,7 @@  static int cui_device_add(struct device 
 		cod->dev = dev;
 		cod->opt = opt;
 		cod->opt_hash = pb_opt_hash(dev, opt);
+		cod->name = opt->name;
 		cod->kd = talloc(i, struct pb_kexec_data);
 
 		cod->kd->image = talloc_strdup(cod->kd, opt->boot_image_file);
@@ -340,7 +384,7 @@  static int cui_device_add(struct device 
 
 		insert_pt++;
 
-		pb_log("%s: adding opt '%s'\n", __func__, cod->opt->name);
+		pb_log("%s: adding opt '%s'\n", __func__, cod->name);
 		pb_log("   image  '%s'\n", cod->kd->image);
 		pb_log("   initrd '%s'\n", cod->kd->initrd);
 		pb_log("   args   '%s'\n", cod->kd->args);
--- a/ui/ncurses/nc-cui.h
+++ b/ui/ncurses/nc-cui.h
@@ -26,10 +26,13 @@ 
 #include "nc-ked.h"
 
 struct cui_opt_data {
+	const char *name;
+	struct pb_kexec_data *kd;
+
+	/* optional data */
 	const struct device *dev;
 	const struct boot_option *opt;
 	uint32_t opt_hash;
-	struct pb_kexec_data *kd;
 };
 
 /**
@@ -66,6 +69,7 @@  int cui_ked_run(struct pmenu_item *item)
 void cui_abort(struct cui *cui);
 void cui_resize(struct cui *cui);
 void cui_on_exit(struct pmenu *menu);
+void cui_on_open(struct pmenu *menu);
 int cui_run_cmd(struct pmenu_item *item);
 
 static inline struct cui *cui_from_arg(void *arg)
--- a/ui/ncurses/nc-ked.c
+++ b/ui/ncurses/nc-ked.c
@@ -149,9 +149,10 @@  static char *ked_chomp(char *s)
 	for (; s < s_end; s++)
 		if (*s != ' ' && *s != '\t')
 			break;
-	start = s;
 
-	for (++s; s < s_end; s++)
+	start = end = s;
+
+	for (; s < s_end; s++)
 		if (*s != ' ' && *s != '\t')
 			end = s;
 	*(end + 1) = 0;
--- a/ui/ncurses/nc-menu.c
+++ b/ui/ncurses/nc-menu.c
@@ -174,13 +174,16 @@  static void pmenu_process_key(struct nc_
 		case '\t':
 			pmenu_move_cursor(menu, REQ_DOWN_ITEM);
 			break;
-
 		case KEY_LEFT:
-		case 'E':
 		case 'e':
 			if (item->on_edit)
 				item->on_edit(item);
 			break;
+		case 'o':
+			DBGS("on_open: %p\n", menu->on_open);
+			if (menu->on_open)
+				menu->on_open(menu);
+			break;
 		case '\n':
 		case '\r':
 			if (item->on_execute)
--- a/ui/ncurses/nc-menu.h
+++ b/ui/ncurses/nc-menu.h
@@ -77,6 +77,7 @@  struct pmenu {
 	unsigned int insert_pt;
 	int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c);
 	void (*on_exit)(struct pmenu *menu);
+	void (*on_open)(struct pmenu *menu);
 };
 
 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
--- a/ui/ncurses/pb-cui.c
+++ b/ui/ncurses/pb-cui.c
@@ -138,7 +138,7 @@  static int pb_kexec_cb(struct cui *cui, 
 {
 	struct pb_cui *pb = pb_from_cui(cui);
 
-	pb_log("%s: %s:%s\n", __func__, cod->dev->name, cod->opt->name);
+	pb_log("%s: %s\n", __func__, cod->name);
 
 	assert(pb->cui->current == &pb->cui->main->scr);
 
@@ -162,9 +162,11 @@  static struct pmenu *pb_mm_init(struct p
 		return NULL;
 	}
 
+	m->on_open = cui_on_open;
+
 	m->scr.frame.title = talloc_strdup(m, "Petitboot");
 	m->scr.frame.help = talloc_strdup(m,
-		"ESC=exit, Enter=accept, E,e=edit");
+		"ESC=exit, Enter=accept, e=edit, o=open");
 	m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
 
 	i = pmenu_item_init(m, 0, "Exit to Shell",
--- a/ui/ncurses/ps3-cui.c
+++ b/ui/ncurses/ps3-cui.c
@@ -199,11 +199,12 @@  static int ps3_kexec_cb(struct cui *cui,
 {
 	struct ps3_cui *ps3 = ps3_from_cui(cui);
 
-	pb_log("%s: %s:%s\n", __func__, cod->dev->name, cod->opt->name);
+	pb_log("%s: %s\n", __func__, cod->name);
 
 	assert(ps3->cui->current == &ps3->cui->main->scr);
 
-	if (cui->default_item != cod->opt_hash || ps3->dirty_values) {
+	if ((cod->opt_hash && cod->opt_hash != cui->default_item)
+		|| ps3->dirty_values) {
 		ps3->values.default_item = cod->opt_hash;
 		ps3_flash_set_values(&ps3->values);
 	}
@@ -326,6 +327,8 @@  static struct pmenu *ps3_mm_init(struct 
 	}
 
 	m->hot_key = ps3_hot_key;
+	m->on_open = cui_on_open;
+
 #if defined(DEBUG)
 	m->scr.frame.title = talloc_strdup(m,
 		"Petitboot PS3 (ver " PACKAGE_VERSION ")");
@@ -333,7 +336,7 @@  static struct pmenu *ps3_mm_init(struct 
 	m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
 #endif
 	m->scr.frame.help = talloc_strdup(m,
-		"ESC=exit, Enter=accept, E,e=edit");
+		"ESC=exit, Enter=accept, e=edit, o=open");
 	m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
 
 	i = pmenu_item_init(m, 0, "Boot GameOS",