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