]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.h
discover/grub2: Allow empty statements
[petitboot] / ui / ncurses / nc-menu.h
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 #if !defined(_PB_NC_MENU_H)
20 #define _PB_NC_MENU_H
21
22 #include <linux/input.h> /* This must be included before ncurses.h */
23 #if defined HAVE_NCURSESW_MENU_H
24 #  include <ncursesw/menu.h>
25 #elif defined HAVE_NCURSES_MENU_H
26 #  include <ncurses/menu.h>
27 #elif defined HAVE_MENU_H
28 #  include <menu.h>
29 #else
30 #  error "Curses menu.h not found."
31 #endif
32
33 #include "log/log.h"
34 #include "types/types.h"
35 #include "nc-scr.h"
36
37 struct pmenu;
38
39 /**
40  * struct pmenu_item - Hold the state of a single menu item.
41  * @i_sig: Signature for callback type checking, should be pmenu_item_sig.
42  * @nci: The ncurses menu item instance for this item.
43  */
44
45 struct pmenu_item {
46         enum pb_nc_sig i_sig;
47         ITEM *nci;
48         struct pmenu *pmenu;
49         void *data;
50         int (*on_edit)(struct pmenu_item *item);
51         int (*on_execute)(struct pmenu_item *item);
52 };
53
54 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu);
55 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
56         unsigned int index, const char *name);
57 int pmenu_item_replace(struct pmenu_item *i, const char *name);
58 void pmenu_item_delete(struct pmenu_item *item);
59
60 static inline struct pmenu_item *pmenu_item_from_arg(void *arg)
61 {
62         struct pmenu_item *item = (struct pmenu_item *)arg;
63
64         assert(item->i_sig == pb_item_sig);
65         return item;
66 }
67
68 static inline struct pmenu_item *pmenu_item_init(struct pmenu *menu,
69         unsigned int index, const char *name)
70 {
71         return pmenu_item_setup(menu, pmenu_item_alloc(menu), index, name);
72 }
73
74 /**
75  * struct pmenu - Data structure defining complete menu.
76  * @insert_pt: Index in nc item array.
77  * @ncm: The ncurses menu instance for this menu.
78  */
79
80 struct pmenu {
81         struct nc_scr scr;
82         MENU *ncm;
83         ITEM **items;
84         unsigned int item_count;
85         unsigned int insert_pt;
86         int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c);
87         void (*on_exit)(struct pmenu *menu);
88         void (*on_open)(struct pmenu *menu);
89 };
90
91 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
92         void (*on_exit)(struct pmenu *));
93 int pmenu_setup(struct pmenu *menu);
94 void pmenu_delete(struct pmenu *menu);
95 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count);
96 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item);
97 struct pmenu_item *pmenu_find_selected(struct pmenu *menu);
98
99 /* convenience routines */
100
101 int pmenu_exit_cb(struct pmenu_item *item);
102
103 static inline struct pmenu *pmenu_from_scr(struct nc_scr *scr)
104 {
105         struct pmenu *pmenu;
106
107         assert(scr->sig == pb_pmenu_sig);
108         pmenu = (struct pmenu *)((char *)scr
109                 - (size_t)&((struct pmenu *)0)->scr);
110         assert(pmenu->scr.sig == pb_pmenu_sig);
111
112         return pmenu;
113 }
114
115 /* debug routines */
116
117 #if defined(DEBUG)
118 enum {do_debug = 1};
119 #else
120 enum {do_debug = 0};
121 #endif
122
123 static inline void pmenu_dump_item(const ITEM *item)
124 {
125         if (do_debug)
126                 pb_log("%p %s\n", item, (item ? item->name.str : "(null)"));
127 }
128
129 static inline void pmenu_dump_items(ITEM *const *items, unsigned int count)
130 {
131         unsigned int i;
132
133         if (do_debug)
134                 for (i = 0; i < count; i++)
135                         pb_log("%u: %p %s\n", i, items[i],
136                                 (items[i] ? items[i]->name.str : "(null)"));
137 }
138
139 #endif