2 * Copyright (C) 2013 IBM Corporation
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.
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.
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
23 #include <talloc/talloc.h>
24 #include <types/types.h>
29 #include "nc-config.h"
30 #include "nc-widgets.h"
35 NET_CONF_TYPE_DHCP_ALL,
36 NET_CONF_TYPE_DHCP_ONE,
40 struct config_screen {
43 struct nc_widgetset *widgetset;
45 void (*on_exit)(struct cui *);
52 struct nc_widget_checkbox *autoboot_f;
53 struct nc_widget_label *autoboot_l;
54 struct nc_widget_textbox *timeout_f;
55 struct nc_widget_label *timeout_l;
57 struct nc_widget_label *network_l;
58 struct nc_widget_select *network_f;
60 struct nc_widget_label *iface_l;
61 struct nc_widget_select *iface_f;
62 struct nc_widget_label *ip_addr_l;
63 struct nc_widget_textbox *ip_addr_f;
64 struct nc_widget_label *ip_mask_l;
65 struct nc_widget_textbox *ip_mask_f;
66 struct nc_widget_label *gateway_l;
67 struct nc_widget_textbox *gateway_f;
68 struct nc_widget_label *dns_l;
69 struct nc_widget_textbox *dns_f;
71 struct nc_widget_button *ok_b;
72 struct nc_widget_button *cancel_b;
76 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
78 struct config_screen *config_screen;
80 assert(scr->sig == pb_config_screen_sig);
81 config_screen = (struct config_screen *)
82 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
83 assert(config_screen->scr.sig == pb_config_screen_sig);
87 static void config_screen_process_key(struct nc_scr *scr, int key)
89 struct config_screen *screen = config_screen_from_scr(scr);
92 handled = widgetset_process_key(screen->widgetset, key);
94 screen->on_exit(screen->cui);
96 wrefresh(screen->scr.main_ncw);
99 static void config_screen_resize(struct nc_scr *scr)
101 struct config_screen *screen = config_screen_from_scr(scr);
105 static int config_screen_post(struct nc_scr *scr)
107 struct config_screen *screen = config_screen_from_scr(scr);
108 widgetset_post(screen->widgetset);
109 nc_scr_frame_draw(scr);
110 wrefresh(scr->main_ncw);
114 static int config_screen_unpost(struct nc_scr *scr)
116 struct config_screen *screen = config_screen_from_scr(scr);
117 widgetset_unpost(screen->widgetset);
121 struct nc_scr *config_screen_scr(struct config_screen *screen)
126 static void ok_click(void *arg)
128 struct config_screen *screen = arg;
129 /* todo: save config */
130 screen->on_exit(screen->cui);
133 static void cancel_click(void *arg)
135 struct config_screen *screen = arg;
139 static int layout_pair(struct config_screen *screen, int y,
140 struct nc_widget_label *label,
141 struct nc_widget *field)
143 struct nc_widget *label_w = widget_label_base(label);
144 widget_move(label_w, y, screen->label_x);
145 widget_move(field, y, screen->field_x);
146 return max(widget_height(label_w), widget_height(field));
149 static void config_screen_layout_widgets(struct config_screen *screen,
150 enum net_conf_type net_conf)
152 struct nc_widget *wl, *wf;
158 y += layout_pair(screen, y, screen->widgets.autoboot_l,
159 widget_checkbox_base(screen->widgets.autoboot_f));
161 y += layout_pair(screen, y, screen->widgets.timeout_l,
162 widget_textbox_base(screen->widgets.timeout_f));
166 y += layout_pair(screen, y, screen->widgets.network_l,
167 widget_select_base(screen->widgets.network_f));
171 /* conditionally show iface select */
172 wl = widget_label_base(screen->widgets.iface_l);
173 wf = widget_select_base(screen->widgets.iface_f);
175 show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
176 net_conf == NET_CONF_TYPE_STATIC;
178 widget_set_visible(wl, show);
179 widget_set_visible(wf, show);
182 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
184 /* conditionally show static IP params */
185 show = net_conf == NET_CONF_TYPE_STATIC;
187 wl = widget_label_base(screen->widgets.ip_addr_l);
188 wf = widget_textbox_base(screen->widgets.ip_addr_f);
189 widget_set_visible(wl, show);
190 widget_set_visible(wf, show);
191 x = screen->field_x + widget_width(wf) + 1;
194 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
196 wl = widget_label_base(screen->widgets.ip_mask_l);
197 wf = widget_textbox_base(screen->widgets.ip_mask_f);
198 widget_set_visible(wl, show);
199 widget_set_visible(wf, show);
202 widget_move(wl, y, x);
203 widget_move(wf, y, x + 2);
207 wl = widget_label_base(screen->widgets.gateway_l);
208 wf = widget_textbox_base(screen->widgets.gateway_f);
209 widget_set_visible(wl, show);
210 widget_set_visible(wf, show);
213 y += layout_pair(screen, y, screen->widgets.gateway_l, wf);
215 wl = widget_label_base(screen->widgets.dns_l);
216 wf = widget_textbox_base(screen->widgets.dns_f);
217 widget_set_visible(wl, show);
218 widget_set_visible(wf, show);
221 y += 1 + layout_pair(screen, y, screen->widgets.dns_l, wf);
223 widget_move(widget_button_base(screen->widgets.ok_b),
225 widget_move(widget_button_base(screen->widgets.cancel_b),
226 y, screen->field_x + 10);
229 static void config_screen_network_change(void *arg, int value)
231 struct config_screen *screen = arg;
232 widgetset_unpost(screen->widgetset);
233 config_screen_layout_widgets(screen, value);
234 widgetset_post(screen->widgetset);
237 static void config_screen_setup_widgets(struct config_screen *screen,
238 const struct config *config,
239 const struct system_info *sysinfo)
241 struct nc_widgetset *set = screen->widgetset;
245 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
248 screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
249 screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
250 config->autoboot_enabled);
252 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
253 screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
254 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
256 screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
257 screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
259 widget_select_add_option(screen->widgets.network_f,
260 NET_CONF_TYPE_DHCP_ALL,
261 "DHCP on all active interfaces",
263 widget_select_add_option(screen->widgets.network_f,
264 NET_CONF_TYPE_DHCP_ONE,
265 "DHCP on a specific interface",
267 widget_select_add_option(screen->widgets.network_f,
268 NET_CONF_TYPE_STATIC,
269 "Static IP configuration",
272 widget_select_on_change(screen->widgets.network_f,
273 config_screen_network_change, screen);
275 screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
276 screen->widgets.iface_f = widget_new_select(set, 0, 0, 20);
278 for (i = 0; i < sysinfo->n_interfaces; i++) {
279 struct interface_info *info = sysinfo->interfaces[i];
280 widget_select_add_option(screen->widgets.iface_f,
281 i, info->name, false);
284 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
285 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, "");
286 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
287 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, "");
289 screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
290 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, "");
292 screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server:");
293 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 16, "");
295 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
297 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
298 cancel_click, screen);
301 struct config_screen *config_screen_init(struct cui *cui,
302 const struct config *config,
303 const struct system_info *sysinfo,
304 void (*on_exit)(struct cui *))
306 struct config_screen *screen;
308 screen = talloc_zero(cui, struct config_screen);
309 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
310 cui, config_screen_process_key,
311 config_screen_post, config_screen_unpost,
312 config_screen_resize);
315 screen->on_exit = on_exit;
317 screen->field_x = 16;
319 screen->scr.frame.ltitle = talloc_strdup(screen,
320 "Petitboot System Configuration");
321 screen->scr.frame.rtitle = NULL;
322 screen->scr.frame.help = talloc_strdup(screen,
323 "tab=next, shift+tab=previous");
324 nc_scr_frame_draw(&screen->scr);
326 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
327 screen->scr.sub_ncw);
328 config_screen_setup_widgets(screen, config, sysinfo);
329 config_screen_layout_widgets(screen, NET_CONF_TYPE_DHCP_ALL);
331 wrefresh(screen->scr.main_ncw);
332 scrollok(screen->scr.sub_ncw, true);