Added old.t
[kugel-rb.git] / apps / menu.c
blob26df94b5a7513436479852f6d3fd6a518ef515fb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Robert E. Hak
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 2005 Kevin Ferrare :
21 - Multi screen support
22 - Rewrote/removed a lot of code now useless with the new gui API
24 #include <stdbool.h>
25 #include <stdlib.h>
27 #include "hwcompat.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "menu.h"
32 #include "button.h"
33 #include "kernel.h"
34 #include "debug.h"
35 #include "usb.h"
36 #include "panic.h"
37 #include "settings.h"
38 #include "status.h"
39 #include "screens.h"
40 #include "talk.h"
41 #include "lang.h"
42 #include "misc.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "icons.h"
46 #endif
48 /* gui api */
49 #include "list.h"
50 #include "statusbar.h"
51 #include "buttonbar.h"
53 struct menu {
54 struct menu_item* items;
55 int (*callback)(int, int);
56 #ifdef HAS_BUTTONBAR
57 struct gui_buttonbar buttonbar;
58 #endif
59 struct gui_synclist synclist;
62 #define MAX_MENUS 5
64 static struct menu menus[MAX_MENUS];
65 static bool inuse[MAX_MENUS] = { false };
67 char * menu_get_itemname(int selected_item, void * data, char *buffer)
69 struct menu *local_menus=(struct menu *)data;
70 (void)buffer;
71 return(P2STR(local_menus->items[selected_item].desc));
74 int menu_find_free(void)
76 int i;
77 /* Tries to find an unused slot to put the new menu */
78 for ( i=0; i<MAX_MENUS; i++ ) {
79 if ( !inuse[i] ) {
80 inuse[i] = true;
81 break;
84 if ( i == MAX_MENUS ) {
85 DEBUGF("Out of menus!\n");
86 return -1;
88 return(i);
91 int menu_init(const struct menu_item* mitems, int count, int (*callback)(int, int),
92 const char *button1, const char *button2, const char *button3)
94 int menu=menu_find_free();
95 if(menu==-1)/* Out of menus */
96 return -1;
97 menus[menu].items = (struct menu_item*)mitems; /* de-const */
98 gui_synclist_init(&(menus[menu].synclist),
99 &menu_get_itemname, &menus[menu]);
100 gui_synclist_set_icon_callback(&(menus[menu].synclist), NULL);
101 gui_synclist_set_nb_items(&(menus[menu].synclist), count);
102 menus[menu].callback = callback;
103 #ifdef HAS_BUTTONBAR
104 gui_buttonbar_init(&(menus[menu].buttonbar));
105 gui_buttonbar_set_display(&(menus[menu].buttonbar), &(screens[SCREEN_MAIN]) );
106 gui_buttonbar_set(&(menus[menu].buttonbar), button1, button2, button3);
107 #else
108 (void)button1;
109 (void)button2;
110 (void)button3;
111 #endif
112 return menu;
115 void menu_exit(int m)
117 inuse[m] = false;
120 int menu_show(int m)
122 #ifdef HAS_BUTTONBAR
123 gui_buttonbar_draw(&(menus[m].buttonbar));
124 #endif
125 bool exit = false;
126 int key;
128 gui_synclist_draw(&(menus[m].synclist));
129 gui_syncstatusbar_draw(&statusbars, true);
130 menu_talk_selected(m);
131 while (!exit) {
132 key = button_get_w_tmo(HZ/2);
134 * "short-circuit" the default keypresses by running the
135 * callback function
136 * The callback may return a new key value, often this will be
137 * BUTTON_NONE or the same key value, but it's perfectly legal
138 * to "simulate" key presses by returning another value.
140 if( menus[m].callback != NULL )
141 key = menus[m].callback(key, m);
142 /* If moved, "say" the entry under the cursor */
143 if(gui_synclist_do_button(&(menus[m].synclist), key))
144 menu_talk_selected(m);
145 switch( key ) {
146 case MENU_ENTER:
147 #ifdef MENU_ENTER2
148 case MENU_ENTER2:
149 #endif
150 #ifdef MENU_RC_ENTER
151 case MENU_RC_ENTER:
152 #endif
153 #ifdef MENU_RC_ENTER2
154 case MENU_RC_ENTER2:
155 #endif
156 return gui_synclist_get_sel_pos(&(menus[m].synclist));
159 case MENU_EXIT:
160 #ifdef MENU_EXIT2
161 case MENU_EXIT2:
162 #endif
163 #ifdef MENU_EXIT_MENU
164 case MENU_EXIT_MENU:
165 #endif
166 #ifdef MENU_RC_EXIT
167 case MENU_RC_EXIT:
168 #endif
169 #ifdef MENU_RC_EXIT_MENU
170 case MENU_RC_EXIT_MENU:
171 #endif
172 exit = true;
173 break;
175 default:
176 if(default_event_handler(key) == SYS_USB_CONNECTED)
177 return MENU_ATTACHED_USB;
178 break;
180 gui_syncstatusbar_draw(&statusbars, false);
182 return MENU_SELECTED_EXIT;
186 bool menu_run(int m)
188 int selected;
189 while (1) {
190 switch (selected=menu_show(m))
192 case MENU_SELECTED_EXIT:
193 return false;
195 case MENU_ATTACHED_USB:
196 return true;
198 default:
200 if (menus[m].items[selected].function &&
201 menus[m].items[selected].function())
202 return true;
203 gui_syncstatusbar_draw(&statusbars, true);
207 return false;
211 * Property function - return the current cursor for "menu"
214 int menu_cursor(int menu)
216 return gui_synclist_get_sel_pos(&(menus[menu].synclist));
220 * Property function - return the "menu" description at "position"
223 char* menu_description(int menu, int position)
225 return P2STR(menus[menu].items[position].desc);
229 * Delete the element "position" from the menu items in "menu"
232 void menu_delete(int menu, int position)
234 int i;
235 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
236 /* copy the menu item from the one below */
237 for( i = position; i < nb_items - 1; i++)
238 menus[menu].items[i] = menus[menu].items[i + 1];
240 gui_synclist_del_item(&(menus[menu].synclist));
243 void menu_insert(int menu, int position, char *desc, bool (*function) (void))
245 int i;
246 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
247 if(position < 0)
248 position = nb_items;
250 /* Move the items below one position forward */
251 for( i = nb_items; i > position; i--)
252 menus[menu].items[i] = menus[menu].items[i - 1];
254 /* Update the current item */
255 menus[menu].items[position].desc = (unsigned char *)desc;
256 menus[menu].items[position].function = function;
257 gui_synclist_add_item(&(menus[menu].synclist));
261 * Property function - return the "count" of menu items in "menu"
264 int menu_count(int menu)
266 return gui_synclist_get_nb_items(&(menus[menu].synclist));
270 * Allows a menu item at the current cursor position in "menu"
271 * to be moved up the list
274 bool menu_moveup(int menu)
276 struct menu_item swap;
277 int selected=menu_cursor(menu);
278 /* can't be the first item ! */
279 if( selected == 0)
280 return false;
282 /* use a temporary variable to do the swap */
283 swap = menus[menu].items[selected - 1];
284 menus[menu].items[selected - 1] = menus[menu].items[selected];
285 menus[menu].items[selected] = swap;
287 gui_synclist_select_previous(&(menus[menu].synclist));
288 return true;
292 * Allows a menu item at the current cursor position in "menu" to be moved down the list
295 bool menu_movedown(int menu)
297 struct menu_item swap;
298 int selected=menu_cursor(menu);
299 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
301 /* can't be the last item ! */
302 if( selected == nb_items - 1)
303 return false;
305 /* use a temporary variable to do the swap */
306 swap = menus[menu].items[selected + 1];
307 menus[menu].items[selected + 1] = menus[menu].items[selected];
308 menus[menu].items[selected] = swap;
310 gui_synclist_select_next(&(menus[menu].synclist));
311 return true;
315 * Allows to set the cursor position. Doesn't redraw by itself.
318 void menu_set_cursor(int menu, int position)
320 gui_synclist_select_item(&(menus[menu].synclist), position);
323 void menu_talk_selected(int m)
325 if(global_settings.talk_menu)
327 int selected=gui_synclist_get_sel_pos(&(menus[m].synclist));
328 int voice_id = P2ID(menus[m].items[selected].desc);
329 if (voice_id >= 0) /* valid ID given? */
330 talk_id(voice_id, false); /* say it */
334 void menu_draw(int m)
336 gui_synclist_draw(&(menus[m].synclist));
339 /* count in letter positions, NOT pixels */
340 void put_cursorxy(int x, int y, bool on)
342 #ifdef HAVE_LCD_BITMAP
343 int fh, fw;
344 int xpos, ypos;
346 /* check here instead of at every call (ugly, but cheap) */
347 if (global_settings.invert_cursor)
348 return;
350 lcd_getstringsize((unsigned char *)"A", &fw, &fh);
351 xpos = x*6;
352 ypos = y*fh + lcd_getymargin();
353 if ( fh > 8 )
354 ypos += (fh - 8) / 2;
355 #endif
357 /* place the cursor */
358 if(on) {
359 #ifdef HAVE_LCD_BITMAP
360 lcd_mono_bitmap(bitmap_icons_6x8[Icon_Cursor], xpos, ypos, 4, 8);
361 #else
362 lcd_putc(x, y, CURSOR_CHAR);
363 #endif
365 else {
366 #if defined(HAVE_LCD_BITMAP)
367 /* I use xy here since it needs to disregard the margins */
368 lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
369 lcd_fillrect (xpos, ypos, 4, 8);
370 lcd_set_drawmode(DRMODE_SOLID);
371 #else
372 lcd_putc(x, y, ' ');
373 #endif