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 extern const struct help_text main_menu_help_text;
45 static void cui_start(void)
47 initscr(); /* Initialize ncurses. */
48 cbreak(); /* Disable line buffering. */
49 noecho(); /* Disable getch() echo. */
50 keypad(stdscr, TRUE); /* Enable num keypad keys. */
51 nonl(); /* Disable new-line translation. */
52 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
53 curs_set(0); /* Make cursor invisible */
54 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
56 /* We may be operating with an incorrect $TERM type; in this case
57 * the keymappings will be slightly broken. We want at least
58 * backspace to work though, so we'll define both DEL and ^H to
60 define_key("\x7f", KEY_BACKSPACE);
61 define_key("\x08", KEY_BACKSPACE);
63 /* we need backtab too, for form navigation. vt220 doesn't include
64 * this (kcbt), but we don't want to require a full linux/xterm termcap
66 define_key("\x1b[Z", KEY_BTAB);
68 while (getch() != ERR) /* flush stdin */
72 static void cui_atexit(void)
80 * cui_abort - Signal the main cui program loop to exit.
82 * Sets cui.abort, which causes the cui_run() routine to return.
85 void cui_abort(struct cui *cui)
87 pb_log("%s: exiting\n", __func__);
92 * cui_resize - Signal the main cui program loop to resize
97 void cui_resize(struct cui *cui)
99 pb_debug("%s: resizing\n", __func__);
104 * cui_on_exit - A generic main menu exit callback.
107 void cui_on_exit(struct pmenu *menu)
109 cui_abort(cui_from_pmenu(menu));
113 * cui_run_cmd - A generic cb to run the supplied command.
116 int cui_run_cmd(struct pmenu_item *item)
119 struct cui *cui = cui_from_item(item);
120 const char **cmd_argv = item->data;
122 nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
126 result = process_run_simple_argv(item, cmd_argv);
129 redrawwin(cui->current->main_ncw);
132 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
133 nc_scr_status_printf(cui->current, _("Failed: %s"),
141 * cui_boot - A generic cb to run kexec.
144 static int cui_boot(struct pmenu_item *item)
147 struct cui *cui = cui_from_item(item);
148 struct cui_opt_data *cod = cod_from_item(item);
150 assert(cui->current == &cui->main->scr);
152 pb_debug("%s: %s\n", __func__, cod->name);
154 nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
156 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
159 nc_scr_status_printf(cui->current,
160 _("Failed: boot %s"), cod->bd->image);
166 static void cui_boot_editor_on_exit(struct cui *cui,
167 struct pmenu_item *item,
168 struct pb_boot_data *bd)
170 struct pmenu *menu = cui->main;
171 struct cui_opt_data *cod;
172 static int user_idx = 0;
174 /* Was the edit cancelled? */
176 cui_set_current(cui, &cui->main->scr);
177 talloc_free(cui->boot_editor);
178 cui->boot_editor = NULL;
182 /* Is this was a new item, we'll need to update the menu */
186 cod = talloc_zero(NULL, struct cui_opt_data);
187 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
189 item = pmenu_item_create(menu, cod->name);
195 item->on_edit = cui_item_edit;
196 item->on_execute = cui_boot;
199 talloc_steal(item, cod);
201 /* Detach the items array. */
202 set_menu_items(menu->ncm, NULL);
204 /* Insert new item at insert_pt. */
205 insert_pt = pmenu_grow(menu, 1);
206 pmenu_item_insert(menu, item, insert_pt);
208 /* Re-attach the items array. */
209 set_menu_items(menu->ncm, menu->items);
210 nc_scr_post(&menu->scr);
215 cod->bd = talloc_steal(cod, bd);
217 set_current_item(item->pmenu->ncm, item->nci);
219 cui_set_current(cui, &cui->main->scr);
220 talloc_free(cui->boot_editor);
221 cui->boot_editor = NULL;
224 void cui_item_edit(struct pmenu_item *item)
226 struct cui *cui = cui_from_item(item);
227 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
228 cui_boot_editor_on_exit);
229 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
232 void cui_item_new(struct pmenu *menu)
234 struct cui *cui = cui_from_pmenu(menu);
235 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
236 cui_boot_editor_on_exit);
237 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
240 static void cui_sysinfo_exit(struct cui *cui)
242 cui_set_current(cui, &cui->main->scr);
243 talloc_free(cui->sysinfo_screen);
244 cui->sysinfo_screen = NULL;
247 void cui_show_sysinfo(struct cui *cui)
249 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
251 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
254 static void cui_config_exit(struct cui *cui)
256 cui_set_current(cui, &cui->main->scr);
257 talloc_free(cui->config_screen);
258 cui->config_screen = NULL;
261 void cui_show_config(struct cui *cui)
263 cui->config_screen = config_screen_init(cui, cui->config,
264 cui->sysinfo, cui_config_exit);
265 cui_set_current(cui, config_screen_scr(cui->config_screen));
268 static void cui_help_exit(struct cui *cui)
270 cui_set_current(cui, help_screen_return_scr(cui->help_screen));
271 talloc_free(cui->help_screen);
272 cui->help_screen = NULL;
275 void cui_show_help(struct cui *cui, const char *title,
276 const struct help_text *text)
281 if (cui->help_screen)
284 cui->help_screen = help_screen_init(cui, cui->current,
285 title, text, cui_help_exit);
287 if (cui->help_screen)
288 cui_set_current(cui, help_screen_scr(cui->help_screen));
292 * cui_set_current - Set the currently active screen and redraw it.
295 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
299 DBGS("%p -> %p\n", cui->current, scr);
301 assert(cui->current != scr);
308 nc_scr_post(cui->current);
313 static bool process_global_keys(struct cui *cui, int key)
317 if (cui->current && cui->current->main_ncw)
325 * cui_process_key - Process input on stdin.
328 static int cui_process_key(void *arg)
330 struct cui *cui = cui_from_arg(arg);
332 assert(cui->current);
337 pb_debug("%s: got key %d\n", __func__, c);
342 if (!cui->has_input) {
343 pb_log("UI input received (key = %d), aborting "
344 "default boot\n", c);
345 discover_client_cancel_default(cui->client);
346 cui->has_input = true;
349 if (process_global_keys(cui, c))
352 cui->current->process_key(cui->current, c);
359 * cui_process_js - Process joystick events.
362 static int cui_process_js(void *arg)
364 struct cui *cui = cui_from_arg(arg);
367 c = pjs_process_event(cui->pjs);
371 cui_process_key(arg);
378 * cui_handle_resize - Handle the term resize.
381 static void cui_handle_resize(struct cui *cui)
385 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
386 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
390 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
392 wclear(cui->current->main_ncw);
393 resize_term(ws.ws_row, ws.ws_col);
394 cui->current->resize(cui->current);
396 /* For some reason this makes ncurses redraw the screen */
398 redrawwin(cui->current->main_ncw);
399 wrefresh(cui->current->main_ncw);
403 * cui_device_add - Client device_add callback.
405 * Creates menu_items for all the device boot_options and inserts those
406 * menu_items into the main menu. Redraws the main menu if it is active.
409 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
412 struct pmenu_item *i, *dev_hdr = NULL;
413 struct cui *cui = cui_from_arg(arg);
414 struct cui_opt_data *cod;
415 const char *tab = " ";
416 unsigned int insert_pt;
417 int result, rows, cols;
421 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
423 selected = current_item(cui->main->ncm);
424 menu_format(cui->main->ncm, &rows, &cols);
426 if (cui->current == &cui->main->scr)
427 nc_scr_unpost(cui->current);
429 /* Check if the boot device is new */
430 dev_hdr = pmenu_find_device(cui->main, dev, opt);
432 /* All actual boot entries are 'tabbed' across */
433 name = talloc_asprintf(cui->main, "%s%s",
434 tab, opt->name ? : "Unknown Name");
436 /* Save the item in opt->ui_info for cui_device_remove() */
437 opt->ui_info = i = pmenu_item_create(cui->main, name);
442 i->on_edit = cui_item_edit;
443 i->on_execute = cui_boot;
444 i->data = cod = talloc(i, struct cui_opt_data);
448 cod->opt_hash = pb_opt_hash(dev, opt);
449 cod->name = opt->name;
450 cod->bd = talloc(i, struct pb_boot_data);
452 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
453 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
454 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
455 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
457 /* This disconnects items array from menu. */
458 result = set_menu_items(cui->main->ncm, NULL);
461 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
463 /* Insert new items at insert_pt. */
465 insert_pt = pmenu_grow(cui->main, 2);
466 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
467 pb_log("%s: adding new device hierarchy %s\n",
468 __func__,opt->device_id);
469 pmenu_item_insert(cui->main, i, insert_pt+1);
471 insert_pt = pmenu_grow(cui->main, 1);
472 pmenu_item_add(cui->main, i, insert_pt);
475 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
476 pb_log(" image '%s'\n", cod->bd->image);
477 pb_log(" initrd '%s'\n", cod->bd->initrd);
478 pb_log(" args '%s'\n", cod->bd->args);
480 /* Re-attach the items array. */
481 result = set_menu_items(cui->main->ncm, cui->main->items);
484 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
487 pb_log("%s\n", __func__);
488 pmenu_dump_items(cui->main->items,
489 item_count(cui->main->ncm) + 1);
492 if (!item_visible(selected)) {
495 top = top_row(cui->main->ncm);
496 idx = item_index(selected);
498 /* If our index is above the current top row, align
499 * us to the new top. Otherwise, align us to the new
501 top = top < idx ? idx - rows : idx;
503 set_top_row(cui->main->ncm, top);
504 set_current_item(cui->main->ncm, selected);
507 if (cui->current == &cui->main->scr)
508 nc_scr_post(cui->current);
514 * cui_device_remove - Client device remove callback.
516 * Removes all the menu_items for the device from the main menu and redraws the
517 * main menu if it is active.
520 static void cui_device_remove(struct device *dev, void *arg)
522 struct cui *cui = cui_from_arg(arg);
523 struct boot_option *opt;
527 pb_log("%s: %p %s\n", __func__, dev, dev->id);
529 if (cui->current == &cui->main->scr)
530 nc_scr_unpost(cui->current);
532 /* This disconnects items array from menu. */
534 result = set_menu_items(cui->main->ncm, NULL);
537 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
539 list_for_each_entry(&dev->boot_options, opt, list) {
540 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
542 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
543 pmenu_remove(cui->main, item);
546 /* Manually remove remaining device hierarchy item */
547 for (i=0; i < cui->main->item_count; i++) {
548 struct pmenu_item *item = item_userptr(cui->main->items[i]);
549 if (!item || !item->data )
552 struct cui_opt_data *data = item->data;
553 if (data && data->dev && data->dev == dev)
554 pmenu_remove(cui->main,item);
557 /* Re-attach the items array. */
559 result = set_menu_items(cui->main->ncm, cui->main->items);
562 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
565 pb_log("%s\n", __func__);
566 pmenu_dump_items(cui->main->items,
567 item_count(cui->main->ncm) + 1);
570 if (cui->current == &cui->main->scr)
571 nc_scr_post(cui->current);
574 static void cui_update_status(struct boot_status *status, void *arg)
576 struct cui *cui = cui_from_arg(arg);
578 nc_scr_status_printf(cui->current,
580 status->type == BOOT_STATUS_ERROR ?
581 _("Error") : _("Info"),
586 static void cui_update_mm_title(struct cui *cui)
588 struct nc_frame *frame = &cui->main->scr.frame;
590 talloc_free(frame->rtitle);
592 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
593 if (cui->sysinfo->identifier)
594 frame->rtitle = talloc_asprintf_append(frame->rtitle,
595 " %s", cui->sysinfo->identifier);
597 if (cui->current == &cui->main->scr)
598 nc_scr_post(cui->current);
601 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
603 struct cui *cui = cui_from_arg(arg);
604 cui->sysinfo = talloc_steal(cui, sysinfo);
606 /* if we're currently displaying the system info screen, inform it
607 * of the updated information. */
608 if (cui->sysinfo_screen)
609 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
611 /* ... and do the same with the config screen... */
612 if (cui->config_screen)
613 config_screen_update(cui->config_screen, cui->config, sysinfo);
615 /* ... and the boot editor. */
616 if (cui->boot_editor)
617 boot_editor_update(cui->boot_editor, sysinfo);
619 cui_update_mm_title(cui);
622 static void cui_update_config(struct config *config, void *arg)
624 struct cui *cui = cui_from_arg(arg);
625 cui->config = talloc_steal(cui, config);
627 if (cui->config_screen)
628 config_screen_update(cui->config_screen, config, cui->sysinfo);
630 if (config->safe_mode)
631 nc_scr_status_printf(cui->current,
632 _("SAFE MODE: select '%s' to continue"),
633 _("Rescan devices"));
636 int cui_send_config(struct cui *cui, struct config *config)
638 return discover_client_send_config(cui->client, config);
641 void cui_send_reinit(struct cui *cui)
643 discover_client_send_reinit(cui->client);
646 static int menu_sysinfo_execute(struct pmenu_item *item)
648 cui_show_sysinfo(cui_from_item(item));
652 static int menu_config_execute(struct pmenu_item *item)
654 cui_show_config(cui_from_item(item));
658 static int menu_reinit_execute(struct pmenu_item *item)
660 cui_send_reinit(cui_from_item(item));
665 * pb_mm_init - Setup the main menu instance.
667 static struct pmenu *main_menu_init(struct cui *cui)
669 struct pmenu_item *i;
673 m = pmenu_init(cui, 5, cui_on_exit);
675 pb_log("%s: failed\n", __func__);
679 m->on_new = cui_item_new;
681 m->scr.frame.ltitle = talloc_asprintf(m,
682 "Petitboot (" PACKAGE_VERSION ")");
683 m->scr.frame.rtitle = NULL;
684 m->scr.frame.help = talloc_strdup(m,
685 _("Enter=accept, e=edit, n=new, x=exit, h=help"));
686 m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
688 /* add a separator */
689 i = pmenu_item_create(m, " ");
690 item_opts_off(i->nci, O_SELECTABLE);
691 pmenu_item_insert(m, i, 0);
693 /* add system items */
694 i = pmenu_item_create(m, _("System information"));
695 i->on_execute = menu_sysinfo_execute;
696 pmenu_item_insert(m, i, 1);
698 i = pmenu_item_create(m, _("System configuration"));
699 i->on_execute = menu_config_execute;
700 pmenu_item_insert(m, i, 2);
702 i = pmenu_item_create(m, _("Rescan devices"));
703 i->on_execute = menu_reinit_execute;
704 pmenu_item_insert(m, i, 3);
706 i = pmenu_item_create(m, _("Exit to shell"));
707 i->on_execute = pmenu_exit_cb;
708 pmenu_item_insert(m, i, 4);
710 result = pmenu_setup(m);
713 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
718 m->help_title = _("main menu");
719 m->help_text = &main_menu_help_text;
721 menu_opts_off(m->ncm, O_SHOWDESC);
722 set_menu_mark(m->ncm, " *");
723 set_current_item(m->ncm, i->nci);
732 static struct discover_client_ops cui_client_ops = {
734 .boot_option_add = cui_boot_option_add,
735 .device_remove = cui_device_remove,
736 .update_status = cui_update_status,
737 .update_sysinfo = cui_update_sysinfo,
738 .update_config = cui_update_config,
742 * cui_init - Setup the cui instance.
743 * @platform_info: A value for the struct cui platform_info member.
745 * Returns a pointer to a struct cui on success, or NULL on error.
747 * Allocates the cui instance, sets up the client and stdin waiters, and
748 * sets up the ncurses menu screen.
751 struct cui *cui_init(void* platform_info,
752 int (*js_map)(const struct js_event *e), int start_deamon)
757 cui = talloc_zero(NULL, struct cui);
759 pb_log("%s: alloc cui failed.\n", __func__);
760 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
764 cui->c_sig = pb_cui_sig;
765 cui->platform_info = platform_info;
766 cui->waitset = waitset_create(cui);
768 process_init(cui, cui->waitset, false);
770 /* Loop here for scripts that just started the server. */
773 for (i = start_deamon ? 2 : 10; i; i--) {
774 cui->client = discover_client_init(cui->waitset,
775 &cui_client_ops, cui);
776 if (cui->client || !i)
778 pb_log("%s: waiting for server %d\n", __func__, i);
782 if (!cui->client && start_deamon) {
787 result = pb_start_daemon(cui);
792 pb_log("%s: discover_client_init failed.\n", __func__);
793 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
795 fprintf(stderr, _("could not start pb-discover, the petitboot "
797 goto fail_client_init;
801 pb_log("%s: discover_client_init failed.\n", __func__);
802 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
804 fprintf(stderr, _("check that pb-discover, "
805 "the petitboot daemon is running.\n"));
806 goto fail_client_init;
810 talloc_steal(cui, cui->client);
813 cui->main = main_menu_init(cui);
815 goto fail_client_init;
817 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
818 cui_process_key, cui);
822 cui->pjs = pjs_init(cui, js_map);
825 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
826 WAIT_IN, cui_process_js, cui);
838 * cui_run - The main cui program loop.
839 * @cui: The cui instance.
840 * @main: The menu to use as the main menu.
842 * Runs the cui engine. Does not return until indicated to do so by some
843 * user action, or an error occurs. Frees the cui object on return.
844 * Returns 0 on success (return to shell), -1 on error (should restart).
847 int cui_run(struct cui *cui)
851 cui->current = &cui->main->scr;
852 cui->default_item = 0;
854 nc_scr_post(cui->current);
857 int result = waiter_poll(cui->waitset);
860 pb_log("%s: poll: %s\n", __func__, strerror(errno));
867 while (cui->resize) {
869 cui_handle_resize(cui);
875 return cui->abort ? 0 : -1;