2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
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.
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.
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
19 #if defined(HAVE_CONFIG_H)
28 #include <util/util.h>
31 #include "talloc/talloc.h"
32 #include "i18n/i18n.h"
33 #include "ui/common/ui-system.h"
38 * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
41 int pmenu_exit_cb(struct pmenu_item *item)
43 assert(item->pmenu->on_exit);
44 item->pmenu->on_exit(item->pmenu);
49 * pmenu_find_selected - Find the selected pmenu_item.
52 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
54 return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
57 static int pmenu_post(struct nc_scr *scr)
60 struct pmenu *menu = pmenu_from_scr(scr);
62 result = post_menu(menu->ncm);
64 nc_scr_frame_draw(scr);
65 wrefresh(menu->scr.main_ncw);
70 static int pmenu_unpost(struct nc_scr *scr)
72 return unpost_menu(pmenu_from_scr(scr)->ncm);
75 static void pmenu_resize(struct nc_scr *scr)
77 /* FIXME: menus can't be resized, need to recreate here */
82 static int pmenu_item_destructor(void *arg)
84 struct pmenu_item *item = arg;
89 static const char *pmenu_item_label(struct pmenu_item *item, const char *name)
91 static int invalid_idx;
97 len = mbstowcs(NULL, name, 0);
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"),
107 tmp = talloc_array(item, wchar_t, len + 1);
108 mbstowcs(tmp, name, len + 1);
110 /* replace anything unprintable with U+FFFD REPLACEMENT CHARACTER */
111 for (i = 0; i < len; i++) {
112 if (!iswprint(tmp[i]))
116 len = wcstombs(NULL, tmp, 0);
117 label = talloc_array(item, char, len + 1);
118 wcstombs(label, tmp, len + 1);
120 pb_log("%s: %s\n", __func__, label);
127 * pmenu_item_create - Allocate and initialize a new pmenu_item instance.
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
133 struct pmenu_item *pmenu_item_create(struct pmenu *menu, const char *name)
135 struct pmenu_item *item = talloc_zero(menu, struct pmenu_item);
138 label = pmenu_item_label(item, name);
140 item->i_sig = pb_item_sig;
142 item->nci = new_item(label, NULL);
149 talloc_set_destructor(item, pmenu_item_destructor);
151 set_item_userptr(item->nci, item);
156 void pmenu_item_insert(struct pmenu *menu, struct pmenu_item *item,
160 assert(index < menu->item_count);
161 assert(menu->items[index] == NULL);
162 assert(menu_items(menu->ncm) == NULL);
164 menu->items[index] = item->nci;
168 * pmenu_item_add - Insert item into appropriate position
170 * Inserts boot entry under matching, predefined device header entry,
171 * moving items in the list if necessary
174 void pmenu_item_add(struct pmenu *menu, struct pmenu_item *item,
175 unsigned int insert_pt)
177 struct cui_opt_data *cod = item->data;
181 /* Items array should already be disconnected */
183 for (dev = 0; dev < menu->item_count; dev++) {
184 if (!menu->items[dev])
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 */
191 if (cod->dev == d->dev) {
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]));
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);
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
217 struct pmenu_item *pmenu_find_device(struct pmenu *menu, struct device *dev,
218 struct boot_option *opt)
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;
231 for (i = 0; i < menu->item_count; i++) {
232 item = item_userptr(menu->items[i]);
234 /* boot entries will have opt defined */
235 if (!cod || cod->opt)
237 if (cod->dev == dev) {
238 pb_debug("%s: opt %s fits under %s\n",__func__,
239 opt->name, opt->device_id);
246 pb_debug("%s: No new device\n",__func__);
250 /* Create a dummy pmenu_item to represent the dev */
251 pb_debug("%s: Building new item\n",__func__);
254 case DEVICE_TYPE_OPTICAL:
255 case DEVICE_TYPE_DISK:
256 case DEVICE_TYPE_USB:
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)) {
266 snprintf(buf,sizeof(buf),"[%s: %s / %s]",
267 device_type_display_name(dev->type),
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)) {
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);
288 case DEVICE_TYPE_ANY:
289 /* This is an option found from a file:// url, not associated
291 snprintf(buf, sizeof(buf), "[Custom Local Options]");
296 /* Assume the device may be able to boot */
300 pb_debug("%s: No matching device found for %s (%s)\n",
301 __func__,opt->device_id, dev->id);
302 snprintf(buf, sizeof(buf), "[%s: %s]",
303 _("Unknown Device"), dev->id);
306 dev_hdr = pmenu_item_create(menu, buf);
308 pb_log("%s: Failed to create item\n",__func__);
312 dev_hdr->on_execute = NULL;
313 item_opts_off(dev_hdr->nci, O_SELECTABLE);
315 /* We identify dev_hdr items as having a valid c->name,
316 * but a NULL c->opt */
317 cod = talloc(dev_hdr, struct cui_opt_data);
318 cod->name = talloc_strdup(dev_hdr, opt->device_id);
323 pb_debug("%s: returning %s\n",__func__,cod->name);
327 static int pmenu_item_get_index(const struct pmenu_item *item)
332 for (i = 0; i < item->pmenu->item_count; i++)
333 if (item->pmenu->items[i] == item->nci)
336 pb_log("%s: not found: %p %s\n", __func__, item,
337 (item ? item->nci->name.str : "(null)"));
342 * pmenu_move_cursor - Move the cursor.
343 * @req: An ncurses request or char to send to menu_driver().
346 static void pmenu_move_cursor(struct pmenu *menu, int req)
348 menu_driver(menu->ncm, req);
349 wrefresh(menu->scr.main_ncw);
353 * pmenu_process_key - Process a user keystroke.
356 static void pmenu_process_key(struct nc_scr *scr, int key)
358 struct pmenu *menu = pmenu_from_scr(scr);
359 struct pmenu_item *item = pmenu_find_selected(menu);
361 nc_scr_status_free(&menu->scr);
364 key = menu->hot_key(menu, item, key);
375 pmenu_move_cursor(menu, REQ_SCR_UPAGE);
378 pmenu_move_cursor(menu, REQ_SCR_DPAGE);
381 pmenu_move_cursor(menu, REQ_FIRST_ITEM);
384 pmenu_move_cursor(menu, REQ_LAST_ITEM);
387 pmenu_move_cursor(menu, REQ_UP_ITEM);
390 pmenu_move_cursor(menu, REQ_PREV_ITEM);
393 pmenu_move_cursor(menu, REQ_DOWN_ITEM);
396 pmenu_move_cursor(menu, REQ_NEXT_ITEM);
409 if (item->on_execute)
410 item->on_execute(item);
413 cui_show_sysinfo(cui_from_arg(scr->ui_ctx));
416 cui_show_config(cui_from_arg(scr->ui_ctx));
419 cui_show_lang(cui_from_arg(scr->ui_ctx));
424 cui_show_help(cui_from_arg(scr->ui_ctx),
425 menu->help_title, menu->help_text);
428 menu_driver(menu->ncm, key);
434 * pmenu_grow - Grow the item array.
435 * @count: The count of new items.
437 * The item array must be disconnected prior to calling pmenu_grow().
438 * Returns the insert point index.
441 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
445 assert(item_count(menu->ncm) == 0 && "not disconnected");
447 pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
448 count, menu->item_count + count);
450 /* Note that items array has a null terminator. */
452 menu->items = talloc_realloc(menu, menu->items, ITEM *,
453 menu->item_count + count + 1);
455 memmove(menu->items + menu->insert_pt + count,
456 menu->items + menu->insert_pt,
457 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
459 memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
461 tmp = menu->insert_pt;
462 menu->insert_pt += count;
463 menu->item_count += count;
469 * pmenu_remove - Remove an item from the item array.
471 * The item array must be disconnected prior to calling pmenu_remove()
474 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
478 assert(item_count(menu->ncm) == 0 && "not disconnected");
480 assert(menu->item_count);
482 pb_log("%s: %u\n", __func__, menu->item_count);
484 index = pmenu_item_get_index(item);
491 /* Note that items array has a null terminator. */
496 memmove(&menu->items[index], &menu->items[index + 1],
497 (menu->item_count - index + 1) * sizeof(ITEM *));
498 menu->items = talloc_realloc(menu, menu->items, ITEM *,
499 menu->item_count + 1);
504 static int pmenu_destructor(void *ptr)
506 struct pmenu *menu = ptr;
507 assert(menu->scr.sig == pb_pmenu_sig);
508 menu->scr.sig = pb_removed_sig;
510 unpost_menu(menu->ncm);
511 free_menu(menu->ncm);
512 delwin(menu->scr.sub_ncw);
513 delwin(menu->scr.main_ncw);
518 * pmenu_init - Allocate and initialize a new menu instance.
520 * Returns a pointer the the initialized struct pmenu instance or NULL on error.
521 * The caller is responsible for calling talloc_free() for the returned
525 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
526 void (*on_exit)(struct pmenu *))
528 struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
532 talloc_set_destructor(menu, pmenu_destructor);
534 /* note items array has a null terminator */
535 menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
541 nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
542 pmenu_post, pmenu_unpost, pmenu_resize);
544 menu->item_count = item_count;
545 menu->insert_pt = 0; /* insert from top */
546 menu->on_exit = on_exit;
552 * pmenu_setup - Create nc menu, setup nc windows.
556 int pmenu_setup(struct pmenu *menu)
560 menu->ncm = new_menu(menu->items);
563 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
568 set_menu_win(menu->ncm, menu->scr.main_ncw);
569 set_menu_sub(menu->ncm, menu->scr.sub_ncw);
571 /* Makes menu scrollable. */
572 set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
574 set_menu_grey(menu->ncm, A_NORMAL);