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 bool cui_detached = false;
51 static struct pmenu *main_menu_init(struct cui *cui);
53 static bool lockdown_active(void)
55 bool lockdown = false;
56 if (access(LOCKDOWN_FILE, F_OK) != -1)
61 static void cui_start(void)
63 initscr(); /* Initialize ncurses. */
64 cbreak(); /* Disable line buffering. */
65 noecho(); /* Disable getch() echo. */
66 keypad(stdscr, TRUE); /* Enable num keypad keys. */
67 nonl(); /* Disable new-line translation. */
68 intrflush(stdscr, FALSE); /* Disable interrupt flush. */
69 curs_set(0); /* Make cursor invisible */
70 nodelay(stdscr, TRUE); /* Enable non-blocking getch() */
72 /* We may be operating with an incorrect $TERM type; in this case
73 * the keymappings will be slightly broken. We want at least
74 * backspace to work though, so we'll define both DEL and ^H to
76 define_key("\x7f", KEY_BACKSPACE);
77 define_key("\x08", KEY_BACKSPACE);
79 /* we need backtab too, for form navigation. vt220 doesn't include
80 * this (kcbt), but we don't want to require a full linux/xterm termcap
82 define_key("\x1b[Z", KEY_BTAB);
84 /* We'll define a few other keys too since they're commonly
85 * used for navigation but the escape character will cause
86 * Petitboot to exit if they're left undefined */
87 define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
88 define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
89 define_key("\x1b\x5b\x31\x7e", KEY_HOME);
90 define_key("\x1b\x5b\x34\x7e", KEY_END);
91 define_key("\x1b\x4f\x48", KEY_HOME);
92 define_key("\x1b\x4f\x46", KEY_END);
93 define_key("OH", KEY_HOME);
94 define_key("OF", KEY_END);
95 define_key("\x1b\x5b\x41", KEY_UP);
96 define_key("\x1b\x5b\x42", KEY_DOWN);
97 define_key("\x1b\x5b\x33\x7e", KEY_DC);
99 while (getch() != ERR) /* flush stdin */
103 static void cui_atexit(void)
112 bool lockdown = lockdown_active();
121 * cui_abort - Signal the main cui program loop to exit.
123 * Sets cui.abort, which causes the cui_run() routine to return.
126 void cui_abort(struct cui *cui)
128 pb_log("%s: exiting\n", __func__);
133 * cui_resize - Signal the main cui program loop to resize
135 * Called at SIGWINCH.
138 void cui_resize(struct cui *cui)
140 pb_debug("%s: resizing\n", __func__);
145 * cui_on_exit - A generic main menu exit callback.
148 void cui_on_exit(struct pmenu *menu)
150 cui_abort(cui_from_pmenu(menu));
154 * cui_run_cmd - A generic cb to run the supplied command.
157 int cui_run_cmd(struct pmenu_item *item)
160 struct cui *cui = cui_from_item(item);
161 const char **cmd_argv = item->data;
163 nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
167 result = process_run_simple_argv(item, cmd_argv);
170 redrawwin(cui->current->main_ncw);
173 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
174 nc_scr_status_printf(cui->current, _("Failed: %s"),
182 * cui_boot - A generic cb to run kexec.
185 static int cui_boot(struct pmenu_item *item)
188 struct cui *cui = cui_from_item(item);
189 struct cui_opt_data *cod = cod_from_item(item);
191 assert(cui->current == &cui->main->scr);
193 pb_debug("%s: %s\n", __func__, cod->name);
195 nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
197 result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
200 nc_scr_status_printf(cui->current,
201 _("Failed: boot %s"), cod->bd->image);
207 static void cui_boot_editor_on_exit(struct cui *cui,
208 struct pmenu_item *item,
209 struct pb_boot_data *bd)
211 struct pmenu *menu = cui->main;
212 struct cui_opt_data *cod;
213 int idx, top, rows, cols;
214 static int user_idx = 0;
216 /* Was the edit cancelled? */
218 cui_set_current(cui, &cui->main->scr);
219 talloc_free(cui->boot_editor);
220 cui->boot_editor = NULL;
224 /* Is this was a new item, we'll need to update the menu */
228 cod = talloc_zero(NULL, struct cui_opt_data);
229 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
231 item = pmenu_item_create(menu, cod->name);
237 item->on_edit = cui_item_edit;
238 item->on_execute = cui_boot;
241 talloc_steal(item, cod);
243 /* Detach the items array. */
244 set_menu_items(menu->ncm, NULL);
246 /* Insert new item at insert_pt. */
247 insert_pt = pmenu_grow(menu, 1);
248 pmenu_item_insert(menu, item, insert_pt);
250 /* Re-attach the items array. */
251 set_menu_items(menu->ncm, menu->items);
253 /* If our index is above the current top row, align
254 * us to the new top. Otherwise, align us to the new
256 menu_format(cui->main->ncm, &rows, &cols);
257 top = top_row(cui->main->ncm);
258 idx = item_index(item->nci);
263 top = idx < rows ? 0 : idx - rows + 1;
265 set_top_row(cui->main->ncm, top);
266 set_current_item(item->pmenu->ncm, item->nci);
268 nc_scr_post(&menu->scr);
273 cod->bd = talloc_steal(cod, bd);
276 cui_set_current(cui, &cui->main->scr);
277 talloc_free(cui->boot_editor);
278 cui->boot_editor = NULL;
281 void cui_item_edit(struct pmenu_item *item)
283 struct cui *cui = cui_from_item(item);
284 cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
285 cui_boot_editor_on_exit);
286 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
289 void cui_item_new(struct pmenu *menu)
291 struct cui *cui = cui_from_pmenu(menu);
292 cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
293 cui_boot_editor_on_exit);
294 cui_set_current(cui, boot_editor_scr(cui->boot_editor));
297 static void cui_sysinfo_exit(struct cui *cui)
299 cui_set_current(cui, &cui->main->scr);
300 talloc_free(cui->sysinfo_screen);
301 cui->sysinfo_screen = NULL;
304 void cui_show_sysinfo(struct cui *cui)
306 cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
308 cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
311 static void cui_config_exit(struct cui *cui)
313 cui_set_current(cui, &cui->main->scr);
314 talloc_free(cui->config_screen);
315 cui->config_screen = NULL;
318 void cui_show_config(struct cui *cui)
320 cui->config_screen = config_screen_init(cui, cui->config,
321 cui->sysinfo, cui_config_exit);
322 cui_set_current(cui, config_screen_scr(cui->config_screen));
325 static void cui_lang_exit(struct cui *cui)
327 cui_set_current(cui, &cui->main->scr);
328 talloc_free(cui->lang_screen);
329 cui->lang_screen = NULL;
332 void cui_show_lang(struct cui *cui)
334 cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
335 cui_set_current(cui, lang_screen_scr(cui->lang_screen));
338 static void cui_add_url_exit(struct cui *cui)
340 cui_set_current(cui, &cui->main->scr);
341 talloc_free(cui->add_url_screen);
342 cui->add_url_screen = NULL;
345 void cui_show_add_url(struct cui *cui)
347 cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
348 cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
351 static void cui_help_exit(struct cui *cui)
353 cui_set_current(cui, help_screen_return_scr(cui->help_screen));
354 talloc_free(cui->help_screen);
355 cui->help_screen = NULL;
358 void cui_show_help(struct cui *cui, const char *title,
359 const struct help_text *text)
364 if (cui->help_screen)
367 cui->help_screen = help_screen_init(cui, cui->current,
368 title, text, cui_help_exit);
370 if (cui->help_screen)
371 cui_set_current(cui, help_screen_scr(cui->help_screen));
374 static void cui_subset_exit(struct cui *cui)
376 cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
377 talloc_free(cui->subset_screen);
378 cui->subset_screen = NULL;
381 void cui_show_subset(struct cui *cui, const char *title,
387 if (cui->subset_screen)
390 cui->subset_screen = subset_screen_init(cui, cui->current,
391 title, arg, cui_subset_exit);
393 if (cui->subset_screen)
394 cui_set_current(cui, subset_screen_scr(cui->subset_screen));
398 * cui_set_current - Set the currently active screen and redraw it.
401 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
405 DBGS("%p -> %p\n", cui->current, scr);
407 assert(cui->current != scr);
414 nc_scr_post(cui->current);
419 static bool process_global_keys(struct cui *cui, int key)
423 if (cui->current && cui->current->main_ncw)
431 * cui_process_key - Process input on stdin.
434 static int cui_process_key(void *arg)
436 struct cui *cui = cui_from_arg(arg);
438 assert(cui->current);
443 pb_debug("%s: got key %d\n", __func__, c);
448 if (!cui->has_input) {
449 cui->has_input = true;
451 pb_log("UI input received (key = %d), aborting "
452 "default boot\n", c);
453 discover_client_cancel_default(cui->client);
455 pb_log("UI input received (key = %d), aborting "
456 "once server connects\n", c);
460 if (process_global_keys(cui, c))
463 cui->current->process_key(cui->current, c);
470 * cui_process_js - Process joystick events.
473 static int cui_process_js(void *arg)
475 struct cui *cui = cui_from_arg(arg);
478 c = pjs_process_event(cui->pjs);
482 cui_process_key(arg);
489 * cui_handle_resize - Handle the term resize.
492 static void cui_handle_resize(struct cui *cui)
496 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
497 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
501 pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
503 wclear(cui->current->main_ncw);
504 resize_term(ws.ws_row, ws.ws_col);
505 cui->current->resize(cui->current);
507 /* For some reason this makes ncurses redraw the screen */
509 redrawwin(cui->current->main_ncw);
510 wrefresh(cui->current->main_ncw);
514 * cui_device_add - Client device_add callback.
516 * Creates menu_items for all the device boot_options and inserts those
517 * menu_items into the main menu. Redraws the main menu if it is active.
520 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
523 struct pmenu_item *i, *dev_hdr = NULL;
524 struct cui *cui = cui_from_arg(arg);
525 struct cui_opt_data *cod;
526 const char *tab = " ";
527 unsigned int insert_pt;
528 int result, rows, cols;
532 pb_debug("%s: %p %s\n", __func__, opt, opt->id);
534 selected = current_item(cui->main->ncm);
535 menu_format(cui->main->ncm, &rows, &cols);
537 if (cui->current == &cui->main->scr)
538 nc_scr_unpost(cui->current);
540 /* Check if the boot device is new */
541 dev_hdr = pmenu_find_device(cui->main, dev, opt);
543 /* All actual boot entries are 'tabbed' across */
544 name = talloc_asprintf(cui->main, "%s%s",
545 tab, opt->name ? : "Unknown Name");
547 /* Save the item in opt->ui_info for cui_device_remove() */
548 opt->ui_info = i = pmenu_item_create(cui->main, name);
553 i->on_edit = cui_item_edit;
554 i->on_execute = cui_boot;
555 i->data = cod = talloc(i, struct cui_opt_data);
559 cod->opt_hash = pb_opt_hash(dev, opt);
560 cod->name = opt->name;
561 cod->bd = talloc(i, struct pb_boot_data);
563 cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
564 cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
565 cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
566 cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
567 cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
569 /* This disconnects items array from menu. */
570 result = set_menu_items(cui->main->ncm, NULL);
573 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
575 /* Insert new items at insert_pt. */
577 insert_pt = pmenu_grow(cui->main, 2);
578 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
579 pb_log("%s: adding new device hierarchy %s\n",
580 __func__,opt->device_id);
581 pmenu_item_insert(cui->main, i, insert_pt+1);
583 insert_pt = pmenu_grow(cui->main, 1);
584 pmenu_item_add(cui->main, i, insert_pt);
587 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
588 pb_log(" image '%s'\n", cod->bd->image);
589 pb_log(" initrd '%s'\n", cod->bd->initrd);
590 pb_log(" args '%s'\n", cod->bd->args);
591 pb_log(" argsig '%s'\n", cod->bd->args_sig_file);
593 /* Re-attach the items array. */
594 result = set_menu_items(cui->main->ncm, cui->main->items);
597 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
600 pb_log("%s\n", __func__);
601 pmenu_dump_items(cui->main->items,
602 item_count(cui->main->ncm) + 1);
605 if (!item_visible(selected)) {
608 top = top_row(cui->main->ncm);
609 idx = item_index(selected);
611 /* If our index is above the current top row, align
612 * us to the new top. Otherwise, align us to the new
614 top = top < idx ? idx - rows + 1 : idx;
616 set_top_row(cui->main->ncm, top);
617 set_current_item(cui->main->ncm, selected);
620 if (cui->current == &cui->main->scr)
621 nc_scr_post(cui->current);
627 * cui_device_remove - Client device remove callback.
629 * Removes all the menu_items for the device from the main menu and redraws the
630 * main menu if it is active.
633 static void cui_device_remove(struct device *dev, void *arg)
635 struct cui *cui = cui_from_arg(arg);
636 struct boot_option *opt;
638 int rows, cols, top, last;
641 pb_log("%s: %p %s\n", __func__, dev, dev->id);
643 if (cui->current == &cui->main->scr)
644 nc_scr_unpost(cui->current);
646 /* This disconnects items array from menu. */
648 result = set_menu_items(cui->main->ncm, NULL);
651 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
653 list_for_each_entry(&dev->boot_options, opt, list) {
654 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
656 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
657 pmenu_remove(cui->main, item);
660 /* Manually remove remaining device hierarchy item */
661 for (i=0; i < cui->main->item_count; i++) {
662 struct pmenu_item *item = item_userptr(cui->main->items[i]);
663 if (!item || !item->data )
666 struct cui_opt_data *data = item->data;
667 if (data && data->dev && data->dev == dev)
668 pmenu_remove(cui->main,item);
671 /* Re-attach the items array. */
673 result = set_menu_items(cui->main->ncm, cui->main->items);
675 /* Move cursor to 'Exit' menu entry */
676 menu_format(cui->main->ncm, &rows, &cols);
677 last = cui->main->item_count - 1;
678 set_current_item(cui->main->ncm, cui->main->items[last]);
679 if (!item_visible(cui->main->items[last])) {
680 top = last < rows ? 0 : last - rows + 1;
681 set_top_row(cui->main->ncm, top);
685 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
688 pb_log("%s\n", __func__);
689 pmenu_dump_items(cui->main->items,
690 item_count(cui->main->ncm) + 1);
693 if (cui->current == &cui->main->scr)
694 nc_scr_post(cui->current);
697 static void cui_update_status(struct boot_status *status, void *arg)
699 struct cui *cui = cui_from_arg(arg);
701 nc_scr_status_printf(cui->current,
703 status->type == BOOT_STATUS_ERROR ?
704 _("Error") : _("Info"),
709 static void cui_update_mm_title(struct cui *cui)
711 struct nc_frame *frame = &cui->main->scr.frame;
713 talloc_free(frame->rtitle);
715 frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
716 if (cui->sysinfo->identifier)
717 frame->rtitle = talloc_asprintf_append(frame->rtitle,
718 " %s", cui->sysinfo->identifier);
720 if (cui->current == &cui->main->scr)
721 nc_scr_post(cui->current);
724 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
726 struct cui *cui = cui_from_arg(arg);
727 cui->sysinfo = talloc_steal(cui, sysinfo);
729 /* if we're currently displaying the system info screen, inform it
730 * of the updated information. */
731 if (cui->sysinfo_screen)
732 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
734 if (cui->subset_screen)
735 subset_screen_update(cui->subset_screen);
737 /* ... and do the same with the config screen... */
738 if (cui->config_screen)
739 config_screen_update(cui->config_screen, cui->config, sysinfo);
741 /* ... and the boot editor. */
742 if (cui->boot_editor)
743 boot_editor_update(cui->boot_editor, sysinfo);
745 cui_update_mm_title(cui);
748 static void cui_update_language(struct cui *cui, char *lang)
753 cur_lang = setlocale(LC_ALL, NULL);
754 if (cur_lang && !strcmp(cur_lang, lang))
757 setlocale(LC_ALL, lang);
759 /* we'll need to update the menu: drop all items and repopulate */
760 repost_menu = cui->current == &cui->main->scr;
762 nc_scr_unpost(cui->current);
764 talloc_free(cui->main);
765 cui->main = main_menu_init(cui);
768 cui->current = &cui->main->scr;
769 nc_scr_post(cui->current);
772 discover_client_enumerate(cui->client);
775 static void cui_update_config(struct config *config, void *arg)
777 struct cui *cui = cui_from_arg(arg);
778 cui->config = talloc_steal(cui, config);
781 cui_update_language(cui, config->lang);
783 if (cui->subset_screen)
784 subset_screen_update(cui->subset_screen);
786 if (cui->config_screen)
787 config_screen_update(cui->config_screen, config, cui->sysinfo);
789 if (config->safe_mode)
790 nc_scr_status_printf(cui->current,
791 _("SAFE MODE: select '%s' to continue"),
792 _("Rescan devices"));
795 int cui_send_config(struct cui *cui, struct config *config)
797 return discover_client_send_config(cui->client, config);
800 int cui_send_url(struct cui *cui, char * url)
802 return discover_client_send_url(cui->client, url);
805 void cui_send_reinit(struct cui *cui)
807 discover_client_send_reinit(cui->client);
810 static int menu_sysinfo_execute(struct pmenu_item *item)
812 cui_show_sysinfo(cui_from_item(item));
816 static int menu_config_execute(struct pmenu_item *item)
818 cui_show_config(cui_from_item(item));
822 static int menu_lang_execute(struct pmenu_item *item)
824 cui_show_lang(cui_from_item(item));
828 static int menu_reinit_execute(struct pmenu_item *item)
830 if (cui_from_item(item)->client)
831 cui_send_reinit(cui_from_item(item));
835 static int menu_add_url_execute(struct pmenu_item *item)
837 if (cui_from_item(item)->client)
838 cui_show_add_url(cui_from_item(item));
843 * pb_mm_init - Setup the main menu instance.
845 static struct pmenu *main_menu_init(struct cui *cui)
847 struct pmenu_item *i;
850 bool lockdown = lockdown_active();
852 m = pmenu_init(cui, 7, cui_on_exit);
854 pb_log("%s: failed\n", __func__);
858 m->on_new = cui_item_new;
860 m->scr.frame.ltitle = talloc_asprintf(m,
861 "Petitboot (" PACKAGE_VERSION ")");
862 m->scr.frame.rtitle = NULL;
863 m->scr.frame.help = talloc_strdup(m,
864 _("Enter=accept, e=edit, n=new, x=exit, l=language, h=help"));
865 m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
867 /* add a separator */
868 i = pmenu_item_create(m, " ");
869 item_opts_off(i->nci, O_SELECTABLE);
870 pmenu_item_insert(m, i, 0);
872 /* add system items */
873 i = pmenu_item_create(m, _("System information"));
874 i->on_execute = menu_sysinfo_execute;
875 pmenu_item_insert(m, i, 1);
877 i = pmenu_item_create(m, _("System configuration"));
878 i->on_execute = menu_config_execute;
879 pmenu_item_insert(m, i, 2);
881 /* this label isn't translated, so we don't want a gettext() here */
882 i = pmenu_item_create(m, "Language");
883 i->on_execute = menu_lang_execute;
884 pmenu_item_insert(m, i, 3);
886 i = pmenu_item_create(m, _("Rescan devices"));
887 i->on_execute = menu_reinit_execute;
888 pmenu_item_insert(m, i, 4);
890 i = pmenu_item_create(m, _("Retrieve config from URL"));
891 i->on_execute = menu_add_url_execute;
892 pmenu_item_insert(m, i, 5);
895 i = pmenu_item_create(m, _("Reboot"));
897 i = pmenu_item_create(m, _("Exit to shell"));
898 i->on_execute = pmenu_exit_cb;
899 pmenu_item_insert(m, i, 6);
901 result = pmenu_setup(m);
904 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
909 m->help_title = _("main menu");
910 m->help_text = &main_menu_help_text;
912 menu_opts_off(m->ncm, O_SHOWDESC);
913 set_menu_mark(m->ncm, " *");
914 set_current_item(m->ncm, i->nci);
923 static struct discover_client_ops cui_client_ops = {
925 .boot_option_add = cui_boot_option_add,
926 .device_remove = cui_device_remove,
927 .update_status = cui_update_status,
928 .update_sysinfo = cui_update_sysinfo,
929 .update_config = cui_update_config,
932 /* cui_server_wait_on_exit - On exit spin until the server is available.
934 * If the program exits before connecting to the server autoboot won't be
935 * cancelled even though there has been keyboard activity. This function is
936 * called by a child process which will spin until the server is connected and
937 * told to cancel autoboot.
939 * Processes exiting from this function will not carry out the cui_atexit()
942 static void cui_server_wait_on_exit(struct cui *cui)
946 while (!cui->client) {
947 cui->client = discover_client_init(cui->waitset,
948 &cui_client_ops, cui);
953 talloc_steal(cui, cui->client);
954 discover_client_cancel_default(cui->client);
957 /* cui_server_wait - Connect to the discover server.
958 * @arg: Pointer to the cui instance.
960 * A timeout callback that attempts to connect to the discover server; on
961 * failure it registers itself with a one second timeout to try again.
962 * On success the cui->client struct will be set.
964 * Since this updates the status line when called it must not be called
965 * before the UI is ready.
967 static int cui_server_wait(void *arg)
969 struct cui *cui = cui_from_arg(arg);
972 pb_debug("We already have a server!\n");
976 /* We haven't yet connected to the server */
977 pb_log("Trying to connect...\n");
978 cui->client = discover_client_init(cui->waitset,
979 &cui_client_ops, cui);
982 waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
984 nc_scr_status_printf(cui->current, "Info: Waiting for server");
986 nc_scr_status_printf(cui->current, "Info: Connected to server!");
987 talloc_steal(cui, cui->client);
989 if (cui->has_input) {
990 pb_log("Aborting default boot on server connect\n");
991 discover_client_cancel_default(cui->client);
999 * cui_init - Setup the cui instance.
1000 * @platform_info: A value for the struct cui platform_info member.
1002 * Returns a pointer to a struct cui on success, or NULL on error.
1004 * Allocates the cui instance, sets up the client and stdin waiters, and
1005 * sets up the ncurses menu screen.
1008 struct cui *cui_init(void* platform_info,
1009 int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
1014 cui = talloc_zero(NULL, struct cui);
1016 pb_log("%s: alloc cui failed.\n", __func__);
1017 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
1021 cui->c_sig = pb_cui_sig;
1022 cui->platform_info = platform_info;
1023 cui->waitset = waitset_create(cui);
1025 process_init(cui, cui->waitset, false);
1027 /* Loop here for scripts that just started the server. */
1030 for (i = start_daemon ? 2 : 15; i && timeout; i--) {
1031 cui->client = discover_client_init(cui->waitset,
1032 &cui_client_ops, cui);
1033 if (cui->client || !i)
1035 pb_log("%s: waiting for server %d\n", __func__, i);
1039 if (!cui->client && start_daemon) {
1044 result = pb_start_daemon(cui);
1049 pb_log("%s: discover_client_init failed.\n", __func__);
1050 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1052 fprintf(stderr, _("could not start pb-discover, the petitboot "
1054 goto fail_client_init;
1057 if (!cui->client && !timeout) {
1058 /* Have the first timeout fire immediately so we can check
1059 * for the server as soon as the UI is ready */
1060 waiter_register_timeout(cui->waitset, 0,
1061 cui_server_wait, cui);
1062 } else if (!cui->client) {
1063 pb_log("%s: discover_client_init failed.\n", __func__);
1064 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1066 fprintf(stderr, _("check that pb-discover, "
1067 "the petitboot daemon is running.\n"));
1068 goto fail_client_init;
1072 talloc_steal(cui, cui->client);
1075 cui->main = main_menu_init(cui);
1077 goto fail_client_init;
1079 waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
1080 cui_process_key, cui);
1084 cui->pjs = pjs_init(cui, js_map);
1087 waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
1088 WAIT_IN, cui_process_js, cui);
1100 * cui_run - The main cui program loop.
1101 * @cui: The cui instance.
1102 * @main: The menu to use as the main menu.
1104 * Runs the cui engine. Does not return until indicated to do so by some
1105 * user action, or an error occurs. Frees the cui object on return.
1106 * Returns 0 on success (return to shell), -1 on error (should restart).
1109 int cui_run(struct cui *cui)
1115 cui->current = &cui->main->scr;
1116 cui->default_item = 0;
1118 nc_scr_post(cui->current);
1121 int result = waiter_poll(cui->waitset);
1124 pb_log("%s: poll: %s\n", __func__, strerror(errno));
1131 while (cui->resize) {
1133 cui_handle_resize(cui);
1140 /* Fork a child to tell the server to cancel autoboot */
1143 cui_server_wait_on_exit(cui);
1147 pb_log("Failed to fork child on exit: %m\n");
1150 return cui->abort ? 0 : -1;