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
25 #include <sys/ioctl.h>
28 #include "talloc/talloc.h"
29 #include "waiter/waiter.h"
30 #include "ui/common/discover-client.h"
33 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
39 * cui_abort - Signal the main cui program loop to exit.
41 * Sets cui.abort, which causes the cui_run() routine to return.
44 void cui_abort(struct cui *cui)
46 pb_log("%s: exiting\n", __func__);
51 * cui_resize - Signal the main cui program loop to resize
56 void cui_resize(struct cui *cui)
58 pb_log("%s: resizing\n", __func__);
63 * cui_make_item_name - Format the menu item name srting.
65 * Returns a talloc string.
68 static char *cui_make_item_name(struct pmenu_item *i, struct cui_opt_data *cod)
75 name = talloc_asprintf(i, "%s:", cod->name);
78 name = talloc_asprintf_append(name, " %s", cod->bd->image);
81 name = talloc_asprintf_append(name, " initrd=%s",
85 name = talloc_asprintf_append(name, " %s", cod->bd->args);
92 * cui_on_exit - A generic main menu ESC callback.
95 void cui_on_exit(struct pmenu *menu)
97 cui_abort(cui_from_pmenu(menu));
101 * cui_run_cmd - A generic cb to run the supplied command.
104 int cui_run_cmd(struct pmenu_item *item)
107 struct cui *cui = cui_from_item(item);
108 const char *const *cmd_argv = item->data;
110 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
114 result = pb_run_cmd(cmd_argv, 1, 0);
117 redrawwin(cui->current->main_ncw);
120 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
121 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
128 * cui_boot - A generic cb to run kexec.
131 static int cui_boot(struct pmenu_item *item)
134 struct cui *cui = cui_from_item(item);
135 struct cui_opt_data *cod = cod_from_item(item);
137 assert(cui->current == &cui->main->scr);
138 assert(cui->on_boot);
140 pb_log("%s: %s\n", __func__, cod->name);
141 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
145 result = cui->on_boot(cui, cod);
148 redrawwin(cui->current->main_ncw);
152 mvaddstr(1, 0, "system is going down now...");
154 sleep(cui->dry_run ? 1 : 60);
157 pb_log("%s: failed: %s\n", __func__, cod->bd->image);
158 nc_scr_status_printf(cui->current, "Failed: kexec %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, enum boot_editor_result boot_editor_result,
168 struct pb_boot_data *bd)
170 struct cui *cui = cui_from_arg(boot_editor->scr.ui_ctx);
172 if (boot_editor_result == boot_editor_update) {
173 struct pmenu_item *i = pmenu_find_selected(cui->main);
174 struct cui_opt_data *cod = cod_from_item(i);
180 talloc_free(cod->bd);
183 name = cui_make_item_name(i, cod);
184 pmenu_item_replace(i, name);
186 /* FIXME: need to make item visible somehow */
187 set_current_item(cui->main->ncm, i->nci);
189 pb_log("%s: updating opt '%s'\n", __func__, cod->name);
190 pb_log(" image '%s'\n", cod->bd->image);
191 pb_log(" initrd '%s'\n", cod->bd->initrd);
192 pb_log(" args '%s'\n", cod->bd->args);
195 cui_set_current(cui, &cui->main->scr);
197 talloc_free(boot_editor);
200 int cui_boot_editor_run(struct pmenu_item *item)
202 struct cui *cui = cui_from_item(item);
203 struct cui_opt_data *cod = cod_from_item(item);
204 struct boot_editor *boot_editor;
206 boot_editor = boot_editor_init(cui, cod->bd, cui_boot_editor_on_exit);
207 cui_set_current(cui, &boot_editor->scr);
213 * cui_set_current - Set the currently active screen and redraw it.
216 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
220 DBGS("%p -> %p\n", cui->current, scr);
222 assert(cui->current != scr);
228 cui->current->post(cui->current);
234 * cui_process_key - Process input on stdin.
237 static int cui_process_key(void *arg)
239 struct cui *cui = cui_from_arg(arg);
241 assert(cui->current);
243 ui_timer_disable(&cui->timer);
244 cui->current->process_key(cui->current);
250 * cui_process_js - Process joystick events.
253 static int cui_process_js(void *arg)
255 struct cui *cui = cui_from_arg(arg);
258 c = pjs_process_event(cui->pjs);
262 cui_process_key(arg);
269 * cui_handle_timeout - Handle the timeout.
272 static void cui_handle_timeout(struct ui_timer *timer)
274 struct cui *cui = cui_from_timer(timer);
275 struct pmenu_item *i = pmenu_find_selected(cui->main);
279 struct cui_opt_data *cod = cod_from_item(i);
280 assert(cod && (cod->opt_hash == cui->default_item));
287 * cui_handle_resize - Handle the term resize.
290 static void cui_handle_resize(struct cui *cui)
294 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
295 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
299 pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
301 wclear(cui->current->main_ncw);
302 resize_term(ws.ws_row, ws.ws_col);
303 cui->current->resize(cui->current);
305 /* For some reason this makes ncurses redraw the screen */
307 redrawwin(cui->current->main_ncw);
308 wrefresh(cui->current->main_ncw);
312 * cui_on_open - Open new item callback.
315 void cui_on_open(struct pmenu *menu)
317 unsigned int insert_pt;
318 struct pmenu_item *i;
319 struct cui_opt_data *cod;
321 menu->scr.unpost(&menu->scr);
323 /* This disconnects items array from menu. */
325 set_menu_items(menu->ncm, NULL);
327 /* Insert new items at insert_pt. */
329 insert_pt = pmenu_grow(menu, 1);
330 i = pmenu_item_alloc(menu);
332 i->on_edit = cui_boot_editor_run;
333 i->on_execute = cui_boot;
334 i->data = cod = talloc_zero(i, struct cui_opt_data);
336 cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
337 cod->bd = talloc_zero(i, struct pb_boot_data);
339 pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
341 /* Re-attach the items array. */
343 set_menu_items(menu->ncm, menu->items);
345 menu->scr.post(&menu->scr);
346 set_current_item(menu->ncm, i->nci);
352 * cui_device_add - Client device_add callback.
354 * Creates menu_items for all the device boot_options and inserts those
355 * menu_items into the main menu. Redraws the main menu if it is active.
358 static int cui_device_add(struct device *dev, void *arg)
360 struct cui *cui = cui_from_arg(arg);
362 struct boot_option *opt;
363 unsigned int o_count; /* device opts */
364 unsigned int insert_pt;
367 pb_log("%s: %p %s\n", __func__, dev, dev->id);
369 selected = current_item(cui->main->ncm);
371 if (cui->current == &cui->main->scr)
372 cui->current->unpost(cui->current);
374 /* This disconnects items array from menu. */
376 result = set_menu_items(cui->main->ncm, NULL);
379 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
382 list_for_each_entry(&dev->boot_options, opt, list)
385 /* Insert new items at insert_pt. */
387 insert_pt = pmenu_grow(cui->main, o_count);
389 list_for_each_entry(&dev->boot_options, opt, list) {
390 struct pmenu_item *i;
391 struct cui_opt_data *cod;
394 /* Save the item in opt->ui_info for cui_device_remove() */
396 opt->ui_info = i = pmenu_item_alloc(cui->main);
398 i->on_edit = cui_boot_editor_run;
399 i->on_execute = cui_boot;
400 i->data = cod = talloc(i, struct cui_opt_data);
404 cod->opt_hash = pb_opt_hash(dev, opt);
405 cod->name = opt->name;
406 cod->bd = talloc(i, struct pb_boot_data);
408 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
409 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
410 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
412 name = cui_make_item_name(i, cod);
413 pmenu_item_setup(cui->main, i, insert_pt, 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 /* If this is the default_item select it and start timer. */
424 if (cod->opt_hash == cui->default_item) {
426 ui_timer_kick(&cui->timer);
430 /* Re-attach the items array. */
432 result = set_menu_items(cui->main->ncm, cui->main->items);
435 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
438 pb_log("%s\n", __func__);
439 pmenu_dump_items(cui->main->items,
440 item_count(cui->main->ncm) + 1);
443 /* FIXME: need to make item visible somehow */
444 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
445 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
446 set_current_item(cui->main->ncm, selected);
448 if (cui->current == &cui->main->scr)
449 cui->current->post(cui->current);
455 * cui_device_remove - Client device remove callback.
457 * Removes all the menu_items for the device from the main menu and redraws the
458 * main menu if it is active.
461 static void cui_device_remove(struct device *dev, void *arg)
463 struct cui *cui = cui_from_arg(arg);
465 struct boot_option *opt;
467 pb_log("%s: %p %s\n", __func__, dev, dev->id);
469 if (cui->current == &cui->main->scr)
470 cui->current->unpost(cui->current);
472 /* This disconnects items array from menu. */
474 result = set_menu_items(cui->main->ncm, NULL);
477 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
479 list_for_each_entry(&dev->boot_options, opt, list) {
480 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
481 struct cui_opt_data *cod = cod_from_item(i);
483 assert(pb_protocol_device_cmp(dev, cod->dev));
484 pmenu_remove(cui->main, i);
486 /* If this is the default_item disable timer. */
488 if (cod->opt_hash == cui->default_item)
489 ui_timer_disable(&cui->timer);
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 cui->current->post(cui->current);
509 static struct discover_client_ops cui_client_ops = {
510 .device_add = cui_device_add,
511 .device_remove = cui_device_remove,
515 * cui_init - Setup the cui instance.
516 * @platform_info: A value for the struct cui platform_info member.
518 * Returns a pointer to a struct cui on success, or NULL on error.
520 * Allocates the cui instance, sets up the client and stdin waiters, and
521 * sets up the ncurses menu screen.
524 struct cui *cui_init(void* platform_info,
525 int (*on_boot)(struct cui *, struct cui_opt_data *),
526 int (*js_map)(const struct js_event *e), int start_deamon, int dry_run)
531 cui = talloc_zero(NULL, struct cui);
534 pb_log("%s: alloc cui failed.\n", __func__);
535 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
539 cui->c_sig = pb_cui_sig;
540 cui->platform_info = platform_info;
541 cui->on_boot = on_boot;
542 cui->timer.handle_timeout = cui_handle_timeout;
543 cui->dry_run = dry_run;
544 cui->waitset = waitset_create(cui);
546 /* Loop here for scripts that just started the server. */
549 for (i = start_deamon ? 2 : 10; i; i--) {
550 cui->client = discover_client_init(cui->waitset,
551 &cui_client_ops, cui);
552 if (cui->client || !i)
554 pb_log("%s: waiting for server %d\n", __func__, i);
558 if (!cui->client && start_deamon) {
563 result = pb_start_daemon();
568 pb_log("%s: discover_client_init failed.\n", __func__);
569 fprintf(stderr, "%s: error: discover_client_init failed.\n",
571 fprintf(stderr, "could not start pb-discover, the petitboot "
573 goto fail_client_init;
577 pb_log("%s: discover_client_init failed.\n", __func__);
578 fprintf(stderr, "%s: error: discover_client_init failed.\n",
580 fprintf(stderr, "check that pb-discover, "
581 "the petitboot daemon is running.\n");
582 goto fail_client_init;
588 waiter_register(cui->waitset, STDIN_FILENO, WAIT_IN,
589 cui_process_key, cui);
593 cui->pjs = pjs_init(cui, js_map);
596 waiter_register(cui->waitset, pjs_get_fd(cui->pjs),
597 WAIT_IN, cui_process_js, cui);
609 * cui_run - The main cui program loop.
610 * @cui: The cui instance.
611 * @main: The menu to use as the main menu.
613 * Runs the cui engine. Does not return until indicated to do so by some
614 * user action, or an error occurs. Frees the cui object on return.
615 * Returns 0 on success (return to shell), -1 on error (should restart).
618 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
623 cui->current = &cui->main->scr;
624 cui->default_item = default_item;
626 cui->current->post(cui->current);
629 int result = waiter_poll(cui->waitset);
631 if (result < 0 && errno != EINTR) {
632 pb_log("%s: poll: %s\n", __func__, strerror(errno));
639 ui_timer_process_sig(&cui->timer);
641 while (cui->resize) {
643 cui_handle_resize(cui);
649 return cui->abort ? 0 : -1;