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
25 #include "talloc/talloc.h"
26 #include "nc-boot-editor.h"
28 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
30 struct boot_editor *boot_editor;
32 assert(scr->sig == pb_boot_editor_sig);
33 boot_editor = (struct boot_editor *)
34 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
35 assert(boot_editor->scr.sig == pb_boot_editor_sig);
39 static struct boot_editor *boot_editor_from_arg(void *arg)
41 struct boot_editor *boot_editor = arg;
43 assert(boot_editor->scr.sig == pb_boot_editor_sig);
48 * boot_editor_move_cursor - Move the cursor, setting correct attributes.
49 * @req: An ncurses request or char to send to form_driver().
52 static int boot_editor_move_cursor(struct boot_editor *boot_editor, int req)
56 wchgat(boot_editor->scr.sub_ncw, 1,
57 boot_editor_attr_field_selected, 0, 0);
58 result = form_driver(boot_editor->ncf, req);
59 wchgat(boot_editor->scr.sub_ncw, 1, boot_editor->attr_cursor, 0, 0);
60 wrefresh(boot_editor->scr.main_ncw);
65 * boot_editor_insert_mode_set - Set the insert mode.
68 static void boot_editor_insert_mode_set(struct boot_editor *boot_editor,
73 boot_editor->attr_cursor = boot_editor_attr_cursor_ins;
76 boot_editor->attr_cursor = boot_editor_attr_cursor_ovl;
79 assert(0 && "bad req");
82 boot_editor_move_cursor(boot_editor, req);
86 * boot_editor_insert_mode_tog - Toggle the insert mode.
89 static void boot_editor_insert_mode_tog(struct boot_editor *boot_editor)
91 if (boot_editor->attr_cursor == boot_editor_attr_cursor_ins)
92 boot_editor_insert_mode_set(boot_editor, REQ_OVL_MODE);
94 boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
98 * boot_editor_move_field - Move selected field, setting correct attributes.
99 * @req: An ncurses request to send to form_driver().
102 static int boot_editor_move_field(struct boot_editor *boot_editor, int req)
106 set_field_back(current_field(boot_editor->ncf),
107 boot_editor_attr_field_normal);
109 result = form_driver(boot_editor->ncf, req);
111 set_field_back(current_field(boot_editor->ncf),
112 boot_editor_attr_field_selected);
114 boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
118 static int boot_editor_post(struct nc_scr *scr)
120 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
122 post_form(boot_editor->ncf);
124 nc_scr_frame_draw(scr);
125 boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
126 boot_editor_move_field(boot_editor, REQ_END_FIELD);
127 boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
129 redrawwin(boot_editor->scr.main_ncw);
130 wrefresh(boot_editor->scr.main_ncw);
135 static int boot_editor_unpost(struct nc_scr *scr)
137 return unpost_form(boot_editor_from_scr(scr)->ncf);
140 static void boot_editor_resize(struct nc_scr *scr)
142 /* FIXME: forms can't be resized, need to recreate here */
143 boot_editor_unpost(scr);
144 boot_editor_post(scr);
148 * boot_editor_chomp - Eat leading and trailing WS.
151 static char *boot_editor_chomp(char *s)
155 char *const s_end = s + strlen(s);
157 for (; s < s_end; s++)
158 if (*s != ' ' && *s != '\t')
163 for (; s < s_end; s++)
164 if (*s != ' ' && *s != '\t')
170 static struct pb_boot_data *boot_editor_prepare_data(
171 struct boot_editor *boot_editor)
173 struct pb_boot_data *bd;
176 bd = talloc(boot_editor, struct pb_boot_data);
181 s = boot_editor_chomp(field_buffer(boot_editor->fields[0], 0));
182 bd->image = *s ? talloc_strdup(bd, s) : NULL;
184 s = boot_editor_chomp(field_buffer(boot_editor->fields[1], 0));
185 bd->initrd = *s ? talloc_strdup(bd, s) : NULL;
187 s = boot_editor_chomp(field_buffer(boot_editor->fields[2], 0));
188 bd->dtb = *s ? talloc_strdup(bd, s) : NULL;
190 s = boot_editor_chomp(field_buffer(boot_editor->fields[3], 0));
191 bd->args = *s ? talloc_strdup(bd, s) : NULL;
197 * boot_editor_process_key - Process a user keystroke.
199 * Called from the cui via the scr:process_key method.
202 static void boot_editor_process_key(struct nc_scr *scr, int key)
204 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
205 struct pb_boot_data *bd;
209 boot_editor_move_cursor(boot_editor, key);
214 boot_editor->on_exit(boot_editor,
215 boot_editor_cancel, NULL);
220 form_driver(boot_editor->ncf, REQ_VALIDATION);
221 bd = boot_editor_prepare_data(boot_editor);
222 boot_editor->on_exit(boot_editor,
223 boot_editor_update, bd);
229 boot_editor_insert_mode_tog(boot_editor);
234 boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
237 boot_editor_move_field(boot_editor, REQ_LAST_FIELD);
240 boot_editor_move_field(boot_editor, REQ_NEXT_FIELD);
243 boot_editor_move_field(boot_editor, REQ_PREV_FIELD);
248 boot_editor_move_cursor(boot_editor, REQ_BEG_FIELD);
251 boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
254 boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR);
257 boot_editor_move_cursor(boot_editor, REQ_RIGHT_CHAR);
260 if (boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR)
262 boot_editor_move_cursor(boot_editor,
266 boot_editor_move_cursor(boot_editor, REQ_DEL_CHAR);
272 * boot_editor_destructor - The talloc destructor for a boot_editor.
275 static int boot_editor_destructor(void *arg)
277 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
280 for (f = boot_editor->fields; *f; f++)
283 free_form(boot_editor->ncf);
284 boot_editor->scr.sig = pb_removed_sig;
289 static FIELD *boot_editor_setup_field(unsigned int y, unsigned int x, char *str)
293 f = new_field(1, COLS - 1 - x, y, x, 0, 0);
294 field_opts_off(f, O_STATIC | O_WRAP);
295 set_max_field(f, 256);
296 set_field_buffer(f, 0, str);
297 set_field_status(f, 0);
301 static FIELD *boot_editor_setup_label(unsigned int y, unsigned int x, char *str)
305 f = new_field(1, strlen(str), y, x, 0, 0);
306 field_opts_off(f, O_ACTIVE);
307 set_field_buffer(f, 0, str);
311 struct boot_editor *boot_editor_init(void *ui_ctx,
312 const struct pb_boot_data *bd,
313 void (*on_exit)(struct boot_editor *,
314 enum boot_editor_result,
315 struct pb_boot_data *))
317 struct boot_editor *boot_editor;
319 pb_log("%s: image: '%s'\n", __func__, bd->image);
320 pb_log("%s: initrd: '%s'\n", __func__, bd->initrd);
321 pb_log("%s: dtb: '%s'\n", __func__, bd->dtb);
322 pb_log("%s: args: '%s'\n", __func__, bd->args);
326 boot_editor = talloc_zero(ui_ctx, struct boot_editor);
331 talloc_set_destructor(boot_editor, boot_editor_destructor);
333 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
334 ui_ctx, boot_editor_process_key,
335 boot_editor_post, boot_editor_unpost, boot_editor_resize);
337 boot_editor->scr.frame.title = talloc_strdup(boot_editor,
338 "Petitboot Option Editor");
339 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
340 "ESC=cancel, Enter=accept");
342 boot_editor->on_exit = on_exit;
344 boot_editor->fields = talloc_array(boot_editor, FIELD *, 9);
346 boot_editor->fields[0] = boot_editor_setup_field(0, 9, bd->image);
347 boot_editor->fields[1] = boot_editor_setup_field(1, 9, bd->initrd);
348 boot_editor->fields[2] = boot_editor_setup_field(2, 9, bd->dtb);
349 boot_editor->fields[3] = boot_editor_setup_field(3, 9, bd->args);
350 boot_editor->fields[4] = boot_editor_setup_label(0, 1, "image:");
351 boot_editor->fields[5] = boot_editor_setup_label(1, 1, "initrd:");
352 boot_editor->fields[6] = boot_editor_setup_label(2, 1, "dtb:");
353 boot_editor->fields[7] = boot_editor_setup_label(3, 1, "args:");
354 boot_editor->fields[8] = NULL;
356 boot_editor->ncf = new_form(boot_editor->fields);
358 set_form_win(boot_editor->ncf, boot_editor->scr.main_ncw);
359 set_form_sub(boot_editor->ncf, boot_editor->scr.sub_ncw);