]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.c
ui/ncurses: Fix boot editor segfault on 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_update - Update the label of an existing pmenu_item.
128  *
129  * The item array must be disconnected prior to calling.
130  */
131 int pmenu_item_update(struct pmenu_item *item, const char *name)
132 {
133         const char *label;
134         ITEM *i;
135
136         if (!item || !item->nci)
137                 return -1;
138
139         label = pmenu_item_label(item, name);
140
141         if (!label)
142                 return -1;
143
144         i = item->nci;
145         i->name.str = label;
146         i->name.length = strncols(label);
147
148         return 0;
149 }
150
151 /**
152  * pmenu_item_create - Allocate and initialize a new pmenu_item instance.
153  *
154  * Returns a pointer the the initialized struct pmenu_item instance or NULL
155  * on error. The caller is responsible for calling talloc_free() for the
156  * returned instance.
157  */
158 struct pmenu_item *pmenu_item_create(struct pmenu *menu, const char *name)
159 {
160         struct pmenu_item *item = talloc_zero(menu, struct pmenu_item);
161         const char *label;
162
163         label = pmenu_item_label(item, name);
164
165         item->i_sig = pb_item_sig;
166         item->pmenu = menu;
167         item->nci = new_item(label, NULL);
168
169         if (!item->nci) {
170                 talloc_free(item);
171                 return NULL;
172         }
173
174         talloc_set_destructor(item, pmenu_item_destructor);
175
176         set_item_userptr(item->nci, item);
177
178         return item;
179 }
180
181 void pmenu_item_insert(struct pmenu *menu, struct pmenu_item *item,
182         unsigned int index)
183 {
184         assert(item);
185         assert(index < menu->item_count);
186         assert(menu->items[index] == NULL);
187         assert(menu_items(menu->ncm) == NULL);
188
189         menu->items[index] = item->nci;
190 }
191
192 /**
193  * pmenu_item_add - Insert item into appropriate position
194  *
195  * Inserts boot entry under matching, predefined device header entry,
196  * moving items in the list if necessary
197  */
198
199 void pmenu_item_add(struct pmenu *menu, struct pmenu_item *item,
200         unsigned int insert_pt)
201 {
202         struct cui_opt_data *cod = item->data;
203         bool found = false;
204         unsigned int dev;
205
206         /* Items array should already be disconnected */
207
208         for (dev = 0; dev < menu->item_count; dev++) {
209                 if (!menu->items[dev])
210                         continue;
211
212                 struct pmenu_item *i = item_userptr(menu->items[dev]);
213                 struct cui_opt_data *d = i->data;
214                 /* Device header will have opt == NULL */
215                 if (d && !d->opt) {
216                         if (cod->dev == d->dev) {
217                                 found = true;
218                                 break;
219                         }
220                 }
221         }
222
223         if (found) {
224                 assert(dev < insert_pt);
225                 /* Shift down entries between header and insert_pt */
226                 memmove(menu->items + dev + 2, menu->items + dev + 1,
227                         ((menu->items + insert_pt) - (menu->items + dev + 1))
228                         * sizeof(menu->items[0]));
229                 memset(menu->items + dev + 1, 0, sizeof(menu->items[0]));
230                 insert_pt = dev + 1;
231         }
232         /* If for some reason we didn't find the matching device,
233          * at least add it to a valid position */
234         pmenu_item_insert(menu, item, insert_pt);
235 }
236
237 /**
238  * pmenu_find_device - Determine if a boot option is new, and if
239  * so return a new pmenu_item to represent its parent device
240  */
241
242 struct pmenu_item *pmenu_find_device(struct pmenu *menu, struct device *dev,
243         struct boot_option *opt)
244 {
245         struct pmenu_item *item, *dev_hdr = NULL;
246         struct cui *cui = cui_from_pmenu(menu);
247         bool newdev = true, matched = false;
248         struct interface_info *intf;
249         struct blockdev_info *bd;
250         struct cui_opt_data *cod;
251         struct system_info *sys;
252         char hwaddr[32];
253         unsigned int i;
254         char buf[256];
255
256         for (i = 0; i < menu->item_count; i++) {
257                 item = item_userptr(menu->items[i]);
258                 cod = cod_from_item(item);
259                 /* boot entries will have opt defined */
260                 if (!cod || cod->opt)
261                         continue;
262                 if (cod->dev == dev) {
263                         pb_debug("%s: opt %s fits under %s\n",__func__,
264                                  opt->name, opt->device_id);
265                         newdev = false;
266                         break;
267                 }
268         }
269
270         if (!newdev) {
271                 pb_debug("%s: No new device\n",__func__);
272                 return NULL;
273         }
274
275         /* Create a dummy pmenu_item to represent the dev */
276         pb_debug("%s: Building new item\n",__func__);
277         sys = cui->sysinfo;
278         switch (dev->type) {
279         case DEVICE_TYPE_OPTICAL:
280         case DEVICE_TYPE_DISK:
281         case DEVICE_TYPE_USB:
282                 /* Find block info */
283                 for (i = 0; sys && i < sys->n_blockdevs; i++) {
284                         bd = sys->blockdevs[i];
285                         if (!strcmp(opt->device_id, bd->name)) {
286                                 matched = true;
287                                 break;
288                         }
289                 }
290                 if (matched) {
291                         snprintf(buf,sizeof(buf),"[%s: %s / %s]",
292                                 device_type_display_name(dev->type),
293                                 bd->name, bd->uuid);
294                 }
295                 break;
296
297         case DEVICE_TYPE_NETWORK:
298                 /* Find interface info */
299                 for (i = 0; sys && i < sys->n_interfaces; i++) {
300                         intf = sys->interfaces[i];
301                         if (!strcmp(opt->device_id, intf->name)) {
302                                 matched = true;
303                                 break;
304                         }
305                 }
306                 if (matched) {
307                         mac_str(intf->hwaddr, intf->hwaddr_size,
308                                 hwaddr, sizeof(hwaddr));
309                         snprintf(buf,sizeof(buf),"[%s: %s / %s]",
310                                 _("Network"), intf->name, hwaddr);
311                 }
312                 break;
313         case DEVICE_TYPE_ANY:
314                 /* This is an option found from a file:// url, not associated
315                  * with any device */
316                 snprintf(buf, sizeof(buf), "[Custom Local Options]");
317                 matched = true;
318                 break;
319
320         default:
321                 /* Assume the device may be able to boot */
322                 break;
323         }
324         if (!matched) {
325                 pb_debug("%s: No matching device found for %s (%s)\n",
326                         __func__,opt->device_id, dev->id);
327                 snprintf(buf, sizeof(buf), "[%s: %s]",
328                         _("Unknown Device"), dev->id);
329         }
330
331         dev_hdr = pmenu_item_create(menu, buf);
332         if (!dev_hdr) {
333                 pb_log("%s: Failed to create item\n",__func__);
334                 return NULL;
335         }
336
337         dev_hdr->on_execute = NULL;
338         item_opts_off(dev_hdr->nci, O_SELECTABLE);
339
340         /* We identify dev_hdr items as having a valid c->name,
341          * but a NULL c->opt */
342         cod = talloc_zero(dev_hdr, struct cui_opt_data);
343         cod->name = talloc_strdup(dev_hdr, opt->device_id);
344         cod->dev = dev;
345         dev_hdr->data = cod;
346
347         pb_debug("%s: returning %s\n",__func__,cod->name);
348         return dev_hdr;
349 }
350
351 static int pmenu_item_get_index(const struct pmenu_item *item)
352 {
353         unsigned int i;
354
355         if (item)
356                 for (i = 0; i < item->pmenu->item_count; i++)
357                         if (item->pmenu->items[i] == item->nci)
358                                 return i;
359
360         pb_log("%s: not found: %p %s\n", __func__, item,
361                 (item ? item->nci->name.str : "(null)"));
362         return -1;
363 }
364
365 /**
366  * pmenu_move_cursor - Move the cursor.
367  * @req: An ncurses request or char to send to menu_driver().
368  */
369
370 static void pmenu_move_cursor(struct pmenu *menu, int req)
371 {
372         menu_driver(menu->ncm, req);
373         wrefresh(menu->scr.main_ncw);
374 }
375
376 /**
377  * pmenu_main_hot_keys - Hot keys for the main boot menu
378  */
379 int pmenu_main_hot_keys(struct pmenu *menu, struct pmenu_item *item, int c)
380 {
381         struct nc_scr *scr = &menu->scr;
382         (void)item;
383
384         switch (c) {
385         case 'i':
386                 cui_show_sysinfo(cui_from_arg(scr->ui_ctx));
387                 break;
388         case 'c':
389                 cui_show_config(cui_from_arg(scr->ui_ctx));
390                 break;
391         case 'l':
392                 cui_show_lang(cui_from_arg(scr->ui_ctx));
393                 break;
394         case 'g':
395                 cui_show_statuslog(cui_from_arg(scr->ui_ctx));
396                 break;
397         default:
398                 return 0;
399         }
400
401         return c;
402 }
403
404 /**
405  * pmenu_process_key - Process a user keystroke.
406  */
407
408 static void pmenu_process_key(struct nc_scr *scr, int key)
409 {
410         struct pmenu *menu = pmenu_from_scr(scr);
411         struct pmenu_item *item = pmenu_find_selected(menu);
412         unsigned int i;
413
414         nc_scr_status_free(&menu->scr);
415
416         if (menu->hot_keys)
417                 for (i = 0; i < menu->n_hot_keys; i++) {
418                         if (menu->hot_keys[i](menu, item, key))
419                                 return;
420                 }
421
422         switch (key) {
423         case 27: /* ESC */
424         case 'x':
425                 if (menu->on_exit)
426                         menu->on_exit(menu);
427                 nc_flush_keys();
428                 return;
429
430         case KEY_PPAGE:
431                 pmenu_move_cursor(menu, REQ_SCR_UPAGE);
432                 break;
433         case KEY_NPAGE:
434                 pmenu_move_cursor(menu, REQ_SCR_DPAGE);
435                 break;
436         case KEY_HOME:
437                 pmenu_move_cursor(menu, REQ_FIRST_ITEM);
438                 break;
439         case KEY_END:
440                 pmenu_move_cursor(menu, REQ_LAST_ITEM);
441                 break;
442         case KEY_UP:
443                 pmenu_move_cursor(menu, REQ_UP_ITEM);
444                 break;
445         case KEY_BTAB:
446                 pmenu_move_cursor(menu, REQ_PREV_ITEM);
447                 break;
448         case KEY_DOWN:
449                 pmenu_move_cursor(menu, REQ_DOWN_ITEM);
450                 break;
451         case '\t':
452                 pmenu_move_cursor(menu, REQ_NEXT_ITEM);
453                 break;
454         case 'e':
455                 if (item->on_edit)
456                         item->on_edit(item);
457                 break;
458         case 'n':
459                 if (menu->on_new)
460                         menu->on_new(menu);
461                 break;
462         case ' ':
463         case '\n':
464         case '\r':
465                 if (item->on_execute)
466                         item->on_execute(item);
467                 break;
468         case KEY_F(1):
469         case 'h':
470                 if (menu->help_text)
471                         cui_show_help(cui_from_arg(scr->ui_ctx),
472                                         menu->help_title, menu->help_text);
473                 break;
474         default:
475                 menu_driver(menu->ncm, key);
476                 break;
477         }
478 }
479
480 /**
481  * pmenu_grow - Grow the item array.
482  * @count: The count of new items.
483  *
484  * The item array must be disconnected prior to calling pmenu_grow().
485  * Returns the insert point index.
486  */
487
488 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
489 {
490         unsigned int tmp;
491
492         assert(item_count(menu->ncm) == 0 && "not disconnected");
493
494         pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
495                 count, menu->item_count + count);
496
497         /* Note that items array has a null terminator. */
498
499         menu->items = talloc_realloc(menu, menu->items, ITEM *,
500                 menu->item_count + count + 1);
501
502         memmove(menu->items + menu->insert_pt + count,
503                 menu->items + menu->insert_pt,
504                 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
505
506         memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
507
508         tmp = menu->insert_pt;
509         menu->insert_pt += count;
510         menu->item_count += count;
511
512         return tmp;
513 }
514
515 /**
516  * pmenu_remove - Remove an item from the item array.
517  *
518  * The item array must be disconnected prior to calling pmenu_remove()
519  */
520
521 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
522 {
523         int index;
524
525         assert(item_count(menu->ncm) == 0 && "not disconnected");
526
527         assert(menu->item_count);
528
529         pb_log("%s: %u\n", __func__, menu->item_count);
530
531         index = pmenu_item_get_index(item);
532
533         if (index < 0)
534                 return -1;
535
536         talloc_free(item);
537
538         /* Note that items array has a null terminator. */
539
540         menu->insert_pt--;
541         menu->item_count--;
542
543         memmove(&menu->items[index], &menu->items[index + 1],
544                 (menu->item_count - index + 1) * sizeof(ITEM *));
545         menu->items = talloc_realloc(menu, menu->items, ITEM *,
546                 menu->item_count + 1);
547
548         return 0;
549 }
550
551 static int pmenu_destructor(void *ptr)
552 {
553         struct pmenu *menu = ptr;
554         assert(menu->scr.sig == pb_pmenu_sig);
555         menu->scr.sig = pb_removed_sig;
556
557         unpost_menu(menu->ncm);
558         free_menu(menu->ncm);
559         delwin(menu->scr.sub_ncw);
560         delwin(menu->scr.main_ncw);
561         return 0;
562 }
563
564 /**
565  * pmenu_init - Allocate and initialize a new menu instance.
566  *
567  * Returns a pointer the the initialized struct pmenu instance or NULL on error.
568  * The caller is responsible for calling talloc_free() for the returned
569  * instance.
570  */
571
572 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
573         void (*on_exit)(struct pmenu *))
574 {
575         struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
576         if (!menu)
577                 return NULL;
578
579         talloc_set_destructor(menu, pmenu_destructor);
580
581         /* note items array has a null terminator */
582         menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
583         if (!menu->items) {
584                 talloc_free(menu);
585                 return NULL;
586         }
587
588         nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
589                 pmenu_post, pmenu_unpost, pmenu_resize);
590
591         menu->item_count = item_count;
592         menu->insert_pt = 0; /* insert from top */
593         menu->on_exit = on_exit;
594
595         return menu;
596 }
597
598 /**
599  * pmenu_setup - Create nc menu, setup nc windows.
600  *
601  */
602
603 int pmenu_setup(struct pmenu *menu)
604 {
605         assert(!menu->ncm);
606
607         menu->ncm = new_menu(menu->items);
608
609         if (!menu->ncm) {
610                 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
611                         strerror(errno));
612                 return -1;
613         }
614
615         set_menu_win(menu->ncm, menu->scr.main_ncw);
616         set_menu_sub(menu->ncm, menu->scr.sub_ncw);
617
618         /* Makes menu scrollable. */
619         set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
620
621         set_menu_grey(menu->ncm, A_NORMAL);
622
623         return 0;
624 }
625