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->args = *s ? talloc_strdup(bd, s) : NULL;
194 * boot_editor_process_key - Process a user keystroke.
196 * Called from the cui via the scr:process_key method.
199 static void boot_editor_process_key(struct nc_scr *scr)
201 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
202 struct pb_boot_data *bd;
210 /* DBGS("%d (%o)\n", c, c); */
214 boot_editor_move_cursor(boot_editor, c);
219 boot_editor->on_exit(boot_editor,
220 boot_editor_cancel, NULL);
225 form_driver(boot_editor->ncf, REQ_VALIDATION);
226 bd = boot_editor_prepare_data(boot_editor);
227 boot_editor->on_exit(boot_editor,
228 boot_editor_update, bd);
234 boot_editor_insert_mode_tog(boot_editor);
239 boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
242 boot_editor_move_field(boot_editor, REQ_LAST_FIELD);
245 boot_editor_move_field(boot_editor, REQ_NEXT_FIELD);
248 boot_editor_move_field(boot_editor, REQ_PREV_FIELD);
253 boot_editor_move_cursor(boot_editor, REQ_BEG_FIELD);
256 boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
259 boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR);
262 boot_editor_move_cursor(boot_editor, REQ_RIGHT_CHAR);
265 if (boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR)
267 boot_editor_move_cursor(boot_editor,
271 boot_editor_move_cursor(boot_editor, REQ_DEL_CHAR);
278 * boot_editor_destructor - The talloc destructor for a boot_editor.
281 static int boot_editor_destructor(void *arg)
283 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
286 for (f = boot_editor->fields; *f; f++)
289 free_form(boot_editor->ncf);
290 boot_editor->scr.sig = pb_removed_sig;
295 static FIELD *boot_editor_setup_field(unsigned int y, unsigned int x, char *str)
299 f = new_field(1, COLS - 1 - x, y, x, 0, 0);
300 field_opts_off(f, O_STATIC | O_WRAP);
301 set_max_field(f, 256);
302 set_field_buffer(f, 0, str);
303 set_field_status(f, 0);
307 static FIELD *boot_editor_setup_label(unsigned int y, unsigned int x, char *str)
311 f = new_field(1, strlen(str), y, x, 0, 0);
312 field_opts_off(f, O_ACTIVE);
313 set_field_buffer(f, 0, str);
317 struct boot_editor *boot_editor_init(void *ui_ctx,
318 const struct pb_boot_data *bd,
319 void (*on_exit)(struct boot_editor *,
320 enum boot_editor_result,
321 struct pb_boot_data *))
323 struct boot_editor *boot_editor;
325 pb_log("%s: image: '%s'\n", __func__, bd->image);
326 pb_log("%s: initrd: '%s'\n", __func__, bd->initrd);
327 pb_log("%s: args: '%s'\n", __func__, bd->args);
331 boot_editor = talloc_zero(ui_ctx, struct boot_editor);
336 talloc_set_destructor(boot_editor, boot_editor_destructor);
338 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
339 ui_ctx, boot_editor_process_key,
340 boot_editor_post, boot_editor_unpost, boot_editor_resize);
342 boot_editor->scr.frame.title = talloc_strdup(boot_editor,
343 "Petitboot Option Editor");
344 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
345 "ESC=cancel, Enter=accept");
347 boot_editor->on_exit = on_exit;
349 boot_editor->fields = talloc_array(boot_editor, FIELD *, 7);
351 boot_editor->fields[0] = boot_editor_setup_field(0, 9, bd->image);
352 boot_editor->fields[1] = boot_editor_setup_field(1, 9, bd->initrd);
353 boot_editor->fields[2] = boot_editor_setup_field(2, 9, bd->args);
354 boot_editor->fields[3] = boot_editor_setup_label(0, 1, "image:");
355 boot_editor->fields[4] = boot_editor_setup_label(1, 1, "initrd:");
356 boot_editor->fields[5] = boot_editor_setup_label(2, 1, "args:");
357 boot_editor->fields[6] = NULL;
359 boot_editor->ncf = new_form(boot_editor->fields);
361 set_form_win(boot_editor->ncf, boot_editor->scr.main_ncw);
362 set_form_sub(boot_editor->ncf, boot_editor->scr.sub_ncw);