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 *image_l;
46 struct nc_widget_textbox *image_f;
47 struct nc_widget_label *initrd_l;
48 struct nc_widget_textbox *initrd_f;
49 struct nc_widget_label *dtb_l;
50 struct nc_widget_textbox *dtb_f;
51 struct nc_widget_label *args_l;
52 struct nc_widget_textbox *args_f;
53 struct nc_widget_button *ok_b;
54 struct nc_widget_button *cancel_b;
58 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
60 struct boot_editor *boot_editor;
62 assert(scr->sig == pb_boot_editor_sig);
63 boot_editor = (struct boot_editor *)
64 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
65 assert(boot_editor->scr.sig == pb_boot_editor_sig);
69 static struct boot_editor *boot_editor_from_arg(void *arg)
71 struct boot_editor *boot_editor = arg;
73 assert(boot_editor->scr.sig == pb_boot_editor_sig);
77 static int boot_editor_post(struct nc_scr *scr)
79 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
81 widgetset_post(boot_editor->widgetset);
82 nc_scr_frame_draw(scr);
83 redrawwin(boot_editor->scr.main_ncw);
84 wrefresh(boot_editor->scr.main_ncw);
89 static int boot_editor_unpost(struct nc_scr *scr)
91 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
95 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
97 return &boot_editor->scr;
100 static void boot_editor_resize(struct nc_scr *scr)
102 /* FIXME: forms can't be resized, need to recreate here */
103 boot_editor_unpost(scr);
104 boot_editor_post(scr);
107 static struct pb_boot_data *boot_editor_prepare_data(
108 struct boot_editor *boot_editor)
110 struct pb_boot_data *bd;
113 bd = talloc(boot_editor, struct pb_boot_data);
118 s = widget_textbox_get_value(boot_editor->widgets.image_f);
119 bd->image = *s ? talloc_strdup(bd, s) : NULL;
121 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
122 bd->initrd = *s ? talloc_strdup(bd, s) : NULL;
124 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
125 bd->dtb = *s ? talloc_strdup(bd, s) : NULL;
127 s = widget_textbox_get_value(boot_editor->widgets.args_f);
128 bd->args = *s ? talloc_strdup(bd, s) : NULL;
134 * boot_editor_process_key - Process a user keystroke.
136 * Called from the cui via the scr:process_key method.
139 static void boot_editor_process_key(struct nc_scr *scr, int key)
141 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
144 handled = widgetset_process_key(boot_editor->widgetset, key);
146 wrefresh(boot_editor->scr.main_ncw);
153 boot_editor->on_exit(boot_editor->cui, NULL, NULL);
159 * boot_editor_destructor - The talloc destructor for a boot_editor.
162 static int boot_editor_destructor(void *arg)
164 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
165 boot_editor->scr.sig = pb_removed_sig;
169 static void ok_click(void *arg)
171 struct boot_editor *boot_editor = arg;
172 struct pb_boot_data *bd;
174 bd = boot_editor_prepare_data(boot_editor);
175 boot_editor->on_exit(boot_editor->cui, boot_editor->item, bd);
178 static void cancel_click(void *arg)
180 struct boot_editor *boot_editor = arg;
181 boot_editor->on_exit(boot_editor->cui, NULL, NULL);
184 static int layout_pair(struct boot_editor *boot_editor, int y,
185 struct nc_widget_label *label,
186 struct nc_widget_textbox *field)
188 struct nc_widget *label_w = widget_label_base(label);
189 struct nc_widget *field_w = widget_textbox_base(field);
190 widget_move(label_w, y, boot_editor->label_x);
191 widget_move(field_w, y, boot_editor->field_x);
192 return max(widget_height(label_w), widget_height(field_w));
195 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
199 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
200 boot_editor->widgets.image_f);
202 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
203 boot_editor->widgets.initrd_f);
205 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
206 boot_editor->widgets.dtb_f);
208 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
209 boot_editor->widgets.args_f);
213 widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
214 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 19);
217 struct boot_editor *boot_editor_init(struct cui *cui,
218 struct pmenu_item *item,
219 const struct system_info *sysinfo,
220 void (*on_exit)(struct cui *cui,
221 struct pmenu_item *item,
222 struct pb_boot_data *bd))
224 char *image, *initrd, *dtb, *args;
225 struct boot_editor *boot_editor;
226 struct nc_widgetset *set;
231 boot_editor = talloc_zero(cui, struct boot_editor);
236 talloc_set_destructor(boot_editor, boot_editor_destructor);
237 boot_editor->cui = cui;
238 boot_editor->item = item;
239 boot_editor->on_exit = on_exit;
241 boot_editor->label_x = 1;
242 boot_editor->field_x = 9;
244 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
245 cui, boot_editor_process_key,
246 boot_editor_post, boot_editor_unpost, boot_editor_resize);
248 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
249 "Petitboot Option Editor");
250 boot_editor->scr.frame.rtitle = NULL;
251 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
255 struct pb_boot_data *bd = cod_from_item(item)->bd;
261 image = initrd = dtb = args = "";
264 field_size = COLS - 1 - boot_editor->field_x;
266 boot_editor->widgetset = set = widgetset_create(boot_editor,
267 boot_editor->scr.main_ncw,
268 boot_editor->scr.sub_ncw);
270 boot_editor->widgets.image_l = widget_new_label(set, 0, 0, "image:");
271 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
274 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0, "initrd:");
275 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
278 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0, "dtb:");
279 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
282 boot_editor->widgets.args_l = widget_new_label(set, 0, 0, "args:");
283 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
286 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
287 "OK", ok_click, boot_editor);
288 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 6,
289 "Cancel", cancel_click, boot_editor);
291 boot_editor_layout_widgets(boot_editor);