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