ui/ncurses: Add basic config editor
[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
21 #include <string.h>
22
23 #include <talloc/talloc.h>
24 #include <types/types.h>
25 #include <log/log.h>
26
27 #include "config.h"
28 #include "nc-cui.h"
29 #include "nc-config.h"
30 #include "nc-widgets.h"
31
32 #define N_FIELDS        9
33
34 struct config_screen {
35         struct nc_scr           scr;
36         struct cui              *cui;
37         struct nc_widgetset     *widgetset;
38         bool                    exit;
39         void                    (*on_exit)(struct cui *);
40
41         int                     label_x;
42         int                     field_x;
43
44         struct {
45                 struct nc_widget_checkbox       *autoboot_f;
46                 struct nc_widget_label          *autoboot_l;
47                 struct nc_widget_textbox        *timeout_f;
48                 struct nc_widget_label          *timeout_l;
49
50                 struct nc_widget_label          *network_l;
51                 struct nc_widget_label          *iface_l;
52                 struct nc_widget_select         *iface_f;
53
54                 struct nc_widget_button         *ok_b;
55                 struct nc_widget_button         *cancel_b;
56         } widgets;
57 };
58
59 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
60 {
61         struct config_screen *config_screen;
62
63         assert(scr->sig == pb_config_screen_sig);
64         config_screen = (struct config_screen *)
65                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
66         assert(config_screen->scr.sig == pb_config_screen_sig);
67         return config_screen;
68 }
69
70 static void config_screen_process_key(struct nc_scr *scr, int key)
71 {
72         struct config_screen *screen = config_screen_from_scr(scr);
73         bool handled;
74
75         handled = widgetset_process_key(screen->widgetset, key);
76         if (screen->exit)
77                 screen->on_exit(screen->cui);
78         else if (handled)
79                 wrefresh(screen->scr.main_ncw);
80 }
81
82 static void config_screen_resize(struct nc_scr *scr)
83 {
84         struct config_screen *screen = config_screen_from_scr(scr);
85         (void)screen;
86 }
87
88 static int config_screen_post(struct nc_scr *scr)
89 {
90         struct config_screen *screen = config_screen_from_scr(scr);
91         widgetset_post(screen->widgetset);
92         nc_scr_frame_draw(scr);
93         wrefresh(scr->main_ncw);
94         return 0;
95 }
96
97 static int config_screen_unpost(struct nc_scr *scr)
98 {
99         struct config_screen *screen = config_screen_from_scr(scr);
100         widgetset_unpost(screen->widgetset);
101         return 0;
102 }
103
104 struct nc_scr *config_screen_scr(struct config_screen *screen)
105 {
106         return &screen->scr;
107 }
108
109 static void ok_click(void *arg)
110 {
111         struct config_screen *screen = arg;
112         /* todo: save config */
113         screen->on_exit(screen->cui);
114 }
115
116 static void cancel_click(void *arg)
117 {
118         struct config_screen *screen = arg;
119         screen->exit = true;
120 }
121
122 static int layout_pair(struct config_screen *screen, int y,
123                 struct nc_widget_label *label,
124                 struct nc_widget *field)
125 {
126         struct nc_widget *label_w = widget_label_base(label);
127         widget_move(label_w, y, screen->label_x);
128         widget_move(field, y, screen->field_x);
129         return max(widget_height(label_w), widget_height(field));
130 }
131
132 static void config_screen_layout_widgets(struct config_screen *screen)
133 {
134         int y;
135
136         y = 1;
137
138         y += layout_pair(screen, y, screen->widgets.autoboot_l,
139                         widget_checkbox_base(screen->widgets.autoboot_f));
140
141         y += layout_pair(screen, y, screen->widgets.timeout_l,
142                         widget_textbox_base(screen->widgets.timeout_f));
143
144         y++;
145
146         widget_move(widget_button_base(screen->widgets.ok_b),
147                         y, screen->field_x);
148         widget_move(widget_button_base(screen->widgets.cancel_b),
149                         y, screen->field_x + 10);
150 }
151
152 static void config_screen_setup_widgets(struct config_screen *screen,
153                 const struct config *config,
154                 const struct system_info *sysinfo)
155 {
156         struct nc_widgetset *set = screen->widgetset;
157         char *str;
158
159         (void)sysinfo;
160
161         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
162                         == N_FIELDS);
163
164         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
165         screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
166                                         config->autoboot_enabled);
167
168         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
169         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
170         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
171
172         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
173                         ok_click, screen);
174         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
175                         cancel_click, screen);
176
177 }
178
179 struct config_screen *config_screen_init(struct cui *cui,
180                 const struct config *config,
181                 const struct system_info *sysinfo,
182                 void (*on_exit)(struct cui *))
183 {
184         struct config_screen *screen;
185
186         screen = talloc_zero(cui, struct config_screen);
187         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
188                         cui, config_screen_process_key,
189                         config_screen_post, config_screen_unpost,
190                         config_screen_resize);
191
192         screen->cui = cui;
193         screen->on_exit = on_exit;
194         screen->label_x = 2;
195         screen->field_x = 16;
196
197         screen->scr.frame.ltitle = talloc_strdup(screen,
198                         "Petitboot System Configuration");
199         screen->scr.frame.rtitle = NULL;
200         screen->scr.frame.help = talloc_strdup(screen,
201                         "tab=next, shift+tab=previous");
202         nc_scr_frame_draw(&screen->scr);
203
204         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
205                         screen->scr.sub_ncw);
206         config_screen_setup_widgets(screen, config, sysinfo);
207         config_screen_layout_widgets(screen);
208
209         wrefresh(screen->scr.main_ncw);
210         scrollok(screen->scr.sub_ncw, true);
211
212         return screen;
213 }