]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.c
Remove nc menu description
[petitboot] / ui / ncurses / nc-menu.c
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 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <string.h>
24
25 #include "log/log.h"
26 #include "talloc/talloc.h"
27 #include "ui/common/ui-system.h"
28 #include "nc-menu.h"
29
30 /**
31  * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
32  */
33
34 int pmenu_exit_cb(struct pmenu_item *item)
35 {
36         assert(item->pmenu->on_exit);
37         item->pmenu->on_exit(item->pmenu);
38         return 0;
39 }
40
41 /**
42  * pmenu_find_selected - Find the selected pmenu_item.
43  */
44
45 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
46 {
47         return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
48 }
49
50 static int pmenu_post(struct nc_scr *scr)
51 {
52         int result;
53         struct pmenu *menu = pmenu_from_scr(scr);
54
55         result = post_menu(menu->ncm);
56
57         nc_scr_frame_draw(scr);
58         redrawwin(menu->scr.main_ncw);
59         wrefresh(menu->scr.main_ncw);
60
61         return result;
62 }
63
64 static int pmenu_unpost(struct nc_scr *scr)
65 {
66         return unpost_menu(pmenu_from_scr(scr)->ncm);
67 }
68
69 static void pmenu_resize(struct nc_scr *scr)
70 {
71         /* FIXME: menus can't be resized, need to recreate here */
72         pmenu_unpost(scr);
73         pmenu_post(scr);
74 }
75
76 /**
77  * pmenu_item_init - Allocate and initialize a new pmenu_item instance.
78  *
79  * Returns a pointer the the initialized struct pmenu_item instance or NULL
80  * on error. The caller is responsible for calling talloc_free() for the
81  * returned instance.
82  */
83
84 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu)
85 {
86         /* Items go with the menu, not the pointer array. */
87
88         struct pmenu_item *i = talloc_zero(menu, struct pmenu_item);
89
90         return i;
91 }
92
93 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
94         unsigned int index, const char *name)
95 {
96         assert(i);
97
98         if (!i)
99                 return NULL;
100
101         i->i_sig = pb_item_sig;
102         i->pmenu = menu;
103         i->nci = new_item(name, NULL);
104
105         if (!i->nci) {
106                 talloc_free(i);
107                 return NULL;
108         }
109
110         set_item_userptr(i->nci, i);
111
112         menu->items[index] = i->nci;
113
114         return i;
115 }
116
117 /**
118  * pmenu_move_cursor - Move the cursor.
119  * @req: An ncurses request or char to send to menu_driver().
120  */
121
122 static void pmenu_move_cursor(struct pmenu *menu, int req)
123 {
124         menu_driver(menu->ncm, req);
125         wrefresh(menu->scr.main_ncw);
126 }
127
128 /**
129  * pmenu_process_key - Process a user keystroke.
130  */
131
132 static void pmenu_process_key(struct nc_scr *scr)
133 {
134         struct pmenu *menu = pmenu_from_scr(scr);
135         struct pmenu_item *item = pmenu_find_selected(menu);
136
137         nc_scr_status_free(&menu->scr);
138
139         while (1) {
140                 int c = getch();
141
142                 if (c == ERR)
143                         return;
144
145                 /* DBGS("%d (%o)\n", c, c); */
146
147                 if (menu->hot_key)
148                         c = menu->hot_key(menu, item, c);
149
150                 switch (c) {
151                 case 27: /* ESC */
152                         if (menu->on_exit)
153                                 menu->on_exit(menu);
154                         nc_flush_keys();
155                         return;
156
157                 case KEY_PPAGE:
158                         pmenu_move_cursor(menu, REQ_SCR_UPAGE);
159                         break;
160                 case KEY_NPAGE:
161                         pmenu_move_cursor(menu, REQ_SCR_DPAGE);
162                         break;
163                 case KEY_HOME:
164                         pmenu_move_cursor(menu, REQ_FIRST_ITEM);
165                         break;
166                 case KEY_END:
167                         pmenu_move_cursor(menu, REQ_LAST_ITEM);
168                         break;
169                 case KEY_UP:
170                         pmenu_move_cursor(menu, REQ_UP_ITEM);
171                         break;
172                 case KEY_DOWN:
173                 case '\t':
174                         pmenu_move_cursor(menu, REQ_DOWN_ITEM);
175                         break;
176                 case KEY_LEFT:
177                 case 'e':
178                         if (item->on_edit)
179                                 item->on_edit(item);
180                         break;
181                 case 'o':
182                         DBGS("on_open: %p\n", menu->on_open);
183                         if (menu->on_open)
184                                 menu->on_open(menu);
185                         break;
186                 case '\n':
187                 case '\r':
188                         if (item->on_execute)
189                                 item->on_execute(item);
190                         break;
191                 default:
192                         menu_driver(menu->ncm, c);
193                         break;
194                 }
195         }
196 }
197
198 /**
199  * pmenu_grow - Grow the item array.
200  * @count: The count of new items.
201  *
202  * The item array must be disconnected prior to calling pmenu_grow().
203  * Returns the insert point index.
204  */
205
206 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
207 {
208         unsigned int tmp;
209
210         assert(item_count(menu->ncm) == 0 && "not disconnected");
211
212         pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
213                 count, menu->item_count + count);
214
215         /* Note that items array has a null terminator. */
216
217         menu->items = talloc_realloc(menu, menu->items, ITEM *,
218                 menu->item_count + count + 1);
219
220         memmove(menu->items + menu->insert_pt + count,
221                 menu->items + menu->insert_pt,
222                 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
223
224         memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
225
226         tmp = menu->insert_pt;
227         menu->insert_pt += count;
228         menu->item_count += count;
229
230         return tmp;
231 }
232
233 static int pmenu_item_get_index(const struct pmenu_item *item)
234 {
235         unsigned int i;
236
237         for (i = 0; i < item->pmenu->item_count; i++)
238                 if (item->pmenu->items[i] == item->nci)
239                         return i;
240
241         pb_log("%s: not found: %p %s\n", __func__, item,
242                 (item ? item->nci->name.str : "(null)"));
243         return -1;
244 }
245
246 /**
247  * pmenu_remove - Remove an item from the item array.
248  *
249  * The item array must be disconnected prior to calling pmenu_remove()
250  */
251
252 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
253 {
254         int index;
255
256         assert(item_count(menu->ncm) == 0 && "not disconnected");
257
258         assert(menu->item_count);
259
260         pb_log("%s: %u\n", __func__, menu->item_count);
261
262         index = pmenu_item_get_index(item);
263
264         if (index < 0)
265                 return -1;
266
267         /* Note that items array has a null terminator. */
268
269         menu->insert_pt--;
270         menu->item_count--;
271
272         memmove(&menu->items[index], &menu->items[index + 1],
273                 (menu->item_count - index + 1) * sizeof(ITEM *));
274         menu->items = talloc_realloc(menu, menu->items, ITEM *,
275                 menu->item_count + 1);
276
277         return 0;
278 }
279
280 /**
281  * pmenu_init - Allocate and initialize a new menu instance.
282  *
283  * Returns a pointer the the initialized struct pmenu instance or NULL on error.
284  * The caller is responsible for calling talloc_free() for the returned
285  * instance.
286  */
287
288 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
289         void (*on_exit)(struct pmenu *))
290 {
291         struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
292
293         if (!menu)
294                 return NULL;
295
296         /* note items array has a null terminator */
297
298         menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
299
300         if (!menu->items) {
301                 talloc_free(menu);
302                 return NULL;
303         }
304
305         nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
306                 pmenu_post, pmenu_unpost, pmenu_resize);
307
308         menu->item_count = item_count;
309         menu->insert_pt = 0; /* insert from top */
310         menu->on_exit = on_exit;
311
312         return menu;
313 }
314
315 /**
316  * pmenu_setup - Create nc menu, setup nc windows.
317  *
318  */
319
320 int pmenu_setup(struct pmenu *menu)
321 {
322         assert(!menu->ncm);
323
324         menu->ncm = new_menu(menu->items);
325
326         if (!menu->ncm) {
327                 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
328                         strerror(errno));
329                 return -1;
330         }
331
332         set_menu_win(menu->ncm, menu->scr.main_ncw);
333         set_menu_sub(menu->ncm, menu->scr.sub_ncw);
334
335         /* Makes menu scrollable. */
336         set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
337
338         return 0;
339 }
340
341 /**
342  * pmenu_delete - Delete a menu instance.
343  *
344  */
345
346 void pmenu_delete(struct pmenu *menu)
347 {
348         unsigned int i;
349
350         assert(menu->scr.sig == pb_pmenu_sig);
351         menu->scr.sig = pb_removed_sig;
352
353         for (i = item_count(menu->ncm); i; i--)
354                 free_item(menu->items[i]);
355
356         free_menu(menu->ncm);
357         delwin(menu->scr.sub_ncw);
358         delwin(menu->scr.main_ncw);
359         talloc_free(menu);
360 }