ui/ncurses: Allow text wrapping in select widgets
[petitboot] / ui / ncurses / nc-config.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
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.
7  *
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.
12  *
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
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
29 #include <log/log.h>
30 #include <i18n/i18n.h>
31
32 #include "nc-cui.h"
33 #include "nc-config.h"
34 #include "nc-widgets.h"
35
36 #define N_FIELDS        34
37
38 extern struct help_text config_help_text;
39
40 enum net_conf_type {
41         NET_CONF_TYPE_DHCP_ALL,
42         NET_CONF_TYPE_DHCP_ONE,
43         NET_CONF_TYPE_STATIC,
44 };
45
46 struct config_screen {
47         struct nc_scr           scr;
48         struct cui              *cui;
49         struct nc_widgetset     *widgetset;
50         WINDOW                  *pad;
51
52         bool                    exit;
53         bool                    show_help;
54         bool                    show_subset;
55         bool                    need_redraw;
56         bool                    need_update;
57
58         void                    (*on_exit)(struct cui *);
59
60         int                     scroll_y;
61
62         int                     label_x;
63         int                     field_x;
64         int                     network_config_y;
65
66         enum net_conf_type      net_conf_type;
67
68         bool                    autoboot_enabled;
69         bool                    ipmi_override;
70
71         struct {
72                 struct nc_widget_label          *boot_order_l;
73                 struct nc_widget_subset         *boot_order_f;
74                 struct nc_widget_label          *boot_empty_l;
75                 struct nc_widget_button         *boot_add_b;
76                 struct nc_widget_button         *boot_none_b;
77                 struct nc_widget_button         *boot_any_b;
78                 struct nc_widget_textbox        *timeout_f;
79                 struct nc_widget_label          *timeout_l;
80                 struct nc_widget_label          *timeout_help_l;
81
82                 struct nc_widget_label          *ipmi_type_l;
83                 struct nc_widget_label          *ipmi_clear_l;
84                 struct nc_widget_checkbox       *ipmi_clear_cb;
85
86                 struct nc_widget_label          *network_l;
87                 struct nc_widget_select         *network_f;
88
89                 struct nc_widget_label          *iface_l;
90                 struct nc_widget_select         *iface_f;
91                 struct nc_widget_label          *ip_addr_l;
92                 struct nc_widget_textbox        *ip_addr_f;
93                 struct nc_widget_label          *ip_mask_l;
94                 struct nc_widget_textbox        *ip_mask_f;
95                 struct nc_widget_label          *ip_addr_mask_help_l;
96                 struct nc_widget_label          *gateway_l;
97                 struct nc_widget_textbox        *gateway_f;
98                 struct nc_widget_label          *gateway_help_l;
99                 struct nc_widget_label          *dns_l;
100                 struct nc_widget_textbox        *dns_f;
101                 struct nc_widget_label          *dns_dhcp_help_l;
102                 struct nc_widget_label          *dns_help_l;
103
104                 struct nc_widget_label          *allow_write_l;
105                 struct nc_widget_select         *allow_write_f;
106
107                 struct nc_widget_label          *safe_mode;
108                 struct nc_widget_button         *ok_b;
109                 struct nc_widget_button         *help_b;
110                 struct nc_widget_button         *cancel_b;
111         } widgets;
112 };
113
114 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
115 {
116         struct config_screen *config_screen;
117
118         assert(scr->sig == pb_config_screen_sig);
119         config_screen = (struct config_screen *)
120                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
121         assert(config_screen->scr.sig == pb_config_screen_sig);
122         return config_screen;
123 }
124
125 static void pad_refresh(struct config_screen *screen)
126 {
127         int y, x, rows, cols;
128
129         getmaxyx(screen->scr.sub_ncw, rows, cols);
130         getbegyx(screen->scr.sub_ncw, y, x);
131
132         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
133 }
134
135 static void config_screen_process_key(struct nc_scr *scr, int key)
136 {
137         struct config_screen *screen = config_screen_from_scr(scr);
138         bool handled;
139
140         handled = widgetset_process_key(screen->widgetset, key);
141
142         if (!handled) {
143                 switch (key) {
144                 case 'x':
145                 case 27: /* esc */
146                         screen->exit = true;
147                         break;
148                 case 'h':
149                         screen->show_help = true;
150                         break;
151                 }
152         }
153
154         if (screen->exit) {
155                 screen->on_exit(screen->cui);
156
157         } else if (screen->show_help) {
158                 screen->show_help = false;
159                 screen->need_redraw = true;
160                 cui_show_help(screen->cui, _("System Configuration"),
161                                 &config_help_text);
162
163         } else if (handled && !screen->show_subset) {
164                 pad_refresh(screen);
165         }
166 }
167
168 static void config_screen_resize(struct nc_scr *scr)
169 {
170         struct config_screen *screen = config_screen_from_scr(scr);
171         (void)screen;
172 }
173
174 static int config_screen_unpost(struct nc_scr *scr)
175 {
176         struct config_screen *screen = config_screen_from_scr(scr);
177         widgetset_unpost(screen->widgetset);
178         return 0;
179 }
180
181 struct nc_scr *config_screen_scr(struct config_screen *screen)
182 {
183         return &screen->scr;
184 }
185
186 static int screen_process_form(struct config_screen *screen)
187 {
188         const struct system_info *sysinfo = screen->cui->sysinfo;
189         enum net_conf_type net_conf_type;
190         struct interface_config *iface;
191         char *str, *end;
192         struct config *config;
193         int i, n_boot_opts, rc, idx;
194         unsigned int *order;
195         bool allow_write;
196         char mac[20];
197
198         config = config_copy(screen, screen->cui->config);
199
200         talloc_free(config->autoboot_opts);
201         config->n_autoboot_opts = 0;
202
203         n_boot_opts = widget_subset_get_order(config, &order,
204                                               screen->widgets.boot_order_f);
205
206         config->autoboot_enabled = n_boot_opts > 0;
207
208         config->n_autoboot_opts = n_boot_opts;
209         config->autoboot_opts = talloc_array(config, struct autoboot_option,
210                                              n_boot_opts);
211
212         for (i = 0; i < n_boot_opts; i++) {
213                 if (order[i] < sysinfo->n_blockdevs) {
214                         /* disk uuid */
215                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
216                         config->autoboot_opts[i].uuid = talloc_strdup(config,
217                                                         sysinfo->blockdevs[order[i]]->uuid);
218                 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
219                         /* net uuid */
220                         order[i] -= sysinfo->n_blockdevs;
221                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
222                         mac_str(sysinfo->interfaces[order[i]]->hwaddr,
223                                 sysinfo->interfaces[order[i]]->hwaddr_size,
224                                 mac, sizeof(mac));
225                         config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
226                 } else {
227                         /* device type */
228                         order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
229                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
230                         config->autoboot_opts[i].type = order[i];
231                 }
232         }
233
234         str = widget_textbox_get_value(screen->widgets.timeout_f);
235         if (str) {
236                 unsigned long x;
237                 errno = 0;
238                 x = strtoul(str, &end, 10);
239                 if (!errno && end != str)
240                         config->autoboot_timeout_sec = x;
241         }
242
243         if (screen->ipmi_override)
244                 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
245                         config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
246
247
248         net_conf_type = widget_select_get_value(screen->widgets.network_f);
249
250         /* if we don't have any network interfaces, prevent per-interface
251          * configuration */
252         if (sysinfo->n_interfaces == 0)
253                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
254
255         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
256                 config->network.n_interfaces = 0;
257
258         } else {
259                 iface = talloc_zero(config, struct interface_config);
260                 config->network.n_interfaces = 1;
261                 config->network.interfaces = talloc_array(config,
262                                 struct interface_config *, 1);
263                 config->network.interfaces[0] = iface;
264
265                 /* copy hwaddr (from the sysinfo interface data) to
266                  * the configuration */
267                 idx = widget_select_get_value(screen->widgets.iface_f);
268                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
269                                 sizeof(iface->hwaddr));
270         }
271
272         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
273                 iface->method = CONFIG_METHOD_DHCP;
274         }
275
276         if (net_conf_type == NET_CONF_TYPE_STATIC) {
277                 char *ip, *mask, *gateway;
278
279                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
280                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
281                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
282
283                 if (!ip || !*ip || !mask || !*mask) {
284                         screen->scr.frame.status =
285                                 _("No IP / mask values are set");
286                         nc_scr_frame_draw(&screen->scr);
287                         talloc_free(config);
288                         return -1;
289                 }
290
291                 iface->method = CONFIG_METHOD_STATIC;
292                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
293                                 ip, mask);
294                 iface->static_config.gateway = talloc_strdup(iface, gateway);
295         }
296
297         str = widget_textbox_get_value(screen->widgets.dns_f);
298         talloc_free(config->network.dns_servers);
299         config->network.dns_servers = NULL;
300         config->network.n_dns_servers = 0;
301
302         if (str && strlen(str)) {
303                 char *dns, *tmp;
304                 int i;
305
306                 for (;;) {
307                         dns = strtok_r(str, " \t", &tmp);
308
309                         if (!dns)
310                                 break;
311
312                         i = config->network.n_dns_servers++;
313                         config->network.dns_servers = talloc_realloc(config,
314                                         config->network.dns_servers,
315                                         const char *,
316                                         config->network.n_dns_servers);
317                         config->network.dns_servers[i] =
318                                 talloc_strdup(config, dns);
319
320                         str = NULL;
321                 }
322         }
323
324         allow_write = widget_select_get_value(screen->widgets.allow_write_f);
325         if (allow_write != config->allow_writes)
326                 config->allow_writes = allow_write;
327
328         config->safe_mode = false;
329         rc = cui_send_config(screen->cui, config);
330         talloc_free(config);
331
332         if (rc)
333                 pb_log("cui_send_config failed!\n");
334         else
335                 pb_debug("config sent!\n");
336
337         return 0;
338 }
339
340 static void ok_click(void *arg)
341 {
342         struct config_screen *screen = arg;
343         if (screen_process_form(screen))
344                 /* errors are written to the status line, so we'll need
345                  * to refresh */
346                 wrefresh(screen->scr.main_ncw);
347         else
348                 screen->exit = true;
349 }
350
351 static void help_click(void *arg)
352 {
353         struct config_screen *screen = arg;
354         screen->show_help = true;
355 }
356
357 static void cancel_click(void *arg)
358 {
359         struct config_screen *screen = arg;
360         screen->exit = true;
361 }
362
363 static int layout_pair(struct config_screen *screen, int y,
364                 struct nc_widget_label *label,
365                 struct nc_widget *field)
366 {
367         struct nc_widget *label_w = widget_label_base(label);
368         widget_move(label_w, y, screen->label_x);
369         widget_move(field, y, screen->field_x);
370         return max(widget_height(label_w), widget_height(field));
371 }
372
373 static void config_screen_layout_widgets(struct config_screen *screen)
374 {
375         struct nc_widget *wl, *wf, *wh;
376         int y, x, help_x;
377         bool show;
378
379         y = 1;
380         /* currently, the longest label we have is the DNS-servers
381          * widget, so layout our screen based on that */
382         help_x = screen->field_x + 2 +
383                 widget_width(widget_textbox_base(screen->widgets.dns_f));
384
385         wl = widget_label_base(screen->widgets.boot_order_l);
386         widget_set_visible(wl, true);
387         widget_move(wl, y, screen->label_x);
388
389         wf = widget_subset_base(screen->widgets.boot_order_f);
390         widget_move(wf, y, screen->field_x);
391         wl = widget_label_base(screen->widgets.boot_empty_l);
392         widget_move(wl, y, screen->field_x);
393
394         if (widget_subset_height(screen->widgets.boot_order_f)) {
395                 widget_set_visible(wf, true);
396                 widget_set_visible(wl, false);
397                 y += widget_height(wf);
398         } else {
399                 widget_set_visible(wl, true);
400                 widget_set_visible(wf, false);
401                 y += 1;
402         }
403
404         y += 1;
405
406         widget_move(widget_button_base(screen->widgets.boot_add_b),
407                         y++, screen->field_x);
408         widget_move(widget_button_base(screen->widgets.boot_any_b),
409                         y++, screen->field_x);
410         widget_move(widget_button_base(screen->widgets.boot_none_b),
411                         y, screen->field_x);
412
413         wf = widget_button_base(screen->widgets.boot_add_b);
414         if (widget_subset_n_inactive(screen->widgets.boot_order_f))
415                 widget_set_visible(wf, true);
416         else
417                 widget_set_visible(wf, false);
418
419         y += 2;
420
421         wf = widget_textbox_base(screen->widgets.timeout_f);
422         wl = widget_label_base(screen->widgets.timeout_l);
423         wh = widget_label_base(screen->widgets.timeout_help_l);
424         widget_set_visible(wl, screen->autoboot_enabled);
425         widget_set_visible(wf, screen->autoboot_enabled);
426         widget_set_visible(wh, screen->autoboot_enabled);
427         if (screen->autoboot_enabled) {
428                 widget_set_visible(wh, screen->autoboot_enabled);
429                 widget_move(wl, y, screen->label_x);
430                 widget_move(wf, y, screen->field_x);
431                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
432                 y += 2;
433         }
434
435         if (screen->ipmi_override) {
436                 wl = widget_label_base(screen->widgets.ipmi_type_l);
437                 widget_set_visible(wl, true);
438                 widget_move(wl, y, screen->label_x);
439                 y += 1;
440
441                 wl = widget_label_base(screen->widgets.ipmi_clear_l);
442                 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
443                 widget_set_visible(wl, true);
444                 widget_set_visible(wf, true);
445                 widget_move(wl, y, screen->label_x);
446                 widget_move(wf, y, screen->field_x);
447                 y += 1;
448         }
449
450         y += 1;
451
452         y += layout_pair(screen, y, screen->widgets.network_l,
453                         widget_select_base(screen->widgets.network_f));
454
455         y += 1;
456
457         /* conditionally show iface select */
458         wl = widget_label_base(screen->widgets.iface_l);
459         wf = widget_select_base(screen->widgets.iface_f);
460
461         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
462                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
463
464         widget_set_visible(wl, show);
465         widget_set_visible(wf, show);
466
467         if (show)
468                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
469
470         /* conditionally show static IP params */
471         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
472
473         wl = widget_label_base(screen->widgets.ip_addr_l);
474         wf = widget_textbox_base(screen->widgets.ip_addr_f);
475         widget_set_visible(wl, show);
476         widget_set_visible(wf, show);
477         x = screen->field_x + widget_width(wf) + 1;
478
479         if (show)
480                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
481
482         wl = widget_label_base(screen->widgets.ip_mask_l);
483         wf = widget_textbox_base(screen->widgets.ip_mask_f);
484         widget_set_visible(wl, show);
485         widget_set_visible(wf, show);
486
487         if (show) {
488                 widget_move(wl, y, x);
489                 widget_move(wf, y, x + 2);
490         }
491
492         /* help for IP/mask */
493         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
494         widget_set_visible(wh, show);
495         if (show) {
496                 widget_move(wh, y, help_x);
497                 y++;
498         }
499
500         wl = widget_label_base(screen->widgets.gateway_l);
501         wf = widget_textbox_base(screen->widgets.gateway_f);
502         wh = widget_label_base(screen->widgets.gateway_help_l);
503         widget_set_visible(wl, show);
504         widget_set_visible(wf, show);
505         widget_set_visible(wh, show);
506
507         if (show) {
508                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
509                 widget_move(wh, y, help_x);
510                 y++;
511         }
512
513         wh = widget_label_base(screen->widgets.dns_help_l);
514         layout_pair(screen, y, screen->widgets.dns_l,
515                         widget_textbox_base(screen->widgets.dns_f));
516         widget_move(wh, y, help_x);
517         y++;
518
519         /* we show the DNS/DHCP help if we're configuring DHCP */
520         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
521         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
522         widget_set_visible(wl, show);
523         if (show) {
524                 widget_move(wl, y, screen->field_x);
525                 y += 1;
526         }
527
528         y += 1;
529
530         show = screen->cui->config->safe_mode;
531         if (show) {
532                 widget_move(widget_label_base(screen->widgets.safe_mode),
533                         y, screen->field_x);
534                 y += 1;
535         }
536
537         layout_pair(screen, y, screen->widgets.allow_write_l,
538                     widget_select_base(screen->widgets.allow_write_f));
539         y += widget_height(widget_select_base(screen->widgets.allow_write_f));
540
541         y += 1;
542
543         widget_move(widget_button_base(screen->widgets.ok_b),
544                         y, screen->field_x);
545         widget_move(widget_button_base(screen->widgets.help_b),
546                         y, screen->field_x + 14);
547         widget_move(widget_button_base(screen->widgets.cancel_b),
548                         y, screen->field_x + 28);
549 }
550
551 static void config_screen_network_change(void *arg, int value)
552 {
553         struct config_screen *screen = arg;
554         screen->net_conf_type = value;
555         widgetset_unpost(screen->widgetset);
556         config_screen_layout_widgets(screen);
557         widgetset_post(screen->widgetset);
558 }
559
560 static void config_screen_boot_order_change(void *arg, int value)
561 {
562         (void)value;
563         struct config_screen *screen = arg;
564         widgetset_unpost(screen->widgetset);
565         config_screen_layout_widgets(screen);
566         widgetset_post(screen->widgetset);
567 }
568
569 static void config_screen_add_device(void *arg)
570 {
571         struct config_screen *screen = arg;
572
573         screen->show_subset = true;
574         cui_show_subset(screen->cui, _("Select a boot device to add"),
575                         screen->widgets.boot_order_f);
576 }
577
578 static void config_screen_autoboot_none(void *arg)
579 {
580         struct config_screen *screen = arg;
581         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
582
583         widget_subset_clear_active(subset);
584         screen->autoboot_enabled = false;
585
586         widgetset_unpost(screen->widgetset);
587         config_screen_layout_widgets(screen);
588         widgetset_post(screen->widgetset);
589 }
590
591 static void config_screen_autoboot_any(void *arg)
592 {
593         struct config_screen *screen = arg;
594         const struct system_info *sysinfo = screen->cui->sysinfo;
595         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
596         int idx;
597
598         widget_subset_clear_active(subset);
599
600         idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
601
602         widget_subset_make_active(screen->widgets.boot_order_f, idx);
603
604         screen->autoboot_enabled = true;
605
606         widgetset_unpost(screen->widgetset);
607         config_screen_layout_widgets(screen);
608         widgetset_post(screen->widgetset);
609 }
610
611 static void config_screen_update_subset(void *arg,
612                         struct nc_widget_subset *subset, int idx)
613 {
614         struct config_screen *screen = arg;
615
616         if (idx >= 0)
617                 widget_subset_make_active(subset, idx);
618         if (!screen->autoboot_enabled)
619                 screen->autoboot_enabled = true;
620         config_screen_layout_widgets(screen);
621 }
622
623 static struct interface_config *first_active_interface(
624                 const struct config *config)
625 {
626         unsigned int i;
627
628         for (i = 0; i < config->network.n_interfaces; i++) {
629                 if (config->network.interfaces[i]->ignore)
630                         continue;
631                 return config->network.interfaces[i];
632         }
633         return NULL;
634 }
635
636 static enum net_conf_type find_net_conf_type(const struct config *config)
637 {
638         struct interface_config *ifcfg;
639
640         ifcfg = first_active_interface(config);
641
642         if (!ifcfg)
643                 return NET_CONF_TYPE_DHCP_ALL;
644
645         else if (ifcfg->method == CONFIG_METHOD_DHCP)
646                 return NET_CONF_TYPE_DHCP_ONE;
647
648         else if (ifcfg->method == CONFIG_METHOD_STATIC)
649                 return NET_CONF_TYPE_STATIC;
650
651         assert(0);
652         return NET_CONF_TYPE_DHCP_ALL;
653 }
654
655 static void config_screen_setup_empty(struct config_screen *screen)
656 {
657         widget_new_label(screen->widgetset, 2, screen->field_x,
658                         _("Waiting for configuration data..."));
659         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
660                         4, screen->field_x, 9, _("Cancel"),
661                         cancel_click, screen);
662 }
663
664 static int find_autoboot_idx(const struct system_info *sysinfo,
665                 struct autoboot_option *opt)
666 {
667         unsigned int i;
668
669         if (opt->boot_type == BOOT_DEVICE_TYPE)
670                 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
671
672         for (i = 0; i < sysinfo->n_blockdevs; i++) {
673                 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
674                         return i;
675         }
676
677         for (i = 0; i < sysinfo->n_interfaces; i++) {
678                 struct interface_info *info = sysinfo->interfaces[i];
679                 char mac[20];
680
681                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
682
683                 if (!strcmp(mac, opt->uuid))
684                         return sysinfo->n_blockdevs + i;
685         }
686
687         return -1;
688 }
689
690 static void config_screen_setup_widgets(struct config_screen *screen,
691                 const struct config *config,
692                 const struct system_info *sysinfo)
693 {
694         struct nc_widgetset *set = screen->widgetset;
695         struct interface_config *ifcfg;
696         char *str, *ip, *mask, *gw;
697         enum net_conf_type type;
698         unsigned int i;
699         int add_len, clear_len, any_len, min_len = 20;
700
701         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
702                         == N_FIELDS);
703
704         type = screen->net_conf_type;
705         ifcfg = first_active_interface(config);
706
707         add_len = max(min_len, strncols(_("Add Device")));
708         clear_len = max(min_len, strncols(_("Clear")));
709         any_len = max(min_len, strncols(_("Clear & Boot Any")));
710
711         screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
712                                         _("Add Device"),
713                                         config_screen_add_device, screen);
714
715         screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
716                                         _("Clear"),
717                                         config_screen_autoboot_none, screen);
718
719         screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
720                                         _("Clear & Boot Any"),
721                                         config_screen_autoboot_any, screen);
722
723         screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
724                                         _("Boot Order:"));
725         screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
726                                         COLS - screen->field_x,
727                                         config_screen_update_subset);
728         screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
729                                         _("(None)"));
730
731         widget_subset_on_change(screen->widgets.boot_order_f,
732                         config_screen_boot_order_change, screen);
733
734         for (i = 0; i < sysinfo->n_blockdevs; i++) {
735                 struct blockdev_info *bd = sysinfo->blockdevs[i];
736                 char *label;
737
738                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
739                                 bd->name, bd->uuid);
740
741                 widget_subset_add_option(screen->widgets.boot_order_f, label);
742         }
743
744         for (i = 0; i < sysinfo->n_interfaces; i++) {
745                 struct interface_info *info = sysinfo->interfaces[i];
746                 char *label, mac[20];
747
748                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
749
750                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
751                                 info->name, mac);
752
753                 widget_subset_add_option(screen->widgets.boot_order_f, label);
754         }
755
756         for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
757                 char *label;
758
759                 if (i == DEVICE_TYPE_ANY)
760                         label = talloc_asprintf(screen, _("Any Device"));
761                 else
762                         label = talloc_asprintf(screen, _("Any %s device"),
763                                                 device_type_display_name(i));
764
765                 widget_subset_add_option(screen->widgets.boot_order_f, label);
766         }
767
768         screen->autoboot_enabled = config->n_autoboot_opts;
769         for (i = 0; i < config->n_autoboot_opts; i++) {
770                 struct autoboot_option *opt = &config->autoboot_opts[i];
771                 int idx;
772
773                 idx = find_autoboot_idx(sysinfo, opt);
774
775                 if (idx >= 0) {
776                         widget_subset_make_active(screen->widgets.boot_order_f,
777                                                   idx);
778                 } else {
779                         if (opt->boot_type == BOOT_DEVICE_TYPE)
780                                 pb_log("%s: Unknown autoboot option: %d\n",
781                                        __func__, opt->type);
782                         else
783                                 pb_log("%s: Unknown autoboot UUID: %s\n",
784                                        __func__, opt->uuid);
785                 }
786         }
787
788
789         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
790         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
791         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
792         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
793                                         _("seconds"));
794
795         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
796         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
797
798         if (config->ipmi_bootdev) {
799                 char *label = talloc_asprintf(screen,
800                                 _("%s IPMI boot option: %s"),
801                                 config->ipmi_bootdev_persistent ?
802                                 "Persistent" : "Temporary",
803                                 ipmi_bootdev_display_name(config->ipmi_bootdev));
804                 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
805                                                         label);
806                 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
807                                                         _("Clear option:"));
808                 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
809                                                         false);
810                 screen->ipmi_override = true;
811         }
812
813         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
814         screen->widgets.network_f = widget_new_select(set, 0, 0,
815                                                 COLS - screen->field_x - 1);
816
817         widget_select_add_option(screen->widgets.network_f,
818                                         NET_CONF_TYPE_DHCP_ALL,
819                                         _("DHCP on all active interfaces"),
820                                         type == NET_CONF_TYPE_DHCP_ALL);
821         widget_select_add_option(screen->widgets.network_f,
822                                         NET_CONF_TYPE_DHCP_ONE,
823                                         _("DHCP on a specific interface"),
824                                         type == NET_CONF_TYPE_DHCP_ONE);
825         widget_select_add_option(screen->widgets.network_f,
826                                         NET_CONF_TYPE_STATIC,
827                                         _("Static IP configuration"),
828                                         type == NET_CONF_TYPE_STATIC);
829
830         widget_select_on_change(screen->widgets.network_f,
831                         config_screen_network_change, screen);
832
833         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
834         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
835
836         for (i = 0; i < sysinfo->n_interfaces; i++) {
837                 struct interface_info *info = sysinfo->interfaces[i];
838                 char str[50], mac[20];
839                 bool is_default;
840
841                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
842                                         sizeof(ifcfg->hwaddr));
843
844                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
845                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
846                                 info->link ? _("link up") : _("link down"));
847
848                 widget_select_add_option(screen->widgets.iface_f,
849                                                 i, str, is_default);
850         }
851
852         gw = ip = mask = NULL;
853         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
854                 char *sep;
855
856                 str = talloc_strdup(screen, ifcfg->static_config.address);
857                 sep = strchr(str, '/');
858                 ip = str;
859
860                 if (sep) {
861                         *sep = '\0';
862                         mask = sep + 1;
863                 }
864                 gw = ifcfg->static_config.gateway;
865         }
866
867         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
868         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
869         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
870         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
871         screen->widgets.ip_addr_mask_help_l =
872                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
873
874         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
875         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
876         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
877         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
878
879         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
880         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
881         screen->widgets.gateway_help_l =
882                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
883
884         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
885         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
886
887         str = talloc_strdup(screen, "");
888         for (i = 0; i < config->network.n_dns_servers; i++) {
889                 str = talloc_asprintf_append(str, "%s%s",
890                                 (i == 0) ? "" : " ",
891                                 config->network.dns_servers[i]);
892         }
893
894         screen->widgets.dns_l = widget_new_label(set, 0, 0,
895                                         _("DNS Server(s):"));
896         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
897         screen->widgets.dns_help_l =
898                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
899
900         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
901
902         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
903                         _("(if not provided by DHCP server)"));
904
905         if (config->safe_mode)
906                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
907                          _("Selecting 'OK' will exit safe mode"));
908
909         screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
910                         _("Disk R/W:"));
911         screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
912                                                 COLS - screen->field_x - 1);
913
914         widget_select_add_option(screen->widgets.allow_write_f, 0,
915                                 _("Prevent all writes to disk"),
916                                 !config->allow_writes);
917
918         widget_select_add_option(screen->widgets.allow_write_f, 1,
919                                 _("Allow bootloader scripts to modify disks"),
920                                 config->allow_writes);
921
922         screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
923                         ok_click, screen);
924         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
925                         help_click, screen);
926         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
927                         cancel_click, screen);
928 }
929
930 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
931 {
932         struct config_screen *screen = arg;
933         int w_y, s_max;
934
935         w_y = widget_y(widget) + widget_focus_y(widget);
936         s_max = getmaxy(screen->scr.sub_ncw) - 1;
937
938         if (w_y < screen->scroll_y)
939                 screen->scroll_y = w_y;
940
941         else if (w_y + screen->scroll_y + 1 > s_max)
942                 screen->scroll_y = 1 + w_y - s_max;
943
944         else
945                 return;
946
947         pad_refresh(screen);
948 }
949
950 static void config_screen_draw(struct config_screen *screen,
951                 const struct config *config,
952                 const struct system_info *sysinfo)
953 {
954         bool repost = false;
955         int height;
956
957         /* The size of the pad we'll need depends on the number of interfaces.
958          *
959          * We use N_FIELDS (which is quite conservative, as some fields share
960          * a line) as a base, then:
961          * - add 6 (as the network select & boot device select fields take 3
962          *   lines each),
963          * - add n_interfaces for every field in the network select field, and
964          * - add (n_blockdevs + n_interfaces) for every field in the boot device
965          *   select field
966          */
967         height = N_FIELDS + 6;
968         if (sysinfo) {
969                 height += sysinfo->n_interfaces;
970                 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
971         }
972         if (!screen->pad || getmaxy(screen->pad) < height) {
973                 if (screen->pad)
974                         delwin(screen->pad);
975                 screen->pad = newpad(height, COLS + 10);
976         }
977
978         if (screen->widgetset) {
979                 widgetset_unpost(screen->widgetset);
980                 talloc_free(screen->widgetset);
981                 repost = true;
982         }
983
984         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
985                         screen->pad);
986         widgetset_set_widget_focus(screen->widgetset,
987                         config_screen_widget_focus, screen);
988
989         if (!config || !sysinfo) {
990                 config_screen_setup_empty(screen);
991         } else {
992                 screen->net_conf_type = find_net_conf_type(config);
993                 config_screen_setup_widgets(screen, config, sysinfo);
994                 config_screen_layout_widgets(screen);
995         }
996
997         if (repost)
998                 widgetset_post(screen->widgetset);
999 }
1000
1001 void config_screen_update(struct config_screen *screen,
1002                 const struct config *config,
1003                 const struct system_info *sysinfo)
1004 {
1005         if (screen->cui->current != config_screen_scr(screen)) {
1006                 screen->need_update = true;
1007                 return;
1008         }
1009
1010         config_screen_draw(screen, config, sysinfo);
1011         pad_refresh(screen);
1012 }
1013
1014 static int config_screen_post(struct nc_scr *scr)
1015 {
1016         struct config_screen *screen = config_screen_from_scr(scr);
1017         screen->show_subset = false;
1018
1019         if (screen->need_update) {
1020                 config_screen_draw(screen, screen->cui->config,
1021                                    screen->cui->sysinfo);
1022                 screen->need_update = false;
1023         } else {
1024                 widgetset_post(screen->widgetset);
1025         }
1026
1027         nc_scr_frame_draw(scr);
1028         if (screen->need_redraw) {
1029                 redrawwin(scr->main_ncw);
1030                 screen->need_redraw = false;
1031         }
1032         wrefresh(screen->scr.main_ncw);
1033         pad_refresh(screen);
1034         return 0;
1035 }
1036
1037 static int config_screen_destroy(void *arg)
1038 {
1039         struct config_screen *screen = arg;
1040         if (screen->pad)
1041                 delwin(screen->pad);
1042         return 0;
1043 }
1044
1045 struct config_screen *config_screen_init(struct cui *cui,
1046                 const struct config *config,
1047                 const struct system_info *sysinfo,
1048                 void (*on_exit)(struct cui *))
1049 {
1050         struct config_screen *screen;
1051
1052         screen = talloc_zero(cui, struct config_screen);
1053         talloc_set_destructor(screen, config_screen_destroy);
1054         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1055                         cui, config_screen_process_key,
1056                         config_screen_post, config_screen_unpost,
1057                         config_screen_resize);
1058
1059         screen->cui = cui;
1060         screen->on_exit = on_exit;
1061         screen->need_redraw = false;
1062         screen->need_update = false;
1063         screen->label_x = 2;
1064         screen->field_x = 17;
1065
1066         screen->ipmi_override = false;
1067         screen->show_subset = false;
1068
1069         screen->scr.frame.ltitle = talloc_strdup(screen,
1070                         _("Petitboot System Configuration"));
1071         screen->scr.frame.rtitle = NULL;
1072         screen->scr.frame.help = talloc_strdup(screen,
1073                         _("tab=next, shift+tab=previous, x=exit, h=help"));
1074         nc_scr_frame_draw(&screen->scr);
1075
1076         scrollok(screen->scr.sub_ncw, true);
1077
1078         config_screen_draw(screen, config, sysinfo);
1079
1080         return screen;
1081 }