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