2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
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.
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.
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
25 #include <sys/ioctl.h>
28 #include "talloc/talloc.h"
29 #include "waiter/waiter.h"
30 #include "ui/common/discover-client.h"
33 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
39 * cui_abort - Signal the main cui program loop to exit.
41 * Sets cui.abort, which causes the cui_run() routine to return.
44 void cui_abort(struct cui *cui)
46 pb_log("%s: exiting\n", __func__);
51 * cui_resize - Signal the main cui program loop to resize
56 void cui_resize(struct cui *cui)
58 pb_log("%s: resizing\n", __func__);
63 * cui_on_exit - A generic main menu ESC callback.
66 void cui_on_exit(struct pmenu *menu)
68 cui_abort(cui_from_pmenu(menu));
72 * cui_run_cmd - A generic cb to run the supplied command.
75 int cui_run_cmd(struct pmenu_item *item)
78 struct cui *cui = cui_from_item(item);
79 const char *const *cmd_argv = item->data;
81 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
85 result = pb_run_cmd(cmd_argv);
88 redrawwin(cui->current->main_ncw);
91 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
92 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
99 * cui_run_kexec - A generic cb to run kexec.
102 static int cui_run_kexec(struct pmenu_item *item)
105 struct cui *cui = cui_from_item(item);
106 struct cui_opt_data *cod = cod_from_item(item);
108 assert(cui->current == &cui->main->scr);
109 assert(cui->on_kexec);
111 pb_log("%s: %s\n", __func__, cod->name);
112 nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
116 result = cui->on_kexec(cui, cod);
119 redrawwin(cui->current->main_ncw);
123 mvaddstr(1, 0, "system is going down now...");
128 pb_log("%s: failed: %s\n", __func__, cod->kd->image);
129 nc_scr_status_printf(cui->current, "Failed: kexec %s", cod->kd->image);
135 * cui_ked_on_exit - The ked on_exit callback.
138 static void cui_ked_on_exit(struct ked *ked, enum ked_result ked_result,
139 struct pb_kexec_data *kd)
141 struct cui *cui = cui_from_arg(ked->scr.ui_ctx);
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);
150 talloc_free(cod->kd);
153 pb_log("%s: updating opt '%s'\n", __func__, cod->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);
159 cui_set_current(cui, &cui->main->scr);
163 if (ked_result == ked_boot) {
164 struct pmenu_item *i = pmenu_find_selected(cui->main);
169 int cui_ked_run(struct pmenu_item *item)
171 struct cui *cui = cui_from_item(item);
174 ked = ked_init(cui, cod_from_item(item)->kd, cui_ked_on_exit);
176 cui_set_current(cui, &ked->scr);
182 * cui_set_current - Set the currently active screen and redraw it.
185 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
189 DBGS("%p -> %p\n", cui->current, scr);
191 assert(cui->current != scr);
197 cui->current->post(cui->current);
202 static int cui_process_key(void *arg)
204 struct cui *cui = cui_from_arg(arg);
206 assert(cui->current);
208 ui_timer_disable(&cui->timer);
209 cui->current->process_key(cui->current);
215 * cui_client_process_socket - Process a socket event from the discover server.
218 static int cui_client_process_socket(void *arg)
220 struct discover_client *client = arg;
222 discover_client_process(client);
227 * cui_handle_timeout - Handle the timeout.
230 static void cui_handle_timeout(struct ui_timer *timer)
232 struct cui *cui = cui_from_timer(timer);
233 struct pmenu_item *i = pmenu_find_selected(cui->main);
237 struct cui_opt_data *cod = cod_from_item(i);
238 assert(cod && (cod->opt_hash == cui->default_item));
245 * cui_handle_resize - Handle the term resize.
248 static void cui_handle_resize(struct cui *cui)
252 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
253 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
257 pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
259 wclear(cui->current->main_ncw);
260 resize_term(ws.ws_row, ws.ws_col);
261 cui->current->resize(cui->current);
263 /* For some reason this makes ncurses redraw the screen */
265 redrawwin(cui->current->main_ncw);
266 wrefresh(cui->current->main_ncw);
270 * cui_on_open - Open new item callback.
273 void cui_on_open(struct pmenu *menu)
275 unsigned int insert_pt;
276 struct pmenu_item *i;
277 struct cui_opt_data *cod;
280 menu->scr.unpost(&menu->scr);
282 /* This disconnects items array from menu. */
284 set_menu_items(menu->ncm, NULL);
286 /* Insert new items at insert_pt. */
288 insert_pt = pmenu_grow(menu, 1);
289 i = pmenu_item_alloc(menu);
291 name = talloc_asprintf(i, "User item %u:", insert_pt);
292 pmenu_item_setup(menu, i, insert_pt, name, NULL);
294 i->on_edit = cui_ked_run;
295 i->on_execute = cui_run_kexec;
296 i->data = cod = talloc_zero(i, struct cui_opt_data);
298 cod->kd = talloc_zero(i, struct pb_kexec_data);
301 /* Re-attach the items array. */
303 set_menu_items(menu->ncm, menu->items);
305 set_current_item(menu->ncm, i->nci);
306 menu->scr.post(&menu->scr);
307 pos_menu_cursor(menu->ncm);
313 * cui_device_add - Client device_add callback.
315 * Creates menu_items for all the device boot_options and inserts those
316 * menu_items into the main menu. Redraws the main menu if it is active.
319 static int cui_device_add(struct device *dev, void *arg)
321 struct cui *cui = cui_from_arg(arg);
323 struct boot_option *opt;
324 unsigned int o_count; /* device opts */
325 unsigned int insert_pt;
328 pb_log("%s: %p %s\n", __func__, dev, dev->id);
330 selected = current_item(cui->main->ncm);
332 if (cui->current == &cui->main->scr)
333 cui->current->unpost(cui->current);
335 /* This disconnects items array from menu. */
337 result = set_menu_items(cui->main->ncm, NULL);
340 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
343 list_for_each_entry(&dev->boot_options, opt, list)
346 /* Insert new items at insert_pt. */
348 insert_pt = pmenu_grow(cui->main, o_count);
350 list_for_each_entry(&dev->boot_options, opt, list) {
351 struct pmenu_item *i;
352 struct cui_opt_data *cod;
356 /* Save the item in opt->ui_info for cui_device_remove() */
358 opt->ui_info = i = pmenu_item_alloc(cui->main);
360 name = talloc_asprintf(i, "%s: %s", opt->name,
363 description = talloc_asprintf(i,
364 " kexec: image='%s' initrd='%s' args='%s'",
365 opt->boot_image_file,
369 pmenu_item_setup(cui->main, i, insert_pt, name, description);
371 i->on_edit = cui_ked_run;
372 i->on_execute = cui_run_kexec;
373 i->data = cod = talloc(i, struct cui_opt_data);
377 cod->opt_hash = pb_opt_hash(dev, opt);
378 cod->name = opt->name;
379 cod->kd = talloc(i, struct pb_kexec_data);
381 cod->kd->image = talloc_strdup(cod->kd, opt->boot_image_file);
382 cod->kd->initrd = talloc_strdup(cod->kd, opt->initrd_file);
383 cod->kd->args = talloc_strdup(cod->kd, opt->boot_args);
387 pb_log("%s: adding opt '%s'\n", __func__, cod->name);
388 pb_log(" image '%s'\n", cod->kd->image);
389 pb_log(" initrd '%s'\n", cod->kd->initrd);
390 pb_log(" args '%s'\n", cod->kd->args);
392 /* If this is the default_item select it and start timer. */
394 if (cod->opt_hash == cui->default_item) {
396 ui_timer_kick(&cui->timer);
400 /* Re-attach the items array. */
402 result = set_menu_items(cui->main->ncm, cui->main->items);
405 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
408 pb_log("%s\n", __func__);
409 pmenu_dump_items(cui->main->items,
410 item_count(cui->main->ncm) + 1);
413 /* FIXME: need to make item visible somehow */
414 menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
415 menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
416 set_current_item(cui->main->ncm, selected);
418 if (cui->current == &cui->main->scr)
419 cui->current->post(cui->current);
425 * cui_device_remove - Client device remove callback.
427 * Removes all the menu_items for the device from the main menu and redraws the
428 * main menu if it is active.
431 static void cui_device_remove(struct device *dev, void *arg)
433 struct cui *cui = cui_from_arg(arg);
435 struct boot_option *opt;
437 pb_log("%s: %p %s\n", __func__, dev, dev->id);
439 if (cui->current == &cui->main->scr)
440 cui->current->unpost(cui->current);
442 /* This disconnects items array from menu. */
444 result = set_menu_items(cui->main->ncm, NULL);
447 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
449 list_for_each_entry(&dev->boot_options, opt, list) {
450 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
451 struct cui_opt_data *cod = cod_from_item(i);
453 assert(pb_protocol_device_cmp(dev, cod->dev));
454 pmenu_remove(cui->main, i);
456 /* If this is the default_item disable timer. */
458 if (cod->opt_hash == cui->default_item)
459 ui_timer_disable(&cui->timer);
462 /* Re-attach the items array. */
464 result = set_menu_items(cui->main->ncm, cui->main->items);
467 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
470 pb_log("%s\n", __func__);
471 pmenu_dump_items(cui->main->items,
472 item_count(cui->main->ncm) + 1);
475 if (cui->current == &cui->main->scr)
476 cui->current->post(cui->current);
479 static struct discover_client_ops cui_client_ops = {
480 .device_add = cui_device_add,
481 .device_remove = cui_device_remove,
485 * cui_init - Setup the cui instance.
486 * @platform_info: A value for the struct cui platform_info member.
488 * Returns a pointer to a struct cui on success, or NULL on error.
490 * Allocates the cui instance, sets up the client and stdin waiters, and
491 * sets up the ncurses menu screen.
494 struct cui *cui_init(void* platform_info,
495 int (*on_kexec)(struct cui *, struct cui_opt_data *))
498 struct discover_client *client;
501 cui = talloc_zero(NULL, struct cui);
504 pb_log("%s: alloc cui failed.\n", __func__);
505 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
509 cui->c_sig = pb_cui_sig;
510 cui->platform_info = platform_info;
511 cui->on_kexec = on_kexec;
512 cui->timer.handle_timeout = cui_handle_timeout;
514 /* Loop here for scripts that just started the server. */
516 for (i = 10; i; i--) {
517 client = discover_client_init(&cui_client_ops, cui);
520 pb_log("%s: waiting for server %d\n", __func__, i);
525 pb_log("%s: discover_client_init failed.\n", __func__);
526 fprintf(stderr, "%s: error: discover_client_init failed.\n",
528 fprintf(stderr, "check that pb-discover, "
529 "the petitboot daemon is running.\n");
530 goto fail_client_init;
536 waiter_register(discover_client_get_fd(client), WAIT_IN,
537 cui_client_process_socket, client);
539 waiter_register(STDIN_FILENO, WAIT_IN, cui_process_key, cui);
550 * cui_run - The main cui program loop.
551 * @cui: The cui instance.
552 * @main: The menu to use as the main menu.
554 * Runs the cui engine. Does not return until indicated to do so by some
555 * user action, or an error occurs. Frees the cui object on return.
556 * Returns 0 on success (return to shell), -1 on error (should restart).
559 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
564 cui->current = &cui->main->scr;
565 cui->default_item = default_item;
567 cui->current->post(cui->current);
570 int result = waiter_poll();
572 if (result < 0 && errno != EINTR) {
573 pb_log("%s: poll: %s\n", __func__, strerror(errno));
580 ui_timer_process_sig(&cui->timer);
582 while (cui->resize) {
584 cui_handle_resize(cui);
590 return cui->abort ? 0 : -1;