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-subset.h"
47 extern const struct help_text main_menu_help_text;
49 static struct pmenu *main_menu_init(struct cui *cui);
51 static bool lockdown_active(void)
53 bool lockdown = false;
54 if (access(LOCKDOWN_FILE, F_OK) != -1)
59 static void cui_start(void)
61 initscr(); /* Initialize ncurses. */
62 cbreak(); /* Disable line buffering. */
63 noecho(); /* Disable getch() echo. */
64 keypad(stdscr, TRUE); /* Enable num keypad keys. */
65 nonl(); /* Disable new-line translation. */
66 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
67 curs_set(0); /* Make cursor invisible */
68 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
70 /* We may be operating with an incorrect $TERM type; in this case
71 * the keymappings will be slightly broken. We want at least
72 * backspace to work though, so we'll define both DEL and ^H to
74 define_key("\x7f", KEY_BACKSPACE);
75 define_key("\x08", KEY_BACKSPACE);
77 /* we need backtab too, for form navigation. vt220 doesn't include
78 * this (kcbt), but we don't want to require a full linux/xterm termcap
80 define_key("\x1b[Z", KEY_BTAB);
82 /* We'll define a few other keys too since they're commonly
83 * used for navigation but the escape character will cause
84 * Petitboot to exit if they're left undefined */
85 define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
86 define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
87 define_key("\x1b\x5b\x31\x7e", KEY_HOME);
88 define_key("\x1b\x5b\x34\x7e", KEY_END);
89 define_key("\x1b\x4f\x48", KEY_HOME);
90 define_key("\x1b\x4f\x46", KEY_END);
91 define_key("OH", KEY_HOME);
92 define_key("OF", KEY_END);
93 define_key("\x1b\x5b\x41", KEY_UP);
94 define_key("\x1b\x5b\x42", KEY_DOWN);
95 define_key("\x1b\x5b\x33\x7e", KEY_DC);
97 while (getch() != ERR) /* flush stdin */
101 static void cui_atexit(void)
107 bool lockdown = lockdown_active();
116 * cui_abort - Signal the main cui program loop to exit.
118 * Sets cui.abort, which causes the cui_run() routine to return.
121 void cui_abort(struct cui *cui)
123 pb_log("%s: exiting\n", __func__);
128 * cui_resize - Signal the main cui program loop to resize
130 * Called at SIGWINCH.
133 void cui_resize(struct cui *cui)
135 pb_debug("%s: resizing\n", __func__);
140 * cui_on_exit - A generic main menu exit callback.
143 void cui_on_exit(struct pmenu *menu)
145 cui_abort(cui_from_pmenu(menu));
149 * cui_run_cmd - A generic cb to run the supplied command.
152 int cui_run_cmd(struct pmenu_item *item)
155 struct cui *cui = cui_from_item(item);
156 const char **cmd_argv = item->data;
158 nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
162 result = process_run_simple_argv(item, cmd_argv);
165 redrawwin(cui->current->main_ncw);
168 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
169 nc_scr_status_printf(cui->current, _("Failed: %s"),
177 * cui_boot - A generic cb to run kexec.
180 static int cui_boot(struct pmenu_item *item)
183 struct cui *cui = cui_from_item(item);
184 struct cui_opt_data *cod = cod_from_item(item);
186 assert(cui->current == &cui->main->scr);
188 pb_debug("%s: %s\n", __func__, cod->name);
190 nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
192 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
195 nc_scr_status_printf(cui->current,
196 _("Failed: boot %s"), cod->bd->image);
202 static void cui_boot_editor_on_exit(struct cui *cui,
203 struct pmenu_item *item,
204 struct pb_boot_data *bd)
206 struct pmenu *menu = cui->main;
207 struct cui_opt_data *cod;
208 int idx, top, rows, cols;
209 static int user_idx = 0;
211 /* Was the edit cancelled? */
213 cui_set_current(cui, &cui->main->scr);
214 talloc_free(cui->boot_editor);
215 cui->boot_editor = NULL;
219 /* Is this was a new item, we'll need to update the menu */
223 cod = talloc_zero(NULL, struct cui_opt_data);
224 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
226 item = pmenu_item_create(menu, cod->name);
232 item->on_edit = cui_item_edit;
233 item->on_execute = cui_boot;
236 talloc_steal(item, cod);
238 /* Detach the items array. */
239 set_menu_items(menu->ncm, NULL);
241 /* Insert new item at insert_pt. */
242 insert_pt = pmenu_grow(menu, 1);
243 pmenu_item_insert(menu, item, insert_pt);
245 /* Re-attach the items array. */
246 set_menu_items(menu->ncm, menu->items);
248 /* If our index is above the current top row, align
249 * us to the new top. Otherwise, align us to the new
251 menu_format(cui->main->ncm, &rows, &cols);
252 top = top_row(cui->main->ncm);
253 idx = item_index(item->nci);
258 top = idx < rows ? 0 : idx - rows + 1;
260 set_top_row(cui->main->ncm, top);
261 set_current_item(item->pmenu->ncm, item->nci);
263 nc_scr_post(&menu->scr);
268 cod->bd = talloc_steal(cod, bd);
271 cui_set_current(cui, &cui->main->scr);
272 talloc_free(cui->boot_editor);
273 cui->boot_editor = NULL;
276 void cui_item_edit(struct pmenu_item *item)
278 struct cui *cui = cui_from_item(item);
279 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
280 cui_boot_editor_on_exit);
281 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
284 void cui_item_new(struct pmenu *menu)
286 struct cui *cui = cui_from_pmenu(menu);
287 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
288 cui_boot_editor_on_exit);
289 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
292 static void cui_sysinfo_exit(struct cui *cui)
294 cui_set_current(cui, &cui->main->scr);
295 talloc_free(cui->sysinfo_screen);
296 cui->sysinfo_screen = NULL;
299 void cui_show_sysinfo(struct cui *cui)
301 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
303 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
306 static void cui_config_exit(struct cui *cui)
308 cui_set_current(cui, &cui->main->scr);
309 talloc_free(cui->config_screen);
310 cui->config_screen = NULL;
313 void cui_show_config(struct cui *cui)
315 cui->config_screen = config_screen_init(cui, cui->config,
316 cui->sysinfo, cui_config_exit);
317 cui_set_current(cui, config_screen_scr(cui->config_screen));
320 static void cui_lang_exit(struct cui *cui)
322 cui_set_current(cui, &cui->main->scr);
323 talloc_free(cui->lang_screen);
324 cui->lang_screen = NULL;
327 void cui_show_lang(struct cui *cui)
329 cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
330 cui_set_current(cui, lang_screen_scr(cui->lang_screen));
333 static void cui_add_url_exit(struct cui *cui)
335 cui_set_current(cui, &cui->main->scr);
336 talloc_free(cui->add_url_screen);
337 cui->add_url_screen = NULL;
340 void cui_show_add_url(struct cui *cui)
342 cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
343 cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
346 static void cui_help_exit(struct cui *cui)
348 cui_set_current(cui, help_screen_return_scr(cui->help_screen));
349 talloc_free(cui->help_screen);
350 cui->help_screen = NULL;
353 void cui_show_help(struct cui *cui, const char *title,
354 const struct help_text *text)
359 if (cui->help_screen)
362 cui->help_screen = help_screen_init(cui, cui->current,
363 title, text, cui_help_exit);
365 if (cui->help_screen)
366 cui_set_current(cui, help_screen_scr(cui->help_screen));
369 static void cui_subset_exit(struct cui *cui)
371 cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
372 talloc_free(cui->subset_screen);
373 cui->subset_screen = NULL;
376 void cui_show_subset(struct cui *cui, const char *title,
382 if (cui->subset_screen)
385 cui->subset_screen = subset_screen_init(cui, cui->current,
386 title, arg, cui_subset_exit);
388 if (cui->subset_screen)
389 cui_set_current(cui, subset_screen_scr(cui->subset_screen));
393 * cui_set_current - Set the currently active screen and redraw it.
396 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
400 DBGS("%p -> %p\n", cui->current, scr);
402 assert(cui->current != scr);
409 nc_scr_post(cui->current);
414 static bool process_global_keys(struct cui *cui, int key)
418 if (cui->current && cui->current->main_ncw)
426 * cui_process_key - Process input on stdin.
429 static int cui_process_key(void *arg)
431 struct cui *cui = cui_from_arg(arg);
433 assert(cui->current);
438 pb_debug("%s: got key %d\n", __func__, c);
443 if (!cui->has_input) {
444 cui->has_input = true;
446 pb_log("UI input received (key = %d), aborting "
447 "default boot\n", c);
448 discover_client_cancel_default(cui->client);
450 pb_log("UI input received (key = %d), aborting "
451 "once server connects\n", c);
455 if (process_global_keys(cui, c))
458 cui->current->process_key(cui->current, c);
465 * cui_process_js - Process joystick events.
468 static int cui_process_js(void *arg)
470 struct cui *cui = cui_from_arg(arg);
473 c = pjs_process_event(cui->pjs);
477 cui_process_key(arg);
484 * cui_handle_resize - Handle the term resize.
487 static void cui_handle_resize(struct cui *cui)
491 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
492 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
496 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
498 wclear(cui->current->main_ncw);
499 resize_term(ws.ws_row, ws.ws_col);
500 cui->current->resize(cui->current);
502 /* For some reason this makes ncurses redraw the screen */
504 redrawwin(cui->current->main_ncw);
505 wrefresh(cui->current->main_ncw);
509 * cui_device_add - Client device_add callback.
511 * Creates menu_items for all the device boot_options and inserts those
512 * menu_items into the main menu. Redraws the main menu if it is active.
515 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
518 struct pmenu_item *i, *dev_hdr = NULL;
519 struct cui *cui = cui_from_arg(arg);
520 struct cui_opt_data *cod;
521 const char *tab = " ";
522 unsigned int insert_pt;
523 int result, rows, cols;
527 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
529 selected = current_item(cui->main->ncm);
530 menu_format(cui->main->ncm, &rows, &cols);
532 if (cui->current == &cui->main->scr)
533 nc_scr_unpost(cui->current);
535 /* Check if the boot device is new */
536 dev_hdr = pmenu_find_device(cui->main, dev, opt);
538 /* All actual boot entries are 'tabbed' across */
539 name = talloc_asprintf(cui->main, "%s%s",
540 tab, opt->name ? : "Unknown Name");
542 /* Save the item in opt->ui_info for cui_device_remove() */
543 opt->ui_info = i = pmenu_item_create(cui->main, name);
548 i->on_edit = cui_item_edit;
549 i->on_execute = cui_boot;
550 i->data = cod = talloc(i, struct cui_opt_data);
554 cod->opt_hash = pb_opt_hash(dev, opt);
555 cod->name = opt->name;
556 cod->bd = talloc(i, struct pb_boot_data);
558 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
559 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
560 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
561 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
562 cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
564 /* This disconnects items array from menu. */
565 result = set_menu_items(cui->main->ncm, NULL);
568 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
570 /* Insert new items at insert_pt. */
572 insert_pt = pmenu_grow(cui->main, 2);
573 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
574 pb_log("%s: adding new device hierarchy %s\n",
575 __func__,opt->device_id);
576 pmenu_item_insert(cui->main, i, insert_pt+1);
578 insert_pt = pmenu_grow(cui->main, 1);
579 pmenu_item_add(cui->main, i, insert_pt);
582 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
583 pb_log(" image '%s'\n", cod->bd->image);
584 pb_log(" initrd '%s'\n", cod->bd->initrd);
585 pb_log(" args '%s'\n", cod->bd->args);
586 pb_log(" argsig '%s'\n", cod->bd->args_sig_file);
588 /* Re-attach the items array. */
589 result = set_menu_items(cui->main->ncm, cui->main->items);
592 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
595 pb_log("%s\n", __func__);
596 pmenu_dump_items(cui->main->items,
597 item_count(cui->main->ncm) + 1);
600 if (!item_visible(selected)) {
603 top = top_row(cui->main->ncm);
604 idx = item_index(selected);
606 /* If our index is above the current top row, align
607 * us to the new top. Otherwise, align us to the new
609 top = top < idx ? idx - rows + 1 : idx;
611 set_top_row(cui->main->ncm, top);
612 set_current_item(cui->main->ncm, selected);
615 if (cui->current == &cui->main->scr)
616 nc_scr_post(cui->current);
622 * cui_device_remove - Client device remove callback.
624 * Removes all the menu_items for the device from the main menu and redraws the
625 * main menu if it is active.
628 static void cui_device_remove(struct device *dev, void *arg)
630 struct cui *cui = cui_from_arg(arg);
631 struct boot_option *opt;
633 int rows, cols, top, last;
636 pb_log("%s: %p %s\n", __func__, dev, dev->id);
638 if (cui->current == &cui->main->scr)
639 nc_scr_unpost(cui->current);
641 /* This disconnects items array from menu. */
643 result = set_menu_items(cui->main->ncm, NULL);
646 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
648 list_for_each_entry(&dev->boot_options, opt, list) {
649 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
651 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
652 pmenu_remove(cui->main, item);
655 /* Manually remove remaining device hierarchy item */
656 for (i=0; i < cui->main->item_count; i++) {
657 struct pmenu_item *item = item_userptr(cui->main->items[i]);
658 if (!item || !item->data )
661 struct cui_opt_data *data = item->data;
662 if (data && data->dev && data->dev == dev)
663 pmenu_remove(cui->main,item);
666 /* Re-attach the items array. */
668 result = set_menu_items(cui->main->ncm, cui->main->items);
670 /* Move cursor to 'Exit' menu entry */
671 menu_format(cui->main->ncm, &rows, &cols);
672 last = cui->main->item_count - 1;
673 set_current_item(cui->main->ncm, cui->main->items[last]);
674 if (!item_visible(cui->main->items[last])) {
675 top = last < rows ? 0 : last - rows + 1;
676 set_top_row(cui->main->ncm, top);
680 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
683 pb_log("%s\n", __func__);
684 pmenu_dump_items(cui->main->items,
685 item_count(cui->main->ncm) + 1);
688 if (cui->current == &cui->main->scr)
689 nc_scr_post(cui->current);
692 static void cui_update_status(struct boot_status *status, void *arg)
694 struct cui *cui = cui_from_arg(arg);
696 nc_scr_status_printf(cui->current,
698 status->type == BOOT_STATUS_ERROR ?
699 _("Error") : _("Info"),
704 static void cui_update_mm_title(struct cui *cui)
706 struct nc_frame *frame = &cui->main->scr.frame;
708 talloc_free(frame->rtitle);
710 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
711 if (cui->sysinfo->identifier)
712 frame->rtitle = talloc_asprintf_append(frame->rtitle,
713 " %s", cui->sysinfo->identifier);
715 if (cui->current == &cui->main->scr)
716 nc_scr_post(cui->current);
719 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
721 struct cui *cui = cui_from_arg(arg);
722 cui->sysinfo = talloc_steal(cui, sysinfo);
724 /* if we're currently displaying the system info screen, inform it
725 * of the updated information. */
726 if (cui->sysinfo_screen)
727 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
729 if (cui->subset_screen)
730 subset_screen_update(cui->subset_screen);
732 /* ... and do the same with the config screen... */
733 if (cui->config_screen)
734 config_screen_update(cui->config_screen, cui->config, sysinfo);
736 /* ... and the boot editor. */
737 if (cui->boot_editor)
738 boot_editor_update(cui->boot_editor, sysinfo);
740 cui_update_mm_title(cui);
743 static void cui_update_language(struct cui *cui, char *lang)
748 cur_lang = setlocale(LC_ALL, NULL);
749 if (cur_lang && !strcmp(cur_lang, lang))
752 setlocale(LC_ALL, lang);
754 /* we'll need to update the menu: drop all items and repopulate */
755 repost_menu = cui->current == &cui->main->scr;
757 nc_scr_unpost(cui->current);
759 talloc_free(cui->main);
760 cui->main = main_menu_init(cui);
763 cui->current = &cui->main->scr;
764 nc_scr_post(cui->current);
767 discover_client_enumerate(cui->client);
770 static void cui_update_config(struct config *config, void *arg)
772 struct cui *cui = cui_from_arg(arg);
773 cui->config = talloc_steal(cui, config);
776 cui_update_language(cui, config->lang);
778 if (cui->subset_screen)
779 subset_screen_update(cui->subset_screen);
781 if (cui->config_screen)
782 config_screen_update(cui->config_screen, config, cui->sysinfo);
784 if (config->safe_mode)
785 nc_scr_status_printf(cui->current,
786 _("SAFE MODE: select '%s' to continue"),
787 _("Rescan devices"));
790 int cui_send_config(struct cui *cui, struct config *config)
792 return discover_client_send_config(cui->client, config);
795 int cui_send_url(struct cui *cui, char * url)
797 return discover_client_send_url(cui->client, url);
800 void cui_send_reinit(struct cui *cui)
802 discover_client_send_reinit(cui->client);
805 static int menu_sysinfo_execute(struct pmenu_item *item)
807 cui_show_sysinfo(cui_from_item(item));
811 static int menu_config_execute(struct pmenu_item *item)
813 cui_show_config(cui_from_item(item));
817 static int menu_lang_execute(struct pmenu_item *item)
819 cui_show_lang(cui_from_item(item));
823 static int menu_reinit_execute(struct pmenu_item *item)
825 if (cui_from_item(item)->client)
826 cui_send_reinit(cui_from_item(item));
830 static int menu_add_url_execute(struct pmenu_item *item)
832 if (cui_from_item(item)->client)
833 cui_show_add_url(cui_from_item(item));
838 * pb_mm_init - Setup the main menu instance.
840 static struct pmenu *main_menu_init(struct cui *cui)
842 struct pmenu_item *i;
845 bool lockdown = lockdown_active();
847 m = pmenu_init(cui, 7, cui_on_exit);
849 pb_log("%s: failed\n", __func__);
853 m->on_new = cui_item_new;
855 m->scr.frame.ltitle = talloc_asprintf(m,
856 "Petitboot (" PACKAGE_VERSION ")");
857 m->scr.frame.rtitle = NULL;
858 m->scr.frame.help = talloc_strdup(m,
859 _("Enter=accept, e=edit, n=new, x=exit, l=language, h=help"));
860 m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
862 /* add a separator */
863 i = pmenu_item_create(m, " ");
864 item_opts_off(i->nci, O_SELECTABLE);
865 pmenu_item_insert(m, i, 0);
867 /* add system items */
868 i = pmenu_item_create(m, _("System information"));
869 i->on_execute = menu_sysinfo_execute;
870 pmenu_item_insert(m, i, 1);
872 i = pmenu_item_create(m, _("System configuration"));
873 i->on_execute = menu_config_execute;
874 pmenu_item_insert(m, i, 2);
876 /* this label isn't translated, so we don't want a gettext() here */
877 i = pmenu_item_create(m, "Language");
878 i->on_execute = menu_lang_execute;
879 pmenu_item_insert(m, i, 3);
881 i = pmenu_item_create(m, _("Rescan devices"));
882 i->on_execute = menu_reinit_execute;
883 pmenu_item_insert(m, i, 4);
885 i = pmenu_item_create(m, _("Retrieve config from URL"));
886 i->on_execute = menu_add_url_execute;
887 pmenu_item_insert(m, i, 5);
890 i = pmenu_item_create(m, _("Reboot"));
892 i = pmenu_item_create(m, _("Exit to shell"));
893 i->on_execute = pmenu_exit_cb;
894 pmenu_item_insert(m, i, 6);
896 result = pmenu_setup(m);
899 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
904 m->help_title = _("main menu");
905 m->help_text = &main_menu_help_text;
907 menu_opts_off(m->ncm, O_SHOWDESC);
908 set_menu_mark(m->ncm, " *");
909 set_current_item(m->ncm, i->nci);
918 static struct discover_client_ops cui_client_ops = {
920 .boot_option_add = cui_boot_option_add,
921 .device_remove = cui_device_remove,
922 .update_status = cui_update_status,
923 .update_sysinfo = cui_update_sysinfo,
924 .update_config = cui_update_config,
927 /* cui_server_wait - Connect to the discover server.
928 * @arg: Pointer to the cui instance.
930 * A timeout callback that attempts to connect to the discover server; on
931 * failure it registers itself with a one second timeout to try again.
932 * On success the cui->client struct will be set.
934 * Since this updates the status line when called it must not be called
935 * before the UI is ready.
937 static int cui_server_wait(void *arg)
939 struct cui *cui = cui_from_arg(arg);
942 pb_debug("We already have a server!\n");
946 /* We haven't yet connected to the server */
947 pb_log("Trying to connect...\n");
948 cui->client = discover_client_init(cui->waitset,
949 &cui_client_ops, cui);
952 waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
954 nc_scr_status_printf(cui->current, "Info: Waiting for server");
956 nc_scr_status_printf(cui->current, "Info: Connected to server!");
957 talloc_steal(cui, cui->client);
959 if (cui->has_input) {
960 pb_log("Aborting default boot on server connect\n");
961 discover_client_cancel_default(cui->client);
969 * cui_init - Setup the cui instance.
970 * @platform_info: A value for the struct cui platform_info member.
972 * Returns a pointer to a struct cui on success, or NULL on error.
974 * Allocates the cui instance, sets up the client and stdin waiters, and
975 * sets up the ncurses menu screen.
978 struct cui *cui_init(void* platform_info,
979 int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
984 cui = talloc_zero(NULL, struct cui);
986 pb_log("%s: alloc cui failed.\n", __func__);
987 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
991 cui->c_sig = pb_cui_sig;
992 cui->platform_info = platform_info;
993 cui->waitset = waitset_create(cui);
995 process_init(cui, cui->waitset, false);
997 /* Loop here for scripts that just started the server. */
1000 for (i = start_daemon ? 2 : 15; i && timeout; i--) {
1001 cui->client = discover_client_init(cui->waitset,
1002 &cui_client_ops, cui);
1003 if (cui->client || !i)
1005 pb_log("%s: waiting for server %d\n", __func__, i);
1009 if (!cui->client && start_daemon) {
1014 result = pb_start_daemon(cui);
1019 pb_log("%s: discover_client_init failed.\n", __func__);
1020 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1022 fprintf(stderr, _("could not start pb-discover, the petitboot "
1024 goto fail_client_init;
1027 if (!cui->client && !timeout) {
1028 /* Have the first timeout fire immediately so we can check
1029 * for the server as soon as the UI is ready */
1030 waiter_register_timeout(cui->waitset, 0,
1031 cui_server_wait, cui);
1032 } else if (!cui->client) {
1033 pb_log("%s: discover_client_init failed.\n", __func__);
1034 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1036 fprintf(stderr, _("check that pb-discover, "
1037 "the petitboot daemon is running.\n"));
1038 goto fail_client_init;
1042 talloc_steal(cui, cui->client);
1045 cui->main = main_menu_init(cui);
1047 goto fail_client_init;
1049 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
1050 cui_process_key, cui);
1054 cui->pjs = pjs_init(cui, js_map);
1057 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
1058 WAIT_IN, cui_process_js, cui);
1070 * cui_run - The main cui program loop.
1071 * @cui: The cui instance.
1072 * @main: The menu to use as the main menu.
1074 * Runs the cui engine. Does not return until indicated to do so by some
1075 * user action, or an error occurs. Frees the cui object on return.
1076 * Returns 0 on success (return to shell), -1 on error (should restart).
1079 int cui_run(struct cui *cui)
1083 cui->current = &cui->main->scr;
1084 cui->default_item = 0;
1086 nc_scr_post(cui->current);
1089 int result = waiter_poll(cui->waitset);
1092 pb_log("%s: poll: %s\n", __func__, strerror(errno));
1099 while (cui->resize) {
1101 cui_handle_resize(cui);
1107 return cui->abort ? 0 : -1;