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"
30 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
32 struct boot_editor *boot_editor;
34 assert(scr->sig == pb_boot_editor_sig);
35 boot_editor = (struct boot_editor *)
36 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
37 assert(boot_editor->scr.sig == pb_boot_editor_sig);
41 static struct boot_editor *boot_editor_from_arg(void *arg)
43 struct boot_editor *boot_editor = arg;
45 assert(boot_editor->scr.sig == pb_boot_editor_sig);
50 * boot_editor_move_cursor - Move the cursor, setting correct attributes.
51 * @req: An ncurses request or char to send to form_driver().
54 static int boot_editor_move_cursor(struct boot_editor *boot_editor, int req)
58 wchgat(boot_editor->scr.sub_ncw, 1,
59 boot_editor_attr_field_selected, 0, 0);
60 result = form_driver(boot_editor->ncf, req);
61 wchgat(boot_editor->scr.sub_ncw, 1, boot_editor->attr_cursor, 0, 0);
62 wrefresh(boot_editor->scr.main_ncw);
67 * boot_editor_insert_mode_set - Set the insert mode.
70 static void boot_editor_insert_mode_set(struct boot_editor *boot_editor,
75 boot_editor->attr_cursor = boot_editor_attr_cursor_ins;
78 boot_editor->attr_cursor = boot_editor_attr_cursor_ovl;
81 assert(0 && "bad req");
84 boot_editor_move_cursor(boot_editor, req);
88 * boot_editor_insert_mode_tog - Toggle the insert mode.
91 static void boot_editor_insert_mode_tog(struct boot_editor *boot_editor)
93 if (boot_editor->attr_cursor == boot_editor_attr_cursor_ins)
94 boot_editor_insert_mode_set(boot_editor, REQ_OVL_MODE);
96 boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
100 * boot_editor_move_field - Move selected field, setting correct attributes.
101 * @req: An ncurses request to send to form_driver().
104 static int boot_editor_move_field(struct boot_editor *boot_editor, int req)
108 set_field_back(current_field(boot_editor->ncf),
109 boot_editor_attr_field_normal);
111 result = form_driver(boot_editor->ncf, req);
113 set_field_back(current_field(boot_editor->ncf),
114 boot_editor_attr_field_selected);
116 boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
120 static int boot_editor_post(struct nc_scr *scr)
122 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
124 post_form(boot_editor->ncf);
126 nc_scr_frame_draw(scr);
127 boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
128 boot_editor_move_field(boot_editor, REQ_END_FIELD);
129 boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
131 redrawwin(boot_editor->scr.main_ncw);
132 wrefresh(boot_editor->scr.main_ncw);
137 static int boot_editor_unpost(struct nc_scr *scr)
139 return unpost_form(boot_editor_from_scr(scr)->ncf);
142 static void boot_editor_resize(struct nc_scr *scr)
144 /* FIXME: forms can't be resized, need to recreate here */
145 boot_editor_unpost(scr);
146 boot_editor_post(scr);
150 * boot_editor_chomp - Eat leading and trailing WS.
153 static char *boot_editor_chomp(char *s)
157 char *const s_end = s + strlen(s);
159 for (; s < s_end; s++)
160 if (*s != ' ' && *s != '\t')
165 for (; s < s_end; s++)
166 if (*s != ' ' && *s != '\t')
172 static struct pb_boot_data *boot_editor_prepare_data(
173 struct boot_editor *boot_editor)
175 struct pb_boot_data *bd;
178 bd = talloc(boot_editor, struct pb_boot_data);
183 s = boot_editor_chomp(field_buffer(boot_editor->fields[0], 0));
184 bd->image = *s ? talloc_strdup(bd, s) : NULL;
186 s = boot_editor_chomp(field_buffer(boot_editor->fields[1], 0));
187 bd->initrd = *s ? talloc_strdup(bd, s) : NULL;
189 s = boot_editor_chomp(field_buffer(boot_editor->fields[2], 0));
190 bd->dtb = *s ? talloc_strdup(bd, s) : NULL;
192 s = boot_editor_chomp(field_buffer(boot_editor->fields[3], 0));
193 bd->args = *s ? talloc_strdup(bd, s) : NULL;
199 * boot_editor_process_key - Process a user keystroke.
201 * Called from the cui via the scr:process_key method.
204 static void boot_editor_process_key(struct nc_scr *scr, int key)
206 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
207 struct pb_boot_data *bd;
211 boot_editor_move_cursor(boot_editor, key);
216 boot_editor->on_exit(boot_editor, boot_editor_cancel, NULL);
221 form_driver(boot_editor->ncf, REQ_VALIDATION);
222 bd = boot_editor_prepare_data(boot_editor);
223 boot_editor->on_exit(boot_editor, 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(struct pmenu *menu,
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 char *image, *initrd, *dtb, *args;
318 struct boot_editor *boot_editor;
322 boot_editor = talloc_zero(menu, struct boot_editor);
327 talloc_set_destructor(boot_editor, boot_editor_destructor);
328 boot_editor->original_pmenu = menu;
330 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
331 menu, boot_editor_process_key,
332 boot_editor_post, boot_editor_unpost, boot_editor_resize);
334 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
335 "Petitboot Option Editor");
336 boot_editor->scr.frame.rtitle = NULL;
337 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
338 "ESC=cancel, Enter=accept");
340 boot_editor->on_exit = on_exit;
342 boot_editor->fields = talloc_array(boot_editor, FIELD *, 9);
350 image = initrd = dtb = args = "";
353 boot_editor->fields[0] = boot_editor_setup_field(0, 9, image);
354 boot_editor->fields[1] = boot_editor_setup_field(1, 9, initrd);
355 boot_editor->fields[2] = boot_editor_setup_field(2, 9, dtb);
356 boot_editor->fields[3] = boot_editor_setup_field(3, 9, args);
357 boot_editor->fields[4] = boot_editor_setup_label(0, 1, "image:");
358 boot_editor->fields[5] = boot_editor_setup_label(1, 1, "initrd:");
359 boot_editor->fields[6] = boot_editor_setup_label(2, 1, "dtb:");
360 boot_editor->fields[7] = boot_editor_setup_label(3, 1, "args:");
361 boot_editor->fields[8] = NULL;
363 boot_editor->ncf = new_form(boot_editor->fields);
365 set_form_win(boot_editor->ncf, boot_editor->scr.main_ncw);
366 set_form_sub(boot_editor->ncf, boot_editor->scr.sub_ncw);