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