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