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