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"
36 #include "ui/common/ui-system.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-sysinfo.h"
42 static void cui_start(void)
44 initscr(); /* Initialize ncurses. */
45 cbreak(); /* Disable line buffering. */
46 noecho(); /* Disable getch() echo. */
47 keypad(stdscr, TRUE); /* Enable num keypad keys. */
48 nonl(); /* Disable new-line translation. */
49 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
50 curs_set(0); /* Make cursor invisible */
51 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
53 /* We may be operating with an incorrect $TERM type; in this case
54 * the keymappings will be slightly broken. We want at least
55 * backspace to work though, so we'll define both DEL and ^H to
57 define_key("\x7f", KEY_BACKSPACE);
58 define_key("\x08", KEY_BACKSPACE);
60 while (getch() != ERR) /* flush stdin */
64 static void cui_atexit(void)
72 * cui_abort - Signal the main cui program loop to exit.
74 * Sets cui.abort, which causes the cui_run() routine to return.
77 void cui_abort(struct cui *cui)
79 pb_log("%s: exiting\n", __func__);
84 * cui_resize - Signal the main cui program loop to resize
89 void cui_resize(struct cui *cui)
91 pb_debug("%s: resizing\n", __func__);
96 * cui_on_exit - A generic main menu exit callback.
99 void cui_on_exit(struct pmenu *menu)
101 cui_abort(cui_from_pmenu(menu));
105 * cui_run_cmd - A generic cb to run the supplied command.
108 int cui_run_cmd(struct pmenu_item *item)
111 struct cui *cui = cui_from_item(item);
112 const char **cmd_argv = item->data;
114 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
118 result = process_run_simple_argv(item, cmd_argv);
121 redrawwin(cui->current->main_ncw);
124 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
125 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
132 * cui_boot - A generic cb to run kexec.
135 static int cui_boot(struct pmenu_item *item)
138 struct cui *cui = cui_from_item(item);
139 struct cui_opt_data *cod = cod_from_item(item);
141 assert(cui->current == &cui->main->scr);
143 pb_debug("%s: %s\n", __func__, cod->name);
145 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
147 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
150 nc_scr_status_printf(cui->current,
151 "Failed: boot %s", cod->bd->image);
157 static void cui_boot_editor_on_exit(struct cui *cui,
158 struct pmenu_item *item,
159 struct pb_boot_data *bd)
161 struct pmenu *menu = cui->main;
162 struct cui_opt_data *cod;
164 /* Was the edit cancelled? */
166 cui_set_current(cui, &cui->main->scr);
167 talloc_free(cui->boot_editor);
168 cui->boot_editor = NULL;
172 /* Is this was a new item, we'll need to update the menu */
176 /* Detach the items array. */
177 set_menu_items(menu->ncm, NULL);
179 /* Insert new item at insert_pt. */
180 insert_pt = pmenu_grow(menu, 1);
181 item = pmenu_item_alloc(menu);
182 item->on_edit = cui_item_edit;
183 item->on_execute = cui_boot;
184 item->data = cod = talloc_zero(item, struct cui_opt_data);
186 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
187 pmenu_item_setup(menu, item, insert_pt,
188 talloc_strdup(item, cod->name));
190 /* Re-attach the items array. */
191 set_menu_items(menu->ncm, menu->items);
192 nc_scr_post(&menu->scr);
197 cod->bd = talloc_steal(cod, bd);
199 set_current_item(item->pmenu->ncm, item->nci);
200 cui_set_current(cui, &cui->main->scr);
201 talloc_free(cui->boot_editor);
202 cui->boot_editor = NULL;
205 void cui_item_edit(struct pmenu_item *item)
207 struct cui *cui = cui_from_item(item);
208 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
209 cui_boot_editor_on_exit);
210 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
213 void cui_item_new(struct pmenu *menu)
215 struct cui *cui = cui_from_pmenu(menu);
216 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
217 cui_boot_editor_on_exit);
218 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
221 static void cui_sysinfo_exit(struct cui *cui)
223 cui_set_current(cui, &cui->main->scr);
224 talloc_free(cui->sysinfo_screen);
225 cui->sysinfo_screen = NULL;
228 void cui_show_sysinfo(struct cui *cui)
230 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
232 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
235 static void cui_config_exit(struct cui *cui)
237 cui_set_current(cui, &cui->main->scr);
238 talloc_free(cui->config_screen);
239 cui->config_screen = NULL;
242 void cui_show_config(struct cui *cui)
244 cui->config_screen = config_screen_init(cui, cui->config,
245 cui->sysinfo, cui_config_exit);
246 cui_set_current(cui, config_screen_scr(cui->config_screen));
250 * cui_set_current - Set the currently active screen and redraw it.
253 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
257 DBGS("%p -> %p\n", cui->current, scr);
259 assert(cui->current != scr);
266 nc_scr_post(cui->current);
271 static bool process_global_keys(struct cui *cui, int key)
275 if (cui->current && cui->current->main_ncw)
283 * cui_process_key - Process input on stdin.
286 static int cui_process_key(void *arg)
288 struct cui *cui = cui_from_arg(arg);
290 assert(cui->current);
293 discover_client_cancel_default(cui->client);
294 cui->has_input = true;
299 pb_debug("%s: got key %d\n", __func__, c);
304 if (process_global_keys(cui, c))
307 cui->current->process_key(cui->current, c);
314 * cui_process_js - Process joystick events.
317 static int cui_process_js(void *arg)
319 struct cui *cui = cui_from_arg(arg);
322 c = pjs_process_event(cui->pjs);
326 cui_process_key(arg);
333 * cui_handle_resize - Handle the term resize.
336 static void cui_handle_resize(struct cui *cui)
340 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
341 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
345 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
347 wclear(cui->current->main_ncw);
348 resize_term(ws.ws_row, ws.ws_col);
349 cui->current->resize(cui->current);
351 /* For some reason this makes ncurses redraw the screen */
353 redrawwin(cui->current->main_ncw);
354 wrefresh(cui->current->main_ncw);
358 * cui_device_add - Client device_add callback.
360 * Creates menu_items for all the device boot_options and inserts those
361 * menu_items into the main menu. Redraws the main menu if it is active.
364 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
367 struct cui *cui = cui_from_arg(arg);
368 struct cui_opt_data *cod;
369 unsigned int insert_pt;
370 struct pmenu_item *i;
374 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
376 selected = current_item(cui->main->ncm);
378 if (cui->current == &cui->main->scr)
379 nc_scr_unpost(cui->current);
381 /* This disconnects items array from menu. */
383 result = set_menu_items(cui->main->ncm, NULL);
386 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
388 /* Insert new items at insert_pt. */
389 insert_pt = pmenu_grow(cui->main, 1);
391 /* Save the item in opt->ui_info for cui_device_remove() */
393 opt->ui_info = i = pmenu_item_alloc(cui->main);
395 i->on_edit = cui_item_edit;
396 i->on_execute = cui_boot;
397 i->data = cod = talloc(i, struct cui_opt_data);
401 cod->opt_hash = pb_opt_hash(dev, opt);
402 cod->name = opt->name;
403 cod->bd = talloc(i, struct pb_boot_data);
405 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
406 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
407 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
408 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
410 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
412 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
413 pb_log(" image '%s'\n", cod->bd->image);
414 pb_log(" initrd '%s'\n", cod->bd->initrd);
415 pb_log(" args '%s'\n", cod->bd->args);
417 /* Re-attach the items array. */
418 result = set_menu_items(cui->main->ncm, cui->main->items);
421 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
424 pb_log("%s\n", __func__);
425 pmenu_dump_items(cui->main->items,
426 item_count(cui->main->ncm) + 1);
429 /* FIXME: need to make item visible somehow */
430 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
431 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
432 set_current_item(cui->main->ncm, selected);
434 if (cui->current == &cui->main->scr)
435 nc_scr_post(cui->current);
441 * cui_device_remove - Client device remove callback.
443 * Removes all the menu_items for the device from the main menu and redraws the
444 * main menu if it is active.
447 static void cui_device_remove(struct device *dev, void *arg)
449 struct cui *cui = cui_from_arg(arg);
451 struct boot_option *opt;
453 pb_log("%s: %p %s\n", __func__, dev, dev->id);
455 if (cui->current == &cui->main->scr)
456 nc_scr_unpost(cui->current);
458 /* This disconnects items array from menu. */
460 result = set_menu_items(cui->main->ncm, NULL);
463 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
465 list_for_each_entry(&dev->boot_options, opt, list) {
466 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
468 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
469 pmenu_remove(cui->main, i);
472 /* Re-attach the items array. */
474 result = set_menu_items(cui->main->ncm, cui->main->items);
477 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
480 pb_log("%s\n", __func__);
481 pmenu_dump_items(cui->main->items,
482 item_count(cui->main->ncm) + 1);
485 if (cui->current == &cui->main->scr)
486 nc_scr_post(cui->current);
489 static void cui_update_status(struct boot_status *status, void *arg)
491 struct cui *cui = cui_from_arg(arg);
493 nc_scr_status_printf(cui->current,
495 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
500 static void cui_update_mm_title(struct cui *cui)
502 struct nc_frame *frame = &cui->main->scr.frame;
504 talloc_free(frame->rtitle);
506 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
507 if (cui->sysinfo->identifier)
508 frame->rtitle = talloc_asprintf_append(frame->rtitle,
509 " %s", cui->sysinfo->identifier);
511 if (cui->current == &cui->main->scr)
512 nc_scr_post(cui->current);
515 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
517 struct cui *cui = cui_from_arg(arg);
518 cui->sysinfo = talloc_steal(cui, sysinfo);
520 /* if we're currently displaying the system info screen, inform it
521 * of the updated information. */
522 if (cui->sysinfo_screen)
523 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
525 /* ... and do the same with the config screen... */
526 if (cui->config_screen)
527 config_screen_update(cui->config_screen, cui->config, sysinfo);
529 /* ... and the boot editor. */
530 if (cui->boot_editor)
531 boot_editor_update(cui->boot_editor, sysinfo);
533 cui_update_mm_title(cui);
536 static void cui_update_config(struct config *config, void *arg)
538 struct cui *cui = cui_from_arg(arg);
539 cui->config = talloc_steal(cui, config);
541 if (cui->config_screen)
542 config_screen_update(cui->config_screen, config, cui->sysinfo);
545 int cui_send_config(struct cui *cui, struct config *config)
547 return discover_client_send_config(cui->client, config);
550 static struct discover_client_ops cui_client_ops = {
552 .boot_option_add = cui_boot_option_add,
553 .device_remove = cui_device_remove,
554 .update_status = cui_update_status,
555 .update_sysinfo = cui_update_sysinfo,
556 .update_config = cui_update_config,
560 * cui_init - Setup the cui instance.
561 * @platform_info: A value for the struct cui platform_info member.
563 * Returns a pointer to a struct cui on success, or NULL on error.
565 * Allocates the cui instance, sets up the client and stdin waiters, and
566 * sets up the ncurses menu screen.
569 struct cui *cui_init(void* platform_info,
570 int (*js_map)(const struct js_event *e), int start_deamon)
575 cui = talloc_zero(NULL, struct cui);
578 pb_log("%s: alloc cui failed.\n", __func__);
579 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
583 cui->c_sig = pb_cui_sig;
584 cui->platform_info = platform_info;
585 cui->waitset = waitset_create(cui);
587 process_init(cui, cui->waitset, false);
589 setlocale(LC_ALL, "");
591 /* Loop here for scripts that just started the server. */
594 for (i = start_deamon ? 2 : 10; i; i--) {
595 cui->client = discover_client_init(cui->waitset,
596 &cui_client_ops, cui);
597 if (cui->client || !i)
599 pb_log("%s: waiting for server %d\n", __func__, i);
603 if (!cui->client && start_deamon) {
608 result = pb_start_daemon(cui);
613 pb_log("%s: discover_client_init failed.\n", __func__);
614 fprintf(stderr, "%s: error: discover_client_init failed.\n",
616 fprintf(stderr, "could not start pb-discover, the petitboot "
618 goto fail_client_init;
622 pb_log("%s: discover_client_init failed.\n", __func__);
623 fprintf(stderr, "%s: error: discover_client_init failed.\n",
625 fprintf(stderr, "check that pb-discover, "
626 "the petitboot daemon is running.\n");
627 goto fail_client_init;
633 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
634 cui_process_key, cui);
638 cui->pjs = pjs_init(cui, js_map);
641 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
642 WAIT_IN, cui_process_js, cui);
654 * cui_run - The main cui program loop.
655 * @cui: The cui instance.
656 * @main: The menu to use as the main menu.
658 * Runs the cui engine. Does not return until indicated to do so by some
659 * user action, or an error occurs. Frees the cui object on return.
660 * Returns 0 on success (return to shell), -1 on error (should restart).
663 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
668 cui->current = &cui->main->scr;
669 cui->default_item = default_item;
671 nc_scr_post(cui->current);
674 int result = waiter_poll(cui->waitset);
677 pb_log("%s: poll: %s\n", __func__, strerror(errno));
684 while (cui->resize) {
686 cui_handle_resize(cui);
692 return cui->abort ? 0 : -1;