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 system_check(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)
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
)
172 audio_pause(); /* first pause playback */
173 bookmark
= create_bookmark();
174 /* Workaround for inability to speak when paused: all callers will
175 just do audio_stop() when we return, so we can do it right
176 away. This makes it possible to speak the "Create a Bookmark?"
177 prompt and the "Bookmark Created" splash. */
179 switch (global_settings
.autocreatebookmark
)
182 return write_bookmark(true, bookmark
);
187 case BOOKMARK_RECENT_ONLY_YES
:
188 return write_bookmark(false, bookmark
);
190 #ifdef HAVE_LCD_BITMAP
191 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
192 const struct text_message message
={lines
, 1};
194 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
195 str(LANG_CONFIRM_WITH_BUTTON
)};
196 const struct text_message message
={lines
, 2};
199 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
201 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
202 return write_bookmark(false, bookmark
);
204 return write_bookmark(true, bookmark
);
209 /* ----------------------------------------------------------------------- */
210 /* This function takes the current current resume information and writes */
211 /* that to the beginning of the bookmark file. */
212 /* This file will contain N number of bookmarks in the following format: */
213 /* resume_index*resume_offset*resume_seed*resume_first_index* */
214 /* resume_file*milliseconds*MP3 Title* */
215 /* ------------------------------------------------------------------------*/
216 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
220 return false; /* something didn't happen correctly, do nothing */
222 if (global_settings
.usemrb
)
223 success
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
226 /* writing the bookmark */
227 if (create_bookmark_file
)
229 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
230 sizeof(global_temp_buffer
));
231 if (generate_bookmark_file_name(name
))
233 success
= add_bookmark(global_bookmark_file_name
, bookmark
, false);
237 splash(HZ
, success
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
238 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
243 /* ----------------------------------------------------------------------- */
244 /* This function adds a bookmark to a file. */
245 /* ------------------------------------------------------------------------*/
246 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
249 int temp_bookmark_file
= 0;
250 int bookmark_file
= 0;
251 int bookmark_count
= 0;
252 char* playlist
= NULL
;
258 /* Opening up a temp bookmark file */
259 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
260 "%s.tmp", bookmark_file_name
);
261 temp_bookmark_file
= open(global_temp_buffer
,
262 O_WRONLY
| O_CREAT
| O_TRUNC
);
263 if (temp_bookmark_file
< 0)
264 return false; /* can't open the temp file */
266 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
268 playlist
= strchr(bookmark
,'/');
269 cp
= strrchr(bookmark
,';');
274 /* Writing the new bookmark to the begining of the temp file */
275 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
276 write(temp_bookmark_file
, "\n", 1);
279 /* Reading in the previous bookmarks and writing them to the temp file */
280 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
281 if (bookmark_file
>= 0)
283 while (read_line(bookmark_file
, global_read_buffer
,
284 sizeof(global_read_buffer
)) > 0)
286 /* The MRB has a max of MAX_BOOKMARKS in it */
287 /* This keeps it from getting too large */
288 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
291 cp
= strchr(global_read_buffer
,'/');
292 tmp
= strrchr(global_read_buffer
,';');
293 if (parse_bookmark(global_read_buffer
, false) &&
294 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
297 write(temp_bookmark_file
, global_read_buffer
,
298 strlen(global_read_buffer
));
299 write(temp_bookmark_file
, "\n", 1);
302 close(bookmark_file
);
304 close(temp_bookmark_file
);
306 remove(bookmark_file_name
);
307 rename(global_temp_buffer
, bookmark_file_name
);
313 /* ----------------------------------------------------------------------- */
314 /* This function takes the system resume data and formats it into a valid */
316 /* ----------------------------------------------------------------------- */
317 static char* create_bookmark()
319 int resume_index
= 0;
323 return NULL
; /* something didn't happen correctly, do nothing */
325 /* grab the currently playing track */
326 struct mp3entry
*id3
= audio_current_track();
330 /* Get some basic resume information */
331 /* queue_resume and queue_resume_index are not used and can be ignored.*/
332 playlist_get_resume_info(&resume_index
);
334 /* Get the currently playing file minus the path */
335 /* This is used when displaying the available bookmarks */
336 file
= strrchr(id3
->path
,'/');
340 /* create the bookmark */
341 snprintf(global_bookmark
, sizeof(global_bookmark
),
342 /* new optional bookmark token descriptors should be inserted
343 just before the "%s;%s" in this line... */
344 #if CONFIG_CODEC == SWCODEC
345 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
347 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
349 /* ... their flags should go here ... */
350 #if CONFIG_CODEC == SWCODEC
357 playlist_get_seed(NULL
),
359 global_settings
.repeat_mode
,
360 global_settings
.playlist_shuffle
,
361 /* ...and their values should go here */
362 #if CONFIG_CODEC == SWCODEC
363 (long)sound_get_pitch(),
364 (long)dsp_get_timestretch(),
366 /* more mandatory tokens */
367 playlist_get_name(NULL
, global_temp_buffer
,
368 sizeof(global_temp_buffer
)),
371 /* checking to see if the bookmark is valid */
372 if (parse_bookmark(global_bookmark
, false))
373 return global_bookmark
;
378 /* ----------------------------------------------------------------------- */
379 /* This function will determine if an autoload is necessary. This is an */
380 /* interface function. */
381 /* ------------------------------------------------------------------------*/
382 bool bookmark_autoload(const char* file
)
384 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
387 /*Checking to see if a bookmark file exists.*/
388 if(!generate_bookmark_file_name(file
))
393 if(!file_exists(global_bookmark_file_name
))
396 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
398 return bookmark_load(global_bookmark_file_name
, true);
402 char* bookmark
= select_bookmark(global_bookmark_file_name
, true);
404 if (bookmark
!= NULL
)
406 if (!play_bookmark(bookmark
))
408 /* Selected bookmark not found. */
409 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
412 /* Act as if autoload was done even if it failed, since the
413 * user did make an active selection.
422 /* ----------------------------------------------------------------------- */
423 /* This function loads the bookmark information into the resume memory. */
424 /* This is an interface function. */
425 /* ------------------------------------------------------------------------*/
426 bool bookmark_load(const char* file
, bool autoload
)
429 char* bookmark
= NULL
;
433 fd
= open(file
, O_RDONLY
);
436 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
437 bookmark
=global_read_buffer
;
443 /* This is not an auto-load, so list the bookmarks */
444 bookmark
= select_bookmark(file
, false);
447 if (bookmark
!= NULL
)
449 if (!play_bookmark(bookmark
))
451 /* Selected bookmark not found. */
454 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
465 static int get_bookmark_count(const char* bookmark_file_name
)
468 int file
= open(bookmark_file_name
, O_RDONLY
);
473 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
482 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
484 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
486 int file
= open(bookmarks
->filename
, O_RDONLY
);
493 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
494 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
496 /* Entire file fits in buffer */
500 bookmarks
->start
= first_line
;
501 bookmarks
->count
= 0;
502 bookmarks
->reload
= false;
504 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
508 if (read_count
>= first_line
)
510 dest
-= strlen(global_read_buffer
) + 1;
512 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
513 + (sizeof(char*) * (bookmarks
->count
+ 1)))
518 strcpy(dest
, global_read_buffer
);
519 bookmarks
->items
[bookmarks
->count
] = dest
;
525 return bookmarks
->start
+ bookmarks
->count
;
528 static const char* get_bookmark_info(int list_index
,
533 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
534 int index
= list_index
/ 2;
536 if (bookmarks
->show_dont_resume
)
540 return list_index
% 2 == 0
541 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
547 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
548 || (index
< bookmarks
->start
))
550 int read_index
= index
;
552 /* Using count as a guide on how far to move could possibly fail
553 * sometimes. Use byte count if that is a problem?
558 /* Move count * 3 / 4 items in the direction the user is moving,
559 * but don't go too close to the end.
561 int offset
= bookmarks
->count
;
562 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
564 if (read_index
< bookmarks
->start
)
569 read_index
= index
- offset
/ 4;
571 if (read_index
> max
)
582 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
588 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
590 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
593 if (list_index
% 2 == 0)
597 int len
= strlen(global_temp_buffer
);
599 if (bookmarks
->show_playlist_name
&& len
> 0)
601 name
= global_temp_buffer
;
604 if (name
[len
] != '/')
615 name
= strrsplt(name
, '/');
622 name
= global_filename
;
626 strrsplt(global_filename
, '.');
627 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
634 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
635 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
636 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
641 static int bookmark_list_voice_cb(int list_index
, void* data
)
643 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
644 int index
= list_index
/ 2;
646 if (bookmarks
->show_dont_resume
)
649 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
652 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
653 bookmarks
->show_playlist_name
);
657 /* ----------------------------------------------------------------------- */
658 /* This displays a the bookmarks in a file and allows the user to */
659 /* select one to play. */
660 /* ------------------------------------------------------------------------*/
661 static char* select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
)
663 struct bookmark_list
* bookmarks
;
664 struct gui_synclist list
;
671 bookmarks
= plugin_get_buffer(&size
);
672 bookmarks
->buffer_size
= size
;
673 bookmarks
->show_dont_resume
= show_dont_resume
;
674 bookmarks
->filename
= bookmark_file_name
;
675 bookmarks
->start
= 0;
676 bookmarks
->show_playlist_name
677 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
678 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
679 if(global_settings
.talk_menu
)
680 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
681 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
689 int count
= get_bookmark_count(bookmark_file_name
);
690 bookmarks
->total_count
= count
;
692 if (bookmarks
->total_count
< 1)
694 /* No more bookmarks, delete file and exit */
695 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
696 remove(bookmark_file_name
);
700 if (bookmarks
->show_dont_resume
)
706 gui_synclist_set_nb_items(&list
, count
* 2);
710 /* Selected item has been deleted */
712 gui_synclist_select_item(&list
, item
* 2);
715 buffer_bookmarks(bookmarks
, bookmarks
->start
);
716 gui_synclist_draw(&list
);
717 cond_talk_ids_fq(VOICE_EXT_BMARK
);
718 gui_synclist_speak_item(&list
);
722 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
723 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
724 item
= gui_synclist_get_sel_pos(&list
) / 2;
726 if (bookmarks
->show_dont_resume
)
731 if (action
== ACTION_STD_CONTEXT
)
733 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
734 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
735 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
736 static const int menu_actions
[] =
738 ACTION_STD_OK
, ACTION_BMS_DELETE
740 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
744 if (selection
>= 0 && selection
<=
745 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
747 action
= menu_actions
[selection
];
757 return bookmarks
->items
[item
- bookmarks
->start
];
760 /* Else fall through */
762 case ACTION_TREE_WPS
:
763 case ACTION_STD_CANCEL
:
767 case ACTION_BMS_DELETE
:
770 const char *lines
[]={
771 ID2P(LANG_REALLY_DELETE
)
773 const char *yes_lines
[]={
777 const struct text_message message
={lines
, 1};
778 const struct text_message yes_message
={yes_lines
, 1};
780 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
782 delete_bookmark(bookmark_file_name
, item
);
783 bookmarks
->reload
= true;
790 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
803 /* ----------------------------------------------------------------------- */
804 /* This function takes a location in a bookmark file and deletes that */
806 /* ------------------------------------------------------------------------*/
807 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
809 int temp_bookmark_file
= 0;
810 int bookmark_file
= 0;
811 int bookmark_count
= 0;
813 /* Opening up a temp bookmark file */
814 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
815 "%s.tmp", bookmark_file_name
);
816 temp_bookmark_file
= open(global_temp_buffer
,
817 O_WRONLY
| O_CREAT
| O_TRUNC
);
819 if (temp_bookmark_file
< 0)
820 return false; /* can't open the temp file */
822 /* Reading in the previous bookmarks and writing them to the temp file */
823 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
824 if (bookmark_file
>= 0)
826 while (read_line(bookmark_file
, global_read_buffer
,
827 sizeof(global_read_buffer
)) > 0)
829 if (bookmark_id
!= bookmark_count
)
831 write(temp_bookmark_file
, global_read_buffer
,
832 strlen(global_read_buffer
));
833 write(temp_bookmark_file
, "\n", 1);
837 close(bookmark_file
);
839 close(temp_bookmark_file
);
841 remove(bookmark_file_name
);
842 rename(global_temp_buffer
, bookmark_file_name
);
847 /* ----------------------------------------------------------------------- */
848 /* This function parses a bookmark, says the voice UI part of it. */
849 /* ------------------------------------------------------------------------*/
850 static void say_bookmark(const char* bookmark
,
851 int bookmark_id
, bool show_playlist_name
)
855 if (!parse_bookmark(bookmark
, true))
857 talk_id(LANG_BOOKMARK_INVALID
, false);
861 talk_number(bookmark_id
+ 1, false);
863 is_dir
= (global_temp_buffer
[0]
864 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
865 #if CONFIG_CODEC == SWCODEC
866 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
867 together, because there is no guarantee that the same mp3
868 parameters are used. */
869 if(show_playlist_name
)
870 { /* It's useful to know which playlist this is */
872 talk_dir_or_spell(global_temp_buffer
,
873 TALK_IDARRAY(VOICE_DIR
), true);
874 else talk_file_or_spell(NULL
, global_temp_buffer
,
875 TALK_IDARRAY(LANG_PLAYLIST
), true);
878 (void)show_playlist_name
;
882 talk_id(LANG_SHUFFLE
, true);
884 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
885 talk_number(bm
.resume_index
+ 1, true);
886 talk_id(LANG_TIME
, true);
887 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
889 #if CONFIG_CODEC == SWCODEC
892 talk_file_or_spell(global_temp_buffer
, global_filename
,
893 TALK_IDARRAY(VOICE_FILE
), true);
895 { /* Unfortunately if this is a playlist, we do not know in which
896 directory the file is and therefore cannot find the track's
898 talk_id(VOICE_FILE
, true);
899 talk_spell(global_filename
, true);
904 /* ----------------------------------------------------------------------- */
905 /* This function parses a bookmark and then plays it. */
906 /* ------------------------------------------------------------------------*/
907 static bool play_bookmark(const char* bookmark
)
909 #if CONFIG_CODEC == SWCODEC
910 /* preset pitch and speed to 100% in case bookmark doesn't have info */
911 bm
.pitch
= sound_get_pitch();
912 bm
.speed
= dsp_get_timestretch();
915 if (parse_bookmark(bookmark
, true))
917 global_settings
.repeat_mode
= bm
.repeat_mode
;
918 global_settings
.playlist_shuffle
= bm
.shuffle
;
919 #if CONFIG_CODEC == SWCODEC
920 sound_set_pitch(bm
.pitch
);
921 dsp_set_timestretch(bm
.speed
);
923 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
924 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
930 static const char* skip_token(const char* s
)
932 while (*s
&& *s
!= ';')
945 static const char* int_token(const char* s
, int* dest
)
948 return skip_token(s
);
951 static const char* long_token(const char* s
, long* dest
)
953 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
954 return skip_token(s
);
957 /* ----------------------------------------------------------------------- */
958 /* This function takes a bookmark and parses it. This function also */
959 /* validates the bookmark. The parse_filenames flag indicates whether */
960 /* the filename tokens are to be extracted. */
961 /* ----------------------------------------------------------------------- */
962 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
964 const char* s
= bookmark
;
967 #define GET_INT_TOKEN(var) s = int_token(s, &var)
968 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
969 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
971 /* if new format bookmark, extract the optional content flags,
972 otherwise treat as an original format bookmark */
974 bool new_format
= (strchr(s
, '>') == s
);
978 GET_INT_TOKEN(opt_flags
);
981 /* extract all original bookmark tokens */
982 GET_INT_TOKEN(bm
.resume_index
);
983 GET_LONG_TOKEN(bm
.resume_offset
);
984 GET_INT_TOKEN(bm
.resume_seed
);
985 if (!new_format
) /* skip deprecated token */
987 GET_LONG_TOKEN(bm
.resume_time
);
988 GET_INT_TOKEN(bm
.repeat_mode
);
989 GET_BOOL_TOKEN(bm
.shuffle
);
991 /* extract all optional bookmark tokens */
992 if (opt_flags
& BM_PITCH
)
993 GET_INT_TOKEN(bm
.pitch
);
994 if (opt_flags
& BM_SPEED
)
995 GET_INT_TOKEN(bm
.speed
);
1002 end
= strchr(s
, ';');
1004 /* extract file names */
1005 if (parse_filenames
)
1007 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1008 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1009 strlcpy(global_temp_buffer
, s
, len
+ 1);
1014 strlcpy(global_filename
, end
, MAX_PATH
);
1021 /* ----------------------------------------------------------------------- */
1022 /* This function is used by multiple functions and is used to generate a */
1023 /* bookmark named based off of the input. */
1024 /* Changing this function could result in how the bookmarks are stored. */
1025 /* it would be here that the centralized/decentralized bookmark code */
1026 /* could be placed. */
1027 /* ----------------------------------------------------------------------- */
1028 static bool generate_bookmark_file_name(const char *in
)
1030 int len
= strlen(in
);
1032 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1033 /* otherwise, name it based on the in variable */
1034 if (!strcmp("/", in
))
1035 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1038 #ifdef HAVE_MULTIVOLUME
1039 /* The "root" of an extra volume need special handling too. */
1040 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1041 !strcmp("/", global_bookmark_file_name
));
1044 strcpy(global_bookmark_file_name
, in
);
1045 if(global_bookmark_file_name
[len
-1] == '/')
1047 #ifdef HAVE_MULTIVOLUME
1049 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1052 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1058 /* ----------------------------------------------------------------------- */
1059 /* Returns true if a bookmark file exists for the current playlist */
1060 /* ----------------------------------------------------------------------- */
1061 bool bookmark_exist(void)
1067 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1068 sizeof(global_temp_buffer
));
1069 if (generate_bookmark_file_name(name
))
1071 exist
= file_exists(global_bookmark_file_name
);
1078 /* ----------------------------------------------------------------------- */
1079 /* Checks the current state of the system and returns true if it is in a */
1080 /* bookmarkable state. */
1081 /* ----------------------------------------------------------------------- */
1082 static bool system_check(void)
1084 int resume_index
= 0;
1086 if (!(audio_status() && audio_current_track()) ||
1087 /* no track playing */
1088 (playlist_get_resume_info(&resume_index
) == -1) ||
1089 /* invalid queue info */
1090 (playlist_modified(NULL
)))
1091 /* can't bookmark while in the queue */