X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fncurses%2Fnc-config.c;h=ca8c44c0e6bfb74a3208f251e83cdd135fb3eb01;hp=b7985aa60e40e13327bc5eb62ccc7e7770f834b5;hb=6afcd04684c27823afc778f712006e02b7470faf;hpb=2a6f3ebc8263e94441c2c865a6b28dae40e35990 diff --git a/ui/ncurses/nc-config.c b/ui/ncurses/nc-config.c index b7985aa..ca8c44c 100644 --- a/ui/ncurses/nc-config.c +++ b/ui/ncurses/nc-config.c @@ -17,9 +17,11 @@ #define _GNU_SOURCE - +#include +#include #include +#include #include #include #include @@ -29,27 +31,55 @@ #include "nc-config.h" #include "nc-widgets.h" -#define N_FIELDS 9 +#define N_FIELDS 23 + +enum net_conf_type { + NET_CONF_TYPE_DHCP_ALL, + NET_CONF_TYPE_DHCP_ONE, + NET_CONF_TYPE_STATIC, +}; struct config_screen { struct nc_scr scr; struct cui *cui; struct nc_widgetset *widgetset; + WINDOW *pad; + bool exit; void (*on_exit)(struct cui *); + int scroll_y; + int label_x; int field_x; + int network_config_y; + + enum net_conf_type net_conf_type; struct { struct nc_widget_checkbox *autoboot_f; 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; + struct nc_widget_label *iface_l; struct nc_widget_select *iface_f; + struct nc_widget_label *ip_addr_l; + 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; struct nc_widget_button *cancel_b; @@ -67,6 +97,16 @@ static struct config_screen *config_screen_from_scr(struct nc_scr *scr) return config_screen; } +static void pad_refresh(struct config_screen *screen) +{ + int y, x, rows, cols; + + getmaxyx(screen->scr.sub_ncw, rows, cols); + getbegyx(screen->scr.sub_ncw, y, x); + + prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols); +} + static void config_screen_process_key(struct nc_scr *scr, int key) { struct config_screen *screen = config_screen_from_scr(scr); @@ -76,7 +116,7 @@ static void config_screen_process_key(struct nc_scr *scr, int key) if (screen->exit) screen->on_exit(screen->cui); else if (handled) - wrefresh(screen->scr.main_ncw); + pad_refresh(screen); } static void config_screen_resize(struct nc_scr *scr) @@ -90,7 +130,7 @@ 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); - wrefresh(scr->main_ncw); + pad_refresh(screen); return 0; } @@ -106,11 +146,111 @@ struct nc_scr *config_screen_scr(struct config_screen *screen) return &screen->scr; } +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; + char *str, *end; + int rc; + + config_set_defaults(config); + + config->autoboot_enabled = + widget_checkbox_get_value(screen->widgets.autoboot_f); + + + str = widget_textbox_get_value(screen->widgets.timeout_f); + if (str) { + unsigned long x; + errno = 0; + x = strtoul(str, &end, 10); + if (!errno && end != str) + config->autoboot_timeout_sec = x; + } + + net_conf_type = widget_select_get_value(screen->widgets.network_f); + + /* if we don't have any network interfaces, prevent per-interface + * configuration */ + if (sysinfo->n_interfaces == 0) + net_conf_type = NET_CONF_TYPE_DHCP_ALL; + + if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) { + config->network.n_interfaces = 0; + + } else { + int idx; + + iface = talloc_zero(config, struct interface_config); + config->network.n_interfaces = 1; + config->network.interfaces = talloc_array(config, + struct interface_config *, 1); + config->network.interfaces[0] = iface; + + /* copy hwaddr (from the sysinfo interface data) to + * the configuration */ + idx = widget_select_get_value(screen->widgets.iface_f); + memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr, + sizeof(iface->hwaddr)); + } + + if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) { + iface->method = CONFIG_METHOD_DHCP; + } + + if (net_conf_type == NET_CONF_TYPE_STATIC) { + 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)); + } + + str = widget_textbox_get_value(screen->widgets.dns_f); + if (str && strlen(str)) { + char *dns, *tmp; + int i; + + for (;;) { + dns = strtok_r(str, " \t", &tmp); + + if (!dns) + break; + + i = config->network.n_dns_servers++; + config->network.dns_servers = talloc_realloc(config, + config->network.dns_servers, + const char *, + config->network.n_dns_servers); + config->network.dns_servers[i] = + talloc_strdup(config, dns); + + str = NULL; + } + } + + rc = cui_send_config(screen->cui, config); + talloc_free(config); + + if (rc) + pb_log("cui_send_config failed!\n"); + else + pb_debug("config sent!\n"); + + return 0; +} + static void ok_click(void *arg) { struct config_screen *screen = arg; - /* todo: save config */ - screen->on_exit(screen->cui); + screen_process_form(screen); + screen->exit = true; } static void cancel_click(void *arg) @@ -129,38 +269,179 @@ static int layout_pair(struct config_screen *screen, int y, return max(widget_height(label_w), widget_height(field)); } -static void config_screen_layout_widgets(struct config_screen *screen) +static void config_screen_layout_widgets(struct config_screen *screen, + enum net_conf_type net_conf) { - int y; + struct nc_widget *wl, *wf, *wh; + int y, x, help_x; + bool show; 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 += 2; + + y += layout_pair(screen, y, screen->widgets.network_l, + widget_select_base(screen->widgets.network_f)); + + y += 1; + + /* conditionally show iface select */ + wl = widget_label_base(screen->widgets.iface_l); + wf = widget_select_base(screen->widgets.iface_f); + + show = net_conf == NET_CONF_TYPE_DHCP_ONE || + net_conf == NET_CONF_TYPE_STATIC; + + widget_set_visible(wl, show); + widget_set_visible(wf, show); + + if (show) + y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1; + + /* conditionally show static IP params */ + show = net_conf == NET_CONF_TYPE_STATIC; + + wl = widget_label_base(screen->widgets.ip_addr_l); + wf = widget_textbox_base(screen->widgets.ip_addr_f); + widget_set_visible(wl, show); + widget_set_visible(wf, show); + x = screen->field_x + widget_width(wf) + 1; + + if (show) + layout_pair(screen, y, screen->widgets.ip_addr_l, wf); + + wl = widget_label_base(screen->widgets.ip_mask_l); + wf = widget_textbox_base(screen->widgets.ip_mask_f); + widget_set_visible(wl, show); + widget_set_visible(wf, show); + + if (show) { + widget_move(wl, y, x); + widget_move(wf, y, x + 2); + } + + /* 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) { + layout_pair(screen, y, screen->widgets.gateway_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)); + 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_dhcp_help_l); + widget_set_visible(wl, show); + if (show) { + widget_move(wl, y, screen->field_x); + y += 1; + } + + y += 1; + widget_move(widget_button_base(screen->widgets.ok_b), y, screen->field_x); widget_move(widget_button_base(screen->widgets.cancel_b), y, screen->field_x + 10); } +static void config_screen_network_change(void *arg, int value) +{ + struct config_screen *screen = arg; + screen->net_conf_type = value; + widgetset_unpost(screen->widgetset); + config_screen_layout_widgets(screen, value); + widgetset_post(screen->widgetset); +} + +static struct interface_config *first_active_interface( + const struct config *config) +{ + unsigned int i; + + for (i = 0; i < config->network.n_interfaces; i++) { + if (config->network.interfaces[i]->ignore) + continue; + return config->network.interfaces[i]; + } + return NULL; +} + +static enum net_conf_type find_net_conf_type(const struct config *config) +{ + struct interface_config *ifcfg; + + ifcfg = first_active_interface(config); + + if (!ifcfg) + return NET_CONF_TYPE_DHCP_ALL; + + else if (ifcfg->method == CONFIG_METHOD_DHCP) + return NET_CONF_TYPE_DHCP_ONE; + + else if (ifcfg->method == CONFIG_METHOD_STATIC) + return NET_CONF_TYPE_STATIC; + + assert(0); + return NET_CONF_TYPE_DHCP_ALL; +} + +static void config_screen_setup_empty(struct config_screen *screen) +{ + widget_new_label(screen->widgetset, 2, screen->field_x, + "Waiting for configuration data..."); + screen->widgets.cancel_b = widget_new_button(screen->widgetset, + 4, screen->field_x, 6, "Cancel", cancel_click, screen); +} + + static void config_screen_setup_widgets(struct config_screen *screen, const struct config *config, const struct system_info *sysinfo) { struct nc_widgetset *set = screen->widgetset; - char *str; - - (void)sysinfo; + struct interface_config *ifcfg; + char *str, *ip, *mask, *gw; + enum net_conf_type type; + unsigned int i; build_assert(sizeof(screen->widgets) / sizeof(struct widget *) == N_FIELDS); + type = screen->net_conf_type; + ifcfg = first_active_interface(config); + screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:"); screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0, config->autoboot_enabled); @@ -168,12 +449,166 @@ 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"); + + screen->widgets.network_l = widget_new_label(set, 0, 0, "Network"); + screen->widgets.network_f = widget_new_select(set, 0, 0, 50); + + widget_select_add_option(screen->widgets.network_f, + NET_CONF_TYPE_DHCP_ALL, + "DHCP on all active interfaces", + type == NET_CONF_TYPE_DHCP_ALL); + widget_select_add_option(screen->widgets.network_f, + NET_CONF_TYPE_DHCP_ONE, + "DHCP on a specific interface", + type == NET_CONF_TYPE_DHCP_ONE); + widget_select_add_option(screen->widgets.network_f, + NET_CONF_TYPE_STATIC, + "Static IP configuration", + type == NET_CONF_TYPE_STATIC); + + widget_select_on_change(screen->widgets.network_f, + 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); + + for (i = 0; i < sysinfo->n_interfaces; i++) { + struct interface_info *info = sysinfo->interfaces[i]; + bool is_default; + + is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr, + sizeof(ifcfg->hwaddr)); + + widget_select_add_option(screen->widgets.iface_f, + i, info->name, is_default); + } + + gw = ip = mask = NULL; + if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) { + char *sep; + + str = talloc_strdup(screen, ifcfg->static_config.address); + sep = strchr(str, '/'); + ip = str; + + if (sep) { + *sep = '\0'; + mask = sep + 1; + } + gw = ifcfg->static_config.gateway; + } + + 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_addr_mask_help_l = + widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)"); + + 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)"); + + str = talloc_strdup(screen, ""); + for (i = 0; i < config->network.n_dns_servers; i++) { + str = talloc_asprintf_append(str, "%s%s", + (i == 0) ? "" : " ", + config->network.dns_servers[i]); + } + + 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_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", ok_click, screen); screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel", cancel_click, 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; + + w_y = widget_y(widget); + w_height = widget_height(widget); + s_max = getmaxy(screen->scr.sub_ncw); + + 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 + return; + + pad_refresh(screen); +} + +void config_screen_update(struct config_screen *screen, + const struct config *config, + const struct system_info *sysinfo) +{ + bool repost = false; + int height; + + /* The size of the pad we'll need depends on the number of interfaces. + * + * We use N_FIELDS (which is quite conservative, as some fields share + * a line) as a base, then add 3 (as the network select field is + * takes 3 lines), and n_interfaces (as the network interface field + * has n_interfaces lines). + */ + height = N_FIELDS + 3; + if (sysinfo) + height += sysinfo->n_interfaces; + if (!screen->pad || getmaxy(screen->pad) < height) { + if (screen->pad) + delwin(screen->pad); + screen->pad = newpad(height, COLS); + } + + if (screen->widgetset) { + widgetset_unpost(screen->widgetset); + talloc_free(screen->widgetset); + repost = true; + } + + screen->widgetset = widgetset_create(screen, screen->scr.main_ncw, + screen->pad); + widgetset_set_widget_focus(screen->widgetset, + config_screen_widget_focus, screen); + + if (!config || !sysinfo) { + config_screen_setup_empty(screen); + } else { + screen->net_conf_type = find_net_conf_type(config); + + config_screen_setup_widgets(screen, config, sysinfo); + config_screen_layout_widgets(screen, screen->net_conf_type); + } + + if (repost) + widgetset_post(screen->widgetset); + + pad_refresh(screen); +} + +static int config_screen_destroy(void *arg) +{ + struct config_screen *screen = arg; + if (screen->pad) + delwin(screen->pad); + return 0; } struct config_screen *config_screen_init(struct cui *cui, @@ -184,6 +619,7 @@ struct config_screen *config_screen_init(struct cui *cui, struct config_screen *screen; screen = talloc_zero(cui, struct config_screen); + talloc_set_destructor(screen, config_screen_destroy); nc_scr_init(&screen->scr, pb_config_screen_sig, 0, cui, config_screen_process_key, config_screen_post, config_screen_unpost, @@ -192,7 +628,7 @@ struct config_screen *config_screen_init(struct cui *cui, screen->cui = cui; screen->on_exit = on_exit; screen->label_x = 2; - screen->field_x = 16; + screen->field_x = 17; screen->scr.frame.ltitle = talloc_strdup(screen, "Petitboot System Configuration"); @@ -201,13 +637,11 @@ struct config_screen *config_screen_init(struct cui *cui, "tab=next, shift+tab=previous"); nc_scr_frame_draw(&screen->scr); - screen->widgetset = widgetset_create(screen, screen->scr.main_ncw, - screen->scr.sub_ncw); - config_screen_setup_widgets(screen, config, sysinfo); - config_screen_layout_widgets(screen); + scrollok(screen->scr.sub_ncw, true); + + config_screen_update(screen, config, sysinfo); wrefresh(screen->scr.main_ncw); - scrollok(screen->scr.sub_ncw, true); return screen; }