X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fncurses%2Fnc-config.c;h=9cc6a29085ff3e4b21b14d3bb11ed4ec689e6763;hp=0f357024a0f3376af47dd9c2e5ec8a400442b88b;hb=702f9c313dea66817cf05196d84cd07889e80c51;hpb=9670732fd1a67ae83cbc5e0c48792231e6a0dcb3 diff --git a/ui/ncurses/nc-config.c b/ui/ncurses/nc-config.c index 0f35702..9cc6a29 100644 --- a/ui/ncurses/nc-config.c +++ b/ui/ncurses/nc-config.c @@ -15,7 +15,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define _GNU_SOURCE +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif #include #include @@ -26,12 +28,11 @@ #include #include -#include "config.h" #include "nc-cui.h" #include "nc-config.h" #include "nc-widgets.h" -#define N_FIELDS 19 +#define N_FIELDS 23 enum net_conf_type { NET_CONF_TYPE_DHCP_ALL, @@ -61,6 +62,7 @@ struct config_screen { struct nc_widget_label *autoboot_l; struct nc_widget_textbox *timeout_f; struct nc_widget_label *timeout_l; + struct nc_widget_label *timeout_help_l; struct nc_widget_label *network_l; struct nc_widget_select *network_f; @@ -71,10 +73,13 @@ struct config_screen { struct nc_widget_textbox *ip_addr_f; struct nc_widget_label *ip_mask_l; struct nc_widget_textbox *ip_mask_f; + struct nc_widget_label *ip_addr_mask_help_l; struct nc_widget_label *gateway_l; struct nc_widget_textbox *gateway_f; + struct nc_widget_label *gateway_help_l; struct nc_widget_label *dns_l; struct nc_widget_textbox *dns_f; + struct nc_widget_label *dns_dhcp_help_l; struct nc_widget_label *dns_help_l; struct nc_widget_button *ok_b; @@ -126,6 +131,8 @@ static int config_screen_post(struct nc_scr *scr) struct config_screen *screen = config_screen_from_scr(scr); widgetset_post(screen->widgetset); nc_scr_frame_draw(scr); + redrawwin(scr->main_ncw); + wrefresh(screen->scr.main_ncw); pad_refresh(screen); return 0; } @@ -145,13 +152,13 @@ struct nc_scr *config_screen_scr(struct config_screen *screen) static int screen_process_form(struct config_screen *screen) { const struct system_info *sysinfo = screen->cui->sysinfo; - struct config *config = talloc_zero(screen, struct config); enum net_conf_type net_conf_type; struct interface_config *iface; + struct config *config; char *str, *end; int rc; - config_set_defaults(config); + config = config_copy(screen, screen->cui->config); config->autoboot_enabled = widget_checkbox_get_value(screen->widgets.autoboot_f); @@ -197,15 +204,24 @@ static int screen_process_form(struct config_screen *screen) } if (net_conf_type == NET_CONF_TYPE_STATIC) { + char *ip, *mask, *gateway; + + 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); + + if (!ip || !*ip || !mask || !*mask) { + screen->scr.frame.status = + "No IP / mask values are set"; + nc_scr_frame_draw(&screen->scr); + talloc_free(config); + return -1; + } + iface->method = CONFIG_METHOD_STATIC; iface->static_config.address = talloc_asprintf(iface, "%s/%s", - widget_textbox_get_value( - screen->widgets.ip_addr_f), - widget_textbox_get_value( - screen->widgets.ip_mask_f)); - iface->static_config.gateway = talloc_strdup(iface, - widget_textbox_get_value( - screen->widgets.gateway_f)); + ip, mask); + iface->static_config.gateway = talloc_strdup(iface, gateway); } str = widget_textbox_get_value(screen->widgets.dns_f); @@ -245,8 +261,12 @@ static int screen_process_form(struct config_screen *screen) static void ok_click(void *arg) { struct config_screen *screen = arg; - screen_process_form(screen); - screen->exit = true; + if (screen_process_form(screen)) + /* errors are written to the status line, so we'll need + * to refresh */ + wrefresh(screen->scr.main_ncw); + else + screen->exit = true; } static void cancel_click(void *arg) @@ -268,19 +288,25 @@ static int layout_pair(struct config_screen *screen, int y, static void config_screen_layout_widgets(struct config_screen *screen, enum net_conf_type net_conf) { - struct nc_widget *wl, *wf; + struct nc_widget *wl, *wf, *wh; + int y, x, help_x; bool show; - int y, x; y = 1; + help_x = screen->field_x + 2 + + widget_width(widget_textbox_base(screen->widgets.dns_f)); y += layout_pair(screen, y, screen->widgets.autoboot_l, widget_checkbox_base(screen->widgets.autoboot_f)); - y += layout_pair(screen, y, screen->widgets.timeout_l, - widget_textbox_base(screen->widgets.timeout_f)); + wf = widget_textbox_base(screen->widgets.timeout_f); + widget_move(widget_label_base(screen->widgets.timeout_l), + y, screen->label_x); + widget_move(wf, y, screen->field_x); + widget_move(widget_label_base(screen->widgets.timeout_help_l), + y, screen->field_x + widget_width(wf) + 1); - y += 1; + y += 2; y += layout_pair(screen, y, screen->widgets.network_l, widget_select_base(screen->widgets.network_f)); @@ -320,23 +346,38 @@ static void config_screen_layout_widgets(struct config_screen *screen, if (show) { widget_move(wl, y, x); widget_move(wf, y, x + 2); - y += 1; + } + + /* help for IP/mask */ + wh = widget_label_base(screen->widgets.ip_addr_mask_help_l); + widget_set_visible(wh, show); + if (show) { + widget_move(wh, y, help_x); + y++; } wl = widget_label_base(screen->widgets.gateway_l); wf = widget_textbox_base(screen->widgets.gateway_f); + wh = widget_label_base(screen->widgets.gateway_help_l); widget_set_visible(wl, show); widget_set_visible(wf, show); + widget_set_visible(wh, show); - if (show) - y += layout_pair(screen, y, screen->widgets.gateway_l, wf) + 1; + if (show) { + layout_pair(screen, y, screen->widgets.gateway_l, wf); + widget_move(wh, y, help_x); + y++; + } - y += layout_pair(screen, y, screen->widgets.dns_l, + wh = widget_label_base(screen->widgets.dns_help_l); + layout_pair(screen, y, screen->widgets.dns_l, widget_textbox_base(screen->widgets.dns_f)); + widget_move(wh, y, help_x); + y++; /* we show the DNS/DHCP help if we're configuring DHCP */ show = net_conf != NET_CONF_TYPE_STATIC; - wl = widget_label_base(screen->widgets.dns_help_l); + wl = widget_label_base(screen->widgets.dns_dhcp_help_l); widget_set_visible(wl, show); if (show) { widget_move(wl, y, screen->field_x); @@ -424,6 +465,9 @@ static void config_screen_setup_widgets(struct config_screen *screen, str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec); screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:"); screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str); + screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds"); + + widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999); screen->widgets.network_l = widget_new_label(set, 0, 0, "Network"); screen->widgets.network_f = widget_new_select(set, 0, 0, 50); @@ -445,17 +489,22 @@ static void config_screen_setup_widgets(struct config_screen *screen, config_screen_network_change, screen); screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:"); - screen->widgets.iface_f = widget_new_select(set, 0, 0, 20); + screen->widgets.iface_f = widget_new_select(set, 0, 0, 50); for (i = 0; i < sysinfo->n_interfaces; i++) { struct interface_info *info = sysinfo->interfaces[i]; + char str[50], mac[20]; bool is_default; is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr, sizeof(ifcfg->hwaddr)); + mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac)); + snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac, + info->link ? "link up" : "link down"); + widget_select_add_option(screen->widgets.iface_f, - i, info->name, is_default); + i, str, is_default); } gw = ip = mask = NULL; @@ -476,10 +525,19 @@ static void config_screen_setup_widgets(struct config_screen *screen, screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:"); screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip); screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/"); - screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask); + screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 4, mask); + screen->widgets.ip_addr_mask_help_l = + widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)"); + + widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f); + widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31); screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:"); screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw); + screen->widgets.gateway_help_l = + widget_new_label(set, 0, 0, "(eg. 192.168.0.1)"); + + widget_textbox_set_validator_ipv4(screen->widgets.gateway_f); str = talloc_strdup(screen, ""); for (i = 0; i < config->network.n_dns_servers; i++) { @@ -490,8 +548,12 @@ static void config_screen_setup_widgets(struct config_screen *screen, screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):"); screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str); + screen->widgets.dns_help_l = + widget_new_label(set, 0, 0, "(eg. 192.168.0.2)"); - screen->widgets.dns_help_l = widget_new_label(set, 0, 0, + widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f); + + screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0, "(if not provided by DHCP server)"); screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK", @@ -503,17 +565,16 @@ 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, w_height, s_max; + int w_y, s_max; - w_y = widget_y(widget); - w_height = widget_height(widget); - s_max = getmaxy(screen->scr.sub_ncw); + w_y = widget_y(widget) + widget_focus_y(widget); + s_max = getmaxy(screen->scr.sub_ncw) - 1; if (w_y < screen->scroll_y) screen->scroll_y = w_y; - else if (w_y + w_height + screen->scroll_y > s_max - 1) - screen->scroll_y = 1 + w_y + w_height - s_max; + else if (w_y + screen->scroll_y + 1 > s_max) + screen->scroll_y = 1 + w_y - s_max; else return; @@ -521,8 +582,7 @@ static void config_screen_widget_focus(struct nc_widget *widget, void *arg) pad_refresh(screen); } - -void config_screen_update(struct config_screen *screen, +static void config_screen_draw(struct config_screen *screen, const struct config *config, const struct system_info *sysinfo) { @@ -539,7 +599,7 @@ void config_screen_update(struct config_screen *screen, height = N_FIELDS + 3; if (sysinfo) height += sysinfo->n_interfaces; - if (!screen->pad || getmaxx(screen->pad) < height) { + if (!screen->pad || getmaxy(screen->pad) < height) { if (screen->pad) delwin(screen->pad); screen->pad = newpad(height, COLS); @@ -567,7 +627,13 @@ void config_screen_update(struct config_screen *screen, if (repost) widgetset_post(screen->widgetset); +} +void config_screen_update(struct config_screen *screen, + const struct config *config, + const struct system_info *sysinfo) +{ + config_screen_draw(screen, config, sysinfo); pad_refresh(screen); } @@ -607,9 +673,7 @@ struct config_screen *config_screen_init(struct cui *cui, scrollok(screen->scr.sub_ncw, true); - config_screen_update(screen, config, sysinfo); - - wrefresh(screen->scr.main_ncw); + config_screen_draw(screen, config, sysinfo); return screen; }