summaryrefslogtreecommitdiffstats
path: root/apps/menu.c
blob: f0c793a78029a75281954ff9dca74f9006fb51ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Robert E. Hak
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdbool.h>
#include "lcd.h"
#include "menu.h"
#include "button.h"
#include "kernel.h"
#include "debug.h"
#include "usb.h"
#include "panic.h"
#include "settings.h"
#include "status.h"

#ifdef HAVE_LCD_BITMAP
#include "icons.h"
#include "widgets.h"
#endif

#ifdef LOADABLE_FONTS
#include "ajf.h"
#endif

struct menu {
    int top;
    int cursor;
    struct menu_items* items;
    int itemcount;
};

#define MAX_MENUS 4

#ifdef HAVE_LCD_BITMAP

#define MARGIN_X    (global_settings.scrollbar ? SCROLLBAR_WIDTH : 0) + CURSOR_WIDTH /* X pixel margin */
#define MARGIN_Y    (global_settings.statusbar ? STATUSBAR_HEIGHT : 0)  /* Y pixel margin */

#define LINE_X      0 /* X position the entry-list starts at */
#define LINE_Y      (global_settings.statusbar ? 1 : 0) /* Y position the entry-list starts at */
#define LINE_HEIGTH 8 /* pixels for each text line */

#define MENU_LINES  (LCD_HEIGHT / LINE_HEIGTH - LINE_Y)

#define CURSOR_X    (global_settings.scrollbar ? 1 : 0)
#define CURSOR_Y    0 /* the cursor is not positioned in regard to
                         the margins, so this is the amount of lines
                         we add to the cursor Y position to position
                         it on a line */
#define CURSOR_WIDTH  4

#define SCROLLBAR_X      0
#define SCROLLBAR_Y      lcd_getymargin()
#define SCROLLBAR_WIDTH  6

#else /* HAVE_LCD_BITMAP */

#define LINE_X      0 /* X position the entry-list starts at */

#define MENU_LINES 2

#define CURSOR_X    0
#define CURSOR_Y    0 /* not really used for players */

#endif /* HAVE_LCD_BITMAP */

#ifdef HAVE_NEW_CHARCELL_LCD
#define CURSOR_CHAR "\x7e"
#else
#define CURSOR_CHAR "\x89"
#endif

static struct menu menus[MAX_MENUS];
static bool inuse[MAX_MENUS] = { false };

/* count in letter posistions, NOT pixels */
void put_cursorxy(int x, int y, bool on)
{
#ifdef HAVE_LCD_BITMAP
#ifdef LOADABLE_FONTS
    int fh;
    unsigned char* font = lcd_getcurrentldfont();
    fh = ajf_get_fontheight(font);
#else 
    int fh = 8;
#endif
#endif

    /* place the cursor */
    if(on) {
#ifdef HAVE_LCD_BITMAP
        lcd_bitmap ( bitmap_icons_6x8[Cursor], 
                     x*6, y*fh + lcd_getymargin(), 4, 8, true);
#elif defined(SIMULATOR)
        /* player simulator */
        unsigned char cursor[] = { 0x7f, 0x3e, 0x1c, 0x08 };
        lcd_bitmap ( cursor, x*6, 12+y*16, 4, 8, true);
#else
        lcd_puts(x, y, CURSOR_CHAR);
#endif
    }
    else {
#if defined(HAVE_LCD_BITMAP)
        /* I use xy here since it needs to disregard the margins */
        lcd_clearrect (x*6, y*fh + lcd_getymargin(), 4, 8);
#elif defined(SIMULATOR)
        /* player simulator in action */
        lcd_clearrect (x*6, 12+y*16, 4, 8);
#else
        lcd_puts(x, y, " ");
#endif
    }
}

static void menu_draw(int m)
{
    int i = 0;
#ifdef LOADABLE_FONTS
    int menu_lines;
    int fh;
    unsigned char* font = lcd_getcurrentldfont();
    fh = ajf_get_fontheight(font);
    if (global_settings.statusbar)
        menu_lines = (LCD_HEIGHT - STATUSBAR_HEIGHT) / fh;
    else
        menu_lines = LCD_HEIGHT/fh;
#else
    int menu_lines = MENU_LINES;
#endif

    lcd_scroll_pause();  /* halt scroll first... */
    lcd_clear_display(); /* ...then clean the screen */
#ifdef HAVE_LCD_BITMAP
    lcd_setmargins(MARGIN_X,MARGIN_Y); /* leave room for cursor and icon */
    lcd_setfont(0);
#endif
    /* correct cursor pos if out of screen */
    if (menus[m].cursor - menus[m].top >= menu_lines)
        menus[m].top++;

    for (i = menus[m].top; 
         (i < menus[m].itemcount) && (i<menus[m].top+menu_lines);
         i++) {
        if((menus[m].cursor - menus[m].top)==(i-menus[m].top))
            lcd_puts_scroll(LINE_X, i-menus[m].top, menus[m].items[i].desc);
        else
            lcd_puts(LINE_X, i-menus[m].top, menus[m].items[i].desc);
    }

    /* place the cursor */
    put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, true);
#ifdef HAVE_LCD_BITMAP
    if (global_settings.scrollbar) 
        scrollbar(SCROLLBAR_X, SCROLLBAR_Y, SCROLLBAR_WIDTH - 1,
                  LCD_HEIGHT - SCROLLBAR_Y, menus[m].itemcount, menus[m].top,
                  menus[m].top + menu_lines, VERTICAL);
#endif
    status_draw();
    lcd_update();
}

/* 
 * Move the cursor to a particular id, 
 *   target: where you want it to be 
 */
static void put_cursor(int m, int target)
{
    bool do_update = true;
#ifdef LOADABLE_FONTS
    int menu_lines;
    int fh;
    unsigned char* font = lcd_getcurrentldfont();
    fh = ajf_get_fontheight(font);
    if (global_settings.statusbar)
        menu_lines = (LCD_HEIGHT-STATUSBAR_HEIGHT)/fh;
    else
        menu_lines = LCD_HEIGHT/fh;
#else
    int menu_lines = MENU_LINES;
#endif
    put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, false);
    menus[m].cursor = target;
    menu_draw(m);

    if ( target < menus[m].top ) {
        menus[m].top--;
        menu_draw(m);
        do_update = false;
    }
    else if ( target-menus[m].top > menu_lines-1 ) {
        menus[m].top++;
        menu_draw(m);
        do_update = false;
    }

    if (do_update) {
        put_cursorxy(CURSOR_X, menus[m].cursor - menus[m].top, true); 
        lcd_update();
    }

}

int menu_init(struct menu_items* mitems, int count)
{
    int i;

    for ( i=0; i<MAX_MENUS; i++ ) {
        if ( !inuse[i] ) {
            inuse[i] = true;
            break;
        }
    }
    if ( i == MAX_MENUS ) {
        DEBUGF("Out of menus!\n");
        return -1;
    }
    menus[i].items = mitems;
    menus[i].itemcount = count;
    menus[i].top = 0;
    menus[i].cursor = 0;

    return i;
}

void menu_exit(int m)
{
    inuse[m] = false;
}

Menu menu_run(int m)
{
#ifndef SIMULATOR
#ifdef HAVE_LCD_BITMAP
    bool laststate;
#endif
#endif
    Menu result = MENU_OK;

    menu_draw(m);

    while(1) {
        switch( button_get_w_tmo(HZ/2) ) {
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_UP:
            case BUTTON_UP | BUTTON_REPEAT:
#else
            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
#endif
                if (menus[m].cursor) {
                    /* move up */
                    put_cursor(m, menus[m].cursor-1);
                }
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_DOWN:
            case BUTTON_DOWN | BUTTON_REPEAT:
#else
            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
#endif
                if (menus[m].cursor < menus[m].itemcount-1) {
                    /* move down */
                    put_cursor(m, menus[m].cursor+1);
                }
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_RIGHT:
#endif
            case BUTTON_PLAY:
                /* Erase current display state */
                lcd_scroll_pause(); /* pause is better than stop when
                                       are gonna clear the screen anyway */
                lcd_clear_display();
            
                /* if a child returns that the contents is changed, we
                   must remember this, even if we perhaps invoke other
                   children too before returning back */
                if(MENU_DISK_CHANGED ==
                   menus[m].items[menus[m].cursor].function())
                  result = MENU_DISK_CHANGED;
            
                /* Return to previous display state */
                menu_draw(m);
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_LEFT:
            case BUTTON_F1:
#else
            case BUTTON_STOP:
            case BUTTON_MENU:
#endif
                lcd_stop_scroll();
                return result;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F3: {
#ifdef HAVE_LCD_BITMAP
                  unsigned char state;
                  state = global_settings.statusbar << 1 | global_settings.scrollbar;
                  state = (state + 1) % 4;
                  global_settings.statusbar = state >> 1;
                  global_settings.scrollbar = state & 0x1;
                  settings_save();

                  menu_draw(m);
#endif
                }
                break;
#endif

#ifndef SIMULATOR
            case SYS_USB_CONNECTED:
#ifdef HAVE_LCD_BITMAP
                laststate = statusbar(false);
#endif
                usb_acknowledge(SYS_USB_CONNECTED_ACK);
                usb_wait_for_disconnect(&button_queue);
#ifdef HAVE_LCD_BITMAP
                statusbar(laststate);
#else
                lcd_icon(ICON_PARAM, true);
#endif
                menu_draw(m);
                result = MENU_DISK_CHANGED;
                break;
#endif

            default:
                break;
        }
        
        status_draw();
        lcd_update();
    }

    return result;
}