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);
150 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
153 nc_scr_status_printf(cui->current,
154 "Failed: boot %s", cod->bd->image);
161 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
164 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
165 enum boot_editor_result boot_editor_result,
166 struct pb_boot_data *bd)
168 struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
169 struct pmenu_item *item = boot_editor->data;
170 struct cui_opt_data *cod;
172 if (boot_editor_result != boot_editor_update) {
173 cui_set_current(cui, &cui->main->scr);
174 talloc_free(boot_editor);
180 /* Is this was a new item, we'll need to update the menu */
182 struct pmenu *menu = boot_editor->original_pmenu;
185 /* Detach the items array. */
186 set_menu_items(menu->ncm, NULL);
188 /* Insert new item at insert_pt. */
189 insert_pt = pmenu_grow(menu, 1);
190 item = pmenu_item_alloc(menu);
191 item->on_edit = cui_item_edit;
192 item->on_execute = cui_boot;
193 item->data = cod = talloc_zero(item, struct cui_opt_data);
195 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
196 pmenu_item_setup(menu, item, insert_pt,
197 talloc_strdup(item, cod->name));
199 /* Re-attach the items array. */
200 set_menu_items(menu->ncm, menu->items);
201 nc_scr_post(&menu->scr);
206 cod->bd = talloc_steal(cod, bd);
208 /* FIXME: need to make item visible somehow */
209 set_current_item(item->pmenu->ncm, item->nci);
210 cui_set_current(cui, &cui->main->scr);
211 talloc_free(boot_editor);
214 void cui_item_edit(struct pmenu_item *item)
216 struct cui *cui = cui_from_item(item);
217 struct cui_opt_data *cod = cod_from_item(item);
218 struct boot_editor *boot_editor;
220 boot_editor = boot_editor_init(item->pmenu, cod->bd,
221 cui_boot_editor_on_exit);
222 boot_editor->data = item;
223 cui_set_current(cui, &boot_editor->scr);
226 void cui_item_new(struct pmenu *menu)
228 struct cui *cui = cui_from_pmenu(menu);
229 struct boot_editor *boot_editor;
231 boot_editor = boot_editor_init(menu, NULL,
232 cui_boot_editor_on_exit);
233 cui_set_current(cui, &boot_editor->scr);
236 static void cui_sysinfo_exit(struct cui *cui)
238 cui_set_current(cui, &cui->main->scr);
239 talloc_free(cui->sysinfo_screen);
240 cui->sysinfo_screen = NULL;
243 void cui_show_sysinfo(struct cui *cui)
245 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
247 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
250 static void cui_config_exit(struct cui *cui)
252 cui_set_current(cui, &cui->main->scr);
253 talloc_free(cui->config_screen);
254 cui->config_screen = NULL;
257 void cui_show_config(struct cui *cui)
259 cui->config_screen = config_screen_init(cui, cui->config,
260 cui->sysinfo, cui_config_exit);
261 cui_set_current(cui, config_screen_scr(cui->config_screen));
265 * cui_set_current - Set the currently active screen and redraw it.
268 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
272 DBGS("%p -> %p\n", cui->current, scr);
274 assert(cui->current != scr);
281 nc_scr_post(cui->current);
286 static bool process_global_keys(struct cui *cui, int key)
290 if (cui->current && cui->current->main_ncw) {
291 redrawwin(cui->current->main_ncw);
292 wrefresh(cui->current->main_ncw);
300 * cui_process_key - Process input on stdin.
303 static int cui_process_key(void *arg)
305 struct cui *cui = cui_from_arg(arg);
307 assert(cui->current);
310 discover_client_cancel_default(cui->client);
311 cui->has_input = true;
319 if (process_global_keys(cui, c))
322 cui->current->process_key(cui->current, c);
329 * cui_process_js - Process joystick events.
332 static int cui_process_js(void *arg)
334 struct cui *cui = cui_from_arg(arg);
337 c = pjs_process_event(cui->pjs);
341 cui_process_key(arg);
348 * cui_handle_resize - Handle the term resize.
351 static void cui_handle_resize(struct cui *cui)
355 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
356 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
360 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
362 wclear(cui->current->main_ncw);
363 resize_term(ws.ws_row, ws.ws_col);
364 cui->current->resize(cui->current);
366 /* For some reason this makes ncurses redraw the screen */
368 redrawwin(cui->current->main_ncw);
369 wrefresh(cui->current->main_ncw);
373 * cui_device_add - Client device_add callback.
375 * Creates menu_items for all the device boot_options and inserts those
376 * menu_items into the main menu. Redraws the main menu if it is active.
379 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
382 struct cui *cui = cui_from_arg(arg);
383 struct cui_opt_data *cod;
384 unsigned int insert_pt;
385 struct pmenu_item *i;
389 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
391 selected = current_item(cui->main->ncm);
393 if (cui->current == &cui->main->scr)
394 nc_scr_unpost(cui->current);
396 /* This disconnects items array from menu. */
398 result = set_menu_items(cui->main->ncm, NULL);
401 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
403 /* Insert new items at insert_pt. */
404 insert_pt = pmenu_grow(cui->main, 1);
406 /* Save the item in opt->ui_info for cui_device_remove() */
408 opt->ui_info = i = pmenu_item_alloc(cui->main);
410 i->on_edit = cui_item_edit;
411 i->on_execute = cui_boot;
412 i->data = cod = talloc(i, struct cui_opt_data);
416 cod->opt_hash = pb_opt_hash(dev, opt);
417 cod->name = opt->name;
418 cod->bd = talloc(i, struct pb_boot_data);
420 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
421 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
422 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
423 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
425 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
427 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
428 pb_log(" image '%s'\n", cod->bd->image);
429 pb_log(" initrd '%s'\n", cod->bd->initrd);
430 pb_log(" args '%s'\n", cod->bd->args);
432 /* Re-attach the items array. */
433 result = set_menu_items(cui->main->ncm, cui->main->items);
436 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
439 pb_log("%s\n", __func__);
440 pmenu_dump_items(cui->main->items,
441 item_count(cui->main->ncm) + 1);
444 /* FIXME: need to make item visible somehow */
445 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
446 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
447 set_current_item(cui->main->ncm, selected);
449 if (cui->current == &cui->main->scr)
450 nc_scr_post(cui->current);
456 * cui_device_remove - Client device remove callback.
458 * Removes all the menu_items for the device from the main menu and redraws the
459 * main menu if it is active.
462 static void cui_device_remove(struct device *dev, void *arg)
464 struct cui *cui = cui_from_arg(arg);
466 struct boot_option *opt;
468 pb_log("%s: %p %s\n", __func__, dev, dev->id);
470 if (cui->current == &cui->main->scr)
471 nc_scr_unpost(cui->current);
473 /* This disconnects items array from menu. */
475 result = set_menu_items(cui->main->ncm, NULL);
478 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
480 list_for_each_entry(&dev->boot_options, opt, list) {
481 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
483 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
484 pmenu_remove(cui->main, i);
487 /* Re-attach the items array. */
489 result = set_menu_items(cui->main->ncm, cui->main->items);
492 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
495 pb_log("%s\n", __func__);
496 pmenu_dump_items(cui->main->items,
497 item_count(cui->main->ncm) + 1);
500 if (cui->current == &cui->main->scr)
501 nc_scr_post(cui->current);
504 static void cui_update_status(struct boot_status *status, void *arg)
506 struct cui *cui = cui_from_arg(arg);
508 nc_scr_status_printf(cui->current,
510 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
515 static void cui_update_mm_title(struct cui *cui)
517 struct nc_frame *frame = &cui->main->scr.frame;
519 talloc_free(frame->rtitle);
521 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
522 if (cui->sysinfo->identifier)
523 frame->rtitle = talloc_asprintf_append(frame->rtitle,
524 " %s", cui->sysinfo->identifier);
526 if (cui->current == &cui->main->scr)
527 nc_scr_post(cui->current);
530 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
532 struct cui *cui = cui_from_arg(arg);
533 cui->sysinfo = talloc_steal(cui, sysinfo);
535 /* if we're currently displaying the system info screen, inform it
536 * of the updated information. */
537 if (cui->sysinfo_screen)
538 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
540 /* ... and do the same with the config screen */
541 if (cui->config_screen)
542 config_screen_update(cui->config_screen, cui->config, sysinfo);
544 cui_update_mm_title(cui);
547 static void cui_update_config(struct config *config, void *arg)
549 struct cui *cui = cui_from_arg(arg);
550 cui->config = talloc_steal(cui, config);
552 if (cui->config_screen)
553 config_screen_update(cui->config_screen, config, cui->sysinfo);
556 int cui_send_config(struct cui *cui, struct config *config)
558 return discover_client_send_config(cui->client, config);
561 static struct discover_client_ops cui_client_ops = {
563 .boot_option_add = cui_boot_option_add,
564 .device_remove = cui_device_remove,
565 .update_status = cui_update_status,
566 .update_sysinfo = cui_update_sysinfo,
567 .update_config = cui_update_config,
571 * cui_init - Setup the cui instance.
572 * @platform_info: A value for the struct cui platform_info member.
574 * Returns a pointer to a struct cui on success, or NULL on error.
576 * Allocates the cui instance, sets up the client and stdin waiters, and
577 * sets up the ncurses menu screen.
580 struct cui *cui_init(void* platform_info,
581 int (*js_map)(const struct js_event *e), int start_deamon)
586 cui = talloc_zero(NULL, struct cui);
589 pb_log("%s: alloc cui failed.\n", __func__);
590 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
594 cui->c_sig = pb_cui_sig;
595 cui->platform_info = platform_info;
596 cui->waitset = waitset_create(cui);
598 process_init(cui, cui->waitset, false);
600 setlocale(LC_ALL, "");
602 /* Loop here for scripts that just started the server. */
605 for (i = start_deamon ? 2 : 10; i; i--) {
606 cui->client = discover_client_init(cui->waitset,
607 &cui_client_ops, cui);
608 if (cui->client || !i)
610 pb_log("%s: waiting for server %d\n", __func__, i);
614 if (!cui->client && start_deamon) {
619 result = pb_start_daemon(cui);
624 pb_log("%s: discover_client_init failed.\n", __func__);
625 fprintf(stderr, "%s: error: discover_client_init failed.\n",
627 fprintf(stderr, "could not start pb-discover, the petitboot "
629 goto fail_client_init;
633 pb_log("%s: discover_client_init failed.\n", __func__);
634 fprintf(stderr, "%s: error: discover_client_init failed.\n",
636 fprintf(stderr, "check that pb-discover, "
637 "the petitboot daemon is running.\n");
638 goto fail_client_init;
644 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
645 cui_process_key, cui);
649 cui->pjs = pjs_init(cui, js_map);
652 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
653 WAIT_IN, cui_process_js, cui);
665 * cui_run - The main cui program loop.
666 * @cui: The cui instance.
667 * @main: The menu to use as the main menu.
669 * Runs the cui engine. Does not return until indicated to do so by some
670 * user action, or an error occurs. Frees the cui object on return.
671 * Returns 0 on success (return to shell), -1 on error (should restart).
674 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
679 cui->current = &cui->main->scr;
680 cui->default_item = default_item;
682 nc_scr_post(cui->current);
685 int result = waiter_poll(cui->waitset);
688 pb_log("%s: poll: %s\n", __func__, strerror(errno));
695 while (cui->resize) {
697 cui_handle_resize(cui);
703 return cui->abort ? 0 : -1;