]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-boot-editor.c
ui/ncurses: Use pmenu_item destrutor to free ncurses ITEMs
[petitboot] / ui / ncurses / nc-boot-editor.c
index a1b752f82f28f3c19ee294215fc1209ff91cdb24..329699ab8ebdb6842369d9ad680cacf8e145907a 100644 (file)
@@ -16,9 +16,9 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#if defined(HAVE_CONFIG_H)
 #include "config.h"
-
-#define _GNU_SOURCE
+#endif
 
 #include <assert.h>
 #include <string.h>
@@ -33,13 +33,21 @@ struct boot_editor {
        struct cui              *cui;
        void                    *data;
        struct pmenu_item       *item;
+       enum {
+               STATE_EDIT,
+               STATE_CANCEL,
+               STATE_SAVE,
+               STATE_HELP,
+       }                       state;
        void                    (*on_exit)(struct cui *cui,
                                        struct pmenu_item *item,
                                        struct pb_boot_data *bd);
 
        int                     label_x;
        int                     field_x;
+       int                     scroll_y;
 
+       WINDOW                  *pad;
        struct nc_widgetset     *widgetset;
        struct {
                struct nc_widget_label          *device_l;
@@ -53,12 +61,19 @@ struct boot_editor {
                struct nc_widget_label          *args_l;
                struct nc_widget_textbox        *args_f;
                struct nc_widget_button         *ok_b;
+               struct nc_widget_button         *help_b;
                struct nc_widget_button         *cancel_b;
        } widgets;
 
        const char              *selected_device;
+       char                    *image;
+       char                    *initrd;
+       char                    *dtb;
+       char                    *args;
 };
 
+extern const char *boot_editor_help_text;
+
 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
 {
        struct boot_editor *boot_editor;
@@ -70,6 +85,17 @@ static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
        return boot_editor;
 }
 
+static void pad_refresh(struct boot_editor *boot_editor)
+{
+       int y, x, rows, cols;
+
+       getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
+       getbegyx(boot_editor->scr.sub_ncw, y, x);
+
+       prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
+                       y, x, rows, cols);
+}
+
 static struct boot_editor *boot_editor_from_arg(void *arg)
 {
        struct boot_editor *boot_editor = arg;
@@ -81,12 +107,11 @@ static struct boot_editor *boot_editor_from_arg(void *arg)
 static int boot_editor_post(struct nc_scr *scr)
 {
        struct boot_editor *boot_editor = boot_editor_from_scr(scr);
-
        widgetset_post(boot_editor->widgetset);
        nc_scr_frame_draw(scr);
-       redrawwin(boot_editor->scr.main_ncw);
+       redrawwin(scr->main_ncw);
        wrefresh(boot_editor->scr.main_ncw);
-
+       pad_refresh(boot_editor);
        return 0;
 }
 
@@ -116,7 +141,7 @@ static char *conditional_prefix(struct pb_boot_data *ctx,
        if (!value || !*value)
                return NULL;
 
-       sep = NULL;
+       sep = "";
        if (!prefix)
                prefix = "";
        else if (prefix[strlen(prefix)] != '/')
@@ -168,19 +193,38 @@ static struct pb_boot_data *boot_editor_prepare_data(
 static void boot_editor_process_key(struct nc_scr *scr, int key)
 {
        struct boot_editor *boot_editor = boot_editor_from_scr(scr);
+       struct pmenu_item *item;
+       struct pb_boot_data *bd;
        bool handled;
 
        handled = widgetset_process_key(boot_editor->widgetset, key);
-       if (handled) {
-               wrefresh(boot_editor->scr.main_ncw);
-               return;
-       }
-
-       switch (key) {
-       case 'x':
-       case 27: /* ESC */
-               boot_editor->on_exit(boot_editor->cui, NULL, NULL);
-               nc_flush_keys();
+       if (handled)
+               pad_refresh(boot_editor);
+
+       else if (key == 'x' || key == 27)
+               boot_editor->state = STATE_CANCEL;
+
+       else if (key == 'h')
+               boot_editor->state = STATE_HELP;
+
+       item = NULL;
+       bd = NULL;
+
+       switch (boot_editor->state) {
+       case STATE_SAVE:
+               item = boot_editor->item;
+               bd = boot_editor_prepare_data(boot_editor);
+               /* fall through */
+       case STATE_CANCEL:
+               boot_editor->on_exit(boot_editor->cui, item, bd);
+               break;
+       case STATE_HELP:
+               boot_editor->state = STATE_EDIT;
+               cui_show_help(boot_editor->cui, "Boot Option Editor",
+                               boot_editor_help_text);
+               break;
+       default:
+               break;
        }
 }
 
@@ -192,22 +236,27 @@ static int boot_editor_destructor(void *arg)
 {
        struct boot_editor *boot_editor = boot_editor_from_arg(arg);
        boot_editor->scr.sig = pb_removed_sig;
+       if (boot_editor->pad)
+               delwin(boot_editor->pad);
        return 0;
 }
 
 static void ok_click(void *arg)
 {
        struct boot_editor *boot_editor = arg;
-       struct pb_boot_data *bd;
+       boot_editor->state = STATE_SAVE;
+}
 
-       bd = boot_editor_prepare_data(boot_editor);
-       boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd);
+static void help_click(void *arg)
+{
+       struct boot_editor *boot_editor = arg;
+       boot_editor->state = STATE_HELP;
 }
 
 static void cancel_click(void *arg)
 {
        struct boot_editor *boot_editor = arg;
-       boot_editor->on_exit(boot_editor->cui, NULL, NULL);
+       boot_editor->state = STATE_CANCEL;
 }
 
 static int layout_pair(struct boot_editor *boot_editor, int y,
@@ -221,6 +270,11 @@ static int layout_pair(struct boot_editor *boot_editor, int y,
        return max(widget_height(label_w), widget_height(field_w));
 }
 
+static int pad_height(int blockdevs_height)
+{
+       return 10 + blockdevs_height;
+}
+
 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
 {
        struct nc_widget *wf, *wl;
@@ -249,7 +303,28 @@ static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
 
        y++;
        widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
-       widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 19);
+       widget_move(widget_button_base(boot_editor->widgets.help_b), y, 19);
+       widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 29);
+}
+
+static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
+{
+       struct boot_editor *boot_editor = arg;
+       int w_y, s_max;
+
+       w_y = widget_y(widget) + widget_focus_y(widget);
+       s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
+
+       if (w_y < boot_editor->scroll_y)
+               boot_editor->scroll_y = w_y;
+
+       else if (w_y + boot_editor->scroll_y + 1 > s_max)
+               boot_editor->scroll_y = 1 + w_y - s_max;
+
+       else
+               return;
+
+       pad_refresh(boot_editor);
 }
 
 static void boot_editor_device_select_change(void *arg, int idx)
@@ -271,7 +346,7 @@ static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
 
        widget_select_drop_options(select);
 
-       for (i = 0; i < sysinfo->n_blockdevs; i++) {
+       for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
                struct blockdev_info *bd_info = sysinfo->blockdevs[i];
                const char *name;
 
@@ -281,18 +356,155 @@ static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
                                !strcmp(bd_info->name,
                                                boot_editor->selected_device);
 
-               widget_select_add_option(boot_editor->widgets.device_f,
-                               i, name, selected);
+               widget_select_add_option(select, i, name, selected);
        }
 
        /* If we're editing an existing option, the paths will be fully-
         * resolved. In this case, we want the manual device pre-selected.
         * However, we only do this if the widget hasn't been manually
         * changed. */
-       selected = boot_editor->item && !boot_editor->selected_device;
+       selected = !boot_editor->selected_device;
+
+       widget_select_add_option(select, -1, "Specify paths/URLs manually",
+                       selected);
+}
+
+static bool path_on_device(struct blockdev_info *bd_info,
+               const char *path)
+{
+       int len;
+
+       if (!bd_info->mountpoint)
+               return false;
+
+       len = strlen(bd_info->mountpoint);
+       if (strncmp(bd_info->mountpoint, path, len))
+               return false;
+
+       /* if the mountpoint doesn't have a trailing slash, ensure that
+        * the path starts with one (so we don't match a "/mnt/sda1/foo" path
+        * on a "/mnt/sda" mountpoint) */
+       return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
+}
+
+
+static void boot_editor_find_device(struct boot_editor *boot_editor,
+               struct pb_boot_data *bd, const struct system_info *sysinfo)
+{
+       struct blockdev_info *bd_info, *tmp;
+       unsigned int i, len;
+
+       if (!sysinfo || !sysinfo->n_blockdevs)
+               return;
+
+       /* find the device for our boot image, by finding the longest
+        * matching blockdev's mountpoint */
+       for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
+               tmp = sysinfo->blockdevs[i];
+               if (!path_on_device(tmp, bd->image))
+                       continue;
+               if (strlen(tmp->mountpoint) <= len)
+                       continue;
+               bd_info = tmp;
+               len = strlen(tmp->mountpoint);
+       }
+
+       if (!bd_info)
+               return;
+
+       /* ensure that other paths are on this device */
+       if (bd->initrd && !path_on_device(bd_info, bd->initrd))
+               return;
+
+       if (bd->dtb && !path_on_device(bd_info, bd->dtb))
+               return;
+
+       /* ok, we match; preselect the device option, and remove the common
+        * prefix */
+       boot_editor->selected_device = bd_info->name;
+       boot_editor->image += len;
+
+       if (boot_editor->initrd)
+               boot_editor->initrd += len;
+       if (boot_editor->dtb)
+               boot_editor->dtb += len;
+}
+
+static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
+               const struct system_info *sysinfo)
+{
+       struct nc_widgetset *set;
+       int field_size;
+
+       field_size = COLS - 1 - boot_editor->field_x;
+
+       boot_editor->widgetset = set = widgetset_create(boot_editor,
+                       boot_editor->scr.main_ncw,
+                       boot_editor->pad);
+
+       widgetset_set_widget_focus(boot_editor->widgetset,
+                       boot_editor_widget_focus, boot_editor);
+
+       boot_editor->widgets.device_l = widget_new_label(set, 0, 0, "Device:");
+       boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
+                                               field_size);
+       widget_select_on_change(boot_editor->widgets.device_f,
+                       boot_editor_device_select_change, boot_editor);
+
+       boot_editor_populate_device_select(boot_editor, sysinfo);
+
+       boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
+                       "Kernel:");
+       boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
+                                               field_size, boot_editor->image);
+
+       boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
+                       "Initrd:");
+       boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
+                                               field_size,
+                                               boot_editor->initrd);
+
+       boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
+                       "Device tree:");
+       boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
+                                               field_size, boot_editor->dtb);
 
-       widget_select_add_option(boot_editor->widgets.device_f,
-                       -1, "Specify paths/URLs manually", selected);
+       boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
+                       "Boot arguments:");
+       boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
+                                       field_size, boot_editor->args);
+
+       boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
+                                       "OK", ok_click, boot_editor);
+       boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 6,
+                                       "Help", help_click, boot_editor);
+       boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
+                                       "Cancel", cancel_click, boot_editor);
+}
+
+void boot_editor_update(struct boot_editor *boot_editor,
+               const struct system_info *sysinfo)
+{
+       int height;
+
+       widgetset_unpost(boot_editor->widgetset);
+
+       height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
+       if (getmaxy(boot_editor->pad) < height) {
+               delwin(boot_editor->pad);
+               boot_editor->pad = newpad(height, COLS);
+               widgetset_set_windows(boot_editor->widgetset,
+                               boot_editor->scr.main_ncw,
+                               boot_editor->pad);
+       }
+
+       boot_editor_populate_device_select(boot_editor, sysinfo);
+
+       boot_editor_layout_widgets(boot_editor);
+
+       widgetset_post(boot_editor->widgetset);
+
+       pad_refresh(boot_editor);
 }
 
 struct boot_editor *boot_editor_init(struct cui *cui,
@@ -302,12 +514,7 @@ struct boot_editor *boot_editor_init(struct cui *cui,
                                struct pmenu_item *item,
                                struct pb_boot_data *bd))
 {
-       char *image, *initrd, *dtb, *args;
        struct boot_editor *boot_editor;
-       struct nc_widgetset *set;
-       int field_size;
-
-       (void)sysinfo;
 
        boot_editor = talloc_zero(cui, struct boot_editor);
 
@@ -318,9 +525,10 @@ struct boot_editor *boot_editor_init(struct cui *cui,
        boot_editor->cui = cui;
        boot_editor->item = item;
        boot_editor->on_exit = on_exit;
+       boot_editor->state = STATE_EDIT;
 
        boot_editor->label_x = 1;
-       boot_editor->field_x = 9;
+       boot_editor->field_x = 17;
 
        nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
                        cui, boot_editor_process_key,
@@ -330,54 +538,29 @@ struct boot_editor *boot_editor_init(struct cui *cui,
                        "Petitboot Option Editor");
        boot_editor->scr.frame.rtitle = NULL;
        boot_editor->scr.frame.help = talloc_strdup(boot_editor,
-                       "Enter=accept");
+                       "tab=next, shift+tab=previous, x=exit, h=help");
+       nc_scr_frame_draw(&boot_editor->scr);
 
        if (item) {
                struct pb_boot_data *bd = cod_from_item(item)->bd;
-               image = bd->image;
-               initrd = bd->initrd;
-               dtb = bd->dtb;
-               args = bd->args;
+               boot_editor->image = bd->image;
+               boot_editor->initrd = bd->initrd;
+               boot_editor->dtb = bd->dtb;
+               boot_editor->args = bd->args;
+               boot_editor_find_device(boot_editor, bd, sysinfo);
        } else {
-               image = initrd = dtb = args = "";
+               boot_editor->image = boot_editor->initrd =
+                       boot_editor->dtb = boot_editor->args = "";
        }
 
-       field_size = COLS - 1 - boot_editor->field_x;
-
-       boot_editor->widgetset = set = widgetset_create(boot_editor,
-                       boot_editor->scr.main_ncw,
-                       boot_editor->scr.sub_ncw);
+       boot_editor->pad = newpad(
+                               pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
+                               COLS);
 
-       boot_editor->widgets.device_l = widget_new_label(set, 0, 0, "device:");
-       boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
-                                               field_size);
-       widget_select_on_change(boot_editor->widgets.device_f,
-                       boot_editor_device_select_change, boot_editor);
-
-       boot_editor_populate_device_select(boot_editor, sysinfo);
-
-       boot_editor->widgets.image_l = widget_new_label(set, 0, 0, "image:");
-       boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
-                                               field_size, image);
-
-       boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0, "initrd:");
-       boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
-                                               field_size, initrd);
-
-       boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0, "dtb:");
-       boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
-                                               field_size, dtb);
-
-       boot_editor->widgets.args_l = widget_new_label(set, 0, 0, "args:");
-       boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
-                                       field_size, args);
-
-       boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
-                                       "OK", ok_click, boot_editor);
-       boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
-                                       "Cancel", cancel_click, boot_editor);
+       boot_editor_setup_widgets(boot_editor, sysinfo);
 
        boot_editor_layout_widgets(boot_editor);
+       wrefresh(boot_editor->scr.main_ncw);
 
        return boot_editor;
 }