39facf5fe73094b16dc8759086c43708daa2b6f3
[petitboot] / ui / twin / pbt-client.c
1 /*
2  *  Copyright Geoff Levand <geoff@infradead.org>
3  *
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.
7  *
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.
12  *
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
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #define _GNU_SOURCE
23 #include <assert.h>
24 #include <string.h>
25
26 #include <pb-protocol/pb-protocol.h>
27
28 #include "pbt-client.h"
29
30 #include "log/log.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "ui/common/discover-client.h"
34
35 static struct pb_opt_data *pbt_opt_data_from_item(struct pbt_item *item)
36 {
37         return item->data;
38 }
39
40 void pbt_client_resize(struct pbt_client *client)
41 {
42         (void)client; // TODO
43 }
44
45 void pbt_frame_status_printf(struct pbt_frame *frame, const char *format, ...)
46 {
47         va_list ap;
48
49         va_start(ap, format);
50         // TODO
51         (void)frame;
52         va_end(ap);
53 }
54
55 static int pbt_client_run_kexec(struct pbt_item *item)
56 {
57         int result;
58         struct pb_opt_data *opt_data = pbt_opt_data_from_item(item);
59
60         pb_log("%s: %s\n", __func__, pbt_item_name(item));
61
62         pbt_frame_status_printf(&item->pbt_client->frame, "Booting %s...",
63                 pbt_item_name(item));
64
65         assert(item->pbt_client->kexec_cb);
66         result = item->pbt_client->kexec_cb(item->pbt_client, opt_data);
67
68         if (!result) {
69                 sleep(item->pbt_client->dry_run ? 1 : 60);
70         }
71
72         pb_log("%s: failed: %s\n", __func__, opt_data->bd->image);
73
74         pbt_frame_status_printf(&item->pbt_client->frame, "Failed: kexec %s",
75                 opt_data->bd->image);
76
77         return 0;
78 }
79
80 static int pbt_client_on_edit(struct pbt_item *item)
81 {
82         DBGS("*** %s ***\n", pbt_item_name(item));
83         return 0;
84 }
85
86 static int pbt_device_add(struct device *dev, struct pbt_client *client)
87 {
88         struct pbt_frame *const frame = &client->frame;
89         struct pbt_item *device_item;
90         struct boot_option *opt;
91         struct pbt_quad q;
92         const char *icon_file;
93         struct pbt_item *selected_item = NULL;
94
95         pb_log("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
96                 dev->n_options);
97
98         pb_protocol_dump_device(dev, "", pb_log_get_stream());
99
100         /* device_item */
101
102         // FIXME: check for existing dev->id
103
104         icon_file = dev->icon_file ? dev->icon_file : pbt_icon_chooser(dev->id);
105
106         device_item = pbt_item_create_reduced(frame->top_menu, dev->id,
107                 frame->top_menu->n_items, icon_file);
108
109         if (!device_item)
110                 goto fail_device_item_create;
111
112         device_item->pb_device = dev;
113         frame->top_menu->n_items++;
114
115         /* sub_menu */
116
117         q.x = frame->top_menu->window->pixmap->width;
118         q.y = 0;
119         q.width = frame->top_menu->scr->tscreen->width - q.x;
120         q.height = frame->top_menu->scr->tscreen->height;
121
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;
127
128         list_for_each_entry(&dev->boot_options, opt, list) {
129                 struct pbt_item *i;
130                 struct pb_opt_data *opt_data;
131
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);
135
136                 if (!i) {
137                         assert(0);
138                         break;
139                 }
140
141                 i->pb_opt = opt;
142                 i->pbt_client = client;
143                 i->on_execute = pbt_client_run_kexec;
144                 i->on_edit = pbt_client_on_edit;
145
146                 i->data = opt_data = talloc(i, struct pb_opt_data);
147                 opt_data->name = opt->name;
148                 opt_data->bd = talloc(i, struct pb_boot_data);
149                 opt_data->bd->image = talloc_strdup(opt_data->bd,
150                         opt->boot_image_file);
151                 opt_data->bd->initrd = talloc_strdup(opt_data->bd,
152                         opt->initrd_file);
153                 opt_data->bd->args = talloc_strdup(opt_data->bd,
154                         opt->boot_args);
155                 opt_data->dev = dev;
156                 opt_data->opt = opt;
157                 opt_data->opt_hash = pb_opt_hash(dev, opt);
158
159                 /* Select the first item as default. */
160
161                 if (!selected_item)
162                         selected_item = i;
163
164                 /* If this is the default_item select it and start timer. */
165
166                 if (opt_data->opt_hash
167                         == device_item->sub_menu->default_item_hash) {
168                         selected_item = i;
169                         ui_timer_kick(&client->signal_data.timer);
170                 }
171         }
172
173         pbt_menu_set_selected(device_item->sub_menu, selected_item);
174
175         pbt_menu_show(frame->top_menu, 1);
176         twin_screen_update(client->frame.scr->tscreen);
177
178         return 0;
179
180 fail_sub_menu_create:
181 fail_device_item_create:
182         assert(0);
183         return -1;
184 }
185
186 static void pbt_device_remove(struct device *dev, struct pbt_client *client)
187 {
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;
194         struct pbt_item *i;
195
196         pb_log("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
197                 dev->n_options);
198
199         pb_protocol_dump_device(dev, "", pb_log_get_stream());
200
201         removed_item = NULL;
202         list_for_each_entry(i_list, i, list) {
203                 if (i->pb_device == dev) {
204                         removed_item = i;
205                         break;
206                 }
207         }
208
209         if (!removed_item) {
210                 pb_log("%s: %p %s: unknown device\n", __func__, dev, dev->id);
211                 assert(0 && "unknown device");
212                 return;
213         }
214
215         prev_item = list_prev_entry(i_list, removed_item, list);
216         next_item = list_next_entry(i_list, removed_item, list);
217
218         if (removed_item == frame->top_menu->selected) {
219                 if (prev_item)
220                         pbt_menu_set_selected(frame->top_menu, prev_item);
221                 else if (next_item)
222                         pbt_menu_set_selected(frame->top_menu, next_item);
223                 else
224                         assert(0 && "empty list");
225         }
226
227         if (next_item) {
228
229                 /* Shift items up. */
230
231                 i = next_item;
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;
237                 }
238         }
239
240         twin_window_hide(last_window);
241         twin_window_destroy(last_window);
242
243         list_remove(&removed_item->list);
244         removed_item->window = NULL;
245         talloc_free(removed_item);
246         frame->top_menu->n_items--;
247
248         pbt_menu_show(frame->top_menu, 1);
249         twin_screen_update(client->frame.scr->tscreen);
250 }
251
252 static struct discover_client_ops pbt_client_ops = {
253         .device_add = (void *)pbt_device_add,
254         .device_remove = (void *)pbt_device_remove,
255 };
256
257 static void pbt_client_destructor(struct pbt_client *client)
258 {
259         pb_log("%s\n", __func__);
260
261         // move to screen twin_x11_destroy(twin_ctx);
262         talloc_free(client->discover_client);
263         memset(client, 0, sizeof(*client));
264 }
265
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)
270 {
271         struct pbt_client *pbt_client;
272         unsigned int i;
273
274         pbt_client = talloc_zero(NULL, struct pbt_client);
275
276         if (!pbt_client) {
277                 pb_log("%s: alloc pbt_client failed.\n", __func__);
278                 fprintf(stderr, "%s: alloc pbt_client failed.\n", __func__);
279                 goto fail_alloc;
280         }
281
282         talloc_set_destructor(pbt_client, (void *)pbt_client_destructor);
283
284         pbt_client->waitset = waitset_create(pbt_client);
285
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);
291
292
293         if (!pbt_client->frame.scr)
294                 goto fail_scr_init;
295
296         /* Loop here for scripts that just started the server. */
297
298 retry_start:
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)
303                         break;
304                 pb_log("%s: waiting for server %d\n", __func__, i);
305                 sleep(1);
306         }
307
308         if (!pbt_client->discover_client && start_deamon) {
309                 int result;
310
311                 start_deamon = 0;
312
313                 result = pb_start_daemon();
314
315                 if (!result)
316                         goto retry_start;
317
318                 pb_log("%s: discover_client_init failed.\n", __func__);
319                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
320                         __func__);
321                 fprintf(stderr, "could not start pb-discover, the petitboot "
322                         "daemon.\n");
323                 goto fail_client_init;
324         }
325
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",
329                         __func__);
330                 fprintf(stderr, "check that pb-discover, "
331                         "the petitboot daemon is running.\n");
332                 goto fail_client_init;
333         }
334
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);
339
340         return pbt_client;
341
342 fail_client_init:
343         talloc_free(pbt_client);
344 fail_scr_init:
345 fail_alloc:
346         return NULL;
347 }