summaryrefslogtreecommitdiff
path: root/apps/menu.c
blob: 459abe68490c9036f0e093180a7eb175173d6f38 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/***************************************************************************
 *             __________               __   ___.
 *   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 <stdlib.h>

#include "hwcompat.h"
#include "lcd.h"
#include "font.h"
#include "backlight.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"
#include "screens.h"
#include "talk.h"
#include "lang.h"
#include "misc.h"

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

struct menu {
    int top;
    int cursor;
    struct menu_item* items;
    int itemcount;
    int (*callback)(int, int);
#if defined(HAVE_LCD_BITMAP) && (CONFIG_KEYPAD == RECORDER_PAD)
    bool use_buttonbar; /* true if a buttonbar is defined */
    const char *buttonbar[3];
#endif
};

#define MAX_MENUS 5

#ifdef HAVE_LCD_BITMAP

/* pixel margins */
#define MARGIN_X (global_settings.scrollbar && \
                  menu_lines < menus[m].itemcount ? SCROLLBAR_WIDTH : 0) +\
                  CURSOR_WIDTH
#define MARGIN_Y (global_settings.statusbar ? STATUSBAR_HEIGHT : 0)

/* position the entry-list starts at */
#define LINE_X   0
#define LINE_Y   (global_settings.statusbar ? 1 : 0)

#define CURSOR_X (global_settings.scrollbar && \
                  menu_lines < menus[m].itemcount ? 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 (global_settings.invert_cursor ? 0 : 4)

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

#else /* HAVE_LCD_BITMAP */

#define LINE_X      1 /* 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 */

#define CURSOR_CHAR 0x92

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

/* count in letter positions, NOT pixels */
void put_cursorxy(int x, int y, bool on)
{
#ifdef HAVE_LCD_BITMAP
    int fh, fw;
    int xpos, ypos;

    /* check here instead of at every call (ugly, but cheap) */
    if (global_settings.invert_cursor)
        return;

    lcd_getstringsize("A", &fw, &fh);
    xpos = x*6;
    ypos = y*fh + lcd_getymargin();
    if ( fh > 8 )
        ypos += (fh - 8) / 2;
#endif

    /* place the cursor */
    if(on) {
#ifdef HAVE_LCD_BITMAP
        lcd_bitmap(bitmap_icons_6x8[Cursor], xpos, ypos, 4, 8);
#else
        lcd_putc(x, y, CURSOR_CHAR);
#endif
    }
    else {
#if defined(HAVE_LCD_BITMAP)
        /* I use xy here since it needs to disregard the margins */
        lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
        lcd_fillrect (xpos, ypos, 4, 8);
        lcd_set_drawmode(DRMODE_SOLID);
#else
        lcd_putc(x, y, ' ');
#endif
    }
}

void menu_draw(int m)
{
    int i = 0;
#ifdef HAVE_LCD_BITMAP
    int fw, fh;
    int menu_lines;
    int height = LCD_HEIGHT;
    
    lcd_setfont(FONT_UI);
    lcd_getstringsize("A", &fw, &fh);
    if (global_settings.statusbar)
        height -= STATUSBAR_HEIGHT;

#if CONFIG_KEYPAD == RECORDER_PAD
    if(global_settings.buttonbar && menus[m].use_buttonbar) {
        buttonbar_set(menus[m].buttonbar[0],
                      menus[m].buttonbar[1],
                      menus[m].buttonbar[2]);
        height -= BUTTONBAR_HEIGHT;
    }
#endif

    menu_lines = height / fh;
    
#else
    int menu_lines = MENU_LINES;
#endif

    lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
    lcd_setmargins(MARGIN_X,MARGIN_Y); /* leave room for cursor and icon */
#endif
    /* Adjust cursor pos if it's below the screen */
    if (menus[m].cursor - menus[m].top >= menu_lines)
        menus[m].top = menus[m].cursor - (menu_lines - 1);

    /* Adjust cursor pos if it's above the screen */
    if(menus[m].cursor < menus[m].top)
        menus[m].top = menus[m].cursor;

    for (i = menus[m].top; 
         (i < menus[m].itemcount) && (i<menus[m].top+menu_lines);
         i++) {

        /* We want to scroll the line where the cursor is */
        if((menus[m].cursor - menus[m].top)==(i-menus[m].top))
#ifdef HAVE_LCD_BITMAP
            if (global_settings.invert_cursor)
                lcd_puts_scroll_style(LINE_X, i-menus[m].top,
                                       P2STR(menus[m].items[i].desc), STYLE_INVERT);
            else
#endif
                lcd_puts_scroll(LINE_X, i-menus[m].top, P2STR(menus[m].items[i].desc));
        else
            lcd_puts(LINE_X, i-menus[m].top, P2STR(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 && menus[m].itemcount > menu_lines) 
        scrollbar(SCROLLBAR_X, SCROLLBAR_Y, SCROLLBAR_WIDTH - 1,
                  height, menus[m].itemcount, menus[m].top,
                  menus[m].top + menu_lines, VERTICAL);

#if CONFIG_KEYPAD == RECORDER_PAD
    if(global_settings.buttonbar && menus[m].use_buttonbar)
        buttonbar_draw();
#endif /* CONFIG_KEYPAD == RECORDER_PAD */
#endif /* HAVE_LCD_BITMAP */
    status_draw(true);

    lcd_update();
}

/* 
 * Move the cursor to a particular id, 
 *   target: where you want it to be 
 */
static void put_cursor(int m, int target)
{
    int voice_id;
    
    menus[m].cursor = target;
    menu_draw(m);

    /* "say" the entry under the cursor */
    if(global_settings.talk_menu)
    {
        voice_id = P2ID(menus[m].items[menus[m].cursor].desc);
        if (voice_id >= 0) /* valid ID given? */
            talk_id(voice_id, false); /* say it */
    }
}

int menu_init(const struct menu_item* mitems, int count, int (*callback)(int, int),
              const char *button1, const char *button2, const char *button3)
{
    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 = (struct menu_item*)mitems; /* de-const */
    menus[i].itemcount = count;
    menus[i].top = 0;
    menus[i].cursor = 0;
    menus[i].callback = callback;
#if defined(HAVE_LCD_BITMAP) && (CONFIG_KEYPAD == RECORDER_PAD)
    menus[i].buttonbar[0] = button1;
    menus[i].buttonbar[1] = button2;
    menus[i].buttonbar[2] = button3;

    if(button1 || button2 || button3)
        menus[i].use_buttonbar = true;
    else
        menus[i].use_buttonbar = false;
#else
    (void)button1;
    (void)button2;
    (void)button3;
#endif
    return i;
}

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

int menu_show(int m)
{
    bool exit = false;
    int key;
#ifdef HAVE_LCD_BITMAP
    int fw, fh;
    int menu_lines;
    int height = LCD_HEIGHT;
    
    lcd_setfont(FONT_UI);
    lcd_getstringsize("A", &fw, &fh);
    if (global_settings.statusbar)
        height -= STATUSBAR_HEIGHT;

#if CONFIG_KEYPAD == RECORDER_PAD
    if(global_settings.buttonbar && menus[m].use_buttonbar) {
        buttonbar_set(menus[m].buttonbar[0],
                      menus[m].buttonbar[1],
                      menus[m].buttonbar[2]);
        height -= BUTTONBAR_HEIGHT;
    }
#endif

    menu_lines = height / fh;
#else
    int menu_lines = MENU_LINES;
#endif

    /* Put the cursor on the first line and draw the menu */
    put_cursor(m, menus[m].cursor);

    while (!exit) {
        key = button_get_w_tmo(HZ/2);

        /*  
         * "short-circuit" the default keypresses by running the
         * callback function
         * The callback may return a new key value, often this will be
         * BUTTON_NONE or the same key value, but it's perfectly legal
         * to "simulate" key presses by returning another value.
         */

        if( menus[m].callback != NULL )
            key = menus[m].callback(key, m);
        
        switch( key ) {
            case MENU_PREV:
            case MENU_PREV | BUTTON_REPEAT:
#ifdef MENU_RC_PREV
            case MENU_RC_PREV:
            case MENU_RC_PREV | BUTTON_REPEAT:
#endif
                if (menus[m].cursor) {
                    /* keep the cursor at 1/3 of the screen */
                    if (menus[m].top && menus[m].cursor - menus[m].top <
                            menu_lines - (2 * menu_lines) / 3)
                        menus[m].top--;
                    /* move up */
                    put_cursor(m, menus[m].cursor-1);
                }
                else {
                    /* move to bottom */
                    menus[m].top = menus[m].itemcount-(menu_lines+1);
                    if (menus[m].top < 0)
                        menus[m].top = 0;
                    put_cursor(m, menus[m].itemcount-1);
                }
                break;

            case MENU_NEXT:
            case MENU_NEXT | BUTTON_REPEAT:
#ifdef MENU_RC_NEXT
            case MENU_RC_NEXT:
            case MENU_RC_NEXT | BUTTON_REPEAT:
#endif
                if (menus[m].cursor < menus[m].itemcount-1) {
                    /* keep the cursor at 2/3 of the screen */
                    if (menus[m].itemcount - menus[m].top > menu_lines &&
                        menus[m].cursor - menus[m].top >= (2 * menu_lines) / 3)
                        menus[m].top++;
                    /* move down */
                    put_cursor(m, menus[m].cursor+1);
                }
                else {
                    /* move to top */
                    menus[m].top = 0;
                    menus[m].cursor = 0;
                    put_cursor(m, 0);
                }
                break;

            case MENU_ENTER:
#ifdef MENU_ENTER2
            case MENU_ENTER2:
#endif
#ifdef MENU_RC_ENTER
            case MENU_RC_ENTER:
#endif
                /* Erase current display state */
                lcd_clear_display();
                return menus[m].cursor;

            case MENU_EXIT:
#ifdef MENU_EXIT2
            case MENU_EXIT2:
#endif
#ifdef MENU_EXIT_MENU
            case MENU_EXIT_MENU:
#endif
#ifdef MENU_RC_EXIT
            case MENU_RC_EXIT:
#endif
                lcd_stop_scroll();
                exit = true;
                break;

            default:
                if(default_event_handler(key) == SYS_USB_CONNECTED)
                    return MENU_ATTACHED_USB;
                break;
        }
        
        status_draw(false);
    }
    return MENU_SELECTED_EXIT;
}


bool menu_run(int m)
{
    while (1) {
        switch (menu_show(m))
        {
            case MENU_SELECTED_EXIT:
                return false;

            case MENU_ATTACHED_USB:
                return true;

            default:
                if ((menus[m].items[menus[m].cursor].function) &&
                    (menus[m].items[menus[m].cursor].function()))
                    return true;
        }
    }
    return false;
}

/*  
 *  Property function - return the current cursor for "menu"
 */

int menu_cursor(int menu)
{
    return menus[menu].cursor;
}

/*  
 *  Property function - return the "menu" description at "position"
 */

char* menu_description(int menu, int position)
{
    return P2STR(menus[menu].items[position].desc);
}

/*  
 *  Delete the element "position" from the menu items in "menu"
 */

void menu_delete(int menu, int position)
{
    int i;
    
    /* copy the menu item from the one below */
    for( i = position; i < (menus[menu].itemcount - 1); i++)
        menus[menu].items[i] = menus[menu].items[i + 1];
 
    /* reduce the count */       
    menus[menu].itemcount--;
    
    /* adjust if this was the last menu item and the cursor was on it */
    if(menus[menu].itemcount && menus[menu].itemcount <= menus[menu].cursor)
        menus[menu].cursor = menus[menu].itemcount - 1;
}

void menu_insert(int menu, int position, char *desc, bool (*function) (void))
{
    int i;

    if(position < 0)
       position = menus[menu].itemcount;

    /* Move the items below one position forward */
    for( i = menus[menu].itemcount; i > position; i--)
       menus[menu].items[i] = menus[menu].items[i - 1];

    /* Increase the count */
    menus[menu].itemcount++;
    
    /* Update the current item */
    menus[menu].items[position].desc = desc;
    menus[menu].items[position].function = function;
}

/*  
 *  Property function - return the "count" of menu items in "menu" 
 */

int menu_count(int menu)
{
    return menus[menu].itemcount;
}

/*  
 *  Allows a menu item at the current cursor position in "menu" to be moved up the list
 */

bool menu_moveup(int menu)
{
    struct menu_item swap;
    
    /* can't be the first item ! */
    if( menus[menu].cursor == 0)
        return false;
    
    /* use a temporary variable to do the swap */    
    swap = menus[menu].items[menus[menu].cursor - 1];
    menus[menu].items[menus[menu].cursor - 1] = menus[menu].items[menus[menu].cursor];
    menus[menu].items[menus[menu].cursor] = swap;
    menus[menu].cursor--;
    
    return true;
}

/*  
 *  Allows a menu item at the current cursor position in "menu" to be moved down the list
 */

bool menu_movedown(int menu)
{
    struct menu_item swap;
    
    /* can't be the last item ! */
    if( menus[menu].cursor == menus[menu].itemcount - 1)
        return false;
    
    /* use a temporary variable to do the swap */    
    swap = menus[menu].items[menus[menu].cursor + 1];
    menus[menu].items[menus[menu].cursor + 1] = menus[menu].items[menus[menu].cursor];
    menus[menu].items[menus[menu].cursor] = swap;
    menus[menu].cursor++;
    
    return true;
}

/*
 * Allows to set the cursor position. Doesn't redraw by itself.
 */

void menu_set_cursor(int menu, int position)
{
    menus[menu].cursor = position;
}