2 * Copyright Geoff Levand <geoff@infradead.org>
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
18 #if defined(HAVE_CONFIG_H)
26 #include <pb-protocol/pb-protocol.h>
28 #include "pbt-client.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "ui/common/discover-client.h"
35 static struct pb_opt_data *pbt_opt_data_from_item(struct pbt_item *item)
40 void pbt_client_resize(struct pbt_client *client)
45 void pbt_frame_status_printf(struct pbt_frame *frame, const char *format, ...)
55 static int pbt_client_run_kexec(struct pbt_item *item)
58 struct pb_opt_data *opt_data = pbt_opt_data_from_item(item);
60 pb_log("%s: %s\n", __func__, pbt_item_name(item));
62 pbt_frame_status_printf(&item->pbt_client->frame, "Booting %s...",
65 assert(item->pbt_client->kexec_cb);
66 result = item->pbt_client->kexec_cb(item->pbt_client, opt_data);
69 sleep(item->pbt_client->dry_run ? 1 : 60);
72 pb_log("%s: failed: %s\n", __func__, opt_data->kd->image);
74 pbt_frame_status_printf(&item->pbt_client->frame, "Failed: kexec %s",
80 static int pbt_client_on_edit(struct pbt_item *item)
82 DBGS("*** %s ***\n", pbt_item_name(item));
86 static int pbt_device_add(struct device *dev, struct pbt_client *client)
88 struct pbt_frame *const frame = &client->frame;
89 struct pbt_item *device_item;
90 struct boot_option *opt;
92 const char *icon_file;
93 struct pbt_item *selected_item = NULL;
95 pb_log("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
98 pb_protocol_dump_device(dev, "", pb_log_get_stream());
102 // FIXME: check for existing dev->id
104 icon_file = dev->icon_file ? dev->icon_file : pbt_icon_chooser(dev->id);
106 device_item = pbt_item_create_reduced(frame->top_menu, dev->id,
107 frame->top_menu->n_items, icon_file);
110 goto fail_device_item_create;
112 device_item->pb_device = dev;
113 frame->top_menu->n_items++;
117 q.x = frame->top_menu->window->pixmap->width;
119 q.width = frame->top_menu->scr->tscreen->width - q.x;
120 q.height = frame->top_menu->scr->tscreen->height;
122 device_item->sub_menu = pbt_menu_create(device_item, dev->id,
123 frame->top_menu->scr, frame->top_menu, &q,
124 &frame->top_menu->layout);
125 if (!device_item->sub_menu)
126 goto fail_sub_menu_create;
128 list_for_each_entry(&dev->boot_options, opt, list) {
130 struct pb_opt_data *opt_data;
132 i = pbt_item_create(device_item->sub_menu,
133 opt->id, device_item->sub_menu->n_items++,
134 opt->icon_file, opt->name, opt->description);
142 i->pbt_client = client;
143 i->on_execute = pbt_client_run_kexec;
144 i->on_edit = pbt_client_on_edit;
146 i->data = opt_data = talloc(i, struct pb_opt_data);
147 opt_data->name = opt->name;
148 opt_data->kd = talloc(i, struct pb_kexec_data);
149 opt_data->kd->image = talloc_strdup(opt_data->kd,
150 opt->boot_image_file);
151 opt_data->kd->initrd = talloc_strdup(opt_data->kd,
153 opt_data->kd->args = talloc_strdup(opt_data->kd,
157 opt_data->opt_hash = pb_opt_hash(dev, opt);
159 /* Select the first item as default. */
164 /* If this is the default_item select it and start timer. */
166 if (opt_data->opt_hash
167 == device_item->sub_menu->default_item_hash) {
169 ui_timer_kick(&client->signal_data.timer);
173 pbt_menu_set_selected(device_item->sub_menu, selected_item);
175 pbt_menu_show(frame->top_menu, 1);
176 twin_screen_update(client->frame.scr->tscreen);
180 fail_sub_menu_create:
181 fail_device_item_create:
186 static void pbt_device_remove(struct device *dev, struct pbt_client *client)
188 struct pbt_frame *const frame = &client->frame;
189 struct list *i_list = frame->top_menu->item_list;
190 twin_window_t *last_window = NULL;
191 struct pbt_item *removed_item;
192 struct pbt_item *prev_item;
193 struct pbt_item *next_item;
196 pb_log("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
199 pb_protocol_dump_device(dev, "", pb_log_get_stream());
202 list_for_each_entry(i_list, i, list) {
203 if (i->pb_device == dev) {
210 pb_log("%s: %p %s: unknown device\n", __func__, dev, dev->id);
211 assert(0 && "unknown device");
215 prev_item = list_prev_entry(i_list, removed_item, list);
216 next_item = list_next_entry(i_list, removed_item, list);
218 if (removed_item == frame->top_menu->selected) {
220 pbt_menu_set_selected(frame->top_menu, prev_item);
222 pbt_menu_set_selected(frame->top_menu, next_item);
224 assert(0 && "empty list");
229 /* Shift items up. */
232 list_for_each_entry_continue(i_list, i, list) {
233 last_window = i->window;
234 i->window = list_prev_entry(i_list, i, list)->window;
235 twin_window_set_name(i->window, last_window->name);
236 i->window->client_data = last_window->client_data;
240 twin_window_hide(last_window);
241 twin_window_destroy(last_window);
243 list_remove(&removed_item->list);
244 removed_item->window = NULL;
245 talloc_free(removed_item);
246 frame->top_menu->n_items--;
248 pbt_menu_show(frame->top_menu, 1);
249 twin_screen_update(client->frame.scr->tscreen);
252 static struct discover_client_ops pbt_client_ops = {
253 .device_add = (void *)pbt_device_add,
254 .device_remove = (void *)pbt_device_remove,
257 static void pbt_client_destructor(struct pbt_client *client)
259 pb_log("%s\n", __func__);
261 // move to screen twin_x11_destroy(twin_ctx);
262 talloc_free(client->discover_client);
263 memset(client, 0, sizeof(*client));
266 struct pbt_client *pbt_client_init(enum pbt_twin_backend backend,
267 unsigned int width, unsigned int height,
268 int (*kexec_cb)(struct pbt_client *, struct pb_opt_data *),
269 int start_deamon, int dry_run)
271 struct pbt_client *pbt_client;
274 pbt_client = talloc_zero(NULL, struct pbt_client);
277 pb_log("%s: alloc pbt_client failed.\n", __func__);
278 fprintf(stderr, "%s: alloc pbt_client failed.\n", __func__);
282 talloc_set_destructor(pbt_client, (void *)pbt_client_destructor);
284 pbt_client->waitset = waitset_create(pbt_client);
286 pbt_client->sig = "pbt_client";
287 pbt_client->kexec_cb = kexec_cb;
288 pbt_client->dry_run = dry_run;
289 pbt_client->frame.scr = pbt_scr_init(pbt_client, pbt_client->waitset,
290 backend, width, height, NULL, NULL);
293 if (!pbt_client->frame.scr)
296 /* Loop here for scripts that just started the server. */
299 for (i = start_deamon ? 2 : 10; i; i--) {
300 pbt_client->discover_client
301 = discover_client_init(&pbt_client_ops, pbt_client);
302 if (pbt_client->discover_client || !i)
304 pb_log("%s: waiting for server %d\n", __func__, i);
308 if (!pbt_client->discover_client && start_deamon) {
313 result = pb_start_daemon();
318 pb_log("%s: discover_client_init failed.\n", __func__);
319 fprintf(stderr, "%s: error: discover_client_init failed.\n",
321 fprintf(stderr, "could not start pb-discover, the petitboot "
323 goto fail_client_init;
326 if (!pbt_client->discover_client) {
327 pb_log("%s: discover_client_init failed.\n", __func__);
328 fprintf(stderr, "%s: error: discover_client_init failed.\n",
330 fprintf(stderr, "check that pb-discover, "
331 "the petitboot daemon is running.\n");
332 goto fail_client_init;
335 waiter_register(pbt_client->waitset,
336 discover_client_get_fd(pbt_client->discover_client),
337 WAIT_IN, (waiter_cb)discover_client_process,
338 pbt_client->discover_client);
343 talloc_free(pbt_client);