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