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