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
26 #include "talloc/talloc.h"
27 #include "ui/common/ui-system.h"
31 * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
34 int pmenu_exit_cb(struct pmenu_item *item)
36 assert(item->pmenu->on_exit);
37 item->pmenu->on_exit(item->pmenu);
42 * pmenu_find_selected - Find the selected pmenu_item.
45 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
47 return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
50 static int pmenu_post(struct nc_scr *scr)
53 struct pmenu *menu = pmenu_from_scr(scr);
55 result = post_menu(menu->ncm);
57 nc_scr_frame_draw(scr);
58 redrawwin(menu->scr.main_ncw);
59 wrefresh(menu->scr.main_ncw);
64 static int pmenu_unpost(struct nc_scr *scr)
66 return unpost_menu(pmenu_from_scr(scr)->ncm);
69 static void pmenu_resize(struct nc_scr *scr)
71 /* FIXME: menus can't be resized, need to recreate here */
77 * pmenu_item_init - Allocate and initialize a new pmenu_item instance.
79 * Returns a pointer the the initialized struct pmenu_item instance or NULL
80 * on error. The caller is responsible for calling talloc_free() for the
84 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu)
86 /* Items go with the menu, not the pointer array. */
88 struct pmenu_item *i = talloc_zero(menu, struct pmenu_item);
93 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
94 unsigned int index, const char *name,
95 const char *description)
102 i->i_sig = pb_item_sig;
104 i->nci = new_item(name, description);
111 set_item_userptr(i->nci, i);
113 menu->items[index] = i->nci;
119 * pmenu_move_cursor - Move the cursor.
120 * @req: An ncurses request or char to send to menu_driver().
123 static void pmenu_move_cursor(struct pmenu *menu, int req)
125 menu_driver(menu->ncm, req);
126 wrefresh(menu->scr.main_ncw);
130 * pmenu_process_key - Process a user keystroke.
133 static void pmenu_process_key(struct nc_scr *scr)
135 struct pmenu *menu = pmenu_from_scr(scr);
136 struct pmenu_item *item = pmenu_find_selected(menu);
138 nc_scr_status_free(&menu->scr);
146 /* DBGS("%d (%o)\n", c, c); */
149 c = menu->hot_key(menu, item, c);
159 pmenu_move_cursor(menu, REQ_SCR_UPAGE);
162 pmenu_move_cursor(menu, REQ_SCR_DPAGE);
165 pmenu_move_cursor(menu, REQ_FIRST_ITEM);
168 pmenu_move_cursor(menu, REQ_LAST_ITEM);
171 pmenu_move_cursor(menu, REQ_UP_ITEM);
175 pmenu_move_cursor(menu, REQ_DOWN_ITEM);
186 if (item->on_execute)
187 item->on_execute(item);
190 menu_driver(menu->ncm, c);
197 * pmenu_grow - Grow the item array.
198 * @count: The count of new items.
200 * The item array must be disconnected prior to calling pmenu_grow().
201 * Returns the insert point index.
204 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
208 assert(item_count(menu->ncm) == 0 && "not disconnected");
210 pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
211 count, menu->item_count + count);
213 /* Note that items array has a null terminator. */
215 menu->items = talloc_realloc(menu, menu->items, ITEM *,
216 menu->item_count + count + 1);
218 memmove(menu->items + menu->insert_pt + count,
219 menu->items + menu->insert_pt,
220 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
222 memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
224 tmp = menu->insert_pt;
225 menu->insert_pt += count;
226 menu->item_count += count;
231 static int pmenu_item_get_index(const struct pmenu_item *item)
235 for (i = 0; i < item->pmenu->item_count; i++)
236 if (item->pmenu->items[i] == item->nci)
239 pb_log("%s: not found: %p %s\n", __func__, item,
240 (item ? item->nci->name.str : "(null)"));
245 * pmenu_remove - Remove an item from the item array.
247 * The item array must be disconnected prior to calling pmenu_remove()
250 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
254 assert(item_count(menu->ncm) == 0 && "not disconnected");
256 assert(menu->item_count);
258 pb_log("%s: %u\n", __func__, menu->item_count);
260 index = pmenu_item_get_index(item);
265 /* Note that items array has a null terminator. */
270 memmove(&menu->items[index], &menu->items[index + 1],
271 (menu->item_count - index + 1) * sizeof(ITEM *));
272 menu->items = talloc_realloc(menu, menu->items, ITEM *,
273 menu->item_count + 1);
279 * pmenu_init - Allocate and initialize a new menu instance.
281 * Returns a pointer the the initialized struct pmenu instance or NULL on error.
282 * The caller is responsible for calling talloc_free() for the returned
286 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
287 void (*on_exit)(struct pmenu *))
289 struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
294 /* note items array has a null terminator */
296 menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
303 nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
304 pmenu_post, pmenu_unpost, pmenu_resize);
306 menu->item_count = item_count;
307 menu->insert_pt = 0; /* insert from top */
308 menu->on_exit = on_exit;
314 * pmenu_setup - Create nc menu, setup nc windows.
318 int pmenu_setup(struct pmenu *menu)
322 menu->ncm = new_menu(menu->items);
325 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
330 set_menu_win(menu->ncm, menu->scr.main_ncw);
331 set_menu_sub(menu->ncm, menu->scr.sub_ncw);
333 /* Makes menu scrollable. */
334 set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
340 * pmenu_delete - Delete a menu instance.
344 void pmenu_delete(struct pmenu *menu)
348 assert(menu->scr.sig == pb_pmenu_sig);
349 menu->scr.sig = pb_removed_sig;
351 for (i = item_count(menu->ncm); i; i--)
352 free_item(menu->items[i]);
354 free_menu(menu->ncm);
355 delwin(menu->scr.sub_ncw);
356 delwin(menu->scr.main_ncw);