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