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