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