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