From: Jeremy Kerr Date: Sun, 17 Nov 2013 03:04:08 +0000 (+1100) Subject: ui/ncurses: Make boot editor API consistent with config & sysinfo screens X-Git-Tag: v1.0.0~321 X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=37c9aee3ffc02a299d94867df9df8132b09fc611 ui/ncurses: Make boot editor API consistent with config & sysinfo screens The boot-editor API is a little more exposed than it needs to be: the boot_editor struct does not need to be available to other files, and the init function and on_exit functions differ from those provided for the config and sysinfo screens. This change unifies the boot_editor API with those for the other screens. Signed-off-by: Jeremy Kerr --- diff --git a/ui/ncurses/nc-boot-editor.c b/ui/ncurses/nc-boot-editor.c index c7c2ed8..02187e0 100644 --- a/ui/ncurses/nc-boot-editor.c +++ b/ui/ncurses/nc-boot-editor.c @@ -28,6 +28,30 @@ #include "nc-boot-editor.h" #include "nc-widgets.h" +struct boot_editor { + struct nc_scr scr; + struct cui *cui; + void *data; + struct pmenu_item *item; + void (*on_exit)(struct cui *cui, + struct pmenu_item *item, + struct pb_boot_data *bd); + + struct nc_widgetset *widgetset; + struct { + struct nc_widget_label *image_l; + struct nc_widget_textbox *image_f; + struct nc_widget_label *initrd_l; + struct nc_widget_textbox *initrd_f; + struct nc_widget_label *dtb_l; + struct nc_widget_textbox *dtb_f; + struct nc_widget_label *args_l; + struct nc_widget_textbox *args_f; + struct nc_widget_button *ok_b; + struct nc_widget_button *cancel_b; + } widgets; +}; + static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr) { struct boot_editor *boot_editor; @@ -65,6 +89,11 @@ static int boot_editor_unpost(struct nc_scr *scr) return 0; } +struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor) +{ + return &boot_editor->scr; +} + static void boot_editor_resize(struct nc_scr *scr) { /* FIXME: forms can't be resized, need to recreate here */ @@ -118,7 +147,7 @@ static void boot_editor_process_key(struct nc_scr *scr, int key) switch (key) { case 'x': case 27: /* ESC */ - boot_editor->on_exit(boot_editor, boot_editor_cancel, NULL); + boot_editor->on_exit(boot_editor->cui, NULL, NULL); nc_flush_keys(); } } @@ -140,38 +169,41 @@ static void ok_click(void *arg) struct pb_boot_data *bd; bd = boot_editor_prepare_data(boot_editor); - boot_editor->on_exit(boot_editor, boot_editor_update, bd); + boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd); } static void cancel_click(void *arg) { struct boot_editor *boot_editor = arg; - boot_editor->on_exit(boot_editor, boot_editor_cancel, NULL); + boot_editor->on_exit(boot_editor->cui, NULL, NULL); } -struct boot_editor *boot_editor_init(struct pmenu *menu, - const struct pb_boot_data *bd, - void (*on_exit)(struct boot_editor *, - enum boot_editor_result, - struct pb_boot_data *)) +struct boot_editor *boot_editor_init(struct cui *cui, + struct pmenu_item *item, + const struct system_info *sysinfo, + void (*on_exit)(struct cui *cui, + struct pmenu_item *item, + struct pb_boot_data *bd)) { int y, field_size, label_x = 1, field_x = 9; char *image, *initrd, *dtb, *args; struct boot_editor *boot_editor; struct nc_widgetset *set; - assert(on_exit); + (void)sysinfo; - boot_editor = talloc_zero(menu, struct boot_editor); + boot_editor = talloc_zero(cui, struct boot_editor); if (!boot_editor) return NULL; talloc_set_destructor(boot_editor, boot_editor_destructor); - boot_editor->original_pmenu = menu; + boot_editor->cui = cui; + boot_editor->item = item; + boot_editor->on_exit = on_exit; nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0, - menu, boot_editor_process_key, + cui, boot_editor_process_key, boot_editor_post, boot_editor_unpost, boot_editor_resize); boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor, @@ -180,9 +212,8 @@ struct boot_editor *boot_editor_init(struct pmenu *menu, boot_editor->scr.frame.help = talloc_strdup(boot_editor, "Enter=accept"); - boot_editor->on_exit = on_exit; - - if (bd) { + if (item) { + struct pb_boot_data *bd = cod_from_item(item)->bd; image = bd->image; initrd = bd->initrd; dtb = bd->dtb; diff --git a/ui/ncurses/nc-boot-editor.h b/ui/ncurses/nc-boot-editor.h index 6b72f25..5c22598 100644 --- a/ui/ncurses/nc-boot-editor.h +++ b/ui/ncurses/nc-boot-editor.h @@ -19,69 +19,24 @@ #if !defined(_PB_NC_KED_H) #define _PB_NC_KED_H -#include /* This must be included before ncurses.h */ -#if defined HAVE_NCURSESW_FORM_H -# include -#elif defined HAVE_NCURSES_FORM_H -# include -#elif defined HAVE_FORM_H -# include -#else -# error "Curses form.h not found." -#endif +#include "ui/common/discover-client.h" #include "types/types.h" -#include "ui/common/ui-system.h" -#include "nc-scr.h" - -enum boot_editor_attr_cursor { - boot_editor_attr_cursor_ins = A_NORMAL, - boot_editor_attr_cursor_ovl = A_NORMAL | A_UNDERLINE, -}; +#include "nc-cui.h" -/** - * enum boot_editor_result - Result code for boot_editor:on_exit(). - * @boot_editor_cancel: The user canceled the operation. - * @boot_editor_update: The args were updated. - */ +struct boot_editor; -enum boot_editor_result { - boot_editor_cancel, - boot_editor_update, -}; - -/** - * struct boot_editor - kexec args editor. - */ +struct boot_editor *boot_editor_init(struct cui *cui, + struct pmenu_item *item, + const struct system_info *sysinfo, + void (*on_exit)(struct cui *cui, + struct pmenu_item *item, + struct pb_boot_data *bd)); -struct boot_editor { - struct nc_scr scr; - void *data; - struct pmenu *original_pmenu; - void (*on_exit)(struct boot_editor *boot_editor, - enum boot_editor_result result, - struct pb_boot_data *bd); - enum boot_editor_attr_cursor attr_cursor; +struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor); - struct nc_widgetset *widgetset; - struct { - struct nc_widget_label *image_l; - struct nc_widget_textbox *image_f; - struct nc_widget_label *initrd_l; - struct nc_widget_textbox *initrd_f; - struct nc_widget_label *dtb_l; - struct nc_widget_textbox *dtb_f; - struct nc_widget_label *args_l; - struct nc_widget_textbox *args_f; - struct nc_widget_button *ok_b; - struct nc_widget_button *cancel_b; - } widgets; -}; +void boot_editor_update(struct boot_editor *boot_editor, + const struct system_info *info); -struct boot_editor *boot_editor_init(struct pmenu *menu, - const struct pb_boot_data *bd, - void (*on_exit)(struct boot_editor *, - enum boot_editor_result, - struct pb_boot_data *)); #endif diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 5af1171..107539b 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -33,15 +33,12 @@ #include "waiter/waiter.h" #include "process/process.h" #include "ui/common/discover-client.h" +#include "ui/common/ui-system.h" #include "nc-cui.h" +#include "nc-boot-editor.h" #include "nc-config.h" #include "nc-sysinfo.h" -static struct cui_opt_data *cod_from_item(struct pmenu_item *item) -{ - return item->data; -} - static void cui_start(void) { initscr(); /* Initialize ncurses. */ @@ -157,29 +154,23 @@ static int cui_boot(struct pmenu_item *item) return 0; } -/** - * cui_boot_editor_on_exit - The boot_editor on_exit callback. - */ - -static void cui_boot_editor_on_exit(struct boot_editor *boot_editor, - enum boot_editor_result boot_editor_result, +static void cui_boot_editor_on_exit(struct cui *cui, + struct pmenu_item *item, struct pb_boot_data *bd) { - struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu); - struct pmenu_item *item = boot_editor->data; + struct pmenu *menu = cui->main; struct cui_opt_data *cod; - if (boot_editor_result != boot_editor_update) { + /* Was the edit cancelled? */ + if (!bd) { cui_set_current(cui, &cui->main->scr); - talloc_free(boot_editor); + talloc_free(cui->boot_editor); + cui->boot_editor = NULL; return; } - assert(bd); - /* Is this was a new item, we'll need to update the menu */ if (!item) { - struct pmenu *menu = boot_editor->original_pmenu; int insert_pt; /* Detach the items array. */ @@ -205,32 +196,26 @@ static void cui_boot_editor_on_exit(struct boot_editor *boot_editor, cod->bd = talloc_steal(cod, bd); - /* FIXME: need to make item visible somehow */ set_current_item(item->pmenu->ncm, item->nci); cui_set_current(cui, &cui->main->scr); - talloc_free(boot_editor); + talloc_free(cui->boot_editor); + cui->boot_editor = NULL; } void cui_item_edit(struct pmenu_item *item) { struct cui *cui = cui_from_item(item); - struct cui_opt_data *cod = cod_from_item(item); - struct boot_editor *boot_editor; - - boot_editor = boot_editor_init(item->pmenu, cod->bd, - cui_boot_editor_on_exit); - boot_editor->data = item; - cui_set_current(cui, &boot_editor->scr); + cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo, + cui_boot_editor_on_exit); + cui_set_current(cui, boot_editor_scr(cui->boot_editor)); } void cui_item_new(struct pmenu *menu) { struct cui *cui = cui_from_pmenu(menu); - struct boot_editor *boot_editor; - - boot_editor = boot_editor_init(menu, NULL, - cui_boot_editor_on_exit); - cui_set_current(cui, &boot_editor->scr); + cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo, + cui_boot_editor_on_exit); + cui_set_current(cui, boot_editor_scr(cui->boot_editor)); } static void cui_sysinfo_exit(struct cui *cui) diff --git a/ui/ncurses/nc-cui.h b/ui/ncurses/nc-cui.h index aba682a..90af361 100644 --- a/ui/ncurses/nc-cui.h +++ b/ui/ncurses/nc-cui.h @@ -23,7 +23,6 @@ #include "ui/common/joystick.h" #include "nc-menu.h" -#include "nc-boot-editor.h" struct cui_opt_data { const char *name; @@ -59,6 +58,7 @@ struct cui { struct sysinfo_screen *sysinfo_screen; struct config *config; struct config_screen *config_screen; + struct boot_editor *boot_editor; struct pjs *pjs; void *platform_info; unsigned int default_item; diff --git a/ui/ncurses/nc-menu.h b/ui/ncurses/nc-menu.h index 693b96c..4639c55 100644 --- a/ui/ncurses/nc-menu.h +++ b/ui/ncurses/nc-menu.h @@ -67,6 +67,11 @@ static inline struct pmenu_item *pmenu_item_from_arg(void *arg) return item; } +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) { diff --git a/ui/ncurses/nc-widgets.c b/ui/ncurses/nc-widgets.c index d17034d..98ea6be 100644 --- a/ui/ncurses/nc-widgets.c +++ b/ui/ncurses/nc-widgets.c @@ -17,6 +17,33 @@ #define _GNU_SOURCE +#include "config.h" + +#include /* This must be included before ncurses.h */ +#if defined HAVE_NCURSESW_CURSES_H +# include +#elif defined HAVE_NCURSESW_H +# include +#elif defined HAVE_NCURSES_CURSES_H +# include +#elif defined HAVE_NCURSES_H +# include +#elif defined HAVE_CURSES_H +# include +#else +# error "Curses header file not found." +#endif + +#if defined HAVE_NCURSESW_FORM_H +# include +#elif defined HAVE_NCURSES_FORM_H +# include +#elif defined HAVE_FORM_H +# include +#else +# error "Curses form.h not found." +#endif + #include #include