summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/nc-menu.h
diff options
context:
space:
mode:
authorGeoff Levand <geoffrey.levand@am.sony.com>2009-04-12 15:11:57 +0000
committerJeremy Kerr <jk@ozlabs.org>2009-06-30 15:29:25 +0800
commit863c609797ca6d556dd3a4586fc07a6f4a5472d4 (patch)
treea4727c82acc5795b56962928c29fa4b113fb3d82 /ui/ncurses/nc-menu.h
parent24990ffe86e0fa80dff2da365541ae67e427c57a (diff)
downloadtalos-petitboot-863c609797ca6d556dd3a4586fc07a6f4a5472d4.tar.gz
talos-petitboot-863c609797ca6d556dd3a4586fc07a6f4a5472d4.zip
Add ncurses UI menu
Add support for an ncurses UI menu object. Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'ui/ncurses/nc-menu.h')
-rw-r--r--ui/ncurses/nc-menu.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/ui/ncurses/nc-menu.h b/ui/ncurses/nc-menu.h
new file mode 100644
index 0000000..b487df9
--- /dev/null
+++ b/ui/ncurses/nc-menu.h
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2009 Sony Computer Entertainment Inc.
+ * Copyright 2009 Sony Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#if !defined(_PB_NC_MENU_H)
+#define _PB_NC_MENU_H
+
+#include <assert.h>
+#include <menu.h>
+
+#include "log/log.h"
+#include "pb-protocol/pb-protocol.h"
+#include "nc-scr.h"
+
+struct pmenu;
+
+/**
+ * struct pmenu_item - Hold the state of a single menu item.
+ * @i_sig: Signature for callback type checking, should be pmenu_item_sig.
+ * @nci: The ncurses menu item instance for this item.
+ */
+
+struct pmenu_item {
+ enum pb_nc_sig i_sig;
+ ITEM *nci;
+ struct pmenu *pmenu;
+ void *data;
+ int (*on_edit)(struct pmenu_item *item);
+ int (*on_execute)(struct pmenu_item *item);
+};
+
+struct pmenu_item *pmenu_item_alloc(struct pmenu *menu);
+struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
+ unsigned int index, const char *name, const char *description);
+void pmenu_item_delete(struct pmenu_item *item);
+
+static inline struct pmenu_item *pmenu_item_from_arg(void *arg)
+{
+ struct pmenu_item *item = (struct pmenu_item *)arg;
+
+ assert(item->i_sig == pb_item_sig);
+ return item;
+}
+
+static inline struct pmenu_item *pmenu_item_init(struct pmenu *menu,
+ unsigned int index, const char *name, const char *description)
+{
+ return pmenu_item_setup(menu, pmenu_item_alloc(menu), index, name,
+ description);
+}
+
+/**
+ * struct pmenu - Data structure defining complete menu.
+ * @insert_pt: Index in nc item array.
+ * @ncm: The ncurses menu instance for this menu.
+ */
+
+struct pmenu {
+ struct nc_scr scr;
+ MENU *ncm;
+ ITEM **items;
+ unsigned int item_count;
+ unsigned int insert_pt;
+ int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c);
+ void (*on_exit)(struct pmenu *menu);
+};
+
+struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
+ void (*on_exit)(struct pmenu *));
+int pmenu_setup(struct pmenu *menu);
+void pmenu_delete(struct pmenu *menu);
+unsigned int pmenu_grow(struct pmenu *menu, unsigned int count);
+int pmenu_remove(struct pmenu *menu, struct pmenu_item *item);
+struct pmenu_item *pmenu_find_selected(struct pmenu *menu);
+
+/* convenience routines */
+
+int pmenu_exit_cb(struct pmenu_item *item);
+
+static inline struct pmenu *pmenu_from_scr(struct nc_scr *scr)
+{
+ struct pmenu *pmenu;
+
+ assert(scr->sig == pb_pmenu_sig);
+ pmenu = (struct pmenu *)((char *)scr
+ - (size_t)&((struct pmenu *)0)->scr);
+ assert(pmenu->scr.sig == pb_pmenu_sig);
+
+ return pmenu;
+}
+
+/* debug routines */
+
+#if defined(DEBUG)
+enum {do_debug = 1};
+#else
+enum {do_debug = 0};
+#endif
+
+static inline void pmenu_dump_item(const ITEM *item)
+{
+ if (do_debug)
+ pb_log("%p %s\n", item, (item ? item->name.str : "(null)"));
+}
+
+static inline void pmenu_dump_items(ITEM *const *items, unsigned int count)
+{
+ unsigned int i;
+
+ if (do_debug)
+ for (i = 0; i < count; i++)
+ pb_log("%u: %p %s\n", i, items[i],
+ (items[i] ? items[i]->name.str : "(null)"));
+}
+
+#endif
OpenPOWER on IntegriCloud