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-sysinfo.h"
39 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
44 static void cui_start(void)
46 initscr(); /* Initialize ncurses. */
47 cbreak(); /* Disable line buffering. */
48 noecho(); /* Disable getch() echo. */
49 keypad(stdscr, TRUE); /* Enable num keypad keys. */
50 nonl(); /* Disable new-line translation. */
51 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
52 curs_set(0); /* Make cursor invisible */
53 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
55 /* We may be operating with an incorrect $TERM type; in this case
56 * the keymappings will be slightly broken. We want at least
57 * backspace to work though, so we'll define both DEL and ^H to
59 define_key("\x7f", KEY_BACKSPACE);
60 define_key("\x08", KEY_BACKSPACE);
62 while (getch() != ERR) /* flush stdin */
66 static void cui_atexit(void)
74 * cui_abort - Signal the main cui program loop to exit.
76 * Sets cui.abort, which causes the cui_run() routine to return.
79 void cui_abort(struct cui *cui)
81 pb_log("%s: exiting\n", __func__);
86 * cui_resize - Signal the main cui program loop to resize
91 void cui_resize(struct cui *cui)
93 pb_debug("%s: resizing\n", __func__);
98 * cui_on_exit - A generic main menu exit callback.
101 void cui_on_exit(struct pmenu *menu)
103 cui_abort(cui_from_pmenu(menu));
107 * cui_run_cmd - A generic cb to run the supplied command.
110 int cui_run_cmd(struct pmenu_item *item)
113 struct cui *cui = cui_from_item(item);
114 const char **cmd_argv = item->data;
116 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
120 result = process_run_simple_argv(item, cmd_argv);
123 redrawwin(cui->current->main_ncw);
126 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
127 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
134 * cui_boot - A generic cb to run kexec.
137 static int cui_boot(struct pmenu_item *item)
140 struct cui *cui = cui_from_item(item);
141 struct cui_opt_data *cod = cod_from_item(item);
143 assert(cui->current == &cui->main->scr);
145 pb_debug("%s: %s\n", __func__, cod->name);
147 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
151 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
154 redrawwin(cui->current->main_ncw);
157 nc_scr_status_printf(cui->current,
158 "Failed: boot %s", cod->bd->image);
165 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
168 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
169 enum boot_editor_result boot_editor_result,
170 struct pb_boot_data *bd)
172 struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
173 struct pmenu_item *item = boot_editor->data;
174 struct cui_opt_data *cod;
176 if (boot_editor_result != boot_editor_update) {
177 cui_set_current(cui, &cui->main->scr);
178 talloc_free(boot_editor);
184 /* Is this was a new item, we'll need to update the menu */
186 struct pmenu *menu = boot_editor->original_pmenu;
189 /* Detach the items array. */
190 set_menu_items(menu->ncm, NULL);
192 /* Insert new item at insert_pt. */
193 insert_pt = pmenu_grow(menu, 1);
194 item = pmenu_item_alloc(menu);
195 item->on_edit = cui_item_edit;
196 item->on_execute = cui_boot;
197 item->data = cod = talloc_zero(item, struct cui_opt_data);
199 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
200 pmenu_item_setup(menu, item, insert_pt,
201 talloc_strdup(item, cod->name));
203 /* Re-attach the items array. */
204 set_menu_items(menu->ncm, menu->items);
205 nc_scr_post(&menu->scr);
210 cod->bd = talloc_steal(cod, bd);
212 /* FIXME: need to make item visible somehow */
213 set_current_item(item->pmenu->ncm, item->nci);
214 cui_set_current(cui, &cui->main->scr);
215 talloc_free(boot_editor);
218 void cui_item_edit(struct pmenu_item *item)
220 struct cui *cui = cui_from_item(item);
221 struct cui_opt_data *cod = cod_from_item(item);
222 struct boot_editor *boot_editor;
224 boot_editor = boot_editor_init(item->pmenu, cod->bd,
225 cui_boot_editor_on_exit);
226 boot_editor->data = item;
227 cui_set_current(cui, &boot_editor->scr);
230 void cui_item_new(struct pmenu *menu)
232 struct cui *cui = cui_from_pmenu(menu);
233 struct boot_editor *boot_editor;
235 boot_editor = boot_editor_init(menu, NULL,
236 cui_boot_editor_on_exit);
237 cui_set_current(cui, &boot_editor->scr);
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));
255 * cui_set_current - Set the currently active screen and redraw it.
258 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
262 DBGS("%p -> %p\n", cui->current, scr);
264 assert(cui->current != scr);
271 nc_scr_post(cui->current);
276 static bool process_global_keys(struct cui *cui, int key)
280 if (cui->current && cui->current->main_ncw) {
281 redrawwin(cui->current->main_ncw);
282 wrefresh(cui->current->main_ncw);
290 * cui_process_key - Process input on stdin.
293 static int cui_process_key(void *arg)
295 struct cui *cui = cui_from_arg(arg);
297 assert(cui->current);
300 discover_client_cancel_default(cui->client);
301 cui->has_input = true;
309 if (process_global_keys(cui, c))
312 cui->current->process_key(cui->current, c);
319 * cui_process_js - Process joystick events.
322 static int cui_process_js(void *arg)
324 struct cui *cui = cui_from_arg(arg);
327 c = pjs_process_event(cui->pjs);
331 cui_process_key(arg);
338 * cui_handle_resize - Handle the term resize.
341 static void cui_handle_resize(struct cui *cui)
345 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
346 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
350 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
352 wclear(cui->current->main_ncw);
353 resize_term(ws.ws_row, ws.ws_col);
354 cui->current->resize(cui->current);
356 /* For some reason this makes ncurses redraw the screen */
358 redrawwin(cui->current->main_ncw);
359 wrefresh(cui->current->main_ncw);
363 * cui_device_add - Client device_add callback.
365 * Creates menu_items for all the device boot_options and inserts those
366 * menu_items into the main menu. Redraws the main menu if it is active.
369 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
372 struct cui *cui = cui_from_arg(arg);
373 struct cui_opt_data *cod;
374 unsigned int insert_pt;
375 struct pmenu_item *i;
379 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
381 selected = current_item(cui->main->ncm);
383 if (cui->current == &cui->main->scr)
384 nc_scr_unpost(cui->current);
386 /* This disconnects items array from menu. */
388 result = set_menu_items(cui->main->ncm, NULL);
391 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
393 /* Insert new items at insert_pt. */
394 insert_pt = pmenu_grow(cui->main, 1);
396 /* Save the item in opt->ui_info for cui_device_remove() */
398 opt->ui_info = i = pmenu_item_alloc(cui->main);
400 i->on_edit = cui_item_edit;
401 i->on_execute = cui_boot;
402 i->data = cod = talloc(i, struct cui_opt_data);
406 cod->opt_hash = pb_opt_hash(dev, opt);
407 cod->name = opt->name;
408 cod->bd = talloc(i, struct pb_boot_data);
410 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
411 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
412 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
413 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
415 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
417 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
418 pb_log(" image '%s'\n", cod->bd->image);
419 pb_log(" initrd '%s'\n", cod->bd->initrd);
420 pb_log(" args '%s'\n", cod->bd->args);
422 /* Re-attach the items array. */
423 result = set_menu_items(cui->main->ncm, cui->main->items);
426 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
429 pb_log("%s\n", __func__);
430 pmenu_dump_items(cui->main->items,
431 item_count(cui->main->ncm) + 1);
434 /* FIXME: need to make item visible somehow */
435 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
436 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
437 set_current_item(cui->main->ncm, selected);
439 if (cui->current == &cui->main->scr)
440 nc_scr_post(cui->current);
446 * cui_device_remove - Client device remove callback.
448 * Removes all the menu_items for the device from the main menu and redraws the
449 * main menu if it is active.
452 static void cui_device_remove(struct device *dev, void *arg)
454 struct cui *cui = cui_from_arg(arg);
456 struct boot_option *opt;
458 pb_log("%s: %p %s\n", __func__, dev, dev->id);
460 if (cui->current == &cui->main->scr)
461 nc_scr_unpost(cui->current);
463 /* This disconnects items array from menu. */
465 result = set_menu_items(cui->main->ncm, NULL);
468 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
470 list_for_each_entry(&dev->boot_options, opt, list) {
471 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
473 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
474 pmenu_remove(cui->main, i);
477 /* Re-attach the items array. */
479 result = set_menu_items(cui->main->ncm, cui->main->items);
482 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
485 pb_log("%s\n", __func__);
486 pmenu_dump_items(cui->main->items,
487 item_count(cui->main->ncm) + 1);
490 if (cui->current == &cui->main->scr)
491 nc_scr_post(cui->current);
494 static void cui_update_status(struct boot_status *status, void *arg)
496 struct cui *cui = cui_from_arg(arg);
498 nc_scr_status_printf(cui->current,
500 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
505 static void cui_update_mm_title(struct cui *cui)
507 struct nc_frame *frame = &cui->main->scr.frame;
509 talloc_free(frame->rtitle);
511 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
512 if (cui->sysinfo->identifier)
513 frame->rtitle = talloc_asprintf_append(frame->rtitle,
514 " %s", cui->sysinfo->identifier);
516 if (cui->current == &cui->main->scr)
517 nc_scr_post(cui->current);
520 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
522 struct cui *cui = cui_from_arg(arg);
523 cui->sysinfo = talloc_steal(cui, sysinfo);
525 /* if we're currently displaying the system info screen, inform it
526 * of the updated information. */
527 if (cui->sysinfo_screen)
528 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
530 cui_update_mm_title(cui);
533 static struct discover_client_ops cui_client_ops = {
535 .boot_option_add = cui_boot_option_add,
536 .device_remove = cui_device_remove,
537 .update_status = cui_update_status,
538 .update_sysinfo = cui_update_sysinfo,
542 * cui_init - Setup the cui instance.
543 * @platform_info: A value for the struct cui platform_info member.
545 * Returns a pointer to a struct cui on success, or NULL on error.
547 * Allocates the cui instance, sets up the client and stdin waiters, and
548 * sets up the ncurses menu screen.
551 struct cui *cui_init(void* platform_info,
552 int (*js_map)(const struct js_event *e), int start_deamon)
557 cui = talloc_zero(NULL, struct cui);
560 pb_log("%s: alloc cui failed.\n", __func__);
561 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
565 cui->c_sig = pb_cui_sig;
566 cui->platform_info = platform_info;
567 cui->waitset = waitset_create(cui);
569 process_init(cui, cui->waitset, false);
571 setlocale(LC_ALL, "");
573 /* Loop here for scripts that just started the server. */
576 for (i = start_deamon ? 2 : 10; i; i--) {
577 cui->client = discover_client_init(cui->waitset,
578 &cui_client_ops, cui);
579 if (cui->client || !i)
581 pb_log("%s: waiting for server %d\n", __func__, i);
585 if (!cui->client && start_deamon) {
590 result = pb_start_daemon(cui);
595 pb_log("%s: discover_client_init failed.\n", __func__);
596 fprintf(stderr, "%s: error: discover_client_init failed.\n",
598 fprintf(stderr, "could not start pb-discover, the petitboot "
600 goto fail_client_init;
604 pb_log("%s: discover_client_init failed.\n", __func__);
605 fprintf(stderr, "%s: error: discover_client_init failed.\n",
607 fprintf(stderr, "check that pb-discover, "
608 "the petitboot daemon is running.\n");
609 goto fail_client_init;
615 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
616 cui_process_key, cui);
620 cui->pjs = pjs_init(cui, js_map);
623 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
624 WAIT_IN, cui_process_js, cui);
636 * cui_run - The main cui program loop.
637 * @cui: The cui instance.
638 * @main: The menu to use as the main menu.
640 * Runs the cui engine. Does not return until indicated to do so by some
641 * user action, or an error occurs. Frees the cui object on return.
642 * Returns 0 on success (return to shell), -1 on error (should restart).
645 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
650 cui->current = &cui->main->scr;
651 cui->default_item = default_item;
653 nc_scr_post(cui->current);
656 int result = waiter_poll(cui->waitset);
659 pb_log("%s: poll: %s\n", __func__, strerror(errno));
666 while (cui->resize) {
668 cui_handle_resize(cui);
674 return cui->abort ? 0 : -1;