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"
38 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
43 static void cui_start(void)
45 initscr(); /* Initialize ncurses. */
46 cbreak(); /* Disable line buffering. */
47 noecho(); /* Disable getch() echo. */
48 keypad(stdscr, TRUE); /* Enable num keypad keys. */
49 nonl(); /* Disable new-line translation. */
50 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
51 curs_set(0); /* Make cursor invisible */
52 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
54 /* We may be operating with an incorrect $TERM type; in this case
55 * the keymappings will be slightly broken. We want at least
56 * backspace to work though, so we'll define both DEL and ^H to
58 define_key("\x7f", KEY_BACKSPACE);
59 define_key("\x08", KEY_BACKSPACE);
61 while (getch() != ERR) /* flush stdin */
65 static void cui_atexit(void)
73 * cui_abort - Signal the main cui program loop to exit.
75 * Sets cui.abort, which causes the cui_run() routine to return.
78 void cui_abort(struct cui *cui)
80 pb_log("%s: exiting\n", __func__);
85 * cui_resize - Signal the main cui program loop to resize
90 void cui_resize(struct cui *cui)
92 pb_debug("%s: resizing\n", __func__);
97 * cui_on_exit - A generic main menu exit callback.
100 void cui_on_exit(struct pmenu *menu)
102 cui_abort(cui_from_pmenu(menu));
106 * cui_run_cmd - A generic cb to run the supplied command.
109 int cui_run_cmd(struct pmenu_item *item)
112 struct cui *cui = cui_from_item(item);
113 const char **cmd_argv = item->data;
115 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
119 result = process_run_simple_argv(item, cmd_argv);
122 redrawwin(cui->current->main_ncw);
125 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
126 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
133 * cui_boot - A generic cb to run kexec.
136 static int cui_boot(struct pmenu_item *item)
139 struct cui *cui = cui_from_item(item);
140 struct cui_opt_data *cod = cod_from_item(item);
142 assert(cui->current == &cui->main->scr);
144 pb_debug("%s: %s\n", __func__, cod->name);
146 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
150 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
153 redrawwin(cui->current->main_ncw);
156 nc_scr_status_printf(cui->current,
157 "Failed: boot %s", cod->bd->image);
164 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
167 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
168 enum boot_editor_result boot_editor_result,
169 struct pb_boot_data *bd)
171 struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
172 struct pmenu_item *item = boot_editor->data;
173 struct cui_opt_data *cod;
175 if (boot_editor_result != boot_editor_update) {
176 cui_set_current(cui, &cui->main->scr);
177 talloc_free(boot_editor);
183 /* Is this was a new item, we'll need to update the menu */
185 struct pmenu *menu = boot_editor->original_pmenu;
188 /* Detach the items array. */
189 set_menu_items(menu->ncm, NULL);
191 /* Insert new item at insert_pt. */
192 insert_pt = pmenu_grow(menu, 1);
193 item = pmenu_item_alloc(menu);
194 item->on_edit = cui_item_edit;
195 item->on_execute = cui_boot;
196 item->data = cod = talloc_zero(item, struct cui_opt_data);
198 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
199 pmenu_item_setup(menu, item, insert_pt,
200 talloc_strdup(item, cod->name));
202 /* Re-attach the items array. */
203 set_menu_items(menu->ncm, menu->items);
204 menu->scr.post(&menu->scr);
209 cod->bd = talloc_steal(cod, bd);
211 /* FIXME: need to make item visible somehow */
212 set_current_item(item->pmenu->ncm, item->nci);
213 cui_set_current(cui, &cui->main->scr);
214 talloc_free(boot_editor);
217 void cui_item_edit(struct pmenu_item *item)
219 struct cui *cui = cui_from_item(item);
220 struct cui_opt_data *cod = cod_from_item(item);
221 struct boot_editor *boot_editor;
223 boot_editor = boot_editor_init(item->pmenu, cod->bd,
224 cui_boot_editor_on_exit);
225 boot_editor->data = item;
226 cui_set_current(cui, &boot_editor->scr);
229 void cui_item_new(struct pmenu *menu)
231 struct cui *cui = cui_from_pmenu(menu);
232 struct boot_editor *boot_editor;
234 boot_editor = boot_editor_init(menu, NULL,
235 cui_boot_editor_on_exit);
236 cui_set_current(cui, &boot_editor->scr);
240 * cui_set_current - Set the currently active screen and redraw it.
243 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
247 DBGS("%p -> %p\n", cui->current, scr);
249 assert(cui->current != scr);
255 cui->current->post(cui->current);
260 static bool process_global_keys(struct cui *cui, int key)
264 if (cui->current && cui->current->main_ncw) {
265 redrawwin(cui->current->main_ncw);
266 wrefresh(cui->current->main_ncw);
274 * cui_process_key - Process input on stdin.
277 static int cui_process_key(void *arg)
279 struct cui *cui = cui_from_arg(arg);
281 assert(cui->current);
284 discover_client_cancel_default(cui->client);
285 cui->has_input = true;
293 if (process_global_keys(cui, c))
296 cui->current->process_key(cui->current, c);
303 * cui_process_js - Process joystick events.
306 static int cui_process_js(void *arg)
308 struct cui *cui = cui_from_arg(arg);
311 c = pjs_process_event(cui->pjs);
315 cui_process_key(arg);
322 * cui_handle_resize - Handle the term resize.
325 static void cui_handle_resize(struct cui *cui)
329 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
330 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
334 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
336 wclear(cui->current->main_ncw);
337 resize_term(ws.ws_row, ws.ws_col);
338 cui->current->resize(cui->current);
340 /* For some reason this makes ncurses redraw the screen */
342 redrawwin(cui->current->main_ncw);
343 wrefresh(cui->current->main_ncw);
347 * cui_device_add - Client device_add callback.
349 * Creates menu_items for all the device boot_options and inserts those
350 * menu_items into the main menu. Redraws the main menu if it is active.
353 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
356 struct cui *cui = cui_from_arg(arg);
357 struct cui_opt_data *cod;
358 unsigned int insert_pt;
359 struct pmenu_item *i;
363 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
365 selected = current_item(cui->main->ncm);
367 if (cui->current == &cui->main->scr)
368 cui->current->unpost(cui->current);
370 /* This disconnects items array from menu. */
372 result = set_menu_items(cui->main->ncm, NULL);
375 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
377 /* Insert new items at insert_pt. */
378 insert_pt = pmenu_grow(cui->main, 1);
380 /* Save the item in opt->ui_info for cui_device_remove() */
382 opt->ui_info = i = pmenu_item_alloc(cui->main);
384 i->on_edit = cui_item_edit;
385 i->on_execute = cui_boot;
386 i->data = cod = talloc(i, struct cui_opt_data);
390 cod->opt_hash = pb_opt_hash(dev, opt);
391 cod->name = opt->name;
392 cod->bd = talloc(i, struct pb_boot_data);
394 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
395 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
396 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
397 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
399 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
401 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
402 pb_log(" image '%s'\n", cod->bd->image);
403 pb_log(" initrd '%s'\n", cod->bd->initrd);
404 pb_log(" args '%s'\n", cod->bd->args);
406 /* Re-attach the items array. */
407 result = set_menu_items(cui->main->ncm, cui->main->items);
410 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
413 pb_log("%s\n", __func__);
414 pmenu_dump_items(cui->main->items,
415 item_count(cui->main->ncm) + 1);
418 /* FIXME: need to make item visible somehow */
419 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
420 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
421 set_current_item(cui->main->ncm, selected);
423 if (cui->current == &cui->main->scr)
424 cui->current->post(cui->current);
430 * cui_device_remove - Client device remove callback.
432 * Removes all the menu_items for the device from the main menu and redraws the
433 * main menu if it is active.
436 static void cui_device_remove(struct device *dev, void *arg)
438 struct cui *cui = cui_from_arg(arg);
440 struct boot_option *opt;
442 pb_log("%s: %p %s\n", __func__, dev, dev->id);
444 if (cui->current == &cui->main->scr)
445 cui->current->unpost(cui->current);
447 /* This disconnects items array from menu. */
449 result = set_menu_items(cui->main->ncm, NULL);
452 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
454 list_for_each_entry(&dev->boot_options, opt, list) {
455 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
457 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
458 pmenu_remove(cui->main, i);
461 /* Re-attach the items array. */
463 result = set_menu_items(cui->main->ncm, cui->main->items);
466 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
469 pb_log("%s\n", __func__);
470 pmenu_dump_items(cui->main->items,
471 item_count(cui->main->ncm) + 1);
474 if (cui->current == &cui->main->scr)
475 cui->current->post(cui->current);
478 static void cui_update_status(struct boot_status *status, void *arg)
480 struct cui *cui = cui_from_arg(arg);
482 nc_scr_status_printf(cui->current,
484 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
489 static void cui_update_mm_title(struct cui *cui)
491 struct nc_frame *frame = &cui->main->scr.frame;
493 talloc_free(frame->rtitle);
495 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
496 if (cui->sysinfo->identifier)
497 frame->rtitle = talloc_asprintf_append(frame->rtitle,
498 " %s", cui->sysinfo->identifier);
500 if (cui->current == &cui->main->scr)
501 cui->current->post(cui->current);
504 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
506 struct cui *cui = cui_from_arg(arg);
507 cui->sysinfo = talloc_steal(cui, sysinfo);
508 cui_update_mm_title(cui);
511 static struct discover_client_ops cui_client_ops = {
513 .boot_option_add = cui_boot_option_add,
514 .device_remove = cui_device_remove,
515 .update_status = cui_update_status,
516 .update_sysinfo = cui_update_sysinfo,
520 * cui_init - Setup the cui instance.
521 * @platform_info: A value for the struct cui platform_info member.
523 * Returns a pointer to a struct cui on success, or NULL on error.
525 * Allocates the cui instance, sets up the client and stdin waiters, and
526 * sets up the ncurses menu screen.
529 struct cui *cui_init(void* platform_info,
530 int (*js_map)(const struct js_event *e), int start_deamon)
535 cui = talloc_zero(NULL, struct cui);
538 pb_log("%s: alloc cui failed.\n", __func__);
539 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
543 cui->c_sig = pb_cui_sig;
544 cui->platform_info = platform_info;
545 cui->waitset = waitset_create(cui);
547 process_init(cui, cui->waitset, false);
549 setlocale(LC_ALL, "");
551 /* Loop here for scripts that just started the server. */
554 for (i = start_deamon ? 2 : 10; i; i--) {
555 cui->client = discover_client_init(cui->waitset,
556 &cui_client_ops, cui);
557 if (cui->client || !i)
559 pb_log("%s: waiting for server %d\n", __func__, i);
563 if (!cui->client && start_deamon) {
568 result = pb_start_daemon(cui);
573 pb_log("%s: discover_client_init failed.\n", __func__);
574 fprintf(stderr, "%s: error: discover_client_init failed.\n",
576 fprintf(stderr, "could not start pb-discover, the petitboot "
578 goto fail_client_init;
582 pb_log("%s: discover_client_init failed.\n", __func__);
583 fprintf(stderr, "%s: error: discover_client_init failed.\n",
585 fprintf(stderr, "check that pb-discover, "
586 "the petitboot daemon is running.\n");
587 goto fail_client_init;
593 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
594 cui_process_key, cui);
598 cui->pjs = pjs_init(cui, js_map);
601 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
602 WAIT_IN, cui_process_js, cui);
614 * cui_run - The main cui program loop.
615 * @cui: The cui instance.
616 * @main: The menu to use as the main menu.
618 * Runs the cui engine. Does not return until indicated to do so by some
619 * user action, or an error occurs. Frees the cui object on return.
620 * Returns 0 on success (return to shell), -1 on error (should restart).
623 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
628 cui->current = &cui->main->scr;
629 cui->default_item = default_item;
631 cui->current->post(cui->current);
634 int result = waiter_poll(cui->waitset);
637 pb_log("%s: poll: %s\n", __func__, strerror(errno));
644 while (cui->resize) {
646 cui_handle_resize(cui);
652 return cui->abort ? 0 : -1;