diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2014-07-24 15:13:24 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2014-07-28 13:50:35 +0800 |
commit | e596e8e66b014161116c207daa8d42cff849bfd2 (patch) | |
tree | 5c32aa7165907d85f1d015c058806d5375d6f28c | |
parent | 0bacabfe38d4e6721d4b32c11c6ef5bff599492c (diff) | |
download | talos-petitboot-e596e8e66b014161116c207daa8d42cff849bfd2.tar.gz talos-petitboot-e596e8e66b014161116c207daa8d42cff849bfd2.zip |
ui/ncurses: Move menu initialisation to cui code
We'll want to re-initialise the petitboot main menu (when the language
is changed), which we can't do if the items are populated entirely from
the main() function.
This change moves the menu initilisation to the cui code, we we can
re-init when necessary.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r-- | po/POTFILES.in | 2 | ||||
-rw-r--r-- | po/en.po | 38 | ||||
-rw-r--r-- | ui/ncurses/Makefile.am | 3 | ||||
-rw-r--r-- | ui/ncurses/generic-main.c | 121 | ||||
-rw-r--r-- | ui/ncurses/nc-cui-help.c (renamed from ui/ncurses/generic-main-help.c) | 0 | ||||
-rw-r--r-- | ui/ncurses/nc-cui.c | 98 | ||||
-rw-r--r-- | ui/ncurses/nc-cui.h | 2 | ||||
-rw-r--r-- | ui/ncurses/nc-menu.c | 34 | ||||
-rw-r--r-- | ui/ncurses/nc-menu.h | 1 |
9 files changed, 144 insertions, 155 deletions
diff --git a/po/POTFILES.in b/po/POTFILES.in index ba9f977..7754f8b 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -7,7 +7,7 @@ ui/ncurses/nc-helpscreen.c ui/ncurses/nc-menu.c ui/ncurses/nc-sysinfo.c ui/ncurses/nc-textscreen.c -ui/ncurses/generic-main-help.c +ui/ncurses/nc-cui-help.c ui/ncurses/nc-boot-editor-help.c ui/ncurses/nc-config-help.c ui/ncurses/nc-sysinfo-help.c @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: petitboot 20140623-g89bd2ed2-dirty\n" "Report-Msgid-Bugs-To: Geoff Levand <geoff@infradead.org>\n" -"POT-Creation-Date: 2014-07-28 13:45+0800\n" +"POT-Creation-Date: 2014-07-28 13:48+0800\n" "PO-Revision-Date: 2014-06-24 13:56+0800\n" "Last-Translator: Jeremy Kerr <jk@ozlabs.org>\n" "Language-Team: English\n" @@ -166,6 +166,24 @@ msgstr "SAFE MODE: select '%s' to continue" msgid "Rescan devices" msgstr "Rescan devices" +msgid "Enter=accept, e=edit, n=new, x=exit, h=help" +msgstr "Enter=accept, e=edit, n=new, x=exit, h=help" + +msgid "Welcome to Petitboot" +msgstr "Welcome to Petitboot" + +msgid "System information" +msgstr "System information" + +msgid "System configuration" +msgstr "System configuration" + +msgid "Exit to shell" +msgstr "Exit to shell" + +msgid "main menu" +msgstr "main menu" + #, c-format msgid "%s: alloc cui failed.\n" msgstr "%s: alloc cui failed.\n" @@ -390,21 +408,3 @@ msgstr "" msgid "Usage" msgstr "Usage" - -msgid "Enter=accept, e=edit, n=new, x=exit, h=help" -msgstr "Enter=accept, e=edit, n=new, x=exit, h=help" - -msgid "Welcome to Petitboot" -msgstr "Welcome to Petitboot" - -msgid "System information" -msgstr "System information" - -msgid "System configuration" -msgstr "System configuration" - -msgid "Exit to shell" -msgstr "Exit to shell" - -msgid "main menu" -msgstr "main menu" diff --git a/ui/ncurses/Makefile.am b/ui/ncurses/Makefile.am index be4c417..39cf597 100644 --- a/ui/ncurses/Makefile.am +++ b/ui/ncurses/Makefile.am @@ -35,6 +35,7 @@ libpbnc_la_SOURCES = \ nc-config-help.c \ nc-cui.c \ nc-cui.h \ + nc-cui-help.c \ nc-boot-editor.c \ nc-boot-editor.h \ nc-boot-editor-help.c \ @@ -59,7 +60,7 @@ sbin_PROGRAMS += petitboot-ps3-nc endif -petitboot_nc_SOURCES = generic-main.c generic-main-help.c +petitboot_nc_SOURCES = generic-main.c petitboot_nc_LDADD = $(common_libs) petitboot_ps3_nc_SOURCES = ps3-main.c diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c index f253469..669053c 100644 --- a/ui/ncurses/generic-main.c +++ b/ui/ncurses/generic-main.c @@ -40,8 +40,6 @@ #include "ui/common/discover-client.h" #include "nc-cui.h" -extern const struct help_text main_menu_help_text; - static void print_version(void) { printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"); @@ -150,113 +148,23 @@ static char *default_log_filename(void) return name; } -/** + +struct cui *cui; + +/* * struct pb_cui - Main cui program instance. * @mm: Main menu. * @svm: Set video mode menu. */ -struct pb_cui { - struct pmenu *mm; - struct cui *cui; -}; - -static int pmenu_sysinfo(struct pmenu_item *item) -{ - cui_show_sysinfo(cui_from_item(item)); - return 0; -} - -static int pmenu_config(struct pmenu_item *item) -{ - cui_show_config(cui_from_item(item)); - return 0; -} - -static int pmenu_reinit(struct pmenu_item *item) -{ - cui_send_reinit(cui_from_item(item)); - return 0; -} - -/** - * pb_mm_init - Setup the main menu instance. - */ - -static struct pmenu *pb_mm_init(struct pb_cui *pb_cui) -{ - int result; - struct pmenu *m; - struct pmenu_item *i; - - m = pmenu_init(pb_cui->cui, 5, cui_on_exit); - - if (!m) { - pb_log("%s: failed\n", __func__); - return NULL; - } - - m->on_new = cui_item_new; - - m->scr.frame.ltitle = talloc_asprintf(m, - "Petitboot (" PACKAGE_VERSION ")"); - m->scr.frame.rtitle = NULL; - m->scr.frame.help = talloc_strdup(m, - _("Enter=accept, e=edit, n=new, x=exit, h=help")); - m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot")); - - i = pmenu_item_create(m, " "); - item_opts_off(i->nci, O_SELECTABLE); - pmenu_item_insert(m, i, 0); - - i = pmenu_item_create(m, _("System information")); - i->on_execute = pmenu_sysinfo; - pmenu_item_insert(m, i, 1); - - i = pmenu_item_create(m, _("System configuration")); - i->on_execute = pmenu_config; - pmenu_item_insert(m, i, 2); - - i = pmenu_item_create(m, _("Rescan devices")); - i->on_execute = pmenu_reinit; - 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); - - if (result) { - pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__, - strerror(errno)); - goto fail_setup; - } - - m->help_title = _("main menu"); - m->help_text = &main_menu_help_text; - - menu_opts_off(m->ncm, O_SHOWDESC); - set_menu_mark(m->ncm, " *"); - set_current_item(m->ncm, i->nci); - - return m; - -fail_setup: - talloc_free(m); - return NULL; -} - -static struct pb_cui pb; - static void sig_handler(int signum) { DBGS("%d\n", signum); switch (signum) { case SIGWINCH: - if (pb.cui) - cui_resize(pb.cui); + if (cui) + cui_resize(cui); break; default: assert(0 && "unknown sig"); @@ -264,8 +172,8 @@ static void sig_handler(int signum) case SIGINT: case SIGHUP: case SIGTERM: - if (pb.cui) - cui_abort(pb.cui); + if (cui) + cui_abort(cui); break; } } @@ -336,18 +244,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - pb.cui = cui_init(&pb, NULL, opts.start_daemon); - - if (!pb.cui) + cui = cui_init(NULL, NULL, opts.start_daemon); + if (!cui) return EXIT_FAILURE; - pb.mm = pb_mm_init(&pb); - - cui_result = cui_run(pb.cui, pb.mm, 0); - - pmenu_delete(pb.mm); + cui_result = cui_run(cui); - talloc_free(pb.cui); + talloc_free(cui); pb_log("--- end ---\n"); diff --git a/ui/ncurses/generic-main-help.c b/ui/ncurses/nc-cui-help.c index ac690c5..ac690c5 100644 --- a/ui/ncurses/generic-main-help.c +++ b/ui/ncurses/nc-cui-help.c diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 761edb3..3c14f21 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -40,6 +40,8 @@ #include "nc-sysinfo.h" #include "nc-helpscreen.h" +extern const struct help_text main_menu_help_text; + static void cui_start(void) { initscr(); /* Initialize ncurses. */ @@ -641,6 +643,92 @@ void cui_send_reinit(struct cui *cui) discover_client_send_reinit(cui->client); } +static int menu_sysinfo_execute(struct pmenu_item *item) +{ + cui_show_sysinfo(cui_from_item(item)); + return 0; +} + +static int menu_config_execute(struct pmenu_item *item) +{ + cui_show_config(cui_from_item(item)); + return 0; +} + +static int menu_reinit_execute(struct pmenu_item *item) +{ + cui_send_reinit(cui_from_item(item)); + return 0; +} + +/** + * pb_mm_init - Setup the main menu instance. + */ +static struct pmenu *main_menu_init(struct cui *cui) +{ + struct pmenu_item *i; + struct pmenu *m; + int result; + + m = pmenu_init(cui, 5, cui_on_exit); + if (!m) { + pb_log("%s: failed\n", __func__); + return NULL; + } + + m->on_new = cui_item_new; + + m->scr.frame.ltitle = talloc_asprintf(m, + "Petitboot (" PACKAGE_VERSION ")"); + m->scr.frame.rtitle = NULL; + m->scr.frame.help = talloc_strdup(m, + _("Enter=accept, e=edit, n=new, x=exit, h=help")); + m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot")); + + /* add a separator */ + i = pmenu_item_create(m, " "); + item_opts_off(i->nci, O_SELECTABLE); + pmenu_item_insert(m, i, 0); + + /* add system items */ + i = pmenu_item_create(m, _("System information")); + i->on_execute = menu_sysinfo_execute; + pmenu_item_insert(m, i, 1); + + i = pmenu_item_create(m, _("System configuration")); + i->on_execute = menu_config_execute; + pmenu_item_insert(m, i, 2); + + i = pmenu_item_create(m, _("Rescan devices")); + i->on_execute = menu_reinit_execute; + 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); + + if (result) { + pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__, + strerror(errno)); + goto fail_setup; + } + + m->help_title = _("main menu"); + m->help_text = &main_menu_help_text; + + menu_opts_off(m->ncm, O_SHOWDESC); + set_menu_mark(m->ncm, " *"); + set_current_item(m->ncm, i->nci); + + return m; + +fail_setup: + talloc_free(m); + return NULL; +} + static struct discover_client_ops cui_client_ops = { .device_add = NULL, .boot_option_add = cui_boot_option_add, @@ -667,7 +755,6 @@ struct cui *cui_init(void* platform_info, unsigned int i; cui = talloc_zero(NULL, struct cui); - if (!cui) { pb_log("%s: alloc cui failed.\n", __func__); fprintf(stderr, _("%s: alloc cui failed.\n"), __func__); @@ -723,6 +810,10 @@ retry_start: talloc_steal(cui, cui->client); cui_start(); + cui->main = main_menu_init(cui); + if (!cui->main) + goto fail_client_init; + waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN, cui_process_key, cui); @@ -753,13 +844,12 @@ fail_alloc: * Returns 0 on success (return to shell), -1 on error (should restart). */ -int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item) +int cui_run(struct cui *cui) { assert(main); - cui->main = main; cui->current = &cui->main->scr; - cui->default_item = default_item; + cui->default_item = 0; nc_scr_post(cui->current); diff --git a/ui/ncurses/nc-cui.h b/ui/ncurses/nc-cui.h index e9e4d38..417126f 100644 --- a/ui/ncurses/nc-cui.h +++ b/ui/ncurses/nc-cui.h @@ -71,7 +71,7 @@ struct cui *cui_init(void* platform_info, int (*js_map)(const struct js_event *e), int start_deamon); struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr); -int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item); +int cui_run(struct cui *cui); void cui_item_edit(struct pmenu_item *item); void cui_item_new(struct pmenu *menu); void cui_show_sysinfo(struct cui *cui); diff --git a/ui/ncurses/nc-menu.c b/ui/ncurses/nc-menu.c index 531ea2d..a77ea98 100644 --- a/ui/ncurses/nc-menu.c +++ b/ui/ncurses/nc-menu.c @@ -492,6 +492,19 @@ int pmenu_remove(struct pmenu *menu, struct pmenu_item *item) return 0; } +static int pmenu_destructor(void *ptr) +{ + struct pmenu *menu = ptr; + assert(menu->scr.sig == pb_pmenu_sig); + menu->scr.sig = pb_removed_sig; + + unpost_menu(menu->ncm); + free_menu(menu->ncm); + delwin(menu->scr.sub_ncw); + delwin(menu->scr.main_ncw); + return 0; +} + /** * pmenu_init - Allocate and initialize a new menu instance. * @@ -504,14 +517,13 @@ struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count, void (*on_exit)(struct pmenu *)) { struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu); - if (!menu) return NULL; - /* note items array has a null terminator */ + talloc_set_destructor(menu, pmenu_destructor); + /* note items array has a null terminator */ menu->items = talloc_zero_array(menu, ITEM *, item_count + 1); - if (!menu->items) { talloc_free(menu); return NULL; @@ -555,19 +567,3 @@ int pmenu_setup(struct pmenu *menu) return 0; } -/** - * pmenu_delete - Delete a menu instance. - * - */ - -void pmenu_delete(struct pmenu *menu) -{ - assert(menu->scr.sig == pb_pmenu_sig); - menu->scr.sig = pb_removed_sig; - - unpost_menu(menu->ncm); - free_menu(menu->ncm); - delwin(menu->scr.sub_ncw); - delwin(menu->scr.main_ncw); - talloc_free(menu); -} diff --git a/ui/ncurses/nc-menu.h b/ui/ncurses/nc-menu.h index 2f66bbb..136bf66 100644 --- a/ui/ncurses/nc-menu.h +++ b/ui/ncurses/nc-menu.h @@ -97,7 +97,6 @@ struct pmenu { struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count, void (*on_exit)(struct pmenu *)); int pmenu_setup(struct pmenu *menu); -void pmenu_delete(struct pmenu *menu); unsigned int pmenu_grow(struct pmenu *menu, unsigned int count); int pmenu_remove(struct pmenu *menu, struct pmenu_item *item); struct pmenu_item *pmenu_find_selected(struct pmenu *menu); |