blob: f187b5d82b39dfc781fac2a8fb2651e47b6c4f44 [file] [log] [blame]
Daniel Stenberg1c0c8612002-05-17 12:22:24 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Robert E. Hak
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
Björn Stenberg2ac05722002-05-26 17:03:17 +000019#include <stdbool.h>
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +000020#include <stdlib.h>
Justin Heinerb5025a82002-08-31 04:58:35 +000021
Linus Nielsen Feltzing0a4b2472002-10-15 12:25:57 +000022#include "hwcompat.h"
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000023#include "lcd.h"
Daniel Stenberg93b231c2002-09-12 13:33:59 +000024#include "font.h"
Justin Heinerb5025a82002-08-31 04:58:35 +000025#include "backlight.h"
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000026#include "menu.h"
Björn Stenbergb21a3bd2002-05-21 14:25:45 +000027#include "button.h"
28#include "kernel.h"
29#include "debug.h"
Justin Heiner26302452002-08-23 02:17:00 +000030#include "usb.h"
Björn Stenbergcd225732002-08-11 09:17:47 +000031#include "panic.h"
Markus Braun5e4c1d22002-08-20 19:37:00 +000032#include "settings.h"
33#include "status.h"
Björn Stenberga4c3b032002-09-24 18:04:15 +000034#include "screens.h"
Markus Braun000c2db2002-08-30 13:49:32 +000035
Daniel Stenbergbf603f92002-06-14 08:47:44 +000036#ifdef HAVE_LCD_BITMAP
37#include "icons.h"
Markus Braun000c2db2002-08-30 13:49:32 +000038#include "widgets.h"
Daniel Stenbergbf603f92002-06-14 08:47:44 +000039#endif
Markus Braun000c2db2002-08-30 13:49:32 +000040
Björn Stenberg2ac05722002-05-26 17:03:17 +000041struct menu {
Björn Stenbergf76cee72002-05-28 15:35:33 +000042 int top;
Björn Stenberg2ac05722002-05-26 17:03:17 +000043 int cursor;
44 struct menu_items* items;
45 int itemcount;
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +000046 int (*callback)(int, int);
Björn Stenberg2ac05722002-05-26 17:03:17 +000047};
48
Hardeep Sidhu107ebc52004-01-26 17:05:21 +000049#define MAX_MENUS 5
Björn Stenberg2ac05722002-05-26 17:03:17 +000050
Björn Stenbergf76cee72002-05-28 15:35:33 +000051#ifdef HAVE_LCD_BITMAP
Markus Braun000c2db2002-08-30 13:49:32 +000052
Björn Stenberg8fb73122002-09-24 19:41:23 +000053/* pixel margins */
54#define MARGIN_X (global_settings.scrollbar && \
55 menu_lines < menus[m].itemcount ? SCROLLBAR_WIDTH : 0) +\
56 CURSOR_WIDTH
57#define MARGIN_Y (global_settings.statusbar ? STATUSBAR_HEIGHT : 0)
Markus Braun000c2db2002-08-30 13:49:32 +000058
Björn Stenberg8fb73122002-09-24 19:41:23 +000059/* position the entry-list starts at */
60#define LINE_X 0
61#define LINE_Y (global_settings.statusbar ? 1 : 0)
Markus Braun000c2db2002-08-30 13:49:32 +000062
Björn Stenberg8fb73122002-09-24 19:41:23 +000063#define CURSOR_X (global_settings.scrollbar && \
64 menu_lines < menus[m].itemcount ? 1 : 0)
65#define CURSOR_Y 0 /* the cursor is not positioned in regard to
66 the margins, so this is the amount of lines
67 we add to the cursor Y position to position
68 it on a line */
Linus Nielsen Feltzinge43b78a2003-04-16 00:12:31 +000069#define CURSOR_WIDTH (global_settings.invert_cursor ? 0 : 4)
Markus Braun000c2db2002-08-30 13:49:32 +000070
71#define SCROLLBAR_X 0
72#define SCROLLBAR_Y lcd_getymargin()
73#define SCROLLBAR_WIDTH 6
74
75#else /* HAVE_LCD_BITMAP */
76
Justin Heinerb5025a82002-08-31 04:58:35 +000077#define LINE_X 1 /* X position the entry-list starts at */
Markus Braun000c2db2002-08-30 13:49:32 +000078
Björn Stenbergf76cee72002-05-28 15:35:33 +000079#define MENU_LINES 2
Markus Braun000c2db2002-08-30 13:49:32 +000080
81#define CURSOR_X 0
82#define CURSOR_Y 0 /* not really used for players */
83
84#endif /* HAVE_LCD_BITMAP */
Björn Stenbergf76cee72002-05-28 15:35:33 +000085
Kjell Ericsonc11b90f2003-01-10 09:55:50 +000086#define CURSOR_CHAR 0x92
Björn Stenberg9314a682002-05-31 09:04:51 +000087
Björn Stenberg2ac05722002-05-26 17:03:17 +000088static struct menu menus[MAX_MENUS];
89static bool inuse[MAX_MENUS] = { false };
Daniel Stenberg1c0c8612002-05-17 12:22:24 +000090
Daniel Stenberg93b231c2002-09-12 13:33:59 +000091/* count in letter positions, NOT pixels */
Daniel Stenbergbf603f92002-06-14 08:47:44 +000092void put_cursorxy(int x, int y, bool on)
93{
Björn Stenberg71f71ef2002-08-11 09:20:53 +000094#ifdef HAVE_LCD_BITMAP
Daniel Stenberg93b231c2002-09-12 13:33:59 +000095 int fh, fw;
Björn Stenbergbed3d3f2002-09-20 08:07:51 +000096 int xpos, ypos;
Björn Stenbergd1a6fa12003-06-05 09:38:26 +000097
98 /* check here instead of at every call (ugly, but cheap) */
Linus Nielsen Feltzinge43b78a2003-04-16 00:12:31 +000099 if (global_settings.invert_cursor)
100 return;
Björn Stenbergd1a6fa12003-06-05 09:38:26 +0000101
Björn Stenberga4c3b032002-09-24 18:04:15 +0000102 lcd_getstringsize("A", &fw, &fh);
Björn Stenbergbed3d3f2002-09-20 08:07:51 +0000103 xpos = x*6;
104 ypos = y*fh + lcd_getymargin();
105 if ( fh > 8 )
106 ypos += (fh - 8) / 2;
Björn Stenberg71f71ef2002-08-11 09:20:53 +0000107#endif
Björn Stenbergcd225732002-08-11 09:17:47 +0000108
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000109 /* place the cursor */
110 if(on) {
111#ifdef HAVE_LCD_BITMAP
112 lcd_bitmap ( bitmap_icons_6x8[Cursor],
Björn Stenbergbed3d3f2002-09-20 08:07:51 +0000113 xpos, ypos, 4, 8, true);
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000114#else
Kjell Ericsonc11b90f2003-01-10 09:55:50 +0000115 lcd_putc(x, y, CURSOR_CHAR);
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000116#endif
117 }
118 else {
Daniel Stenbergc4be1ee2002-06-19 13:00:14 +0000119#if defined(HAVE_LCD_BITMAP)
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000120 /* I use xy here since it needs to disregard the margins */
Björn Stenbergbed3d3f2002-09-20 08:07:51 +0000121 lcd_clearrect (xpos, ypos, 4, 8);
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000122#else
Björn Stenberg3b974742002-09-09 23:57:00 +0000123 lcd_putc(x, y, ' ');
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000124#endif
125 }
126}
127
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000128void menu_draw(int m)
Björn Stenbergf76cee72002-05-28 15:35:33 +0000129{
130 int i = 0;
Eric Linenberg038df5c2002-09-16 03:18:49 +0000131#ifdef HAVE_LCD_BITMAP
Daniel Stenberg93b231c2002-09-12 13:33:59 +0000132 int fw, fh;
Björn Stenbergcd225732002-08-11 09:17:47 +0000133 int menu_lines;
Björn Stenberga4c3b032002-09-24 18:04:15 +0000134 lcd_setfont(FONT_UI);
135 lcd_getstringsize("A", &fw, &fh);
Markus Braun5e4c1d22002-08-20 19:37:00 +0000136 if (global_settings.statusbar)
137 menu_lines = (LCD_HEIGHT - STATUSBAR_HEIGHT) / fh;
138 else
139 menu_lines = LCD_HEIGHT/fh;
Björn Stenbergcd225732002-08-11 09:17:47 +0000140#else
141 int menu_lines = MENU_LINES;
142#endif
Björn Stenbergf76cee72002-05-28 15:35:33 +0000143
Daniel Stenberga8e89fd2002-08-22 21:45:22 +0000144 lcd_clear_display(); /* ...then clean the screen */
Björn Stenbergf76cee72002-05-28 15:35:33 +0000145#ifdef HAVE_LCD_BITMAP
Markus Braun000c2db2002-08-30 13:49:32 +0000146 lcd_setmargins(MARGIN_X,MARGIN_Y); /* leave room for cursor and icon */
Björn Stenbergf76cee72002-05-28 15:35:33 +0000147#endif
Markus Braun5e4c1d22002-08-20 19:37:00 +0000148 /* correct cursor pos if out of screen */
149 if (menus[m].cursor - menus[m].top >= menu_lines)
150 menus[m].top++;
151
Björn Stenbergf76cee72002-05-28 15:35:33 +0000152 for (i = menus[m].top;
Björn Stenbergcd225732002-08-11 09:17:47 +0000153 (i < menus[m].itemcount) && (i<menus[m].top+menu_lines);
Björn Stenbergf76cee72002-05-28 15:35:33 +0000154 i++) {
Linus Nielsen Feltzing8fa02172002-08-02 07:59:25 +0000155 if((menus[m].cursor - menus[m].top)==(i-menus[m].top))
Linus Nielsen Feltzinge43b78a2003-04-16 00:12:31 +0000156#ifdef HAVE_LCD_BITMAP
157 if (global_settings.invert_cursor)
158 lcd_puts_scroll_style(LINE_X, i-menus[m].top,
159 menus[m].items[i].desc, STYLE_INVERT);
160 else
161#endif
162 lcd_puts_scroll(LINE_X, i-menus[m].top, menus[m].items[i].desc);
Linus Nielsen Feltzing8fa02172002-08-02 07:59:25 +0000163 else
Markus Braun000c2db2002-08-30 13:49:32 +0000164 lcd_puts(LINE_X, i-menus[m].top, menus[m].items[i].desc);
Björn Stenbergf76cee72002-05-28 15:35:33 +0000165 }
Robert Hakc501ac72002-05-30 08:06:42 +0000166
Daniel Stenbergbf603f92002-06-14 08:47:44 +0000167 /* place the cursor */
Markus Braun000c2db2002-08-30 13:49:32 +0000168 put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, true);
Markus Braun5e4c1d22002-08-20 19:37:00 +0000169#ifdef HAVE_LCD_BITMAP
Björn Stenberg8fb73122002-09-24 19:41:23 +0000170 if (global_settings.scrollbar && menus[m].itemcount > menu_lines)
Markus Braun000c2db2002-08-30 13:49:32 +0000171 scrollbar(SCROLLBAR_X, SCROLLBAR_Y, SCROLLBAR_WIDTH - 1,
172 LCD_HEIGHT - SCROLLBAR_Y, menus[m].itemcount, menus[m].top,
173 menus[m].top + menu_lines, VERTICAL);
Markus Braun5e4c1d22002-08-20 19:37:00 +0000174#endif
Björn Stenberg942bc942003-04-23 11:26:25 +0000175 status_draw(true);
Björn Stenbergf76cee72002-05-28 15:35:33 +0000176 lcd_update();
177}
178
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000179/*
180 * Move the cursor to a particular id,
181 * target: where you want it to be
182 */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000183static void put_cursor(int m, int target)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000184{
Robert Hakc501ac72002-05-30 08:06:42 +0000185 bool do_update = true;
Eric Linenberg038df5c2002-09-16 03:18:49 +0000186#ifdef HAVE_LCD_BITMAP
Daniel Stenberg93b231c2002-09-12 13:33:59 +0000187 int fw, fh;
Björn Stenbergcd225732002-08-11 09:17:47 +0000188 int menu_lines;
Linus Nielsen Feltzingf1659a32003-11-20 01:51:15 +0000189 lcd_setfont(FONT_UI);
Björn Stenberga4c3b032002-09-24 18:04:15 +0000190 lcd_getstringsize("A", &fw, &fh);
Markus Braun5e4c1d22002-08-20 19:37:00 +0000191 if (global_settings.statusbar)
Daniel Stenberg93b231c2002-09-12 13:33:59 +0000192 menu_lines = (LCD_HEIGHT - STATUSBAR_HEIGHT) / fh;
Markus Braun5e4c1d22002-08-20 19:37:00 +0000193 else
194 menu_lines = LCD_HEIGHT/fh;
Björn Stenbergcd225732002-08-11 09:17:47 +0000195#else
196 int menu_lines = MENU_LINES;
197#endif
Daniel Stenberg93b231c2002-09-12 13:33:59 +0000198
Markus Braun000c2db2002-08-30 13:49:32 +0000199 put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, false);
Robert Hakc501ac72002-05-30 08:06:42 +0000200 menus[m].cursor = target;
Linus Nielsen Feltzing8fa02172002-08-02 07:59:25 +0000201 menu_draw(m);
Björn Stenbergf76cee72002-05-28 15:35:33 +0000202
203 if ( target < menus[m].top ) {
204 menus[m].top--;
205 menu_draw(m);
Robert Hakc501ac72002-05-30 08:06:42 +0000206 do_update = false;
Björn Stenbergf76cee72002-05-28 15:35:33 +0000207 }
Björn Stenbergcd225732002-08-11 09:17:47 +0000208 else if ( target-menus[m].top > menu_lines-1 ) {
Björn Stenbergf76cee72002-05-28 15:35:33 +0000209 menus[m].top++;
210 menu_draw(m);
Robert Hakc501ac72002-05-30 08:06:42 +0000211 do_update = false;
Björn Stenbergf76cee72002-05-28 15:35:33 +0000212 }
Robert Hakc501ac72002-05-30 08:06:42 +0000213
Linus Nielsen Feltzinge43b78a2003-04-16 00:12:31 +0000214 if (do_update && !global_settings.invert_cursor) {
215 put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, true);
Robert Hakc501ac72002-05-30 08:06:42 +0000216 lcd_update();
217 }
218
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000219}
220
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000221int menu_init(struct menu_items* mitems, int count, int (*callback)(int, int))
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000222{
Björn Stenberg2ac05722002-05-26 17:03:17 +0000223 int i;
224
225 for ( i=0; i<MAX_MENUS; i++ ) {
226 if ( !inuse[i] ) {
227 inuse[i] = true;
228 break;
229 }
230 }
231 if ( i == MAX_MENUS ) {
232 DEBUGF("Out of menus!\n");
233 return -1;
234 }
235 menus[i].items = mitems;
236 menus[i].itemcount = count;
Björn Stenbergf76cee72002-05-28 15:35:33 +0000237 menus[i].top = 0;
Björn Stenberg2ac05722002-05-26 17:03:17 +0000238 menus[i].cursor = 0;
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000239 menus[i].callback = callback;
Björn Stenberg2ac05722002-05-26 17:03:17 +0000240
241 return i;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000242}
243
Björn Stenberg2ac05722002-05-26 17:03:17 +0000244void menu_exit(int m)
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000245{
Björn Stenberg2ac05722002-05-26 17:03:17 +0000246 inuse[m] = false;
Daniel Stenberg1c0c8612002-05-17 12:22:24 +0000247}
248
Kjell Ericson5cd393c2003-01-29 08:26:11 +0000249int menu_show(int m)
Robert Hak7ec9aa32002-05-18 11:41:37 +0000250{
Björn Stenbergb1b8bd42002-09-24 17:22:12 +0000251 bool exit = false;
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000252 int key;
Linus Nielsen Feltzingf1659a32003-11-20 01:51:15 +0000253#ifdef HAVE_LCD_BITMAP
254 int fw, fh;
255 int menu_lines;
256 lcd_setfont(FONT_UI);
257 lcd_getstringsize("A", &fw, &fh);
258 if (global_settings.statusbar)
259 menu_lines = (LCD_HEIGHT - STATUSBAR_HEIGHT) / fh;
260 else
261 menu_lines = LCD_HEIGHT/fh;
Linus Nielsen Feltzingf1659a32003-11-20 01:51:15 +0000262#endif
Justin Heiner26302452002-08-23 02:17:00 +0000263
Björn Stenberg2ac05722002-05-26 17:03:17 +0000264 menu_draw(m);
Justin Heiner26302452002-08-23 02:17:00 +0000265
Björn Stenbergb1b8bd42002-09-24 17:22:12 +0000266 while (!exit) {
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000267 key = button_get_w_tmo(HZ/2);
268
269 /*
270 * "short-circuit" the default keypresses by running the callback function
271 */
272
273 if( menus[m].callback != NULL )
274 key = menus[m].callback(key, m); /* make sure there's no match in the switch */
275
276 switch( key ) {
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000277#ifdef HAVE_RECORDER_KEYPAD
278 case BUTTON_UP:
Björn Stenbergcbc71792002-08-10 09:04:55 +0000279 case BUTTON_UP | BUTTON_REPEAT:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000280#else
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000281 case BUTTON_LEFT:
Linus Nielsen Feltzing8fa02172002-08-02 07:59:25 +0000282 case BUTTON_LEFT | BUTTON_REPEAT:
Robert Hak7ec9aa32002-05-18 11:41:37 +0000283#endif
Björn Stenberg4caebab2002-05-29 14:25:06 +0000284 if (menus[m].cursor) {
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000285 /* move up */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000286 put_cursor(m, menus[m].cursor-1);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000287 }
Eric Linenbergc076b272002-09-04 04:21:10 +0000288 else {
289 /* move to bottom */
290#ifdef HAVE_RECORDER_KEYPAD
Linus Nielsen Feltzingf1659a32003-11-20 01:51:15 +0000291 menus[m].top = menus[m].itemcount-(menu_lines+1);
Eric Linenbergc076b272002-09-04 04:21:10 +0000292#else
293 menus[m].top = menus[m].itemcount-3;
294#endif
295 if (menus[m].top < 0)
296 menus[m].top = 0;
297 menus[m].cursor = menus[m].itemcount-1;
298 put_cursor(m, menus[m].itemcount-1);
299 }
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000300 break;
Robert Hakdfba2102002-05-21 08:47:34 +0000301
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000302#ifdef HAVE_RECORDER_KEYPAD
303 case BUTTON_DOWN:
Björn Stenbergcbc71792002-08-10 09:04:55 +0000304 case BUTTON_DOWN | BUTTON_REPEAT:
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000305#else
306 case BUTTON_RIGHT:
Linus Nielsen Feltzing8fa02172002-08-02 07:59:25 +0000307 case BUTTON_RIGHT | BUTTON_REPEAT:
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000308#endif
Björn Stenberg4caebab2002-05-29 14:25:06 +0000309 if (menus[m].cursor < menus[m].itemcount-1) {
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000310 /* move down */
Björn Stenberg2ac05722002-05-26 17:03:17 +0000311 put_cursor(m, menus[m].cursor+1);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000312 }
Eric Linenbergc076b272002-09-04 04:21:10 +0000313 else {
314 /* move to top */
315 menus[m].top = 0;
316 menus[m].cursor = 0;
317 put_cursor(m, 0);
318 }
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000319 break;
320
321#ifdef HAVE_RECORDER_KEYPAD
322 case BUTTON_RIGHT:
323#endif
324 case BUTTON_PLAY:
325 /* Erase current display state */
326 lcd_clear_display();
Kjell Ericson5cd393c2003-01-29 08:26:11 +0000327 return menus[m].cursor;
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000328
329#ifdef HAVE_RECORDER_KEYPAD
330 case BUTTON_LEFT:
Linus Nielsen Feltzinga552f4c2002-07-27 19:53:26 +0000331 case BUTTON_F1:
Björn Stenberg461d6e32003-12-04 00:08:25 +0000332 case BUTTON_OFF | BUTTON_REPEAT:
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000333#else
334 case BUTTON_STOP:
Björn Stenberg60fad542002-05-29 12:28:08 +0000335 case BUTTON_MENU:
Björn Stenberg461d6e32003-12-04 00:08:25 +0000336 case BUTTON_STOP | BUTTON_REPEAT:
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000337#endif
Kjell Ericson767d6042003-01-23 14:28:16 +0000338 lcd_stop_scroll();
Björn Stenbergb1b8bd42002-09-24 17:22:12 +0000339 exit = true;
340 break;
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000341
Justin Heiner26302452002-08-23 02:17:00 +0000342 case SYS_USB_CONNECTED:
Björn Stenbergb1b8bd42002-09-24 17:22:12 +0000343 usb_screen();
Björn Stenberg40d866b2002-09-01 20:08:08 +0000344#ifdef HAVE_LCD_CHARCELLS
Mats Lidell18739872002-10-15 12:56:05 +0000345 status_set_param(false);
Justin Heiner26302452002-08-23 02:17:00 +0000346#endif
Kjell Ericson5cd393c2003-01-29 08:26:11 +0000347 return MENU_ATTACHED_USB;
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000348 }
349
Björn Stenberg942bc942003-04-23 11:26:25 +0000350 status_draw(false);
Björn Stenbergb21a3bd2002-05-21 14:25:45 +0000351 }
Kjell Ericson5cd393c2003-01-29 08:26:11 +0000352 return MENU_SELECTED_EXIT;
353}
Daniel Stenbergb2850762002-08-23 12:32:52 +0000354
Kjell Ericson5cd393c2003-01-29 08:26:11 +0000355
356bool menu_run(int m)
357{
358 bool stop=false;
359 while (!stop) {
360 int result=menu_show(m);
361 if (result == MENU_SELECTED_EXIT)
362 return false;
363 else if (result == MENU_ATTACHED_USB)
364 return true;
365 if (menus[m].items[menus[m].cursor].function()) {
366 return true;
367 }
368 }
369 return false;
Robert Hak7ec9aa32002-05-18 11:41:37 +0000370}
Linus Nielsen Feltzing10b92c42004-03-12 10:20:33 +0000371
372/*
373 * Property function - return the current cursor for "menu"
374 */
375
376int menu_cursor(int menu)
377{
378 return menus[menu].cursor;
379}
380
381/*
382 * Property function - return the "menu" description at "position"
383 */
384
385char* menu_description(int menu, int position)
386{
387 return menus[menu].items[position].desc;
388}
389
390/*
391 * Delete the element "position" from the menu items in "menu"
392 */
393
394void menu_delete(int menu, int position)
395{
396 int i;
397
398 /* copy the menu item from the one below */
399 for( i = position; i < (menus[menu].itemcount - 1); i++)
400 menus[menu].items[i] = menus[menu].items[i + 1];
401
402 /* reduce the count */
403 menus[menu].itemcount--;
404
405 /* adjust if this was the last menu item and the cursor was on it */
406 if( menus[menu].itemcount <= menus[menu].cursor)
407 menus[menu].cursor = menus[menu].itemcount - 1;
408}
409
410/*
411 * Property function - return the "count" of menu items in "menu"
412 */
413
414int menu_count(int menu)
415{
416 return menus[menu].itemcount;
417}
418
419/*
420 * Allows a menu item at the current cursor position in "menu" to be moved up the list
421 */
422
423bool menu_moveup(int menu)
424{
425 struct menu_items swap;
426
427 /* can't be the first item ! */
428 if( menus[menu].cursor == 0)
429 return false;
430
431 /* use a temporary variable to do the swap */
432 swap = menus[menu].items[menus[menu].cursor - 1];
433 menus[menu].items[menus[menu].cursor - 1] = menus[menu].items[menus[menu].cursor];
434 menus[menu].items[menus[menu].cursor] = swap;
435 menus[menu].cursor--;
436
437 return true;
438}
439
440/*
441 * Allows a menu item at the current cursor position in "menu" to be moved down the list
442 */
443
444bool menu_movedown(int menu)
445{
446 struct menu_items swap;
447
448 /* can't be the last item ! */
449 if( menus[menu].cursor == menus[menu].itemcount - 1)
450 return false;
451
452 /* use a temporary variable to do the swap */
453 swap = menus[menu].items[menus[menu].cursor + 1];
454 menus[menu].items[menus[menu].cursor + 1] = menus[menu].items[menus[menu].cursor];
455 menus[menu].items[menus[menu].cursor] = swap;
456 menus[menu].cursor++;
457
458 return true;
459}