discover/grub2: Allow to separate the --id argument using a space char
[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 #include <assert.h>
23 #include <string.h>
24
25 #include <pb-protocol/pb-protocol.h>
26
27 #include "pbt-client.h"
28
29 #include "log/log.h"
30 #include "talloc/talloc.h"
31 #include "waiter/waiter.h"
32 #include "process/process.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_boot(struct pbt_item *item)
56 {
57         int result;
58         struct pb_opt_data *opt_data = pbt_opt_data_from_item(item);
59
60         pb_debug("%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         result = discover_client_boot(item->pbt_client->discover_client,
66                         NULL, opt_data->opt, opt_data->bd);
67
68         if (result) {
69                 pb_log_fn("failed: %s\n", opt_data->bd->image);
70                 pbt_frame_status_printf(&item->pbt_client->frame,
71                                 "Failed: kexec %s", opt_data->bd->image);
72         }
73
74         return 0;
75 }
76
77 static int pbt_client_on_edit(struct pbt_item *item)
78 {
79         DBGS("*** %s ***\n", pbt_item_name(item));
80         return 0;
81 }
82
83 static int pbt_device_add(struct device *dev, struct pbt_client *client)
84 {
85         struct pbt_frame *const frame = &client->frame;
86         struct pbt_item *item;
87         struct pbt_quad q;
88         const char *icon_file;
89
90         pb_debug("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
91                 dev->n_options);
92
93         pb_protocol_dump_device(dev, "", pb_log_get_stream());
94
95         // FIXME: check for existing dev->id
96
97         icon_file = dev->icon_file ? dev->icon_file : pbt_icon_chooser(dev->id);
98
99         item = pbt_item_create_reduced(frame->top_menu, dev->id,
100                 frame->top_menu->n_items, icon_file);
101
102         if (!item)
103                 return -1;
104
105         item->pb_device = dev;
106         dev->ui_info = item;
107         frame->top_menu->n_items++;
108
109         /* sub_menu */
110         q.x = frame->top_menu->window->pixmap->width;
111         q.y = 0;
112         q.width = frame->top_menu->scr->tscreen->width - q.x;
113         q.height = frame->top_menu->scr->tscreen->height;
114
115         item->sub_menu = pbt_menu_create(item, dev->id,
116                 frame->top_menu->scr, frame->top_menu, &q,
117                 &frame->top_menu->layout);
118         if (!item->sub_menu)
119                 return -1;
120
121         pbt_menu_show(frame->top_menu, 1);
122
123         return 0;
124 }
125
126 static int pbt_boot_option_add(struct device *dev, struct boot_option *opt,
127                 struct pbt_client *client)
128 {
129         struct pbt_item *opt_item, *device_item;
130         struct pb_opt_data *opt_data;
131
132         device_item = dev->ui_info;
133
134         opt_item = pbt_item_create(device_item->sub_menu,
135                         opt->id, device_item->sub_menu->n_items++,
136                         opt->icon_file, opt->name, opt->description);
137
138         opt_item->pb_opt = opt;
139         opt_item->pbt_client = client;
140         opt_item->on_execute = pbt_client_boot;
141         opt_item->on_edit = pbt_client_on_edit;
142
143         opt_item->data = opt_data = talloc(opt_item, struct pb_opt_data);
144         opt_data->name = opt->name;
145         opt_data->bd = talloc(opt_item, struct pb_boot_data);
146         opt_data->bd->image = talloc_strdup(opt_data->bd,
147                 opt->boot_image_file);
148         opt_data->bd->initrd = talloc_strdup(opt_data->bd,
149                 opt->initrd_file);
150         opt_data->bd->dtb = talloc_strdup(opt_data->bd,
151                 opt->dtb_file);
152         opt_data->bd->args = talloc_strdup(opt_data->bd,
153                 opt->boot_args);
154         opt_data->opt = opt;
155         opt_data->opt_hash = pb_opt_hash(dev, opt);
156
157         /* If this is the default_item select it and start timer. */
158         if (opt_data->opt_hash == device_item->sub_menu->default_item_hash) {
159                 device_item->selected_item = opt_item;
160                 pbt_menu_set_selected(device_item->sub_menu, opt_item);
161                 ui_timer_kick(&client->signal_data.timer);
162         }
163
164         /* Select the first item as default. */
165         if (!device_item->selected_item)
166                 pbt_menu_set_selected(device_item->sub_menu, opt_item);
167
168         twin_screen_update(client->frame.scr->tscreen);
169
170         return 0;
171 }
172
173 static void pbt_device_remove(struct device *dev, struct pbt_client *client)
174 {
175         struct pbt_frame *const frame = &client->frame;
176         struct list *i_list = frame->top_menu->item_list;
177         twin_window_t *last_window = NULL;
178         struct pbt_item *removed_item;
179         struct pbt_item *prev_item;
180         struct pbt_item *next_item;
181         struct pbt_item *i;
182
183         pb_debug("%s: %p %s: n_options %d\n", __func__, dev, dev->id,
184                 dev->n_options);
185
186         pb_protocol_dump_device(dev, "", pb_log_get_stream());
187
188         removed_item = NULL;
189         list_for_each_entry(i_list, i, list) {
190                 if (i->pb_device == dev) {
191                         removed_item = i;
192                         break;
193                 }
194         }
195
196         if (!removed_item) {
197                 pb_log_fn("%p %s: unknown device\n", dev, dev->id);
198                 assert(0 && "unknown device");
199                 return;
200         }
201
202         prev_item = list_prev_entry(i_list, removed_item, list);
203         next_item = list_next_entry(i_list, removed_item, list);
204
205         if (removed_item == frame->top_menu->selected) {
206                 if (prev_item)
207                         pbt_menu_set_selected(frame->top_menu, prev_item);
208                 else if (next_item)
209                         pbt_menu_set_selected(frame->top_menu, next_item);
210                 else
211                         assert(0 && "empty list");
212         }
213
214         if (next_item) {
215
216                 /* Shift items up. */
217
218                 i = next_item;
219                 list_for_each_entry_continue(i_list, i, list) {
220                         last_window = i->window;
221                         i->window = list_prev_entry(i_list, i, list)->window;
222                         twin_window_set_name(i->window, last_window->name);
223                         i->window->client_data = last_window->client_data;
224                 }
225         }
226
227         twin_window_hide(last_window);
228         twin_window_destroy(last_window);
229
230         list_remove(&removed_item->list);
231         removed_item->window = NULL;
232         talloc_free(removed_item);
233         frame->top_menu->n_items--;
234
235         pbt_menu_show(frame->top_menu, 1);
236         twin_screen_update(client->frame.scr->tscreen);
237 }
238
239 static struct discover_client_ops pbt_client_ops = {
240         .device_add = (void *)pbt_device_add,
241         .boot_option_add = (void *)pbt_boot_option_add,
242         .device_remove = (void *)pbt_device_remove,
243 };
244
245 static void pbt_client_destructor(struct pbt_client *client)
246 {
247         pb_debug("%s\n", __func__);
248
249         // move to screen twin_x11_destroy(twin_ctx);
250         talloc_free(client->discover_client);
251         memset(client, 0, sizeof(*client));
252 }
253
254 struct pbt_client *pbt_client_init(enum pbt_twin_backend backend,
255         unsigned int width, unsigned int height, int start_daemon)
256 {
257         struct pbt_client *pbt_client;
258         unsigned int i;
259
260         pbt_client = talloc_zero(NULL, struct pbt_client);
261
262         if (!pbt_client) {
263                 pb_log_fn("alloc pbt_client failed.\n");
264                 fprintf(stderr, "%s: alloc pbt_client failed.\n", __func__);
265                 goto fail_alloc;
266         }
267
268         talloc_set_destructor(pbt_client, (void *)pbt_client_destructor);
269
270         pbt_client->waitset = waitset_create(pbt_client);
271
272         process_init(pbt_client, pbt_client->waitset, false);
273
274         pbt_client->sig = "pbt_client";
275         pbt_client->frame.scr = pbt_scr_init(pbt_client, pbt_client->waitset,
276                         backend, width, height, NULL, NULL);
277
278
279         if (!pbt_client->frame.scr)
280                 goto fail_scr_init;
281
282         /* Loop here for scripts that just started the server. */
283
284 retry_start:
285         for (i = start_daemon ? 2 : 10; i; i--) {
286                 pbt_client->discover_client
287                         = discover_client_init(pbt_client->waitset,
288                                         &pbt_client_ops, pbt_client);
289                 if (pbt_client->discover_client || !i)
290                         break;
291                 pb_log_fn("waiting for server %d\n", i);
292                 sleep(1);
293         }
294
295         if (!pbt_client->discover_client && start_daemon) {
296                 int result;
297
298                 start_daemon = 0;
299
300                 result = pb_start_daemon(pbt_client);
301
302                 if (!result)
303                         goto retry_start;
304
305                 pb_log_fn("discover_client_init failed.\n");
306                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
307                         __func__);
308                 fprintf(stderr, "could not start pb-discover, the petitboot "
309                         "daemon.\n");
310                 goto fail_client_init;
311         }
312
313         if (!pbt_client->discover_client) {
314                 pb_log_fn("discover_client_init failed.\n");
315                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
316                         __func__);
317                 fprintf(stderr, "check that pb-discover, "
318                         "the petitboot daemon is running.\n");
319                 goto fail_client_init;
320         }
321
322         return pbt_client;
323
324 fail_client_init:
325         talloc_free(pbt_client);
326 fail_scr_init:
327 fail_alloc:
328         return NULL;
329 }