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