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