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