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