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