]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-boot-editor.c
ui/ncurses: Draw boot editor into a scrollable pad
[petitboot] / ui / ncurses / nc-boot-editor.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #define _GNU_SOURCE
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "log/log.h"
27 #include "talloc/talloc.h"
28 #include "nc-boot-editor.h"
29 #include "nc-widgets.h"
30
31 struct boot_editor {
32         struct nc_scr           scr;
33         struct cui              *cui;
34         void                    *data;
35         struct pmenu_item       *item;
36         void                    (*on_exit)(struct cui *cui,
37                                         struct pmenu_item *item,
38                                         struct pb_boot_data *bd);
39
40         int                     label_x;
41         int                     field_x;
42         int                     scroll_y;
43
44         WINDOW                  *pad;
45         struct nc_widgetset     *widgetset;
46         struct {
47                 struct nc_widget_label          *device_l;
48                 struct nc_widget_select         *device_f;
49                 struct nc_widget_label          *image_l;
50                 struct nc_widget_textbox        *image_f;
51                 struct nc_widget_label          *initrd_l;
52                 struct nc_widget_textbox        *initrd_f;
53                 struct nc_widget_label          *dtb_l;
54                 struct nc_widget_textbox        *dtb_f;
55                 struct nc_widget_label          *args_l;
56                 struct nc_widget_textbox        *args_f;
57                 struct nc_widget_button         *ok_b;
58                 struct nc_widget_button         *cancel_b;
59         } widgets;
60
61         const char              *selected_device;
62 };
63
64 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
65 {
66         struct boot_editor *boot_editor;
67
68         assert(scr->sig == pb_boot_editor_sig);
69         boot_editor = (struct boot_editor *)
70                 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
71         assert(boot_editor->scr.sig == pb_boot_editor_sig);
72         return boot_editor;
73 }
74
75 static void pad_refresh(struct boot_editor *boot_editor)
76 {
77         int y, x, rows, cols;
78
79         getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
80         getbegyx(boot_editor->scr.sub_ncw, y, x);
81
82         prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
83                         y, x, rows, cols);
84 }
85
86 static struct boot_editor *boot_editor_from_arg(void *arg)
87 {
88         struct boot_editor *boot_editor = arg;
89
90         assert(boot_editor->scr.sig == pb_boot_editor_sig);
91         return boot_editor;
92 }
93
94 static int boot_editor_post(struct nc_scr *scr)
95 {
96         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
97
98         widgetset_post(boot_editor->widgetset);
99         nc_scr_frame_draw(scr);
100         pad_refresh(boot_editor);
101         return 0;
102 }
103
104 static int boot_editor_unpost(struct nc_scr *scr)
105 {
106         widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
107         return 0;
108 }
109
110 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
111 {
112         return &boot_editor->scr;
113 }
114
115 static void boot_editor_resize(struct nc_scr *scr)
116 {
117         /* FIXME: forms can't be resized, need to recreate here */
118         boot_editor_unpost(scr);
119         boot_editor_post(scr);
120 }
121
122 static char *conditional_prefix(struct pb_boot_data *ctx,
123                 const char *prefix, const char *value)
124 {
125         const char *sep;
126
127         if (!value || !*value)
128                 return NULL;
129
130         sep = NULL;
131         if (!prefix)
132                 prefix = "";
133         else if (prefix[strlen(prefix)] != '/')
134                 sep = "/";
135
136         return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
137 }
138
139 static struct pb_boot_data *boot_editor_prepare_data(
140                 struct boot_editor *boot_editor)
141 {
142         struct pb_boot_data *bd;
143         char *s, *prefix;
144         int idx;
145
146         bd = talloc(boot_editor, struct pb_boot_data);
147
148         if (!bd)
149                 return NULL;
150
151         idx = widget_select_get_value(boot_editor->widgets.device_f);
152         if (idx == -1 || (unsigned int)idx >
153                         boot_editor->cui->sysinfo->n_blockdevs)
154                 prefix = NULL;
155         else
156                 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
157
158         s = widget_textbox_get_value(boot_editor->widgets.image_f);
159         bd->image = conditional_prefix(bd, prefix, s);
160
161         s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
162         bd->initrd = conditional_prefix(bd, prefix, s);
163
164         s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
165         bd->dtb = conditional_prefix(bd, prefix, s);
166
167         s = widget_textbox_get_value(boot_editor->widgets.args_f);
168         bd->args = *s ? talloc_strdup(bd, s) : NULL;
169
170         return bd;
171 }
172
173 /**
174  * boot_editor_process_key - Process a user keystroke.
175  *
176  * Called from the cui via the scr:process_key method.
177  */
178
179 static void boot_editor_process_key(struct nc_scr *scr, int key)
180 {
181         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
182         bool handled;
183
184         handled = widgetset_process_key(boot_editor->widgetset, key);
185         if (handled) {
186                 pad_refresh(boot_editor);
187                 return;
188         }
189
190         switch (key) {
191         case 'x':
192         case 27: /* ESC */
193                 boot_editor->on_exit(boot_editor->cui, NULL, NULL);
194                 nc_flush_keys();
195         }
196 }
197
198 /**
199  * boot_editor_destructor - The talloc destructor for a boot_editor.
200  */
201
202 static int boot_editor_destructor(void *arg)
203 {
204         struct boot_editor *boot_editor = boot_editor_from_arg(arg);
205         boot_editor->scr.sig = pb_removed_sig;
206         if (boot_editor->pad)
207                 delwin(boot_editor->pad);
208         return 0;
209 }
210
211 static void ok_click(void *arg)
212 {
213         struct boot_editor *boot_editor = arg;
214         struct pb_boot_data *bd;
215
216         bd = boot_editor_prepare_data(boot_editor);
217         boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd);
218 }
219
220 static void cancel_click(void *arg)
221 {
222         struct boot_editor *boot_editor = arg;
223         boot_editor->on_exit(boot_editor->cui, NULL, NULL);
224 }
225
226 static int layout_pair(struct boot_editor *boot_editor, int y,
227                 struct nc_widget_label *label,
228                 struct nc_widget_textbox *field)
229 {
230         struct nc_widget *label_w = widget_label_base(label);
231         struct nc_widget *field_w = widget_textbox_base(field);
232         widget_move(label_w, y, boot_editor->label_x);
233         widget_move(field_w, y, boot_editor->field_x);
234         return max(widget_height(label_w), widget_height(field_w));
235 }
236
237 static int pad_height(int blockdevs_height)
238 {
239         return 10 + blockdevs_height;
240 }
241
242 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
243 {
244         struct nc_widget *wf, *wl;
245         int y = 1;
246
247         wl = widget_label_base(boot_editor->widgets.device_l);
248         wf = widget_select_base(boot_editor->widgets.device_f);
249         widget_move(wl, y, boot_editor->label_x);
250         widget_move(wf, y, boot_editor->field_x);
251
252         y += widget_height(wf) + 1;
253
254
255         y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
256                                          boot_editor->widgets.image_f);
257
258         y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
259                                          boot_editor->widgets.initrd_f);
260
261         y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
262                                          boot_editor->widgets.dtb_f);
263
264         y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
265                                          boot_editor->widgets.args_f);
266
267
268         y++;
269         widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
270         widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 19);
271
272         pad_refresh(boot_editor);
273 }
274
275 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
276 {
277         struct boot_editor *boot_editor = arg;
278         int w_y, w_height, s_max;
279
280         w_y = widget_y(widget);
281         w_height = widget_height(widget);
282         s_max = getmaxy(boot_editor->scr.sub_ncw);
283
284         if (w_y < boot_editor->scroll_y)
285                 boot_editor->scroll_y = w_y;
286
287         else if (w_y + w_height + boot_editor->scroll_y > s_max - 1)
288                 boot_editor->scroll_y = 1 + w_y + w_height - s_max;
289
290         else
291                 return;
292
293         pad_refresh(boot_editor);
294 }
295
296 static void boot_editor_device_select_change(void *arg, int idx)
297 {
298         struct boot_editor *boot_editor = arg;
299         if (idx == -1)
300                 boot_editor->selected_device = NULL;
301         else
302                 boot_editor->selected_device =
303                         boot_editor->cui->sysinfo->blockdevs[idx]->name;
304 }
305
306 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
307                 const struct system_info *sysinfo)
308 {
309         struct nc_widget_select *select = boot_editor->widgets.device_f;
310         unsigned int i;
311         bool selected;
312
313         widget_select_drop_options(select);
314
315         for (i = 0; i < sysinfo->n_blockdevs; i++) {
316                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
317                 const char *name;
318
319                 name = talloc_asprintf(boot_editor, "%s [%s]",
320                                 bd_info->name, bd_info->uuid);
321                 selected = boot_editor->selected_device &&
322                                 !strcmp(bd_info->name,
323                                                 boot_editor->selected_device);
324
325                 widget_select_add_option(boot_editor->widgets.device_f,
326                                 i, name, selected);
327         }
328
329         /* If we're editing an existing option, the paths will be fully-
330          * resolved. In this case, we want the manual device pre-selected.
331          * However, we only do this if the widget hasn't been manually
332          * changed. */
333         selected = !boot_editor->selected_device;
334
335         widget_select_add_option(boot_editor->widgets.device_f,
336                         -1, "Specify paths/URLs manually", selected);
337 }
338
339 static bool path_on_device(struct blockdev_info *bd_info,
340                 const char *path)
341 {
342         int len;
343
344         if (!bd_info->mountpoint)
345                 return false;
346
347         len = strlen(bd_info->mountpoint);
348         if (strncmp(bd_info->mountpoint, path, len))
349                 return false;
350
351         /* if the mountpoint doesn't have a trailing slash, ensure that
352          * the path starts with one (so we don't match a "/mnt/sda1/foo" path
353          * on a "/mnt/sda" mountpoint) */
354         return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
355 }
356
357
358 static void boot_editor_find_device(struct boot_editor *boot_editor,
359                 struct pb_boot_data *bd, const struct system_info *sysinfo,
360                 char **image, char **initrd, char **dtb)
361 {
362         struct blockdev_info *bd_info, *tmp;
363         unsigned int i, len;
364
365         if (!sysinfo || !sysinfo->n_blockdevs)
366                 return;
367
368         /* find the device for our boot image, by finding the longest
369          * matching blockdev's mountpoint */
370         for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
371                 tmp = sysinfo->blockdevs[i];
372                 if (!path_on_device(tmp, bd->image))
373                         continue;
374                 if (strlen(tmp->mountpoint) <= len)
375                         continue;
376                 bd_info = tmp;
377                 len = strlen(tmp->mountpoint);
378         }
379
380         if (!bd_info)
381                 return;
382
383         /* ensure that other paths are on this device */
384         if (bd->initrd && !path_on_device(bd_info, bd->initrd))
385                 return;
386
387         if (bd->dtb && !path_on_device(bd_info, bd->dtb))
388                 return;
389
390         /* ok, we match; preselect the device option, and remove the common
391          * prefix */
392         boot_editor->selected_device = bd_info->name;
393         *image += len;
394
395         if (*initrd)
396                 *initrd += len;
397         if (*dtb)
398                 *dtb += len;
399 }
400
401 struct boot_editor *boot_editor_init(struct cui *cui,
402                 struct pmenu_item *item,
403                 const struct system_info *sysinfo,
404                 void (*on_exit)(struct cui *cui,
405                                 struct pmenu_item *item,
406                                 struct pb_boot_data *bd))
407 {
408         char *image, *initrd, *dtb, *args;
409         struct boot_editor *boot_editor;
410         struct nc_widgetset *set;
411         int field_size;
412
413         (void)sysinfo;
414
415         boot_editor = talloc_zero(cui, struct boot_editor);
416
417         if (!boot_editor)
418                 return NULL;
419
420         talloc_set_destructor(boot_editor, boot_editor_destructor);
421         boot_editor->cui = cui;
422         boot_editor->item = item;
423         boot_editor->on_exit = on_exit;
424
425         boot_editor->label_x = 1;
426         boot_editor->field_x = 9;
427
428         nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
429                         cui, boot_editor_process_key,
430                 boot_editor_post, boot_editor_unpost, boot_editor_resize);
431
432         boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
433                         "Petitboot Option Editor");
434         boot_editor->scr.frame.rtitle = NULL;
435         boot_editor->scr.frame.help = talloc_strdup(boot_editor,
436                         "Enter=accept");
437
438         if (item) {
439                 struct pb_boot_data *bd = cod_from_item(item)->bd;
440                 image = bd->image;
441                 initrd = bd->initrd;
442                 dtb = bd->dtb;
443                 args = bd->args;
444                 boot_editor_find_device(boot_editor, bd, sysinfo,
445                                 &image, &initrd, &dtb);
446         } else {
447                 image = initrd = dtb = args = "";
448         }
449
450         field_size = COLS - 1 - boot_editor->field_x;
451
452         boot_editor->pad = newpad(pad_height(sysinfo->n_blockdevs), COLS);
453
454         boot_editor->widgetset = set = widgetset_create(boot_editor,
455                         boot_editor->scr.main_ncw,
456                         boot_editor->pad);
457
458         widgetset_set_widget_focus(boot_editor->widgetset,
459                         boot_editor_widget_focus, boot_editor);
460
461         boot_editor->widgets.device_l = widget_new_label(set, 0, 0, "device:");
462         boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
463                                                 field_size);
464         widget_select_on_change(boot_editor->widgets.device_f,
465                         boot_editor_device_select_change, boot_editor);
466
467         boot_editor_populate_device_select(boot_editor, sysinfo);
468
469         boot_editor->widgets.image_l = widget_new_label(set, 0, 0, "image:");
470         boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
471                                                 field_size, image);
472
473         boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0, "initrd:");
474         boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
475                                                 field_size, initrd);
476
477         boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0, "dtb:");
478         boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
479                                                 field_size, dtb);
480
481         boot_editor->widgets.args_l = widget_new_label(set, 0, 0, "args:");
482         boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
483                                         field_size, args);
484
485         boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
486                                         "OK", ok_click, boot_editor);
487         boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
488                                         "Cancel", cancel_click, boot_editor);
489
490         boot_editor_layout_widgets(boot_editor);
491
492         return boot_editor;
493 }