summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/nc-config.c
blob: 0b2a8d7b06738f60185b70c1b94615b1a460ad81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 *  Copyright (C) 2013 IBM Corporation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _GNU_SOURCE


#include <string.h>

#include <talloc/talloc.h>
#include <types/types.h>
#include <log/log.h>

#include "config.h"
#include "nc-cui.h"
#include "nc-config.h"
#include "nc-widgets.h"

#define N_FIELDS	18

enum net_conf_type {
	NET_CONF_TYPE_DHCP_ALL,
	NET_CONF_TYPE_DHCP_ONE,
	NET_CONF_TYPE_STATIC,
};

struct config_screen {
	struct nc_scr		scr;
	struct cui		*cui;
	struct nc_widgetset	*widgetset;
	bool			exit;
	void			(*on_exit)(struct cui *);

	int			label_x;
	int			field_x;
	int			network_config_y;

	struct {
		struct nc_widget_checkbox	*autoboot_f;
		struct nc_widget_label		*autoboot_l;
		struct nc_widget_textbox	*timeout_f;
		struct nc_widget_label		*timeout_l;

		struct nc_widget_label		*network_l;
		struct nc_widget_select		*network_f;

		struct nc_widget_label		*iface_l;
		struct nc_widget_select		*iface_f;
		struct nc_widget_label		*ip_addr_l;
		struct nc_widget_textbox	*ip_addr_f;
		struct nc_widget_label		*ip_mask_l;
		struct nc_widget_textbox	*ip_mask_f;
		struct nc_widget_label		*gateway_l;
		struct nc_widget_textbox	*gateway_f;
		struct nc_widget_label		*dns_l;
		struct nc_widget_textbox	*dns_f;

		struct nc_widget_button		*ok_b;
		struct nc_widget_button		*cancel_b;
	} widgets;
};

static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
{
	struct config_screen *config_screen;

	assert(scr->sig == pb_config_screen_sig);
	config_screen = (struct config_screen *)
		((char *)scr - (size_t)&((struct config_screen *)0)->scr);
	assert(config_screen->scr.sig == pb_config_screen_sig);
	return config_screen;
}

static void config_screen_process_key(struct nc_scr *scr, int key)
{
	struct config_screen *screen = config_screen_from_scr(scr);
	bool handled;

	handled = widgetset_process_key(screen->widgetset, key);
	if (screen->exit)
		screen->on_exit(screen->cui);
	else if (handled)
		wrefresh(screen->scr.main_ncw);
}

static void config_screen_resize(struct nc_scr *scr)
{
	struct config_screen *screen = config_screen_from_scr(scr);
	(void)screen;
}

static int config_screen_post(struct nc_scr *scr)
{
	struct config_screen *screen = config_screen_from_scr(scr);
	widgetset_post(screen->widgetset);
	nc_scr_frame_draw(scr);
	wrefresh(scr->main_ncw);
	return 0;
}

static int config_screen_unpost(struct nc_scr *scr)
{
	struct config_screen *screen = config_screen_from_scr(scr);
	widgetset_unpost(screen->widgetset);
	return 0;
}

struct nc_scr *config_screen_scr(struct config_screen *screen)
{
	return &screen->scr;
}

static void ok_click(void *arg)
{
	struct config_screen *screen = arg;
	/* todo: save config */
	screen->on_exit(screen->cui);
}

static void cancel_click(void *arg)
{
	struct config_screen *screen = arg;
	screen->exit = true;
}

static int layout_pair(struct config_screen *screen, int y,
		struct nc_widget_label *label,
		struct nc_widget *field)
{
	struct nc_widget *label_w = widget_label_base(label);
	widget_move(label_w, y, screen->label_x);
	widget_move(field, y, screen->field_x);
	return max(widget_height(label_w), widget_height(field));
}

static void config_screen_layout_widgets(struct config_screen *screen,
		enum net_conf_type net_conf)
{
	struct nc_widget *wl, *wf;
	bool show;
	int y, x;

	y = 1;

	y += layout_pair(screen, y, screen->widgets.autoboot_l,
			widget_checkbox_base(screen->widgets.autoboot_f));

	y += layout_pair(screen, y, screen->widgets.timeout_l,
			widget_textbox_base(screen->widgets.timeout_f));

	y += 1;

	y += layout_pair(screen, y, screen->widgets.network_l,
			widget_select_base(screen->widgets.network_f));

	y += 1;

	/* conditionally show iface select */
	wl = widget_label_base(screen->widgets.iface_l);
	wf = widget_select_base(screen->widgets.iface_f);

	show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
		net_conf == NET_CONF_TYPE_STATIC;

	widget_set_visible(wl, show);
	widget_set_visible(wf, show);

	if (show)
		y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;

	/* conditionally show static IP params */
	show = net_conf == NET_CONF_TYPE_STATIC;

	wl = widget_label_base(screen->widgets.ip_addr_l);
	wf = widget_textbox_base(screen->widgets.ip_addr_f);
	widget_set_visible(wl, show);
	widget_set_visible(wf, show);
	x = screen->field_x + widget_width(wf) + 1;

	if (show)
		layout_pair(screen, y, screen->widgets.ip_addr_l, wf);

	wl = widget_label_base(screen->widgets.ip_mask_l);
	wf = widget_textbox_base(screen->widgets.ip_mask_f);
	widget_set_visible(wl, show);
	widget_set_visible(wf, show);

	if (show) {
		widget_move(wl, y, x);
		widget_move(wf, y, x + 2);
		y += 1;
	}

	wl = widget_label_base(screen->widgets.gateway_l);
	wf = widget_textbox_base(screen->widgets.gateway_f);
	widget_set_visible(wl, show);
	widget_set_visible(wf, show);

	if (show)
		y += layout_pair(screen, y, screen->widgets.gateway_l, wf);

	wl = widget_label_base(screen->widgets.dns_l);
	wf = widget_textbox_base(screen->widgets.dns_f);
	widget_set_visible(wl, show);
	widget_set_visible(wf, show);

	if (show)
		y += 1 + layout_pair(screen, y, screen->widgets.dns_l, wf);

	widget_move(widget_button_base(screen->widgets.ok_b),
			y, screen->field_x);
	widget_move(widget_button_base(screen->widgets.cancel_b),
			y, screen->field_x + 10);
}

static void config_screen_network_change(void *arg, int value)
{
	struct config_screen *screen = arg;
	widgetset_unpost(screen->widgetset);
	config_screen_layout_widgets(screen, value);
	widgetset_post(screen->widgetset);
}

static void config_screen_setup_widgets(struct config_screen *screen,
		const struct config *config,
		const struct system_info *sysinfo)
{
	struct nc_widgetset *set = screen->widgetset;
	unsigned int i;
	char *str;

	build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
			== N_FIELDS);

	screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
	screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
					config->autoboot_enabled);

	str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
	screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
	screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);

	screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
	screen->widgets.network_f = widget_new_select(set, 0, 0, 50);

	widget_select_add_option(screen->widgets.network_f,
					NET_CONF_TYPE_DHCP_ALL,
					"DHCP on all active interfaces",
					true);
	widget_select_add_option(screen->widgets.network_f,
					NET_CONF_TYPE_DHCP_ONE,
					"DHCP on a specific interface",
					false);
	widget_select_add_option(screen->widgets.network_f,
					NET_CONF_TYPE_STATIC,
					"Static IP configuration",
					false);

	widget_select_on_change(screen->widgets.network_f,
			config_screen_network_change, screen);

	screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
	screen->widgets.iface_f = widget_new_select(set, 0, 0, 20);

	for (i = 0; i < sysinfo->n_interfaces; i++) {
		struct interface_info *info = sysinfo->interfaces[i];
		widget_select_add_option(screen->widgets.iface_f,
						i, info->name, false);
	}

	screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
	screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, "");
	screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
	screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, "");

	screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
	screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, "");

	screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server:");
	screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 16, "");

	screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
			ok_click, screen);
	screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
			cancel_click, screen);
}

struct config_screen *config_screen_init(struct cui *cui,
		const struct config *config,
		const struct system_info *sysinfo,
		void (*on_exit)(struct cui *))
{
	struct config_screen *screen;

	screen = talloc_zero(cui, struct config_screen);
	nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
			cui, config_screen_process_key,
			config_screen_post, config_screen_unpost,
			config_screen_resize);

	screen->cui = cui;
	screen->on_exit = on_exit;
	screen->label_x = 2;
	screen->field_x = 16;

	screen->scr.frame.ltitle = talloc_strdup(screen,
			"Petitboot System Configuration");
	screen->scr.frame.rtitle = NULL;
	screen->scr.frame.help = talloc_strdup(screen,
			"tab=next, shift+tab=previous");
	nc_scr_frame_draw(&screen->scr);

	screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
			screen->scr.sub_ncw);
	config_screen_setup_widgets(screen, config, sysinfo);
	config_screen_layout_widgets(screen, NET_CONF_TYPE_DHCP_ALL);

	wrefresh(screen->scr.main_ncw);
	scrollok(screen->scr.sub_ncw, true);

	return screen;
}
OpenPOWER on IntegriCloud