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