]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-boot-editor.c
ui/ncurses: Allow text wrapping in select widgets
[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 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "log/log.h"
27 #include "talloc/talloc.h"
28 #include "i18n/i18n.h"
29 #include "nc-boot-editor.h"
30 #include "nc-widgets.h"
31
32 struct boot_editor {
33         struct nc_scr           scr;
34         struct cui              *cui;
35         void                    *data;
36         struct pmenu_item       *item;
37         enum {
38                 STATE_EDIT,
39                 STATE_CANCEL,
40                 STATE_SAVE,
41                 STATE_HELP,
42         }                       state;
43         void                    (*on_exit)(struct cui *cui,
44                                         struct pmenu_item *item,
45                                         struct pb_boot_data *bd);
46         bool                    need_redraw;
47         bool                    need_update;
48
49         int                     label_x;
50         int                     field_x;
51         int                     scroll_y;
52
53         WINDOW                  *pad;
54         struct nc_widgetset     *widgetset;
55         struct {
56                 struct nc_widget_label          *device_l;
57                 struct nc_widget_select         *device_f;
58                 struct nc_widget_label          *image_l;
59                 struct nc_widget_textbox        *image_f;
60                 struct nc_widget_label          *initrd_l;
61                 struct nc_widget_textbox        *initrd_f;
62                 struct nc_widget_label          *dtb_l;
63                 struct nc_widget_textbox        *dtb_f;
64                 struct nc_widget_label          *args_l;
65                 struct nc_widget_textbox        *args_f;
66                 struct nc_widget_button         *ok_b;
67                 struct nc_widget_button         *help_b;
68                 struct nc_widget_button         *cancel_b;
69         } widgets;
70
71         const char              *selected_device;
72         char                    *image;
73         char                    *initrd;
74         char                    *dtb;
75         char                    *args;
76 };
77
78 extern const struct help_text boot_editor_help_text;
79
80 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
81 {
82         struct boot_editor *boot_editor;
83
84         assert(scr->sig == pb_boot_editor_sig);
85         boot_editor = (struct boot_editor *)
86                 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
87         assert(boot_editor->scr.sig == pb_boot_editor_sig);
88         return boot_editor;
89 }
90
91 static void pad_refresh(struct boot_editor *boot_editor)
92 {
93         int y, x, rows, cols;
94
95         getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
96         getbegyx(boot_editor->scr.sub_ncw, y, x);
97
98         prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
99                         y, x, rows, cols);
100 }
101
102 static struct boot_editor *boot_editor_from_arg(void *arg)
103 {
104         struct boot_editor *boot_editor = arg;
105
106         assert(boot_editor->scr.sig == pb_boot_editor_sig);
107         return boot_editor;
108 }
109
110 static int boot_editor_post(struct nc_scr *scr)
111 {
112         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
113
114         if (boot_editor->need_update) {
115                 boot_editor_update(boot_editor, boot_editor->cui->sysinfo);
116                 boot_editor->need_update = false;
117         } else {
118                 widgetset_post(boot_editor->widgetset);
119         }
120
121         nc_scr_frame_draw(scr);
122         if (boot_editor->need_redraw) {
123                 redrawwin(scr->main_ncw);
124                 boot_editor->need_redraw = false;
125         }
126         wrefresh(boot_editor->scr.main_ncw);
127         pad_refresh(boot_editor);
128         return 0;
129 }
130
131 static int boot_editor_unpost(struct nc_scr *scr)
132 {
133         widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
134         return 0;
135 }
136
137 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
138 {
139         return &boot_editor->scr;
140 }
141
142 static void boot_editor_resize(struct nc_scr *scr)
143 {
144         /* FIXME: forms can't be resized, need to recreate here */
145         boot_editor_unpost(scr);
146         boot_editor_post(scr);
147 }
148
149 static char *conditional_prefix(struct pb_boot_data *ctx,
150                 const char *prefix, const char *value)
151 {
152         const char *sep;
153
154         if (!value || !*value)
155                 return NULL;
156
157         sep = "";
158         if (!prefix)
159                 prefix = "";
160         else if (prefix[strlen(prefix)] != '/')
161                 sep = "/";
162
163         return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
164 }
165
166 static struct pb_boot_data *boot_editor_prepare_data(
167                 struct boot_editor *boot_editor)
168 {
169         struct pb_boot_data *bd;
170         char *s, *prefix;
171         int idx;
172
173         bd = talloc(boot_editor, struct pb_boot_data);
174
175         if (!bd)
176                 return NULL;
177
178         idx = widget_select_get_value(boot_editor->widgets.device_f);
179         if (idx == -1 || (unsigned int)idx >
180                         boot_editor->cui->sysinfo->n_blockdevs)
181                 prefix = NULL;
182         else
183                 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
184
185         s = widget_textbox_get_value(boot_editor->widgets.image_f);
186         bd->image = conditional_prefix(bd, prefix, s);
187         if (!bd->image) {
188                 talloc_free(bd);
189                 return NULL;
190         }
191
192         s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
193         bd->initrd = conditional_prefix(bd, prefix, s);
194
195         s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
196         bd->dtb = conditional_prefix(bd, prefix, s);
197
198         s = widget_textbox_get_value(boot_editor->widgets.args_f);
199         bd->args = *s ? talloc_strdup(bd, s) : NULL;
200
201         return bd;
202 }
203
204 /**
205  * boot_editor_process_key - Process a user keystroke.
206  *
207  * Called from the cui via the scr:process_key method.
208  */
209
210 static void boot_editor_process_key(struct nc_scr *scr, int key)
211 {
212         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
213         struct pmenu_item *item;
214         struct pb_boot_data *bd;
215         bool handled;
216
217         handled = widgetset_process_key(boot_editor->widgetset, key);
218         if (handled)
219                 pad_refresh(boot_editor);
220
221         else if (key == 'x' || key == 27)
222                 boot_editor->state = STATE_CANCEL;
223
224         else if (key == 'h')
225                 boot_editor->state = STATE_HELP;
226
227         item = NULL;
228         bd = NULL;
229
230         switch (boot_editor->state) {
231         case STATE_SAVE:
232                 item = boot_editor->item;
233                 bd = boot_editor_prepare_data(boot_editor);
234                 if (!bd) {
235                         /* Incomplete entry */
236                         boot_editor->state = STATE_EDIT;
237                         break;
238                 }
239                 /* fall through */
240         case STATE_CANCEL:
241                 boot_editor->on_exit(boot_editor->cui, item, bd);
242                 break;
243         case STATE_HELP:
244                 boot_editor->state = STATE_EDIT;
245                 boot_editor->need_redraw = true;
246                 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
247                                 &boot_editor_help_text);
248                 break;
249         default:
250                 break;
251         }
252 }
253
254 /**
255  * boot_editor_destructor - The talloc destructor for a boot_editor.
256  */
257
258 static int boot_editor_destructor(void *arg)
259 {
260         struct boot_editor *boot_editor = boot_editor_from_arg(arg);
261         boot_editor->scr.sig = pb_removed_sig;
262         if (boot_editor->pad)
263                 delwin(boot_editor->pad);
264         return 0;
265 }
266
267 static void ok_click(void *arg)
268 {
269         struct boot_editor *boot_editor = arg;
270         boot_editor->state = STATE_SAVE;
271 }
272
273 static void help_click(void *arg)
274 {
275         struct boot_editor *boot_editor = arg;
276         boot_editor->state = STATE_HELP;
277 }
278
279 static void cancel_click(void *arg)
280 {
281         struct boot_editor *boot_editor = arg;
282         boot_editor->state = STATE_CANCEL;
283 }
284
285 static int layout_pair(struct boot_editor *boot_editor, int y,
286                 struct nc_widget_label *label,
287                 struct nc_widget_textbox *field)
288 {
289         struct nc_widget *label_w = widget_label_base(label);
290         struct nc_widget *field_w = widget_textbox_base(field);
291         widget_move(label_w, y, boot_editor->label_x);
292         widget_move(field_w, y, boot_editor->field_x);
293         return max(widget_height(label_w), widget_height(field_w));
294 }
295
296 static int pad_height(int blockdevs_height)
297 {
298         return 10 + blockdevs_height;
299 }
300
301 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
302 {
303         struct nc_widget *wf, *wl;
304         int y = 1;
305
306         wl = widget_label_base(boot_editor->widgets.device_l);
307         wf = widget_select_base(boot_editor->widgets.device_f);
308         widget_move(wl, y, boot_editor->label_x);
309         widget_move(wf, y, boot_editor->field_x);
310
311         y += widget_height(wf) + 1;
312
313
314         y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
315                                          boot_editor->widgets.image_f);
316
317         y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
318                                          boot_editor->widgets.initrd_f);
319
320         y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
321                                          boot_editor->widgets.dtb_f);
322
323         y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
324                                          boot_editor->widgets.args_f);
325
326
327         y++;
328         widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
329                     boot_editor->field_x);
330         widget_move(widget_button_base(boot_editor->widgets.help_b), y,
331                     boot_editor->field_x + 14);
332         widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
333                     boot_editor->field_x + 28);
334 }
335
336 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
337 {
338         struct boot_editor *boot_editor = arg;
339         int w_y, s_max;
340
341         w_y = widget_y(widget) + widget_focus_y(widget);
342         s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
343
344         if (w_y < boot_editor->scroll_y)
345                 boot_editor->scroll_y = w_y;
346
347         else if (w_y + boot_editor->scroll_y + 1 > s_max)
348                 boot_editor->scroll_y = 1 + w_y - s_max;
349
350         else
351                 return;
352
353         pad_refresh(boot_editor);
354 }
355
356 static void boot_editor_device_select_change(void *arg, int idx)
357 {
358         struct boot_editor *boot_editor = arg;
359         if (idx == -1)
360                 boot_editor->selected_device = NULL;
361         else
362                 boot_editor->selected_device =
363                         boot_editor->cui->sysinfo->blockdevs[idx]->name;
364 }
365
366 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
367                 const struct system_info *sysinfo)
368 {
369         struct nc_widget_select *select = boot_editor->widgets.device_f;
370         unsigned int i;
371         bool selected;
372
373         widget_select_drop_options(select);
374
375         for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
376                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
377                 const char *name;
378
379                 name = talloc_asprintf(boot_editor, "%s [%s]",
380                                 bd_info->name, bd_info->uuid);
381                 selected = boot_editor->selected_device &&
382                                 !strcmp(bd_info->name,
383                                                 boot_editor->selected_device);
384
385                 widget_select_add_option(select, i, name, selected);
386         }
387
388         /* If we're editing an existing option, the paths will be fully-
389          * resolved. In this case, we want the manual device pre-selected.
390          * However, we only do this if the widget hasn't been manually
391          * changed. */
392         selected = !boot_editor->selected_device;
393
394         widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
395                         selected);
396 }
397
398 static bool path_on_device(struct blockdev_info *bd_info,
399                 const char *path)
400 {
401         int len;
402
403         if (!bd_info->mountpoint)
404                 return false;
405
406         len = strlen(bd_info->mountpoint);
407         if (strncmp(bd_info->mountpoint, path, len))
408                 return false;
409
410         /* if the mountpoint doesn't have a trailing slash, ensure that
411          * the path starts with one (so we don't match a "/mnt/sda1/foo" path
412          * on a "/mnt/sda" mountpoint) */
413         return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
414 }
415
416
417 static void boot_editor_find_device(struct boot_editor *boot_editor,
418                 struct pb_boot_data *bd, const struct system_info *sysinfo)
419 {
420         struct blockdev_info *bd_info, *tmp;
421         unsigned int i, len;
422
423         if (!sysinfo || !sysinfo->n_blockdevs)
424                 return;
425
426         /* find the device for our boot image, by finding the longest
427          * matching blockdev's mountpoint */
428         for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
429                 tmp = sysinfo->blockdevs[i];
430                 if (!path_on_device(tmp, bd->image))
431                         continue;
432                 if (strlen(tmp->mountpoint) <= len)
433                         continue;
434                 bd_info = tmp;
435                 len = strlen(tmp->mountpoint);
436         }
437
438         if (!bd_info)
439                 return;
440
441         /* ensure that other paths are on this device */
442         if (bd->initrd && !path_on_device(bd_info, bd->initrd))
443                 return;
444
445         if (bd->dtb && !path_on_device(bd_info, bd->dtb))
446                 return;
447
448         /* ok, we match; preselect the device option, and remove the common
449          * prefix */
450         boot_editor->selected_device = bd_info->name;
451         boot_editor->image += len;
452
453         if (boot_editor->initrd)
454                 boot_editor->initrd += len;
455         if (boot_editor->dtb)
456                 boot_editor->dtb += len;
457 }
458
459 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
460                 const struct system_info *sysinfo)
461 {
462         struct nc_widgetset *set;
463         int field_size;
464
465         field_size = COLS - 1 - boot_editor->field_x;
466
467         boot_editor->widgetset = set = widgetset_create(boot_editor,
468                         boot_editor->scr.main_ncw,
469                         boot_editor->pad);
470
471         widgetset_set_widget_focus(boot_editor->widgetset,
472                         boot_editor_widget_focus, boot_editor);
473
474         boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
475                         _("Device:"));
476         boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
477                                                 field_size);
478         widget_select_on_change(boot_editor->widgets.device_f,
479                         boot_editor_device_select_change, boot_editor);
480
481         boot_editor_populate_device_select(boot_editor, sysinfo);
482
483         boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
484                         _("Kernel:"));
485         boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
486                                                 field_size, boot_editor->image);
487
488         boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
489                         _("Initrd:"));
490         boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
491                                                 field_size,
492                                                 boot_editor->initrd);
493
494         boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
495                         _("Device tree:"));
496         boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
497                                                 field_size, boot_editor->dtb);
498
499         boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
500                         _("Boot arguments:"));
501         boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
502                                         field_size, boot_editor->args);
503
504         boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
505                                         _("OK"), ok_click, boot_editor);
506         boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
507                                         _("Help"), help_click, boot_editor);
508         boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
509                                         _("Cancel"), cancel_click, boot_editor);
510 }
511
512 void boot_editor_update(struct boot_editor *boot_editor,
513                 const struct system_info *sysinfo)
514 {
515         int height;
516
517         if (boot_editor->cui->current != boot_editor_scr(boot_editor)) {
518                 boot_editor->need_update = true;
519                 return;
520         }
521
522         widgetset_unpost(boot_editor->widgetset);
523
524         height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
525         if (getmaxy(boot_editor->pad) < height) {
526                 delwin(boot_editor->pad);
527                 boot_editor->pad = newpad(height, COLS);
528                 widgetset_set_windows(boot_editor->widgetset,
529                                 boot_editor->scr.main_ncw,
530                                 boot_editor->pad);
531         }
532
533         boot_editor_populate_device_select(boot_editor, sysinfo);
534
535         boot_editor_layout_widgets(boot_editor);
536
537         widgetset_post(boot_editor->widgetset);
538
539         pad_refresh(boot_editor);
540 }
541
542 struct boot_editor *boot_editor_init(struct cui *cui,
543                 struct pmenu_item *item,
544                 const struct system_info *sysinfo,
545                 void (*on_exit)(struct cui *cui,
546                                 struct pmenu_item *item,
547                                 struct pb_boot_data *bd))
548 {
549         struct boot_editor *boot_editor;
550
551         boot_editor = talloc_zero(cui, struct boot_editor);
552
553         if (!boot_editor)
554                 return NULL;
555
556         talloc_set_destructor(boot_editor, boot_editor_destructor);
557         boot_editor->cui = cui;
558         boot_editor->item = item;
559         boot_editor->on_exit = on_exit;
560         boot_editor->state = STATE_EDIT;
561         boot_editor->need_redraw = false;
562         boot_editor->need_update = false;
563
564         int ncols1 = strncols(_("Device tree:"));
565         int ncols2 = strncols(_("Boot arguments:"));
566
567         boot_editor->label_x = 1;
568         boot_editor->field_x = 2 + max(ncols1, ncols2);
569
570         nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
571                         cui, boot_editor_process_key,
572                 boot_editor_post, boot_editor_unpost, boot_editor_resize);
573
574         boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
575                         _("Petitboot Option Editor"));
576         boot_editor->scr.frame.rtitle = NULL;
577         boot_editor->scr.frame.help = talloc_strdup(boot_editor,
578                         _("tab=next, shift+tab=previous, x=exit, h=help"));
579         nc_scr_frame_draw(&boot_editor->scr);
580
581         if (item) {
582                 struct pb_boot_data *bd = cod_from_item(item)->bd;
583                 boot_editor->image = bd->image;
584                 boot_editor->initrd = bd->initrd;
585                 boot_editor->dtb = bd->dtb;
586                 boot_editor->args = bd->args;
587                 boot_editor_find_device(boot_editor, bd, sysinfo);
588         } else {
589                 boot_editor->image = boot_editor->initrd =
590                         boot_editor->dtb = boot_editor->args = "";
591         }
592
593         boot_editor->pad = newpad(
594                                 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
595                                 COLS);
596
597         boot_editor_setup_widgets(boot_editor, sysinfo);
598
599         boot_editor_layout_widgets(boot_editor);
600         wrefresh(boot_editor->scr.main_ncw);
601
602         return boot_editor;
603 }