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
28 #include <sys/ioctl.h>
31 #include "pb-protocol/pb-protocol.h"
32 #include "talloc/talloc.h"
33 #include "waiter/waiter.h"
34 #include "process/process.h"
35 #include "ui/common/discover-client.h"
37 #include "nc-config.h"
38 #include "nc-sysinfo.h"
40 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
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 while (getch() != ERR) /* flush stdin */
67 static void cui_atexit(void)
75 * cui_abort - Signal the main cui program loop to exit.
77 * Sets cui.abort, which causes the cui_run() routine to return.
80 void cui_abort(struct cui *cui)
82 pb_log("%s: exiting\n", __func__);
87 * cui_resize - Signal the main cui program loop to resize
92 void cui_resize(struct cui *cui)
94 pb_debug("%s: resizing\n", __func__);
99 * cui_on_exit - A generic main menu exit callback.
102 void cui_on_exit(struct pmenu *menu)
104 cui_abort(cui_from_pmenu(menu));
108 * cui_run_cmd - A generic cb to run the supplied command.
111 int cui_run_cmd(struct pmenu_item *item)
114 struct cui *cui = cui_from_item(item);
115 const char **cmd_argv = item->data;
117 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
121 result = process_run_simple_argv(item, cmd_argv);
124 redrawwin(cui->current->main_ncw);
127 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
128 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
135 * cui_boot - A generic cb to run kexec.
138 static int cui_boot(struct pmenu_item *item)
141 struct cui *cui = cui_from_item(item);
142 struct cui_opt_data *cod = cod_from_item(item);
144 assert(cui->current == &cui->main->scr);
146 pb_debug("%s: %s\n", __func__, cod->name);
148 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
152 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
155 redrawwin(cui->current->main_ncw);
158 nc_scr_status_printf(cui->current,
159 "Failed: boot %s", cod->bd->image);
166 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
169 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
170 enum boot_editor_result boot_editor_result,
171 struct pb_boot_data *bd)
173 struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
174 struct pmenu_item *item = boot_editor->data;
175 struct cui_opt_data *cod;
177 if (boot_editor_result != boot_editor_update) {
178 cui_set_current(cui, &cui->main->scr);
179 talloc_free(boot_editor);
185 /* Is this was a new item, we'll need to update the menu */
187 struct pmenu *menu = boot_editor->original_pmenu;
190 /* Detach the items array. */
191 set_menu_items(menu->ncm, NULL);
193 /* Insert new item at insert_pt. */
194 insert_pt = pmenu_grow(menu, 1);
195 item = pmenu_item_alloc(menu);
196 item->on_edit = cui_item_edit;
197 item->on_execute = cui_boot;
198 item->data = cod = talloc_zero(item, struct cui_opt_data);
200 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
201 pmenu_item_setup(menu, item, insert_pt,
202 talloc_strdup(item, cod->name));
204 /* Re-attach the items array. */
205 set_menu_items(menu->ncm, menu->items);
206 nc_scr_post(&menu->scr);
211 cod->bd = talloc_steal(cod, bd);
213 /* FIXME: need to make item visible somehow */
214 set_current_item(item->pmenu->ncm, item->nci);
215 cui_set_current(cui, &cui->main->scr);
216 talloc_free(boot_editor);
219 void cui_item_edit(struct pmenu_item *item)
221 struct cui *cui = cui_from_item(item);
222 struct cui_opt_data *cod = cod_from_item(item);
223 struct boot_editor *boot_editor;
225 boot_editor = boot_editor_init(item->pmenu, cod->bd,
226 cui_boot_editor_on_exit);
227 boot_editor->data = item;
228 cui_set_current(cui, &boot_editor->scr);
231 void cui_item_new(struct pmenu *menu)
233 struct cui *cui = cui_from_pmenu(menu);
234 struct boot_editor *boot_editor;
236 boot_editor = boot_editor_init(menu, NULL,
237 cui_boot_editor_on_exit);
238 cui_set_current(cui, &boot_editor->scr);
241 static void cui_sysinfo_exit(struct cui *cui)
243 cui_set_current(cui, &cui->main->scr);
244 talloc_free(cui->sysinfo_screen);
245 cui->sysinfo_screen = NULL;
248 void cui_show_sysinfo(struct cui *cui)
250 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
252 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
255 static void cui_config_exit(struct cui *cui)
257 cui_set_current(cui, &cui->main->scr);
258 talloc_free(cui->config_screen);
259 cui->config_screen = NULL;
262 void cui_show_config(struct cui *cui)
264 cui->config_screen = config_screen_init(cui, cui->config,
265 cui->sysinfo, cui_config_exit);
266 cui_set_current(cui, config_screen_scr(cui->config_screen));
270 * cui_set_current - Set the currently active screen and redraw it.
273 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
277 DBGS("%p -> %p\n", cui->current, scr);
279 assert(cui->current != scr);
286 nc_scr_post(cui->current);
291 static bool process_global_keys(struct cui *cui, int key)
295 if (cui->current && cui->current->main_ncw) {
296 redrawwin(cui->current->main_ncw);
297 wrefresh(cui->current->main_ncw);
305 * cui_process_key - Process input on stdin.
308 static int cui_process_key(void *arg)
310 struct cui *cui = cui_from_arg(arg);
312 assert(cui->current);
315 discover_client_cancel_default(cui->client);
316 cui->has_input = true;
324 if (process_global_keys(cui, c))
327 cui->current->process_key(cui->current, c);
334 * cui_process_js - Process joystick events.
337 static int cui_process_js(void *arg)
339 struct cui *cui = cui_from_arg(arg);
342 c = pjs_process_event(cui->pjs);
346 cui_process_key(arg);
353 * cui_handle_resize - Handle the term resize.
356 static void cui_handle_resize(struct cui *cui)
360 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
361 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
365 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
367 wclear(cui->current->main_ncw);
368 resize_term(ws.ws_row, ws.ws_col);
369 cui->current->resize(cui->current);
371 /* For some reason this makes ncurses redraw the screen */
373 redrawwin(cui->current->main_ncw);
374 wrefresh(cui->current->main_ncw);
378 * cui_device_add - Client device_add callback.
380 * Creates menu_items for all the device boot_options and inserts those
381 * menu_items into the main menu. Redraws the main menu if it is active.
384 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
387 struct cui *cui = cui_from_arg(arg);
388 struct cui_opt_data *cod;
389 unsigned int insert_pt;
390 struct pmenu_item *i;
394 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
396 selected = current_item(cui->main->ncm);
398 if (cui->current == &cui->main->scr)
399 nc_scr_unpost(cui->current);
401 /* This disconnects items array from menu. */
403 result = set_menu_items(cui->main->ncm, NULL);
406 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
408 /* Insert new items at insert_pt. */
409 insert_pt = pmenu_grow(cui->main, 1);
411 /* Save the item in opt->ui_info for cui_device_remove() */
413 opt->ui_info = i = pmenu_item_alloc(cui->main);
415 i->on_edit = cui_item_edit;
416 i->on_execute = cui_boot;
417 i->data = cod = talloc(i, struct cui_opt_data);
421 cod->opt_hash = pb_opt_hash(dev, opt);
422 cod->name = opt->name;
423 cod->bd = talloc(i, struct pb_boot_data);
425 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
426 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
427 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
428 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
430 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
432 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
433 pb_log(" image '%s'\n", cod->bd->image);
434 pb_log(" initrd '%s'\n", cod->bd->initrd);
435 pb_log(" args '%s'\n", cod->bd->args);
437 /* Re-attach the items array. */
438 result = set_menu_items(cui->main->ncm, cui->main->items);
441 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
444 pb_log("%s\n", __func__);
445 pmenu_dump_items(cui->main->items,
446 item_count(cui->main->ncm) + 1);
449 /* FIXME: need to make item visible somehow */
450 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
451 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
452 set_current_item(cui->main->ncm, selected);
454 if (cui->current == &cui->main->scr)
455 nc_scr_post(cui->current);
461 * cui_device_remove - Client device remove callback.
463 * Removes all the menu_items for the device from the main menu and redraws the
464 * main menu if it is active.
467 static void cui_device_remove(struct device *dev, void *arg)
469 struct cui *cui = cui_from_arg(arg);
471 struct boot_option *opt;
473 pb_log("%s: %p %s\n", __func__, dev, dev->id);
475 if (cui->current == &cui->main->scr)
476 nc_scr_unpost(cui->current);
478 /* This disconnects items array from menu. */
480 result = set_menu_items(cui->main->ncm, NULL);
483 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
485 list_for_each_entry(&dev->boot_options, opt, list) {
486 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
488 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
489 pmenu_remove(cui->main, i);
492 /* Re-attach the items array. */
494 result = set_menu_items(cui->main->ncm, cui->main->items);
497 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
500 pb_log("%s\n", __func__);
501 pmenu_dump_items(cui->main->items,
502 item_count(cui->main->ncm) + 1);
505 if (cui->current == &cui->main->scr)
506 nc_scr_post(cui->current);
509 static void cui_update_status(struct boot_status *status, void *arg)
511 struct cui *cui = cui_from_arg(arg);
513 nc_scr_status_printf(cui->current,
515 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
520 static void cui_update_mm_title(struct cui *cui)
522 struct nc_frame *frame = &cui->main->scr.frame;
524 talloc_free(frame->rtitle);
526 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
527 if (cui->sysinfo->identifier)
528 frame->rtitle = talloc_asprintf_append(frame->rtitle,
529 " %s", cui->sysinfo->identifier);
531 if (cui->current == &cui->main->scr)
532 nc_scr_post(cui->current);
535 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
537 struct cui *cui = cui_from_arg(arg);
538 cui->sysinfo = talloc_steal(cui, sysinfo);
540 /* if we're currently displaying the system info screen, inform it
541 * of the updated information. */
542 if (cui->sysinfo_screen)
543 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
545 /* ... and do the same with the config screen */
546 if (cui->config_screen)
547 config_screen_update(cui->config_screen, cui->config, sysinfo);
549 cui_update_mm_title(cui);
552 static void cui_update_config(struct config *config, void *arg)
554 struct cui *cui = cui_from_arg(arg);
555 cui->config = talloc_steal(cui, config);
557 if (cui->config_screen)
558 config_screen_update(cui->config_screen, config, cui->sysinfo);
561 int cui_send_config(struct cui *cui, struct config *config)
563 return discover_client_send_config(cui->client, config);
566 static struct discover_client_ops cui_client_ops = {
568 .boot_option_add = cui_boot_option_add,
569 .device_remove = cui_device_remove,
570 .update_status = cui_update_status,
571 .update_sysinfo = cui_update_sysinfo,
572 .update_config = cui_update_config,
576 * cui_init - Setup the cui instance.
577 * @platform_info: A value for the struct cui platform_info member.
579 * Returns a pointer to a struct cui on success, or NULL on error.
581 * Allocates the cui instance, sets up the client and stdin waiters, and
582 * sets up the ncurses menu screen.
585 struct cui *cui_init(void* platform_info,
586 int (*js_map)(const struct js_event *e), int start_deamon)
591 cui = talloc_zero(NULL, struct cui);
594 pb_log("%s: alloc cui failed.\n", __func__);
595 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
599 cui->c_sig = pb_cui_sig;
600 cui->platform_info = platform_info;
601 cui->waitset = waitset_create(cui);
603 process_init(cui, cui->waitset, false);
605 setlocale(LC_ALL, "");
607 /* Loop here for scripts that just started the server. */
610 for (i = start_deamon ? 2 : 10; i; i--) {
611 cui->client = discover_client_init(cui->waitset,
612 &cui_client_ops, cui);
613 if (cui->client || !i)
615 pb_log("%s: waiting for server %d\n", __func__, i);
619 if (!cui->client && start_deamon) {
624 result = pb_start_daemon(cui);
629 pb_log("%s: discover_client_init failed.\n", __func__);
630 fprintf(stderr, "%s: error: discover_client_init failed.\n",
632 fprintf(stderr, "could not start pb-discover, the petitboot "
634 goto fail_client_init;
638 pb_log("%s: discover_client_init failed.\n", __func__);
639 fprintf(stderr, "%s: error: discover_client_init failed.\n",
641 fprintf(stderr, "check that pb-discover, "
642 "the petitboot daemon is running.\n");
643 goto fail_client_init;
649 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
650 cui_process_key, cui);
654 cui->pjs = pjs_init(cui, js_map);
657 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
658 WAIT_IN, cui_process_js, cui);
670 * cui_run - The main cui program loop.
671 * @cui: The cui instance.
672 * @main: The menu to use as the main menu.
674 * Runs the cui engine. Does not return until indicated to do so by some
675 * user action, or an error occurs. Frees the cui object on return.
676 * Returns 0 on success (return to shell), -1 on error (should restart).
679 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
684 cui->current = &cui->main->scr;
685 cui->default_item = default_item;
687 nc_scr_post(cui->current);
690 int result = waiter_poll(cui->waitset);
693 pb_log("%s: poll: %s\n", __func__, strerror(errno));
700 while (cui->resize) {
702 cui_handle_resize(cui);
708 return cui->abort ? 0 : -1;