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