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