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)
44 * cui_abort - Signal the main cui program loop to exit.
46 * Sets cui.abort, which causes the cui_run() routine to return.
49 void cui_abort(struct cui *cui)
51 pb_log("%s: exiting\n", __func__);
56 * cui_resize - Signal the main cui program loop to resize
61 void cui_resize(struct cui *cui)
63 pb_log("%s: resizing\n", __func__);
68 * cui_on_exit - A generic main menu ESC callback.
71 void cui_on_exit(struct pmenu *menu)
73 cui_abort(cui_from_pmenu(menu));
77 * cui_run_cmd - A generic cb to run the supplied command.
80 int cui_run_cmd(struct pmenu_item *item)
83 struct cui *cui = cui_from_item(item);
84 const char **cmd_argv = item->data;
86 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
90 result = process_run_simple_argv(item, cmd_argv);
93 redrawwin(cui->current->main_ncw);
96 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
97 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
104 * cui_boot - A generic cb to run kexec.
107 static int cui_boot(struct pmenu_item *item)
110 struct cui *cui = cui_from_item(item);
111 struct cui_opt_data *cod = cod_from_item(item);
113 assert(cui->current == &cui->main->scr);
115 pb_log("%s: %s\n", __func__, cod->name);
117 pb_log("%s: missing opt?\n", __func__);
121 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
125 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
128 redrawwin(cui->current->main_ncw);
131 nc_scr_status_printf(cui->current,
132 "Failed: boot %s", cod->bd->image);
139 * cui_boot_editor_on_exit - The boot_editor on_exit callback.
142 static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
143 enum boot_editor_result boot_editor_result,
144 struct pb_boot_data *bd)
146 struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
147 struct pmenu_item *item = boot_editor->data;
148 struct cui_opt_data *cod;
150 if (boot_editor_result != boot_editor_update) {
151 cui_set_current(cui, &cui->main->scr);
152 talloc_free(boot_editor);
158 /* Is this was a new item, we'll need to update the menu */
160 struct pmenu *menu = boot_editor->original_pmenu;
163 /* Detach the items array. */
164 set_menu_items(menu->ncm, NULL);
166 /* Insert new item at insert_pt. */
167 insert_pt = pmenu_grow(menu, 1);
168 item = pmenu_item_alloc(menu);
169 item->on_edit = cui_item_edit;
170 item->on_execute = cui_boot;
171 item->data = cod = talloc_zero(item, struct cui_opt_data);
173 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
174 pmenu_item_setup(menu, item, insert_pt,
175 talloc_strdup(item, cod->name));
177 /* Re-attach the items array. */
178 set_menu_items(menu->ncm, menu->items);
179 menu->scr.post(&menu->scr);
184 cod->bd = talloc_steal(cod, bd);
186 /* FIXME: need to make item visible somehow */
187 set_current_item(item->pmenu->ncm, item->nci);
188 cui_set_current(cui, &cui->main->scr);
189 talloc_free(boot_editor);
192 void cui_item_edit(struct pmenu_item *item)
194 struct cui *cui = cui_from_item(item);
195 struct cui_opt_data *cod = cod_from_item(item);
196 struct boot_editor *boot_editor;
198 boot_editor = boot_editor_init(item->pmenu, cod->bd,
199 cui_boot_editor_on_exit);
200 boot_editor->data = item;
201 cui_set_current(cui, &boot_editor->scr);
204 void cui_item_new(struct pmenu *menu)
206 struct cui *cui = cui_from_pmenu(menu);
207 struct boot_editor *boot_editor;
209 boot_editor = boot_editor_init(menu, NULL,
210 cui_boot_editor_on_exit);
211 cui_set_current(cui, &boot_editor->scr);
215 * cui_set_current - Set the currently active screen and redraw it.
218 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
222 DBGS("%p -> %p\n", cui->current, scr);
224 assert(cui->current != scr);
230 cui->current->post(cui->current);
235 static bool process_global_keys(struct cui *cui, int key)
239 if (cui->current && cui->current->main_ncw) {
240 redrawwin(cui->current->main_ncw);
241 wrefresh(cui->current->main_ncw);
249 * cui_process_key - Process input on stdin.
252 static int cui_process_key(void *arg)
254 struct cui *cui = cui_from_arg(arg);
256 assert(cui->current);
259 discover_client_cancel_default(cui->client);
260 cui->has_input = true;
268 if (process_global_keys(cui, c))
271 cui->current->process_key(cui->current, c);
278 * cui_process_js - Process joystick events.
281 static int cui_process_js(void *arg)
283 struct cui *cui = cui_from_arg(arg);
286 c = pjs_process_event(cui->pjs);
290 cui_process_key(arg);
297 * cui_handle_resize - Handle the term resize.
300 static void cui_handle_resize(struct cui *cui)
304 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
305 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
309 pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
311 wclear(cui->current->main_ncw);
312 resize_term(ws.ws_row, ws.ws_col);
313 cui->current->resize(cui->current);
315 /* For some reason this makes ncurses redraw the screen */
317 redrawwin(cui->current->main_ncw);
318 wrefresh(cui->current->main_ncw);
322 * cui_device_add - Client device_add callback.
324 * Creates menu_items for all the device boot_options and inserts those
325 * menu_items into the main menu. Redraws the main menu if it is active.
328 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
331 struct cui *cui = cui_from_arg(arg);
332 struct cui_opt_data *cod;
333 unsigned int insert_pt;
334 struct pmenu_item *i;
338 pb_log("%s: %p %s\n", __func__, opt, opt->id);
340 selected = current_item(cui->main->ncm);
342 if (cui->current == &cui->main->scr)
343 cui->current->unpost(cui->current);
345 /* This disconnects items array from menu. */
347 result = set_menu_items(cui->main->ncm, NULL);
350 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
352 /* Insert new items at insert_pt. */
353 insert_pt = pmenu_grow(cui->main, 1);
355 /* Save the item in opt->ui_info for cui_device_remove() */
357 opt->ui_info = i = pmenu_item_alloc(cui->main);
359 i->on_edit = cui_item_edit;
360 i->on_execute = cui_boot;
361 i->data = cod = talloc(i, struct cui_opt_data);
365 cod->opt_hash = pb_opt_hash(dev, opt);
366 cod->name = opt->name;
367 cod->bd = talloc(i, struct pb_boot_data);
369 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
370 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
371 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
372 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
374 pmenu_item_setup(cui->main, i, insert_pt, cod->name);
376 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
377 pb_log(" image '%s'\n", cod->bd->image);
378 pb_log(" initrd '%s'\n", cod->bd->initrd);
379 pb_log(" args '%s'\n", cod->bd->args);
381 /* Re-attach the items array. */
382 result = set_menu_items(cui->main->ncm, cui->main->items);
385 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
388 pb_log("%s\n", __func__);
389 pmenu_dump_items(cui->main->items,
390 item_count(cui->main->ncm) + 1);
393 /* FIXME: need to make item visible somehow */
394 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
395 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
396 set_current_item(cui->main->ncm, selected);
398 if (cui->current == &cui->main->scr)
399 cui->current->post(cui->current);
405 * cui_device_remove - Client device remove callback.
407 * Removes all the menu_items for the device from the main menu and redraws the
408 * main menu if it is active.
411 static void cui_device_remove(struct device *dev, void *arg)
413 struct cui *cui = cui_from_arg(arg);
415 struct boot_option *opt;
417 pb_log("%s: %p %s\n", __func__, dev, dev->id);
419 if (cui->current == &cui->main->scr)
420 cui->current->unpost(cui->current);
422 /* This disconnects items array from menu. */
424 result = set_menu_items(cui->main->ncm, NULL);
427 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
429 list_for_each_entry(&dev->boot_options, opt, list) {
430 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
432 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
433 pmenu_remove(cui->main, i);
436 /* Re-attach the items array. */
438 result = set_menu_items(cui->main->ncm, cui->main->items);
441 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
444 pb_log("%s\n", __func__);
445 pmenu_dump_items(cui->main->items,
446 item_count(cui->main->ncm) + 1);
449 if (cui->current == &cui->main->scr)
450 cui->current->post(cui->current);
453 static void cui_update_status(struct boot_status *status, void *arg)
455 struct cui *cui = cui_from_arg(arg);
457 nc_scr_status_printf(cui->current,
459 status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
464 static void cui_update_mm_title(struct cui *cui)
466 struct nc_frame *frame = &cui->main->scr.frame;
468 talloc_free(frame->rtitle);
470 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
471 if (cui->sysinfo->identifier)
472 frame->rtitle = talloc_asprintf_append(frame->rtitle,
473 " %s", cui->sysinfo->identifier);
475 if (cui->current == &cui->main->scr)
476 cui->current->post(cui->current);
479 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
481 struct cui *cui = cui_from_arg(arg);
482 cui->sysinfo = talloc_steal(cui, sysinfo);
483 cui_update_mm_title(cui);
486 static struct discover_client_ops cui_client_ops = {
488 .boot_option_add = cui_boot_option_add,
489 .device_remove = cui_device_remove,
490 .update_status = cui_update_status,
491 .update_sysinfo = cui_update_sysinfo,
495 * cui_init - Setup the cui instance.
496 * @platform_info: A value for the struct cui platform_info member.
498 * Returns a pointer to a struct cui on success, or NULL on error.
500 * Allocates the cui instance, sets up the client and stdin waiters, and
501 * sets up the ncurses menu screen.
504 struct cui *cui_init(void* platform_info,
505 int (*js_map)(const struct js_event *e), int start_deamon)
510 cui = talloc_zero(NULL, struct cui);
513 pb_log("%s: alloc cui failed.\n", __func__);
514 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
518 cui->c_sig = pb_cui_sig;
519 cui->platform_info = platform_info;
520 cui->waitset = waitset_create(cui);
522 process_init(cui, cui->waitset, false);
524 setlocale(LC_ALL, "");
526 /* Loop here for scripts that just started the server. */
529 for (i = start_deamon ? 2 : 10; i; i--) {
530 cui->client = discover_client_init(cui->waitset,
531 &cui_client_ops, cui);
532 if (cui->client || !i)
534 pb_log("%s: waiting for server %d\n", __func__, i);
538 if (!cui->client && start_deamon) {
543 result = pb_start_daemon(cui);
548 pb_log("%s: discover_client_init failed.\n", __func__);
549 fprintf(stderr, "%s: error: discover_client_init failed.\n",
551 fprintf(stderr, "could not start pb-discover, the petitboot "
553 goto fail_client_init;
557 pb_log("%s: discover_client_init failed.\n", __func__);
558 fprintf(stderr, "%s: error: discover_client_init failed.\n",
560 fprintf(stderr, "check that pb-discover, "
561 "the petitboot daemon is running.\n");
562 goto fail_client_init;
568 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
569 cui_process_key, cui);
573 cui->pjs = pjs_init(cui, js_map);
576 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
577 WAIT_IN, cui_process_js, cui);
589 * cui_run - The main cui program loop.
590 * @cui: The cui instance.
591 * @main: The menu to use as the main menu.
593 * Runs the cui engine. Does not return until indicated to do so by some
594 * user action, or an error occurs. Frees the cui object on return.
595 * Returns 0 on success (return to shell), -1 on error (should restart).
598 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
603 cui->current = &cui->main->scr;
604 cui->default_item = default_item;
606 cui->current->post(cui->current);
609 int result = waiter_poll(cui->waitset);
612 pb_log("%s: poll: %s\n", __func__, strerror(errno));
619 while (cui->resize) {
621 cui_handle_resize(cui);
627 return cui->abort ? 0 : -1;