ui/ncurses: Abstract text-screen code from sysinfo screen
[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
31 #include "nc-cui.h"
32 #include "nc-config.h"
33 #include "nc-widgets.h"
34
35 #define N_FIELDS        23
36
37 enum net_conf_type {
38         NET_CONF_TYPE_DHCP_ALL,
39         NET_CONF_TYPE_DHCP_ONE,
40         NET_CONF_TYPE_STATIC,
41 };
42
43 struct config_screen {
44         struct nc_scr           scr;
45         struct cui              *cui;
46         struct nc_widgetset     *widgetset;
47         WINDOW                  *pad;
48
49         bool                    exit;
50         void                    (*on_exit)(struct cui *);
51
52         int                     scroll_y;
53
54         int                     label_x;
55         int                     field_x;
56         int                     network_config_y;
57
58         enum net_conf_type      net_conf_type;
59
60         struct {
61                 struct nc_widget_checkbox       *autoboot_f;
62                 struct nc_widget_label          *autoboot_l;
63                 struct nc_widget_textbox        *timeout_f;
64                 struct nc_widget_label          *timeout_l;
65                 struct nc_widget_label          *timeout_help_l;
66
67                 struct nc_widget_label          *network_l;
68                 struct nc_widget_select         *network_f;
69
70                 struct nc_widget_label          *iface_l;
71                 struct nc_widget_select         *iface_f;
72                 struct nc_widget_label          *ip_addr_l;
73                 struct nc_widget_textbox        *ip_addr_f;
74                 struct nc_widget_label          *ip_mask_l;
75                 struct nc_widget_textbox        *ip_mask_f;
76                 struct nc_widget_label          *ip_addr_mask_help_l;
77                 struct nc_widget_label          *gateway_l;
78                 struct nc_widget_textbox        *gateway_f;
79                 struct nc_widget_label          *gateway_help_l;
80                 struct nc_widget_label          *dns_l;
81                 struct nc_widget_textbox        *dns_f;
82                 struct nc_widget_label          *dns_dhcp_help_l;
83                 struct nc_widget_label          *dns_help_l;
84
85                 struct nc_widget_button         *ok_b;
86                 struct nc_widget_button         *cancel_b;
87         } widgets;
88 };
89
90 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
91 {
92         struct config_screen *config_screen;
93
94         assert(scr->sig == pb_config_screen_sig);
95         config_screen = (struct config_screen *)
96                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
97         assert(config_screen->scr.sig == pb_config_screen_sig);
98         return config_screen;
99 }
100
101 static void pad_refresh(struct config_screen *screen)
102 {
103         int y, x, rows, cols;
104
105         getmaxyx(screen->scr.sub_ncw, rows, cols);
106         getbegyx(screen->scr.sub_ncw, y, x);
107
108         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
109 }
110
111 static void config_screen_process_key(struct nc_scr *scr, int key)
112 {
113         struct config_screen *screen = config_screen_from_scr(scr);
114         bool handled;
115
116         handled = widgetset_process_key(screen->widgetset, key);
117         if (screen->exit)
118                 screen->on_exit(screen->cui);
119         else if (handled)
120                 pad_refresh(screen);
121 }
122
123 static void config_screen_resize(struct nc_scr *scr)
124 {
125         struct config_screen *screen = config_screen_from_scr(scr);
126         (void)screen;
127 }
128
129 static int config_screen_post(struct nc_scr *scr)
130 {
131         struct config_screen *screen = config_screen_from_scr(scr);
132         widgetset_post(screen->widgetset);
133         nc_scr_frame_draw(scr);
134         redrawwin(scr->main_ncw);
135         wrefresh(screen->scr.main_ncw);
136         pad_refresh(screen);
137         return 0;
138 }
139
140 static int config_screen_unpost(struct nc_scr *scr)
141 {
142         struct config_screen *screen = config_screen_from_scr(scr);
143         widgetset_unpost(screen->widgetset);
144         return 0;
145 }
146
147 struct nc_scr *config_screen_scr(struct config_screen *screen)
148 {
149         return &screen->scr;
150 }
151
152 static int screen_process_form(struct config_screen *screen)
153 {
154         const struct system_info *sysinfo = screen->cui->sysinfo;
155         enum net_conf_type net_conf_type;
156         struct interface_config *iface;
157         struct config *config;
158         char *str, *end;
159         int rc;
160
161         config = config_copy(screen, screen->cui->config);
162
163         config->autoboot_enabled =
164                 widget_checkbox_get_value(screen->widgets.autoboot_f);
165
166
167         str = widget_textbox_get_value(screen->widgets.timeout_f);
168         if (str) {
169                 unsigned long x;
170                 errno = 0;
171                 x = strtoul(str, &end, 10);
172                 if (!errno && end != str)
173                         config->autoboot_timeout_sec = x;
174         }
175
176         net_conf_type = widget_select_get_value(screen->widgets.network_f);
177
178         /* if we don't have any network interfaces, prevent per-interface
179          * configuration */
180         if (sysinfo->n_interfaces == 0)
181                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
182
183         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
184                 config->network.n_interfaces = 0;
185
186         } else {
187                 int idx;
188
189                 iface = talloc_zero(config, struct interface_config);
190                 config->network.n_interfaces = 1;
191                 config->network.interfaces = talloc_array(config,
192                                 struct interface_config *, 1);
193                 config->network.interfaces[0] = iface;
194
195                 /* copy hwaddr (from the sysinfo interface data) to
196                  * the configuration */
197                 idx = widget_select_get_value(screen->widgets.iface_f);
198                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
199                                 sizeof(iface->hwaddr));
200         }
201
202         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
203                 iface->method = CONFIG_METHOD_DHCP;
204         }
205
206         if (net_conf_type == NET_CONF_TYPE_STATIC) {
207                 char *ip, *mask, *gateway;
208
209                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
210                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
211                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
212
213                 if (!ip || !*ip || !mask || !*mask) {
214                         screen->scr.frame.status =
215                                 "No IP / mask values are set";
216                         nc_scr_frame_draw(&screen->scr);
217                         talloc_free(config);
218                         return -1;
219                 }
220
221                 iface->method = CONFIG_METHOD_STATIC;
222                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
223                                 ip, mask);
224                 iface->static_config.gateway = talloc_strdup(iface, gateway);
225         }
226
227         str = widget_textbox_get_value(screen->widgets.dns_f);
228         if (str && strlen(str)) {
229                 char *dns, *tmp;
230                 int i;
231
232                 for (;;) {
233                         dns = strtok_r(str, " \t", &tmp);
234
235                         if (!dns)
236                                 break;
237
238                         i = config->network.n_dns_servers++;
239                         config->network.dns_servers = talloc_realloc(config,
240                                         config->network.dns_servers,
241                                         const char *,
242                                         config->network.n_dns_servers);
243                         config->network.dns_servers[i] =
244                                 talloc_strdup(config, dns);
245
246                         str = NULL;
247                 }
248         }
249
250         rc = cui_send_config(screen->cui, config);
251         talloc_free(config);
252
253         if (rc)
254                 pb_log("cui_send_config failed!\n");
255         else
256                 pb_debug("config sent!\n");
257
258         return 0;
259 }
260
261 static void ok_click(void *arg)
262 {
263         struct config_screen *screen = arg;
264         if (screen_process_form(screen))
265                 /* errors are written to the status line, so we'll need
266                  * to refresh */
267                 wrefresh(screen->scr.main_ncw);
268         else
269                 screen->exit = true;
270 }
271
272 static void cancel_click(void *arg)
273 {
274         struct config_screen *screen = arg;
275         screen->exit = true;
276 }
277
278 static int layout_pair(struct config_screen *screen, int y,
279                 struct nc_widget_label *label,
280                 struct nc_widget *field)
281 {
282         struct nc_widget *label_w = widget_label_base(label);
283         widget_move(label_w, y, screen->label_x);
284         widget_move(field, y, screen->field_x);
285         return max(widget_height(label_w), widget_height(field));
286 }
287
288 static void config_screen_layout_widgets(struct config_screen *screen,
289                 enum net_conf_type net_conf)
290 {
291         struct nc_widget *wl, *wf, *wh;
292         int y, x, help_x;
293         bool show;
294
295         y = 1;
296         help_x = screen->field_x + 2 +
297                 widget_width(widget_textbox_base(screen->widgets.dns_f));
298
299         y += layout_pair(screen, y, screen->widgets.autoboot_l,
300                         widget_checkbox_base(screen->widgets.autoboot_f));
301
302         wf = widget_textbox_base(screen->widgets.timeout_f);
303         widget_move(widget_label_base(screen->widgets.timeout_l),
304                         y, screen->label_x);
305         widget_move(wf, y, screen->field_x);
306         widget_move(widget_label_base(screen->widgets.timeout_help_l),
307                         y, screen->field_x + widget_width(wf) + 1);
308
309         y += 2;
310
311         y += layout_pair(screen, y, screen->widgets.network_l,
312                         widget_select_base(screen->widgets.network_f));
313
314         y += 1;
315
316         /* conditionally show iface select */
317         wl = widget_label_base(screen->widgets.iface_l);
318         wf = widget_select_base(screen->widgets.iface_f);
319
320         show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
321                 net_conf == NET_CONF_TYPE_STATIC;
322
323         widget_set_visible(wl, show);
324         widget_set_visible(wf, show);
325
326         if (show)
327                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
328
329         /* conditionally show static IP params */
330         show = net_conf == NET_CONF_TYPE_STATIC;
331
332         wl = widget_label_base(screen->widgets.ip_addr_l);
333         wf = widget_textbox_base(screen->widgets.ip_addr_f);
334         widget_set_visible(wl, show);
335         widget_set_visible(wf, show);
336         x = screen->field_x + widget_width(wf) + 1;
337
338         if (show)
339                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
340
341         wl = widget_label_base(screen->widgets.ip_mask_l);
342         wf = widget_textbox_base(screen->widgets.ip_mask_f);
343         widget_set_visible(wl, show);
344         widget_set_visible(wf, show);
345
346         if (show) {
347                 widget_move(wl, y, x);
348                 widget_move(wf, y, x + 2);
349         }
350
351         /* help for IP/mask */
352         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
353         widget_set_visible(wh, show);
354         if (show) {
355                 widget_move(wh, y, help_x);
356                 y++;
357         }
358
359         wl = widget_label_base(screen->widgets.gateway_l);
360         wf = widget_textbox_base(screen->widgets.gateway_f);
361         wh = widget_label_base(screen->widgets.gateway_help_l);
362         widget_set_visible(wl, show);
363         widget_set_visible(wf, show);
364         widget_set_visible(wh, show);
365
366         if (show) {
367                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
368                 widget_move(wh, y, help_x);
369                 y++;
370         }
371
372         wh = widget_label_base(screen->widgets.dns_help_l);
373         layout_pair(screen, y, screen->widgets.dns_l,
374                         widget_textbox_base(screen->widgets.dns_f));
375         widget_move(wh, y, help_x);
376         y++;
377
378         /* we show the DNS/DHCP help if we're configuring DHCP */
379         show = net_conf != NET_CONF_TYPE_STATIC;
380         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
381         widget_set_visible(wl, show);
382         if (show) {
383                 widget_move(wl, y, screen->field_x);
384                 y += 1;
385         }
386
387         y += 1;
388
389         widget_move(widget_button_base(screen->widgets.ok_b),
390                         y, screen->field_x);
391         widget_move(widget_button_base(screen->widgets.cancel_b),
392                         y, screen->field_x + 10);
393 }
394
395 static void config_screen_network_change(void *arg, int value)
396 {
397         struct config_screen *screen = arg;
398         screen->net_conf_type = value;
399         widgetset_unpost(screen->widgetset);
400         config_screen_layout_widgets(screen, value);
401         widgetset_post(screen->widgetset);
402 }
403
404 static struct interface_config *first_active_interface(
405                 const struct config *config)
406 {
407         unsigned int i;
408
409         for (i = 0; i < config->network.n_interfaces; i++) {
410                 if (config->network.interfaces[i]->ignore)
411                         continue;
412                 return config->network.interfaces[i];
413         }
414         return NULL;
415 }
416
417 static enum net_conf_type find_net_conf_type(const struct config *config)
418 {
419         struct interface_config *ifcfg;
420
421         ifcfg = first_active_interface(config);
422
423         if (!ifcfg)
424                 return NET_CONF_TYPE_DHCP_ALL;
425
426         else if (ifcfg->method == CONFIG_METHOD_DHCP)
427                 return NET_CONF_TYPE_DHCP_ONE;
428
429         else if (ifcfg->method == CONFIG_METHOD_STATIC)
430                 return NET_CONF_TYPE_STATIC;
431
432         assert(0);
433         return NET_CONF_TYPE_DHCP_ALL;
434 }
435
436 static void config_screen_setup_empty(struct config_screen *screen)
437 {
438         widget_new_label(screen->widgetset, 2, screen->field_x,
439                         "Waiting for configuration data...");
440         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
441                         4, screen->field_x, 6, "Cancel", cancel_click, screen);
442 }
443
444
445 static void config_screen_setup_widgets(struct config_screen *screen,
446                 const struct config *config,
447                 const struct system_info *sysinfo)
448 {
449         struct nc_widgetset *set = screen->widgetset;
450         struct interface_config *ifcfg;
451         char *str, *ip, *mask, *gw;
452         enum net_conf_type type;
453         unsigned int i;
454
455         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
456                         == N_FIELDS);
457
458         type = screen->net_conf_type;
459         ifcfg = first_active_interface(config);
460
461         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
462         screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
463                                         config->autoboot_enabled);
464
465         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
466         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
467         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
468         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds");
469
470         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
471
472         screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
473         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
474
475         widget_select_add_option(screen->widgets.network_f,
476                                         NET_CONF_TYPE_DHCP_ALL,
477                                         "DHCP on all active interfaces",
478                                         type == NET_CONF_TYPE_DHCP_ALL);
479         widget_select_add_option(screen->widgets.network_f,
480                                         NET_CONF_TYPE_DHCP_ONE,
481                                         "DHCP on a specific interface",
482                                         type == NET_CONF_TYPE_DHCP_ONE);
483         widget_select_add_option(screen->widgets.network_f,
484                                         NET_CONF_TYPE_STATIC,
485                                         "Static IP configuration",
486                                         type == NET_CONF_TYPE_STATIC);
487
488         widget_select_on_change(screen->widgets.network_f,
489                         config_screen_network_change, screen);
490
491         screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
492         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
493
494         for (i = 0; i < sysinfo->n_interfaces; i++) {
495                 struct interface_info *info = sysinfo->interfaces[i];
496                 char str[50], mac[20];
497                 bool is_default;
498
499                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
500                                         sizeof(ifcfg->hwaddr));
501
502                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
503                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
504                                 info->link ? "link up" : "link down");
505
506                 widget_select_add_option(screen->widgets.iface_f,
507                                                 i, str, is_default);
508         }
509
510         gw = ip = mask = NULL;
511         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
512                 char *sep;
513
514                 str = talloc_strdup(screen, ifcfg->static_config.address);
515                 sep = strchr(str, '/');
516                 ip = str;
517
518                 if (sep) {
519                         *sep = '\0';
520                         mask = sep + 1;
521                 }
522                 gw = ifcfg->static_config.gateway;
523         }
524
525         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
526         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
527         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
528         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 4, mask);
529         screen->widgets.ip_addr_mask_help_l =
530                 widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)");
531
532         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
533         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
534
535         screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
536         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
537         screen->widgets.gateway_help_l =
538                 widget_new_label(set, 0, 0, "(eg. 192.168.0.1)");
539
540         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
541
542         str = talloc_strdup(screen, "");
543         for (i = 0; i < config->network.n_dns_servers; i++) {
544                 str = talloc_asprintf_append(str, "%s%s",
545                                 (i == 0) ? "" : " ",
546                                 config->network.dns_servers[i]);
547         }
548
549         screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
550         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
551         screen->widgets.dns_help_l =
552                 widget_new_label(set, 0, 0, "(eg. 192.168.0.2)");
553
554         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
555
556         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
557                         "(if not provided by DHCP server)");
558
559         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
560                         ok_click, screen);
561         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
562                         cancel_click, screen);
563 }
564
565 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
566 {
567         struct config_screen *screen = arg;
568         int w_y, s_max;
569
570         w_y = widget_y(widget) + widget_focus_y(widget);
571         s_max = getmaxy(screen->scr.sub_ncw) - 1;
572
573         if (w_y < screen->scroll_y)
574                 screen->scroll_y = w_y;
575
576         else if (w_y + screen->scroll_y + 1 > s_max)
577                 screen->scroll_y = 1 + w_y - s_max;
578
579         else
580                 return;
581
582         pad_refresh(screen);
583 }
584
585 static void config_screen_draw(struct config_screen *screen,
586                 const struct config *config,
587                 const struct system_info *sysinfo)
588 {
589         bool repost = false;
590         int height;
591
592         /* The size of the pad we'll need depends on the number of interfaces.
593          *
594          * We use N_FIELDS (which is quite conservative, as some fields share
595          * a line) as a base, then add 3 (as the network select field is
596          * takes 3 lines), and n_interfaces (as the network interface field
597          * has n_interfaces lines).
598          */
599         height = N_FIELDS + 3;
600         if (sysinfo)
601                 height += sysinfo->n_interfaces;
602         if (!screen->pad || getmaxy(screen->pad) < height) {
603                 if (screen->pad)
604                         delwin(screen->pad);
605                 screen->pad = newpad(height, COLS);
606         }
607
608         if (screen->widgetset) {
609                 widgetset_unpost(screen->widgetset);
610                 talloc_free(screen->widgetset);
611                 repost = true;
612         }
613
614         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
615                         screen->pad);
616         widgetset_set_widget_focus(screen->widgetset,
617                         config_screen_widget_focus, screen);
618
619         if (!config || !sysinfo) {
620                 config_screen_setup_empty(screen);
621         } else {
622                 screen->net_conf_type = find_net_conf_type(config);
623
624                 config_screen_setup_widgets(screen, config, sysinfo);
625                 config_screen_layout_widgets(screen, screen->net_conf_type);
626         }
627
628         if (repost)
629                 widgetset_post(screen->widgetset);
630 }
631
632 void config_screen_update(struct config_screen *screen,
633                 const struct config *config,
634                 const struct system_info *sysinfo)
635 {
636         config_screen_draw(screen, config, sysinfo);
637         pad_refresh(screen);
638 }
639
640 static int config_screen_destroy(void *arg)
641 {
642         struct config_screen *screen = arg;
643         if (screen->pad)
644                 delwin(screen->pad);
645         return 0;
646 }
647
648 struct config_screen *config_screen_init(struct cui *cui,
649                 const struct config *config,
650                 const struct system_info *sysinfo,
651                 void (*on_exit)(struct cui *))
652 {
653         struct config_screen *screen;
654
655         screen = talloc_zero(cui, struct config_screen);
656         talloc_set_destructor(screen, config_screen_destroy);
657         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
658                         cui, config_screen_process_key,
659                         config_screen_post, config_screen_unpost,
660                         config_screen_resize);
661
662         screen->cui = cui;
663         screen->on_exit = on_exit;
664         screen->label_x = 2;
665         screen->field_x = 17;
666
667         screen->scr.frame.ltitle = talloc_strdup(screen,
668                         "Petitboot System Configuration");
669         screen->scr.frame.rtitle = NULL;
670         screen->scr.frame.help = talloc_strdup(screen,
671                         "tab=next, shift+tab=previous");
672         nc_scr_frame_draw(&screen->scr);
673
674         scrollok(screen->scr.sub_ncw, true);
675
676         config_screen_draw(screen, config, sysinfo);
677
678         return screen;
679 }