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