1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C)2003 by Benjamin Metzler
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
44 #include "filefuncs.h"
47 #define MAX_BOOKMARKS 10
48 #define MAX_BOOKMARK_SIZE 350
49 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
51 /* Used to buffer bookmarks while displaying the bookmark list. */
59 bool show_dont_resume
;
61 bool show_playlist_name
;
65 /* flags for optional bookmark tokens */
72 unsigned long resume_offset
;
82 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
84 static char* create_bookmark(void);
85 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
);
86 static void say_bookmark(const char* bookmark
,
87 int bookmark_id
, bool show_playlist_name
);
88 static bool play_bookmark(const char* bookmark
);
89 static bool generate_bookmark_file_name(const char *in
);
90 static bool parse_bookmark(const char *bookmark
, const bool get_filenames
);
91 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
);
92 static const char* get_bookmark_info(int list_index
,
96 static int select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
, char** selected_bookmark
);
97 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
);
98 static int get_bookmark_count(const char* bookmark_file_name
);
100 #define TEMP_BUF_SIZE (MAX_PATH + 1)
101 static char global_temp_buffer
[TEMP_BUF_SIZE
];
102 /* File name created by generate_bookmark_file_name */
103 static char global_bookmark_file_name
[MAX_PATH
];
104 static char global_read_buffer
[MAX_BOOKMARK_SIZE
];
105 /* Bookmark created by create_bookmark*/
106 static char global_bookmark
[MAX_BOOKMARK_SIZE
];
107 /* Filename from parsed bookmark (can be made local where needed) */
108 static char global_filename
[MAX_PATH
];
110 /* ----------------------------------------------------------------------- */
111 /* This is an interface function from the context menu. */
112 /* Returns true on successful bookmark creation. */
113 /* ----------------------------------------------------------------------- */
114 bool bookmark_create_menu(void)
116 return write_bookmark(true, create_bookmark());
119 /* ----------------------------------------------------------------------- */
120 /* This function acts as the load interface from the context menu. */
121 /* This function determines the bookmark file name and then loads that file*/
122 /* for the user. The user can then select or delete previous bookmarks. */
123 /* This function returns BOOKMARK_SUCCESS on the selection of a track to */
124 /* resume, BOOKMARK_FAIL if the menu is exited without a selection and */
125 /* BOOKMARK_USB_CONNECTED if the menu is forced to exit due to a USB */
127 /* ----------------------------------------------------------------------- */
128 int bookmark_load_menu(void)
131 int ret
= BOOKMARK_FAIL
;
133 push_current_activity(ACTIVITY_BOOKMARKSLIST
);
135 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
136 sizeof(global_temp_buffer
));
137 if (generate_bookmark_file_name(name
))
139 ret
= select_bookmark(global_bookmark_file_name
, false, &bookmark
);
140 if (bookmark
!= NULL
)
142 ret
= play_bookmark(bookmark
) ? BOOKMARK_SUCCESS
: BOOKMARK_FAIL
;
146 pop_current_activity();
150 /* ----------------------------------------------------------------------- */
151 /* Gives the user a list of the Most Recent Bookmarks. This is an */
152 /* interface function */
153 /* Returns true on the successful selection of a recent bookmark. */
154 /* ----------------------------------------------------------------------- */
155 bool bookmark_mrb_load()
160 push_current_activity(ACTIVITY_BOOKMARKSLIST
);
161 select_bookmark(RECENT_BOOKMARK_FILE
, false, &bookmark
);
162 if (bookmark
!= NULL
)
164 ret
= play_bookmark(bookmark
);
167 pop_current_activity();
171 /* ----------------------------------------------------------------------- */
172 /* This function handles an autobookmark creation. This is an interface */
174 /* Returns true on successful bookmark creation. */
175 /* ----------------------------------------------------------------------- */
176 bool bookmark_autobookmark(bool prompt_ok
)
181 if (!bookmark_is_bookmarkable_state())
184 audio_pause(); /* first pause playback */
185 update
= (global_settings
.autoupdatebookmark
&& bookmark_exists());
186 bookmark
= create_bookmark();
187 #if CONFIG_CODEC != SWCODEC
188 /* Workaround for inability to speak when paused: all callers will
189 just do audio_stop() when we return, so we can do it right
190 away. This makes it possible to speak the "Create a Bookmark?"
191 prompt and the "Bookmark Created" splash. */
196 return write_bookmark(true, bookmark
);
198 switch (global_settings
.autocreatebookmark
)
201 return write_bookmark(true, bookmark
);
206 case BOOKMARK_RECENT_ONLY_YES
:
207 return write_bookmark(false, bookmark
);
209 #ifdef HAVE_LCD_BITMAP
210 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
211 const struct text_message message
={lines
, 1};
213 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
214 str(LANG_CONFIRM_WITH_BUTTON
)};
215 const struct text_message message
={lines
, 2};
218 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
220 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
221 return write_bookmark(false, bookmark
);
223 return write_bookmark(true, bookmark
);
228 /* ----------------------------------------------------------------------- */
229 /* This function takes the current current resume information and writes */
230 /* that to the beginning of the bookmark file. */
231 /* This file will contain N number of bookmarks in the following format: */
232 /* resume_index*resume_offset*resume_seed*resume_first_index* */
233 /* resume_file*milliseconds*MP3 Title* */
234 /* Returns true on successful bookmark write. */
235 /* Returns false if any part of the bookmarking process fails. It is */
236 /* possible that a bookmark is successfully added to the most recent */
237 /* bookmark list but fails to be added to the bookmark file or vice versa. */
238 /* ------------------------------------------------------------------------*/
239 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
245 ret
= false; /* something didn't happen correctly, do nothing */
249 if (global_settings
.usemrb
)
250 ret
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
253 /* writing the bookmark */
254 if (create_bookmark_file
)
256 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
257 sizeof(global_temp_buffer
));
258 if (generate_bookmark_file_name(name
))
260 ret
= ret
& add_bookmark(global_bookmark_file_name
, bookmark
, false);
264 ret
= false; /* generating bookmark file failed */
269 splash(HZ
, ret
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
270 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
275 /* ----------------------------------------------------------------------- */
276 /* This function adds a bookmark to a file. */
277 /* Returns true on successful bookmark add. */
278 /* ------------------------------------------------------------------------*/
279 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
282 int temp_bookmark_file
= 0;
283 int bookmark_file
= 0;
284 int bookmark_count
= 0;
285 char* playlist
= NULL
;
291 /* Opening up a temp bookmark file */
292 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
293 "%s.tmp", bookmark_file_name
);
294 temp_bookmark_file
= open(global_temp_buffer
,
295 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
296 if (temp_bookmark_file
< 0)
297 return false; /* can't open the temp file */
299 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
301 playlist
= strchr(bookmark
,'/');
302 cp
= strrchr(bookmark
,';');
307 /* Writing the new bookmark to the begining of the temp file */
308 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
309 write(temp_bookmark_file
, "\n", 1);
312 /* Reading in the previous bookmarks and writing them to the temp file */
313 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
314 if (bookmark_file
>= 0)
316 while (read_line(bookmark_file
, global_read_buffer
,
317 sizeof(global_read_buffer
)) > 0)
319 /* The MRB has a max of MAX_BOOKMARKS in it */
320 /* This keeps it from getting too large */
321 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
324 cp
= strchr(global_read_buffer
,'/');
325 tmp
= strrchr(global_read_buffer
,';');
326 if (parse_bookmark(global_read_buffer
, false) &&
327 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
330 write(temp_bookmark_file
, global_read_buffer
,
331 strlen(global_read_buffer
));
332 write(temp_bookmark_file
, "\n", 1);
335 close(bookmark_file
);
337 close(temp_bookmark_file
);
339 remove(bookmark_file_name
);
340 rename(global_temp_buffer
, bookmark_file_name
);
346 /* ----------------------------------------------------------------------- */
347 /* This function takes the system resume data and formats it into a valid */
349 /* Returns not NULL on successful bookmark format. */
350 /* ----------------------------------------------------------------------- */
351 static char* create_bookmark()
353 int resume_index
= 0;
356 if (!bookmark_is_bookmarkable_state())
357 return NULL
; /* something didn't happen correctly, do nothing */
359 /* grab the currently playing track */
360 struct mp3entry
*id3
= audio_current_track();
364 /* Get some basic resume information */
365 /* queue_resume and queue_resume_index are not used and can be ignored.*/
366 playlist_get_resume_info(&resume_index
);
368 /* Get the currently playing file minus the path */
369 /* This is used when displaying the available bookmarks */
370 file
= strrchr(id3
->path
,'/');
374 /* create the bookmark */
375 snprintf(global_bookmark
, sizeof(global_bookmark
),
376 /* new optional bookmark token descriptors should be inserted
377 just before the "%s;%s" in this line... */
378 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
379 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
381 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
383 /* ... their flags should go here ... */
384 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
391 playlist_get_seed(NULL
),
393 global_settings
.repeat_mode
,
394 global_settings
.playlist_shuffle
,
395 /* ...and their values should go here */
396 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
397 (long)sound_get_pitch(),
398 (long)dsp_get_timestretch(),
400 /* more mandatory tokens */
401 playlist_get_name(NULL
, global_temp_buffer
,
402 sizeof(global_temp_buffer
)),
405 /* checking to see if the bookmark is valid */
406 if (parse_bookmark(global_bookmark
, false))
407 return global_bookmark
;
412 /* ----------------------------------------------------------------------- */
413 /* This function will determine if an autoload is necessary. This is an */
414 /* interface function. */
415 /* Returns true on bookmark load or bookmark selection. */
416 /* ------------------------------------------------------------------------*/
417 bool bookmark_autoload(const char* file
)
421 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
424 /*Checking to see if a bookmark file exists.*/
425 if(!generate_bookmark_file_name(file
))
430 if(!file_exists(global_bookmark_file_name
))
433 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
435 return bookmark_load(global_bookmark_file_name
, true);
439 select_bookmark(global_bookmark_file_name
, true, &bookmark
);
441 if (bookmark
!= NULL
)
443 if (!play_bookmark(bookmark
))
445 /* Selected bookmark not found. */
446 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
449 /* Act as if autoload was done even if it failed, since the
450 * user did make an active selection.
459 /* ----------------------------------------------------------------------- */
460 /* This function loads the bookmark information into the resume memory. */
461 /* This is an interface function. */
462 /* Returns true on successful bookmark load. */
463 /* ------------------------------------------------------------------------*/
464 bool bookmark_load(const char* file
, bool autoload
)
467 char* bookmark
= NULL
;
471 fd
= open(file
, O_RDONLY
);
474 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
475 bookmark
=global_read_buffer
;
481 /* This is not an auto-load, so list the bookmarks */
482 select_bookmark(file
, false, &bookmark
);
485 if (bookmark
!= NULL
)
487 if (!play_bookmark(bookmark
))
489 /* Selected bookmark not found. */
492 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
503 static int get_bookmark_count(const char* bookmark_file_name
)
506 int file
= open(bookmark_file_name
, O_RDONLY
);
511 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
520 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
522 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
524 int file
= open(bookmarks
->filename
, O_RDONLY
);
531 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
532 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
534 /* Entire file fits in buffer */
538 bookmarks
->start
= first_line
;
539 bookmarks
->count
= 0;
540 bookmarks
->reload
= false;
542 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
546 if (read_count
>= first_line
)
548 dest
-= strlen(global_read_buffer
) + 1;
550 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
551 + (sizeof(char*) * (bookmarks
->count
+ 1)))
556 strcpy(dest
, global_read_buffer
);
557 bookmarks
->items
[bookmarks
->count
] = dest
;
563 return bookmarks
->start
+ bookmarks
->count
;
566 static const char* get_bookmark_info(int list_index
,
571 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
572 int index
= list_index
/ 2;
574 if (bookmarks
->show_dont_resume
)
578 return list_index
% 2 == 0
579 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
585 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
586 || (index
< bookmarks
->start
))
588 int read_index
= index
;
590 /* Using count as a guide on how far to move could possibly fail
591 * sometimes. Use byte count if that is a problem?
596 /* Move count * 3 / 4 items in the direction the user is moving,
597 * but don't go too close to the end.
599 int offset
= bookmarks
->count
;
600 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
602 if (read_index
< bookmarks
->start
)
607 read_index
= index
- offset
/ 4;
609 if (read_index
> max
)
620 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
626 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
628 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
631 if (list_index
% 2 == 0)
635 int len
= strlen(global_temp_buffer
);
637 if (bookmarks
->show_playlist_name
&& len
> 0)
639 name
= global_temp_buffer
;
642 if (name
[len
] != '/')
653 name
= strrsplt(name
, '/');
660 name
= global_filename
;
664 strrsplt(global_filename
, '.');
665 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
672 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
673 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
674 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
679 static int bookmark_list_voice_cb(int list_index
, void* data
)
681 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
682 int index
= list_index
/ 2;
684 if (bookmarks
->show_dont_resume
)
687 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
690 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
691 bookmarks
->show_playlist_name
);
695 /* ----------------------------------------------------------------------- */
696 /* This displays the bookmarks in a file and allows the user to */
697 /* select one to play. */
698 /* *selected_bookmark contains a non NULL value on successful bookmark */
700 /* Returns BOOKMARK_SUCCESS on successful bookmark selection, BOOKMARK_FAIL*/
701 /* if no selection was made and BOOKMARK_USB_CONNECTED if the selection */
702 /* menu is forced to exit due to a USB connection. */
703 /* ------------------------------------------------------------------------*/
704 static int select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
, char** selected_bookmark
)
706 struct bookmark_list
* bookmarks
;
707 struct gui_synclist list
;
713 int ret
= BOOKMARK_FAIL
;
715 bookmarks
= plugin_get_buffer(&size
);
716 bookmarks
->buffer_size
= size
;
717 bookmarks
->show_dont_resume
= show_dont_resume
;
718 bookmarks
->filename
= bookmark_file_name
;
719 bookmarks
->start
= 0;
720 bookmarks
->show_playlist_name
721 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
722 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
723 if(global_settings
.talk_menu
)
724 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
725 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
733 int count
= get_bookmark_count(bookmark_file_name
);
734 bookmarks
->total_count
= count
;
736 if (bookmarks
->total_count
< 1)
738 /* No more bookmarks, delete file and exit */
739 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
740 remove(bookmark_file_name
);
741 *selected_bookmark
= NULL
;
742 return BOOKMARK_FAIL
;
745 if (bookmarks
->show_dont_resume
)
751 gui_synclist_set_nb_items(&list
, count
* 2);
755 /* Selected item has been deleted */
757 gui_synclist_select_item(&list
, item
* 2);
760 buffer_bookmarks(bookmarks
, bookmarks
->start
);
761 gui_synclist_draw(&list
);
762 cond_talk_ids_fq(VOICE_EXT_BMARK
);
763 gui_synclist_speak_item(&list
);
767 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
768 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
769 item
= gui_synclist_get_sel_pos(&list
) / 2;
771 if (bookmarks
->show_dont_resume
)
776 if (action
== ACTION_STD_CONTEXT
)
778 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
779 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
780 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
781 static const int menu_actions
[] =
783 ACTION_STD_OK
, ACTION_BMS_DELETE
785 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
789 if (selection
>= 0 && selection
<=
790 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
792 action
= menu_actions
[selection
];
802 *selected_bookmark
= bookmarks
->items
[item
- bookmarks
->start
];
803 return BOOKMARK_SUCCESS
;
806 /* Else fall through */
808 case ACTION_TREE_WPS
:
809 case ACTION_STD_CANCEL
:
813 case ACTION_BMS_DELETE
:
816 const char *lines
[]={
817 ID2P(LANG_REALLY_DELETE
)
819 const char *yes_lines
[]={
823 const struct text_message message
={lines
, 1};
824 const struct text_message yes_message
={yes_lines
, 1};
826 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
828 delete_bookmark(bookmark_file_name
, item
);
829 bookmarks
->reload
= true;
836 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
838 ret
= BOOKMARK_USB_CONNECTED
;
847 *selected_bookmark
= NULL
;
851 /* ----------------------------------------------------------------------- */
852 /* This function takes a location in a bookmark file and deletes that */
854 /* Returns true on successful bookmark deletion. */
855 /* ------------------------------------------------------------------------*/
856 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
858 int temp_bookmark_file
= 0;
859 int bookmark_file
= 0;
860 int bookmark_count
= 0;
862 /* Opening up a temp bookmark file */
863 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
864 "%s.tmp", bookmark_file_name
);
865 temp_bookmark_file
= open(global_temp_buffer
,
866 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
868 if (temp_bookmark_file
< 0)
869 return false; /* can't open the temp file */
871 /* Reading in the previous bookmarks and writing them to the temp file */
872 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
873 if (bookmark_file
>= 0)
875 while (read_line(bookmark_file
, global_read_buffer
,
876 sizeof(global_read_buffer
)) > 0)
878 if (bookmark_id
!= bookmark_count
)
880 write(temp_bookmark_file
, global_read_buffer
,
881 strlen(global_read_buffer
));
882 write(temp_bookmark_file
, "\n", 1);
886 close(bookmark_file
);
888 close(temp_bookmark_file
);
890 remove(bookmark_file_name
);
891 rename(global_temp_buffer
, bookmark_file_name
);
896 /* ----------------------------------------------------------------------- */
897 /* This function parses a bookmark, says the voice UI part of it. */
898 /* ------------------------------------------------------------------------*/
899 static void say_bookmark(const char* bookmark
,
900 int bookmark_id
, bool show_playlist_name
)
902 if (!parse_bookmark(bookmark
, true))
904 talk_id(LANG_BOOKMARK_INVALID
, false);
908 talk_number(bookmark_id
+ 1, false);
910 #if CONFIG_CODEC == SWCODEC
911 bool is_dir
= (global_temp_buffer
[0]
912 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
914 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
915 together, because there is no guarantee that the same mp3
916 parameters are used. */
917 if(show_playlist_name
)
918 { /* It's useful to know which playlist this is */
920 talk_dir_or_spell(global_temp_buffer
,
921 TALK_IDARRAY(VOICE_DIR
), true);
922 else talk_file_or_spell(NULL
, global_temp_buffer
,
923 TALK_IDARRAY(LANG_PLAYLIST
), true);
926 (void)show_playlist_name
;
930 talk_id(LANG_SHUFFLE
, true);
932 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
933 talk_number(bm
.resume_index
+ 1, true);
934 talk_id(LANG_TIME
, true);
935 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
937 #if CONFIG_CODEC == SWCODEC
940 talk_file_or_spell(global_temp_buffer
, global_filename
,
941 TALK_IDARRAY(VOICE_FILE
), true);
943 { /* Unfortunately if this is a playlist, we do not know in which
944 directory the file is and therefore cannot find the track's
946 talk_id(VOICE_FILE
, true);
947 talk_spell(global_filename
, true);
952 /* ----------------------------------------------------------------------- */
953 /* This function parses a bookmark and then plays it. */
954 /* Returns true on successful bookmark play. */
955 /* ------------------------------------------------------------------------*/
956 static bool play_bookmark(const char* bookmark
)
958 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
959 /* preset pitch and speed to 100% in case bookmark doesn't have info */
960 bm
.pitch
= sound_get_pitch();
961 bm
.speed
= dsp_get_timestretch();
964 if (parse_bookmark(bookmark
, true))
966 global_settings
.repeat_mode
= bm
.repeat_mode
;
967 global_settings
.playlist_shuffle
= bm
.shuffle
;
968 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
969 sound_set_pitch(bm
.pitch
);
970 dsp_set_timestretch(bm
.speed
);
972 if (!warn_on_pl_erase())
974 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
975 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
981 static const char* skip_token(const char* s
)
983 while (*s
&& *s
!= ';')
996 static const char* int_token(const char* s
, int* dest
)
999 return skip_token(s
);
1002 static const char* long_token(const char* s
, long* dest
)
1004 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
1005 return skip_token(s
);
1008 /* ----------------------------------------------------------------------- */
1009 /* This function takes a bookmark and parses it. This function also */
1010 /* validates the bookmark. The parse_filenames flag indicates whether */
1011 /* the filename tokens are to be extracted. */
1012 /* Returns true on successful bookmark parse. */
1013 /* ----------------------------------------------------------------------- */
1014 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
1016 const char* s
= bookmark
;
1019 #define GET_INT_TOKEN(var) s = int_token(s, &var)
1020 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
1021 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
1023 /* if new format bookmark, extract the optional content flags,
1024 otherwise treat as an original format bookmark */
1026 bool new_format
= (strchr(s
, '>') == s
);
1030 GET_INT_TOKEN(opt_flags
);
1033 /* extract all original bookmark tokens */
1034 GET_INT_TOKEN(bm
.resume_index
);
1035 GET_LONG_TOKEN(bm
.resume_offset
);
1036 GET_INT_TOKEN(bm
.resume_seed
);
1037 if (!new_format
) /* skip deprecated token */
1039 GET_LONG_TOKEN(bm
.resume_time
);
1040 GET_INT_TOKEN(bm
.repeat_mode
);
1041 GET_BOOL_TOKEN(bm
.shuffle
);
1043 /* extract all optional bookmark tokens */
1044 if (opt_flags
& BM_PITCH
)
1045 GET_INT_TOKEN(bm
.pitch
);
1046 if (opt_flags
& BM_SPEED
)
1047 GET_INT_TOKEN(bm
.speed
);
1054 end
= strchr(s
, ';');
1056 /* extract file names */
1057 if (parse_filenames
)
1059 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1060 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1061 strlcpy(global_temp_buffer
, s
, len
+ 1);
1066 strlcpy(global_filename
, end
, MAX_PATH
);
1073 /* ----------------------------------------------------------------------- */
1074 /* This function is used by multiple functions and is used to generate a */
1075 /* bookmark named based off of the input. */
1076 /* Changing this function could result in how the bookmarks are stored. */
1077 /* it would be here that the centralized/decentralized bookmark code */
1078 /* could be placed. */
1079 /* Always returns true */
1080 /* ----------------------------------------------------------------------- */
1081 static bool generate_bookmark_file_name(const char *in
)
1083 int len
= strlen(in
);
1085 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1086 /* otherwise, name it based on the in variable */
1087 if (!strcmp("/", in
))
1088 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1091 #ifdef HAVE_MULTIVOLUME
1092 /* The "root" of an extra volume need special handling too. */
1093 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1094 !strcmp("/", global_bookmark_file_name
));
1097 strcpy(global_bookmark_file_name
, in
);
1098 if(global_bookmark_file_name
[len
-1] == '/')
1100 #ifdef HAVE_MULTIVOLUME
1102 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1105 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1111 /* ----------------------------------------------------------------------- */
1112 /* Returns true if a bookmark file exists for the current playlist. */
1113 /* This is an interface function. */
1114 /* ----------------------------------------------------------------------- */
1115 bool bookmark_exists(void)
1119 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1120 sizeof(global_temp_buffer
));
1121 if (generate_bookmark_file_name(name
))
1123 exist
= file_exists(global_bookmark_file_name
);
1128 /* ----------------------------------------------------------------------- */
1129 /* Checks the current state of the system and returns true if it is in a */
1130 /* bookmarkable state. */
1131 /* This is an interface funtion. */
1132 /* ----------------------------------------------------------------------- */
1133 bool bookmark_is_bookmarkable_state(void)
1135 int resume_index
= 0;
1137 if (!(audio_status() && audio_current_track()) ||
1138 /* no track playing */
1139 (playlist_get_resume_info(&resume_index
) == -1) ||
1140 /* invalid queue info */
1141 (playlist_modified(NULL
)))
1142 /* can't bookmark while in the queue */