]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-cui.c
discover: Bring down configured interfaces on discover server exit.
[petitboot] / ui / ncurses / nc-cui.c
index 33a9f73e81b966f50e2a11a1793edb94be3c3793..e3422734ebd06d83ec30415b8012c89beb54435a 100644 (file)
@@ -16,9 +16,9 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#if defined(HAVE_CONFIG_H)
 #include "config.h"
-
-#define _GNU_SOURCE
+#endif
 
 #include <assert.h>
 #include <errno.h>
 #include "waiter/waiter.h"
 #include "process/process.h"
 #include "ui/common/discover-client.h"
+#include "ui/common/ui-system.h"
 #include "nc-cui.h"
+#include "nc-boot-editor.h"
+#include "nc-config.h"
+#include "nc-sysinfo.h"
+
+static void cui_start(void)
+{
+       initscr();                      /* Initialize ncurses. */
+       cbreak();                       /* Disable line buffering. */
+       noecho();                       /* Disable getch() echo. */
+       keypad(stdscr, TRUE);           /* Enable num keypad keys. */
+       nonl();                         /* Disable new-line translation. */
+       intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
+       curs_set(0);                    /* Make cursor invisible */
+       nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
+
+       /* We may be operating with an incorrect $TERM type; in this case
+        * the keymappings will be slightly broken. We want at least
+        * backspace to work though, so we'll define both DEL and ^H to
+        * map to backspace */
+       define_key("\x7f", KEY_BACKSPACE);
+       define_key("\x08", KEY_BACKSPACE);
+
+       while (getch() != ERR)          /* flush stdin */
+               (void)0;
+}
 
-static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
+static void cui_atexit(void)
 {
-       return item->data;
+       clear();
+       refresh();
+       endwin();
 }
 
 /**
@@ -60,12 +88,12 @@ void cui_abort(struct cui *cui)
 
 void cui_resize(struct cui *cui)
 {
-       pb_log("%s: resizing\n", __func__);
+       pb_debug("%s: resizing\n", __func__);
        cui->resize = 1;
 }
 
 /**
- * cui_on_exit - A generic main menu ESC callback.
+ * cui_on_exit - A generic main menu exit callback.
  */
 
 void cui_on_exit(struct pmenu *menu)
@@ -112,17 +140,12 @@ static int cui_boot(struct pmenu_item *item)
 
        assert(cui->current == &cui->main->scr);
 
-       pb_log("%s: %s\n", __func__, cod->name);
+       pb_debug("%s: %s\n", __func__, cod->name);
 
        nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
 
-       def_prog_mode();
-
        result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
 
-       reset_prog_mode();
-       redrawwin(cui->current->main_ncw);
-
        if (result) {
                nc_scr_status_printf(cui->current,
                                "Failed: boot %s", cod->bd->image);
@@ -131,29 +154,23 @@ static int cui_boot(struct pmenu_item *item)
        return 0;
 }
 
-/**
- * cui_boot_editor_on_exit - The boot_editor on_exit callback.
- */
-
-static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
-               enum boot_editor_result boot_editor_result,
+static void cui_boot_editor_on_exit(struct cui *cui,
+               struct pmenu_item *item,
                struct pb_boot_data *bd)
 {
-       struct cui *cui = cui_from_pmenu(boot_editor->original_pmenu);
-       struct pmenu_item *item = boot_editor->data;
+       struct pmenu *menu = cui->main;
        struct cui_opt_data *cod;
 
-       if (boot_editor_result != boot_editor_update) {
+       /* Was the edit cancelled? */
+       if (!bd) {
                cui_set_current(cui, &cui->main->scr);
-               talloc_free(boot_editor);
+               talloc_free(cui->boot_editor);
+               cui->boot_editor = NULL;
                return;
        }
 
-       assert(bd);
-
        /* Is this was a new item, we'll need to update the menu */
        if (!item) {
-               struct pmenu *menu = boot_editor->original_pmenu;
                int insert_pt;
 
                /* Detach the items array. */
@@ -172,39 +189,61 @@ static void cui_boot_editor_on_exit(struct boot_editor *boot_editor,
 
                /* Re-attach the items array. */
                set_menu_items(menu->ncm, menu->items);
-               menu->scr.post(&menu->scr);
+               nc_scr_post(&menu->scr);
        } else {
                cod = item->data;
        }
 
        cod->bd = talloc_steal(cod, bd);
 
-       /* FIXME: need to make item visible somehow */
        set_current_item(item->pmenu->ncm, item->nci);
        cui_set_current(cui, &cui->main->scr);
-       talloc_free(boot_editor);
+       talloc_free(cui->boot_editor);
+       cui->boot_editor = NULL;
 }
 
 void cui_item_edit(struct pmenu_item *item)
 {
        struct cui *cui = cui_from_item(item);
-       struct cui_opt_data *cod = cod_from_item(item);
-       struct boot_editor *boot_editor;
-
-       boot_editor = boot_editor_init(item->pmenu, cod->bd,
-                       cui_boot_editor_on_exit);
-       boot_editor->data = item;
-       cui_set_current(cui, &boot_editor->scr);
+       cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
+                                       cui_boot_editor_on_exit);
+       cui_set_current(cui, boot_editor_scr(cui->boot_editor));
 }
 
 void cui_item_new(struct pmenu *menu)
 {
        struct cui *cui = cui_from_pmenu(menu);
-       struct boot_editor *boot_editor;
+       cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
+                                       cui_boot_editor_on_exit);
+       cui_set_current(cui, boot_editor_scr(cui->boot_editor));
+}
+
+static void cui_sysinfo_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->sysinfo_screen);
+       cui->sysinfo_screen = NULL;
+}
+
+void cui_show_sysinfo(struct cui *cui)
+{
+       cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
+                       cui_sysinfo_exit);
+       cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
+}
 
-       boot_editor = boot_editor_init(menu, NULL,
-                       cui_boot_editor_on_exit);
-       cui_set_current(cui, &boot_editor->scr);
+static void cui_config_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->config_screen);
+       cui->config_screen = NULL;
+}
+
+void cui_show_config(struct cui *cui)
+{
+       cui->config_screen = config_screen_init(cui, cui->config,
+                       cui->sysinfo, cui_config_exit);
+       cui_set_current(cui, config_screen_scr(cui->config_screen));
 }
 
 /**
@@ -220,10 +259,11 @@ struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
        assert(cui->current != scr);
 
        old = cui->current;
-       old->unpost(old);
+       nc_scr_unpost(old);
 
        cui->current = scr;
-       cui->current->post(cui->current);
+
+       nc_scr_post(cui->current);
 
        return old;
 }
@@ -232,10 +272,8 @@ static bool process_global_keys(struct cui *cui, int key)
 {
        switch (key) {
        case 0xc:
-               if (cui->current && cui->current->main_ncw) {
-                       redrawwin(cui->current->main_ncw);
-                       wrefresh(cui->current->main_ncw);
-               }
+               if (cui->current && cui->current->main_ncw)
+                       wrefresh(curscr);
                return true;
        }
        return false;
@@ -258,6 +296,8 @@ static int cui_process_key(void *arg)
        for (;;) {
                int c = getch();
 
+               pb_debug("%s: got key %d\n", __func__, c);
+
                if (c == ERR)
                        break;
 
@@ -302,7 +342,7 @@ static void cui_handle_resize(struct cui *cui)
                return;
        }
 
-       pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
+       pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
 
        wclear(cui->current->main_ncw);
        resize_term(ws.ws_row, ws.ws_col);
@@ -331,12 +371,12 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
        ITEM *selected;
        int result;
 
-       pb_log("%s: %p %s\n", __func__, opt, opt->id);
+       pb_debug("%s: %p %s\n", __func__, opt, opt->id);
 
        selected = current_item(cui->main->ncm);
 
        if (cui->current == &cui->main->scr)
-               cui->current->unpost(cui->current);
+               nc_scr_unpost(cui->current);
 
        /* This disconnects items array from menu. */
 
@@ -392,7 +432,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
        set_current_item(cui->main->ncm, selected);
 
        if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               nc_scr_post(cui->current);
 
        return 0;
 }
@@ -413,7 +453,7 @@ static void cui_device_remove(struct device *dev, void *arg)
        pb_log("%s: %p %s\n", __func__, dev, dev->id);
 
        if (cui->current == &cui->main->scr)
-               cui->current->unpost(cui->current);
+               nc_scr_unpost(cui->current);
 
        /* This disconnects items array from menu. */
 
@@ -443,7 +483,7 @@ static void cui_device_remove(struct device *dev, void *arg)
        }
 
        if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               nc_scr_post(cui->current);
 }
 
 static void cui_update_status(struct boot_status *status, void *arg)
@@ -469,22 +509,51 @@ static void cui_update_mm_title(struct cui *cui)
                                " %s", cui->sysinfo->identifier);
 
        if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               nc_scr_post(cui->current);
 }
 
 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
        cui->sysinfo = talloc_steal(cui, sysinfo);
+
+       /* if we're currently displaying the system info screen, inform it
+        * of the updated information. */
+       if (cui->sysinfo_screen)
+               sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
+
+       /* ... and do the same with the config screen... */
+       if (cui->config_screen)
+               config_screen_update(cui->config_screen, cui->config, sysinfo);
+
+       /* ... and the boot editor. */
+       if (cui->boot_editor)
+               boot_editor_update(cui->boot_editor, sysinfo);
+
        cui_update_mm_title(cui);
 }
 
+static void cui_update_config(struct config *config, void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+       cui->config = talloc_steal(cui, config);
+
+       if (cui->config_screen)
+               config_screen_update(cui->config_screen, config, cui->sysinfo);
+}
+
+int cui_send_config(struct cui *cui, struct config *config)
+{
+       return discover_client_send_config(cui->client, config);
+}
+
 static struct discover_client_ops cui_client_ops = {
        .device_add = NULL,
        .boot_option_add = cui_boot_option_add,
        .device_remove = cui_device_remove,
        .update_status = cui_update_status,
        .update_sysinfo = cui_update_sysinfo,
+       .update_config = cui_update_config,
 };
 
 /**
@@ -558,8 +627,8 @@ retry_start:
                goto fail_client_init;
        }
 
-       atexit(nc_atexit);
-       nc_start();
+       atexit(cui_atexit);
+       cui_start();
 
        waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
                        cui_process_key, cui);
@@ -599,7 +668,7 @@ int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
        cui->current = &cui->main->scr;
        cui->default_item = default_item;
 
-       cui->current->post(cui->current);
+       nc_scr_post(cui->current);
 
        while (1) {
                int result = waiter_poll(cui->waitset);
@@ -618,7 +687,7 @@ int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
                }
        }
 
-       nc_atexit();
+       cui_atexit();
 
        return cui->abort ? 0 : -1;
 }