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