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 char* select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
);
97 static bool is_bookmarkable_state(void);
98 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
);
99 static int get_bookmark_count(const char* bookmark_file_name
);
101 #define TEMP_BUF_SIZE (MAX_PATH + 1)
102 static char global_temp_buffer
[TEMP_BUF_SIZE
];
103 /* File name created by generate_bookmark_file_name */
104 static char global_bookmark_file_name
[MAX_PATH
];
105 static char global_read_buffer
[MAX_BOOKMARK_SIZE
];
106 /* Bookmark created by create_bookmark*/
107 static char global_bookmark
[MAX_BOOKMARK_SIZE
];
108 /* Filename from parsed bookmark (can be made local where needed) */
109 static char global_filename
[MAX_PATH
];
111 /* ----------------------------------------------------------------------- */
112 /* This is the interface function from the main menu. */
113 /* ----------------------------------------------------------------------- */
114 bool bookmark_create_menu(void)
116 write_bookmark(true, create_bookmark());
120 /* ----------------------------------------------------------------------- */
121 /* This function acts as the load interface from the main menu */
122 /* This function determines the bookmark file name and then loads that file*/
123 /* for the user. The user can then select a bookmark to load. */
124 /* If no file/directory is currently playing, the menu item does not work. */
125 /* ----------------------------------------------------------------------- */
126 bool bookmark_load_menu(void)
128 if (is_bookmarkable_state())
130 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
131 sizeof(global_temp_buffer
));
132 if (generate_bookmark_file_name(name
))
134 char* bookmark
= select_bookmark(global_bookmark_file_name
, false);
136 if (bookmark
!= NULL
)
138 return play_bookmark(bookmark
);
146 /* ----------------------------------------------------------------------- */
147 /* Gives the user a list of the Most Recent Bookmarks. This is an */
148 /* interface function */
149 /* ----------------------------------------------------------------------- */
150 bool bookmark_mrb_load()
152 char* bookmark
= select_bookmark(RECENT_BOOKMARK_FILE
, false);
154 if (bookmark
!= NULL
)
156 return play_bookmark(bookmark
);
162 /* ----------------------------------------------------------------------- */
163 /* This function handles an autobookmark creation. This is an interface */
165 /* ----------------------------------------------------------------------- */
166 bool bookmark_autobookmark(bool prompt_ok
)
171 if (!is_bookmarkable_state())
174 audio_pause(); /* first pause playback */
175 update
= (global_settings
.autoupdatebookmark
&& bookmark_exists());
176 bookmark
= create_bookmark();
177 /* Workaround for inability to speak when paused: all callers will
178 just do audio_stop() when we return, so we can do it right
179 away. This makes it possible to speak the "Create a Bookmark?"
180 prompt and the "Bookmark Created" splash. */
183 return write_bookmark(true, bookmark
);
184 switch (global_settings
.autocreatebookmark
)
187 return write_bookmark(true, bookmark
);
192 case BOOKMARK_RECENT_ONLY_YES
:
193 return write_bookmark(false, bookmark
);
195 #ifdef HAVE_LCD_BITMAP
196 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
197 const struct text_message message
={lines
, 1};
199 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
200 str(LANG_CONFIRM_WITH_BUTTON
)};
201 const struct text_message message
={lines
, 2};
204 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
206 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
207 return write_bookmark(false, bookmark
);
209 return write_bookmark(true, bookmark
);
214 /* ----------------------------------------------------------------------- */
215 /* This function takes the current current resume information and writes */
216 /* that to the beginning of the bookmark file. */
217 /* This file will contain N number of bookmarks in the following format: */
218 /* resume_index*resume_offset*resume_seed*resume_first_index* */
219 /* resume_file*milliseconds*MP3 Title* */
220 /* ------------------------------------------------------------------------*/
221 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
225 return false; /* something didn't happen correctly, do nothing */
227 if (global_settings
.usemrb
)
228 success
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
231 /* writing the bookmark */
232 if (create_bookmark_file
)
234 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
235 sizeof(global_temp_buffer
));
236 if (generate_bookmark_file_name(name
))
238 success
= add_bookmark(global_bookmark_file_name
, bookmark
, false);
242 splash(HZ
, success
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
243 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
248 /* ----------------------------------------------------------------------- */
249 /* This function adds a bookmark to a file. */
250 /* ------------------------------------------------------------------------*/
251 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
254 int temp_bookmark_file
= 0;
255 int bookmark_file
= 0;
256 int bookmark_count
= 0;
257 char* playlist
= NULL
;
263 /* Opening up a temp bookmark file */
264 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
265 "%s.tmp", bookmark_file_name
);
266 temp_bookmark_file
= open(global_temp_buffer
,
267 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
268 if (temp_bookmark_file
< 0)
269 return false; /* can't open the temp file */
271 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
273 playlist
= strchr(bookmark
,'/');
274 cp
= strrchr(bookmark
,';');
279 /* Writing the new bookmark to the begining of the temp file */
280 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
281 write(temp_bookmark_file
, "\n", 1);
284 /* Reading in the previous bookmarks and writing them to the temp file */
285 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
286 if (bookmark_file
>= 0)
288 while (read_line(bookmark_file
, global_read_buffer
,
289 sizeof(global_read_buffer
)) > 0)
291 /* The MRB has a max of MAX_BOOKMARKS in it */
292 /* This keeps it from getting too large */
293 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
296 cp
= strchr(global_read_buffer
,'/');
297 tmp
= strrchr(global_read_buffer
,';');
298 if (parse_bookmark(global_read_buffer
, false) &&
299 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
302 write(temp_bookmark_file
, global_read_buffer
,
303 strlen(global_read_buffer
));
304 write(temp_bookmark_file
, "\n", 1);
307 close(bookmark_file
);
309 close(temp_bookmark_file
);
311 remove(bookmark_file_name
);
312 rename(global_temp_buffer
, bookmark_file_name
);
318 /* ----------------------------------------------------------------------- */
319 /* This function takes the system resume data and formats it into a valid */
321 /* ----------------------------------------------------------------------- */
322 static char* create_bookmark()
324 int resume_index
= 0;
327 if (!is_bookmarkable_state())
328 return NULL
; /* something didn't happen correctly, do nothing */
330 /* grab the currently playing track */
331 struct mp3entry
*id3
= audio_current_track();
335 /* Get some basic resume information */
336 /* queue_resume and queue_resume_index are not used and can be ignored.*/
337 playlist_get_resume_info(&resume_index
);
339 /* Get the currently playing file minus the path */
340 /* This is used when displaying the available bookmarks */
341 file
= strrchr(id3
->path
,'/');
345 /* create the bookmark */
346 snprintf(global_bookmark
, sizeof(global_bookmark
),
347 /* new optional bookmark token descriptors should be inserted
348 just before the "%s;%s" in this line... */
349 #if CONFIG_CODEC == SWCODEC
350 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
352 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
354 /* ... their flags should go here ... */
355 #if CONFIG_CODEC == SWCODEC
362 playlist_get_seed(NULL
),
364 global_settings
.repeat_mode
,
365 global_settings
.playlist_shuffle
,
366 /* ...and their values should go here */
367 #if CONFIG_CODEC == SWCODEC
368 (long)sound_get_pitch(),
369 (long)dsp_get_timestretch(),
371 /* more mandatory tokens */
372 playlist_get_name(NULL
, global_temp_buffer
,
373 sizeof(global_temp_buffer
)),
376 /* checking to see if the bookmark is valid */
377 if (parse_bookmark(global_bookmark
, false))
378 return global_bookmark
;
383 /* ----------------------------------------------------------------------- */
384 /* This function will determine if an autoload is necessary. This is an */
385 /* interface function. */
386 /* ------------------------------------------------------------------------*/
387 bool bookmark_autoload(const char* file
)
389 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
392 /*Checking to see if a bookmark file exists.*/
393 if(!generate_bookmark_file_name(file
))
398 if(!file_exists(global_bookmark_file_name
))
401 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
403 return bookmark_load(global_bookmark_file_name
, true);
407 char* bookmark
= select_bookmark(global_bookmark_file_name
, true);
409 if (bookmark
!= NULL
)
411 if (!play_bookmark(bookmark
))
413 /* Selected bookmark not found. */
414 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
417 /* Act as if autoload was done even if it failed, since the
418 * user did make an active selection.
427 /* ----------------------------------------------------------------------- */
428 /* This function loads the bookmark information into the resume memory. */
429 /* This is an interface function. */
430 /* ------------------------------------------------------------------------*/
431 bool bookmark_load(const char* file
, bool autoload
)
434 char* bookmark
= NULL
;
438 fd
= open(file
, O_RDONLY
);
441 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
442 bookmark
=global_read_buffer
;
448 /* This is not an auto-load, so list the bookmarks */
449 bookmark
= select_bookmark(file
, false);
452 if (bookmark
!= NULL
)
454 if (!play_bookmark(bookmark
))
456 /* Selected bookmark not found. */
459 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
470 static int get_bookmark_count(const char* bookmark_file_name
)
473 int file
= open(bookmark_file_name
, O_RDONLY
);
478 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
487 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
489 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
491 int file
= open(bookmarks
->filename
, O_RDONLY
);
498 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
499 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
501 /* Entire file fits in buffer */
505 bookmarks
->start
= first_line
;
506 bookmarks
->count
= 0;
507 bookmarks
->reload
= false;
509 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
513 if (read_count
>= first_line
)
515 dest
-= strlen(global_read_buffer
) + 1;
517 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
518 + (sizeof(char*) * (bookmarks
->count
+ 1)))
523 strcpy(dest
, global_read_buffer
);
524 bookmarks
->items
[bookmarks
->count
] = dest
;
530 return bookmarks
->start
+ bookmarks
->count
;
533 static const char* get_bookmark_info(int list_index
,
538 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
539 int index
= list_index
/ 2;
541 if (bookmarks
->show_dont_resume
)
545 return list_index
% 2 == 0
546 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
552 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
553 || (index
< bookmarks
->start
))
555 int read_index
= index
;
557 /* Using count as a guide on how far to move could possibly fail
558 * sometimes. Use byte count if that is a problem?
563 /* Move count * 3 / 4 items in the direction the user is moving,
564 * but don't go too close to the end.
566 int offset
= bookmarks
->count
;
567 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
569 if (read_index
< bookmarks
->start
)
574 read_index
= index
- offset
/ 4;
576 if (read_index
> max
)
587 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
593 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
595 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
598 if (list_index
% 2 == 0)
602 int len
= strlen(global_temp_buffer
);
604 if (bookmarks
->show_playlist_name
&& len
> 0)
606 name
= global_temp_buffer
;
609 if (name
[len
] != '/')
620 name
= strrsplt(name
, '/');
627 name
= global_filename
;
631 strrsplt(global_filename
, '.');
632 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
639 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
640 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
641 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
646 static int bookmark_list_voice_cb(int list_index
, void* data
)
648 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
649 int index
= list_index
/ 2;
651 if (bookmarks
->show_dont_resume
)
654 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
657 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
658 bookmarks
->show_playlist_name
);
662 /* ----------------------------------------------------------------------- */
663 /* This displays a the bookmarks in a file and allows the user to */
664 /* select one to play. */
665 /* ------------------------------------------------------------------------*/
666 static char* select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
)
668 struct bookmark_list
* bookmarks
;
669 struct gui_synclist list
;
676 bookmarks
= plugin_get_buffer(&size
);
677 bookmarks
->buffer_size
= size
;
678 bookmarks
->show_dont_resume
= show_dont_resume
;
679 bookmarks
->filename
= bookmark_file_name
;
680 bookmarks
->start
= 0;
681 bookmarks
->show_playlist_name
682 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
683 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
684 if(global_settings
.talk_menu
)
685 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
686 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
694 int count
= get_bookmark_count(bookmark_file_name
);
695 bookmarks
->total_count
= count
;
697 if (bookmarks
->total_count
< 1)
699 /* No more bookmarks, delete file and exit */
700 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
701 remove(bookmark_file_name
);
705 if (bookmarks
->show_dont_resume
)
711 gui_synclist_set_nb_items(&list
, count
* 2);
715 /* Selected item has been deleted */
717 gui_synclist_select_item(&list
, item
* 2);
720 buffer_bookmarks(bookmarks
, bookmarks
->start
);
721 gui_synclist_draw(&list
);
722 cond_talk_ids_fq(VOICE_EXT_BMARK
);
723 gui_synclist_speak_item(&list
);
727 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
728 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
729 item
= gui_synclist_get_sel_pos(&list
) / 2;
731 if (bookmarks
->show_dont_resume
)
736 if (action
== ACTION_STD_CONTEXT
)
738 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
739 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
740 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
741 static const int menu_actions
[] =
743 ACTION_STD_OK
, ACTION_BMS_DELETE
745 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
749 if (selection
>= 0 && selection
<=
750 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
752 action
= menu_actions
[selection
];
762 return bookmarks
->items
[item
- bookmarks
->start
];
765 /* Else fall through */
767 case ACTION_TREE_WPS
:
768 case ACTION_STD_CANCEL
:
772 case ACTION_BMS_DELETE
:
775 const char *lines
[]={
776 ID2P(LANG_REALLY_DELETE
)
778 const char *yes_lines
[]={
782 const struct text_message message
={lines
, 1};
783 const struct text_message yes_message
={yes_lines
, 1};
785 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
787 delete_bookmark(bookmark_file_name
, item
);
788 bookmarks
->reload
= true;
795 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
808 /* ----------------------------------------------------------------------- */
809 /* This function takes a location in a bookmark file and deletes that */
811 /* ------------------------------------------------------------------------*/
812 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
814 int temp_bookmark_file
= 0;
815 int bookmark_file
= 0;
816 int bookmark_count
= 0;
818 /* Opening up a temp bookmark file */
819 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
820 "%s.tmp", bookmark_file_name
);
821 temp_bookmark_file
= open(global_temp_buffer
,
822 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
824 if (temp_bookmark_file
< 0)
825 return false; /* can't open the temp file */
827 /* Reading in the previous bookmarks and writing them to the temp file */
828 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
829 if (bookmark_file
>= 0)
831 while (read_line(bookmark_file
, global_read_buffer
,
832 sizeof(global_read_buffer
)) > 0)
834 if (bookmark_id
!= bookmark_count
)
836 write(temp_bookmark_file
, global_read_buffer
,
837 strlen(global_read_buffer
));
838 write(temp_bookmark_file
, "\n", 1);
842 close(bookmark_file
);
844 close(temp_bookmark_file
);
846 remove(bookmark_file_name
);
847 rename(global_temp_buffer
, bookmark_file_name
);
852 /* ----------------------------------------------------------------------- */
853 /* This function parses a bookmark, says the voice UI part of it. */
854 /* ------------------------------------------------------------------------*/
855 static void say_bookmark(const char* bookmark
,
856 int bookmark_id
, bool show_playlist_name
)
860 if (!parse_bookmark(bookmark
, true))
862 talk_id(LANG_BOOKMARK_INVALID
, false);
866 talk_number(bookmark_id
+ 1, false);
868 is_dir
= (global_temp_buffer
[0]
869 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
870 #if CONFIG_CODEC == SWCODEC
871 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
872 together, because there is no guarantee that the same mp3
873 parameters are used. */
874 if(show_playlist_name
)
875 { /* It's useful to know which playlist this is */
877 talk_dir_or_spell(global_temp_buffer
,
878 TALK_IDARRAY(VOICE_DIR
), true);
879 else talk_file_or_spell(NULL
, global_temp_buffer
,
880 TALK_IDARRAY(LANG_PLAYLIST
), true);
883 (void)show_playlist_name
;
887 talk_id(LANG_SHUFFLE
, true);
889 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
890 talk_number(bm
.resume_index
+ 1, true);
891 talk_id(LANG_TIME
, true);
892 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
894 #if CONFIG_CODEC == SWCODEC
897 talk_file_or_spell(global_temp_buffer
, global_filename
,
898 TALK_IDARRAY(VOICE_FILE
), true);
900 { /* Unfortunately if this is a playlist, we do not know in which
901 directory the file is and therefore cannot find the track's
903 talk_id(VOICE_FILE
, true);
904 talk_spell(global_filename
, true);
909 /* ----------------------------------------------------------------------- */
910 /* This function parses a bookmark and then plays it. */
911 /* ------------------------------------------------------------------------*/
912 static bool play_bookmark(const char* bookmark
)
914 #if CONFIG_CODEC == SWCODEC
915 /* preset pitch and speed to 100% in case bookmark doesn't have info */
916 bm
.pitch
= sound_get_pitch();
917 bm
.speed
= dsp_get_timestretch();
920 if (parse_bookmark(bookmark
, true))
922 global_settings
.repeat_mode
= bm
.repeat_mode
;
923 global_settings
.playlist_shuffle
= bm
.shuffle
;
924 #if CONFIG_CODEC == SWCODEC
925 sound_set_pitch(bm
.pitch
);
926 dsp_set_timestretch(bm
.speed
);
928 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
929 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
935 static const char* skip_token(const char* s
)
937 while (*s
&& *s
!= ';')
950 static const char* int_token(const char* s
, int* dest
)
953 return skip_token(s
);
956 static const char* long_token(const char* s
, long* dest
)
958 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
959 return skip_token(s
);
962 /* ----------------------------------------------------------------------- */
963 /* This function takes a bookmark and parses it. This function also */
964 /* validates the bookmark. The parse_filenames flag indicates whether */
965 /* the filename tokens are to be extracted. */
966 /* ----------------------------------------------------------------------- */
967 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
969 const char* s
= bookmark
;
972 #define GET_INT_TOKEN(var) s = int_token(s, &var)
973 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
974 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
976 /* if new format bookmark, extract the optional content flags,
977 otherwise treat as an original format bookmark */
979 bool new_format
= (strchr(s
, '>') == s
);
983 GET_INT_TOKEN(opt_flags
);
986 /* extract all original bookmark tokens */
987 GET_INT_TOKEN(bm
.resume_index
);
988 GET_LONG_TOKEN(bm
.resume_offset
);
989 GET_INT_TOKEN(bm
.resume_seed
);
990 if (!new_format
) /* skip deprecated token */
992 GET_LONG_TOKEN(bm
.resume_time
);
993 GET_INT_TOKEN(bm
.repeat_mode
);
994 GET_BOOL_TOKEN(bm
.shuffle
);
996 /* extract all optional bookmark tokens */
997 if (opt_flags
& BM_PITCH
)
998 GET_INT_TOKEN(bm
.pitch
);
999 if (opt_flags
& BM_SPEED
)
1000 GET_INT_TOKEN(bm
.speed
);
1007 end
= strchr(s
, ';');
1009 /* extract file names */
1010 if (parse_filenames
)
1012 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1013 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1014 strlcpy(global_temp_buffer
, s
, len
+ 1);
1019 strlcpy(global_filename
, end
, MAX_PATH
);
1026 /* ----------------------------------------------------------------------- */
1027 /* This function is used by multiple functions and is used to generate a */
1028 /* bookmark named based off of the input. */
1029 /* Changing this function could result in how the bookmarks are stored. */
1030 /* it would be here that the centralized/decentralized bookmark code */
1031 /* could be placed. */
1032 /* ----------------------------------------------------------------------- */
1033 static bool generate_bookmark_file_name(const char *in
)
1035 int len
= strlen(in
);
1037 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1038 /* otherwise, name it based on the in variable */
1039 if (!strcmp("/", in
))
1040 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1043 #ifdef HAVE_MULTIVOLUME
1044 /* The "root" of an extra volume need special handling too. */
1045 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1046 !strcmp("/", global_bookmark_file_name
));
1049 strcpy(global_bookmark_file_name
, in
);
1050 if(global_bookmark_file_name
[len
-1] == '/')
1052 #ifdef HAVE_MULTIVOLUME
1054 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1057 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1063 /* ----------------------------------------------------------------------- */
1064 /* Returns true if a bookmark file exists for the current playlist */
1065 /* ----------------------------------------------------------------------- */
1066 bool bookmark_exists(void)
1070 if(is_bookmarkable_state())
1072 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1073 sizeof(global_temp_buffer
));
1074 if (generate_bookmark_file_name(name
))
1076 exist
= file_exists(global_bookmark_file_name
);
1083 /* ----------------------------------------------------------------------- */
1084 /* Checks the current state of the system and returns true if it is in a */
1085 /* bookmarkable state. */
1086 /* ----------------------------------------------------------------------- */
1087 static bool is_bookmarkable_state(void)
1089 int resume_index
= 0;
1091 if (!(audio_status() && audio_current_track()) ||
1092 /* no track playing */
1093 (playlist_get_resume_info(&resume_index
) == -1) ||
1094 /* invalid queue info */
1095 (playlist_modified(NULL
)))
1096 /* can't bookmark while in the queue */