]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
3f8995eba809c92b46417e2c70be529dbab2fdc5
[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 /**
203  * cui_process_key - Process input on stdin.
204  */
205
206 static int cui_process_key(void *arg)
207 {
208         struct cui *cui = cui_from_arg(arg);
209
210         assert(cui->current);
211
212         ui_timer_disable(&cui->timer);
213         for (;;) {
214                 int c = getch();
215
216                 if (c == ERR)
217                         break;
218
219                 cui->current->process_key(cui->current, c);
220         }
221
222         return 0;
223 }
224
225 /**
226  * cui_process_js - Process joystick events.
227  */
228
229 static int cui_process_js(void *arg)
230 {
231         struct cui *cui = cui_from_arg(arg);
232         int c;
233
234         c = pjs_process_event(cui->pjs);
235
236         if (c) {
237                 ungetch(c);
238                 cui_process_key(arg);
239         }
240
241         return 0;
242 }
243
244 /**
245  * cui_handle_timeout - Handle the timeout.
246  */
247
248 static void cui_handle_timeout(struct ui_timer *timer)
249 {
250         struct cui *cui = cui_from_timer(timer);
251         struct pmenu_item *i = pmenu_find_selected(cui->main);
252
253 #if defined(DEBUG)
254         {
255                 struct cui_opt_data *cod = cod_from_item(i);
256                 assert(cod && (cod->opt_hash == cui->default_item));
257         }
258 #endif
259         i->on_execute(i);
260 }
261
262 /**
263  * cui_handle_resize - Handle the term resize.
264  */
265
266 static void cui_handle_resize(struct cui *cui)
267 {
268         struct winsize ws;
269
270         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
271                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
272                 return;
273         }
274
275         pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
276
277         wclear(cui->current->main_ncw);
278         resize_term(ws.ws_row, ws.ws_col);
279         cui->current->resize(cui->current);
280
281         /* For some reason this makes ncurses redraw the screen */
282         getch();
283         redrawwin(cui->current->main_ncw);
284         wrefresh(cui->current->main_ncw);
285 }
286
287 /**
288  * cui_on_open - Open new item callback.
289  */
290
291 void cui_on_open(struct pmenu *menu)
292 {
293         unsigned int insert_pt;
294         struct pmenu_item *i;
295         struct cui_opt_data *cod;
296
297         menu->scr.unpost(&menu->scr);
298
299         /* This disconnects items array from menu. */
300
301         set_menu_items(menu->ncm, NULL);
302
303         /* Insert new items at insert_pt. */
304
305         insert_pt = pmenu_grow(menu, 1);
306         i = pmenu_item_alloc(menu);
307
308         i->on_edit = cui_boot_editor_run;
309         i->on_execute = cui_boot;
310         i->data = cod = talloc_zero(i, struct cui_opt_data);
311
312         cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
313         cod->bd = talloc_zero(i, struct pb_boot_data);
314
315         pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
316
317         /* Re-attach the items array. */
318
319         set_menu_items(menu->ncm, menu->items);
320
321         menu->scr.post(&menu->scr);
322         set_current_item(menu->ncm, i->nci);
323
324         i->on_edit(i);
325 }
326
327 /**
328  * cui_device_add - Client device_add callback.
329  *
330  * Creates menu_items for all the device boot_options and inserts those
331  * menu_items into the main menu.  Redraws the main menu if it is active.
332  */
333
334 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
335                 void *arg)
336 {
337         struct cui *cui = cui_from_arg(arg);
338         struct cui_opt_data *cod;
339         unsigned int insert_pt;
340         struct pmenu_item *i;
341         ITEM *selected;
342         int result;
343
344         pb_log("%s: %p %s\n", __func__, opt, opt->id);
345
346         selected = current_item(cui->main->ncm);
347
348         if (cui->current == &cui->main->scr)
349                 cui->current->unpost(cui->current);
350
351         /* This disconnects items array from menu. */
352
353         result = set_menu_items(cui->main->ncm, NULL);
354
355         if (result)
356                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
357
358         /* Insert new items at insert_pt. */
359         insert_pt = pmenu_grow(cui->main, 1);
360
361         /* Save the item in opt->ui_info for cui_device_remove() */
362
363         opt->ui_info = i = pmenu_item_alloc(cui->main);
364
365         i->on_edit = cui_boot_editor_run;
366         i->on_execute = cui_boot;
367         i->data = cod = talloc(i, struct cui_opt_data);
368
369         cod->opt = opt;
370         cod->dev = dev;
371         cod->opt_hash = pb_opt_hash(dev, opt);
372         cod->name = opt->name;
373         cod->bd = talloc(i, struct pb_boot_data);
374
375         cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
376         cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
377         cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
378
379         pmenu_item_setup(cui->main, i, insert_pt, cod->name);
380
381         pb_log("%s: adding opt '%s'\n", __func__, cod->name);
382         pb_log("   image  '%s'\n", cod->bd->image);
383         pb_log("   initrd '%s'\n", cod->bd->initrd);
384         pb_log("   args   '%s'\n", cod->bd->args);
385
386         /* If this is the default_item select it and start timer. */
387         if (cod->opt_hash == cui->default_item) {
388                 selected = i->nci;
389                 ui_timer_kick(&cui->timer);
390         }
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                 struct cui_opt_data *cod = cod_from_item(i);
443
444                 assert(pb_protocol_device_cmp(dev, cod->dev));
445                 pmenu_remove(cui->main, i);
446
447                 /* If this is the default_item disable timer. */
448
449                 if (cod->opt_hash == cui->default_item)
450                         ui_timer_disable(&cui->timer);
451         }
452
453         /* Re-attach the items array. */
454
455         result = set_menu_items(cui->main->ncm, cui->main->items);
456
457         if (result)
458                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
459
460         if (0) {
461                 pb_log("%s\n", __func__);
462                 pmenu_dump_items(cui->main->items,
463                         item_count(cui->main->ncm) + 1);
464         }
465
466         if (cui->current == &cui->main->scr)
467                 cui->current->post(cui->current);
468 }
469
470 static void cui_update_status(struct boot_status *status, void *arg)
471 {
472         struct cui *cui = cui_from_arg(arg);
473
474         nc_scr_status_printf(cui->current,
475                         "%s: %s",
476                         status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
477                         status->message);
478
479 }
480
481 static struct discover_client_ops cui_client_ops = {
482         .device_add = NULL,
483         .boot_option_add = cui_boot_option_add,
484         .device_remove = cui_device_remove,
485         .update_status = cui_update_status,
486 };
487
488 /**
489  * cui_init - Setup the cui instance.
490  * @platform_info: A value for the struct cui platform_info member.
491  *
492  * Returns a pointer to a struct cui on success, or NULL on error.
493  *
494  * Allocates the cui instance, sets up the client and stdin waiters, and
495  * sets up the ncurses menu screen.
496  */
497
498 struct cui *cui_init(void* platform_info,
499         int (*js_map)(const struct js_event *e), int start_deamon)
500 {
501         struct cui *cui;
502         unsigned int i;
503
504         cui = talloc_zero(NULL, struct cui);
505
506         if (!cui) {
507                 pb_log("%s: alloc cui failed.\n", __func__);
508                 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
509                 goto fail_alloc;
510         }
511
512         cui->c_sig = pb_cui_sig;
513         cui->platform_info = platform_info;
514         cui->timer.handle_timeout = cui_handle_timeout;
515         cui->waitset = waitset_create(cui);
516
517         /* Loop here for scripts that just started the server. */
518
519 retry_start:
520         for (i = start_deamon ? 2 : 10; i; i--) {
521                 cui->client = discover_client_init(cui->waitset,
522                                 &cui_client_ops, cui);
523                 if (cui->client || !i)
524                         break;
525                 pb_log("%s: waiting for server %d\n", __func__, i);
526                 sleep(1);
527         }
528
529         if (!cui->client && start_deamon) {
530                 int result;
531
532                 start_deamon = 0;
533
534                 result = pb_start_daemon();
535
536                 if (!result)
537                         goto retry_start;
538
539                 pb_log("%s: discover_client_init failed.\n", __func__);
540                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
541                         __func__);
542                 fprintf(stderr, "could not start pb-discover, the petitboot "
543                         "daemon.\n");
544                 goto fail_client_init;
545         }
546
547         if (!cui->client) {
548                 pb_log("%s: discover_client_init failed.\n", __func__);
549                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
550                         __func__);
551                 fprintf(stderr, "check that pb-discover, "
552                         "the petitboot daemon is running.\n");
553                 goto fail_client_init;
554         }
555
556         atexit(nc_atexit);
557         nc_start();
558
559         waiter_register(cui->waitset, STDIN_FILENO, WAIT_IN,
560                         cui_process_key, cui);
561
562         if (js_map) {
563
564                 cui->pjs = pjs_init(cui, js_map);
565
566                 if (cui->pjs)
567                         waiter_register(cui->waitset, pjs_get_fd(cui->pjs),
568                                         WAIT_IN, cui_process_js, cui);
569         }
570
571         return cui;
572
573 fail_client_init:
574         talloc_free(cui);
575 fail_alloc:
576         return NULL;
577 }
578
579 /**
580  * cui_run - The main cui program loop.
581  * @cui: The cui instance.
582  * @main: The menu to use as the main menu.
583  *
584  * Runs the cui engine.  Does not return until indicated to do so by some
585  * user action, or an error occurs.  Frees the cui object on return.
586  * Returns 0 on success (return to shell), -1 on error (should restart).
587  */
588
589 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
590 {
591         assert(main);
592
593         cui->main = main;
594         cui->current = &cui->main->scr;
595         cui->default_item = default_item;
596
597         cui->current->post(cui->current);
598
599         while (1) {
600                 int result = waiter_poll(cui->waitset);
601
602                 if (result < 0 && errno != EINTR) {
603                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
604                         break;
605                 }
606
607                 if (cui->abort)
608                         break;
609
610                 ui_timer_process_sig(&cui->timer);
611
612                 while (cui->resize) {
613                         cui->resize = 0;
614                         cui_handle_resize(cui);
615                 }
616         }
617
618         nc_atexit();
619
620         return cui->abort ? 0 : -1;
621 }