2 * Petitboot twin bootloader
4 * Copyright Geoff Levand <geoff@infradead.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #if defined(HAVE_CONFIG_H)
33 #include "talloc/talloc.h"
34 #include "waiter/waiter.h"
35 #include "ui/common/timer.h"
37 #include "pbt-client.h"
41 static struct pbt_client *client_from_item(struct pbt_item *item)
46 static int exit_to_shell_cb(struct pbt_item *item)
48 struct pbt_client *client = client_from_item(item);
50 client->signal_data.abort = 1;
54 static int edit_preferences_cb(struct pbt_item *item)
56 struct pbt_client *client = client_from_item(item);
60 pb_debug("%s: TODO\n", __func__);
65 static struct pbt_item *setup_system_item(struct pbt_menu *menu,
66 struct pbt_client *client)
68 struct pbt_item *top_item;
69 struct pbt_item *sub_item;
72 top_item = pbt_item_create_reduced(menu, "system", 0,
73 PB_ARTWORK_PATH "/applications-system.png");
76 goto fail_top_item_create;
80 q.x = menu->window->pixmap->width;
82 q.width = menu->scr->tscreen->width - q.x;
83 q.height = menu->scr->tscreen->height;
85 top_item->sub_menu = pbt_menu_create(top_item, "system", menu->scr,
86 menu, &q, &menu->layout);
88 if (!top_item->sub_menu)
89 goto fail_sub_menu_create;
91 sub_item = pbt_item_create(top_item->sub_menu, "Preferences", 0,
92 PB_ARTWORK_PATH "/configure.png", "Preferences",
93 "Edit petitboot preferences");
98 sub_item->on_execute = edit_preferences_cb;
99 sub_item->data = client;
100 pbt_menu_set_selected(top_item->sub_menu, sub_item);
102 sub_item = pbt_item_create(top_item->sub_menu, "Exit to Shell", 1,
103 PB_ARTWORK_PATH "/utilities-terminal.png", "Exit to Shell",
104 "Exit to a system shell prompt");
107 goto fail_sub_item_1;
109 sub_item->on_execute = exit_to_shell_cb;
110 sub_item->data = client;
112 top_item->sub_menu->n_items = 2;
114 /* Set shell item as default */
116 pbt_menu_set_selected(top_item->sub_menu, sub_item);
122 fail_sub_menu_create:
124 fail_top_item_create:
125 // FIXME: need cleanup
130 static struct pbt_menu *menu_create(struct pbt_client *client)
132 static struct pbt_menu_layout layout = {
136 .title = {.font_size = 30, .color = 0xff000000,},
137 .text = {.font_size = 18, .color = 0xff800000,},
140 struct pbt_menu *device_menu;
141 struct pbt_item *system_item;
144 const struct pbt_border *border;
146 assert(client->frame.scr);
148 icon = pbt_icon_load(NULL);
153 assert((unsigned int)icon->height == layout.item_height);
155 /* Create main (device) menu */
157 border = &pbt_right_border;
161 q.width = icon->width + 2 * layout.item_space + border->left
163 q.height = client->frame.scr->tscreen->height;
165 device_menu = pbt_menu_create(client, "device", client->frame.scr, NULL,
171 //FIXME: move to accessors
172 device_menu->background_color = 0x80000000;
173 device_menu->border = *border;
175 /* Setup system item */
177 system_item = setup_system_item(device_menu, client);
180 goto fail_system_item;
182 device_menu->n_items++;
184 /* Set system item as default */
186 pbt_menu_set_selected(device_menu, system_item);
187 pbt_menu_set_focus(device_menu, 1);
188 pbt_menu_show(device_menu, 1);
190 pbt_menu_redraw(device_menu);
195 // FIXME: need cleanup
201 static int run(struct pbt_client *client)
204 int result = waiter_poll(client->waitset);
207 pb_log("%s: poll: %s\n", __func__, strerror(errno));
211 if (client->signal_data.abort)
214 while (client->signal_data.resize) {
215 client->signal_data.resize = 0;
216 pbt_client_resize(client);
223 static struct pb_signal_data *_signal_data;
225 static void set_signal_data(struct pb_signal_data *sd)
230 static struct pb_signal_data *get_signal_data(void)
235 static void sig_handler(int signum)
237 DBGS("%d\n", signum);
239 struct pb_signal_data *sd = get_signal_data();
249 assert(0 && "unknown sig");
260 * main - twin bootloader main routine.
263 int main(int argc, char *argv[])
265 static struct sigaction sa;
266 static struct pbt_opts opts;
269 struct pbt_client *client;
272 result = pbt_opts_parse(&opts, argc, argv);
279 if (opts.show_help == pbt_opt_yes) {
284 if (opts.show_version == pbt_opt_yes) {
290 if (strcmp(opts.log_file, "-")) {
291 FILE *log = fopen(opts.log_file, "a");
297 pb_log("--- petitboot-twin ---\n");
299 sa.sa_handler = sig_handler;
300 result = sigaction(SIGALRM, &sa, NULL);
301 result += sigaction(SIGHUP, &sa, NULL);
302 result += sigaction(SIGINT, &sa, NULL);
303 result += sigaction(SIGTERM, &sa, NULL);
304 result += sigaction(SIGWINCH, &sa, NULL);
307 pb_log("%s sigaction failed.\n", __func__);
311 client = pbt_client_init(opts.backend, 1024, 640, opts.start_daemon);
314 ui_result = EXIT_FAILURE;
318 set_signal_data(&client->signal_data);
320 client->frame.top_menu = menu_create(client);
322 if (!client->frame.top_menu) {
323 ui_result = EXIT_FAILURE;
327 twin_screen_update(client->frame.scr->tscreen);
328 ui_result = run(client);
333 pb_log("--- end ---\n");
335 return ui_result ? EXIT_FAILURE : EXIT_SUCCESS;