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"
46 #define MAX_BOOKMARKS 10
47 #define MAX_BOOKMARK_SIZE 350
48 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
50 /* Used to buffer bookmarks while displaying the bookmark list. */
58 bool show_dont_resume
;
60 bool show_playlist_name
;
64 /* flags for optional bookmark tokens */
71 unsigned long resume_offset
;
81 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
83 static char* create_bookmark(void);
84 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
);
85 static void say_bookmark(const char* bookmark
,
86 int bookmark_id
, bool show_playlist_name
);
87 static bool play_bookmark(const char* bookmark
);
88 static bool generate_bookmark_file_name(const char *in
);
89 static bool parse_bookmark(const char *bookmark
, const bool get_filenames
);
90 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
);
91 static const char* get_bookmark_info(int list_index
,
95 static int select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
, char** selected_bookmark
);
96 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
);
97 static int get_bookmark_count(const char* bookmark_file_name
);
99 #define TEMP_BUF_SIZE (MAX_PATH + 1)
100 static char global_temp_buffer
[TEMP_BUF_SIZE
];
101 /* File name created by generate_bookmark_file_name */
102 static char global_bookmark_file_name
[MAX_PATH
];
103 static char global_read_buffer
[MAX_BOOKMARK_SIZE
];
104 /* Bookmark created by create_bookmark*/
105 static char global_bookmark
[MAX_BOOKMARK_SIZE
];
106 /* Filename from parsed bookmark (can be made local where needed) */
107 static char global_filename
[MAX_PATH
];
109 /* ----------------------------------------------------------------------- */
110 /* This is an interface function from the context menu. */
111 /* Returns true on successful bookmark creation. */
112 /* ----------------------------------------------------------------------- */
113 bool bookmark_create_menu(void)
115 return write_bookmark(true, create_bookmark());
118 /* ----------------------------------------------------------------------- */
119 /* This function acts as the load interface from the context menu. */
120 /* This function determines the bookmark file name and then loads that file*/
121 /* for the user. The user can then select or delete previous bookmarks. */
122 /* This function returns BOOKMARK_SUCCESS on the selection of a track to */
123 /* resume, BOOKMARK_FAIL if the menu is exited without a selection and */
124 /* BOOKMARK_USB_CONNECTED if the menu is forced to exit due to a USB */
126 /* ----------------------------------------------------------------------- */
127 int bookmark_load_menu(void)
130 int ret
= BOOKMARK_FAIL
;
132 push_current_activity(ACTIVITY_BOOKMARKSLIST
);
134 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
135 sizeof(global_temp_buffer
));
136 if (generate_bookmark_file_name(name
))
138 ret
= select_bookmark(global_bookmark_file_name
, false, &bookmark
);
139 if (bookmark
!= NULL
)
141 ret
= play_bookmark(bookmark
) ? BOOKMARK_SUCCESS
: BOOKMARK_FAIL
;
145 pop_current_activity();
149 /* ----------------------------------------------------------------------- */
150 /* Gives the user a list of the Most Recent Bookmarks. This is an */
151 /* interface function */
152 /* Returns true on the successful selection of a recent bookmark. */
153 /* ----------------------------------------------------------------------- */
154 bool bookmark_mrb_load()
159 push_current_activity(ACTIVITY_BOOKMARKSLIST
);
160 select_bookmark(RECENT_BOOKMARK_FILE
, false, &bookmark
);
161 if (bookmark
!= NULL
)
163 ret
= play_bookmark(bookmark
);
166 pop_current_activity();
170 /* ----------------------------------------------------------------------- */
171 /* This function handles an autobookmark creation. This is an interface */
173 /* Returns true on successful bookmark creation. */
174 /* ----------------------------------------------------------------------- */
175 bool bookmark_autobookmark(bool prompt_ok
)
180 if (!bookmark_is_bookmarkable_state())
183 audio_pause(); /* first pause playback */
184 update
= (global_settings
.autoupdatebookmark
&& bookmark_exists());
185 bookmark
= create_bookmark();
186 #if CONFIG_CODEC != SWCODEC
187 /* Workaround for inability to speak when paused: all callers will
188 just do audio_stop() when we return, so we can do it right
189 away. This makes it possible to speak the "Create a Bookmark?"
190 prompt and the "Bookmark Created" splash. */
195 return write_bookmark(true, bookmark
);
197 switch (global_settings
.autocreatebookmark
)
200 return write_bookmark(true, bookmark
);
205 case BOOKMARK_RECENT_ONLY_YES
:
206 return write_bookmark(false, bookmark
);
208 #ifdef HAVE_LCD_BITMAP
209 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
210 const struct text_message message
={lines
, 1};
212 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
213 str(LANG_CONFIRM_WITH_BUTTON
)};
214 const struct text_message message
={lines
, 2};
217 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
219 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
220 return write_bookmark(false, bookmark
);
222 return write_bookmark(true, bookmark
);
227 /* ----------------------------------------------------------------------- */
228 /* This function takes the current current resume information and writes */
229 /* that to the beginning of the bookmark file. */
230 /* This file will contain N number of bookmarks in the following format: */
231 /* resume_index*resume_offset*resume_seed*resume_first_index* */
232 /* resume_file*milliseconds*MP3 Title* */
233 /* Returns true on successful bookmark write. */
234 /* Returns false if any part of the bookmarking process fails. It is */
235 /* possible that a bookmark is successfully added to the most recent */
236 /* bookmark list but fails to be added to the bookmark file or vice versa. */
237 /* ------------------------------------------------------------------------*/
238 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
244 ret
= false; /* something didn't happen correctly, do nothing */
248 if (global_settings
.usemrb
)
249 ret
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
252 /* writing the bookmark */
253 if (create_bookmark_file
)
255 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
256 sizeof(global_temp_buffer
));
257 if (generate_bookmark_file_name(name
))
259 ret
= ret
& add_bookmark(global_bookmark_file_name
, bookmark
, false);
263 ret
= false; /* generating bookmark file failed */
268 splash(HZ
, ret
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
269 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
274 /* ----------------------------------------------------------------------- */
275 /* This function adds a bookmark to a file. */
276 /* Returns true on successful bookmark add. */
277 /* ------------------------------------------------------------------------*/
278 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
281 int temp_bookmark_file
= 0;
282 int bookmark_file
= 0;
283 int bookmark_count
= 0;
284 char* playlist
= NULL
;
290 /* Opening up a temp bookmark file */
291 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
292 "%s.tmp", bookmark_file_name
);
293 temp_bookmark_file
= open(global_temp_buffer
,
294 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
295 if (temp_bookmark_file
< 0)
296 return false; /* can't open the temp file */
298 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
300 playlist
= strchr(bookmark
,'/');
301 cp
= strrchr(bookmark
,';');
306 /* Writing the new bookmark to the begining of the temp file */
307 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
308 write(temp_bookmark_file
, "\n", 1);
311 /* Reading in the previous bookmarks and writing them to the temp file */
312 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
313 if (bookmark_file
>= 0)
315 while (read_line(bookmark_file
, global_read_buffer
,
316 sizeof(global_read_buffer
)) > 0)
318 /* The MRB has a max of MAX_BOOKMARKS in it */
319 /* This keeps it from getting too large */
320 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
323 cp
= strchr(global_read_buffer
,'/');
324 tmp
= strrchr(global_read_buffer
,';');
325 if (parse_bookmark(global_read_buffer
, false) &&
326 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
329 write(temp_bookmark_file
, global_read_buffer
,
330 strlen(global_read_buffer
));
331 write(temp_bookmark_file
, "\n", 1);
334 close(bookmark_file
);
336 close(temp_bookmark_file
);
338 remove(bookmark_file_name
);
339 rename(global_temp_buffer
, bookmark_file_name
);
345 /* ----------------------------------------------------------------------- */
346 /* This function takes the system resume data and formats it into a valid */
348 /* Returns not NULL on successful bookmark format. */
349 /* ----------------------------------------------------------------------- */
350 static char* create_bookmark()
352 int resume_index
= 0;
355 if (!bookmark_is_bookmarkable_state())
356 return NULL
; /* something didn't happen correctly, do nothing */
358 /* grab the currently playing track */
359 struct mp3entry
*id3
= audio_current_track();
363 /* Get some basic resume information */
364 /* queue_resume and queue_resume_index are not used and can be ignored.*/
365 playlist_get_resume_info(&resume_index
);
367 /* Get the currently playing file minus the path */
368 /* This is used when displaying the available bookmarks */
369 file
= strrchr(id3
->path
,'/');
373 /* create the bookmark */
374 snprintf(global_bookmark
, sizeof(global_bookmark
),
375 /* new optional bookmark token descriptors should be inserted
376 just before the "%s;%s" in this line... */
377 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
378 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
380 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
382 /* ... their flags should go here ... */
383 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
390 playlist_get_seed(NULL
),
392 global_settings
.repeat_mode
,
393 global_settings
.playlist_shuffle
,
394 /* ...and their values should go here */
395 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
396 (long)sound_get_pitch(),
397 (long)dsp_get_timestretch(),
399 /* more mandatory tokens */
400 playlist_get_name(NULL
, global_temp_buffer
,
401 sizeof(global_temp_buffer
)),
404 /* checking to see if the bookmark is valid */
405 if (parse_bookmark(global_bookmark
, false))
406 return global_bookmark
;
411 /* ----------------------------------------------------------------------- */
412 /* This function will determine if an autoload is necessary. This is an */
413 /* interface function. */
414 /* Returns true on bookmark load or bookmark selection. */
415 /* ------------------------------------------------------------------------*/
416 bool bookmark_autoload(const char* file
)
420 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
423 /*Checking to see if a bookmark file exists.*/
424 if(!generate_bookmark_file_name(file
))
429 if(!file_exists(global_bookmark_file_name
))
432 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
434 return bookmark_load(global_bookmark_file_name
, true);
438 select_bookmark(global_bookmark_file_name
, true, &bookmark
);
440 if (bookmark
!= NULL
)
442 if (!play_bookmark(bookmark
))
444 /* Selected bookmark not found. */
445 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
448 /* Act as if autoload was done even if it failed, since the
449 * user did make an active selection.
458 /* ----------------------------------------------------------------------- */
459 /* This function loads the bookmark information into the resume memory. */
460 /* This is an interface function. */
461 /* Returns true on successful bookmark load. */
462 /* ------------------------------------------------------------------------*/
463 bool bookmark_load(const char* file
, bool autoload
)
466 char* bookmark
= NULL
;
470 fd
= open(file
, O_RDONLY
);
473 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
474 bookmark
=global_read_buffer
;
480 /* This is not an auto-load, so list the bookmarks */
481 select_bookmark(file
, false, &bookmark
);
484 if (bookmark
!= NULL
)
486 if (!play_bookmark(bookmark
))
488 /* Selected bookmark not found. */
491 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
502 static int get_bookmark_count(const char* bookmark_file_name
)
505 int file
= open(bookmark_file_name
, O_RDONLY
);
510 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
519 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
521 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
523 int file
= open(bookmarks
->filename
, O_RDONLY
);
530 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
531 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
533 /* Entire file fits in buffer */
537 bookmarks
->start
= first_line
;
538 bookmarks
->count
= 0;
539 bookmarks
->reload
= false;
541 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
545 if (read_count
>= first_line
)
547 dest
-= strlen(global_read_buffer
) + 1;
549 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
550 + (sizeof(char*) * (bookmarks
->count
+ 1)))
555 strcpy(dest
, global_read_buffer
);
556 bookmarks
->items
[bookmarks
->count
] = dest
;
562 return bookmarks
->start
+ bookmarks
->count
;
565 static const char* get_bookmark_info(int list_index
,
570 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
571 int index
= list_index
/ 2;
573 if (bookmarks
->show_dont_resume
)
577 return list_index
% 2 == 0
578 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
584 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
585 || (index
< bookmarks
->start
))
587 int read_index
= index
;
589 /* Using count as a guide on how far to move could possibly fail
590 * sometimes. Use byte count if that is a problem?
595 /* Move count * 3 / 4 items in the direction the user is moving,
596 * but don't go too close to the end.
598 int offset
= bookmarks
->count
;
599 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
601 if (read_index
< bookmarks
->start
)
606 read_index
= index
- offset
/ 4;
608 if (read_index
> max
)
619 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
625 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
627 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
630 if (list_index
% 2 == 0)
634 int len
= strlen(global_temp_buffer
);
636 if (bookmarks
->show_playlist_name
&& len
> 0)
638 name
= global_temp_buffer
;
641 if (name
[len
] != '/')
652 name
= strrsplt(name
, '/');
659 name
= global_filename
;
663 strrsplt(global_filename
, '.');
664 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
671 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
672 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
673 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
678 static int bookmark_list_voice_cb(int list_index
, void* data
)
680 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
681 int index
= list_index
/ 2;
683 if (bookmarks
->show_dont_resume
)
686 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
689 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
690 bookmarks
->show_playlist_name
);
694 /* ----------------------------------------------------------------------- */
695 /* This displays the bookmarks in a file and allows the user to */
696 /* select one to play. */
697 /* *selected_bookmark contains a non NULL value on successful bookmark */
699 /* Returns BOOKMARK_SUCCESS on successful bookmark selection, BOOKMARK_FAIL*/
700 /* if no selection was made and BOOKMARK_USB_CONNECTED if the selection */
701 /* menu is forced to exit due to a USB connection. */
702 /* ------------------------------------------------------------------------*/
703 static int select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
, char** selected_bookmark
)
705 struct bookmark_list
* bookmarks
;
706 struct gui_synclist list
;
712 int ret
= BOOKMARK_FAIL
;
714 bookmarks
= plugin_get_buffer(&size
);
715 bookmarks
->buffer_size
= size
;
716 bookmarks
->show_dont_resume
= show_dont_resume
;
717 bookmarks
->filename
= bookmark_file_name
;
718 bookmarks
->start
= 0;
719 bookmarks
->show_playlist_name
720 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
721 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
722 if(global_settings
.talk_menu
)
723 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
724 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
732 int count
= get_bookmark_count(bookmark_file_name
);
733 bookmarks
->total_count
= count
;
735 if (bookmarks
->total_count
< 1)
737 /* No more bookmarks, delete file and exit */
738 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
739 remove(bookmark_file_name
);
740 *selected_bookmark
= NULL
;
741 return BOOKMARK_FAIL
;
744 if (bookmarks
->show_dont_resume
)
750 gui_synclist_set_nb_items(&list
, count
* 2);
754 /* Selected item has been deleted */
756 gui_synclist_select_item(&list
, item
* 2);
759 buffer_bookmarks(bookmarks
, bookmarks
->start
);
760 gui_synclist_draw(&list
);
761 cond_talk_ids_fq(VOICE_EXT_BMARK
);
762 gui_synclist_speak_item(&list
);
766 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
767 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
768 item
= gui_synclist_get_sel_pos(&list
) / 2;
770 if (bookmarks
->show_dont_resume
)
775 if (action
== ACTION_STD_CONTEXT
)
777 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
778 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
779 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
780 static const int menu_actions
[] =
782 ACTION_STD_OK
, ACTION_BMS_DELETE
784 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
788 if (selection
>= 0 && selection
<=
789 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
791 action
= menu_actions
[selection
];
801 *selected_bookmark
= bookmarks
->items
[item
- bookmarks
->start
];
802 return BOOKMARK_SUCCESS
;
805 /* Else fall through */
807 case ACTION_TREE_WPS
:
808 case ACTION_STD_CANCEL
:
812 case ACTION_BMS_DELETE
:
815 const char *lines
[]={
816 ID2P(LANG_REALLY_DELETE
)
818 const char *yes_lines
[]={
822 const struct text_message message
={lines
, 1};
823 const struct text_message yes_message
={yes_lines
, 1};
825 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
827 delete_bookmark(bookmark_file_name
, item
);
828 bookmarks
->reload
= true;
835 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
837 ret
= BOOKMARK_USB_CONNECTED
;
846 *selected_bookmark
= NULL
;
850 /* ----------------------------------------------------------------------- */
851 /* This function takes a location in a bookmark file and deletes that */
853 /* Returns true on successful bookmark deletion. */
854 /* ------------------------------------------------------------------------*/
855 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
857 int temp_bookmark_file
= 0;
858 int bookmark_file
= 0;
859 int bookmark_count
= 0;
861 /* Opening up a temp bookmark file */
862 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
863 "%s.tmp", bookmark_file_name
);
864 temp_bookmark_file
= open(global_temp_buffer
,
865 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
867 if (temp_bookmark_file
< 0)
868 return false; /* can't open the temp file */
870 /* Reading in the previous bookmarks and writing them to the temp file */
871 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
872 if (bookmark_file
>= 0)
874 while (read_line(bookmark_file
, global_read_buffer
,
875 sizeof(global_read_buffer
)) > 0)
877 if (bookmark_id
!= bookmark_count
)
879 write(temp_bookmark_file
, global_read_buffer
,
880 strlen(global_read_buffer
));
881 write(temp_bookmark_file
, "\n", 1);
885 close(bookmark_file
);
887 close(temp_bookmark_file
);
889 remove(bookmark_file_name
);
890 rename(global_temp_buffer
, bookmark_file_name
);
895 /* ----------------------------------------------------------------------- */
896 /* This function parses a bookmark, says the voice UI part of it. */
897 /* ------------------------------------------------------------------------*/
898 static void say_bookmark(const char* bookmark
,
899 int bookmark_id
, bool show_playlist_name
)
901 if (!parse_bookmark(bookmark
, true))
903 talk_id(LANG_BOOKMARK_INVALID
, false);
907 talk_number(bookmark_id
+ 1, false);
909 #if CONFIG_CODEC == SWCODEC
910 bool is_dir
= (global_temp_buffer
[0]
911 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
913 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
914 together, because there is no guarantee that the same mp3
915 parameters are used. */
916 if(show_playlist_name
)
917 { /* It's useful to know which playlist this is */
919 talk_dir_or_spell(global_temp_buffer
,
920 TALK_IDARRAY(VOICE_DIR
), true);
921 else talk_file_or_spell(NULL
, global_temp_buffer
,
922 TALK_IDARRAY(LANG_PLAYLIST
), true);
925 (void)show_playlist_name
;
929 talk_id(LANG_SHUFFLE
, true);
931 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
932 talk_number(bm
.resume_index
+ 1, true);
933 talk_id(LANG_TIME
, true);
934 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
936 #if CONFIG_CODEC == SWCODEC
939 talk_file_or_spell(global_temp_buffer
, global_filename
,
940 TALK_IDARRAY(VOICE_FILE
), true);
942 { /* Unfortunately if this is a playlist, we do not know in which
943 directory the file is and therefore cannot find the track's
945 talk_id(VOICE_FILE
, true);
946 talk_spell(global_filename
, true);
951 /* ----------------------------------------------------------------------- */
952 /* This function parses a bookmark and then plays it. */
953 /* Returns true on successful bookmark play. */
954 /* ------------------------------------------------------------------------*/
955 static bool play_bookmark(const char* bookmark
)
957 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
958 /* preset pitch and speed to 100% in case bookmark doesn't have info */
959 bm
.pitch
= sound_get_pitch();
960 bm
.speed
= dsp_get_timestretch();
963 if (parse_bookmark(bookmark
, true))
965 global_settings
.repeat_mode
= bm
.repeat_mode
;
966 global_settings
.playlist_shuffle
= bm
.shuffle
;
967 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
968 sound_set_pitch(bm
.pitch
);
969 dsp_set_timestretch(bm
.speed
);
971 if (!warn_on_pl_erase())
973 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
974 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
980 static const char* skip_token(const char* s
)
982 while (*s
&& *s
!= ';')
995 static const char* int_token(const char* s
, int* dest
)
998 return skip_token(s
);
1001 static const char* long_token(const char* s
, long* dest
)
1003 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
1004 return skip_token(s
);
1007 /* ----------------------------------------------------------------------- */
1008 /* This function takes a bookmark and parses it. This function also */
1009 /* validates the bookmark. The parse_filenames flag indicates whether */
1010 /* the filename tokens are to be extracted. */
1011 /* Returns true on successful bookmark parse. */
1012 /* ----------------------------------------------------------------------- */
1013 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
1015 const char* s
= bookmark
;
1018 #define GET_INT_TOKEN(var) s = int_token(s, &var)
1019 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
1020 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
1022 /* if new format bookmark, extract the optional content flags,
1023 otherwise treat as an original format bookmark */
1025 bool new_format
= (strchr(s
, '>') == s
);
1029 GET_INT_TOKEN(opt_flags
);
1032 /* extract all original bookmark tokens */
1033 GET_INT_TOKEN(bm
.resume_index
);
1034 GET_LONG_TOKEN(bm
.resume_offset
);
1035 GET_INT_TOKEN(bm
.resume_seed
);
1036 if (!new_format
) /* skip deprecated token */
1038 GET_LONG_TOKEN(bm
.resume_time
);
1039 GET_INT_TOKEN(bm
.repeat_mode
);
1040 GET_BOOL_TOKEN(bm
.shuffle
);
1042 /* extract all optional bookmark tokens */
1043 if (opt_flags
& BM_PITCH
)
1044 GET_INT_TOKEN(bm
.pitch
);
1045 if (opt_flags
& BM_SPEED
)
1046 GET_INT_TOKEN(bm
.speed
);
1053 end
= strchr(s
, ';');
1055 /* extract file names */
1056 if (parse_filenames
)
1058 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1059 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1060 strlcpy(global_temp_buffer
, s
, len
+ 1);
1065 strlcpy(global_filename
, end
, MAX_PATH
);
1072 /* ----------------------------------------------------------------------- */
1073 /* This function is used by multiple functions and is used to generate a */
1074 /* bookmark named based off of the input. */
1075 /* Changing this function could result in how the bookmarks are stored. */
1076 /* it would be here that the centralized/decentralized bookmark code */
1077 /* could be placed. */
1078 /* Always returns true */
1079 /* ----------------------------------------------------------------------- */
1080 static bool generate_bookmark_file_name(const char *in
)
1082 int len
= strlen(in
);
1084 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1085 /* otherwise, name it based on the in variable */
1086 if (!strcmp("/", in
))
1087 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1090 #ifdef HAVE_MULTIVOLUME
1091 /* The "root" of an extra volume need special handling too. */
1092 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1093 !strcmp("/", global_bookmark_file_name
));
1096 strcpy(global_bookmark_file_name
, in
);
1097 if(global_bookmark_file_name
[len
-1] == '/')
1099 #ifdef HAVE_MULTIVOLUME
1101 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1104 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1110 /* ----------------------------------------------------------------------- */
1111 /* Returns true if a bookmark file exists for the current playlist. */
1112 /* This is an interface function. */
1113 /* ----------------------------------------------------------------------- */
1114 bool bookmark_exists(void)
1118 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1119 sizeof(global_temp_buffer
));
1120 if (generate_bookmark_file_name(name
))
1122 exist
= file_exists(global_bookmark_file_name
);
1127 /* ----------------------------------------------------------------------- */
1128 /* Checks the current state of the system and returns true if it is in a */
1129 /* bookmarkable state. */
1130 /* This is an interface funtion. */
1131 /* ----------------------------------------------------------------------- */
1132 bool bookmark_is_bookmarkable_state(void)
1134 int resume_index
= 0;
1136 if (!(audio_status() && audio_current_track()) ||
1137 /* no track playing */
1138 (playlist_get_resume_info(&resume_index
) == -1) ||
1139 /* invalid queue info */
1140 (playlist_modified(NULL
)))
1141 /* can't bookmark while in the queue */