From patchwork Wed Jul 8 00:11:57 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 29567 Return-Path: X-Original-To: patchwork-incoming@bilbo.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (ozlabs.org [203.10.76.45]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx.ozlabs.org", Issuer "CA Cert Signing Authority" (verified OK)) by bilbo.ozlabs.org (Postfix) with ESMTPS id 45A42B708E for ; Wed, 8 Jul 2009 10:24:06 +1000 (EST) Received: by ozlabs.org (Postfix) id 2DCEADEF4C; Wed, 8 Jul 2009 10:21:09 +1000 (EST) Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 2A24BDEF4B for ; Wed, 8 Jul 2009 10:21:09 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4B931DDEF4; Wed, 8 Jul 2009 10:18:26 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX18Xz8L67IQCEPF30h+BKmX+Bo0UaOdtnMs@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n680IKYa026691 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 8 Jul 2009 00:18:20 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n680IJG2026690; Wed, 8 Jul 2009 00:18:19 GMT Message-Id: <20090708001136.497804114@am.sony.com> User-Agent: quilt/0.47-1 Date: Tue, 07 Jul 2009 17:11:57 -0700 From: Geoff Levand To: Jeremy Kerr In-Reply-To: <20090708001134.934244536@am.sony.com> References: <20090708001134.934244536@am.sony.com> Content-Disposition: inline; filename=nc-menu-update-items.diff X-Virus-Scanned: ClamAV 0.93.3/9541/Tue Jul 7 17:31:53 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Wed, 08 Jul 2009 00:18:20 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 23/31] petitboot: Update item text on edit X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Update the boot option menu item text on edit. Signed-off-by: Geoff Levand --- 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(-) --- 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)