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