summaryrefslogtreecommitdiffstats
path: root/apps/wps.c
blob: a7ccebce019fa9553e0b0631535e6d20a092aa12 (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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Jerome Kuptz
 *
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "file.h"
#include "lcd.h"
#include "button.h"
#include "kernel.h"
#include "tree.h"
#include "debug.h"
#include "sprintf.h"
#include "settings.h"
#include "wps.h"
#include "mpeg.h"
#include "usb.h"
#include "powermgmt.h"
#include "status.h"
#include "main_menu.h"
#include "ata.h"
#ifdef HAVE_LCD_BITMAP
#include "icons.h"
#include "widgets.h"
#endif

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

#define FF_REWIND_MIN_STEP 1000 /* minimum ff/rewind step is 1 second */
#define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */ 
                                /* 3% of 30min file == 54s step size */

#define WPS_CONFIG "/wps.config"

#ifdef HAVE_LCD_BITMAP
    #define PLAY_DISPLAY_2LINEID3        0 
    #define PLAY_DISPLAY_FILENAME_SCROLL 1 
    #define PLAY_DISPLAY_TRACK_TITLE     2 
    #define PLAY_DISPLAY_CUSTOM_WPS      3 
#else
    #define PLAY_DISPLAY_1LINEID3        0 
    #define PLAY_DISPLAY_2LINEID3        1 
    #define PLAY_DISPLAY_FILENAME_SCROLL 2 
    #define PLAY_DISPLAY_TRACK_TITLE     3 
    #define PLAY_DISPLAY_CUSTOM_WPS      4 
#endif

#ifdef HAVE_RECORDER_KEYPAD
#define RELEASE_MASK (BUTTON_F1 | BUTTON_DOWN | BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP)
#else
#define RELEASE_MASK (BUTTON_MENU | BUTTON_STOP | BUTTON_LEFT | BUTTON_RIGHT | BUTTON_PLAY)
#endif

bool keys_locked = false;
static bool ff_rewind = false;
static bool paused = false;
static int ff_rewind_count = 0;
static struct mp3entry* id3 = NULL;
static int old_release_mask;

static char custom_wps[5][64];
static char wps_display[5][64];
static int scroll_line;
static int scroll_line_custom;

static void draw_screen(void)
{
    int font_height;

#ifdef LOADABLE_FONTS
    unsigned char *font = lcd_getcurrentldfont();
    font_height = ajf_get_fontheight(font);
#else
    font_height = 8;
#endif

    lcd_clear_display();
    if(!id3 && !mpeg_is_playing())
    {
#ifdef HAVE_LCD_CHARCELLS
        lcd_puts(0, 0, "End of list");
        lcd_puts(0, 1, "<Press ON>");
#else
        lcd_puts(0, 2, "<End of song list>");
        lcd_puts(5, 4, "Press ON");
#endif
    }
    else
    {
        static int last_wps = -1;
        if ((last_wps != global_settings.wps_display
            && global_settings.wps_display == PLAY_DISPLAY_CUSTOM_WPS))
        {
            load_custom_wps();
            last_wps = global_settings.wps_display;
        }

        switch ( global_settings.wps_display ) {
            case PLAY_DISPLAY_TRACK_TITLE:
            {
                char ch = '/';
                char* end;
                char* szTok;
                char* szDelimit;
                char* szPeriod;
                char szArtist[26];
                char szBuff[257];
                int tmpcnt = 0;

                szBuff[sizeof(szBuff)-1] = 0;
                strncpy(szBuff, id3->path, sizeof(szBuff));

                szTok = strtok_r(szBuff, "/", &end);
                szTok = strtok_r(NULL, "/", &end);

                /* Assume path format of: Genre/Artist/Album/Mp3_file */
                strncpy(szArtist,szTok,sizeof(szArtist));
                szArtist[sizeof(szArtist)-1] = 0;
                szDelimit = strrchr(id3->path, ch);
                lcd_puts(0, 0, szArtist?szArtist:"<nothing>");

                /* removes the .mp3 from the end of the display buffer */
                szPeriod = strrchr(szDelimit, '.');
                if (szPeriod != NULL)
                    *szPeriod = 0;

                snprintf(wps_display[0],sizeof(wps_display[0]),"%s",
                        (++szDelimit));
#ifdef HAVE_LCD_CHARCELLS
                snprintf(wps_display[1],sizeof(wps_display[1]),"%s",
                        "%pc/%pt");
#endif
                for(tmpcnt=2;tmpcnt<=5;tmpcnt++)
                    wps_display[tmpcnt][0] = 0;
                scroll_line = 0;
                refresh_wps(false);
                break;
            }
            case PLAY_DISPLAY_FILENAME_SCROLL:
            {
                snprintf(wps_display[0],sizeof(wps_display[0]),"%s",
                        "%pp/%pe: %fn");
#ifdef HAVE_LCD_CHARCELLS
                snprintf(wps_display[1],sizeof(wps_display[1]),"%s",
                        "%pc/%pt");
#endif
                scroll_line = 0;
                refresh_wps(false);
                break;
            }
            case PLAY_DISPLAY_2LINEID3:
            {
#ifdef HAVE_LCD_BITMAP
                int l = 0;

                snprintf(wps_display[l],sizeof(wps_display[l]),"%s","%fn");
                snprintf(wps_display[l++],sizeof(wps_display[l]),"%s","%it");
                snprintf(wps_display[l++],sizeof(wps_display[l]),"%s","%id");
                snprintf(wps_display[l++],sizeof(wps_display[l]),"%s","%ia");
                if(!global_settings.statusbar && font_height <= 8)
                {
                    if(id3->vbr)
                        snprintf(wps_display[l++],sizeof(wps_display[l]),"%s",
                                "%fb kbit (avg)");
                    else
                        snprintf(wps_display[l],sizeof(wps_display[l]),"%s",
                                "%fb kbit");
                    snprintf(wps_display[l],sizeof(wps_display[l]),"%s",
                            "%ff Hz");
                }
                else
                {
                    if(id3->vbr)
                        snprintf(wps_display[l++],sizeof(wps_display[l]),"%s",
                                "%fb kbit(a) %ffHz");
                    else
                        snprintf(wps_display[l],sizeof(wps_display[l]),"%s",
                                "%fb kbit    %ffHz");
                }
                scroll_line = 0;
                refresh_wps(false);
#else
                snprintf(wps_display[0],sizeof(wps_display[0]),"%s","%ia");
                snprintf(wps_display[1],sizeof(wps_display[1]),"%s","%it");
                scroll_line = 1;
                refresh_wps(false);
#endif
                break;
            }
#ifdef HAVE_LCD_CHARCELLS
            case PLAY_DISPLAY_1LINEID3:
            {
                snprintf(wps_display[0],sizeof(wps_display[0]),"%s",
                        "%pp/%pe: %fc");
                snprintf(wps_display[1],sizeof(wps_display[1]),"%s",
                        "%pc/%pt");
                scroll_line = 0;
                refresh_wps(false);
                break;
            }
#endif
            case PLAY_DISPLAY_CUSTOM_WPS:
            {
                if(custom_wps[0] == 0)
                {
                    snprintf(wps_display[0],sizeof(wps_display[0]),"%s",
                        "Couldn't Load Custom WPS");
                    snprintf(wps_display[1],sizeof(wps_display[1]),"%s",
                            "%pc/%pt");
                }
                refresh_wps(false);
                break;
            }
        }
    }
    status_draw();
    lcd_update();
}

#ifdef PLAYER_PROGRESS
int bin2int(char *input, int size)
{
    int result=0;
    while(size--) {
        result <<= 1;
        result += (*input++ - '0');
    }
    return result;
}
#endif

bool refresh_wps(bool refresh_scroll)
{
    int l;
#ifdef HAVE_LCD_BITMAP
    int bmp_time_line;
#endif

    if(!id3)
    {
        lcd_stop_scroll();
        lcd_clear_display();
        return(false);
    }

#ifdef HAVE_LCD_CHARCELL
    for(l = 0; l <= 1; l++)
#else
    for(l = 0; l <= 5; l++)
#endif
    {
        if(global_settings.wps_display == PLAY_DISPLAY_CUSTOM_WPS)
        {
            scroll_line = scroll_line_custom;
            if(scroll_line != l)
                    display_custom_wps(0, l, false, custom_wps[l]);
            else
                if(refresh_scroll)
                        display_custom_wps(0, l, true, custom_wps[l]);
        }
        else
        {
            if(scroll_line != l)
                    display_custom_wps(0, l, false, wps_display[l]);
            if(scroll_line == l && refresh_scroll)
                    display_custom_wps(0, l, true, wps_display[l]);                
        }
    }
#ifdef HAVE_LCD_BITMAP
    if(global_settings.statusbar)
        bmp_time_line = 5;
    else
        bmp_time_line = 6;
    snprintf(wps_display[bmp_time_line],sizeof(wps_display[bmp_time_line]),
             "%s","Time: %pc/%pt");

    slidebar(0, LCD_HEIGHT-6, LCD_WIDTH, 6, id3->elapsed*100/id3->length, Grow_Right);
    lcd_update();
#endif
#ifdef PLAYER_PROGRESS
#ifdef HAVE_LCD_CHARCELLS
    draw_player_progress(10,1);
#endif
#endif
    return(true);
}

#ifdef PLAYER_PROGRESS
#ifdef HAVE_LCD_CHARCELLS
void draw_player_progress(int x, int y)
{
    char player_progressbar[8];
    char binline[35];
    char charline[5];
    int songpos = 0;
    int tmpcnt;

    memset(binline, 0, sizeof(binline));
    memset(charline, 0, sizeof(charline));
    songpos = (id3->elapsed * 35) / id3->length;
    for(tmpcnt=0;tmpcnt<=songpos;tmpcnt++)
        binline[tmpcnt] = 1;
    for(tmpcnt=0;tmpcnt<=6;tmpcnt++)
        player_progressbar[tmpcnt] = bin2int(binline+(tmpcnt*5),4);
    lcd_define_pattern(8,player_progressbar,7);
    lcd_puts(x,y,"\01");
}
#endif
#endif

bool load_custom_wps(void)
{
    int fd;
    int l = 0;
    int numread = 1;
    char cchr[0];

    for(l=0;l<=5;l++)
    {
         custom_wps[l][0] = 0;
    }
    l = 0;

    fd = open(WPS_CONFIG, O_RDONLY);
    if(-1 == fd)
    {
        close(fd);
        return(false);
    }

    while(l<=5)
    {
        numread = read(fd, cchr, 1);
        if(numread==0)
            break;
        switch(cchr[0])
        {
            case 10: /* LF */
                l++;
                break;
            case 13: /* CR ... Ignore it */
                break;                
            default:
                snprintf(custom_wps[l], sizeof(custom_wps[l]), "%s%c", custom_wps[l], cchr[0]);
                break;
        }
    }
    close(fd);

    scroll_line_custom = 0;
    for(l=0;l<=5;l++)
    {
        if(custom_wps[l][0] == '%' && custom_wps[l][1] == 's')
            scroll_line_custom = l;
    }
    return(true);
}

bool display_custom_wps(int x_val, int y_val, bool do_scroll, char *wps_string)
{
    char buffer[128];
    char tmpbuf[64];
    int con_flag = 0;  /* (0)Not inside of if/else
                          (1)Inside of If
                          (2)Inside of Else */
    char con_if[64];
    char con_else[64];
    char cchr1;
    char cchr2;
    char cchr3;
    unsigned int seek;

    char* szLast;

    szLast = strrchr(id3->path, '/');
    if(szLast)
        /* point to the first letter in the file name */
        szLast++;

    buffer[0] = 0;

    seek = -1;
    while(1)
    {
        seek++;
        cchr1 = wps_string[seek];
        tmpbuf[0] = 0;
        switch(cchr1)
        {
            case '%':
                seek++;
                cchr2 = wps_string[seek];
                switch(cchr2)
                {
                    case 'i':  /* ID3 Information */
                        seek++;
                        cchr3 = wps_string[seek];
                        switch(cchr3)
                        {
                            case 't':  /* ID3 Title */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%s", 
                                         id3->title?id3->title:"<no title>");
                                break;
                            case 'a':  /* ID3 Artist */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%s", 
                                         id3->artist?id3->artist:"<no artist>");
                                break;
                            case 'n':  /* ID3 Track Number */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d", id3->tracknum);
                                break;
                            case 'd':  /* ID3 Album/Disc */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%s", id3->album);
                                break;
                        }
                        break;
                    case 'f':  /* File Information */
                        seek++;
                        cchr3 = wps_string[seek];
                        switch(cchr3)
                        {
                            case 'c':  /* Conditional Filename \ ID3 Artist-Title */
                                if(id3->artist && id3->title)
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s - %s",
                                             id3->artist?id3->artist:"<no artist>",
                                             id3->title?id3->title:"<no title>");
                                else
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s",
                                             szLast?szLast:id3->path);
                                break;
                            case 'd':  /* Conditional Filename \ ID3 Title-Artist */
                                if(id3->artist && id3->title)
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s - %s",
                                             id3->title?id3->title:"<no title>",
                                             id3->artist?id3->artist:"<no artist>");
                                else
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s",
                                             szLast?szLast:id3->path);
                                break;
                            case 'b':  /* File Bitrate */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d", id3->bitrate);
                                break;
                            case 'f':  /* File Frequency */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d", id3->frequency);
                                break;
                            case 'p':  /* File Path */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%s", id3->path);
                                break;
                            case 'n':  /* File Name */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%s",
                                         szLast?szLast:id3->path);
                                break;
                            case 's':  /* File Size (In Kilobytes) */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d",
                                         id3->filesize / 1024);
                                break;
                        }
                        break;
                    case 'p':  /* Playlist/Song Information */
                        seek++;
                        cchr3 = wps_string[seek];
                        switch(cchr3)
                        {
                            case 'p':  /* Playlist Position */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d", id3->index + 1);
                                break;
                            case 'e':  /* Playlist Total Entries */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d", playlist.amount);
                                break;
                            case 'c':  /* Current Time in Song */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d:%02d",
                                         (id3->elapsed + ff_rewind_count) / 60000,
                                         (id3->elapsed + ff_rewind_count) % 60000 / 1000);
                                break;
                            case 'r': /* Remaining Time in Song */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d:%02d",
                                         (id3->length - (id3->elapsed + ff_rewind_count)) / 60000,
                                         (id3->length - (id3->elapsed + ff_rewind_count)) % 60000 / 1000);
                                break;
                            case 't':  /* Total Time */
                                snprintf(tmpbuf, sizeof(tmpbuf), "%d:%02d",
                                         id3->length / 60000,
                                         id3->length % 60000 / 1000);
                                break;
                        }
                        break;
                    case '%':  /* Displays % */
                        snprintf(tmpbuf, sizeof(tmpbuf), "%%");
                        break;
                    case '?':  /* Conditional Display of ID3/File */
                        switch(con_flag)
                        {
                            case 0:
                                con_if[0] = 0;
                                con_else[0] = 0;
                                con_flag = 1;
                                break;
                            default:
                                if(id3->artist && id3->title)
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s", con_if);
                                else
                                    snprintf(tmpbuf, sizeof(tmpbuf), "%s", con_else);
                                con_flag = 0;
                                break;
                        }
                        break;
                    case ':': /* Seperator for Conditional ID3/File Display */
                        con_flag = 2;
                        break;
                }
                switch(con_flag)
                {
                    case 0:
                        snprintf(buffer, sizeof(buffer), "%s%s", buffer, tmpbuf);
                        break;
                    case 1:
                        snprintf(con_if, sizeof(con_if), "%s%s", con_if, tmpbuf);
                        break;
                    case 2:
                        snprintf(con_else, sizeof(con_else), "%s%s", con_else, tmpbuf);
                        break;
                }
                break;
            default:
                switch(con_flag)
                {
                    case 0:
                        snprintf(buffer, sizeof(buffer), "%s%c", buffer, cchr1);
                        break;
                    case 1:
                        snprintf(con_if, sizeof(con_if), "%s%c", con_if, cchr1);
                        break;
                    case 2:
                        snprintf(con_else, sizeof(con_else), "%s%c", con_else, cchr1);
                        break;
                }
                break;
        }
        if(seek >= strlen(wps_string))
        {
            if(do_scroll)
            {
                lcd_stop_scroll();
                lcd_puts_scroll(x_val, y_val, buffer);
            }
            else
                lcd_puts(x_val, y_val, buffer);
            return(true);
        }
    }
    return(true);
}

int player_id3_show(void)
{
#ifdef HAVE_PLAYER_KEYPAD
    int button;
    int menu_pos = 0;
    int menu_max = 6;
    bool menu_changed = true;
    char scroll_text[MAX_PATH];

    lcd_stop_scroll();
    lcd_clear_display();
    lcd_puts(0, 0, "-ID3 Info- ");
    lcd_puts(0, 1, "--Screen-- ");
    sleep(HZ*1.5);
 
    while(1)
    {
        button = button_get(false);

        switch(button)
        {
            case BUTTON_LEFT:
                menu_changed = true;
                if(menu_pos > 0)
                    menu_pos--;
                else
                    menu_pos = menu_max;
                break;

            case BUTTON_RIGHT:
                menu_changed = true;
                if(menu_pos < menu_max)
                    menu_pos++;
                else
                    menu_pos = 0;
                break;
            
            case BUTTON_REPEAT:
                break;

            case BUTTON_STOP:
            case BUTTON_PLAY:
                lcd_stop_scroll();
                draw_screen();
                return(0);
                break;

#ifndef SIMULATOR
            case SYS_USB_CONNECTED: 
                /* Tell the USB thread that we are safe */
                DEBUGF("wps got SYS_USB_CONNECTED\n");
                usb_acknowledge(SYS_USB_CONNECTED_ACK);
                    
                /* Wait until the USB cable is extracted again */
                usb_wait_for_disconnect(&button_queue);

                /* Signal to our caller that we have been in USB mode */
                return SYS_USB_CONNECTED;
                break;
#endif

        }

        switch(menu_pos)
        {
            case 0:
                lcd_puts(0, 0, "[Title]");
                snprintf(scroll_text,sizeof(scroll_text), "%s",
                        id3->title?id3->title:"<no title>");
                break;
            case 1:
                lcd_puts(0, 0, "[Artist]");
                snprintf(scroll_text,sizeof(scroll_text), "%s",
                        id3->artist?id3->artist:"<no artist>");
                break;
            case 2:
                lcd_puts(0, 0, "[Album]");
                snprintf(scroll_text,sizeof(scroll_text), "%s",
                        id3->album?id3->album:"<no album>");
                break;
            case 3:
                lcd_puts(0, 0, "[Length]");
                snprintf(scroll_text,sizeof(scroll_text), "%d:%02d",
                   id3->length / 60000,
                   id3->length % 60000 / 1000 );
                break;
            case 4:
                lcd_puts(0, 0, "[Bitrate]");
                snprintf(scroll_text,sizeof(scroll_text), "%d kbps", 
                        id3->bitrate);
                break;
            case 5:
                lcd_puts(0, 0, "[Frequency]");
                snprintf(scroll_text,sizeof(scroll_text), "%d kHz",
                        id3->frequency);
                break;
            case 6:
                lcd_puts(0, 0, "[Path]");
                snprintf(scroll_text,sizeof(scroll_text), "%s",
                        id3->path);
                break;
        }

        if(menu_changed == true)
        {
            menu_changed = false;
            lcd_stop_scroll();
            lcd_clear_display();
            lcd_puts_scroll(0, 1, scroll_text);
        }

        lcd_update();
        yield();
    }
#endif
    return(0);
}

void display_volume_level(int vol_level)
{
    char buffer[32];

    lcd_stop_scroll();
    snprintf(buffer,sizeof(buffer),"Vol: %d %%       ", vol_level * 2);

#ifdef HAVE_LCD_CHARCELLS
    lcd_puts(0, 0, buffer);
#else
    lcd_puts(2, 3, buffer);
    lcd_update();
#endif

    sleep(HZ/6);
}

void display_keylock_text(bool locked)
{
    lcd_stop_scroll();
    lcd_clear_display();

#ifdef HAVE_LCD_CHARCELLS
    if(locked)
        lcd_puts(0, 0, "Keylock ON");
    else
        lcd_puts(0, 0, "Keylock OFF");
#else
    if(locked)
    {
        lcd_puts(2, 3, "Key lock is ON");
    }
    else
    {
        lcd_puts(2, 3, "Key lock is OFF");
    }
    lcd_update();
#endif
    
    sleep(HZ);
}

void display_mute_text(bool muted)
{
    lcd_stop_scroll();
    lcd_clear_display();

#ifdef HAVE_LCD_CHARCELLS
    if(muted)
        lcd_puts(0, 0, "Mute ON");
    else
        lcd_puts(0, 0, "Mute OFF");
#else
    if(muted)
    {
        lcd_puts(2, 3, "Mute is ON");
    }
    else
    {
        lcd_puts(2, 3, "Mute is OFF");
    }
    lcd_update();
#endif
    
    sleep(HZ);
}

static void handle_usb(void)
{
#ifndef SIMULATOR
#ifdef HAVE_LCD_BITMAP
    bool laststate=statusbar(false);
#endif
    /* Tell the USB thread that we are safe */
    DEBUGF("wps got SYS_USB_CONNECTED\n");
    usb_acknowledge(SYS_USB_CONNECTED_ACK);
                    
    /* Wait until the USB cable is extracted again */
    usb_wait_for_disconnect(&button_queue);

#ifdef HAVE_LCD_BITMAP
    statusbar(laststate);
#endif
#endif
}

static bool ffwd_rew(int button)
{
    unsigned int ff_rewind_step = 0; /* current rewind step size */ 
    unsigned int ff_rewind_max_step = 0; /* max rewind step size */ 
    long ff_rewind_accel_tick = 0; /* next time to bump ff/rewind step size */ 
    bool exit = false;
    bool usb = false;

    while (!exit) {
        switch ( button ) {
            case BUTTON_LEFT | BUTTON_REPEAT:
                if (ff_rewind)
                {
                    ff_rewind_count -= ff_rewind_step; 
                    if (global_settings.ff_rewind_accel != 0 && 
                        current_tick >= ff_rewind_accel_tick) 
                    { 
                        ff_rewind_step *= 2; 
                        if (ff_rewind_step > ff_rewind_max_step) 
                            ff_rewind_step = ff_rewind_max_step; 
                        ff_rewind_accel_tick = current_tick + 
                            global_settings.ff_rewind_accel*HZ; 
                    } 
                }
                else
                {
                    if ( mpeg_is_playing() && id3 && id3->length )
                    {
                        if (!paused)
                            mpeg_pause();
#ifdef HAVE_PLAYER_KEYPAD
                        lcd_stop_scroll();
#endif
                        status_set_playmode(STATUS_FASTBACKWARD);
                        ff_rewind = true;
                        ff_rewind_max_step = 
                            id3->length * FF_REWIND_MAX_PERCENT / 100; 
                        ff_rewind_step = FF_REWIND_MIN_STEP;
                        if (ff_rewind_step > ff_rewind_max_step) 
                            ff_rewind_step = ff_rewind_max_step; 
                        ff_rewind_count = -ff_rewind_step; 
                        ff_rewind_accel_tick = current_tick + 
                            global_settings.ff_rewind_accel*HZ; 
                    }
                    else
                        break;
                }

                if ((int)(id3->elapsed + ff_rewind_count) < 0)
                    ff_rewind_count = -id3->elapsed;

                refresh_wps(false);
                break;

            case BUTTON_RIGHT | BUTTON_REPEAT:
                if (ff_rewind)
                {
                    ff_rewind_count += ff_rewind_step; 
                    if (global_settings.ff_rewind_accel != 0 && 
                        current_tick >= ff_rewind_accel_tick) 
                    { 
                        ff_rewind_step *= 2; 
                        if (ff_rewind_step > ff_rewind_max_step) 
                            ff_rewind_step = ff_rewind_max_step; 
                        ff_rewind_accel_tick = current_tick + 
                            global_settings.ff_rewind_accel*HZ; 
                    } 
                }
                else
                {
                    if ( mpeg_is_playing() && id3 && id3->length )
                    {
                        if (!paused)
                            mpeg_pause();
#ifdef HAVE_PLAYER_KEYPAD
                        lcd_stop_scroll();
#endif
                        status_set_playmode(STATUS_FASTFORWARD);
                        ff_rewind = true;
                        ff_rewind_max_step = 
                            id3->length * FF_REWIND_MAX_PERCENT / 100; 
                        ff_rewind_step = FF_REWIND_MIN_STEP; 
                        if (ff_rewind_step > ff_rewind_max_step) 
                            ff_rewind_step = ff_rewind_max_step; 
                        ff_rewind_count = ff_rewind_step; 
                        ff_rewind_accel_tick = current_tick + 
                            global_settings.ff_rewind_accel*HZ; 
                    }
                    else
                        break;
                }

                if ((id3->elapsed + ff_rewind_count) > id3->length)
                    ff_rewind_count = id3->length - id3->elapsed;

                refresh_wps(false);
                break;

            case BUTTON_LEFT | BUTTON_REL:
                /* rewind */
                mpeg_ff_rewind(ff_rewind_count);
                ff_rewind_count = 0;
                ff_rewind = false;
                if (paused)
                    status_set_playmode(STATUS_PAUSE);
                else {
                    mpeg_resume();
                    status_set_playmode(STATUS_PLAY);
                }
#ifdef HAVE_LCD_CHARCELLS
                draw_screen();
#endif
                exit = true;
                break;

            case BUTTON_RIGHT | BUTTON_REL: 
                /* fast forward */
                mpeg_ff_rewind(ff_rewind_count);
                ff_rewind_count = 0;
                ff_rewind = false;
                if (paused)
                    status_set_playmode(STATUS_PAUSE);
                else {
                    mpeg_resume();
                    status_set_playmode(STATUS_PLAY);
                }
#ifdef HAVE_LCD_CHARCELLS
                draw_screen();
#endif
                exit = true;
                break;

            case SYS_USB_CONNECTED:
                handle_usb();
                usb = true;
                exit = true;
                break;
        }
        if (!exit)
            button = button_get(true);
    }
    refresh_wps(true);
    return usb;
}

static void update(void)
{
    if (mpeg_has_changed_track())
    {
        lcd_stop_scroll();
        id3 = mpeg_current_track();
        draw_screen();
        refresh_wps(true);
    }

    if (id3) {
        refresh_wps(false);
    }

    status_draw();

    /* save resume data */
    if ( id3 && 
         global_settings.resume &&
         global_settings.resume_offset != id3->offset ) {
        DEBUGF("R%X,%X (%X)\n", global_settings.resume_offset,
               id3->offset,id3);
        global_settings.resume_index = id3->index;
        global_settings.resume_offset = id3->offset;
        settings_save();
    }
}


static bool keylock(void)
{
    bool exit = false;

#ifdef HAVE_LCD_CHARCELLS
    lcd_icon(ICON_RECORD, true);
    lcd_icon(ICON_PARAM, false);
#endif
    display_keylock_text(true);
    keys_locked = true;
    refresh_wps(true);
    draw_screen();
    status_draw();
    while (button_get(false)); /* clear button queue */

    while (!exit) {
        switch ( button_get_w_tmo(HZ/5) ) {
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1 | BUTTON_DOWN:
#else
            case BUTTON_MENU | BUTTON_STOP:
#endif
#ifdef HAVE_LCD_CHARCELLS
                lcd_icon(ICON_RECORD, false);
#endif
                display_keylock_text(false);
                keys_locked = false;
                exit = true;
                while (button_get(false)); /* clear button queue */
                break;

            case SYS_USB_CONNECTED:
                handle_usb();
                return true;

            case BUTTON_NONE:
                update();
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1:
#else
            case BUTTON_MENU:
#endif
                /* ignore menu key, to avoid displaying "Keylock ON"
                   every time we unlock the keys */
                break;

            default:
                display_keylock_text(true);
                while (button_get(false)); /* clear button queue */
                refresh_wps(true);
                draw_screen();
                break;
        }
    }
    return false;
}

static bool menu(void)
{
    static bool muted = false;
    bool exit = false;
    int last_button = 0;

#ifdef HAVE_LCD_CHARCELLS
    lcd_icon(ICON_PARAM, true);
#endif

    while (!exit) {
        int button = button_get(true);
        
        switch ( button ) {
            /* go into menu */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1 | BUTTON_REL:
#else
            case BUTTON_MENU | BUTTON_REL:
#endif
                exit = true;
                if ( !last_button ) {
                    lcd_stop_scroll();
                    button_set_release(old_release_mask);
                    main_menu();
                    old_release_mask = button_set_release(RELEASE_MASK);
                }
                break;

                /* mute */
#ifdef HAVE_PLAYER_KEYPAD
            case BUTTON_MENU | BUTTON_PLAY:
#else
            case BUTTON_F1 | BUTTON_UP:
#endif
                if ( muted )
                    mpeg_sound_set(SOUND_VOLUME, global_settings.volume);
                else
                    mpeg_sound_set(SOUND_VOLUME, 0);
                muted = !muted;
#ifdef HAVE_LCD_CHARCELLS
                lcd_icon(ICON_PARAM, false);
#endif
                display_mute_text(muted);
                break;

                /* key lock */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1 | BUTTON_DOWN:
#else
            case BUTTON_MENU | BUTTON_STOP:
#endif
                if (keylock())
                    return true;
                exit = true;
                break;

#ifdef HAVE_PLAYER_KEYPAD
                /* change volume */
            case BUTTON_MENU | BUTTON_LEFT:
            case BUTTON_MENU | BUTTON_LEFT | BUTTON_REPEAT:
                global_settings.volume--;
                if(global_settings.volume < mpeg_sound_min(SOUND_VOLUME))
                    global_settings.volume = mpeg_sound_min(SOUND_VOLUME);
                mpeg_sound_set(SOUND_VOLUME, global_settings.volume);
                display_volume_level(global_settings.volume);
                draw_screen();
                status_draw();
                settings_save();
                break;

                /* change volume */
            case BUTTON_MENU | BUTTON_RIGHT:
            case BUTTON_MENU | BUTTON_RIGHT | BUTTON_REPEAT:
                global_settings.volume++;
                if(global_settings.volume > mpeg_sound_max(SOUND_VOLUME))
                    global_settings.volume = mpeg_sound_max(SOUND_VOLUME);
                mpeg_sound_set(SOUND_VOLUME, global_settings.volume);
                display_volume_level(global_settings.volume);
                draw_screen();
                status_draw();
                settings_save();
                break;

                /* show id3 tags */
            case BUTTON_MENU | BUTTON_ON:
                lcd_stop_scroll();
                lcd_icon(ICON_PARAM, true);
                lcd_icon(ICON_AUDIO, true);
                if(player_id3_show() == SYS_USB_CONNECTED)
                    return true;
                lcd_icon(ICON_PARAM, false);
                lcd_icon(ICON_AUDIO, true);
                draw_screen();
                exit = true;
                break;
#endif

            case SYS_USB_CONNECTED:
                handle_usb();
                return true;
        }
        last_button = button;
    }

#ifdef HAVE_LCD_CHARCELLS
    lcd_icon(ICON_PARAM, false);
#endif

    draw_screen();
    refresh_wps(true);
    return false;
}

/* demonstrates showing different formats from playtune */
int wps_show(void)
{
    int button;
    bool ignore_keyup = true;
    bool restore = false;

    id3 = NULL;

    old_release_mask = button_set_release(RELEASE_MASK);

#ifdef HAVE_LCD_CHARCELLS
    lcd_icon(ICON_AUDIO, true);
    lcd_icon(ICON_PARAM, false);
#else
    if(global_settings.statusbar)
        lcd_setmargins(0, STATUSBAR_HEIGHT);
    else
        lcd_setmargins(0, 0);
#endif

    ff_rewind = false;
    ff_rewind_count = 0;

    load_custom_wps();  /* Load the Custom WPS file, if there is one */

    if(mpeg_is_playing())
    {
        id3 = mpeg_current_track();
        if (id3) {
            draw_screen();
            refresh_wps(true);
        }
        restore = true;
    }

    while ( 1 )
    {
        button = button_get_w_tmo(HZ/5);

        /* discard first event if it's a button release */
        if (button && ignore_keyup)
        {
            ignore_keyup = false;
            if (button & BUTTON_REL && button != SYS_USB_CONNECTED)
                continue;
        }
        
        switch(button)
        {
            /* exit to dir browser */
            case BUTTON_ON:
#ifdef HAVE_LCD_CHARCELLS
                lcd_icon(ICON_RECORD, false);
                lcd_icon(ICON_AUDIO, false);
#endif
                button_set_release(old_release_mask);
                return 0;
                
                /* play/pause */
            case BUTTON_PLAY:
                if ( paused )
                {
                    mpeg_resume();
                    paused = false;
                    status_set_playmode(STATUS_PLAY);
                }
                else
                {
                    mpeg_pause();
                    paused = true;
                    status_set_playmode(STATUS_PAUSE);
                    if (global_settings.resume) {
                        settings_save();
#ifndef HAVE_RTC
                        ata_flush();
#endif
                    }
                }
                break;

                /* volume up */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_UP:
            case BUTTON_UP | BUTTON_REPEAT:
#endif
            case BUTTON_VOL_UP:
                global_settings.volume++;
                if(global_settings.volume > mpeg_sound_max(SOUND_VOLUME))
                    global_settings.volume = mpeg_sound_max(SOUND_VOLUME);
                mpeg_sound_set(SOUND_VOLUME, global_settings.volume);
                status_draw();
                settings_save();
                break;

                /* volume down */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_DOWN:
            case BUTTON_DOWN | BUTTON_REPEAT:
#endif
            case BUTTON_VOL_DOWN:
                global_settings.volume--;
                if(global_settings.volume < mpeg_sound_min(SOUND_VOLUME))
                    global_settings.volume = mpeg_sound_min(SOUND_VOLUME);
                mpeg_sound_set(SOUND_VOLUME, global_settings.volume);
                status_draw();
                settings_save();
                break;

                /* fast forward / rewind */
            case BUTTON_LEFT | BUTTON_REPEAT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
                ffwd_rew(button);
                break;

                /* prev / restart */
            case BUTTON_LEFT | BUTTON_REL:
                if (!id3 || (id3->elapsed < 3*1000))
                    mpeg_prev();
                else {
                    if (!paused)
                        mpeg_pause();

                    mpeg_ff_rewind(-(id3->elapsed));

                    if (!paused)
                        mpeg_resume();
                }
                break;

                /* next */
            case BUTTON_RIGHT | BUTTON_REL:
                mpeg_next();
                break;

                /* menu key functions */
#ifdef HAVE_PLAYER_KEYPAD
            case BUTTON_MENU:
#else
            case BUTTON_F1:
#endif
                if (menu())
                    return SYS_USB_CONNECTED;
                break;

                /* toggle status bar */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F3:
#ifdef HAVE_LCD_BITMAP
                global_settings.statusbar = !global_settings.statusbar;
                settings_save();
                if(global_settings.statusbar)
                    lcd_setmargins(0, STATUSBAR_HEIGHT);
                else
                    lcd_setmargins(0, 0);
                restore = true;
#endif
                break;
#endif

                /* stop and exit wps */
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_OFF:
#else
            case BUTTON_STOP:
#endif
#ifdef HAVE_LCD_CHARCELLS
                lcd_icon(ICON_RECORD, false);
                lcd_icon(ICON_AUDIO, false);
#endif
                mpeg_stop();
                status_set_playmode(STATUS_STOP);
                button_set_release(old_release_mask);
                return 0;
                    
#ifndef SIMULATOR
            case SYS_USB_CONNECTED:
                handle_usb();
                return SYS_USB_CONNECTED;
#endif

            case BUTTON_NONE: /* Timeout */
                update();
                break;
        }

        if ( button )
            ata_spin();

        if (restore) {
            restore = false;
            draw_screen();
            if (id3)
                refresh_wps(false);
        }
    }
}