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 "talloc/talloc.h"
29 #include "ui/common/ui-system.h"
34 * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
37 int pmenu_exit_cb(struct pmenu_item *item)
39 assert(item->pmenu->on_exit);
40 item->pmenu->on_exit(item->pmenu);
45 * pmenu_find_selected - Find the selected pmenu_item.
48 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
50 return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
53 static int pmenu_post(struct nc_scr *scr)
56 struct pmenu *menu = pmenu_from_scr(scr);
58 result = post_menu(menu->ncm);
60 nc_scr_frame_draw(scr);
61 redrawwin(menu->scr.main_ncw);
62 wrefresh(menu->scr.main_ncw);
67 static int pmenu_unpost(struct nc_scr *scr)
69 return unpost_menu(pmenu_from_scr(scr)->ncm);
72 static void pmenu_resize(struct nc_scr *scr)
74 /* FIXME: menus can't be resized, need to recreate here */
80 * pmenu_item_init - Allocate and initialize a new pmenu_item instance.
82 * Returns a pointer the the initialized struct pmenu_item instance or NULL
83 * on error. The caller is responsible for calling talloc_free() for the
87 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu)
89 /* Items go with the menu, not the pointer array. */
91 struct pmenu_item *i = talloc_zero(menu, struct pmenu_item);
96 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
97 unsigned int index, const char *name)
105 i->i_sig = pb_item_sig;
107 i->nci = new_item(name, NULL);
112 set_item_userptr(i->nci, i);
114 menu->items[index] = i->nci;
119 static int pmenu_item_get_index(const struct pmenu_item *item)
123 for (i = 0; i < item->pmenu->item_count; i++)
124 if (item->pmenu->items[i] == item->nci)
127 pb_log("%s: not found: %p %s\n", __func__, item,
128 (item ? item->nci->name.str : "(null)"));
133 * pmenu_item_replace - Replace the menu item with a new one.
135 * Use this routine to change a menu item's text.
138 int pmenu_item_replace(struct pmenu_item *i, const char *name)
148 index = pmenu_item_get_index(i);
151 assert(0 && "get_index failed");
155 nci = new_item(name, NULL);
158 assert(0 && "new_item failed");
162 set_item_userptr(nci, i);
164 nc_scr_unpost(&menu->scr);
165 set_menu_items(menu->ncm, NULL);
167 // FIXME: need to assure item name is a talloc string.
168 /* talloc_free((char *)item_name(i->nci)); */
171 menu->items[index] = nci;
174 set_menu_items(menu->ncm, menu->items);
175 nc_scr_post(&menu->scr);
181 * pmenu_move_cursor - Move the cursor.
182 * @req: An ncurses request or char to send to menu_driver().
185 static void pmenu_move_cursor(struct pmenu *menu, int req)
187 menu_driver(menu->ncm, req);
188 wrefresh(menu->scr.main_ncw);
192 * pmenu_process_key - Process a user keystroke.
195 static void pmenu_process_key(struct nc_scr *scr, int key)
197 struct pmenu *menu = pmenu_from_scr(scr);
198 struct pmenu_item *item = pmenu_find_selected(menu);
200 nc_scr_status_free(&menu->scr);
203 key = menu->hot_key(menu, item, key);
214 pmenu_move_cursor(menu, REQ_SCR_UPAGE);
217 pmenu_move_cursor(menu, REQ_SCR_DPAGE);
220 pmenu_move_cursor(menu, REQ_FIRST_ITEM);
223 pmenu_move_cursor(menu, REQ_LAST_ITEM);
226 pmenu_move_cursor(menu, REQ_UP_ITEM);
229 pmenu_move_cursor(menu, REQ_PREV_ITEM);
232 pmenu_move_cursor(menu, REQ_DOWN_ITEM);
235 pmenu_move_cursor(menu, REQ_NEXT_ITEM);
248 if (item->on_execute)
249 item->on_execute(item);
252 cui_show_sysinfo(cui_from_arg(scr->ui_ctx));
255 cui_show_config(cui_from_arg(scr->ui_ctx));
260 cui_show_help(cui_from_arg(scr->ui_ctx),
261 menu->help_title, menu->help_text);
264 menu_driver(menu->ncm, key);
270 * pmenu_grow - Grow the item array.
271 * @count: The count of new items.
273 * The item array must be disconnected prior to calling pmenu_grow().
274 * Returns the insert point index.
277 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
281 assert(item_count(menu->ncm) == 0 && "not disconnected");
283 pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
284 count, menu->item_count + count);
286 /* Note that items array has a null terminator. */
288 menu->items = talloc_realloc(menu, menu->items, ITEM *,
289 menu->item_count + count + 1);
291 memmove(menu->items + menu->insert_pt + count,
292 menu->items + menu->insert_pt,
293 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
295 memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
297 tmp = menu->insert_pt;
298 menu->insert_pt += count;
299 menu->item_count += count;
305 * pmenu_remove - Remove an item from the item array.
307 * The item array must be disconnected prior to calling pmenu_remove()
310 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
314 assert(item_count(menu->ncm) == 0 && "not disconnected");
316 assert(menu->item_count);
318 pb_log("%s: %u\n", __func__, menu->item_count);
320 index = pmenu_item_get_index(item);
325 free_item(item->nci);
328 /* Note that items array has a null terminator. */
333 memmove(&menu->items[index], &menu->items[index + 1],
334 (menu->item_count - index + 1) * sizeof(ITEM *));
335 menu->items = talloc_realloc(menu, menu->items, ITEM *,
336 menu->item_count + 1);
342 * pmenu_init - Allocate and initialize a new menu instance.
344 * Returns a pointer the the initialized struct pmenu instance or NULL on error.
345 * The caller is responsible for calling talloc_free() for the returned
349 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
350 void (*on_exit)(struct pmenu *))
352 struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
357 /* note items array has a null terminator */
359 menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
366 nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
367 pmenu_post, pmenu_unpost, pmenu_resize);
369 menu->item_count = item_count;
370 menu->insert_pt = 0; /* insert from top */
371 menu->on_exit = on_exit;
377 * pmenu_setup - Create nc menu, setup nc windows.
381 int pmenu_setup(struct pmenu *menu)
385 menu->ncm = new_menu(menu->items);
388 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
393 set_menu_win(menu->ncm, menu->scr.main_ncw);
394 set_menu_sub(menu->ncm, menu->scr.sub_ncw);
396 /* Makes menu scrollable. */
397 set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
399 set_menu_grey(menu->ncm, A_NORMAL);
405 * pmenu_delete - Delete a menu instance.
409 void pmenu_delete(struct pmenu *menu)
413 assert(menu->scr.sig == pb_pmenu_sig);
414 menu->scr.sig = pb_removed_sig;
416 for (i = item_count(menu->ncm); i; i--)
417 free_item(menu->items[i - 1]);
419 free_menu(menu->ncm);
420 delwin(menu->scr.sub_ncw);
421 delwin(menu->scr.main_ncw);