summaryrefslogtreecommitdiffstats
path: root/apps/bookmark.c
blob: e832165b3c7198eb8c125e7a1ae8a64e26a88b98 (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
 /***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2003 by Benjamin Metzler
 *
 * 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 <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "applimits.h"
#include "lcd.h"
#include "button.h"
#include "usb.h"
#include "mpeg.h"
#include "wps.h"
#include "settings.h"
#include "bookmark.h"
#include "dir.h"
#include "status.h"
#include "system.h"
#include "errno.h"
#include "icons.h"
#include "atoi.h"
#include "string.h"
#include "menu.h"
#include "lang.h"
#include "screens.h"
#include "status.h"
#include "debug.h"
#include "kernel.h"
#include "sprintf.h"

#define MAX_BOOKMARKS 10
#define MAX_BOOKMARK_SIZE  350
#define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"

static bool  add_bookmark(char* bookmark_file_name, char* bookmark);
static bool  bookmark_load_menu(void);
static bool  check_bookmark(char* bookmark);
static char* create_bookmark(void);
static bool  delete_bookmark(char* bookmark_file_name, int bookmark_id);
static void  display_bookmark(char* bookmark,
                              int bookmark_id, 
                              int bookmark_count);
static bool  generate_bookmark_file_name(char *in,
                                         char *out,
                                         unsigned int max_length);
static char* get_bookmark(char* bookmark_file, int bookmark_count);
static bool  parse_bookmark(char *bookmark,
                            int *resume_index,
                            int *resume_offset,
                            int *resume_seed,
                            int *resume_first_index,
                            char* resume_file,
                            unsigned int resume_file_size,
                            int* ms,
                            int * repeat_mode,
                            bool *shuffle,
                            char* file_name,
                            unsigned int max_file_name_size);
static char* select_bookmark(char* bookmark_file_name);
static bool  system_check(void);
static bool  write_bookmark(bool create_bookmark_file);
static int   get_bookmark_count(char* bookmark_file_name);

static char global_temp_buffer[MAX_PATH+1];
static char global_bookmark_file_name[MAX_PATH];
static char global_read_buffer[MAX_BOOKMARK_SIZE];
static char global_bookmark[MAX_BOOKMARK_SIZE];

/* ----------------------------------------------------------------------- */
/* Displays the bookmark menu options for the user to decide.  This is an  */
/* interface function.                                                     */
/* ----------------------------------------------------------------------- */
bool bookmark_menu(void)
{
    int m;
    bool result;

    struct menu_items items[] = {
        { STR(LANG_BOOKMARK_MENU_CREATE), bookmark_create_menu},
        { STR(LANG_BOOKMARK_MENU_LIST), bookmark_load_menu},
        { STR(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS), bookmark_mrb_load},
    };

    m=menu_init( items, sizeof items / sizeof(struct menu_items), NULL );

#ifdef HAVE_LCD_CHARCELLS
    status_set_param(true);
#endif
    result = menu_run(m);
#ifdef HAVE_LCD_CHARCELLS
    status_set_param(false);
#endif
    menu_exit(m);

    settings_save();

    return result;
}

/* ----------------------------------------------------------------------- */
/* This is the interface function from the main menu.                      */
/* ----------------------------------------------------------------------- */
bool bookmark_create_menu(void)
{
    write_bookmark(true);
    return false;
}

/* ----------------------------------------------------------------------- */
/* This function acts as the load interface from the main menu             */
/* This function determines the bookmark file name and then loads that file*/
/* for the user.  The user can then select a bookmark to load.             */
/* If no file/directory is currently playing, the menu item does not work. */
/* ----------------------------------------------------------------------- */
static bool bookmark_load_menu(void)
{
    bool success = true;
    int  offset;
    int  seed;
    int  index;
    char* bookmark;

    if(!system_check())
        return false;
    else
    {
        char* name = playlist_get_name(NULL, global_temp_buffer,
                                       sizeof(global_temp_buffer));
        if (generate_bookmark_file_name(name,
                                        global_bookmark_file_name,
                                        sizeof(global_bookmark_file_name)))
        {
            bookmark = select_bookmark(global_bookmark_file_name);
            if (!bookmark)
                return false;  /* User exited without selecting a bookmark */

            success = parse_bookmark(bookmark,
                                     &index,
                                     &offset,
                                     &seed,
                                     NULL,
                                     global_temp_buffer,
                                     sizeof(global_temp_buffer),
                                     NULL,
                                     &global_settings.repeat_mode,
                                     &global_settings.playlist_shuffle,
                                     NULL, 0);
        }
        else
        {
            /* something bad happened while creating bookmark name*/
            success = false;
        }

        if (success)
            bookmark_play(global_temp_buffer, index, offset, seed);
    }

    return success;
}

/* ----------------------------------------------------------------------- */
/* Gives the user a list of the Most Recent Bookmarks.  This is an         */
/* interface function                                                      */
/* ----------------------------------------------------------------------- */
bool bookmark_mrb_load()
{
    bool success = true;
    int  offset;
    int  seed;
    int  index;
    char* bookmark;

    bookmark = select_bookmark(RECENT_BOOKMARK_FILE);
    if (!bookmark)
        return false;  /* User exited without selecting a bookmark */

    success = parse_bookmark(bookmark,
                             &index,
                             &offset,
                             &seed,
                             NULL,
                             global_temp_buffer,
                             sizeof(global_temp_buffer),
                             NULL,
                             &global_settings.repeat_mode,
                             &global_settings.playlist_shuffle,
                             NULL, 0);

    if (success)
        bookmark_play(global_temp_buffer, index, offset, seed);

    return success;
}


/* ----------------------------------------------------------------------- */
/* This function handles an autobookmark creation.  This is an interface   */
/* function.                                                               */
/* ----------------------------------------------------------------------- */
bool bookmark_autobookmark(void)
{
    /* prompts the user as to create a bookmark */
    bool done = false;
    int  key = 0;

    if (!system_check())
        return false;

    mpeg_pause();    /* first pause playback */
    switch (global_settings.autocreatebookmark)
    {
        case BOOKMARK_YES:
            return write_bookmark(true);

        case BOOKMARK_NO:
            return false;

        case BOOKMARK_RECENT_ONLY_YES:
            return write_bookmark(false);
    }

    /* Prompting user to confirm bookmark creation */
    lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
    lcd_puts(0,0, str(LANG_AUTO_BOOKMARK_QUERY));
    lcd_puts(0,1, str(LANG_CONFIRM_WITH_PLAY_RECORDER));
    lcd_puts(0,2, str(LANG_CANCEL_WITH_ANY_RECORDER));
#else
    status_draw(false);
    lcd_puts(0,0, str(LANG_AUTO_BOOKMARK_QUERY));
    lcd_puts(0,1,str(LANG_RESUME_CONFIRM_PLAYER));
#endif
    lcd_update();

    while (!done)
    {
        /* Wait for a key to be pushed */
        key = button_get(true);
        switch (key)
        {
            case BUTTON_DOWN | BUTTON_REL:
            case BUTTON_ON | BUTTON_REL:
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_OFF | BUTTON_REL:
            case BUTTON_RIGHT | BUTTON_REL:
            case BUTTON_UP | BUTTON_REL:
#endif
            case BUTTON_LEFT | BUTTON_REL:
                done = true;
                break;

            case BUTTON_PLAY | BUTTON_REL:
                if (global_settings.autocreatebookmark ==
                    BOOKMARK_RECENT_ONLY_ASK)
                    write_bookmark(false);
                else
                    write_bookmark(true);
                done = true;
                break;

            case SYS_USB_CONNECTED:
                usb_screen();
#ifdef HAVE_LCD_CHARCELLS
                status_set_param(true);
#endif
                return false;
        }
    }
    return true;
}

/* ----------------------------------------------------------------------- */
/* This function takes the current current resume information and writes   */
/* that to the beginning of the bookmark file.                             */
/* This file will contain N number of bookmarks in the following format:   */
/* resume_index*resume_offset*resume_seed*resume_first_index*              */
/* resume_file*milliseconds*MP3 Title*                                     */
/* ------------------------------------------------------------------------*/
static bool write_bookmark(bool create_bookmark_file)
{
    bool   success=false;
    char*  bookmark;

    if (!system_check())
       return false; /* something didn't happen correctly, do nothing */

    bookmark = create_bookmark();
    if (!bookmark)
       return false; /* something didn't happen correctly, do nothing */

    if (global_settings.usemrb)
        success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark);


    /* writing the bookmark */
    if (create_bookmark_file)
    {
        char* name = playlist_get_name(NULL, global_temp_buffer,
                                       sizeof(global_temp_buffer));
        if (generate_bookmark_file_name(name,
                                        global_bookmark_file_name,
                                        sizeof(global_bookmark_file_name)))
        {
            success = add_bookmark(global_bookmark_file_name, bookmark);
        }
    }

    if (success)
        splash(HZ, true, str(LANG_BOOKMARK_CREATE_SUCCESS));
    else
        splash(HZ, true, str(LANG_BOOKMARK_CREATE_FAILURE));

    return true;
}

/* ----------------------------------------------------------------------- */
/* This function adds a bookmark to a file.                                */
/* ------------------------------------------------------------------------*/
static bool add_bookmark(char* bookmark_file_name, char* bookmark)
{
    int    temp_bookmark_file = 0;
    int    bookmark_file = 0;
    int    bookmark_count = 0;
    char*  playlist = NULL;
    char*  cp;
    int    len = 0;
    bool   unique = false;

    /* Opening up a temp bookmark file */
    snprintf(global_temp_buffer, sizeof(global_temp_buffer),
             "%s.tmp", bookmark_file_name);
    temp_bookmark_file = open(global_temp_buffer,
                              O_WRONLY | O_CREAT | O_TRUNC);
    if (temp_bookmark_file < 0)
        return false; /* can't open the temp file */

    if (!strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE) &&
        (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
    {
        playlist = strchr(bookmark,'/');
        cp = strrchr(bookmark,';');
        len = cp - playlist;
        unique = true;
    }

    /* Writing the new bookmark to the begining of the temp file */
    write(temp_bookmark_file, bookmark, strlen(bookmark));
    write(temp_bookmark_file, "\n", 1);
    bookmark_count++;

    /* Reading in the previous bookmarks and writing them to the temp file */
    bookmark_file = open(bookmark_file_name, O_RDONLY);
    if (bookmark_file >= 0)
    {
        while (read_line(bookmark_file, global_read_buffer,
                         sizeof(global_read_buffer)))
        {
            /* The MRB has a max of MAX_BOOKMARKS in it */
            /* This keeps it from getting too large */
            if ((strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE)==0))
            {
                if(bookmark_count >= MAX_BOOKMARKS)
                break;
            }
                        
            if (unique)
            {
                cp=strchr(global_read_buffer,'/');
                if (check_bookmark(global_read_buffer) &&
                    strncmp(playlist,cp,len))
                {
                    bookmark_count++;
                    write(temp_bookmark_file, global_read_buffer,
                          strlen(global_read_buffer));
                    write(temp_bookmark_file, "\n", 1);
                }
            }
            else
            {
                if (check_bookmark(global_read_buffer))
                {
                    bookmark_count++;
                    write(temp_bookmark_file, global_read_buffer,
                          strlen(global_read_buffer));
                    write(temp_bookmark_file, "\n", 1);
                }
            }
        }
        close(bookmark_file);
    }
    close(temp_bookmark_file);

    remove(bookmark_file_name);
    rename(global_temp_buffer, bookmark_file_name);

    return true;
}


/* ----------------------------------------------------------------------- */
/* This function takes the system resume data and formats it into a valid  */
/* bookmark.                                                               */
/* ----------------------------------------------------------------------- */
static char* create_bookmark()
{
    int resume_index = 0;
    char *file;

    /* grab the currently playing track */
    struct mp3entry *id3 = mpeg_current_track();
    if(!id3)
        return NULL;

    /* Get some basic resume information */
    /* queue_resume and queue_resume_index are not used and can be ignored.*/
    playlist_get_resume_info(&resume_index);

    /* Get the currently playing file minus the path */
    /* This is used when displaying the available bookmarks */
    file = strrchr(id3->path,'/');
    if(NULL == file)
        return NULL;

    /* create the bookmark */
    snprintf(global_bookmark, sizeof(global_bookmark),
             "%d;%d;%d;%d;%d;%d;%d;%s;%s",
             resume_index,
             id3->offset,
             playlist_get_seed(NULL),
             0,
             id3->elapsed,
             global_settings.repeat_mode,
             global_settings.playlist_shuffle,
             playlist_get_name(NULL, global_temp_buffer,
                sizeof(global_temp_buffer)),
             file+1);

    /* checking to see if the bookmark is valid */
    if (check_bookmark(global_bookmark))
        return global_bookmark;
    else
        return NULL;
}

static bool check_bookmark(char* bookmark)
{
    return parse_bookmark(bookmark,
                          NULL,NULL,NULL, NULL,
                          NULL,0,NULL,NULL,
                          NULL, NULL, 0);
}

/* ----------------------------------------------------------------------- */
/* This function will determine if an autoload is necessary.  This is an   */
/* interface function.                                                     */
/* ------------------------------------------------------------------------*/
bool bookmark_autoload(char* file)
{
    int  key;
    int  fd;
    bool done = false;

    if(global_settings.autoloadbookmark == BOOKMARK_NO)
        return false;

    /*Checking to see if a bookmark file exists.*/
    if(!generate_bookmark_file_name(file,
                                   global_bookmark_file_name,
                                   sizeof(global_bookmark_file_name)))
    {
        return false;
    }

    fd = open(global_bookmark_file_name, O_RDONLY);
    if(fd<0)
        return false;
    if(-1 == lseek(fd, 0, SEEK_END))
    {
        close(fd);
        return false;
    }
    close(fd);

    if(global_settings.autoloadbookmark == BOOKMARK_YES)
    {
        return bookmark_load(global_bookmark_file_name, true);
    }
    else
    {
        while (button_get(false)); /* clear button queue */
        /* Prompting user to confirm bookmark load */
        lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
        lcd_puts_scroll(0,0, str(LANG_BOOKMARK_AUTOLOAD_QUERY));
        lcd_puts(0,1, str(LANG_CONFIRM_WITH_PLAY_RECORDER));
        lcd_puts(0,2, str(LANG_BOOKMARK_SELECT_LIST_BOOKMARKS));
        lcd_puts(0,3, str(LANG_CANCEL_WITH_ANY_RECORDER));
#else
        status_draw(false);
        lcd_puts_scroll(0,0, str(LANG_BOOKMARK_AUTOLOAD_QUERY));
        lcd_puts(0,1,str(LANG_RESUME_CONFIRM_PLAYER));
#endif
        lcd_update();

        sleep(100);

        while(!done)
        {
            /* Wait for a key to be pushed */
            while (button_get(false)); /* clear button queue */
            key = button_get(true);
            switch(key)
            {
                default:
                    return false;
#ifdef HAVE_LCD_BITMAP
                case BUTTON_DOWN:
                    return bookmark_load(global_bookmark_file_name, false);
#endif
                case BUTTON_PLAY:
                    return bookmark_load(global_bookmark_file_name, true);
                case SYS_USB_CONNECTED:
                    status_set_playmode(STATUS_STOP);
                    usb_screen();
#ifdef HAVE_LCD_CHARCELLS
                    status_set_param(true);
#endif
                    return true;
            }
        }
        return true;
    }
}

/* ----------------------------------------------------------------------- */
/* This function loads the bookmark information into the resume memory.    */
/* This is an interface function.                                          */
/* ------------------------------------------------------------------------*/
bool bookmark_load(char* file, bool autoload)
{
    int  fd;
    bool success = true;
    int  offset;
    int  seed;
    int  index;
    char* bookmark = NULL;;

    if(autoload)
    {
        fd = open(file, O_RDONLY);
        if(fd >= 0)
        {
            if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)))
                bookmark=global_read_buffer;
            close(fd);
        }
    }
    else
    {
        /* This is not an auto-load, so list the bookmarks */
        bookmark=select_bookmark(file);
        if(!bookmark)
            return true;  /* User exited without selecting a bookmark */
    }

    if(bookmark)
    {
        success = parse_bookmark(bookmark,
                                &index,
                                &offset,
                                &seed,
                                NULL,
                                global_temp_buffer,
                                sizeof(global_temp_buffer),
                                NULL,
                                &global_settings.repeat_mode,
                                &global_settings.playlist_shuffle,
                                NULL, 0);

    }

    if(success)
        bookmark_play(global_temp_buffer,index,offset,seed);

    return success;
}


static int get_bookmark_count(char* bookmark_file_name)
{
    int read_count = 0;
    int file = open(bookmark_file_name, O_RDONLY);

    if(file < 0)
        return -1;

    /* Get the requested bookmark */
    while(read_line(file, global_read_buffer, sizeof(global_read_buffer)))
    {
        if(check_bookmark(global_read_buffer))
            read_count++;
    }
    
    close(file);
    return read_count;
 
    
}


/* ----------------------------------------------------------------------- */
/* This displays a the bookmarks in a file and allows the user to          */
/* select one to play.                                                     */
/* ------------------------------------------------------------------------*/
static char* select_bookmark(char* bookmark_file_name)
{
    int bookmark_id = 0;
    bool delete_this_bookmark = true;
    int  key = 0;
    char* bookmark;
    int bookmark_count = 0;

    while(true)
    {
        /* Handles the case where the user wants to go below the 0th bookmark */
        if(bookmark_id < 0)
            bookmark_id = 0;

        if(delete_this_bookmark)
        {
            bookmark_count = get_bookmark_count(bookmark_file_name);
            delete_this_bookmark = false;
        }
        
        bookmark = get_bookmark(bookmark_file_name, bookmark_id);

        if (!bookmark)
        {
            /* if there were no bookmarks in the file, delete the file and exit. */
            if(bookmark_id == 0)
            {
                splash(HZ, true, str(LANG_BOOKMARK_LOAD_EMPTY));
                remove(bookmark_file_name);
                while (button_get(false)); /* clear button queue */
                return NULL;
            }
            else
            {
               bookmark_id--;
            }
        }
        else
        {
            display_bookmark(bookmark, bookmark_id, bookmark_count);
        }

        /* waiting for the user to click a button */
        while (button_get(false)); /* clear button queue */
        key = button_get(true);
        switch(key)
        {
            case BUTTON_PLAY:
                /* User wants to use this bookmark */
                return bookmark;

            case BUTTON_ON | BUTTON_PLAY:
                /* User wants to delete this bookmark */
                delete_this_bookmark = true;
                break;

            case SYS_USB_CONNECTED:
                usb_screen();
#ifdef HAVE_LCD_CHARCELLS
                status_set_param(true);
#endif
                return NULL;
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_UP:
                bookmark_id--;
                break;

            case BUTTON_DOWN:
                bookmark_id++;
                break;

            case BUTTON_LEFT:
            case BUTTON_OFF:
                return NULL;
#else
            case BUTTON_LEFT:
                bookmark_id--;
                break;

            case BUTTON_RIGHT:
                bookmark_id++;
                break;

            case BUTTON_STOP:
                return NULL;
#endif
        }

        if (delete_this_bookmark)
        {
           delete_bookmark(bookmark_file_name, bookmark_id);
           bookmark_id--;
        }
    }

    return NULL;
}


/* ----------------------------------------------------------------------- */
/* This function takes a location in a bookmark file and deletes that      */
/* bookmark.                                                               */
/* ------------------------------------------------------------------------*/
static bool delete_bookmark(char* bookmark_file_name, int bookmark_id)
{
    int temp_bookmark_file = 0;
    int bookmark_file = 0;
    int bookmark_count = 0;

    /* Opening up a temp bookmark file */
    snprintf(global_temp_buffer, sizeof(global_temp_buffer),
             "%s.tmp", bookmark_file_name);
    temp_bookmark_file = open(global_temp_buffer,
                              O_WRONLY | O_CREAT | O_TRUNC);
    bookmark_file = open(bookmark_file_name, O_RDONLY);

    if (temp_bookmark_file < 0 || bookmark_file < 0)
        return false; /* can't open one of the files */

    /* Reading in the previous bookmarks and writing them to the temp file */
    while (read_line(bookmark_file, global_read_buffer,
                     sizeof(global_read_buffer)))
    {
        /* The MRB has a max of MAX_BOOKMARKS in it */
        /* This keeps it from getting too large */
        if ((strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE)==0))
        {
            if(bookmark_count >= MAX_BOOKMARKS)
            break;
        }
        
        if (check_bookmark(global_read_buffer))
        {
            if (bookmark_id != bookmark_count)
            {
                write(temp_bookmark_file, global_read_buffer,
                      strlen(global_read_buffer));
                write(temp_bookmark_file, "\n", 1);
            }
            bookmark_count++;
        }
    }

    close(bookmark_file);
    close(temp_bookmark_file);

    remove(bookmark_file_name);
    rename(global_temp_buffer, bookmark_file_name);

    return true;
}

/* ----------------------------------------------------------------------- */
/* This function parses a bookmark and displays it for the user.           */
/* ------------------------------------------------------------------------*/
static void display_bookmark(char* bookmark,
                             int bookmark_id,
                             int bookmark_count)
{
    int  resume_index = 0;
    int  ms = 0;
    int  repeat_mode = 0;
    bool playlist_shuffle = false;
    char MP3_file_name[45];
    int  len;
    char *dot;

    /* getting the index and the time into the file */
    parse_bookmark(bookmark,
                   &resume_index, NULL, NULL, NULL, NULL, 0,
                   &ms, &repeat_mode, &playlist_shuffle,
                   MP3_file_name, sizeof(MP3_file_name));

    lcd_clear_display();
    lcd_stop_scroll();

#ifdef HAVE_LCD_BITMAP
    /* bookmark shuffle and repeat states*/
    switch (repeat_mode)
    {
        case REPEAT_ONE:
            statusbar_icon_play_mode(Icon_RepeatOne);
            break;

        case REPEAT_ALL:
            statusbar_icon_play_mode(Icon_Repeat);
            break;
    }
    if(playlist_shuffle)
        statusbar_icon_shuffle();

    /* File Name */
    len=strlen(MP3_file_name);
    if (len>3)
      dot=strrchr(MP3_file_name + len - 4, '.');
    else
      dot=NULL;
    if (dot)
        *dot='\0';
    lcd_puts_scroll(0, 0, MP3_file_name);
    if (dot)
        *dot='.';

    /* bookmark number */
    snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %2d/%2d",
             str(LANG_BOOKMARK_SELECT_BOOKMARK_TEXT),
             bookmark_id + 1, bookmark_count);
    lcd_puts_scroll(0, 1, global_temp_buffer);

    /* bookmark resume index */
    snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %2d",
             str(LANG_BOOKMARK_SELECT_INDEX_TEXT), resume_index+1);
    lcd_puts_scroll(0, 2, global_temp_buffer);

    /* elapsed time*/
    snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %d:%02d",
             str(LANG_BOOKMARK_SELECT_TIME_TEXT),
             ms / 60000,
             ms % 60000 / 1000);
    lcd_puts_scroll(0, 3, global_temp_buffer);

    /* commands */
    lcd_puts_scroll(0, 4, str(LANG_BOOKMARK_SELECT_PLAY));
    lcd_puts_scroll(0, 5, str(LANG_BOOKMARK_SELECT_EXIT));
    lcd_puts_scroll(0, 6, str(LANG_BOOKMARK_SELECT_DELETE));
#else
    (void)bookmark_id;
    len=strlen(MP3_file_name);
    if (len>3)
      dot=strrchr(MP3_file_name+len-4,'.');
    else
      dot=NULL;
    if (dot)
        *dot='\0';
    snprintf(global_temp_buffer, sizeof(global_temp_buffer),
             "%2d, %d:%02d, %s,",
             (bookmark_count+1),
             ms / 60000,
             ms % 60000 / 1000,
             MP3_file_name);
    status_draw(false);
    lcd_puts_scroll(0,0,global_temp_buffer);
    lcd_puts(0,1,str(LANG_RESUME_CONFIRM_PLAYER));
    if (dot)
        *dot='.';
#endif
    lcd_update();
}

/* ----------------------------------------------------------------------- */
/* This function retrieves a given bookmark from a file.                   */
/* If the bookmark requested is beyond the number of bookmarks available   */
/* in the file, it will return the last one.                               */
/* It also returns the index number of the bookmark in the file            */
/* ------------------------------------------------------------------------*/
static char* get_bookmark(char* bookmark_file, int bookmark_count)
{
    int read_count = -1;
    int result = 0;
    int file = open(bookmark_file, O_RDONLY);

    if (file < 0)
        return NULL;

    /* Get the requested bookmark */
    while (read_count < bookmark_count)
    {
        /*Reading in a single bookmark */
        result = read_line(file,
                           global_read_buffer,
                           sizeof(global_read_buffer));

        /* Reading past the last bookmark in the file
           causes the loop to stop */
        if (result <= 0)
            break;

        read_count++;
    }

    close(file);
    if (read_count == bookmark_count)
        return global_read_buffer;
    else
        return NULL;
}

/* ----------------------------------------------------------------------- */
/* This function takes a bookmark and parses it.  This function also       */
/* validates the bookmark.  Passing in NULL for an output variable         */
/* indicates that value is not requested.                                  */
/* ----------------------------------------------------------------------- */
static bool parse_bookmark(char *bookmark,
                           int *resume_index,
                           int *resume_offset,
                           int *resume_seed,
                           int *resume_first_index,
                           char* resume_file,
                           unsigned int resume_file_size,
                           int* ms,
                           int * repeat_mode, bool *shuffle,
                           char* file_name,
                           unsigned int max_file_name_size)
{
    /* First check to see if a valid line was passed in. */
    int  bookmark_len                = strlen(bookmark);
    int  local_resume_index          = 0;
    int  local_resume_offset         = 0;
    int  local_resume_seed           = 0;
    int  local_resume_first_index    = 0;
    int  local_mS                    = 0;
    int  local_shuffle               = 0;
    int  local_repeat_mode           = 0;
    char* local_resume_file          = NULL;
    char* local_file_name            = NULL;
    char* field;
    char* end;
    static char bookmarkcopy[MAX_BOOKMARK_SIZE];

    /* Don't do anything if the bookmark length is 0 */
    if (bookmark_len <= 0)
        return false;

    /* Making a dup of the bookmark to use with strtok_r */
    strncpy(bookmarkcopy, bookmark, sizeof(bookmarkcopy));
    bookmarkcopy[sizeof(bookmarkcopy) - 1] = 0;

    /* resume_index */
    if ((field = strtok_r(bookmarkcopy, ";", &end)))
        local_resume_index = atoi(field);
    else
        return false;

    /* resume_offset */
    if ((field = strtok_r(NULL, ";", &end)))
        local_resume_offset = atoi(field);
    else
        return false;

    /* resume_seed */
    if ((field = strtok_r(NULL, ";", &end)))
        local_resume_seed = atoi(field);
    else
        return false;

    /* resume_first_index */
    if ((field = strtok_r(NULL, ";", &end)))
        local_resume_first_index = atoi(field);
    else
        return false;

    /* Milliseconds into MP3.  Used for the bookmark select menu */
    if ((field = strtok_r(NULL, ";", &end)))
        local_mS = atoi(field);
    else
        return false;

    /* repeat_mode */
    if ((field = strtok_r(NULL, ";", &end)))
        local_repeat_mode = atoi(field);
    else
        return false;

    /* shuffle mode */
    if ((field = strtok_r(NULL, ";", &end)))
        local_shuffle = atoi(field);
    else
        return false;

    /* resume_file & file_name (for the bookmark select menu)*/
    if (end)
    {
        local_resume_file = strtok_r(NULL, ";", &end);
        if (local_resume_file[strlen(local_resume_file) - 1] == '/')
            local_resume_file[strlen(local_resume_file) - 1] = '\0';

        if (end)
            local_file_name = strtok_r(NULL, ";", &end);
    }
    else
        return false;

    /* Only return the values the calling function wants */
    if (resume_index)
        *resume_index = local_resume_index;

    if (resume_offset)
        *resume_offset = local_resume_offset;

    if (resume_seed)
        *resume_seed = local_resume_seed;

    if (resume_first_index)
        *resume_first_index = local_resume_first_index;

    if (resume_file && local_resume_file)
    {
        strncpy(resume_file, local_resume_file,
                MIN(strlen(local_resume_file), resume_file_size-1));
        resume_file[MIN(strlen(local_resume_file), resume_file_size-1)]=0;
    }

    if (ms)
        *ms = local_mS;

    if (shuffle)
        *shuffle = local_shuffle;

    if (repeat_mode)
        *repeat_mode = local_repeat_mode;

    if (file_name && local_file_name)
    {
        strncpy(file_name, local_file_name,
                MIN(strlen(local_file_name),max_file_name_size-1));
        file_name[MIN(strlen(local_file_name),max_file_name_size-1)]=0;
    }

    return true;
}

/* ----------------------------------------------------------------------- */
/* This function is used by multiple functions and is used to generate a   */
/* bookmark named based off of the input.                                  */
/* Changing this function could result in how the bookmarks are stored.    */
/* it would be here that the centralized/decentralized bookmark code       */
/* could be placed.                                                        */
/* ----------------------------------------------------------------------- */
static bool generate_bookmark_file_name(char *in, char *out,
                                        unsigned int max_length)
{
    char* cp;

    if (!in || !out || max_length <= 0)
        return false;

    if (max_length < strlen(in)+6)
        return false;

    /* if this is a root dir MP3, rename the boomark file root_dir.bmark */
    /* otherwise, name it based on the in variable */
    cp = in;

    cp = in + strlen(in) - 1;
    if (*cp == '/')
        *cp = 0;

    cp = in;
    if (*cp == '/')
        cp++;

    if (strlen(in) > 0)
        snprintf(out, max_length, "/%s.%s", cp, "bmark");
    else
        snprintf(out, max_length, "/root_dir.%s", "bmark");

    return true;
}

/* ----------------------------------------------------------------------- */
/* Checks the current state of the system and returns if it is in a        */
/* bookmarkable state.                                                     */
/* ----------------------------------------------------------------------- */
/* Inputs:                                                                 */
/* ----------------------------------------------------------------------- */
/* Outputs:                                                                */
/* return bool:  Indicates if the system was in a bookmarkable state       */
/* ----------------------------------------------------------------------- */
static bool system_check(void)
{
    int resume_index = 0;
    struct mp3entry *id3 = mpeg_current_track();

    if (!id3)
    {
        /* no track playing */
        return false; 
    }

    /* Checking to see if playing a queued track */
    if (playlist_get_resume_info(&resume_index) == -1)
    {
        /* something bad happened while getting the queue information */
        return false;
    }
    else if (playlist_modified(NULL))
    {
        /* can't bookmark while in the queue */
        return false;
    }

    return true;
}