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