2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
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.
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.
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
27 #include "talloc/talloc.h"
28 #include "nc-boot-editor.h"
29 #include "nc-widgets.h"
35 struct pmenu_item *item;
36 void (*on_exit)(struct cui *cui,
37 struct pmenu_item *item,
38 struct pb_boot_data *bd);
43 struct nc_widgetset *widgetset;
45 struct nc_widget_label *device_l;
46 struct nc_widget_select *device_f;
47 struct nc_widget_label *image_l;
48 struct nc_widget_textbox *image_f;
49 struct nc_widget_label *initrd_l;
50 struct nc_widget_textbox *initrd_f;
51 struct nc_widget_label *dtb_l;
52 struct nc_widget_textbox *dtb_f;
53 struct nc_widget_label *args_l;
54 struct nc_widget_textbox *args_f;
55 struct nc_widget_button *ok_b;
56 struct nc_widget_button *cancel_b;
59 const char *selected_device;
62 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
64 struct boot_editor *boot_editor;
66 assert(scr->sig == pb_boot_editor_sig);
67 boot_editor = (struct boot_editor *)
68 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
69 assert(boot_editor->scr.sig == pb_boot_editor_sig);
73 static struct boot_editor *boot_editor_from_arg(void *arg)
75 struct boot_editor *boot_editor = arg;
77 assert(boot_editor->scr.sig == pb_boot_editor_sig);
81 static int boot_editor_post(struct nc_scr *scr)
83 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
85 widgetset_post(boot_editor->widgetset);
86 nc_scr_frame_draw(scr);
87 redrawwin(boot_editor->scr.main_ncw);
88 wrefresh(boot_editor->scr.main_ncw);
93 static int boot_editor_unpost(struct nc_scr *scr)
95 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
99 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
101 return &boot_editor->scr;
104 static void boot_editor_resize(struct nc_scr *scr)
106 /* FIXME: forms can't be resized, need to recreate here */
107 boot_editor_unpost(scr);
108 boot_editor_post(scr);
111 static char *conditional_prefix(struct pb_boot_data *ctx,
112 const char *prefix, const char *value)
116 if (!value || !*value)
122 else if (prefix[strlen(prefix)] != '/')
125 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
128 static struct pb_boot_data *boot_editor_prepare_data(
129 struct boot_editor *boot_editor)
131 struct pb_boot_data *bd;
135 bd = talloc(boot_editor, struct pb_boot_data);
140 idx = widget_select_get_value(boot_editor->widgets.device_f);
141 if (idx == -1 || (unsigned int)idx >
142 boot_editor->cui->sysinfo->n_blockdevs)
145 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
147 s = widget_textbox_get_value(boot_editor->widgets.image_f);
148 bd->image = conditional_prefix(bd, prefix, s);
150 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
151 bd->initrd = conditional_prefix(bd, prefix, s);
153 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
154 bd->dtb = conditional_prefix(bd, prefix, s);
156 s = widget_textbox_get_value(boot_editor->widgets.args_f);
157 bd->args = *s ? talloc_strdup(bd, s) : NULL;
163 * boot_editor_process_key - Process a user keystroke.
165 * Called from the cui via the scr:process_key method.
168 static void boot_editor_process_key(struct nc_scr *scr, int key)
170 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
173 handled = widgetset_process_key(boot_editor->widgetset, key);
175 wrefresh(boot_editor->scr.main_ncw);
182 boot_editor->on_exit(boot_editor->cui, NULL, NULL);
188 * boot_editor_destructor - The talloc destructor for a boot_editor.
191 static int boot_editor_destructor(void *arg)
193 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
194 boot_editor->scr.sig = pb_removed_sig;
198 static void ok_click(void *arg)
200 struct boot_editor *boot_editor = arg;
201 struct pb_boot_data *bd;
203 bd = boot_editor_prepare_data(boot_editor);
204 boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd);
207 static void cancel_click(void *arg)
209 struct boot_editor *boot_editor = arg;
210 boot_editor->on_exit(boot_editor->cui, NULL, NULL);
213 static int layout_pair(struct boot_editor *boot_editor, int y,
214 struct nc_widget_label *label,
215 struct nc_widget_textbox *field)
217 struct nc_widget *label_w = widget_label_base(label);
218 struct nc_widget *field_w = widget_textbox_base(field);
219 widget_move(label_w, y, boot_editor->label_x);
220 widget_move(field_w, y, boot_editor->field_x);
221 return max(widget_height(label_w), widget_height(field_w));
224 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
226 struct nc_widget *wf, *wl;
229 wl = widget_label_base(boot_editor->widgets.device_l);
230 wf = widget_select_base(boot_editor->widgets.device_f);
231 widget_move(wl, y, boot_editor->label_x);
232 widget_move(wf, y, boot_editor->field_x);
234 y += widget_height(wf) + 1;
237 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
238 boot_editor->widgets.image_f);
240 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
241 boot_editor->widgets.initrd_f);
243 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
244 boot_editor->widgets.dtb_f);
246 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
247 boot_editor->widgets.args_f);
251 widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
252 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 19);
255 static void boot_editor_device_select_change(void *arg, int idx)
257 struct boot_editor *boot_editor = arg;
259 boot_editor->selected_device = NULL;
261 boot_editor->selected_device =
262 boot_editor->cui->sysinfo->blockdevs[idx]->name;
265 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
266 const struct system_info *sysinfo)
268 struct nc_widget_select *select = boot_editor->widgets.device_f;
272 widget_select_drop_options(select);
274 for (i = 0; i < sysinfo->n_blockdevs; i++) {
275 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
278 name = talloc_asprintf(boot_editor, "%s [%s]",
279 bd_info->name, bd_info->uuid);
280 selected = boot_editor->selected_device &&
281 !strcmp(bd_info->name,
282 boot_editor->selected_device);
284 widget_select_add_option(boot_editor->widgets.device_f,
288 /* If we're editing an existing option, the paths will be fully-
289 * resolved. In this case, we want the manual device pre-selected.
290 * However, we only do this if the widget hasn't been manually
292 selected = boot_editor->item && !boot_editor->selected_device;
294 widget_select_add_option(boot_editor->widgets.device_f,
295 -1, "Specify paths/URLs manually", selected);
298 struct boot_editor *boot_editor_init(struct cui *cui,
299 struct pmenu_item *item,
300 const struct system_info *sysinfo,
301 void (*on_exit)(struct cui *cui,
302 struct pmenu_item *item,
303 struct pb_boot_data *bd))
305 char *image, *initrd, *dtb, *args;
306 struct boot_editor *boot_editor;
307 struct nc_widgetset *set;
312 boot_editor = talloc_zero(cui, struct boot_editor);
317 talloc_set_destructor(boot_editor, boot_editor_destructor);
318 boot_editor->cui = cui;
319 boot_editor->item = item;
320 boot_editor->on_exit = on_exit;
322 boot_editor->label_x = 1;
323 boot_editor->field_x = 9;
325 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
326 cui, boot_editor_process_key,
327 boot_editor_post, boot_editor_unpost, boot_editor_resize);
329 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
330 "Petitboot Option Editor");
331 boot_editor->scr.frame.rtitle = NULL;
332 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
336 struct pb_boot_data *bd = cod_from_item(item)->bd;
342 image = initrd = dtb = args = "";
345 field_size = COLS - 1 - boot_editor->field_x;
347 boot_editor->widgetset = set = widgetset_create(boot_editor,
348 boot_editor->scr.main_ncw,
349 boot_editor->scr.sub_ncw);
351 boot_editor->widgets.device_l = widget_new_label(set, 0, 0, "device:");
352 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
354 widget_select_on_change(boot_editor->widgets.device_f,
355 boot_editor_device_select_change, boot_editor);
357 boot_editor_populate_device_select(boot_editor, sysinfo);
359 boot_editor->widgets.image_l = widget_new_label(set, 0, 0, "image:");
360 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
363 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0, "initrd:");
364 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
367 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0, "dtb:");
368 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
371 boot_editor->widgets.args_l = widget_new_label(set, 0, 0, "args:");
372 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
375 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
376 "OK", ok_click, boot_editor);
377 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
378 "Cancel", cancel_click, boot_editor);
380 boot_editor_layout_widgets(boot_editor);