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 <sys/ioctl.h>
28 #include "pb-protocol/pb-protocol.h"
29 #include "talloc/talloc.h"
30 #include "waiter/waiter.h"
31 #include "ui/common/discover-client.h"
34 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
40 * cui_abort - Signal the main cui program loop to exit.
42 * Sets cui.abort, which causes the cui_run() routine to return.
45 void cui_abort(struct cui *cui)
47 pb_log("%s: exiting\n", __func__);
52 * cui_resize - Signal the main cui program loop to resize
57 void cui_resize(struct cui *cui)
59 pb_log("%s: resizing\n", __func__);
64 * cui_on_exit - A generic main menu ESC callback.
67 void cui_on_exit(struct pmenu *menu)
69 cui_abort(cui_from_pmenu(menu));
73 * cui_run_cmd - A generic cb to run the supplied command.
76 int cui_run_cmd(struct pmenu_item *item)
79 struct cui *cui = cui_from_item(item);
80 const char *const *cmd_argv = item->data;
82 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
86 result = pb_run_cmd(cmd_argv, 1, 0);
89 redrawwin(cui->current->main_ncw);
92 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
93 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
100 * cui_boot - A generic cb to run kexec.
103 static int cui_boot(struct pmenu_item *item)
106 struct cui *cui = cui_from_item(item);
107 struct cui_opt_data *cod = cod_from_item(item);
109 assert(cui->current == &cui->main->scr);
111 pb_log("%s: %s\n", __func__, cod->name);
113 pb_log("%s: missing opt?\n", __func__);
117 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
121 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
124 redrawwin(cui->current->main_ncw);
127 nc_scr_status_printf(cui->current,
128 "Failed: boot %s", cod->bd->image);
135 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
138 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor, enum boot_editor_result boot_editor_result,
139 struct pb_boot_data *bd)
141 struct cui *cui = cui_from_arg(boot_editor->scr.ui_ctx);
143 if (boot_editor_result == boot_editor_update) {
144 struct pmenu_item *i = pmenu_find_selected(cui->main);
145 struct cui_opt_data *cod = cod_from_item(i);
150 talloc_free(cod->bd);
153 pmenu_item_replace(i, cod->name);
155 /* FIXME: need to make item visible somehow */
156 set_current_item(cui->main->ncm, i->nci);
158 pb_log("%s: updating opt '%s'\n", __func__, cod->name);
159 pb_log(" image '%s'\n", cod->bd->image);
160 pb_log(" initrd '%s'\n", cod->bd->initrd);
161 pb_log(" dtb '%s'\n", cod->bd->dtb);
162 pb_log(" args '%s'\n", cod->bd->args);
165 cui_set_current(cui, &cui->main->scr);
167 talloc_free(boot_editor);
170 int cui_boot_editor_run(struct pmenu_item *item)
172 struct cui *cui = cui_from_item(item);
173 struct cui_opt_data *cod = cod_from_item(item);
174 struct boot_editor *boot_editor;
176 boot_editor = boot_editor_init(cui, cod->bd, cui_boot_editor_on_exit);
177 cui_set_current(cui, &boot_editor->scr);
183 * cui_set_current - Set the currently active screen and redraw it.
186 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
190 DBGS("%p -> %p\n", cui->current, scr);
192 assert(cui->current != scr);
198 cui->current->post(cui->current);
203 static bool process_global_keys(struct cui *cui, int key)
207 if (cui->current && cui->current->main_ncw) {
208 redrawwin(cui->current->main_ncw);
209 wrefresh(cui->current->main_ncw);
217 * cui_process_key - Process input on stdin.
220 static int cui_process_key(void *arg)
222 struct cui *cui = cui_from_arg(arg);
224 assert(cui->current);
227 discover_client_cancel_default(cui->client);
228 cui->has_input = true;
236 if (process_global_keys(cui, c))
239 cui->current->process_key(cui->current, c);
246 * cui_process_js - Process joystick events.
249 static int cui_process_js(void *arg)
251 struct cui *cui = cui_from_arg(arg);
254 c = pjs_process_event(cui->pjs);
258 cui_process_key(arg);
265 * cui_handle_resize - Handle the term resize.
268 static void cui_handle_resize(struct cui *cui)
272 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
273 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
277 pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
279 wclear(cui->current->main_ncw);
280 resize_term(ws.ws_row, ws.ws_col);
281 cui->current->resize(cui->current);
283 /* For some reason this makes ncurses redraw the screen */
285 redrawwin(cui->current->main_ncw);
286 wrefresh(cui->current->main_ncw);
290 * cui_on_open - Open new item callback.
293 void cui_on_open(struct pmenu *menu)
295 unsigned int insert_pt;
296 struct pmenu_item *i;
297 struct cui_opt_data *cod;
299 menu->scr.unpost(&menu->scr);
301 /* This disconnects items array from menu. */
303 set_menu_items(menu->ncm, NULL);
305 /* Insert new items at insert_pt. */
307 insert_pt = pmenu_grow(menu, 1);
308 i = pmenu_item_alloc(menu);
310 i->on_edit = cui_boot_editor_run;
311 i->on_execute = cui_boot;
312 i->data = cod = talloc_zero(i, struct cui_opt_data);
314 cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
315 cod->bd = talloc_zero(i, struct pb_boot_data);
317 pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
319 /* Re-attach the items array. */
321 set_menu_items(menu->ncm, menu->items);
323 menu->scr.post(&menu->scr);
324 set_current_item(menu->ncm, i->nci);
330 * cui_device_add - Client device_add callback.
332 * Creates menu_items for all the device boot_options and inserts those
333 * menu_items into the main menu. Redraws the main menu if it is active.
336 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
339 struct cui *cui = cui_from_arg(arg);
340 struct cui_opt_data *cod;
341 unsigned int insert_pt;
342 struct pmenu_item *i;
346 pb_log("%s: %p %s\n", __func__, opt, opt->id);
348 selected = current_item(cui->main->ncm);
350 if (cui->current == &cui->main->scr)
351 cui->current->unpost(cui->current);
353 /* This disconnects items array from menu. */
355 result = set_menu_items(cui->main->ncm, NULL);
358 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
360 /* Insert new items at insert_pt. */
361 insert_pt = pmenu_grow(cui->main, 1);
363 /* Save the item in opt->ui_info for cui_device_remove() */
365 opt->ui_info = i = pmenu_item_alloc(cui->main);
367 i->on_edit = cui_boot_editor_run;
368 i->on_execute = cui_boot;
369 i->data = cod = talloc(i, struct cui_opt_data);
373 cod->opt_hash = pb_opt_hash(dev, opt);
374 cod->name = opt->name;
375 cod->bd = talloc(i, struct pb_boot_data);
377 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
378 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
379 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
380 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
382 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
384 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
385 pb_log(" image '%s'\n", cod->bd->image);
386 pb_log(" initrd '%s'\n", cod->bd->initrd);
387 pb_log(" args '%s'\n", cod->bd->args);
389 /* Re-attach the items array. */
390 result = set_menu_items(cui->main->ncm, cui->main->items);
393 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
396 pb_log("%s\n", __func__);
397 pmenu_dump_items(cui->main->items,
398 item_count(cui->main->ncm) + 1);
401 /* FIXME: need to make item visible somehow */
402 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
403 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
404 set_current_item(cui->main->ncm, selected);
406 if (cui->current == &cui->main->scr)
407 cui->current->post(cui->current);
413 * cui_device_remove - Client device remove callback.
415 * Removes all the menu_items for the device from the main menu and redraws the
416 * main menu if it is active.
419 static void cui_device_remove(struct device *dev, void *arg)
421 struct cui *cui = cui_from_arg(arg);
423 struct boot_option *opt;
425 pb_log("%s: %p %s\n", __func__, dev, dev->id);
427 if (cui->current == &cui->main->scr)
428 cui->current->unpost(cui->current);
430 /* This disconnects items array from menu. */
432 result = set_menu_items(cui->main->ncm, NULL);
435 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
437 list_for_each_entry(&dev->boot_options, opt, list) {
438 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
439 struct cui_opt_data *cod = cod_from_item(i);
441 assert(pb_protocol_device_cmp(dev, cod->dev));
442 pmenu_remove(cui->main, i);
445 /* Re-attach the items array. */
447 result = set_menu_items(cui->main->ncm, cui->main->items);
450 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
453 pb_log("%s\n", __func__);
454 pmenu_dump_items(cui->main->items,
455 item_count(cui->main->ncm) + 1);
458 if (cui->current == &cui->main->scr)
459 cui->current->post(cui->current);
462 static void cui_update_status(struct boot_status *status, void *arg)
464 struct cui *cui = cui_from_arg(arg);
466 nc_scr_status_printf(cui->current,
468 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
473 static struct discover_client_ops cui_client_ops = {
475 .boot_option_add = cui_boot_option_add,
476 .device_remove = cui_device_remove,
477 .update_status = cui_update_status,
481 * cui_init - Setup the cui instance.
482 * @platform_info: A value for the struct cui platform_info member.
484 * Returns a pointer to a struct cui on success, or NULL on error.
486 * Allocates the cui instance, sets up the client and stdin waiters, and
487 * sets up the ncurses menu screen.
490 struct cui *cui_init(void* platform_info,
491 int (*js_map)(const struct js_event *e), int start_deamon)
496 cui = talloc_zero(NULL, struct cui);
499 pb_log("%s: alloc cui failed.\n", __func__);
500 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
504 cui->c_sig = pb_cui_sig;
505 cui->platform_info = platform_info;
506 cui->waitset = waitset_create(cui);
508 /* Loop here for scripts that just started the server. */
511 for (i = start_deamon ? 2 : 10; i; i--) {
512 cui->client = discover_client_init(cui->waitset,
513 &cui_client_ops, cui);
514 if (cui->client || !i)
516 pb_log("%s: waiting for server %d\n", __func__, i);
520 if (!cui->client && start_deamon) {
525 result = pb_start_daemon();
530 pb_log("%s: discover_client_init failed.\n", __func__);
531 fprintf(stderr, "%s: error: discover_client_init failed.\n",
533 fprintf(stderr, "could not start pb-discover, the petitboot "
535 goto fail_client_init;
539 pb_log("%s: discover_client_init failed.\n", __func__);
540 fprintf(stderr, "%s: error: discover_client_init failed.\n",
542 fprintf(stderr, "check that pb-discover, "
543 "the petitboot daemon is running.\n");
544 goto fail_client_init;
550 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
551 cui_process_key, cui);
555 cui->pjs = pjs_init(cui, js_map);
558 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
559 WAIT_IN, cui_process_js, cui);
571 * cui_run - The main cui program loop.
572 * @cui: The cui instance.
573 * @main: The menu to use as the main menu.
575 * Runs the cui engine. Does not return until indicated to do so by some
576 * user action, or an error occurs. Frees the cui object on return.
577 * Returns 0 on success (return to shell), -1 on error (should restart).
580 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
585 cui->current = &cui->main->scr;
586 cui->default_item = default_item;
588 cui->current->post(cui->current);
591 int result = waiter_poll(cui->waitset);
593 if (result < 0 && errno != EINTR) {
594 pb_log("%s: poll: %s\n", __func__, strerror(errno));
601 while (cui->resize) {
603 cui_handle_resize(cui);
609 return cui->abort ? 0 : -1;