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