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