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()
155 push_current_activity(ACTIVITY_BOOKMARKSLIST
);
156 bookmark
= select_bookmark(RECENT_BOOKMARK_FILE
, false);
157 if (bookmark
!= NULL
)
159 ret
= play_bookmark(bookmark
);
162 pop_current_activity();
166 /* ----------------------------------------------------------------------- */
167 /* This function handles an autobookmark creation. This is an interface */
169 /* ----------------------------------------------------------------------- */
170 bool bookmark_autobookmark(bool prompt_ok
)
175 if (!is_bookmarkable_state())
178 audio_pause(); /* first pause playback */
179 update
= (global_settings
.autoupdatebookmark
&& bookmark_exists());
180 bookmark
= create_bookmark();
181 /* Workaround for inability to speak when paused: all callers will
182 just do audio_stop() when we return, so we can do it right
183 away. This makes it possible to speak the "Create a Bookmark?"
184 prompt and the "Bookmark Created" splash. */
188 return write_bookmark(true, bookmark
);
190 switch (global_settings
.autocreatebookmark
)
193 return write_bookmark(true, bookmark
);
198 case BOOKMARK_RECENT_ONLY_YES
:
199 return write_bookmark(false, bookmark
);
201 #ifdef HAVE_LCD_BITMAP
202 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
203 const struct text_message message
={lines
, 1};
205 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
206 str(LANG_CONFIRM_WITH_BUTTON
)};
207 const struct text_message message
={lines
, 2};
210 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
212 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
213 return write_bookmark(false, bookmark
);
215 return write_bookmark(true, bookmark
);
220 /* ----------------------------------------------------------------------- */
221 /* This function takes the current current resume information and writes */
222 /* that to the beginning of the bookmark file. */
223 /* This file will contain N number of bookmarks in the following format: */
224 /* resume_index*resume_offset*resume_seed*resume_first_index* */
225 /* resume_file*milliseconds*MP3 Title* */
226 /* ------------------------------------------------------------------------*/
227 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
231 return false; /* something didn't happen correctly, do nothing */
233 if (global_settings
.usemrb
)
234 success
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
237 /* writing the bookmark */
238 if (create_bookmark_file
)
240 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
241 sizeof(global_temp_buffer
));
242 if (generate_bookmark_file_name(name
))
244 success
= add_bookmark(global_bookmark_file_name
, bookmark
, false);
248 splash(HZ
, success
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
249 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
254 /* ----------------------------------------------------------------------- */
255 /* This function adds a bookmark to a file. */
256 /* ------------------------------------------------------------------------*/
257 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
260 int temp_bookmark_file
= 0;
261 int bookmark_file
= 0;
262 int bookmark_count
= 0;
263 char* playlist
= NULL
;
269 /* Opening up a temp bookmark file */
270 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
271 "%s.tmp", bookmark_file_name
);
272 temp_bookmark_file
= open(global_temp_buffer
,
273 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
274 if (temp_bookmark_file
< 0)
275 return false; /* can't open the temp file */
277 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
279 playlist
= strchr(bookmark
,'/');
280 cp
= strrchr(bookmark
,';');
285 /* Writing the new bookmark to the begining of the temp file */
286 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
287 write(temp_bookmark_file
, "\n", 1);
290 /* Reading in the previous bookmarks and writing them to the temp file */
291 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
292 if (bookmark_file
>= 0)
294 while (read_line(bookmark_file
, global_read_buffer
,
295 sizeof(global_read_buffer
)) > 0)
297 /* The MRB has a max of MAX_BOOKMARKS in it */
298 /* This keeps it from getting too large */
299 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
302 cp
= strchr(global_read_buffer
,'/');
303 tmp
= strrchr(global_read_buffer
,';');
304 if (parse_bookmark(global_read_buffer
, false) &&
305 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
308 write(temp_bookmark_file
, global_read_buffer
,
309 strlen(global_read_buffer
));
310 write(temp_bookmark_file
, "\n", 1);
313 close(bookmark_file
);
315 close(temp_bookmark_file
);
317 remove(bookmark_file_name
);
318 rename(global_temp_buffer
, bookmark_file_name
);
324 /* ----------------------------------------------------------------------- */
325 /* This function takes the system resume data and formats it into a valid */
327 /* ----------------------------------------------------------------------- */
328 static char* create_bookmark()
330 int resume_index
= 0;
333 if (!is_bookmarkable_state())
334 return NULL
; /* something didn't happen correctly, do nothing */
336 /* grab the currently playing track */
337 struct mp3entry
*id3
= audio_current_track();
341 /* Get some basic resume information */
342 /* queue_resume and queue_resume_index are not used and can be ignored.*/
343 playlist_get_resume_info(&resume_index
);
345 /* Get the currently playing file minus the path */
346 /* This is used when displaying the available bookmarks */
347 file
= strrchr(id3
->path
,'/');
351 /* create the bookmark */
352 snprintf(global_bookmark
, sizeof(global_bookmark
),
353 /* new optional bookmark token descriptors should be inserted
354 just before the "%s;%s" in this line... */
355 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
356 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
358 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
360 /* ... their flags should go here ... */
361 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
368 playlist_get_seed(NULL
),
370 global_settings
.repeat_mode
,
371 global_settings
.playlist_shuffle
,
372 /* ...and their values should go here */
373 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
374 (long)sound_get_pitch(),
375 (long)dsp_get_timestretch(),
377 /* more mandatory tokens */
378 playlist_get_name(NULL
, global_temp_buffer
,
379 sizeof(global_temp_buffer
)),
382 /* checking to see if the bookmark is valid */
383 if (parse_bookmark(global_bookmark
, false))
384 return global_bookmark
;
389 /* ----------------------------------------------------------------------- */
390 /* This function will determine if an autoload is necessary. This is an */
391 /* interface function. */
392 /* ------------------------------------------------------------------------*/
393 bool bookmark_autoload(const char* file
)
395 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
398 /*Checking to see if a bookmark file exists.*/
399 if(!generate_bookmark_file_name(file
))
404 if(!file_exists(global_bookmark_file_name
))
407 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
409 return bookmark_load(global_bookmark_file_name
, true);
413 char* bookmark
= select_bookmark(global_bookmark_file_name
, true);
415 if (bookmark
!= NULL
)
417 if (!play_bookmark(bookmark
))
419 /* Selected bookmark not found. */
420 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
423 /* Act as if autoload was done even if it failed, since the
424 * user did make an active selection.
433 /* ----------------------------------------------------------------------- */
434 /* This function loads the bookmark information into the resume memory. */
435 /* This is an interface function. */
436 /* ------------------------------------------------------------------------*/
437 bool bookmark_load(const char* file
, bool autoload
)
440 char* bookmark
= NULL
;
444 fd
= open(file
, O_RDONLY
);
447 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
448 bookmark
=global_read_buffer
;
454 /* This is not an auto-load, so list the bookmarks */
455 bookmark
= select_bookmark(file
, false);
458 if (bookmark
!= NULL
)
460 if (!play_bookmark(bookmark
))
462 /* Selected bookmark not found. */
465 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
476 static int get_bookmark_count(const char* bookmark_file_name
)
479 int file
= open(bookmark_file_name
, O_RDONLY
);
484 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
493 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
495 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
497 int file
= open(bookmarks
->filename
, O_RDONLY
);
504 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
505 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
507 /* Entire file fits in buffer */
511 bookmarks
->start
= first_line
;
512 bookmarks
->count
= 0;
513 bookmarks
->reload
= false;
515 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
519 if (read_count
>= first_line
)
521 dest
-= strlen(global_read_buffer
) + 1;
523 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
524 + (sizeof(char*) * (bookmarks
->count
+ 1)))
529 strcpy(dest
, global_read_buffer
);
530 bookmarks
->items
[bookmarks
->count
] = dest
;
536 return bookmarks
->start
+ bookmarks
->count
;
539 static const char* get_bookmark_info(int list_index
,
544 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
545 int index
= list_index
/ 2;
547 if (bookmarks
->show_dont_resume
)
551 return list_index
% 2 == 0
552 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
558 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
559 || (index
< bookmarks
->start
))
561 int read_index
= index
;
563 /* Using count as a guide on how far to move could possibly fail
564 * sometimes. Use byte count if that is a problem?
569 /* Move count * 3 / 4 items in the direction the user is moving,
570 * but don't go too close to the end.
572 int offset
= bookmarks
->count
;
573 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
575 if (read_index
< bookmarks
->start
)
580 read_index
= index
- offset
/ 4;
582 if (read_index
> max
)
593 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
599 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
601 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
604 if (list_index
% 2 == 0)
608 int len
= strlen(global_temp_buffer
);
610 if (bookmarks
->show_playlist_name
&& len
> 0)
612 name
= global_temp_buffer
;
615 if (name
[len
] != '/')
626 name
= strrsplt(name
, '/');
633 name
= global_filename
;
637 strrsplt(global_filename
, '.');
638 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
645 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
646 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
647 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
652 static int bookmark_list_voice_cb(int list_index
, void* data
)
654 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
655 int index
= list_index
/ 2;
657 if (bookmarks
->show_dont_resume
)
660 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
663 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
664 bookmarks
->show_playlist_name
);
668 /* ----------------------------------------------------------------------- */
669 /* This displays a the bookmarks in a file and allows the user to */
670 /* select one to play. */
671 /* ------------------------------------------------------------------------*/
672 static char* select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
)
674 struct bookmark_list
* bookmarks
;
675 struct gui_synclist list
;
682 bookmarks
= plugin_get_buffer(&size
);
683 bookmarks
->buffer_size
= size
;
684 bookmarks
->show_dont_resume
= show_dont_resume
;
685 bookmarks
->filename
= bookmark_file_name
;
686 bookmarks
->start
= 0;
687 bookmarks
->show_playlist_name
688 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
689 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
690 if(global_settings
.talk_menu
)
691 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
692 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
700 int count
= get_bookmark_count(bookmark_file_name
);
701 bookmarks
->total_count
= count
;
703 if (bookmarks
->total_count
< 1)
705 /* No more bookmarks, delete file and exit */
706 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
707 remove(bookmark_file_name
);
711 if (bookmarks
->show_dont_resume
)
717 gui_synclist_set_nb_items(&list
, count
* 2);
721 /* Selected item has been deleted */
723 gui_synclist_select_item(&list
, item
* 2);
726 buffer_bookmarks(bookmarks
, bookmarks
->start
);
727 gui_synclist_draw(&list
);
728 cond_talk_ids_fq(VOICE_EXT_BMARK
);
729 gui_synclist_speak_item(&list
);
733 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
734 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
735 item
= gui_synclist_get_sel_pos(&list
) / 2;
737 if (bookmarks
->show_dont_resume
)
742 if (action
== ACTION_STD_CONTEXT
)
744 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
745 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
746 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
747 static const int menu_actions
[] =
749 ACTION_STD_OK
, ACTION_BMS_DELETE
751 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
755 if (selection
>= 0 && selection
<=
756 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
758 action
= menu_actions
[selection
];
768 return bookmarks
->items
[item
- bookmarks
->start
];
771 /* Else fall through */
773 case ACTION_TREE_WPS
:
774 case ACTION_STD_CANCEL
:
778 case ACTION_BMS_DELETE
:
781 const char *lines
[]={
782 ID2P(LANG_REALLY_DELETE
)
784 const char *yes_lines
[]={
788 const struct text_message message
={lines
, 1};
789 const struct text_message yes_message
={yes_lines
, 1};
791 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
793 delete_bookmark(bookmark_file_name
, item
);
794 bookmarks
->reload
= true;
801 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
814 /* ----------------------------------------------------------------------- */
815 /* This function takes a location in a bookmark file and deletes that */
817 /* ------------------------------------------------------------------------*/
818 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
820 int temp_bookmark_file
= 0;
821 int bookmark_file
= 0;
822 int bookmark_count
= 0;
824 /* Opening up a temp bookmark file */
825 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
826 "%s.tmp", bookmark_file_name
);
827 temp_bookmark_file
= open(global_temp_buffer
,
828 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
830 if (temp_bookmark_file
< 0)
831 return false; /* can't open the temp file */
833 /* Reading in the previous bookmarks and writing them to the temp file */
834 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
835 if (bookmark_file
>= 0)
837 while (read_line(bookmark_file
, global_read_buffer
,
838 sizeof(global_read_buffer
)) > 0)
840 if (bookmark_id
!= bookmark_count
)
842 write(temp_bookmark_file
, global_read_buffer
,
843 strlen(global_read_buffer
));
844 write(temp_bookmark_file
, "\n", 1);
848 close(bookmark_file
);
850 close(temp_bookmark_file
);
852 remove(bookmark_file_name
);
853 rename(global_temp_buffer
, bookmark_file_name
);
858 /* ----------------------------------------------------------------------- */
859 /* This function parses a bookmark, says the voice UI part of it. */
860 /* ------------------------------------------------------------------------*/
861 static void say_bookmark(const char* bookmark
,
862 int bookmark_id
, bool show_playlist_name
)
864 if (!parse_bookmark(bookmark
, true))
866 talk_id(LANG_BOOKMARK_INVALID
, false);
870 talk_number(bookmark_id
+ 1, false);
872 #if CONFIG_CODEC == SWCODEC
873 bool is_dir
= (global_temp_buffer
[0]
874 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
876 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
877 together, because there is no guarantee that the same mp3
878 parameters are used. */
879 if(show_playlist_name
)
880 { /* It's useful to know which playlist this is */
882 talk_dir_or_spell(global_temp_buffer
,
883 TALK_IDARRAY(VOICE_DIR
), true);
884 else talk_file_or_spell(NULL
, global_temp_buffer
,
885 TALK_IDARRAY(LANG_PLAYLIST
), true);
888 (void)show_playlist_name
;
892 talk_id(LANG_SHUFFLE
, true);
894 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
895 talk_number(bm
.resume_index
+ 1, true);
896 talk_id(LANG_TIME
, true);
897 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
899 #if CONFIG_CODEC == SWCODEC
902 talk_file_or_spell(global_temp_buffer
, global_filename
,
903 TALK_IDARRAY(VOICE_FILE
), true);
905 { /* Unfortunately if this is a playlist, we do not know in which
906 directory the file is and therefore cannot find the track's
908 talk_id(VOICE_FILE
, true);
909 talk_spell(global_filename
, true);
914 /* ----------------------------------------------------------------------- */
915 /* This function parses a bookmark and then plays it. */
916 /* ------------------------------------------------------------------------*/
917 static bool play_bookmark(const char* bookmark
)
919 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
920 /* preset pitch and speed to 100% in case bookmark doesn't have info */
921 bm
.pitch
= sound_get_pitch();
922 bm
.speed
= dsp_get_timestretch();
925 if (parse_bookmark(bookmark
, true))
927 global_settings
.repeat_mode
= bm
.repeat_mode
;
928 global_settings
.playlist_shuffle
= bm
.shuffle
;
929 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
930 sound_set_pitch(bm
.pitch
);
931 dsp_set_timestretch(bm
.speed
);
933 if (!warn_on_pl_erase())
935 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
936 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
942 static const char* skip_token(const char* s
)
944 while (*s
&& *s
!= ';')
957 static const char* int_token(const char* s
, int* dest
)
960 return skip_token(s
);
963 static const char* long_token(const char* s
, long* dest
)
965 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
966 return skip_token(s
);
969 /* ----------------------------------------------------------------------- */
970 /* This function takes a bookmark and parses it. This function also */
971 /* validates the bookmark. The parse_filenames flag indicates whether */
972 /* the filename tokens are to be extracted. */
973 /* ----------------------------------------------------------------------- */
974 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
976 const char* s
= bookmark
;
979 #define GET_INT_TOKEN(var) s = int_token(s, &var)
980 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
981 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
983 /* if new format bookmark, extract the optional content flags,
984 otherwise treat as an original format bookmark */
986 bool new_format
= (strchr(s
, '>') == s
);
990 GET_INT_TOKEN(opt_flags
);
993 /* extract all original bookmark tokens */
994 GET_INT_TOKEN(bm
.resume_index
);
995 GET_LONG_TOKEN(bm
.resume_offset
);
996 GET_INT_TOKEN(bm
.resume_seed
);
997 if (!new_format
) /* skip deprecated token */
999 GET_LONG_TOKEN(bm
.resume_time
);
1000 GET_INT_TOKEN(bm
.repeat_mode
);
1001 GET_BOOL_TOKEN(bm
.shuffle
);
1003 /* extract all optional bookmark tokens */
1004 if (opt_flags
& BM_PITCH
)
1005 GET_INT_TOKEN(bm
.pitch
);
1006 if (opt_flags
& BM_SPEED
)
1007 GET_INT_TOKEN(bm
.speed
);
1014 end
= strchr(s
, ';');
1016 /* extract file names */
1017 if (parse_filenames
)
1019 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1020 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1021 strlcpy(global_temp_buffer
, s
, len
+ 1);
1026 strlcpy(global_filename
, end
, MAX_PATH
);
1033 /* ----------------------------------------------------------------------- */
1034 /* This function is used by multiple functions and is used to generate a */
1035 /* bookmark named based off of the input. */
1036 /* Changing this function could result in how the bookmarks are stored. */
1037 /* it would be here that the centralized/decentralized bookmark code */
1038 /* could be placed. */
1039 /* ----------------------------------------------------------------------- */
1040 static bool generate_bookmark_file_name(const char *in
)
1042 int len
= strlen(in
);
1044 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1045 /* otherwise, name it based on the in variable */
1046 if (!strcmp("/", in
))
1047 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1050 #ifdef HAVE_MULTIVOLUME
1051 /* The "root" of an extra volume need special handling too. */
1052 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1053 !strcmp("/", global_bookmark_file_name
));
1056 strcpy(global_bookmark_file_name
, in
);
1057 if(global_bookmark_file_name
[len
-1] == '/')
1059 #ifdef HAVE_MULTIVOLUME
1061 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1064 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1070 /* ----------------------------------------------------------------------- */
1071 /* Returns true if a bookmark file exists for the current playlist */
1072 /* ----------------------------------------------------------------------- */
1073 bool bookmark_exists(void)
1077 if(is_bookmarkable_state())
1079 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1080 sizeof(global_temp_buffer
));
1081 if (generate_bookmark_file_name(name
))
1083 exist
= file_exists(global_bookmark_file_name
);
1090 /* ----------------------------------------------------------------------- */
1091 /* Checks the current state of the system and returns true if it is in a */
1092 /* bookmarkable state. */
1093 /* ----------------------------------------------------------------------- */
1094 static bool is_bookmarkable_state(void)
1096 int resume_index
= 0;
1098 if (!(audio_status() && audio_current_track()) ||
1099 /* no track playing */
1100 (playlist_get_resume_info(&resume_index
) == -1) ||
1101 /* invalid queue info */
1102 (playlist_modified(NULL
)))
1103 /* can't bookmark while in the queue */