summaryrefslogtreecommitdiff
path: root/apps/playlist_viewer.c
blob: 8b9470cdcadcdf46f59a344aa94c557d775aa8b9 (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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
/***************************************************************************
 *
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2003 Hardeep Sidhu
 *
 * 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 <string.h>
#include <sprintf.h>
#include "playlist.h"
#include "mpeg.h"
#include "screens.h"
#include "status.h"
#include "settings.h"
#include "icons.h"
#include "menu.h"
#include "plugin.h"
#include "keyboard.h"
#include "tree.h"
#include "onplay.h"

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

#include "lang.h"

#include "playlist_viewer.h"

/* Defines for LCD display purposes.  Taken from tree.c */
#ifdef HAVE_LCD_BITMAP
    #define CURSOR_X        (global_settings.scrollbar && \
                             viewer.num_tracks>viewer.num_display_lines?1:0)
    #define CURSOR_Y        0 
    #define CURSOR_WIDTH    (global_settings.invert_cursor ? 0 : 4)

    #define ICON_WIDTH      ((viewer.char_width > 6) ? viewer.char_width : 6)

    #define MARGIN_X        ((global_settings.scrollbar && \
                             viewer.num_tracks > viewer.num_display_lines ? \
                             SCROLLBAR_WIDTH : 0) + CURSOR_WIDTH + \
                             (global_settings.playlist_viewer_icons ? \
                                ICON_WIDTH : 0))
    #define MARGIN_Y        (global_settings.statusbar ? STATUSBAR_HEIGHT : 0)

    #define LINE_X          0
    #define LINE_Y          (global_settings.statusbar ? 1 : 0)

    #define SCROLLBAR_X     0
    #define SCROLLBAR_Y     lcd_getymargin()
    #define SCROLLBAR_WIDTH 6
#else
    #define MARGIN_X        0
    #define MARGIN_Y        0
    #define LINE_X          2
    #define LINE_Y          0
    #define CURSOR_X        0
    #define CURSOR_Y        0
#endif

/* Maximum number of tracks we can have loaded at one time */
#define MAX_PLAYLIST_ENTRIES 200

/* Default playlist name for saving */
#define DEFAULT_PLAYLIST_NAME "/viewer.m3u"

/* Index of track on display line _pos */
#define INDEX(_pos) (viewer.first_display_index - viewer.first_index + (_pos))

/* Global playlist viewer settings */
struct playlist_viewer_info {
    struct playlist_info* playlist; /* playlist being viewed                */
    char *name_buffer;          /* Buffer used to store track names         */
    int buffer_size;            /* Size of name buffer                      */

    int num_display_lines;      /* Number of lines on lcd                   */
    int line_height;            /* Height (in pixels) of display line       */
    int char_width;             /* Width (in pixels) of a character         */

    int num_tracks;             /* Number of tracks in playlist             */
    int current_playing_track;  /* Index of current playing track           */

    int num_loaded;             /* Number of track entries loaded in viewer */
    int first_index;            /* Index of first loaded track              */
    int last_index;             /* Index of last loaded track               */
    int first_display_index;    /* Index of first track on display          */
    int last_display_index;     /* Index of last track on display           */
    int cursor_pos;             /* Line number of cursor                    */

    int move_track;             /* Playlist index of track to move or -1    */
};

/* Information about a specific track */
struct playlist_entry {
    char *name;                 /* Formatted track name                     */
    int index;                  /* Playlist index                           */
    int display_index;          /* Display index                            */
    bool queued;                /* Is track queued?                         */
};

static struct playlist_viewer_info  viewer;
static struct playlist_entry        tracks[MAX_PLAYLIST_ENTRIES];

/* Used when viewing playlists on disk */
static struct playlist_info         temp_playlist;

#ifdef HAVE_LCD_BITMAP
extern unsigned char bitmap_icons_6x8[LastIcon][6];
#endif

static bool initialize(char* filename, bool reload);
static void load_playlist_entries(int start_index);
static void load_playlist_entries_r(int end_index);
static int  load_entry(int index, int pos, char* p, int size);
static void format_name(char* dest, char* src);
static void format_line(struct playlist_entry* track, char* str, int len);
static void display_playlist(void);
static void update_display_line(int line, bool scroll);
static void scroll_display(int lines);
static void update_first_index(void);
static bool update_playlist(bool force);
#ifdef BUTTON_ON
static int  onplay_menu(int index);
#endif
static bool viewer_menu(void);
static bool show_icons(void);
static bool show_indices(void);
static bool track_display(void);
static bool save_playlist(void);

/* Initialize the playlist viewer. */
static bool initialize(char* filename, bool reload)
{
    char* buffer;
    int buffer_size;
    bool is_playing = mpeg_status() & MPEG_STATUS_PLAY;

    if (!filename && !is_playing)
        /* Nothing is playing, exit */
        return false;

    buffer = plugin_get_buffer(&buffer_size);
    if (!buffer)
        return false;

    if (!filename)
        viewer.playlist = NULL;
    else
    {
        /* Viewing playlist on disk */
        char *dir, *file, *temp_ptr;
        char *index_buffer = NULL;
        int  index_buffer_size = 0;
        
        viewer.playlist = &temp_playlist;

        /* Separate directory from filename */
        temp_ptr = strrchr(filename+1,'/');
        if (temp_ptr)
        {
            *temp_ptr = 0;
            dir = filename;
            file = temp_ptr + 1;
        }
        else
        {
            dir = "/";
            file = filename+1;
        }

        if (is_playing)
        {
            /* Something is playing, use half the plugin buffer for playlist
               indices */
            index_buffer_size = buffer_size / 2;
            index_buffer = buffer;
        }

        playlist_create_ex(viewer.playlist, dir, file, index_buffer,
            index_buffer_size, buffer+index_buffer_size,
            buffer_size-index_buffer_size);

        if (temp_ptr)
            *temp_ptr = '/';

        buffer += index_buffer_size;
        buffer_size -= index_buffer_size;
    }

    viewer.name_buffer = buffer;
    viewer.buffer_size = buffer_size;

#ifdef HAVE_LCD_BITMAP
    {
        char icon_chars[] = "MQ"; /* characters used as icons */
        unsigned int i;

        viewer.char_width = 0;
        viewer.line_height = 0;

        /* Use icon characters to calculate largest possible width/height so
           that we set proper margins */ 
        for (i=0; i<sizeof(icon_chars); i++)
        {
            char str[2];
            int w, h;

            snprintf(str, sizeof(str), "%c", icon_chars[i]);
            lcd_getstringsize(str, &w, &h);

            if (w > viewer.char_width)
                viewer.char_width = w;

            if (h > viewer.line_height)
            {
                viewer.line_height = h;
                viewer.num_display_lines = (LCD_HEIGHT - MARGIN_Y)/h;
            }
        }
    }
#else
    viewer.num_display_lines = 2;
    viewer.char_width = 1;
    viewer.line_height = 1;
#endif

    viewer.move_track = -1;

    if (!reload)
    {
        viewer.cursor_pos = 0;

        if (!viewer.playlist)
            /* Start displaying at current playing track */
            viewer.first_display_index = playlist_get_display_index() - 1;
        else
            viewer.first_display_index = 0;

        update_first_index();
    }

    if (!update_playlist(true))
        return false;

    return true;
}

/* Load tracks starting at start_index */
static void load_playlist_entries(int start_index)
{
    int num_entries = viewer.num_tracks - start_index;
    char* p = viewer.name_buffer;
    int remaining = viewer.buffer_size;
    int i;

    viewer.first_index = start_index;

    if (num_entries > MAX_PLAYLIST_ENTRIES)
        num_entries = MAX_PLAYLIST_ENTRIES;

    for(i=0; i<num_entries; i++, start_index++)
    {
        int len = load_entry(start_index, i, p, remaining);
        if (len < 0)
        {
            /* Out of name buffer space */
            num_entries = i;
            break;
        }

        p += len;
        remaining -= len;
    }

    viewer.num_loaded = num_entries;
    viewer.last_index = viewer.first_index + (viewer.num_loaded - 1);
}

/* Load tracks in reverse, ending at end_index */
static void load_playlist_entries_r(int end_index)
{
    int num_entries = end_index;
    char* p = viewer.name_buffer;
    int remaining = viewer.buffer_size;
    int i;

    viewer.last_index = end_index;

    if (num_entries >= MAX_PLAYLIST_ENTRIES)
        num_entries = MAX_PLAYLIST_ENTRIES-1;

    for(i=num_entries; i>=0; i--, end_index--)
    {
        int len = load_entry(end_index, i, p, remaining);
        if (len < 0)
        {
            int j;

            /* Out of name buffer space */
            num_entries -= i;

            /* Shift loaded tracks up such that first track is index 0 */
            for (j=0; j<num_entries; j++, i++)
            {
                tracks[j].name = tracks[i].name;
                tracks[j].index = tracks[i].index;
                tracks[j].display_index = tracks[i].display_index;
                tracks[j].queued = tracks[i].queued;
            }

            break;
        }

        p += len;
        remaining -= len;
    }

    viewer.first_index = viewer.last_index - num_entries;

    num_entries++;
    if (!viewer.first_index &&
         num_entries < viewer.num_tracks &&
         num_entries < MAX_PLAYLIST_ENTRIES)
    {
        /* Lets see if we can load more data at the end of the list */
        int max = viewer.num_tracks;
        if (max > MAX_PLAYLIST_ENTRIES)
            max = MAX_PLAYLIST_ENTRIES;

        for (i = num_entries; i<max; i++)
        {
            int len = load_entry(num_entries, num_entries, p, remaining);
            if (len < 0)
                /* Out of name buffer space */
                break;
            
            p += len;
            remaining -= len;

            num_entries++;
            viewer.last_index++;
        }
    }

    viewer.num_loaded = num_entries;
}

/* Load track at playlist index.  pos is the position in the tracks array and
   p is a pointer to the name buffer (max size),  Returns -1 if buffer is
   full. */
static int load_entry(int index, int pos, char* p, int size)
{
    struct playlist_track_info info;
    int len;
    int result = 0;

    /* Playlist viewer orders songs based on display index.  We need to
       convert to real playlist index to access track */
    index = (index + playlist_get_first_index(viewer.playlist)) %
        viewer.num_tracks;
    if (playlist_get_track_info(viewer.playlist, index, &info) < 0)
        return -1;
    
    len = strlen(info.filename) + 1;
    
    if (len <= size)
    {
        strcpy(p, info.filename);
        
        tracks[pos].name = p;
        tracks[pos].index = info.index;
        tracks[pos].display_index = info.display_index;
        tracks[pos].queued = info.attr & PLAYLIST_ATTR_QUEUED;
        
        result = len;
    }
    else
        result = -1;

    return result;
}

/* Format trackname for display purposes */
static void format_name(char* dest, char* src)
{
    switch (global_settings.playlist_viewer_track_display)
    {
        case 0:
        default:
        {
            /* Only display the mp3 filename */
            char* p = strrchr(src, '/');
            int len;
            
            strcpy(dest, p+1);
            len = strlen(dest);
            
            /* Remove the extension */
            if (!strcasecmp(&dest[len-4], ".mp3") ||
                !strcasecmp(&dest[len-4], ".mp2") ||
                !strcasecmp(&dest[len-4], ".mpa"))
                dest[len-4] = '\0';

            break;
        }
        case 1:
            /* Full path */
            strcpy(dest, src);
            break;
    }
}

/* Format display line */
static void format_line(struct playlist_entry* track, char* str, int len)
{
    char name[MAX_PATH];

    format_name(name, track->name);

    if (global_settings.playlist_viewer_indices)
        /* Display playlist index */
        snprintf(str, len, "%d. %s", track->display_index, name);
    else
        snprintf(str, len, "%s", name);

}

/* Display tracks on screen */
static void display_playlist(void)
{
    int i;
    int num_display_tracks =
        viewer.last_display_index - viewer.first_display_index;

    lcd_clear_display();

#ifdef HAVE_LCD_BITMAP
    lcd_setmargins(MARGIN_X, MARGIN_Y);
    lcd_setfont(FONT_UI);
#endif

    for (i=0; i<=num_display_tracks; i++)
    {
        if (global_settings.playlist_viewer_icons)
        {
            /* Icons */
            if (tracks[INDEX(i)].index == viewer.current_playing_track)
            {
                /* Current playing track */
#ifdef HAVE_LCD_BITMAP
                int offset=0;
                if ( viewer.line_height > 8 )
                    offset = (viewer.line_height - 8) / 2;
                lcd_bitmap(bitmap_icons_6x8[File], 
                    CURSOR_X * 6 + CURSOR_WIDTH, 
                    MARGIN_Y+(i*viewer.line_height) + offset,
                    6, 8, true);
#else
                lcd_putc(LINE_X-1, i, File);
#endif
            }
            else if (tracks[INDEX(i)].index == viewer.move_track)
            {
                /* Track we are moving */
#ifdef HAVE_LCD_BITMAP
                lcd_putsxy(CURSOR_X * 6 + CURSOR_WIDTH,
                    MARGIN_Y+(i*viewer.line_height), "M");
#else
                lcd_putc(LINE_X-1, i, 'M');
#endif
            }
            else if (tracks[INDEX(i)].queued)
            {
                /* Queued track */
#ifdef HAVE_LCD_BITMAP
                lcd_putsxy(CURSOR_X * 6 + CURSOR_WIDTH,
                    MARGIN_Y+(i*viewer.line_height), "Q");
#else
                lcd_putc(LINE_X-1, i, 'Q');
#endif
            }
        }

        update_display_line(i, false);
    }

#ifdef HAVE_LCD_BITMAP
    if (global_settings.scrollbar &&
        (viewer.num_tracks > viewer.num_display_lines))
        scrollbar(SCROLLBAR_X, SCROLLBAR_Y, SCROLLBAR_WIDTH - 1,
                  LCD_HEIGHT - SCROLLBAR_Y, viewer.num_tracks-1,
                  viewer.first_display_index, viewer.last_display_index,
                  VERTICAL);
#endif

    put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, true);
    status_draw(true);
}

/* Scroll cursor or display by num lines */
static void scroll_display(int lines)
{
    int new_index = viewer.first_display_index + viewer.cursor_pos + lines;
    bool pagescroll = false;
    bool wrap = false;

    put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, false);

    if (lines > 1 || lines < -1)
        pagescroll = true;

    if (new_index < 0)
    {
        /* Wrap around if not pageup */
        if (pagescroll)
            new_index = 0;
        else
        {
            new_index += viewer.num_tracks;
            viewer.cursor_pos = viewer.num_display_lines-1;
            wrap = true;
        }
    }
    else if (new_index >= viewer.num_tracks)
    {
        /* Wrap around if not pagedown */
        if (pagescroll)
            new_index = viewer.num_tracks - 1;
        else
        {
            new_index -= viewer.num_tracks;
            viewer.cursor_pos = 0;
            wrap = true;
        }
    }

    if (new_index >= viewer.first_display_index &&
        new_index <= viewer.last_display_index)
    {
        /* Just update the cursor */
        viewer.cursor_pos = new_index - viewer.first_display_index;
    }
    else
    {
        /* New track is outside of display */
        if (wrap)
            viewer.first_display_index = new_index;
        else
            viewer.first_display_index = viewer.first_display_index + lines;

        if (viewer.first_display_index < 0)
            viewer.first_display_index = 0;

        viewer.last_display_index =
            viewer.first_display_index + (viewer.num_display_lines - 1);
        if (viewer.last_display_index >= viewer.num_tracks)
        {
            /* display as many tracks as possible on screen */
            if (viewer.first_display_index > 0)
            {
                viewer.first_display_index -=
                    (viewer.last_display_index - viewer.num_tracks + 1);
                if (viewer.first_display_index < 0)
                    viewer.first_display_index = 0;
            }

            viewer.last_display_index = viewer.num_tracks - 1;
        }

        if (viewer.cursor_pos >
                (viewer.last_display_index - viewer.first_display_index))
            viewer.cursor_pos =
                viewer.last_display_index - viewer.first_display_index;

        /* Load more data if needed */
        if (viewer.first_display_index < viewer.first_index)
            load_playlist_entries_r(viewer.last_display_index);
        else if (viewer.last_display_index > viewer.last_index)
            load_playlist_entries(viewer.first_display_index);

        display_playlist();
    }

    put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, true);
}

/* Update lcd line.  Scroll line if requested */
static void update_display_line(int line, bool scroll)
{
    char str[MAX_PATH + 16];
    
    format_line(&tracks[INDEX(line)], str, sizeof(str));

    if (scroll)
    {
#ifdef HAVE_LCD_BITMAP
        if (global_settings.invert_cursor)
            lcd_puts_scroll_style(LINE_X, line, str, STYLE_INVERT);
        else
#endif
            lcd_puts_scroll(LINE_X, line, str);
    }
    else
        lcd_puts(LINE_X, line, str);
}

/* Update first index, if necessary, to put as much as possible on the
   screen */
static void update_first_index(void)
{
    /* viewer.num_tracks may be invalid at this point */
    int num_tracks = playlist_amount_ex(viewer.playlist);

    if ((num_tracks - viewer.first_display_index) < viewer.num_display_lines)
    {
        /* Try to display as much as possible */
        int old_index = viewer.first_display_index;

        viewer.first_display_index = num_tracks - viewer.num_display_lines;
        if (viewer.first_display_index < 0)
            viewer.first_display_index = 0;

        /* Cursor should still point at current track */
        viewer.cursor_pos += old_index - viewer.first_display_index;
    }
}

/* Update playlist in case something has changed or forced */
static bool update_playlist(bool force)
{
    if (!viewer.playlist)
        playlist_get_resume_info(&viewer.current_playing_track);
    else
        viewer.current_playing_track = -1;
        
    if (force || playlist_amount_ex(viewer.playlist) != viewer.num_tracks)
    {
        int index;

        /* Reload tracks */
        viewer.num_tracks = playlist_amount_ex(viewer.playlist);
        if (viewer.num_tracks < 0)
            return false;
        
        index = viewer.first_display_index;

        load_playlist_entries(index);

        if (viewer.num_loaded <= 0)
            return false;
        
        viewer.first_display_index = viewer.first_index;
        viewer.last_display_index =
            viewer.first_index + viewer.num_display_lines - 1;
        if (viewer.last_display_index >= viewer.num_tracks)
            viewer.last_display_index = viewer.num_tracks - 1;
    }

    display_playlist();

    return true;
}

#ifdef BUTTON_ON
/* Menu of playlist commands.  Invoked via ON+PLAY on main viewer screen.
   Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
   changed. */
static int onplay_menu(int index)
{
    struct menu_items menu[3]; /* increase this if you add entries! */
    int m, i=0, result, ret = 0;
    bool current = (tracks[index].index == viewer.current_playing_track);

    menu[i].desc = str(LANG_REMOVE);
    menu[i].voice_id = LANG_REMOVE;
    i++;

    menu[i].desc = str(LANG_MOVE);
    menu[i].voice_id = LANG_MOVE;
    i++;

    menu[i].desc = str(LANG_FILE_OPTIONS);
    menu[i].voice_id = LANG_FILE_OPTIONS;
    i++;

    m = menu_init(menu, i, NULL);
    result = menu_show(m);
    if (result == MENU_ATTACHED_USB)
        ret = -1;
    else if (result >= 0)
    {
        /* Abort current move */
        viewer.move_track = -1;

        switch (result)
        {
            case 0:
                /* delete track */
                if (current)
                    mpeg_stop();

                playlist_delete(viewer.playlist, tracks[index].index);

                if (current)
                {
                    /* Start playing new track except if it's the last track
                       in the playlist and repeat mode is disabled */
                    if (tracks[index].display_index != viewer.num_tracks ||
                        global_settings.repeat_mode == REPEAT_ALL)
                    {
                        mpeg_play(0);
                        viewer.current_playing_track = -1;
                    }
                }

                ret = 1;
                break;
            case 1:
                /* move track */
                viewer.move_track = tracks[index].index;
                ret = 0;
                break;
            case 2:
            {
                onplay(tracks[index].name, TREE_ATTR_MPA);

                if (!viewer.playlist)
                    ret = 1;
                else
                    ret = 0;

                break;
            }
        }
    }

    menu_exit(m);

    return ret;
}
#endif

/* Menu of viewer options.  Invoked via F1(r) or Menu(p). */
static bool viewer_menu(void)
{
    int m;
    bool result;

    struct menu_items items[] = {
        { STR(LANG_SHOW_ICONS),             show_icons },
        { STR(LANG_SHOW_INDICES),           show_indices },
        { STR(LANG_TRACK_DISPLAY),          track_display },
        { STR(LANG_SAVE_DYNAMIC_PLAYLIST),  save_playlist },
    };
    
    m=menu_init( items, sizeof(items) / sizeof(*items), NULL );
    result = menu_run(m);
    menu_exit(m);

    settings_save();

    return result;
}

/* Show icons in viewer? */
static bool show_icons(void)
{
    return set_bool(str(LANG_SHOW_ICONS),
        &global_settings.playlist_viewer_icons);
}

/* Show indices of tracks? */
static bool show_indices(void)
{
    return set_bool(str(LANG_SHOW_INDICES),
        &global_settings.playlist_viewer_indices);
}

/* How to display a track */
static bool track_display(void)
{
    char* names[] = {
        str(LANG_DISPLAY_TRACK_NAME_ONLY),
        str(LANG_DISPLAY_FULL_PATH)
    };
    
    return set_option(str(LANG_TRACK_DISPLAY), 
        &global_settings.playlist_viewer_track_display, INT, names, 2, NULL);
}

/* Save playlist to disk */
static bool save_playlist(void)
{
    char filename[MAX_PATH+1];

    strncpy(filename, DEFAULT_PLAYLIST_NAME, sizeof(filename));

    if (!kbd_input(filename, sizeof(filename)))
    {
        playlist_save(viewer.playlist, filename);
        
        /* reload in case playlist was saved to cwd */
        reload_directory();
    }

    return false;
}

/* View current playlist */
bool playlist_viewer(void)
{
    return playlist_viewer_ex(NULL);
}

/* Main viewer function.  Filename identifies playlist to be viewed.  If NULL,
   view current playlist. */
bool playlist_viewer_ex(char* filename)
{
    bool ret = false;       /* return value */
    bool exit=false;        /* exit viewer */
    bool update=true;       /* update display */
    bool cursor_on=true;    /* used for flashing cursor */
    int old_cursor_pos;     /* last cursor position */
    int button; 

    if (!initialize(filename, false))
        goto exit;

    old_cursor_pos = viewer.cursor_pos;

    while (!exit)
    {
        int track;

        /* Timeout so we can determine if play status has changed */
        button = button_get_w_tmo(HZ/2);

        if (!viewer.playlist && !(mpeg_status() & MPEG_STATUS_PLAY))
        {
            /* Play has stopped */
#ifdef HAVE_LCD_CHARCELLS
            splash(HZ, true, str(LANG_END_PLAYLIST_PLAYER));
#else
            splash(HZ, true, str(LANG_END_PLAYLIST_RECORDER));
#endif
            status_set_playmode(STATUS_STOP);
            goto exit;
        }

        if (viewer.move_track != -1 || !cursor_on)
        {
            /* Flash cursor to identify that we are moving a track */
            cursor_on = !cursor_on;
#ifdef HAVE_LCD_BITMAP
            if (global_settings.invert_cursor)
            {
                lcd_invertrect(
                    MARGIN_X, MARGIN_Y+(viewer.cursor_pos*viewer.line_height),
                    LCD_WIDTH, viewer.line_height);
                lcd_invertscroll(LINE_X, viewer.cursor_pos);
            }
            else
                put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos,
                    cursor_on);

            lcd_update_rect(
                0, MARGIN_Y + (viewer.cursor_pos * viewer.line_height),
                LCD_WIDTH, viewer.line_height);
#else
            put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, cursor_on);
            lcd_update();
#endif
        }

        if (!viewer.playlist)
            playlist_get_resume_info(&track);
        else
            track = -1;

        if (track != viewer.current_playing_track ||
            playlist_amount_ex(viewer.playlist) != viewer.num_tracks)
        {
            /* Playlist has changed (new track started?) */
            update_first_index();
            if (!update_playlist(false))
                exit = true;
            else
                update = true;

            /* Abort move on playlist change */
            viewer.move_track = -1;
        }

        switch (button)
        {
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_OFF:
            case BUTTON_LEFT:
#else
            case BUTTON_STOP:
#endif
                exit = true;
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_UP:
            case BUTTON_UP | BUTTON_REPEAT:
#else
            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
#endif
                scroll_display(-1);
                update = true;
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_DOWN:
            case BUTTON_DOWN | BUTTON_REPEAT:
#else
            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
#endif
                scroll_display(1);
                update = true;
                break;

#ifdef BUTTON_ON
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_ON | BUTTON_UP:
            case BUTTON_ON | BUTTON_UP | BUTTON_REPEAT:
#else
            case BUTTON_ON | BUTTON_LEFT:
            case BUTTON_ON | BUTTON_LEFT | BUTTON_REPEAT:
#endif
                /* Pageup */
                scroll_display(-viewer.num_display_lines);
                update = true;
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_ON | BUTTON_DOWN:
            case BUTTON_ON | BUTTON_DOWN | BUTTON_REPEAT:
#else
            case BUTTON_ON | BUTTON_RIGHT:
            case BUTTON_ON | BUTTON_RIGHT | BUTTON_REPEAT:
#endif
                /* Pagedown */
                scroll_display(viewer.num_display_lines);
                update = true;
                break;
#endif /* BUTTON_ON */

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_RIGHT:
#endif
            case BUTTON_PLAY:
                if (viewer.move_track >= 0)
                {
                    /* Move track */
                    int ret;

                    ret = playlist_move(viewer.playlist, viewer.move_track,
                        tracks[INDEX(viewer.cursor_pos)].index);
                    if (ret < 0)
                        splash(HZ, true, str(LANG_MOVE_FAILED));

                    update_playlist(true);
                    viewer.move_track = -1;
                }
                else if (!viewer.playlist)
                {
                    /* Stop current track and play new track */
                    mpeg_stop();
                    playlist_start(tracks[INDEX(viewer.cursor_pos)].index, 0);
                    status_set_playmode(STATUS_PLAY);
                    update_playlist(false);
                }
                else
                {
                    /* Play track from playlist on disk */
                    mpeg_stop();

                    /* New playlist */
                    if (playlist_set_current(viewer.playlist) < 0)
                        goto exit;

                    playlist_start(tracks[INDEX(viewer.cursor_pos)].index, 0);
                    status_set_playmode(STATUS_PLAY);

                    /* Our playlist is now the current list */
                    if (!initialize(NULL, true))
                        goto exit;
                }

                display_playlist();
                update = true;
                break;

#ifdef BUTTON_ON
            case BUTTON_ON | BUTTON_PLAY:
            {
                /* ON+PLAY menu */
                int ret;

                ret = onplay_menu(INDEX(viewer.cursor_pos));

                if (ret < 0)
                {
                    ret = true;
                    goto exit;
                }
                else if (ret > 0)
                {
                    /* Playlist changed */
                    update_first_index();
                    update_playlist(false);
                    if (viewer.num_tracks <= 0)
                        exit = true;
                }
                else
                    display_playlist();

                update = true;
                break;
            }
#endif /* BUTTON_ON */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1:
#else
            case BUTTON_MENU:
#endif
                if (viewer_menu())
                {
                    ret = true;
                    goto exit;
                }
               
                display_playlist();
                update = true;
                break;

            case SYS_USB_CONNECTED:
                usb_screen();
                ret = true;
                goto exit;
                break;

            case BUTTON_NONE:
                status_draw(false);
                break;
        }

        if (update && !exit)
        {
            lcd_stop_scroll();

            if (viewer.cursor_pos >
                (viewer.last_display_index - viewer.first_display_index))
            {
                /* Cursor position is invalid */
                put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, false);
                viewer.cursor_pos =
                    viewer.last_display_index - viewer.first_display_index;
                put_cursorxy(CURSOR_X, CURSOR_Y + viewer.cursor_pos, true);
            }

            if (viewer.cursor_pos != old_cursor_pos &&
                old_cursor_pos <=
                    (viewer.last_display_index - viewer.first_display_index))
                /* Stop scrolling previous line */
                update_display_line(old_cursor_pos, false);

            /* Scroll line at new cursor position */
            update_display_line(viewer.cursor_pos, true);

            lcd_update();

            old_cursor_pos = viewer.cursor_pos;
            cursor_on = true;
            update = false;
        }
    }

exit:
    if (viewer.playlist)
        playlist_close(viewer.playlist);
    return ret;
}