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