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