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
19 #if defined(HAVE_CONFIG_H)
27 #include <sys/ioctl.h>
28 #include <sys/reboot.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 "i18n/i18n.h"
36 #include "ui/common/discover-client.h"
37 #include "ui/common/ui-system.h"
39 #include "nc-boot-editor.h"
40 #include "nc-config.h"
41 #include "nc-add-url.h"
42 #include "nc-sysinfo.h"
44 #include "nc-helpscreen.h"
45 #include "nc-statuslog.h"
46 #include "nc-subset.h"
48 extern const struct help_text main_menu_help_text;
50 static bool cui_detached = false;
52 static struct pmenu *main_menu_init(struct cui *cui);
54 static bool lockdown_active(void)
56 bool lockdown = false;
57 if (access(LOCKDOWN_FILE, F_OK) != -1)
62 static void cui_start(void)
64 initscr(); /* Initialize ncurses. */
65 cbreak(); /* Disable line buffering. */
66 noecho(); /* Disable getch() echo. */
67 keypad(stdscr, TRUE); /* Enable num keypad keys. */
68 nonl(); /* Disable new-line translation. */
69 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
70 curs_set(0); /* Make cursor invisible */
71 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
73 /* We may be operating with an incorrect $TERM type; in this case
74 * the keymappings will be slightly broken. We want at least
75 * backspace to work though, so we'll define both DEL and ^H to
77 define_key("\x7f", KEY_BACKSPACE);
78 define_key("\x08", KEY_BACKSPACE);
80 /* we need backtab too, for form navigation. vt220 doesn't include
81 * this (kcbt), but we don't want to require a full linux/xterm termcap
83 define_key("\x1b[Z", KEY_BTAB);
85 /* We'll define a few other keys too since they're commonly
86 * used for navigation but the escape character will cause
87 * Petitboot to exit if they're left undefined */
88 define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
89 define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
90 define_key("\x1b\x5b\x31\x7e", KEY_HOME);
91 define_key("\x1b\x5b\x34\x7e", KEY_END);
92 define_key("\x1b\x4f\x48", KEY_HOME);
93 define_key("\x1b\x4f\x46", KEY_END);
94 define_key("OH", KEY_HOME);
95 define_key("OF", KEY_END);
96 define_key("\x1b\x5b\x41", KEY_UP);
97 define_key("\x1b\x5b\x42", KEY_DOWN);
98 define_key("\x1b\x5b\x33\x7e", KEY_DC);
100 while (getch() != ERR) /* flush stdin */
104 static void cui_atexit(void)
113 bool lockdown = lockdown_active();
122 * cui_abort - Signal the main cui program loop to exit.
124 * Sets cui.abort, which causes the cui_run() routine to return.
127 void cui_abort(struct cui *cui)
129 pb_log("%s: exiting\n", __func__);
134 * cui_resize - Signal the main cui program loop to resize
136 * Called at SIGWINCH.
139 void cui_resize(struct cui *cui)
141 pb_debug("%s: resizing\n", __func__);
146 * cui_on_exit - A generic main menu exit callback.
149 void cui_on_exit(struct pmenu *menu)
151 cui_abort(cui_from_pmenu(menu));
155 * cui_run_cmd - A generic cb to run the supplied command.
158 int cui_run_cmd(struct pmenu_item *item)
161 struct cui *cui = cui_from_item(item);
162 const char **cmd_argv = item->data;
164 nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
168 result = process_run_simple_argv(item, cmd_argv);
171 redrawwin(cui->current->main_ncw);
174 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
175 nc_scr_status_printf(cui->current, _("Failed: %s"),
183 * cui_boot - A generic cb to run kexec.
186 static int cui_boot(struct pmenu_item *item)
189 struct cui *cui = cui_from_item(item);
190 struct cui_opt_data *cod = cod_from_item(item);
192 assert(cui->current == &cui->main->scr);
194 pb_debug("%s: %s\n", __func__, cod->name);
196 nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
198 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
201 nc_scr_status_printf(cui->current,
202 _("Failed: boot %s"), cod->bd->image);
208 static void cui_boot_editor_on_exit(struct cui *cui,
209 struct pmenu_item *item,
210 struct pb_boot_data *bd)
212 struct pmenu *menu = cui->main;
213 struct cui_opt_data *cod;
214 int idx, top, rows, cols;
215 static int user_idx = 0;
217 /* Was the edit cancelled? */
219 cui_set_current(cui, &cui->main->scr);
220 talloc_free(cui->boot_editor);
221 cui->boot_editor = NULL;
225 /* Is this was a new item, we'll need to update the menu */
229 cod = talloc_zero(NULL, struct cui_opt_data);
230 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
232 item = pmenu_item_create(menu, cod->name);
238 item->on_edit = cui_item_edit;
239 item->on_execute = cui_boot;
242 talloc_steal(item, cod);
244 /* Detach the items array. */
245 set_menu_items(menu->ncm, NULL);
247 /* Insert new item at insert_pt. */
248 insert_pt = pmenu_grow(menu, 1);
249 pmenu_item_insert(menu, item, insert_pt);
251 /* Re-attach the items array. */
252 set_menu_items(menu->ncm, menu->items);
254 /* If our index is above the current top row, align
255 * us to the new top. Otherwise, align us to the new
257 menu_format(cui->main->ncm, &rows, &cols);
258 top = top_row(cui->main->ncm);
259 idx = item_index(item->nci);
264 top = idx < rows ? 0 : idx - rows + 1;
266 set_top_row(cui->main->ncm, top);
267 set_current_item(item->pmenu->ncm, item->nci);
269 nc_scr_post(&menu->scr);
274 cod->bd = talloc_steal(cod, bd);
277 cui_set_current(cui, &cui->main->scr);
278 talloc_free(cui->boot_editor);
279 cui->boot_editor = NULL;
282 void cui_item_edit(struct pmenu_item *item)
284 struct cui *cui = cui_from_item(item);
285 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
286 cui_boot_editor_on_exit);
287 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
290 void cui_item_new(struct pmenu *menu)
292 struct cui *cui = cui_from_pmenu(menu);
293 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
294 cui_boot_editor_on_exit);
295 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
298 static void cui_sysinfo_exit(struct cui *cui)
300 cui_set_current(cui, &cui->main->scr);
301 talloc_free(cui->sysinfo_screen);
302 cui->sysinfo_screen = NULL;
305 void cui_show_sysinfo(struct cui *cui)
307 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
309 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
312 static void cui_config_exit(struct cui *cui)
314 cui_set_current(cui, &cui->main->scr);
315 talloc_free(cui->config_screen);
316 cui->config_screen = NULL;
319 void cui_show_config(struct cui *cui)
321 cui->config_screen = config_screen_init(cui, cui->config,
322 cui->sysinfo, cui_config_exit);
323 cui_set_current(cui, config_screen_scr(cui->config_screen));
326 static void cui_lang_exit(struct cui *cui)
328 cui_set_current(cui, &cui->main->scr);
329 talloc_free(cui->lang_screen);
330 cui->lang_screen = NULL;
333 void cui_show_lang(struct cui *cui)
335 cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
336 cui_set_current(cui, lang_screen_scr(cui->lang_screen));
339 static void cui_statuslog_exit(struct cui *cui)
341 cui_set_current(cui, &cui->main->scr);
342 talloc_free(cui->statuslog_screen);
343 cui->statuslog_screen = NULL;
346 void cui_show_statuslog(struct cui *cui)
348 cui->statuslog_screen = statuslog_screen_init(cui, cui_statuslog_exit);
349 cui_set_current(cui, statuslog_screen_scr(cui->statuslog_screen));
352 static void cui_add_url_exit(struct cui *cui)
354 cui_set_current(cui, &cui->main->scr);
355 talloc_free(cui->add_url_screen);
356 cui->add_url_screen = NULL;
359 void cui_show_add_url(struct cui *cui)
361 cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
362 cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
365 static void cui_help_exit(struct cui *cui)
367 cui_set_current(cui, help_screen_return_scr(cui->help_screen));
368 talloc_free(cui->help_screen);
369 cui->help_screen = NULL;
372 void cui_show_help(struct cui *cui, const char *title,
373 const struct help_text *text)
378 if (cui->help_screen)
381 cui->help_screen = help_screen_init(cui, cui->current,
382 title, text, cui_help_exit);
384 if (cui->help_screen)
385 cui_set_current(cui, help_screen_scr(cui->help_screen));
388 static void cui_subset_exit(struct cui *cui)
390 cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
391 talloc_free(cui->subset_screen);
392 cui->subset_screen = NULL;
395 void cui_show_subset(struct cui *cui, const char *title,
401 if (cui->subset_screen)
404 cui->subset_screen = subset_screen_init(cui, cui->current,
405 title, arg, cui_subset_exit);
407 if (cui->subset_screen)
408 cui_set_current(cui, subset_screen_scr(cui->subset_screen));
412 * cui_set_current - Set the currently active screen and redraw it.
415 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
419 DBGS("%p -> %p\n", cui->current, scr);
421 assert(cui->current != scr);
428 nc_scr_post(cui->current);
433 static bool process_global_keys(struct cui *cui, int key)
437 if (cui->current && cui->current->main_ncw)
445 * cui_process_key - Process input on stdin.
448 static int cui_process_key(void *arg)
450 struct cui *cui = cui_from_arg(arg);
452 assert(cui->current);
457 pb_debug("%s: got key %d\n", __func__, c);
462 if (!cui->has_input) {
463 cui->has_input = true;
465 pb_log("UI input received (key = %d), aborting "
466 "default boot\n", c);
467 discover_client_cancel_default(cui->client);
469 pb_log("UI input received (key = %d), aborting "
470 "once server connects\n", c);
474 if (process_global_keys(cui, c))
477 cui->current->process_key(cui->current, c);
484 * cui_process_js - Process joystick events.
487 static int cui_process_js(void *arg)
489 struct cui *cui = cui_from_arg(arg);
492 c = pjs_process_event(cui->pjs);
496 cui_process_key(arg);
503 * cui_handle_resize - Handle the term resize.
506 static void cui_handle_resize(struct cui *cui)
510 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
511 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
515 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
517 wclear(cui->current->main_ncw);
518 resize_term(ws.ws_row, ws.ws_col);
519 cui->current->resize(cui->current);
521 /* For some reason this makes ncurses redraw the screen */
523 redrawwin(cui->current->main_ncw);
524 wrefresh(cui->current->main_ncw);
528 * cui_device_add - Client device_add callback.
530 * Creates menu_items for all the device boot_options and inserts those
531 * menu_items into the main menu. Redraws the main menu if it is active.
534 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
537 struct pmenu_item *i, *dev_hdr = NULL;
538 struct cui *cui = cui_from_arg(arg);
539 struct cui_opt_data *cod;
540 const char *tab = " ";
541 unsigned int insert_pt;
542 int result, rows, cols;
546 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
548 selected = current_item(cui->main->ncm);
549 menu_format(cui->main->ncm, &rows, &cols);
551 if (cui->current == &cui->main->scr)
552 nc_scr_unpost(cui->current);
554 /* Check if the boot device is new */
555 dev_hdr = pmenu_find_device(cui->main, dev, opt);
557 /* All actual boot entries are 'tabbed' across */
558 name = talloc_asprintf(cui->main, "%s%s",
559 tab, opt->name ? : "Unknown Name");
561 /* Save the item in opt->ui_info for cui_device_remove() */
562 opt->ui_info = i = pmenu_item_create(cui->main, name);
567 i->on_edit = cui_item_edit;
568 i->on_execute = cui_boot;
569 i->data = cod = talloc(i, struct cui_opt_data);
573 cod->opt_hash = pb_opt_hash(dev, opt);
574 cod->name = opt->name;
575 cod->bd = talloc(i, struct pb_boot_data);
577 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
578 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
579 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
580 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
581 cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
583 /* This disconnects items array from menu. */
584 result = set_menu_items(cui->main->ncm, NULL);
587 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
589 /* Insert new items at insert_pt. */
591 insert_pt = pmenu_grow(cui->main, 2);
592 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
593 pb_log("%s: adding new device hierarchy %s\n",
594 __func__,opt->device_id);
595 pmenu_item_insert(cui->main, i, insert_pt+1);
597 insert_pt = pmenu_grow(cui->main, 1);
598 pmenu_item_add(cui->main, i, insert_pt);
601 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
602 pb_log(" image '%s'\n", cod->bd->image);
603 pb_log(" initrd '%s'\n", cod->bd->initrd);
604 pb_log(" args '%s'\n", cod->bd->args);
605 pb_log(" argsig '%s'\n", cod->bd->args_sig_file);
607 /* Re-attach the items array. */
608 result = set_menu_items(cui->main->ncm, cui->main->items);
611 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
614 pb_log("%s\n", __func__);
615 pmenu_dump_items(cui->main->items,
616 item_count(cui->main->ncm) + 1);
619 if (!item_visible(selected)) {
622 top = top_row(cui->main->ncm);
623 idx = item_index(selected);
625 /* If our index is above the current top row, align
626 * us to the new top. Otherwise, align us to the new
628 top = top < idx ? idx - rows + 1 : idx;
630 set_top_row(cui->main->ncm, top);
631 set_current_item(cui->main->ncm, selected);
634 if (cui->current == &cui->main->scr)
635 nc_scr_post(cui->current);
641 * cui_device_remove - Client device remove callback.
643 * Removes all the menu_items for the device from the main menu and redraws the
644 * main menu if it is active.
647 static void cui_device_remove(struct device *dev, void *arg)
649 struct cui *cui = cui_from_arg(arg);
650 struct boot_option *opt;
652 int rows, cols, top, last;
655 pb_log("%s: %p %s\n", __func__, dev, dev->id);
657 if (cui->current == &cui->main->scr)
658 nc_scr_unpost(cui->current);
660 /* This disconnects items array from menu. */
662 result = set_menu_items(cui->main->ncm, NULL);
665 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
667 list_for_each_entry(&dev->boot_options, opt, list) {
668 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
670 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
671 pmenu_remove(cui->main, item);
674 /* Manually remove remaining device hierarchy item */
675 for (i=0; i < cui->main->item_count; i++) {
676 struct pmenu_item *item = item_userptr(cui->main->items[i]);
677 if (!item || !item->data )
680 struct cui_opt_data *data = item->data;
681 if (data && data->dev && data->dev == dev)
682 pmenu_remove(cui->main,item);
685 /* Re-attach the items array. */
687 result = set_menu_items(cui->main->ncm, cui->main->items);
689 /* Move cursor to 'Exit' menu entry */
690 menu_format(cui->main->ncm, &rows, &cols);
691 last = cui->main->item_count - 1;
692 set_current_item(cui->main->ncm, cui->main->items[last]);
693 if (!item_visible(cui->main->items[last])) {
694 top = last < rows ? 0 : last - rows + 1;
695 set_top_row(cui->main->ncm, top);
699 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
702 pb_log("%s\n", __func__);
703 pmenu_dump_items(cui->main->items,
704 item_count(cui->main->ncm) + 1);
707 if (cui->current == &cui->main->scr)
708 nc_scr_post(cui->current);
711 static void cui_update_status(struct status *status, void *arg)
713 struct cui *cui = cui_from_arg(arg);
715 statuslog_append_steal(cui, cui->statuslog, status);
717 nc_scr_status_printf(cui->current, "%s", status->message);
720 static void cui_update_mm_title(struct cui *cui)
722 struct nc_frame *frame = &cui->main->scr.frame;
724 talloc_free(frame->rtitle);
726 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
727 if (cui->sysinfo->identifier)
728 frame->rtitle = talloc_asprintf_append(frame->rtitle,
729 " %s", cui->sysinfo->identifier);
731 if (cui->current == &cui->main->scr)
732 nc_scr_post(cui->current);
735 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
737 struct cui *cui = cui_from_arg(arg);
738 cui->sysinfo = talloc_steal(cui, sysinfo);
740 /* if we're currently displaying the system info screen, inform it
741 * of the updated information. */
742 if (cui->sysinfo_screen)
743 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
745 if (cui->subset_screen)
746 subset_screen_update(cui->subset_screen);
748 /* ... and do the same with the config screen... */
749 if (cui->config_screen)
750 config_screen_update(cui->config_screen, cui->config, sysinfo);
752 /* ... and the boot editor. */
753 if (cui->boot_editor)
754 boot_editor_update(cui->boot_editor, sysinfo);
756 cui_update_mm_title(cui);
759 static void cui_update_language(struct cui *cui, char *lang)
764 cur_lang = setlocale(LC_ALL, NULL);
765 if (cur_lang && !strcmp(cur_lang, lang))
768 setlocale(LC_ALL, lang);
770 /* we'll need to update the menu: drop all items and repopulate */
771 repost_menu = cui->current == &cui->main->scr;
773 nc_scr_unpost(cui->current);
775 talloc_free(cui->main);
776 cui->main = main_menu_init(cui);
779 cui->current = &cui->main->scr;
780 nc_scr_post(cui->current);
783 discover_client_enumerate(cui->client);
786 static void cui_update_config(struct config *config, void *arg)
788 struct cui *cui = cui_from_arg(arg);
789 cui->config = talloc_steal(cui, config);
792 cui_update_language(cui, config->lang);
794 if (cui->subset_screen)
795 subset_screen_update(cui->subset_screen);
797 if (cui->config_screen)
798 config_screen_update(cui->config_screen, config, cui->sysinfo);
800 if (config->safe_mode)
801 nc_scr_status_printf(cui->current,
802 _("SAFE MODE: select '%s' to continue"),
803 _("Rescan devices"));
806 int cui_send_config(struct cui *cui, struct config *config)
808 return discover_client_send_config(cui->client, config);
811 int cui_send_url(struct cui *cui, char * url)
813 return discover_client_send_url(cui->client, url);
816 void cui_send_reinit(struct cui *cui)
818 discover_client_send_reinit(cui->client);
821 static int menu_sysinfo_execute(struct pmenu_item *item)
823 cui_show_sysinfo(cui_from_item(item));
827 static int menu_config_execute(struct pmenu_item *item)
829 cui_show_config(cui_from_item(item));
833 static int menu_lang_execute(struct pmenu_item *item)
835 cui_show_lang(cui_from_item(item));
839 static int menu_statuslog_execute(struct pmenu_item *item)
841 cui_show_statuslog(cui_from_item(item));
845 static int menu_reinit_execute(struct pmenu_item *item)
847 if (cui_from_item(item)->client)
848 cui_send_reinit(cui_from_item(item));
852 static int menu_add_url_execute(struct pmenu_item *item)
854 if (cui_from_item(item)->client)
855 cui_show_add_url(cui_from_item(item));
860 * pb_mm_init - Setup the main menu instance.
862 static struct pmenu *main_menu_init(struct cui *cui)
864 struct pmenu_item *i;
867 bool lockdown = lockdown_active();
869 m = pmenu_init(cui, 8, cui_on_exit);
871 pb_log("%s: failed\n", __func__);
875 m->on_new = cui_item_new;
877 m->scr.frame.ltitle = talloc_asprintf(m,
878 "Petitboot (" PACKAGE_VERSION ")");
879 m->scr.frame.rtitle = NULL;
880 m->scr.frame.help = talloc_strdup(m,
881 _("Enter=accept, e=edit, n=new, x=exit, l=language, g=log, h=help"));
882 m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
884 /* add a separator */
885 i = pmenu_item_create(m, " ");
886 item_opts_off(i->nci, O_SELECTABLE);
887 pmenu_item_insert(m, i, 0);
889 /* add system items */
890 i = pmenu_item_create(m, _("System information"));
891 i->on_execute = menu_sysinfo_execute;
892 pmenu_item_insert(m, i, 1);
894 i = pmenu_item_create(m, _("System configuration"));
895 i->on_execute = menu_config_execute;
896 pmenu_item_insert(m, i, 2);
898 i = pmenu_item_create(m, _("System status log"));
899 i->on_execute = menu_statuslog_execute;
900 pmenu_item_insert(m, i, 3);
902 /* this label isn't translated, so we don't want a gettext() here */
903 i = pmenu_item_create(m, "Language");
904 i->on_execute = menu_lang_execute;
905 pmenu_item_insert(m, i, 4);
907 i = pmenu_item_create(m, _("Rescan devices"));
908 i->on_execute = menu_reinit_execute;
909 pmenu_item_insert(m, i, 5);
911 i = pmenu_item_create(m, _("Retrieve config from URL"));
912 i->on_execute = menu_add_url_execute;
913 pmenu_item_insert(m, i, 6);
916 i = pmenu_item_create(m, _("Reboot"));
918 i = pmenu_item_create(m, _("Exit to shell"));
919 i->on_execute = pmenu_exit_cb;
920 pmenu_item_insert(m, i, 7);
922 result = pmenu_setup(m);
925 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
930 m->help_title = _("main menu");
931 m->help_text = &main_menu_help_text;
933 menu_opts_off(m->ncm, O_SHOWDESC);
934 set_menu_mark(m->ncm, " *");
935 set_current_item(m->ncm, i->nci);
944 static struct discover_client_ops cui_client_ops = {
946 .boot_option_add = cui_boot_option_add,
947 .device_remove = cui_device_remove,
948 .update_status = cui_update_status,
949 .update_sysinfo = cui_update_sysinfo,
950 .update_config = cui_update_config,
953 /* cui_server_wait_on_exit - On exit spin until the server is available.
955 * If the program exits before connecting to the server autoboot won't be
956 * cancelled even though there has been keyboard activity. This function is
957 * called by a child process which will spin until the server is connected and
958 * told to cancel autoboot.
960 * Processes exiting from this function will not carry out the cui_atexit()
963 static void cui_server_wait_on_exit(struct cui *cui)
967 while (!cui->client) {
968 cui->client = discover_client_init(cui->waitset,
969 &cui_client_ops, cui);
974 talloc_steal(cui, cui->client);
975 discover_client_cancel_default(cui->client);
978 /* cui_server_wait - Connect to the discover server.
979 * @arg: Pointer to the cui instance.
981 * A timeout callback that attempts to connect to the discover server; on
982 * failure it registers itself with a one second timeout to try again.
983 * On success the cui->client struct will be set.
985 * Since this updates the status line when called it must not be called
986 * before the UI is ready.
988 static int cui_server_wait(void *arg)
990 struct cui *cui = cui_from_arg(arg);
993 pb_debug("We already have a server!\n");
997 /* We haven't yet connected to the server */
998 pb_log("Trying to connect...\n");
999 cui->client = discover_client_init(cui->waitset,
1000 &cui_client_ops, cui);
1003 waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
1005 nc_scr_status_printf(cui->current,
1006 "Info: Waiting for device discovery");
1008 nc_scr_status_printf(cui->current,
1009 "Info: Connected to pb-discover!");
1010 talloc_steal(cui, cui->client);
1012 if (cui->has_input) {
1013 pb_log("Aborting default boot on pb-discover connect\n");
1014 discover_client_cancel_default(cui->client);
1022 * cui_init - Setup the cui instance.
1023 * @platform_info: A value for the struct cui platform_info member.
1025 * Returns a pointer to a struct cui on success, or NULL on error.
1027 * Allocates the cui instance, sets up the client and stdin waiters, and
1028 * sets up the ncurses menu screen.
1031 struct cui *cui_init(void* platform_info,
1032 int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
1037 cui = talloc_zero(NULL, struct cui);
1039 pb_log("%s: alloc cui failed.\n", __func__);
1040 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
1044 cui->c_sig = pb_cui_sig;
1045 cui->platform_info = platform_info;
1046 cui->waitset = waitset_create(cui);
1047 cui->statuslog = statuslog_init(cui);
1049 process_init(cui, cui->waitset, false);
1051 /* Loop here for scripts that just started the server. */
1054 for (i = start_daemon ? 2 : 15; i && timeout; i--) {
1055 cui->client = discover_client_init(cui->waitset,
1056 &cui_client_ops, cui);
1057 if (cui->client || !i)
1059 pb_log("%s: waiting for server %d\n", __func__, i);
1063 if (!cui->client && start_daemon) {
1068 result = pb_start_daemon(cui);
1073 pb_log("%s: discover_client_init failed.\n", __func__);
1074 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1076 fprintf(stderr, _("could not start pb-discover, the petitboot "
1078 goto fail_client_init;
1081 if (!cui->client && !timeout) {
1082 /* Have the first timeout fire immediately so we can check
1083 * for the server as soon as the UI is ready */
1084 waiter_register_timeout(cui->waitset, 0,
1085 cui_server_wait, cui);
1086 } else if (!cui->client) {
1087 pb_log("%s: discover_client_init failed.\n", __func__);
1088 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1090 fprintf(stderr, _("check that pb-discover, "
1091 "the petitboot daemon is running.\n"));
1092 goto fail_client_init;
1096 talloc_steal(cui, cui->client);
1099 cui->main = main_menu_init(cui);
1101 goto fail_client_init;
1103 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
1104 cui_process_key, cui);
1108 cui->pjs = pjs_init(cui, js_map);
1111 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
1112 WAIT_IN, cui_process_js, cui);
1124 * cui_run - The main cui program loop.
1125 * @cui: The cui instance.
1126 * @main: The menu to use as the main menu.
1128 * Runs the cui engine. Does not return until indicated to do so by some
1129 * user action, or an error occurs. Frees the cui object on return.
1130 * Returns 0 on success (return to shell), -1 on error (should restart).
1133 int cui_run(struct cui *cui)
1139 cui->current = &cui->main->scr;
1140 cui->default_item = 0;
1142 nc_scr_post(cui->current);
1145 int result = waiter_poll(cui->waitset);
1148 pb_log("%s: poll: %s\n", __func__, strerror(errno));
1155 while (cui->resize) {
1157 cui_handle_resize(cui);
1164 /* Fork a child to tell the server to cancel autoboot */
1167 cui_server_wait_on_exit(cui);
1171 pb_log("Failed to fork child on exit: %m\n");
1174 return cui->abort ? 0 : -1;