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
19 #if defined(HAVE_CONFIG_H)
27 #include <sys/ioctl.h>
30 #include "pb-protocol/pb-protocol.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "process/process.h"
34 #include "i18n/i18n.h"
35 #include "ui/common/discover-client.h"
36 #include "ui/common/ui-system.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-sysinfo.h"
41 #include "nc-helpscreen.h"
43 static void cui_start(void)
45 initscr(); /* Initialize ncurses. */
46 cbreak(); /* Disable line buffering. */
47 noecho(); /* Disable getch() echo. */
48 keypad(stdscr, TRUE); /* Enable num keypad keys. */
49 nonl(); /* Disable new-line translation. */
50 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
51 curs_set(0); /* Make cursor invisible */
52 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
54 /* We may be operating with an incorrect $TERM type; in this case
55 * the keymappings will be slightly broken. We want at least
56 * backspace to work though, so we'll define both DEL and ^H to
58 define_key("\x7f", KEY_BACKSPACE);
59 define_key("\x08", KEY_BACKSPACE);
61 /* we need backtab too, for form navigation. vt220 doesn't include
62 * this (kcbt), but we don't want to require a full linux/xterm termcap
64 define_key("\x1b[Z", KEY_BTAB);
66 while (getch() != ERR) /* flush stdin */
70 static void cui_atexit(void)
78 * cui_abort - Signal the main cui program loop to exit.
80 * Sets cui.abort, which causes the cui_run() routine to return.
83 void cui_abort(struct cui *cui)
85 pb_log("%s: exiting\n", __func__);
90 * cui_resize - Signal the main cui program loop to resize
95 void cui_resize(struct cui *cui)
97 pb_debug("%s: resizing\n", __func__);
102 * cui_on_exit - A generic main menu exit callback.
105 void cui_on_exit(struct pmenu *menu)
107 cui_abort(cui_from_pmenu(menu));
111 * cui_run_cmd - A generic cb to run the supplied command.
114 int cui_run_cmd(struct pmenu_item *item)
117 struct cui *cui = cui_from_item(item);
118 const char **cmd_argv = item->data;
120 nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
124 result = process_run_simple_argv(item, cmd_argv);
127 redrawwin(cui->current->main_ncw);
130 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
131 nc_scr_status_printf(cui->current, _("Failed: %s"),
139 * cui_boot - A generic cb to run kexec.
142 static int cui_boot(struct pmenu_item *item)
145 struct cui *cui = cui_from_item(item);
146 struct cui_opt_data *cod = cod_from_item(item);
148 assert(cui->current == &cui->main->scr);
150 pb_debug("%s: %s\n", __func__, cod->name);
152 nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
154 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
157 nc_scr_status_printf(cui->current,
158 _("Failed: boot %s"), cod->bd->image);
164 static void cui_boot_editor_on_exit(struct cui *cui,
165 struct pmenu_item *item,
166 struct pb_boot_data *bd)
168 struct pmenu *menu = cui->main;
169 struct cui_opt_data *cod;
170 static int user_idx = 0;
172 /* Was the edit cancelled? */
174 cui_set_current(cui, &cui->main->scr);
175 talloc_free(cui->boot_editor);
176 cui->boot_editor = NULL;
180 /* Is this was a new item, we'll need to update the menu */
184 cod = talloc_zero(NULL, struct cui_opt_data);
185 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
187 item = pmenu_item_create(menu, cod->name);
193 item->on_edit = cui_item_edit;
194 item->on_execute = cui_boot;
197 talloc_steal(item, cod);
199 /* Detach the items array. */
200 set_menu_items(menu->ncm, NULL);
202 /* Insert new item at insert_pt. */
203 insert_pt = pmenu_grow(menu, 1);
204 pmenu_item_insert(menu, item, insert_pt);
206 /* Re-attach the items array. */
207 set_menu_items(menu->ncm, menu->items);
208 nc_scr_post(&menu->scr);
213 cod->bd = talloc_steal(cod, bd);
215 set_current_item(item->pmenu->ncm, item->nci);
217 cui_set_current(cui, &cui->main->scr);
218 talloc_free(cui->boot_editor);
219 cui->boot_editor = NULL;
222 void cui_item_edit(struct pmenu_item *item)
224 struct cui *cui = cui_from_item(item);
225 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
226 cui_boot_editor_on_exit);
227 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
230 void cui_item_new(struct pmenu *menu)
232 struct cui *cui = cui_from_pmenu(menu);
233 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
234 cui_boot_editor_on_exit);
235 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
238 static void cui_sysinfo_exit(struct cui *cui)
240 cui_set_current(cui, &cui->main->scr);
241 talloc_free(cui->sysinfo_screen);
242 cui->sysinfo_screen = NULL;
245 void cui_show_sysinfo(struct cui *cui)
247 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
249 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
252 static void cui_config_exit(struct cui *cui)
254 cui_set_current(cui, &cui->main->scr);
255 talloc_free(cui->config_screen);
256 cui->config_screen = NULL;
259 void cui_show_config(struct cui *cui)
261 cui->config_screen = config_screen_init(cui, cui->config,
262 cui->sysinfo, cui_config_exit);
263 cui_set_current(cui, config_screen_scr(cui->config_screen));
266 static void cui_help_exit(struct cui *cui)
268 cui_set_current(cui, help_screen_return_scr(cui->help_screen));
269 talloc_free(cui->help_screen);
270 cui->help_screen = NULL;
273 void cui_show_help(struct cui *cui, const char *title, const char *text)
278 if (cui->help_screen)
281 cui->help_screen = help_screen_init(cui, cui->current,
282 title, text, cui_help_exit);
284 if (cui->help_screen)
285 cui_set_current(cui, help_screen_scr(cui->help_screen));
289 * cui_set_current - Set the currently active screen and redraw it.
292 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
296 DBGS("%p -> %p\n", cui->current, scr);
298 assert(cui->current != scr);
305 nc_scr_post(cui->current);
310 static bool process_global_keys(struct cui *cui, int key)
314 if (cui->current && cui->current->main_ncw)
322 * cui_process_key - Process input on stdin.
325 static int cui_process_key(void *arg)
327 struct cui *cui = cui_from_arg(arg);
329 assert(cui->current);
334 pb_debug("%s: got key %d\n", __func__, c);
339 if (!cui->has_input) {
340 pb_log("UI input received (key = %d), aborting "
341 "default boot\n", c);
342 discover_client_cancel_default(cui->client);
343 cui->has_input = true;
346 if (process_global_keys(cui, c))
349 cui->current->process_key(cui->current, c);
356 * cui_process_js - Process joystick events.
359 static int cui_process_js(void *arg)
361 struct cui *cui = cui_from_arg(arg);
364 c = pjs_process_event(cui->pjs);
368 cui_process_key(arg);
375 * cui_handle_resize - Handle the term resize.
378 static void cui_handle_resize(struct cui *cui)
382 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
383 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
387 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
389 wclear(cui->current->main_ncw);
390 resize_term(ws.ws_row, ws.ws_col);
391 cui->current->resize(cui->current);
393 /* For some reason this makes ncurses redraw the screen */
395 redrawwin(cui->current->main_ncw);
396 wrefresh(cui->current->main_ncw);
400 * cui_device_add - Client device_add callback.
402 * Creates menu_items for all the device boot_options and inserts those
403 * menu_items into the main menu. Redraws the main menu if it is active.
406 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
409 struct pmenu_item *i, *dev_hdr = NULL;
410 struct cui *cui = cui_from_arg(arg);
411 struct cui_opt_data *cod;
412 const char *tab = " ";
413 unsigned int insert_pt;
414 int result, rows, cols;
418 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
420 selected = current_item(cui->main->ncm);
421 menu_format(cui->main->ncm, &rows, &cols);
423 if (cui->current == &cui->main->scr)
424 nc_scr_unpost(cui->current);
426 /* Check if the boot device is new */
427 dev_hdr = pmenu_find_device(cui->main, dev, opt);
429 /* All actual boot entries are 'tabbed' across */
430 name = talloc_asprintf(cui->main, "%s%s",
431 tab, opt->name ? : "Unknown Name");
433 /* Save the item in opt->ui_info for cui_device_remove() */
434 opt->ui_info = i = pmenu_item_create(cui->main, name);
439 i->on_edit = cui_item_edit;
440 i->on_execute = cui_boot;
441 i->data = cod = talloc(i, struct cui_opt_data);
445 cod->opt_hash = pb_opt_hash(dev, opt);
446 cod->name = opt->name;
447 cod->bd = talloc(i, struct pb_boot_data);
449 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
450 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
451 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
452 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
454 /* This disconnects items array from menu. */
455 result = set_menu_items(cui->main->ncm, NULL);
458 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
460 /* Insert new items at insert_pt. */
462 insert_pt = pmenu_grow(cui->main, 2);
463 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
464 pb_log("%s: adding new device hierarchy %s\n",
465 __func__,opt->device_id);
466 pmenu_item_insert(cui->main, i, insert_pt+1);
468 insert_pt = pmenu_grow(cui->main, 1);
469 pmenu_item_add(cui->main, i, insert_pt);
472 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
473 pb_log(" image '%s'\n", cod->bd->image);
474 pb_log(" initrd '%s'\n", cod->bd->initrd);
475 pb_log(" args '%s'\n", cod->bd->args);
477 /* Re-attach the items array. */
478 result = set_menu_items(cui->main->ncm, cui->main->items);
481 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
484 pb_log("%s\n", __func__);
485 pmenu_dump_items(cui->main->items,
486 item_count(cui->main->ncm) + 1);
489 if (!item_visible(selected)) {
492 top = top_row(cui->main->ncm);
493 idx = item_index(selected);
495 /* If our index is above the current top row, align
496 * us to the new top. Otherwise, align us to the new
498 top = top < idx ? idx - rows : idx;
500 set_top_row(cui->main->ncm, top);
501 set_current_item(cui->main->ncm, selected);
504 if (cui->current == &cui->main->scr)
505 nc_scr_post(cui->current);
511 * cui_device_remove - Client device remove callback.
513 * Removes all the menu_items for the device from the main menu and redraws the
514 * main menu if it is active.
517 static void cui_device_remove(struct device *dev, void *arg)
519 struct cui *cui = cui_from_arg(arg);
520 struct boot_option *opt;
524 pb_log("%s: %p %s\n", __func__, dev, dev->id);
526 if (cui->current == &cui->main->scr)
527 nc_scr_unpost(cui->current);
529 /* This disconnects items array from menu. */
531 result = set_menu_items(cui->main->ncm, NULL);
534 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
536 list_for_each_entry(&dev->boot_options, opt, list) {
537 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
539 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
540 pmenu_remove(cui->main, item);
543 /* Manually remove remaining device hierarchy item */
544 for (i=0; i < cui->main->item_count; i++) {
545 struct pmenu_item *item = item_userptr(cui->main->items[i]);
546 if (!item || !item->data )
549 struct cui_opt_data *data = item->data;
550 if (data && data->dev && data->dev == dev)
551 pmenu_remove(cui->main,item);
554 /* Re-attach the items array. */
556 result = set_menu_items(cui->main->ncm, cui->main->items);
559 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
562 pb_log("%s\n", __func__);
563 pmenu_dump_items(cui->main->items,
564 item_count(cui->main->ncm) + 1);
567 if (cui->current == &cui->main->scr)
568 nc_scr_post(cui->current);
571 static void cui_update_status(struct boot_status *status, void *arg)
573 struct cui *cui = cui_from_arg(arg);
575 nc_scr_status_printf(cui->current,
577 status->type == BOOT_STATUS_ERROR ?
578 _("Error") : _("Info"),
583 static void cui_update_mm_title(struct cui *cui)
585 struct nc_frame *frame = &cui->main->scr.frame;
587 talloc_free(frame->rtitle);
589 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
590 if (cui->sysinfo->identifier)
591 frame->rtitle = talloc_asprintf_append(frame->rtitle,
592 " %s", cui->sysinfo->identifier);
594 if (cui->current == &cui->main->scr)
595 nc_scr_post(cui->current);
598 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
600 struct cui *cui = cui_from_arg(arg);
601 cui->sysinfo = talloc_steal(cui, sysinfo);
603 /* if we're currently displaying the system info screen, inform it
604 * of the updated information. */
605 if (cui->sysinfo_screen)
606 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
608 /* ... and do the same with the config screen... */
609 if (cui->config_screen)
610 config_screen_update(cui->config_screen, cui->config, sysinfo);
612 /* ... and the boot editor. */
613 if (cui->boot_editor)
614 boot_editor_update(cui->boot_editor, sysinfo);
616 cui_update_mm_title(cui);
619 static void cui_update_config(struct config *config, void *arg)
621 struct cui *cui = cui_from_arg(arg);
622 cui->config = talloc_steal(cui, config);
624 if (cui->config_screen)
625 config_screen_update(cui->config_screen, config, cui->sysinfo);
627 if (config->safe_mode)
628 nc_scr_status_printf(cui->current,
629 "SAFE MODE: select '%s' to continue",
633 int cui_send_config(struct cui *cui, struct config *config)
635 return discover_client_send_config(cui->client, config);
638 void cui_send_reinit(struct cui *cui)
640 discover_client_send_reinit(cui->client);
643 static struct discover_client_ops cui_client_ops = {
645 .boot_option_add = cui_boot_option_add,
646 .device_remove = cui_device_remove,
647 .update_status = cui_update_status,
648 .update_sysinfo = cui_update_sysinfo,
649 .update_config = cui_update_config,
653 * cui_init - Setup the cui instance.
654 * @platform_info: A value for the struct cui platform_info member.
656 * Returns a pointer to a struct cui on success, or NULL on error.
658 * Allocates the cui instance, sets up the client and stdin waiters, and
659 * sets up the ncurses menu screen.
662 struct cui *cui_init(void* platform_info,
663 int (*js_map)(const struct js_event *e), int start_deamon)
668 cui = talloc_zero(NULL, struct cui);
671 pb_log("%s: alloc cui failed.\n", __func__);
672 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
676 cui->c_sig = pb_cui_sig;
677 cui->platform_info = platform_info;
678 cui->waitset = waitset_create(cui);
680 process_init(cui, cui->waitset, false);
682 /* Loop here for scripts that just started the server. */
685 for (i = start_deamon ? 2 : 10; i; i--) {
686 cui->client = discover_client_init(cui->waitset,
687 &cui_client_ops, cui);
688 if (cui->client || !i)
690 pb_log("%s: waiting for server %d\n", __func__, i);
694 if (!cui->client && start_deamon) {
699 result = pb_start_daemon(cui);
704 pb_log("%s: discover_client_init failed.\n", __func__);
705 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
707 fprintf(stderr, _("could not start pb-discover, the petitboot "
709 goto fail_client_init;
713 pb_log("%s: discover_client_init failed.\n", __func__);
714 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
716 fprintf(stderr, _("check that pb-discover, "
717 "the petitboot daemon is running.\n"));
718 goto fail_client_init;
722 talloc_steal(cui, cui->client);
725 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
726 cui_process_key, cui);
730 cui->pjs = pjs_init(cui, js_map);
733 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
734 WAIT_IN, cui_process_js, cui);
746 * cui_run - The main cui program loop.
747 * @cui: The cui instance.
748 * @main: The menu to use as the main menu.
750 * Runs the cui engine. Does not return until indicated to do so by some
751 * user action, or an error occurs. Frees the cui object on return.
752 * Returns 0 on success (return to shell), -1 on error (should restart).
755 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
760 cui->current = &cui->main->scr;
761 cui->default_item = default_item;
763 nc_scr_post(cui->current);
766 int result = waiter_poll(cui->waitset);
769 pb_log("%s: poll: %s\n", __func__, strerror(errno));
776 while (cui->resize) {
778 cui_handle_resize(cui);
784 return cui->abort ? 0 : -1;