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