]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.c
ui/ncurses: Properly reposition cursor after menu update
[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 #include <util/util.h>
29
30 #include "log/log.h"
31 #include "talloc/talloc.h"
32 #include "i18n/i18n.h"
33 #include "ui/common/ui-system.h"
34 #include "nc-cui.h"
35 #include "nc-menu.h"
36
37 /**
38  * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
39  */
40
41 int pmenu_exit_cb(struct pmenu_item *item)
42 {
43         assert(item->pmenu->on_exit);
44         item->pmenu->on_exit(item->pmenu);
45         return 0;
46 }
47
48 /**
49  * pmenu_find_selected - Find the selected pmenu_item.
50  */
51
52 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
53 {
54         return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
55 }
56
57 static int pmenu_post(struct nc_scr *scr)
58 {
59         int result;
60         struct pmenu *menu = pmenu_from_scr(scr);
61
62         result = post_menu(menu->ncm);
63
64         nc_scr_frame_draw(scr);
65         wrefresh(menu->scr.main_ncw);
66
67         return result;
68 }
69
70 static int pmenu_unpost(struct nc_scr *scr)
71 {
72         return unpost_menu(pmenu_from_scr(scr)->ncm);
73 }
74
75 static void pmenu_resize(struct nc_scr *scr)
76 {
77         /* FIXME: menus can't be resized, need to recreate here */
78         pmenu_unpost(scr);
79         pmenu_post(scr);
80 }
81
82 static int pmenu_item_destructor(void *arg)
83 {
84         struct pmenu_item *item = arg;
85         free_item(item->nci);
86         return 0;
87 }
88
89 static const char *pmenu_item_label(struct pmenu_item *item, const char *name)
90 {
91         static int invalid_idx;
92         unsigned int i;
93         wchar_t *tmp;
94         char *label;
95         size_t len;
96
97         len = mbstowcs(NULL, name, 0);
98
99         /* if we have an invalid multibyte sequence, create an entirely
100          * new name, indicating that we had invalid input */
101         if (len == SIZE_MAX) {
102                 name = talloc_asprintf(item, _("!Invalid option %d"),
103                                 ++invalid_idx);
104                 return name;
105         }
106
107         tmp = talloc_array(item, wchar_t, len + 1);
108         mbstowcs(tmp, name, len + 1);
109
110         /* replace anything unprintable with U+FFFD REPLACEMENT CHARACTER */
111         for (i = 0; i < len; i++) {
112                 if (!iswprint(tmp[i]))
113                         tmp[i] = 0xfffd;
114         }
115
116         len = wcstombs(NULL, tmp, 0);
117         label = talloc_array(item, char, len + 1);
118         wcstombs(label, tmp, len + 1);
119
120         pb_log("%s: %s\n", __func__, label);
121
122         talloc_free(tmp);
123         return label;
124 }
125
126 /**
127  * pmenu_item_create - Allocate and initialize a new pmenu_item instance.
128  *
129  * Returns a pointer the the initialized struct pmenu_item instance or NULL
130  * on error. The caller is responsible for calling talloc_free() for the
131  * returned instance.
132  */
133 struct pmenu_item *pmenu_item_create(struct pmenu *menu, const char *name)
134 {
135         struct pmenu_item *item = talloc_zero(menu, struct pmenu_item);
136         const char *label;
137
138         label = pmenu_item_label(item, name);
139
140         item->i_sig = pb_item_sig;
141         item->pmenu = menu;
142         item->nci = new_item(label, NULL);
143
144         if (!item->nci) {
145                 talloc_free(item);
146                 return NULL;
147         }
148
149         talloc_set_destructor(item, pmenu_item_destructor);
150
151         set_item_userptr(item->nci, item);
152
153         return item;
154 }
155
156 void pmenu_item_insert(struct pmenu *menu, struct pmenu_item *item,
157         unsigned int index)
158 {
159         assert(item);
160         assert(index < menu->item_count);
161         assert(menu->items[index] == NULL);
162         assert(menu_items(menu->ncm) == NULL);
163
164         menu->items[index] = item->nci;
165 }
166
167 /**
168  * pmenu_item_add - Insert item into appropriate position
169  *
170  * Inserts boot entry under matching, predefined device header entry,
171  * moving items in the list if necessary
172  */
173
174 void pmenu_item_add(struct pmenu *menu, struct pmenu_item *item,
175         unsigned int insert_pt)
176 {
177         struct cui_opt_data *cod = item->data;
178         bool found = false;
179         unsigned int dev;
180
181         /* Items array should already be disconnected */
182
183         for (dev = 0; dev < menu->item_count; dev++) {
184                 if (!menu->items[dev])
185                         continue;
186
187                 struct pmenu_item *i = item_userptr(menu->items[dev]);
188                 struct cui_opt_data *d = i->data;
189                 /* Device header will have opt == NULL */
190                 if (d && !d->opt) {
191                         if (cod->dev == d->dev) {
192                                 found = true;
193                                 break;
194                         }
195                 }
196         }
197
198         if (found) {
199                 assert(dev < insert_pt);
200                 /* Shift down entries between header and insert_pt */
201                 memmove(menu->items + dev + 2, menu->items + dev + 1,
202                         ((menu->items + insert_pt) - (menu->items + dev + 1))
203                         * sizeof(menu->items[0]));
204                 memset(menu->items + dev + 1, 0, sizeof(menu->items[0]));
205                 insert_pt = dev + 1;
206         }
207         /* If for some reason we didn't find the matching device,
208          * at least add it to a valid position */
209         pmenu_item_insert(menu, item, insert_pt);
210 }
211
212 /**
213  * pmenu_find_device - Determine if a boot option is new, and if
214  * so return a new pmenu_item to represent its parent device
215  */
216
217 struct pmenu_item *pmenu_find_device(struct pmenu *menu, struct device *dev,
218         struct boot_option *opt)
219 {
220         struct pmenu_item *item, *dev_hdr = NULL;
221         struct cui *cui = cui_from_pmenu(menu);
222         bool newdev = true, matched = false;
223         struct interface_info *intf;
224         struct blockdev_info *bd;
225         struct cui_opt_data *cod;
226         struct system_info *sys;
227         char hwaddr[32];
228         unsigned int i;
229         char buf[256];
230
231         for (i = 0; i < menu->item_count; i++) {
232                 item = item_userptr(menu->items[i]);
233                 cod = item->data;
234                 /* boot entries will have opt defined */
235                 if (!cod || cod->opt)
236                         continue;
237                 if (cod->dev == dev) {
238                         pb_debug("%s: opt %s fits under %s\n",__func__,
239                                  opt->name, opt->device_id);
240                         newdev = false;
241                         break;
242                 }
243         }
244
245         if (!newdev) {
246                 pb_debug("%s: No new device\n",__func__);
247                 return NULL;
248         }
249
250         /* Create a dummy pmenu_item to represent the dev */
251         pb_debug("%s: Building new item\n",__func__);
252         sys = cui->sysinfo;
253         switch (dev->type) {
254         case DEVICE_TYPE_OPTICAL:
255         case DEVICE_TYPE_DISK:
256                 /* Find block info */
257                 for (i = 0; sys && i < sys->n_blockdevs; i++) {
258                         bd = sys->blockdevs[i];
259                         if (!strcmp(opt->device_id, bd->name)) {
260                                 matched = true;
261                                 break;
262                         }
263                 }
264                 if (matched) {
265                         snprintf(buf,sizeof(buf),"[%s: %s / %s]",
266                                 dev->type == DEVICE_TYPE_DISK ?
267                                         _("Disk") : _("CD/DVD"),
268                                 bd->name, bd->uuid);
269                 }
270                 break;
271
272         case DEVICE_TYPE_NETWORK:
273                 /* Find interface info */
274                 for (i = 0; sys && i < sys->n_interfaces; i++) {
275                         intf = sys->interfaces[i];
276                         if (!strcmp(opt->device_id, intf->name)) {
277                                 matched = true;
278                                 break;
279                         }
280                 }
281                 if (matched) {
282                         mac_str(intf->hwaddr, intf->hwaddr_size,
283                                 hwaddr, sizeof(hwaddr));
284                         snprintf(buf,sizeof(buf),"[%s: %s / %s]",
285                                 _("Network"), intf->name, hwaddr);
286                 }
287                 break;
288
289         default:
290                 /* Assume the device may be able to boot */
291                 break;
292         }
293         if (!matched) {
294                 pb_debug("%s: No matching device found for %s (%s)\n",
295                         __func__,opt->device_id, dev->id);
296                 snprintf(buf, sizeof(buf), "[%s: %s]",
297                         _("Unknown Device"), dev->id);
298         }
299
300         dev_hdr = pmenu_item_create(menu, buf);
301         if (!dev_hdr) {
302                 pb_log("%s: Failed to create item\n",__func__);
303                 return NULL;
304         }
305
306         dev_hdr->on_execute = NULL;
307         item_opts_off(dev_hdr->nci, O_SELECTABLE);
308
309         /* We identify dev_hdr items as having a valid c->name,
310          * but a NULL c->opt */
311         cod = talloc(dev_hdr, struct cui_opt_data);
312         cod->name = talloc_strdup(dev_hdr, opt->device_id);
313         cod->dev = dev;
314         cod->opt = NULL;
315         dev_hdr->data = cod;
316
317         pb_debug("%s: returning %s\n",__func__,cod->name);
318         return dev_hdr;
319 }
320
321 static int pmenu_item_get_index(const struct pmenu_item *item)
322 {
323         unsigned int i;
324
325         for (i = 0; i < item->pmenu->item_count; i++)
326                 if (item->pmenu->items[i] == item->nci)
327                         return i;
328
329         pb_log("%s: not found: %p %s\n", __func__, item,
330                 (item ? item->nci->name.str : "(null)"));
331         return -1;
332 }
333
334 /**
335  * pmenu_move_cursor - Move the cursor.
336  * @req: An ncurses request or char to send to menu_driver().
337  */
338
339 static void pmenu_move_cursor(struct pmenu *menu, int req)
340 {
341         menu_driver(menu->ncm, req);
342         wrefresh(menu->scr.main_ncw);
343 }
344
345 /**
346  * pmenu_process_key - Process a user keystroke.
347  */
348
349 static void pmenu_process_key(struct nc_scr *scr, int key)
350 {
351         struct pmenu *menu = pmenu_from_scr(scr);
352         struct pmenu_item *item = pmenu_find_selected(menu);
353
354         nc_scr_status_free(&menu->scr);
355
356         if (menu->hot_key)
357                 key = menu->hot_key(menu, item, key);
358
359         switch (key) {
360         case 27: /* ESC */
361         case 'x':
362                 if (menu->on_exit)
363                         menu->on_exit(menu);
364                 nc_flush_keys();
365                 return;
366
367         case KEY_PPAGE:
368                 pmenu_move_cursor(menu, REQ_SCR_UPAGE);
369                 break;
370         case KEY_NPAGE:
371                 pmenu_move_cursor(menu, REQ_SCR_DPAGE);
372                 break;
373         case KEY_HOME:
374                 pmenu_move_cursor(menu, REQ_FIRST_ITEM);
375                 break;
376         case KEY_END:
377                 pmenu_move_cursor(menu, REQ_LAST_ITEM);
378                 break;
379         case KEY_UP:
380                 pmenu_move_cursor(menu, REQ_UP_ITEM);
381                 break;
382         case KEY_BTAB:
383                 pmenu_move_cursor(menu, REQ_PREV_ITEM);
384                 break;
385         case KEY_DOWN:
386                 pmenu_move_cursor(menu, REQ_DOWN_ITEM);
387                 break;
388         case '\t':
389                 pmenu_move_cursor(menu, REQ_NEXT_ITEM);
390                 break;
391         case 'e':
392                 if (item->on_edit)
393                         item->on_edit(item);
394                 break;
395         case 'n':
396                 if (menu->on_new)
397                         menu->on_new(menu);
398                 break;
399         case ' ':
400         case '\n':
401         case '\r':
402                 if (item->on_execute)
403                         item->on_execute(item);
404                 break;
405         case 'i':
406                 cui_show_sysinfo(cui_from_arg(scr->ui_ctx));
407                 break;
408         case 'c':
409                 cui_show_config(cui_from_arg(scr->ui_ctx));
410                 break;
411         case 'l':
412                 cui_show_lang(cui_from_arg(scr->ui_ctx));
413                 break;
414         case KEY_F(1):
415         case 'h':
416                 if (menu->help_text)
417                         cui_show_help(cui_from_arg(scr->ui_ctx),
418                                         menu->help_title, menu->help_text);
419                 break;
420         default:
421                 menu_driver(menu->ncm, key);
422                 break;
423         }
424 }
425
426 /**
427  * pmenu_grow - Grow the item array.
428  * @count: The count of new items.
429  *
430  * The item array must be disconnected prior to calling pmenu_grow().
431  * Returns the insert point index.
432  */
433
434 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
435 {
436         unsigned int tmp;
437
438         assert(item_count(menu->ncm) == 0 && "not disconnected");
439
440         pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
441                 count, menu->item_count + count);
442
443         /* Note that items array has a null terminator. */
444
445         menu->items = talloc_realloc(menu, menu->items, ITEM *,
446                 menu->item_count + count + 1);
447
448         memmove(menu->items + menu->insert_pt + count,
449                 menu->items + menu->insert_pt,
450                 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
451
452         memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
453
454         tmp = menu->insert_pt;
455         menu->insert_pt += count;
456         menu->item_count += count;
457
458         return tmp;
459 }
460
461 /**
462  * pmenu_remove - Remove an item from the item array.
463  *
464  * The item array must be disconnected prior to calling pmenu_remove()
465  */
466
467 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
468 {
469         int index;
470
471         assert(item_count(menu->ncm) == 0 && "not disconnected");
472
473         assert(menu->item_count);
474
475         pb_log("%s: %u\n", __func__, menu->item_count);
476
477         index = pmenu_item_get_index(item);
478
479         if (index < 0)
480                 return -1;
481
482         talloc_free(item);
483
484         /* Note that items array has a null terminator. */
485
486         menu->insert_pt--;
487         menu->item_count--;
488
489         memmove(&menu->items[index], &menu->items[index + 1],
490                 (menu->item_count - index + 1) * sizeof(ITEM *));
491         menu->items = talloc_realloc(menu, menu->items, ITEM *,
492                 menu->item_count + 1);
493
494         return 0;
495 }
496
497 static int pmenu_destructor(void *ptr)
498 {
499         struct pmenu *menu = ptr;
500         assert(menu->scr.sig == pb_pmenu_sig);
501         menu->scr.sig = pb_removed_sig;
502
503         unpost_menu(menu->ncm);
504         free_menu(menu->ncm);
505         delwin(menu->scr.sub_ncw);
506         delwin(menu->scr.main_ncw);
507         return 0;
508 }
509
510 /**
511  * pmenu_init - Allocate and initialize a new menu instance.
512  *
513  * Returns a pointer the the initialized struct pmenu instance or NULL on error.
514  * The caller is responsible for calling talloc_free() for the returned
515  * instance.
516  */
517
518 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
519         void (*on_exit)(struct pmenu *))
520 {
521         struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
522         if (!menu)
523                 return NULL;
524
525         talloc_set_destructor(menu, pmenu_destructor);
526
527         /* note items array has a null terminator */
528         menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
529         if (!menu->items) {
530                 talloc_free(menu);
531                 return NULL;
532         }
533
534         nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
535                 pmenu_post, pmenu_unpost, pmenu_resize);
536
537         menu->item_count = item_count;
538         menu->insert_pt = 0; /* insert from top */
539         menu->on_exit = on_exit;
540
541         return menu;
542 }
543
544 /**
545  * pmenu_setup - Create nc menu, setup nc windows.
546  *
547  */
548
549 int pmenu_setup(struct pmenu *menu)
550 {
551         assert(!menu->ncm);
552
553         menu->ncm = new_menu(menu->items);
554
555         if (!menu->ncm) {
556                 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
557                         strerror(errno));
558                 return -1;
559         }
560
561         set_menu_win(menu->ncm, menu->scr.main_ncw);
562         set_menu_sub(menu->ncm, menu->scr.sub_ncw);
563
564         /* Makes menu scrollable. */
565         set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
566
567         set_menu_grey(menu->ncm, A_NORMAL);
568
569         return 0;
570 }
571