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