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