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