]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-config.c
ui: add URL for static configurations to load a specified file
[petitboot] / ui / ncurses / nc-config.c
index b257076d03b2f50be229b2a51a718abe8a8822e7..e7451bb5dc08fda57d54cd9a73e9bbcd32a60711 100644 (file)
@@ -33,7 +33,7 @@
 #include "nc-config.h"
 #include "nc-widgets.h"
 
-#define N_FIELDS       34
+#define N_FIELDS       37
 
 extern struct help_text config_help_text;
 
@@ -53,6 +53,7 @@ struct config_screen {
        bool                    show_help;
        bool                    show_subset;
        bool                    need_redraw;
+       bool                    need_update;
 
        void                    (*on_exit)(struct cui *);
 
@@ -95,6 +96,9 @@ struct config_screen {
                struct nc_widget_label          *gateway_l;
                struct nc_widget_textbox        *gateway_f;
                struct nc_widget_label          *gateway_help_l;
+               struct nc_widget_label          *url_l;
+               struct nc_widget_textbox        *url_f;
+               struct nc_widget_label          *url_help_l;
                struct nc_widget_label          *dns_l;
                struct nc_widget_textbox        *dns_f;
                struct nc_widget_label          *dns_dhcp_help_l;
@@ -170,21 +174,6 @@ static void config_screen_resize(struct nc_scr *scr)
        (void)screen;
 }
 
-static int config_screen_post(struct nc_scr *scr)
-{
-       struct config_screen *screen = config_screen_from_scr(scr);
-       screen->show_subset = false;
-       widgetset_post(screen->widgetset);
-       nc_scr_frame_draw(scr);
-       if (screen->need_redraw) {
-               redrawwin(scr->main_ncw);
-               screen->need_redraw = false;
-       }
-       wrefresh(screen->scr.main_ncw);
-       pad_refresh(screen);
-       return 0;
-}
-
 static int config_screen_unpost(struct nc_scr *scr)
 {
        struct config_screen *screen = config_screen_from_scr(scr);
@@ -288,11 +277,12 @@ static int screen_process_form(struct config_screen *screen)
        }
 
        if (net_conf_type == NET_CONF_TYPE_STATIC) {
-               char *ip, *mask, *gateway;
+               char *ip, *mask, *gateway, *url;
 
                ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
                mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
                gateway = widget_textbox_get_value(screen->widgets.gateway_f);
+               url = widget_textbox_get_value(screen->widgets.url_f);
 
                if (!ip || !*ip || !mask || !*mask) {
                        screen->scr.frame.status =
@@ -306,6 +296,7 @@ static int screen_process_form(struct config_screen *screen)
                iface->static_config.address = talloc_asprintf(iface, "%s/%s",
                                ip, mask);
                iface->static_config.gateway = talloc_strdup(iface, gateway);
+               iface->static_config.url = talloc_strdup(iface, url);
        }
 
        str = widget_textbox_get_value(screen->widgets.dns_f);
@@ -524,6 +515,19 @@ static void config_screen_layout_widgets(struct config_screen *screen)
                y++;
        }
 
+       wl = widget_label_base(screen->widgets.url_l);
+       wf = widget_textbox_base(screen->widgets.url_f);
+       wh = widget_label_base(screen->widgets.url_help_l);
+       widget_set_visible(wl, show);
+       widget_set_visible(wf, show);
+       widget_set_visible(wh, show);
+
+       if (show) {
+               layout_pair(screen, y, screen->widgets.url_l, wf);
+               widget_move(wh, y, help_x);
+               y++;
+       }
+
        wh = widget_label_base(screen->widgets.dns_help_l);
        layout_pair(screen, y, screen->widgets.dns_l,
                        widget_textbox_base(screen->widgets.dns_f));
@@ -707,7 +711,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 {
        struct nc_widgetset *set = screen->widgetset;
        struct interface_config *ifcfg;
-       char *str, *ip, *mask, *gw;
+       char *str, *ip, *mask, *gw, *url;
        enum net_conf_type type;
        unsigned int i;
        int add_len, clear_len, any_len, min_len = 20;
@@ -825,7 +829,8 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        }
 
        screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
-       screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
+       screen->widgets.network_f = widget_new_select(set, 0, 0,
+                                               COLS - screen->field_x - 1);
 
        widget_select_add_option(screen->widgets.network_f,
                                        NET_CONF_TYPE_DHCP_ALL,
@@ -862,7 +867,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                                                i, str, is_default);
        }
 
-       gw = ip = mask = NULL;
+       url = gw = ip = mask = NULL;
        if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
                char *sep;
 
@@ -875,6 +880,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                        mask = sep + 1;
                }
                gw = ifcfg->static_config.gateway;
+               url = ifcfg->static_config.url;
        }
 
        screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
@@ -897,6 +903,11 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        widget_textbox_set_fixed_size(screen->widgets.gateway_f);
        widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
 
+       screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
+       screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
+       screen->widgets.url_help_l =
+               widget_new_label(set, 0, 0, _("(eg. tftp://)"));
+
        str = talloc_strdup(screen, "");
        for (i = 0; i < config->network.n_dns_servers; i++) {
                str = talloc_asprintf_append(str, "%s%s",
@@ -922,11 +933,12 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
                        _("Disk R/W:"));
        screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
-                                               COLS - screen->field_x);
+                                               COLS - screen->field_x - 1);
 
        widget_select_add_option(screen->widgets.allow_write_f, 0,
                                _("Prevent all writes to disk"),
                                !config->allow_writes);
+
        widget_select_add_option(screen->widgets.allow_write_f, 1,
                                _("Allow bootloader scripts to modify disks"),
                                config->allow_writes);
@@ -942,18 +954,22 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
 {
        struct config_screen *screen = arg;
-       int w_y, s_max;
+       int w_y, w_height, w_focus, s_max, adjust;
 
-       w_y = widget_y(widget) + widget_focus_y(widget);
+       w_height = widget_height(widget);
+       w_focus = widget_focus_y(widget);
+       w_y = widget_y(widget) + w_focus;
        s_max = getmaxy(screen->scr.sub_ncw) - 1;
 
        if (w_y < screen->scroll_y)
                screen->scroll_y = w_y;
 
-       else if (w_y + screen->scroll_y + 1 > s_max)
-               screen->scroll_y = 1 + w_y - s_max;
-
-       else
+       else if (w_y + screen->scroll_y + 1 > s_max) {
+               /* Fit as much of the widget into the screen as possible */
+               adjust = min(s_max - 1, w_height - w_focus);
+               if (w_y + adjust >= screen->scroll_y + s_max)
+                       screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
+       } else
                return;
 
        pad_refresh(screen);
@@ -1014,10 +1030,38 @@ void config_screen_update(struct config_screen *screen,
                const struct config *config,
                const struct system_info *sysinfo)
 {
+       if (screen->cui->current != config_screen_scr(screen)) {
+               screen->need_update = true;
+               return;
+       }
+
        config_screen_draw(screen, config, sysinfo);
        pad_refresh(screen);
 }
 
+static int config_screen_post(struct nc_scr *scr)
+{
+       struct config_screen *screen = config_screen_from_scr(scr);
+       screen->show_subset = false;
+
+       if (screen->need_update) {
+               config_screen_draw(screen, screen->cui->config,
+                                  screen->cui->sysinfo);
+               screen->need_update = false;
+       } else {
+               widgetset_post(screen->widgetset);
+       }
+
+       nc_scr_frame_draw(scr);
+       if (screen->need_redraw) {
+               redrawwin(scr->main_ncw);
+               screen->need_redraw = false;
+       }
+       wrefresh(screen->scr.main_ncw);
+       pad_refresh(screen);
+       return 0;
+}
+
 static int config_screen_destroy(void *arg)
 {
        struct config_screen *screen = arg;
@@ -1043,6 +1087,7 @@ struct config_screen *config_screen_init(struct cui *cui,
        screen->cui = cui;
        screen->on_exit = on_exit;
        screen->need_redraw = false;
+       screen->need_update = false;
        screen->label_x = 2;
        screen->field_x = 17;