summaryrefslogtreecommitdiffstats
path: root/ui/twin/main-generic.c
blob: 5020fa2f345ff8319a8805f07de400fc3b073379 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Petitboot twin bootloader
 *
 *  Copyright Geoff Levand <geoff@infradead.org>
 *
 *  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
 */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include "log/log.h"
#include "talloc/talloc.h"
#include "waiter/waiter.h"
#include "ui/common/timer.h"

#include "pbt-client.h"
#include "pbt-main.h"


static struct pbt_client *client_from_item(struct pbt_item *item)
{
	return item->data;
}

static int exit_to_shell_cb(struct pbt_item *item)
{
	struct pbt_client *client = client_from_item(item);

	client->signal_data.abort = 1;
	return 0;
}

static int edit_preferences_cb(struct pbt_item *item)
{
	struct pbt_client *client = client_from_item(item);

	(void)client;

	pb_log("%s: TODO\n", __func__);

	return 0;
}

static struct pbt_item *setup_system_item(struct pbt_menu *menu,
	struct pbt_client *client)
{
	struct pbt_item *top_item;
	struct pbt_item *sub_item;
	struct pbt_quad q;

	top_item = pbt_item_create_reduced(menu, "system", 0,
		PB_ARTWORK_PATH "/system.png");

	if (!top_item)
		goto fail_top_item_create;

	/* sub_menu */

	q.x = menu->window->pixmap->width;
	q.y = 0;
	q.width = menu->scr->tscreen->width - q.x;
	q.height = menu->scr->tscreen->height;

	top_item->sub_menu = pbt_menu_create(top_item, "system", menu->scr,
		menu, &q, &menu->layout);

	if (!top_item->sub_menu)
		goto fail_sub_menu_create;

	sub_item = pbt_item_create(top_item->sub_menu, "Preferences", 0,
		PB_ARTWORK_PATH "/system.png", "Preferences",
		"Edit petitboot preferences");

	if (!sub_item)
		goto fail_sub_item_0;

	sub_item->on_execute = edit_preferences_cb;
	sub_item->data = client;
	pbt_menu_set_selected(top_item->sub_menu, sub_item);

	sub_item = pbt_item_create(top_item->sub_menu, "Exit to Shell", 1,
		PB_ARTWORK_PATH "/system.png", "Exit to Shell",
		"Exit to a system shell prompt");

	if (!sub_item)
		goto fail_sub_item_1;

	sub_item->on_execute = exit_to_shell_cb;
	sub_item->data = client;

	top_item->sub_menu->n_items = 2;

	/* Set shell item as default */

	pbt_menu_set_selected(top_item->sub_menu, sub_item);

	return top_item;

fail_sub_item_1:
fail_sub_item_0:
fail_sub_menu_create:
// FIXME: todo
fail_top_item_create:
// FIXME: need cleanup
	assert(0);
	return NULL;
}

static struct pbt_menu *menu_create(struct pbt_client *client)
{
	static struct pbt_menu_layout layout = {
		.item_height = 64,
		.item_space = 10,
		.text_space = 5,
		.title = {.font_size = 30, .color = 0xff000000,},
		.text = {.font_size = 18, .color = 0xff800000,},
	};

	struct pbt_menu *device_menu;
	struct pbt_item *system_item;
	struct pbt_quad q;
	twin_pixmap_t *icon;
	const struct pbt_border *border;

	assert(client->frame.scr);

	icon = pbt_icon_load(NULL);

	if (!icon)
		return NULL;

	assert((unsigned int)icon->height == layout.item_height);

	/* Create main (device) menu */

	border = &pbt_right_border;

	q.x = 0;
	q.y = 0;
	q.width = icon->width + 2 * layout.item_space + border->left
		+ border->right;
	q.height = client->frame.scr->tscreen->height;

	device_menu = pbt_menu_create(client, "device", client->frame.scr, NULL,
		&q, &layout);

	if (!device_menu)
		goto fail_menu;

	//FIXME: move to accessors
	device_menu->background_color = 0x80000000;
	device_menu->border = *border;

	/* Setup system item */

	system_item = setup_system_item(device_menu, client);

	if (!system_item)
		goto fail_system_item;

	device_menu->n_items++;

	/* Set system item as default */

	pbt_menu_set_selected(device_menu, system_item);
	pbt_menu_set_focus(device_menu, 1);
	pbt_menu_show(device_menu, 1);

	pbt_menu_redraw(device_menu);

	return device_menu;

fail_system_item:
	// FIXME: need cleanup
fail_menu:
	assert(0);
	return NULL;
}

static int kexec_cb(__attribute__((unused)) struct pbt_client *client, struct pb_opt_data *opt_data)
{
	int result;

	assert(opt_data);

	pb_log("%s: %s\n", __func__, opt_data->name);

	result = pb_run_kexec(opt_data->kd);

	return result;
}

static int run(struct pbt_client *client)
{
	while (1) {
		int result = waiter_poll();

		if (result < 0 && errno != EINTR) {
			pb_log("%s: poll: %s\n", __func__, strerror(errno));
			break;
		}

		if (client->signal_data.abort)
			break;

		ui_timer_process_sig(&client->signal_data.timer);

		while (client->signal_data.resize) {
			client->signal_data.resize = 0;
			pbt_client_resize(client);
		}
	}

	return 0;
}

static struct pb_signal_data *_signal_data;

static void set_signal_data(struct pb_signal_data *sd)
{
	_signal_data = sd;
}

static struct pb_signal_data *get_signal_data(void)
{
	return _signal_data;
}

static void sig_handler(int signum)
{
	DBGS("%d\n", signum);

	struct pb_signal_data *sd = get_signal_data();

	if (!sd)
		return;

	switch (signum) {
	case SIGALRM:
		ui_timer_sigalrm(&sd->timer);
		break;
	case SIGWINCH:
		sd->resize = 1;
		break;
	default:
		assert(0 && "unknown sig");
		/* fall through */
	case SIGINT:
	case SIGHUP:
	case SIGTERM:
		sd->abort = 1;
		break;
	}
}

/**
 * main - twin bootloader main routine.
 */

int main(int argc, char *argv[])
{
	static struct sigaction sa;
	static struct pbt_opts opts;
	int result;
	int ui_result;
	FILE *log;
	struct pbt_client *client;

	result = pbt_opts_parse(&opts, argc, argv);

	if (result) {
		pbt_print_usage();
		return EXIT_FAILURE;
	}

	if (opts.show_help == pbt_opt_yes) {
		pbt_print_usage();
		return EXIT_SUCCESS;
	}

	if (opts.show_version == pbt_opt_yes) {
		pbt_print_version();
		return EXIT_SUCCESS;
	}

	log = fopen(opts.log_file, "a");
	assert(log);
	pb_log_set_stream(log);

#if defined(DEBUG)
	pb_log_always_flush(1);
#endif

	pb_log("--- petitboot-twin ---\n");

	sa.sa_handler = sig_handler;
	result = sigaction(SIGALRM, &sa, NULL);
	result += sigaction(SIGHUP, &sa, NULL);
	result += sigaction(SIGINT, &sa, NULL);
	result += sigaction(SIGTERM, &sa, NULL);
	result += sigaction(SIGWINCH, &sa, NULL);

	if (result) {
		pb_log("%s sigaction failed.\n", __func__);
		return EXIT_FAILURE;
	}

	client = pbt_client_init(opts.backend, 900, 300, kexec_cb);

	if (!client) {
		ui_result = EXIT_FAILURE;
		goto done;
	}

	set_signal_data(&client->signal_data);

	client->frame.top_menu = menu_create(client);

	if (!client->frame.top_menu) {
		ui_result = EXIT_FAILURE;
		goto done;
	}

	twin_screen_update(client->frame.scr->tscreen);
	ui_result = run(client);

done:
	talloc_free(client);

	pb_log("--- end ---\n");

	return ui_result ? EXIT_FAILURE : EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud