X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fncurses%2Fnc-config.c;h=8c5e9a2fc1756577cd61ad12d305de3e445f384b;hp=9cc6a29085ff3e4b21b14d3bb11ed4ec689e6763;hb=9073914cfe15b2c81a6cdd5988fbfdfe817e797a;hpb=7f7e3688f3285ea803a5579b240cff54958e16e2 diff --git a/ui/ncurses/nc-config.c b/ui/ncurses/nc-config.c index 9cc6a29..8c5e9a2 100644 --- a/ui/ncurses/nc-config.c +++ b/ui/ncurses/nc-config.c @@ -32,7 +32,15 @@ #include "nc-config.h" #include "nc-widgets.h" -#define N_FIELDS 23 +#define N_FIELDS 25 + +extern const char *config_help_text; + +enum autoboot_type { + AUTOBOOT_ANY, + AUTOBOOT_ONE, + AUTOBOOT_DISABLED, +}; enum net_conf_type { NET_CONF_TYPE_DHCP_ALL, @@ -47,6 +55,7 @@ struct config_screen { WINDOW *pad; bool exit; + bool show_help; void (*on_exit)(struct cui *); int scroll_y; @@ -56,10 +65,12 @@ struct config_screen { int network_config_y; enum net_conf_type net_conf_type; + enum autoboot_type autoboot_type; struct { - struct nc_widget_checkbox *autoboot_f; + struct nc_widget_select *autoboot_f; struct nc_widget_label *autoboot_l; + struct nc_widget_select *boot_device_f; struct nc_widget_textbox *timeout_f; struct nc_widget_label *timeout_l; struct nc_widget_label *timeout_help_l; @@ -83,6 +94,7 @@ struct config_screen { struct nc_widget_label *dns_help_l; struct nc_widget_button *ok_b; + struct nc_widget_button *help_b; struct nc_widget_button *cancel_b; } widgets; }; @@ -114,10 +126,30 @@ static void config_screen_process_key(struct nc_scr *scr, int key) bool handled; handled = widgetset_process_key(screen->widgetset, key); - if (screen->exit) + + if (!handled) { + switch (key) { + case 'x': + case 27: /* esc */ + screen->exit = true; + break; + case 'h': + screen->show_help = true; + break; + } + } + + if (screen->exit) { screen->on_exit(screen->cui); - else if (handled) + + } else if (screen->show_help) { + screen->show_help = false; + cui_show_help(screen->cui, "System Configuration", + config_help_text); + + } else if (handled) { pad_refresh(screen); + } } static void config_screen_resize(struct nc_scr *scr) @@ -156,13 +188,38 @@ static int screen_process_form(struct config_screen *screen) struct interface_config *iface; struct config *config; char *str, *end; - int rc; + int rc, idx; config = config_copy(screen, screen->cui->config); - config->autoboot_enabled = - widget_checkbox_get_value(screen->widgets.autoboot_f); + screen->autoboot_type = + widget_select_get_value(screen->widgets.autoboot_f); + + config->autoboot_enabled = screen->autoboot_type != AUTOBOOT_DISABLED; + + if (screen->autoboot_type == AUTOBOOT_ONE) { + char mac[20], *uuid = NULL; + + /* if idx is -1 here, we have an unknown UUID selected. + * Otherwise, it's a blockdev index (idx <= n_blockdevs) or an + * interface index. + */ + idx = widget_select_get_value(screen->widgets.boot_device_f); + if (idx >= (int)sysinfo->n_blockdevs) { + struct interface_info *info = sysinfo-> + interfaces[idx - sysinfo->n_blockdevs]; + mac_str(info->hwaddr, info->hwaddr_size, + mac, sizeof(mac)); + uuid = mac; + } else if (idx != -1) { + uuid = sysinfo->blockdevs[idx]->uuid; + } + if (uuid) { + talloc_free(config->boot_device); + config->boot_device = talloc_strdup(config, uuid); + } + } str = widget_textbox_get_value(screen->widgets.timeout_f); if (str) { @@ -184,8 +241,6 @@ static int screen_process_form(struct config_screen *screen) 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, @@ -225,6 +280,10 @@ static int screen_process_form(struct config_screen *screen) } str = widget_textbox_get_value(screen->widgets.dns_f); + talloc_free(config->network.dns_servers); + config->network.dns_servers = NULL; + config->network.n_dns_servers = 0; + if (str && strlen(str)) { char *dns, *tmp; int i; @@ -269,6 +328,12 @@ static void ok_click(void *arg) screen->exit = true; } +static void help_click(void *arg) +{ + struct config_screen *screen = arg; + screen->show_help = true; +} + static void cancel_click(void *arg) { struct config_screen *screen = arg; @@ -285,8 +350,7 @@ 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, - enum net_conf_type net_conf) +static void config_screen_layout_widgets(struct config_screen *screen) { struct nc_widget *wl, *wf, *wh; int y, x, help_x; @@ -297,16 +361,35 @@ static void config_screen_layout_widgets(struct config_screen *screen, 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)); + widget_select_base(screen->widgets.autoboot_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); + wf = widget_select_base(screen->widgets.boot_device_f); + if (screen->autoboot_type == AUTOBOOT_ONE) { + widget_set_visible(wf, true); + widget_move(wf, y, screen->field_x + 3); + y += widget_height(wf); + } else { + widget_set_visible(wf, false); + } + + y += 1; - y += 2; + wf = widget_textbox_base(screen->widgets.timeout_f); + wl = widget_label_base(screen->widgets.timeout_l); + wh = widget_label_base(screen->widgets.timeout_help_l); + if (screen->autoboot_type != AUTOBOOT_DISABLED) { + widget_set_visible(wl, true); + widget_set_visible(wf, true); + widget_set_visible(wh, true); + widget_move(wl, y, screen->label_x); + widget_move(wf, y, screen->field_x); + widget_move(wh, y, screen->field_x + widget_width(wf) + 1); + y += 2; + } else { + widget_set_visible(wl, false); + widget_set_visible(wf, false); + widget_set_visible(wh, false); + } y += layout_pair(screen, y, screen->widgets.network_l, widget_select_base(screen->widgets.network_f)); @@ -317,8 +400,8 @@ static void config_screen_layout_widgets(struct config_screen *screen, 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; + show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE || + screen->net_conf_type == NET_CONF_TYPE_STATIC; widget_set_visible(wl, show); widget_set_visible(wf, show); @@ -327,7 +410,7 @@ static void config_screen_layout_widgets(struct config_screen *screen, y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1; /* conditionally show static IP params */ - show = net_conf == NET_CONF_TYPE_STATIC; + show = screen->net_conf_type == NET_CONF_TYPE_STATIC; wl = widget_label_base(screen->widgets.ip_addr_l); wf = widget_textbox_base(screen->widgets.ip_addr_f); @@ -376,7 +459,7 @@ static void config_screen_layout_widgets(struct config_screen *screen, y++; /* we show the DNS/DHCP help if we're configuring DHCP */ - show = net_conf != NET_CONF_TYPE_STATIC; + show = screen->net_conf_type != NET_CONF_TYPE_STATIC; wl = widget_label_base(screen->widgets.dns_dhcp_help_l); widget_set_visible(wl, show); if (show) { @@ -388,8 +471,10 @@ static void config_screen_layout_widgets(struct config_screen *screen, widget_move(widget_button_base(screen->widgets.ok_b), y, screen->field_x); - widget_move(widget_button_base(screen->widgets.cancel_b), + widget_move(widget_button_base(screen->widgets.help_b), y, screen->field_x + 10); + widget_move(widget_button_base(screen->widgets.cancel_b), + y, screen->field_x + 20); } static void config_screen_network_change(void *arg, int value) @@ -397,7 +482,16 @@ 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); + config_screen_layout_widgets(screen); + widgetset_post(screen->widgetset); +} + +static void config_screen_autoboot_change(void *arg, int value) +{ + struct config_screen *screen = arg; + screen->autoboot_type = value; + widgetset_unpost(screen->widgetset); + config_screen_layout_widgets(screen); widgetset_post(screen->widgetset); } @@ -451,6 +545,7 @@ static void config_screen_setup_widgets(struct config_screen *screen, char *str, *ip, *mask, *gw; enum net_conf_type type; unsigned int i; + bool found; build_assert(sizeof(screen->widgets) / sizeof(struct widget *) == N_FIELDS); @@ -459,14 +554,84 @@ static void config_screen_setup_widgets(struct config_screen *screen, 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); + screen->widgets.autoboot_f = widget_new_select(set, 0, 0, 55); + + widget_select_on_change(screen->widgets.autoboot_f, + config_screen_autoboot_change, screen); + + screen->widgets.boot_device_f = widget_new_select(set, 0, 0, 55); + + widget_select_add_option(screen->widgets.autoboot_f, + AUTOBOOT_DISABLED, + "Don't autoboot", + screen->autoboot_type == + AUTOBOOT_DISABLED); + widget_select_add_option(screen->widgets.autoboot_f, + AUTOBOOT_ANY, + "Autoboot from any disk/network device", + screen->autoboot_type == + AUTOBOOT_ANY); + widget_select_add_option(screen->widgets.autoboot_f, + AUTOBOOT_ONE, + "Only autoboot from a specific " + "disk/network device", + screen->autoboot_type == + AUTOBOOT_ONE); + + found = false; + + for (i = 0; i < sysinfo->n_blockdevs; i++) { + struct blockdev_info *bd = sysinfo->blockdevs[i]; + bool selected; + char *label; + + selected = config->boot_device && + !strcmp(config->boot_device, bd->uuid); + if (selected) + found = true; + + label = talloc_asprintf(screen, "disk: %s [uuid: %s]", + bd->name, bd->uuid); + + widget_select_add_option(screen->widgets.boot_device_f, i, + label, selected); + } + + for (i = 0; i < sysinfo->n_interfaces; i++) { + struct interface_info *info = sysinfo->interfaces[i]; + char *label, mac[20]; + bool selected; + + mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac)); + selected = config->boot_device && + !strcmp(config->boot_device, mac); + if (selected) + found = true; + + label = talloc_asprintf(screen, "net: %s [mac: %s]", + info->name, mac); + + widget_select_add_option(screen->widgets.boot_device_f, + i + sysinfo->n_blockdevs, + label, selected); + } + + if (screen->autoboot_type == AUTOBOOT_ONE && !found) { + char *label; + + label = talloc_asprintf(screen, "Unknown UUID: %s", + config->boot_device); + + widget_select_add_option(screen->widgets.boot_device_f, -1, + label, true); + } 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_fixed_size(screen->widgets.timeout_f); widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999); screen->widgets.network_l = widget_new_label(set, 0, 0, "Network"); @@ -525,10 +690,12 @@ 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, 4, mask); + 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)"); + widget_textbox_set_fixed_size(screen->widgets.ip_addr_f); + widget_textbox_set_fixed_size(screen->widgets.ip_mask_f); widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f); widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31); @@ -537,6 +704,7 @@ static void config_screen_setup_widgets(struct config_screen *screen, screen->widgets.gateway_help_l = widget_new_label(set, 0, 0, "(eg. 192.168.0.1)"); + widget_textbox_set_fixed_size(screen->widgets.gateway_f); widget_textbox_set_validator_ipv4(screen->widgets.gateway_f); str = talloc_strdup(screen, ""); @@ -558,6 +726,8 @@ static void config_screen_setup_widgets(struct config_screen *screen, screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK", ok_click, screen); + screen->widgets.help_b = widget_new_button(set, 0, 0, 6, "Help", + help_click, screen); screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel", cancel_click, screen); } @@ -620,9 +790,14 @@ static void config_screen_draw(struct config_screen *screen, config_screen_setup_empty(screen); } else { screen->net_conf_type = find_net_conf_type(config); + if (!config->autoboot_enabled) + screen->autoboot_type = AUTOBOOT_DISABLED; + else + screen->autoboot_type = config->boot_device ? + AUTOBOOT_ONE : AUTOBOOT_ANY; config_screen_setup_widgets(screen, config, sysinfo); - config_screen_layout_widgets(screen, screen->net_conf_type); + config_screen_layout_widgets(screen); } if (repost) @@ -668,7 +843,7 @@ struct config_screen *config_screen_init(struct cui *cui, "Petitboot System Configuration"); screen->scr.frame.rtitle = NULL; screen->scr.frame.help = talloc_strdup(screen, - "tab=next, shift+tab=previous"); + "tab=next, shift+tab=previous, x=exit, h=help"); nc_scr_frame_draw(&screen->scr); scrollok(screen->scr.sub_ncw, true);