]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.c
Update item text on edit
[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 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <string.h>
24
25 #include "log/log.h"
26 #include "talloc/talloc.h"
27 #include "ui/common/ui-system.h"
28 #include "nc-menu.h"
29
30 /**
31  * pmenu_exit_cb - Callback helper that runs run menu.on_exit().
32  */
33
34 int pmenu_exit_cb(struct pmenu_item *item)
35 {
36         assert(item->pmenu->on_exit);
37         item->pmenu->on_exit(item->pmenu);
38         return 0;
39 }
40
41 /**
42  * pmenu_find_selected - Find the selected pmenu_item.
43  */
44
45 struct pmenu_item *pmenu_find_selected(struct pmenu *menu)
46 {
47         return pmenu_item_from_arg(item_userptr(current_item(menu->ncm)));
48 }
49
50 static int pmenu_post(struct nc_scr *scr)
51 {
52         int result;
53         struct pmenu *menu = pmenu_from_scr(scr);
54
55         result = post_menu(menu->ncm);
56
57         nc_scr_frame_draw(scr);
58         redrawwin(menu->scr.main_ncw);
59         wrefresh(menu->scr.main_ncw);
60
61         return result;
62 }
63
64 static int pmenu_unpost(struct nc_scr *scr)
65 {
66         return unpost_menu(pmenu_from_scr(scr)->ncm);
67 }
68
69 static void pmenu_resize(struct nc_scr *scr)
70 {
71         /* FIXME: menus can't be resized, need to recreate here */
72         pmenu_unpost(scr);
73         pmenu_post(scr);
74 }
75
76 /**
77  * pmenu_item_init - Allocate and initialize a new pmenu_item instance.
78  *
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
81  * returned instance.
82  */
83
84 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu)
85 {
86         /* Items go with the menu, not the pointer array. */
87
88         struct pmenu_item *i = talloc_zero(menu, struct pmenu_item);
89
90         return i;
91 }
92
93 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
94         unsigned int index, const char *name)
95 {
96         assert(i);
97         assert(name);
98
99         if (!i)
100                 return NULL;
101
102         i->i_sig = pb_item_sig;
103         i->pmenu = menu;
104         i->nci = new_item(name, NULL);
105
106         if (!i->nci) {
107                 talloc_free(i);
108                 return NULL;
109         }
110
111         set_item_userptr(i->nci, i);
112
113         menu->items[index] = i->nci;
114
115         return i;
116 }
117
118 static int pmenu_item_get_index(const struct pmenu_item *item)
119 {
120         unsigned int i;
121
122         for (i = 0; i < item->pmenu->item_count; i++)
123                 if (item->pmenu->items[i] == item->nci)
124                         return i;
125
126         pb_log("%s: not found: %p %s\n", __func__, item,
127                 (item ? item->nci->name.str : "(null)"));
128         return -1;
129 }
130
131 /**
132  * pmenu_item_replace - Replace the menu item with a new one.
133  *
134  * Use this routine to change a menu item's text.
135  */
136
137 int pmenu_item_replace(struct pmenu_item *i, const char *name)
138 {
139         struct pmenu *menu;
140         ITEM *nci;
141         int index;
142
143         assert(name);
144         assert(i->nci);
145
146         menu = i->pmenu;
147         index = pmenu_item_get_index(i);
148
149         if (index < 0) {
150                 assert(0 && "get_index failed");
151                 return -1;
152         }
153
154         nci = new_item(name, NULL);
155
156         if (!nci) {
157                 assert(0 && "new_item failed");
158                 return -1;
159         }
160
161         set_item_userptr(nci, i);
162
163         menu->scr.unpost(&menu->scr);
164         set_menu_items(menu->ncm, NULL);
165
166         // FIXME: need to assure item name is a talloc string.
167         /* talloc_free((char *)item_name(i->nci)); */
168
169         free_item(i->nci);
170         menu->items[index] = nci;
171         i->nci = nci;
172
173         set_menu_items(menu->ncm, menu->items);
174         menu->scr.post(&menu->scr);
175
176         return 0;
177 }
178
179 /**
180  * pmenu_move_cursor - Move the cursor.
181  * @req: An ncurses request or char to send to menu_driver().
182  */
183
184 static void pmenu_move_cursor(struct pmenu *menu, int req)
185 {
186         menu_driver(menu->ncm, req);
187         wrefresh(menu->scr.main_ncw);
188 }
189
190 /**
191  * pmenu_process_key - Process a user keystroke.
192  */
193
194 static void pmenu_process_key(struct nc_scr *scr)
195 {
196         struct pmenu *menu = pmenu_from_scr(scr);
197         struct pmenu_item *item = pmenu_find_selected(menu);
198
199         nc_scr_status_free(&menu->scr);
200
201         while (1) {
202                 int c = getch();
203
204                 if (c == ERR)
205                         return;
206
207                 /* DBGS("%d (%o)\n", c, c); */
208
209                 if (menu->hot_key)
210                         c = menu->hot_key(menu, item, c);
211
212                 switch (c) {
213                 case 27: /* ESC */
214                         if (menu->on_exit)
215                                 menu->on_exit(menu);
216                         nc_flush_keys();
217                         return;
218
219                 case KEY_PPAGE:
220                         pmenu_move_cursor(menu, REQ_SCR_UPAGE);
221                         break;
222                 case KEY_NPAGE:
223                         pmenu_move_cursor(menu, REQ_SCR_DPAGE);
224                         break;
225                 case KEY_HOME:
226                         pmenu_move_cursor(menu, REQ_FIRST_ITEM);
227                         break;
228                 case KEY_END:
229                         pmenu_move_cursor(menu, REQ_LAST_ITEM);
230                         break;
231                 case KEY_UP:
232                         pmenu_move_cursor(menu, REQ_UP_ITEM);
233                         break;
234                 case KEY_DOWN:
235                 case '\t':
236                         pmenu_move_cursor(menu, REQ_DOWN_ITEM);
237                         break;
238                 case KEY_LEFT:
239                 case 'e':
240                         if (item->on_edit)
241                                 item->on_edit(item);
242                         break;
243                 case 'o':
244                         DBGS("on_open: %p\n", menu->on_open);
245                         if (menu->on_open)
246                                 menu->on_open(menu);
247                         break;
248                 case '\n':
249                 case '\r':
250                         if (item->on_execute)
251                                 item->on_execute(item);
252                         break;
253                 default:
254                         menu_driver(menu->ncm, c);
255                         break;
256                 }
257         }
258 }
259
260 /**
261  * pmenu_grow - Grow the item array.
262  * @count: The count of new items.
263  *
264  * The item array must be disconnected prior to calling pmenu_grow().
265  * Returns the insert point index.
266  */
267
268 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count)
269 {
270         unsigned int tmp;
271
272         assert(item_count(menu->ncm) == 0 && "not disconnected");
273
274         pb_log("%s: %u current + %u new = %u\n", __func__, menu->item_count,
275                 count, menu->item_count + count);
276
277         /* Note that items array has a null terminator. */
278
279         menu->items = talloc_realloc(menu, menu->items, ITEM *,
280                 menu->item_count + count + 1);
281
282         memmove(menu->items + menu->insert_pt + count,
283                 menu->items + menu->insert_pt,
284                 (menu->item_count - menu->insert_pt + 1) * sizeof(ITEM *));
285
286         memset(menu->items + menu->insert_pt, 0, count * sizeof(ITEM *));
287
288         tmp = menu->insert_pt;
289         menu->insert_pt += count;
290         menu->item_count += count;
291
292         return tmp;
293 }
294
295 /**
296  * pmenu_remove - Remove an item from the item array.
297  *
298  * The item array must be disconnected prior to calling pmenu_remove()
299  */
300
301 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item)
302 {
303         int index;
304
305         assert(item_count(menu->ncm) == 0 && "not disconnected");
306
307         assert(menu->item_count);
308
309         pb_log("%s: %u\n", __func__, menu->item_count);
310
311         index = pmenu_item_get_index(item);
312
313         if (index < 0)
314                 return -1;
315
316         free_item(item->nci);
317         talloc_free(item);
318
319         /* Note that items array has a null terminator. */
320
321         menu->insert_pt--;
322         menu->item_count--;
323
324         memmove(&menu->items[index], &menu->items[index + 1],
325                 (menu->item_count - index + 1) * sizeof(ITEM *));
326         menu->items = talloc_realloc(menu, menu->items, ITEM *,
327                 menu->item_count + 1);
328
329         return 0;
330 }
331
332 /**
333  * pmenu_init - Allocate and initialize a new menu instance.
334  *
335  * Returns a pointer the the initialized struct pmenu instance or NULL on error.
336  * The caller is responsible for calling talloc_free() for the returned
337  * instance.
338  */
339
340 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
341         void (*on_exit)(struct pmenu *))
342 {
343         struct pmenu *menu = talloc_zero(ui_ctx, struct pmenu);
344
345         if (!menu)
346                 return NULL;
347
348         /* note items array has a null terminator */
349
350         menu->items = talloc_zero_array(menu, ITEM *, item_count + 1);
351
352         if (!menu->items) {
353                 talloc_free(menu);
354                 return NULL;
355         }
356
357         nc_scr_init(&menu->scr, pb_pmenu_sig, 0, ui_ctx, pmenu_process_key,
358                 pmenu_post, pmenu_unpost, pmenu_resize);
359
360         menu->item_count = item_count;
361         menu->insert_pt = 0; /* insert from top */
362         menu->on_exit = on_exit;
363
364         return menu;
365 }
366
367 /**
368  * pmenu_setup - Create nc menu, setup nc windows.
369  *
370  */
371
372 int pmenu_setup(struct pmenu *menu)
373 {
374         assert(!menu->ncm);
375
376         menu->ncm = new_menu(menu->items);
377
378         if (!menu->ncm) {
379                 pb_log("%s:%d: new_menu failed: %s\n", __func__, __LINE__,
380                         strerror(errno));
381                 return -1;
382         }
383
384         set_menu_win(menu->ncm, menu->scr.main_ncw);
385         set_menu_sub(menu->ncm, menu->scr.sub_ncw);
386
387         /* Makes menu scrollable. */
388         set_menu_format(menu->ncm, LINES - nc_scr_frame_lines, 1);
389
390         return 0;
391 }
392
393 /**
394  * pmenu_delete - Delete a menu instance.
395  *
396  */
397
398 void pmenu_delete(struct pmenu *menu)
399 {
400         unsigned int i;
401
402         assert(menu->scr.sig == pb_pmenu_sig);
403         menu->scr.sig = pb_removed_sig;
404
405         for (i = item_count(menu->ncm); i; i--)
406                 free_item(menu->items[i - 1]);
407
408         free_menu(menu->ncm);
409         delwin(menu->scr.sub_ncw);
410         delwin(menu->scr.main_ncw);
411         talloc_free(menu);
412 }