2 * Copyright (C) 2013 IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #if defined(HAVE_CONFIG_H)
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
32 #include "nc-config.h"
33 #include "nc-widgets.h"
37 extern const char *config_help_text;
40 NET_CONF_TYPE_DHCP_ALL,
41 NET_CONF_TYPE_DHCP_ONE,
45 struct config_screen {
48 struct nc_widgetset *widgetset;
53 void (*on_exit)(struct cui *);
61 enum net_conf_type net_conf_type;
64 struct nc_widget_checkbox *autoboot_f;
65 struct nc_widget_label *autoboot_l;
66 struct nc_widget_textbox *timeout_f;
67 struct nc_widget_label *timeout_l;
68 struct nc_widget_label *timeout_help_l;
70 struct nc_widget_label *network_l;
71 struct nc_widget_select *network_f;
73 struct nc_widget_label *iface_l;
74 struct nc_widget_select *iface_f;
75 struct nc_widget_label *ip_addr_l;
76 struct nc_widget_textbox *ip_addr_f;
77 struct nc_widget_label *ip_mask_l;
78 struct nc_widget_textbox *ip_mask_f;
79 struct nc_widget_label *ip_addr_mask_help_l;
80 struct nc_widget_label *gateway_l;
81 struct nc_widget_textbox *gateway_f;
82 struct nc_widget_label *gateway_help_l;
83 struct nc_widget_label *dns_l;
84 struct nc_widget_textbox *dns_f;
85 struct nc_widget_label *dns_dhcp_help_l;
86 struct nc_widget_label *dns_help_l;
88 struct nc_widget_button *ok_b;
89 struct nc_widget_button *help_b;
90 struct nc_widget_button *cancel_b;
94 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
96 struct config_screen *config_screen;
98 assert(scr->sig == pb_config_screen_sig);
99 config_screen = (struct config_screen *)
100 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
101 assert(config_screen->scr.sig == pb_config_screen_sig);
102 return config_screen;
105 static void pad_refresh(struct config_screen *screen)
107 int y, x, rows, cols;
109 getmaxyx(screen->scr.sub_ncw, rows, cols);
110 getbegyx(screen->scr.sub_ncw, y, x);
112 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
115 static void config_screen_process_key(struct nc_scr *scr, int key)
117 struct config_screen *screen = config_screen_from_scr(scr);
120 handled = widgetset_process_key(screen->widgetset, key);
129 screen->show_help = true;
135 screen->on_exit(screen->cui);
137 } else if (screen->show_help) {
138 screen->show_help = false;
139 cui_show_help(screen->cui, "System Configuration",
142 } else if (handled) {
147 static void config_screen_resize(struct nc_scr *scr)
149 struct config_screen *screen = config_screen_from_scr(scr);
153 static int config_screen_post(struct nc_scr *scr)
155 struct config_screen *screen = config_screen_from_scr(scr);
156 widgetset_post(screen->widgetset);
157 nc_scr_frame_draw(scr);
158 redrawwin(scr->main_ncw);
159 wrefresh(screen->scr.main_ncw);
164 static int config_screen_unpost(struct nc_scr *scr)
166 struct config_screen *screen = config_screen_from_scr(scr);
167 widgetset_unpost(screen->widgetset);
171 struct nc_scr *config_screen_scr(struct config_screen *screen)
176 static int screen_process_form(struct config_screen *screen)
178 const struct system_info *sysinfo = screen->cui->sysinfo;
179 enum net_conf_type net_conf_type;
180 struct interface_config *iface;
181 struct config *config;
185 config = config_copy(screen, screen->cui->config);
187 config->autoboot_enabled =
188 widget_checkbox_get_value(screen->widgets.autoboot_f);
191 str = widget_textbox_get_value(screen->widgets.timeout_f);
195 x = strtoul(str, &end, 10);
196 if (!errno && end != str)
197 config->autoboot_timeout_sec = x;
200 net_conf_type = widget_select_get_value(screen->widgets.network_f);
202 /* if we don't have any network interfaces, prevent per-interface
204 if (sysinfo->n_interfaces == 0)
205 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
207 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
208 config->network.n_interfaces = 0;
213 iface = talloc_zero(config, struct interface_config);
214 config->network.n_interfaces = 1;
215 config->network.interfaces = talloc_array(config,
216 struct interface_config *, 1);
217 config->network.interfaces[0] = iface;
219 /* copy hwaddr (from the sysinfo interface data) to
220 * the configuration */
221 idx = widget_select_get_value(screen->widgets.iface_f);
222 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
223 sizeof(iface->hwaddr));
226 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
227 iface->method = CONFIG_METHOD_DHCP;
230 if (net_conf_type == NET_CONF_TYPE_STATIC) {
231 char *ip, *mask, *gateway;
233 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
234 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
235 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
237 if (!ip || !*ip || !mask || !*mask) {
238 screen->scr.frame.status =
239 "No IP / mask values are set";
240 nc_scr_frame_draw(&screen->scr);
245 iface->method = CONFIG_METHOD_STATIC;
246 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
248 iface->static_config.gateway = talloc_strdup(iface, gateway);
251 str = widget_textbox_get_value(screen->widgets.dns_f);
252 if (str && strlen(str)) {
257 dns = strtok_r(str, " \t", &tmp);
262 i = config->network.n_dns_servers++;
263 config->network.dns_servers = talloc_realloc(config,
264 config->network.dns_servers,
266 config->network.n_dns_servers);
267 config->network.dns_servers[i] =
268 talloc_strdup(config, dns);
274 rc = cui_send_config(screen->cui, config);
278 pb_log("cui_send_config failed!\n");
280 pb_debug("config sent!\n");
285 static void ok_click(void *arg)
287 struct config_screen *screen = arg;
288 if (screen_process_form(screen))
289 /* errors are written to the status line, so we'll need
291 wrefresh(screen->scr.main_ncw);
296 static void help_click(void *arg)
298 struct config_screen *screen = arg;
299 screen->show_help = true;
302 static void cancel_click(void *arg)
304 struct config_screen *screen = arg;
308 static int layout_pair(struct config_screen *screen, int y,
309 struct nc_widget_label *label,
310 struct nc_widget *field)
312 struct nc_widget *label_w = widget_label_base(label);
313 widget_move(label_w, y, screen->label_x);
314 widget_move(field, y, screen->field_x);
315 return max(widget_height(label_w), widget_height(field));
318 static void config_screen_layout_widgets(struct config_screen *screen,
319 enum net_conf_type net_conf)
321 struct nc_widget *wl, *wf, *wh;
326 help_x = screen->field_x + 2 +
327 widget_width(widget_textbox_base(screen->widgets.dns_f));
329 y += layout_pair(screen, y, screen->widgets.autoboot_l,
330 widget_checkbox_base(screen->widgets.autoboot_f));
332 wf = widget_textbox_base(screen->widgets.timeout_f);
333 widget_move(widget_label_base(screen->widgets.timeout_l),
335 widget_move(wf, y, screen->field_x);
336 widget_move(widget_label_base(screen->widgets.timeout_help_l),
337 y, screen->field_x + widget_width(wf) + 1);
341 y += layout_pair(screen, y, screen->widgets.network_l,
342 widget_select_base(screen->widgets.network_f));
346 /* conditionally show iface select */
347 wl = widget_label_base(screen->widgets.iface_l);
348 wf = widget_select_base(screen->widgets.iface_f);
350 show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
351 net_conf == NET_CONF_TYPE_STATIC;
353 widget_set_visible(wl, show);
354 widget_set_visible(wf, show);
357 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
359 /* conditionally show static IP params */
360 show = net_conf == NET_CONF_TYPE_STATIC;
362 wl = widget_label_base(screen->widgets.ip_addr_l);
363 wf = widget_textbox_base(screen->widgets.ip_addr_f);
364 widget_set_visible(wl, show);
365 widget_set_visible(wf, show);
366 x = screen->field_x + widget_width(wf) + 1;
369 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
371 wl = widget_label_base(screen->widgets.ip_mask_l);
372 wf = widget_textbox_base(screen->widgets.ip_mask_f);
373 widget_set_visible(wl, show);
374 widget_set_visible(wf, show);
377 widget_move(wl, y, x);
378 widget_move(wf, y, x + 2);
381 /* help for IP/mask */
382 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
383 widget_set_visible(wh, show);
385 widget_move(wh, y, help_x);
389 wl = widget_label_base(screen->widgets.gateway_l);
390 wf = widget_textbox_base(screen->widgets.gateway_f);
391 wh = widget_label_base(screen->widgets.gateway_help_l);
392 widget_set_visible(wl, show);
393 widget_set_visible(wf, show);
394 widget_set_visible(wh, show);
397 layout_pair(screen, y, screen->widgets.gateway_l, wf);
398 widget_move(wh, y, help_x);
402 wh = widget_label_base(screen->widgets.dns_help_l);
403 layout_pair(screen, y, screen->widgets.dns_l,
404 widget_textbox_base(screen->widgets.dns_f));
405 widget_move(wh, y, help_x);
408 /* we show the DNS/DHCP help if we're configuring DHCP */
409 show = net_conf != NET_CONF_TYPE_STATIC;
410 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
411 widget_set_visible(wl, show);
413 widget_move(wl, y, screen->field_x);
419 widget_move(widget_button_base(screen->widgets.ok_b),
421 widget_move(widget_button_base(screen->widgets.help_b),
422 y, screen->field_x + 10);
423 widget_move(widget_button_base(screen->widgets.cancel_b),
424 y, screen->field_x + 20);
427 static void config_screen_network_change(void *arg, int value)
429 struct config_screen *screen = arg;
430 screen->net_conf_type = value;
431 widgetset_unpost(screen->widgetset);
432 config_screen_layout_widgets(screen, value);
433 widgetset_post(screen->widgetset);
436 static struct interface_config *first_active_interface(
437 const struct config *config)
441 for (i = 0; i < config->network.n_interfaces; i++) {
442 if (config->network.interfaces[i]->ignore)
444 return config->network.interfaces[i];
449 static enum net_conf_type find_net_conf_type(const struct config *config)
451 struct interface_config *ifcfg;
453 ifcfg = first_active_interface(config);
456 return NET_CONF_TYPE_DHCP_ALL;
458 else if (ifcfg->method == CONFIG_METHOD_DHCP)
459 return NET_CONF_TYPE_DHCP_ONE;
461 else if (ifcfg->method == CONFIG_METHOD_STATIC)
462 return NET_CONF_TYPE_STATIC;
465 return NET_CONF_TYPE_DHCP_ALL;
468 static void config_screen_setup_empty(struct config_screen *screen)
470 widget_new_label(screen->widgetset, 2, screen->field_x,
471 "Waiting for configuration data...");
472 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
473 4, screen->field_x, 6, "Cancel", cancel_click, screen);
477 static void config_screen_setup_widgets(struct config_screen *screen,
478 const struct config *config,
479 const struct system_info *sysinfo)
481 struct nc_widgetset *set = screen->widgetset;
482 struct interface_config *ifcfg;
483 char *str, *ip, *mask, *gw;
484 enum net_conf_type type;
487 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
490 type = screen->net_conf_type;
491 ifcfg = first_active_interface(config);
493 screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
494 screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
495 config->autoboot_enabled);
497 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
498 screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
499 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
500 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds");
502 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
504 screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
505 screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
507 widget_select_add_option(screen->widgets.network_f,
508 NET_CONF_TYPE_DHCP_ALL,
509 "DHCP on all active interfaces",
510 type == NET_CONF_TYPE_DHCP_ALL);
511 widget_select_add_option(screen->widgets.network_f,
512 NET_CONF_TYPE_DHCP_ONE,
513 "DHCP on a specific interface",
514 type == NET_CONF_TYPE_DHCP_ONE);
515 widget_select_add_option(screen->widgets.network_f,
516 NET_CONF_TYPE_STATIC,
517 "Static IP configuration",
518 type == NET_CONF_TYPE_STATIC);
520 widget_select_on_change(screen->widgets.network_f,
521 config_screen_network_change, screen);
523 screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
524 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
526 for (i = 0; i < sysinfo->n_interfaces; i++) {
527 struct interface_info *info = sysinfo->interfaces[i];
528 char str[50], mac[20];
531 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
532 sizeof(ifcfg->hwaddr));
534 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
535 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
536 info->link ? "link up" : "link down");
538 widget_select_add_option(screen->widgets.iface_f,
542 gw = ip = mask = NULL;
543 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
546 str = talloc_strdup(screen, ifcfg->static_config.address);
547 sep = strchr(str, '/');
554 gw = ifcfg->static_config.gateway;
557 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
558 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
559 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
560 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 4, mask);
561 screen->widgets.ip_addr_mask_help_l =
562 widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)");
564 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
565 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
567 screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
568 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
569 screen->widgets.gateway_help_l =
570 widget_new_label(set, 0, 0, "(eg. 192.168.0.1)");
572 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
574 str = talloc_strdup(screen, "");
575 for (i = 0; i < config->network.n_dns_servers; i++) {
576 str = talloc_asprintf_append(str, "%s%s",
578 config->network.dns_servers[i]);
581 screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
582 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
583 screen->widgets.dns_help_l =
584 widget_new_label(set, 0, 0, "(eg. 192.168.0.2)");
586 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
588 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
589 "(if not provided by DHCP server)");
591 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
593 screen->widgets.help_b = widget_new_button(set, 0, 0, 6, "Help",
595 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
596 cancel_click, screen);
599 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
601 struct config_screen *screen = arg;
604 w_y = widget_y(widget) + widget_focus_y(widget);
605 s_max = getmaxy(screen->scr.sub_ncw) - 1;
607 if (w_y < screen->scroll_y)
608 screen->scroll_y = w_y;
610 else if (w_y + screen->scroll_y + 1 > s_max)
611 screen->scroll_y = 1 + w_y - s_max;
619 static void config_screen_draw(struct config_screen *screen,
620 const struct config *config,
621 const struct system_info *sysinfo)
626 /* The size of the pad we'll need depends on the number of interfaces.
628 * We use N_FIELDS (which is quite conservative, as some fields share
629 * a line) as a base, then add 3 (as the network select field is
630 * takes 3 lines), and n_interfaces (as the network interface field
631 * has n_interfaces lines).
633 height = N_FIELDS + 3;
635 height += sysinfo->n_interfaces;
636 if (!screen->pad || getmaxy(screen->pad) < height) {
639 screen->pad = newpad(height, COLS);
642 if (screen->widgetset) {
643 widgetset_unpost(screen->widgetset);
644 talloc_free(screen->widgetset);
648 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
650 widgetset_set_widget_focus(screen->widgetset,
651 config_screen_widget_focus, screen);
653 if (!config || !sysinfo) {
654 config_screen_setup_empty(screen);
656 screen->net_conf_type = find_net_conf_type(config);
658 config_screen_setup_widgets(screen, config, sysinfo);
659 config_screen_layout_widgets(screen, screen->net_conf_type);
663 widgetset_post(screen->widgetset);
666 void config_screen_update(struct config_screen *screen,
667 const struct config *config,
668 const struct system_info *sysinfo)
670 config_screen_draw(screen, config, sysinfo);
674 static int config_screen_destroy(void *arg)
676 struct config_screen *screen = arg;
682 struct config_screen *config_screen_init(struct cui *cui,
683 const struct config *config,
684 const struct system_info *sysinfo,
685 void (*on_exit)(struct cui *))
687 struct config_screen *screen;
689 screen = talloc_zero(cui, struct config_screen);
690 talloc_set_destructor(screen, config_screen_destroy);
691 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
692 cui, config_screen_process_key,
693 config_screen_post, config_screen_unpost,
694 config_screen_resize);
697 screen->on_exit = on_exit;
699 screen->field_x = 17;
701 screen->scr.frame.ltitle = talloc_strdup(screen,
702 "Petitboot System Configuration");
703 screen->scr.frame.rtitle = NULL;
704 screen->scr.frame.help = talloc_strdup(screen,
705 "tab=next, shift+tab=previous, x=exit, h=help");
706 nc_scr_frame_draw(&screen->scr);
708 scrollok(screen->scr.sub_ncw, true);
710 config_screen_draw(screen, config, sysinfo);