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