]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
4f03409fac2ed81723ca7e3dcdd607f6f30a0a78
[petitboot] / ui / ncurses / nc-cui.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28
29 #include "log/log.h"
30 #include "pb-protocol/pb-protocol.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "process/process.h"
34 #include "i18n/i18n.h"
35 #include "ui/common/discover-client.h"
36 #include "ui/common/ui-system.h"
37 #include "nc-cui.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-sysinfo.h"
41 #include "nc-lang.h"
42 #include "nc-helpscreen.h"
43
44 extern const struct help_text main_menu_help_text;
45
46 static struct pmenu *main_menu_init(struct cui *cui);
47
48 static void cui_start(void)
49 {
50         initscr();                      /* Initialize ncurses. */
51         cbreak();                       /* Disable line buffering. */
52         noecho();                       /* Disable getch() echo. */
53         keypad(stdscr, TRUE);           /* Enable num keypad keys. */
54         nonl();                         /* Disable new-line translation. */
55         intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
56         curs_set(0);                    /* Make cursor invisible */
57         nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
58
59         /* We may be operating with an incorrect $TERM type; in this case
60          * the keymappings will be slightly broken. We want at least
61          * backspace to work though, so we'll define both DEL and ^H to
62          * map to backspace */
63         define_key("\x7f", KEY_BACKSPACE);
64         define_key("\x08", KEY_BACKSPACE);
65
66         /* we need backtab too, for form navigation. vt220 doesn't include
67          * this (kcbt), but we don't want to require a full linux/xterm termcap
68          */
69         define_key("\x1b[Z", KEY_BTAB);
70
71         while (getch() != ERR)          /* flush stdin */
72                 (void)0;
73 }
74
75 static void cui_atexit(void)
76 {
77         clear();
78         refresh();
79         endwin();
80 }
81
82 /**
83  * cui_abort - Signal the main cui program loop to exit.
84  *
85  * Sets cui.abort, which causes the cui_run() routine to return.
86  */
87
88 void cui_abort(struct cui *cui)
89 {
90         pb_log("%s: exiting\n", __func__);
91         cui->abort = 1;
92 }
93
94 /**
95  * cui_resize - Signal the main cui program loop to resize
96  *
97  * Called at SIGWINCH.
98  */
99
100 void cui_resize(struct cui *cui)
101 {
102         pb_debug("%s: resizing\n", __func__);
103         cui->resize = 1;
104 }
105
106 /**
107  * cui_on_exit - A generic main menu exit callback.
108  */
109
110 void cui_on_exit(struct pmenu *menu)
111 {
112         cui_abort(cui_from_pmenu(menu));
113 }
114
115 /**
116  * cui_run_cmd - A generic cb to run the supplied command.
117  */
118
119 int cui_run_cmd(struct pmenu_item *item)
120 {
121         int result;
122         struct cui *cui = cui_from_item(item);
123         const char **cmd_argv = item->data;
124
125         nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
126
127         def_prog_mode();
128
129         result = process_run_simple_argv(item, cmd_argv);
130
131         reset_prog_mode();
132         redrawwin(cui->current->main_ncw);
133
134         if (result) {
135                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
136                 nc_scr_status_printf(cui->current, _("Failed: %s"),
137                                 cmd_argv[0]);
138         }
139
140         return result;
141 }
142
143 /**
144  * cui_boot - A generic cb to run kexec.
145  */
146
147 static int cui_boot(struct pmenu_item *item)
148 {
149         int result;
150         struct cui *cui = cui_from_item(item);
151         struct cui_opt_data *cod = cod_from_item(item);
152
153         assert(cui->current == &cui->main->scr);
154
155         pb_debug("%s: %s\n", __func__, cod->name);
156
157         nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
158
159         result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
160
161         if (result) {
162                 nc_scr_status_printf(cui->current,
163                                 _("Failed: boot %s"), cod->bd->image);
164         }
165
166         return 0;
167 }
168
169 static void cui_boot_editor_on_exit(struct cui *cui,
170                 struct pmenu_item *item,
171                 struct pb_boot_data *bd)
172 {
173         struct pmenu *menu = cui->main;
174         struct cui_opt_data *cod;
175         static int user_idx = 0;
176
177         /* Was the edit cancelled? */
178         if (!bd) {
179                 cui_set_current(cui, &cui->main->scr);
180                 talloc_free(cui->boot_editor);
181                 cui->boot_editor = NULL;
182                 return;
183         }
184
185         /* Is this was a new item, we'll need to update the menu */
186         if (!item) {
187                 int insert_pt;
188
189                 cod = talloc_zero(NULL, struct cui_opt_data);
190                 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
191
192                 item = pmenu_item_create(menu, cod->name);
193                 if (!item) {
194                         talloc_free(cod);
195                         goto out;
196                 }
197
198                 item->on_edit = cui_item_edit;
199                 item->on_execute = cui_boot;
200                 item->data = cod;
201
202                 talloc_steal(item, cod);
203
204                 /* Detach the items array. */
205                 set_menu_items(menu->ncm, NULL);
206
207                 /* Insert new item at insert_pt. */
208                 insert_pt = pmenu_grow(menu, 1);
209                 pmenu_item_insert(menu, item, insert_pt);
210
211                 /* Re-attach the items array. */
212                 set_menu_items(menu->ncm, menu->items);
213                 nc_scr_post(&menu->scr);
214         } else {
215                 cod = item->data;
216         }
217
218         cod->bd = talloc_steal(cod, bd);
219
220         set_current_item(item->pmenu->ncm, item->nci);
221 out:
222         cui_set_current(cui, &cui->main->scr);
223         talloc_free(cui->boot_editor);
224         cui->boot_editor = NULL;
225 }
226
227 void cui_item_edit(struct pmenu_item *item)
228 {
229         struct cui *cui = cui_from_item(item);
230         cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
231                                         cui_boot_editor_on_exit);
232         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
233 }
234
235 void cui_item_new(struct pmenu *menu)
236 {
237         struct cui *cui = cui_from_pmenu(menu);
238         cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
239                                         cui_boot_editor_on_exit);
240         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
241 }
242
243 static void cui_sysinfo_exit(struct cui *cui)
244 {
245         cui_set_current(cui, &cui->main->scr);
246         talloc_free(cui->sysinfo_screen);
247         cui->sysinfo_screen = NULL;
248 }
249
250 void cui_show_sysinfo(struct cui *cui)
251 {
252         cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
253                         cui_sysinfo_exit);
254         cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
255 }
256
257 static void cui_config_exit(struct cui *cui)
258 {
259         cui_set_current(cui, &cui->main->scr);
260         talloc_free(cui->config_screen);
261         cui->config_screen = NULL;
262 }
263
264 void cui_show_config(struct cui *cui)
265 {
266         cui->config_screen = config_screen_init(cui, cui->config,
267                         cui->sysinfo, cui_config_exit);
268         cui_set_current(cui, config_screen_scr(cui->config_screen));
269 }
270
271 static void cui_lang_exit(struct cui *cui)
272 {
273         cui_set_current(cui, &cui->main->scr);
274         talloc_free(cui->lang_screen);
275         cui->lang_screen = NULL;
276 }
277
278 void cui_show_lang(struct cui *cui)
279 {
280         cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
281         cui_set_current(cui, lang_screen_scr(cui->lang_screen));
282 }
283
284 static void cui_help_exit(struct cui *cui)
285 {
286         cui_set_current(cui, help_screen_return_scr(cui->help_screen));
287         talloc_free(cui->help_screen);
288         cui->help_screen = NULL;
289 }
290
291 void cui_show_help(struct cui *cui, const char *title,
292                 const struct help_text *text)
293 {
294         if (!cui->current)
295                 return;
296
297         if (cui->help_screen)
298                 return;
299
300         cui->help_screen = help_screen_init(cui, cui->current,
301                         title, text, cui_help_exit);
302
303         if (cui->help_screen)
304                 cui_set_current(cui, help_screen_scr(cui->help_screen));
305 }
306
307 /**
308  * cui_set_current - Set the currently active screen and redraw it.
309  */
310
311 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
312 {
313         struct nc_scr *old;
314
315         DBGS("%p -> %p\n", cui->current, scr);
316
317         assert(cui->current != scr);
318
319         old = cui->current;
320         nc_scr_unpost(old);
321
322         cui->current = scr;
323
324         nc_scr_post(cui->current);
325
326         return old;
327 }
328
329 static bool process_global_keys(struct cui *cui, int key)
330 {
331         switch (key) {
332         case 0xc:
333                 if (cui->current && cui->current->main_ncw)
334                         wrefresh(curscr);
335                 return true;
336         }
337         return false;
338 }
339
340 /**
341  * cui_process_key - Process input on stdin.
342  */
343
344 static int cui_process_key(void *arg)
345 {
346         struct cui *cui = cui_from_arg(arg);
347
348         assert(cui->current);
349
350         for (;;) {
351                 int c = getch();
352
353                 pb_debug("%s: got key %d\n", __func__, c);
354
355                 if (c == ERR)
356                         break;
357
358                 if (!cui->has_input) {
359                         pb_log("UI input received (key = %d), aborting "
360                                         "default boot\n", c);
361                         discover_client_cancel_default(cui->client);
362                         cui->has_input = true;
363                 }
364
365                 if (process_global_keys(cui, c))
366                         continue;
367
368                 cui->current->process_key(cui->current, c);
369         }
370
371         return 0;
372 }
373
374 /**
375  * cui_process_js - Process joystick events.
376  */
377
378 static int cui_process_js(void *arg)
379 {
380         struct cui *cui = cui_from_arg(arg);
381         int c;
382
383         c = pjs_process_event(cui->pjs);
384
385         if (c) {
386                 ungetch(c);
387                 cui_process_key(arg);
388         }
389
390         return 0;
391 }
392
393 /**
394  * cui_handle_resize - Handle the term resize.
395  */
396
397 static void cui_handle_resize(struct cui *cui)
398 {
399         struct winsize ws;
400
401         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
402                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
403                 return;
404         }
405
406         pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
407
408         wclear(cui->current->main_ncw);
409         resize_term(ws.ws_row, ws.ws_col);
410         cui->current->resize(cui->current);
411
412         /* For some reason this makes ncurses redraw the screen */
413         getch();
414         redrawwin(cui->current->main_ncw);
415         wrefresh(cui->current->main_ncw);
416 }
417
418 /**
419  * cui_device_add - Client device_add callback.
420  *
421  * Creates menu_items for all the device boot_options and inserts those
422  * menu_items into the main menu.  Redraws the main menu if it is active.
423  */
424
425 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
426                 void *arg)
427 {
428         struct pmenu_item *i, *dev_hdr = NULL;
429         struct cui *cui = cui_from_arg(arg);
430         struct cui_opt_data *cod;
431         const char *tab = "  ";
432         unsigned int insert_pt;
433         int result, rows, cols;
434         ITEM *selected;
435         char *name;
436
437         pb_debug("%s: %p %s\n", __func__, opt, opt->id);
438
439         selected = current_item(cui->main->ncm);
440         menu_format(cui->main->ncm, &rows, &cols);
441
442         if (cui->current == &cui->main->scr)
443                 nc_scr_unpost(cui->current);
444
445         /* Check if the boot device is new */
446         dev_hdr = pmenu_find_device(cui->main, dev, opt);
447
448         /* All actual boot entries are 'tabbed' across */
449         name = talloc_asprintf(cui->main, "%s%s",
450                         tab, opt->name ? : "Unknown Name");
451
452         /* Save the item in opt->ui_info for cui_device_remove() */
453         opt->ui_info = i = pmenu_item_create(cui->main, name);
454         talloc_free(name);
455         if (!i)
456                 return -1;
457
458         i->on_edit = cui_item_edit;
459         i->on_execute = cui_boot;
460         i->data = cod = talloc(i, struct cui_opt_data);
461
462         cod->opt = opt;
463         cod->dev = dev;
464         cod->opt_hash = pb_opt_hash(dev, opt);
465         cod->name = opt->name;
466         cod->bd = talloc(i, struct pb_boot_data);
467
468         cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
469         cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
470         cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
471         cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
472
473         /* This disconnects items array from menu. */
474         result = set_menu_items(cui->main->ncm, NULL);
475
476         if (result)
477                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
478
479         /* Insert new items at insert_pt. */
480         if (dev_hdr) {
481                 insert_pt = pmenu_grow(cui->main, 2);
482                 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
483                 pb_log("%s: adding new device hierarchy %s\n",
484                         __func__,opt->device_id);
485                 pmenu_item_insert(cui->main, i, insert_pt+1);
486         } else {
487                 insert_pt = pmenu_grow(cui->main, 1);
488                 pmenu_item_add(cui->main, i, insert_pt);
489         }
490
491         pb_log("%s: adding opt '%s'\n", __func__, cod->name);
492         pb_log("   image  '%s'\n", cod->bd->image);
493         pb_log("   initrd '%s'\n", cod->bd->initrd);
494         pb_log("   args   '%s'\n", cod->bd->args);
495
496         /* Re-attach the items array. */
497         result = set_menu_items(cui->main->ncm, cui->main->items);
498
499         if (result)
500                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
501
502         if (0) {
503                 pb_log("%s\n", __func__);
504                 pmenu_dump_items(cui->main->items,
505                         item_count(cui->main->ncm) + 1);
506         }
507
508         if (!item_visible(selected)) {
509                 int idx, top;
510
511                 top = top_row(cui->main->ncm);
512                 idx = item_index(selected);
513
514                 /* If our index is above the current top row, align
515                  * us to the new top. Otherwise, align us to the new
516                  * bottom */
517                 top = top < idx ? idx - rows : idx;
518
519                 set_top_row(cui->main->ncm, top);
520                 set_current_item(cui->main->ncm, selected);
521         }
522
523         if (cui->current == &cui->main->scr)
524                 nc_scr_post(cui->current);
525
526         return 0;
527 }
528
529 /**
530  * cui_device_remove - Client device remove callback.
531  *
532  * Removes all the menu_items for the device from the main menu and redraws the
533  * main menu if it is active.
534  */
535
536 static void cui_device_remove(struct device *dev, void *arg)
537 {
538         struct cui *cui = cui_from_arg(arg);
539         struct boot_option *opt;
540         unsigned int i;
541         int result;
542
543         pb_log("%s: %p %s\n", __func__, dev, dev->id);
544
545         if (cui->current == &cui->main->scr)
546                 nc_scr_unpost(cui->current);
547
548         /* This disconnects items array from menu. */
549
550         result = set_menu_items(cui->main->ncm, NULL);
551
552         if (result)
553                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
554
555         list_for_each_entry(&dev->boot_options, opt, list) {
556                 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
557
558                 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
559                 pmenu_remove(cui->main, item);
560         }
561
562         /* Manually remove remaining device hierarchy item */
563         for (i=0; i < cui->main->item_count; i++) {
564                 struct pmenu_item *item = item_userptr(cui->main->items[i]);
565                 if (!item || !item->data )
566                         continue;
567
568                 struct cui_opt_data *data = item->data;
569                 if (data && data->dev && data->dev == dev)
570                         pmenu_remove(cui->main,item);
571         }
572
573         /* Re-attach the items array. */
574
575         result = set_menu_items(cui->main->ncm, cui->main->items);
576
577         if (result)
578                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
579
580         if (0) {
581                 pb_log("%s\n", __func__);
582                 pmenu_dump_items(cui->main->items,
583                         item_count(cui->main->ncm) + 1);
584         }
585
586         if (cui->current == &cui->main->scr)
587                 nc_scr_post(cui->current);
588 }
589
590 static void cui_update_status(struct boot_status *status, void *arg)
591 {
592         struct cui *cui = cui_from_arg(arg);
593
594         nc_scr_status_printf(cui->current,
595                         "%s: %s",
596                         status->type == BOOT_STATUS_ERROR ?
597                                 _("Error") : _("Info"),
598                         status->message);
599
600 }
601
602 static void cui_update_mm_title(struct cui *cui)
603 {
604         struct nc_frame *frame = &cui->main->scr.frame;
605
606         talloc_free(frame->rtitle);
607
608         frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
609         if (cui->sysinfo->identifier)
610                 frame->rtitle = talloc_asprintf_append(frame->rtitle,
611                                 " %s", cui->sysinfo->identifier);
612
613         if (cui->current == &cui->main->scr)
614                 nc_scr_post(cui->current);
615 }
616
617 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
618 {
619         struct cui *cui = cui_from_arg(arg);
620         cui->sysinfo = talloc_steal(cui, sysinfo);
621
622         /* if we're currently displaying the system info screen, inform it
623          * of the updated information. */
624         if (cui->sysinfo_screen)
625                 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
626
627         /* ... and do the same with the config screen... */
628         if (cui->config_screen)
629                 config_screen_update(cui->config_screen, cui->config, sysinfo);
630
631         /* ... and the boot editor. */
632         if (cui->boot_editor)
633                 boot_editor_update(cui->boot_editor, sysinfo);
634
635         cui_update_mm_title(cui);
636 }
637
638 static void cui_update_language(struct cui *cui, char *lang)
639 {
640         bool repost_menu;
641         char *cur_lang;
642
643         cur_lang = setlocale(LC_ALL, NULL);
644         if (cur_lang && !strcmp(cur_lang, lang))
645                 return;
646
647         setlocale(LC_ALL, lang);
648
649         /* we'll need to update the menu: drop all items and repopulate */
650         repost_menu = cui->current == &cui->main->scr;
651         if (repost_menu)
652                 nc_scr_unpost(cui->current);
653
654         talloc_free(cui->main);
655         cui->main = main_menu_init(cui);
656
657         if (repost_menu) {
658                 cui->current = &cui->main->scr;
659                 nc_scr_post(cui->current);
660         }
661
662         discover_client_enumerate(cui->client);
663 }
664
665 static void cui_update_config(struct config *config, void *arg)
666 {
667         struct cui *cui = cui_from_arg(arg);
668         cui->config = talloc_steal(cui, config);
669
670         if (config->lang)
671                 cui_update_language(cui, config->lang);
672
673         if (cui->config_screen)
674                 config_screen_update(cui->config_screen, config, cui->sysinfo);
675
676         if (config->safe_mode)
677                 nc_scr_status_printf(cui->current,
678                                 _("SAFE MODE: select '%s' to continue"),
679                                 _("Rescan devices"));
680 }
681
682 int cui_send_config(struct cui *cui, struct config *config)
683 {
684         return discover_client_send_config(cui->client, config);
685 }
686
687 void cui_send_reinit(struct cui *cui)
688 {
689         discover_client_send_reinit(cui->client);
690 }
691
692 static int menu_sysinfo_execute(struct pmenu_item *item)
693 {
694         cui_show_sysinfo(cui_from_item(item));
695         return 0;
696 }
697
698 static int menu_config_execute(struct pmenu_item *item)
699 {
700         cui_show_config(cui_from_item(item));
701         return 0;
702 }
703
704 static int menu_lang_execute(struct pmenu_item *item)
705 {
706         cui_show_lang(cui_from_item(item));
707         return 0;
708 }
709
710 static int menu_reinit_execute(struct pmenu_item *item)
711 {
712         cui_send_reinit(cui_from_item(item));
713         return 0;
714 }
715
716 /**
717  * pb_mm_init - Setup the main menu instance.
718  */
719 static struct pmenu *main_menu_init(struct cui *cui)
720 {
721         struct pmenu_item *i;
722         struct pmenu *m;
723         int result;
724
725         m = pmenu_init(cui, 6, cui_on_exit);
726         if (!m) {
727                 pb_log("%s: failed\n", __func__);
728                 return NULL;
729         }
730
731         m->on_new = cui_item_new;
732
733         m->scr.frame.ltitle = talloc_asprintf(m,
734                 "Petitboot (" PACKAGE_VERSION ")");
735         m->scr.frame.rtitle = NULL;
736         m->scr.frame.help = talloc_strdup(m,
737                 _("Enter=accept, e=edit, n=new, x=exit, l=language, h=help"));
738         m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
739
740         /* add a separator */
741         i = pmenu_item_create(m, " ");
742         item_opts_off(i->nci, O_SELECTABLE);
743         pmenu_item_insert(m, i, 0);
744
745         /* add system items */
746         i = pmenu_item_create(m, _("System information"));
747         i->on_execute = menu_sysinfo_execute;
748         pmenu_item_insert(m, i, 1);
749
750         i = pmenu_item_create(m, _("System configuration"));
751         i->on_execute = menu_config_execute;
752         pmenu_item_insert(m, i, 2);
753
754         /* this label isn't translated, so we don't want a gettext() here */
755         i = pmenu_item_create(m, "Language");
756         i->on_execute = menu_lang_execute;
757         pmenu_item_insert(m, i, 3);
758
759         i = pmenu_item_create(m, _("Rescan devices"));
760         i->on_execute = menu_reinit_execute;
761         pmenu_item_insert(m, i, 4);
762
763         i = pmenu_item_create(m, _("Exit to shell"));
764         i->on_execute = pmenu_exit_cb;
765         pmenu_item_insert(m, i, 5);
766
767         result = pmenu_setup(m);
768
769         if (result) {
770                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
771                         strerror(errno));
772                 goto fail_setup;
773         }
774
775         m->help_title = _("main menu");
776         m->help_text = &main_menu_help_text;
777
778         menu_opts_off(m->ncm, O_SHOWDESC);
779         set_menu_mark(m->ncm, " *");
780         set_current_item(m->ncm, i->nci);
781
782         return m;
783
784 fail_setup:
785         talloc_free(m);
786         return NULL;
787 }
788
789 static struct discover_client_ops cui_client_ops = {
790         .device_add = NULL,
791         .boot_option_add = cui_boot_option_add,
792         .device_remove = cui_device_remove,
793         .update_status = cui_update_status,
794         .update_sysinfo = cui_update_sysinfo,
795         .update_config = cui_update_config,
796 };
797
798 /**
799  * cui_init - Setup the cui instance.
800  * @platform_info: A value for the struct cui platform_info member.
801  *
802  * Returns a pointer to a struct cui on success, or NULL on error.
803  *
804  * Allocates the cui instance, sets up the client and stdin waiters, and
805  * sets up the ncurses menu screen.
806  */
807
808 struct cui *cui_init(void* platform_info,
809         int (*js_map)(const struct js_event *e), int start_deamon)
810 {
811         struct cui *cui;
812         unsigned int i;
813
814         cui = talloc_zero(NULL, struct cui);
815         if (!cui) {
816                 pb_log("%s: alloc cui failed.\n", __func__);
817                 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
818                 goto fail_alloc;
819         }
820
821         cui->c_sig = pb_cui_sig;
822         cui->platform_info = platform_info;
823         cui->waitset = waitset_create(cui);
824
825         process_init(cui, cui->waitset, false);
826
827         /* Loop here for scripts that just started the server. */
828
829 retry_start:
830         for (i = start_deamon ? 2 : 10; i; i--) {
831                 cui->client = discover_client_init(cui->waitset,
832                                 &cui_client_ops, cui);
833                 if (cui->client || !i)
834                         break;
835                 pb_log("%s: waiting for server %d\n", __func__, i);
836                 sleep(1);
837         }
838
839         if (!cui->client && start_deamon) {
840                 int result;
841
842                 start_deamon = 0;
843
844                 result = pb_start_daemon(cui);
845
846                 if (!result)
847                         goto retry_start;
848
849                 pb_log("%s: discover_client_init failed.\n", __func__);
850                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
851                         __func__);
852                 fprintf(stderr, _("could not start pb-discover, the petitboot "
853                         "daemon.\n"));
854                 goto fail_client_init;
855         }
856
857         if (!cui->client) {
858                 pb_log("%s: discover_client_init failed.\n", __func__);
859                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
860                         __func__);
861                 fprintf(stderr, _("check that pb-discover, "
862                         "the petitboot daemon is running.\n"));
863                 goto fail_client_init;
864         }
865
866         atexit(cui_atexit);
867         talloc_steal(cui, cui->client);
868         cui_start();
869
870         cui->main = main_menu_init(cui);
871         if (!cui->main)
872                 goto fail_client_init;
873
874         waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
875                         cui_process_key, cui);
876
877         if (js_map) {
878
879                 cui->pjs = pjs_init(cui, js_map);
880
881                 if (cui->pjs)
882                         waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
883                                         WAIT_IN, cui_process_js, cui);
884         }
885
886         return cui;
887
888 fail_client_init:
889         talloc_free(cui);
890 fail_alloc:
891         return NULL;
892 }
893
894 /**
895  * cui_run - The main cui program loop.
896  * @cui: The cui instance.
897  * @main: The menu to use as the main menu.
898  *
899  * Runs the cui engine.  Does not return until indicated to do so by some
900  * user action, or an error occurs.  Frees the cui object on return.
901  * Returns 0 on success (return to shell), -1 on error (should restart).
902  */
903
904 int cui_run(struct cui *cui)
905 {
906         assert(main);
907
908         cui->current = &cui->main->scr;
909         cui->default_item = 0;
910
911         nc_scr_post(cui->current);
912
913         while (1) {
914                 int result = waiter_poll(cui->waitset);
915
916                 if (result < 0) {
917                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
918                         break;
919                 }
920
921                 if (cui->abort)
922                         break;
923
924                 while (cui->resize) {
925                         cui->resize = 0;
926                         cui_handle_resize(cui);
927                 }
928         }
929
930         cui_atexit();
931
932         return cui->abort ? 0 : -1;
933 }