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. */
184 return write_bookmark(true, bookmark
);
186 switch (global_settings
.autocreatebookmark
)
189 return write_bookmark(true, bookmark
);
194 case BOOKMARK_RECENT_ONLY_YES
:
195 return write_bookmark(false, bookmark
);
197 #ifdef HAVE_LCD_BITMAP
198 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
)};
199 const struct text_message message
={lines
, 1};
201 const char *lines
[]={ID2P(LANG_AUTO_BOOKMARK_QUERY
),
202 str(LANG_CONFIRM_WITH_BUTTON
)};
203 const struct text_message message
={lines
, 2};
206 if(prompt_ok
&& gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
)
208 if (global_settings
.autocreatebookmark
== BOOKMARK_RECENT_ONLY_ASK
)
209 return write_bookmark(false, bookmark
);
211 return write_bookmark(true, bookmark
);
216 /* ----------------------------------------------------------------------- */
217 /* This function takes the current current resume information and writes */
218 /* that to the beginning of the bookmark file. */
219 /* This file will contain N number of bookmarks in the following format: */
220 /* resume_index*resume_offset*resume_seed*resume_first_index* */
221 /* resume_file*milliseconds*MP3 Title* */
222 /* ------------------------------------------------------------------------*/
223 static bool write_bookmark(bool create_bookmark_file
, const char *bookmark
)
227 return false; /* something didn't happen correctly, do nothing */
229 if (global_settings
.usemrb
)
230 success
= add_bookmark(RECENT_BOOKMARK_FILE
, bookmark
, true);
233 /* writing the bookmark */
234 if (create_bookmark_file
)
236 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
237 sizeof(global_temp_buffer
));
238 if (generate_bookmark_file_name(name
))
240 success
= add_bookmark(global_bookmark_file_name
, bookmark
, false);
244 splash(HZ
, success
? ID2P(LANG_BOOKMARK_CREATE_SUCCESS
)
245 : ID2P(LANG_BOOKMARK_CREATE_FAILURE
));
250 /* ----------------------------------------------------------------------- */
251 /* This function adds a bookmark to a file. */
252 /* ------------------------------------------------------------------------*/
253 static bool add_bookmark(const char* bookmark_file_name
, const char* bookmark
,
256 int temp_bookmark_file
= 0;
257 int bookmark_file
= 0;
258 int bookmark_count
= 0;
259 char* playlist
= NULL
;
265 /* Opening up a temp bookmark file */
266 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
267 "%s.tmp", bookmark_file_name
);
268 temp_bookmark_file
= open(global_temp_buffer
,
269 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
270 if (temp_bookmark_file
< 0)
271 return false; /* can't open the temp file */
273 if (most_recent
&& (global_settings
.usemrb
== BOOKMARK_UNIQUE_ONLY
))
275 playlist
= strchr(bookmark
,'/');
276 cp
= strrchr(bookmark
,';');
281 /* Writing the new bookmark to the begining of the temp file */
282 write(temp_bookmark_file
, bookmark
, strlen(bookmark
));
283 write(temp_bookmark_file
, "\n", 1);
286 /* Reading in the previous bookmarks and writing them to the temp file */
287 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
288 if (bookmark_file
>= 0)
290 while (read_line(bookmark_file
, global_read_buffer
,
291 sizeof(global_read_buffer
)) > 0)
293 /* The MRB has a max of MAX_BOOKMARKS in it */
294 /* This keeps it from getting too large */
295 if (most_recent
&& (bookmark_count
>= MAX_BOOKMARKS
))
298 cp
= strchr(global_read_buffer
,'/');
299 tmp
= strrchr(global_read_buffer
,';');
300 if (parse_bookmark(global_read_buffer
, false) &&
301 (!unique
|| len
!= tmp
-cp
|| strncmp(playlist
,cp
,len
)))
304 write(temp_bookmark_file
, global_read_buffer
,
305 strlen(global_read_buffer
));
306 write(temp_bookmark_file
, "\n", 1);
309 close(bookmark_file
);
311 close(temp_bookmark_file
);
313 remove(bookmark_file_name
);
314 rename(global_temp_buffer
, bookmark_file_name
);
320 /* ----------------------------------------------------------------------- */
321 /* This function takes the system resume data and formats it into a valid */
323 /* ----------------------------------------------------------------------- */
324 static char* create_bookmark()
326 int resume_index
= 0;
329 if (!is_bookmarkable_state())
330 return NULL
; /* something didn't happen correctly, do nothing */
332 /* grab the currently playing track */
333 struct mp3entry
*id3
= audio_current_track();
337 /* Get some basic resume information */
338 /* queue_resume and queue_resume_index are not used and can be ignored.*/
339 playlist_get_resume_info(&resume_index
);
341 /* Get the currently playing file minus the path */
342 /* This is used when displaying the available bookmarks */
343 file
= strrchr(id3
->path
,'/');
347 /* create the bookmark */
348 snprintf(global_bookmark
, sizeof(global_bookmark
),
349 /* new optional bookmark token descriptors should be inserted
350 just before the "%s;%s" in this line... */
351 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
352 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
354 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
356 /* ... their flags should go here ... */
357 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
364 playlist_get_seed(NULL
),
366 global_settings
.repeat_mode
,
367 global_settings
.playlist_shuffle
,
368 /* ...and their values should go here */
369 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
370 (long)sound_get_pitch(),
371 (long)dsp_get_timestretch(),
373 /* more mandatory tokens */
374 playlist_get_name(NULL
, global_temp_buffer
,
375 sizeof(global_temp_buffer
)),
378 /* checking to see if the bookmark is valid */
379 if (parse_bookmark(global_bookmark
, false))
380 return global_bookmark
;
385 /* ----------------------------------------------------------------------- */
386 /* This function will determine if an autoload is necessary. This is an */
387 /* interface function. */
388 /* ------------------------------------------------------------------------*/
389 bool bookmark_autoload(const char* file
)
391 if(global_settings
.autoloadbookmark
== BOOKMARK_NO
)
394 /*Checking to see if a bookmark file exists.*/
395 if(!generate_bookmark_file_name(file
))
400 if(!file_exists(global_bookmark_file_name
))
403 if(global_settings
.autoloadbookmark
== BOOKMARK_YES
)
405 return bookmark_load(global_bookmark_file_name
, true);
409 char* bookmark
= select_bookmark(global_bookmark_file_name
, true);
411 if (bookmark
!= NULL
)
413 if (!play_bookmark(bookmark
))
415 /* Selected bookmark not found. */
416 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
419 /* Act as if autoload was done even if it failed, since the
420 * user did make an active selection.
429 /* ----------------------------------------------------------------------- */
430 /* This function loads the bookmark information into the resume memory. */
431 /* This is an interface function. */
432 /* ------------------------------------------------------------------------*/
433 bool bookmark_load(const char* file
, bool autoload
)
436 char* bookmark
= NULL
;
440 fd
= open(file
, O_RDONLY
);
443 if(read_line(fd
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
444 bookmark
=global_read_buffer
;
450 /* This is not an auto-load, so list the bookmarks */
451 bookmark
= select_bookmark(file
, false);
454 if (bookmark
!= NULL
)
456 if (!play_bookmark(bookmark
))
458 /* Selected bookmark not found. */
461 splash(HZ
*2, ID2P(LANG_NOTHING_TO_RESUME
));
472 static int get_bookmark_count(const char* bookmark_file_name
)
475 int file
= open(bookmark_file_name
, O_RDONLY
);
480 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
489 static int buffer_bookmarks(struct bookmark_list
* bookmarks
, int first_line
)
491 char* dest
= ((char*) bookmarks
) + bookmarks
->buffer_size
- 1;
493 int file
= open(bookmarks
->filename
, O_RDONLY
);
500 if ((first_line
!= 0) && ((size_t) filesize(file
) < bookmarks
->buffer_size
501 - sizeof(*bookmarks
) - (sizeof(char*) * bookmarks
->total_count
)))
503 /* Entire file fits in buffer */
507 bookmarks
->start
= first_line
;
508 bookmarks
->count
= 0;
509 bookmarks
->reload
= false;
511 while(read_line(file
, global_read_buffer
, sizeof(global_read_buffer
)) > 0)
515 if (read_count
>= first_line
)
517 dest
-= strlen(global_read_buffer
) + 1;
519 if (dest
< ((char*) bookmarks
) + sizeof(*bookmarks
)
520 + (sizeof(char*) * (bookmarks
->count
+ 1)))
525 strcpy(dest
, global_read_buffer
);
526 bookmarks
->items
[bookmarks
->count
] = dest
;
532 return bookmarks
->start
+ bookmarks
->count
;
535 static const char* get_bookmark_info(int list_index
,
540 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
541 int index
= list_index
/ 2;
543 if (bookmarks
->show_dont_resume
)
547 return list_index
% 2 == 0
548 ? (char*) str(LANG_BOOKMARK_DONT_RESUME
) : " ";
554 if (bookmarks
->reload
|| (index
>= bookmarks
->start
+ bookmarks
->count
)
555 || (index
< bookmarks
->start
))
557 int read_index
= index
;
559 /* Using count as a guide on how far to move could possibly fail
560 * sometimes. Use byte count if that is a problem?
565 /* Move count * 3 / 4 items in the direction the user is moving,
566 * but don't go too close to the end.
568 int offset
= bookmarks
->count
;
569 int max
= bookmarks
->total_count
- (bookmarks
->count
/ 2);
571 if (read_index
< bookmarks
->start
)
576 read_index
= index
- offset
/ 4;
578 if (read_index
> max
)
589 if (buffer_bookmarks(bookmarks
, read_index
) <= index
)
595 if (!parse_bookmark(bookmarks
->items
[index
- bookmarks
->start
], true))
597 return list_index
% 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID
) : " ";
600 if (list_index
% 2 == 0)
604 int len
= strlen(global_temp_buffer
);
606 if (bookmarks
->show_playlist_name
&& len
> 0)
608 name
= global_temp_buffer
;
611 if (name
[len
] != '/')
622 name
= strrsplt(name
, '/');
629 name
= global_filename
;
633 strrsplt(global_filename
, '.');
634 snprintf(buffer
, buffer_len
, format
, name
, global_filename
);
641 format_time(time_buf
, sizeof(time_buf
), bm
.resume_time
);
642 snprintf(buffer
, buffer_len
, "%s, %d%s", time_buf
, bm
.resume_index
+ 1,
643 bm
.shuffle
? (char*) str(LANG_BOOKMARK_SHUFFLE
) : "");
648 static int bookmark_list_voice_cb(int list_index
, void* data
)
650 struct bookmark_list
* bookmarks
= (struct bookmark_list
*) data
;
651 int index
= list_index
/ 2;
653 if (bookmarks
->show_dont_resume
)
656 return talk_id(LANG_BOOKMARK_DONT_RESUME
, false);
659 say_bookmark(bookmarks
->items
[index
- bookmarks
->start
], index
,
660 bookmarks
->show_playlist_name
);
664 /* ----------------------------------------------------------------------- */
665 /* This displays a the bookmarks in a file and allows the user to */
666 /* select one to play. */
667 /* ------------------------------------------------------------------------*/
668 static char* select_bookmark(const char* bookmark_file_name
, bool show_dont_resume
)
670 struct bookmark_list
* bookmarks
;
671 struct gui_synclist list
;
678 bookmarks
= plugin_get_buffer(&size
);
679 bookmarks
->buffer_size
= size
;
680 bookmarks
->show_dont_resume
= show_dont_resume
;
681 bookmarks
->filename
= bookmark_file_name
;
682 bookmarks
->start
= 0;
683 bookmarks
->show_playlist_name
684 = strcmp(bookmark_file_name
, RECENT_BOOKMARK_FILE
) == 0;
685 gui_synclist_init(&list
, &get_bookmark_info
, (void*) bookmarks
, false, 2, NULL
);
686 if(global_settings
.talk_menu
)
687 gui_synclist_set_voice_callback(&list
, bookmark_list_voice_cb
);
688 gui_synclist_set_title(&list
, str(LANG_BOOKMARK_SELECT_BOOKMARK
),
696 int count
= get_bookmark_count(bookmark_file_name
);
697 bookmarks
->total_count
= count
;
699 if (bookmarks
->total_count
< 1)
701 /* No more bookmarks, delete file and exit */
702 splash(HZ
, ID2P(LANG_BOOKMARK_LOAD_EMPTY
));
703 remove(bookmark_file_name
);
707 if (bookmarks
->show_dont_resume
)
713 gui_synclist_set_nb_items(&list
, count
* 2);
717 /* Selected item has been deleted */
719 gui_synclist_select_item(&list
, item
* 2);
722 buffer_bookmarks(bookmarks
, bookmarks
->start
);
723 gui_synclist_draw(&list
);
724 cond_talk_ids_fq(VOICE_EXT_BMARK
);
725 gui_synclist_speak_item(&list
);
729 list_do_action(CONTEXT_BOOKMARKSCREEN
, HZ
/ 2,
730 &list
, &action
, LIST_WRAP_UNLESS_HELD
);
731 item
= gui_synclist_get_sel_pos(&list
) / 2;
733 if (bookmarks
->show_dont_resume
)
738 if (action
== ACTION_STD_CONTEXT
)
740 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_BOOKMARK_CONTEXT_MENU
),
741 NULL
, ID2P(LANG_BOOKMARK_CONTEXT_RESUME
),
742 ID2P(LANG_BOOKMARK_CONTEXT_DELETE
));
743 static const int menu_actions
[] =
745 ACTION_STD_OK
, ACTION_BMS_DELETE
747 int selection
= do_menu(&menu_items
, NULL
, NULL
, false);
751 if (selection
>= 0 && selection
<=
752 (int) (sizeof(menu_actions
) / sizeof(menu_actions
[0])))
754 action
= menu_actions
[selection
];
764 return bookmarks
->items
[item
- bookmarks
->start
];
767 /* Else fall through */
769 case ACTION_TREE_WPS
:
770 case ACTION_STD_CANCEL
:
774 case ACTION_BMS_DELETE
:
777 const char *lines
[]={
778 ID2P(LANG_REALLY_DELETE
)
780 const char *yes_lines
[]={
784 const struct text_message message
={lines
, 1};
785 const struct text_message yes_message
={yes_lines
, 1};
787 if(gui_syncyesno_run(&message
, &yes_message
, NULL
)==YESNO_YES
)
789 delete_bookmark(bookmark_file_name
, item
);
790 bookmarks
->reload
= true;
797 if (default_event_handler(action
) == SYS_USB_CONNECTED
)
810 /* ----------------------------------------------------------------------- */
811 /* This function takes a location in a bookmark file and deletes that */
813 /* ------------------------------------------------------------------------*/
814 static bool delete_bookmark(const char* bookmark_file_name
, int bookmark_id
)
816 int temp_bookmark_file
= 0;
817 int bookmark_file
= 0;
818 int bookmark_count
= 0;
820 /* Opening up a temp bookmark file */
821 snprintf(global_temp_buffer
, sizeof(global_temp_buffer
),
822 "%s.tmp", bookmark_file_name
);
823 temp_bookmark_file
= open(global_temp_buffer
,
824 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
826 if (temp_bookmark_file
< 0)
827 return false; /* can't open the temp file */
829 /* Reading in the previous bookmarks and writing them to the temp file */
830 bookmark_file
= open(bookmark_file_name
, O_RDONLY
);
831 if (bookmark_file
>= 0)
833 while (read_line(bookmark_file
, global_read_buffer
,
834 sizeof(global_read_buffer
)) > 0)
836 if (bookmark_id
!= bookmark_count
)
838 write(temp_bookmark_file
, global_read_buffer
,
839 strlen(global_read_buffer
));
840 write(temp_bookmark_file
, "\n", 1);
844 close(bookmark_file
);
846 close(temp_bookmark_file
);
848 remove(bookmark_file_name
);
849 rename(global_temp_buffer
, bookmark_file_name
);
854 /* ----------------------------------------------------------------------- */
855 /* This function parses a bookmark, says the voice UI part of it. */
856 /* ------------------------------------------------------------------------*/
857 static void say_bookmark(const char* bookmark
,
858 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 #if CONFIG_CODEC == SWCODEC
869 bool is_dir
= (global_temp_buffer
[0]
870 && global_temp_buffer
[strlen(global_temp_buffer
)-1] == '/');
872 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
873 together, because there is no guarantee that the same mp3
874 parameters are used. */
875 if(show_playlist_name
)
876 { /* It's useful to know which playlist this is */
878 talk_dir_or_spell(global_temp_buffer
,
879 TALK_IDARRAY(VOICE_DIR
), true);
880 else talk_file_or_spell(NULL
, global_temp_buffer
,
881 TALK_IDARRAY(LANG_PLAYLIST
), true);
884 (void)show_playlist_name
;
888 talk_id(LANG_SHUFFLE
, true);
890 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT
, true);
891 talk_number(bm
.resume_index
+ 1, true);
892 talk_id(LANG_TIME
, true);
893 talk_value(bm
.resume_time
/ 1000, UNIT_TIME
, true);
895 #if CONFIG_CODEC == SWCODEC
898 talk_file_or_spell(global_temp_buffer
, global_filename
,
899 TALK_IDARRAY(VOICE_FILE
), true);
901 { /* Unfortunately if this is a playlist, we do not know in which
902 directory the file is and therefore cannot find the track's
904 talk_id(VOICE_FILE
, true);
905 talk_spell(global_filename
, true);
910 /* ----------------------------------------------------------------------- */
911 /* This function parses a bookmark and then plays it. */
912 /* ------------------------------------------------------------------------*/
913 static bool play_bookmark(const char* bookmark
)
915 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
916 /* preset pitch and speed to 100% in case bookmark doesn't have info */
917 bm
.pitch
= sound_get_pitch();
918 bm
.speed
= dsp_get_timestretch();
921 if (parse_bookmark(bookmark
, true))
923 global_settings
.repeat_mode
= bm
.repeat_mode
;
924 global_settings
.playlist_shuffle
= bm
.shuffle
;
925 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
926 sound_set_pitch(bm
.pitch
);
927 dsp_set_timestretch(bm
.speed
);
929 if (!warn_on_pl_erase())
931 return bookmark_play(global_temp_buffer
, bm
.resume_index
,
932 bm
.resume_offset
, bm
.resume_seed
, global_filename
);
938 static const char* skip_token(const char* s
)
940 while (*s
&& *s
!= ';')
953 static const char* int_token(const char* s
, int* dest
)
956 return skip_token(s
);
959 static const char* long_token(const char* s
, long* dest
)
961 *dest
= atoi(s
); /* Should be atol, but we don't have it. */
962 return skip_token(s
);
965 /* ----------------------------------------------------------------------- */
966 /* This function takes a bookmark and parses it. This function also */
967 /* validates the bookmark. The parse_filenames flag indicates whether */
968 /* the filename tokens are to be extracted. */
969 /* ----------------------------------------------------------------------- */
970 static bool parse_bookmark(const char *bookmark
, const bool parse_filenames
)
972 const char* s
= bookmark
;
975 #define GET_INT_TOKEN(var) s = int_token(s, &var)
976 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
977 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
979 /* if new format bookmark, extract the optional content flags,
980 otherwise treat as an original format bookmark */
982 bool new_format
= (strchr(s
, '>') == s
);
986 GET_INT_TOKEN(opt_flags
);
989 /* extract all original bookmark tokens */
990 GET_INT_TOKEN(bm
.resume_index
);
991 GET_LONG_TOKEN(bm
.resume_offset
);
992 GET_INT_TOKEN(bm
.resume_seed
);
993 if (!new_format
) /* skip deprecated token */
995 GET_LONG_TOKEN(bm
.resume_time
);
996 GET_INT_TOKEN(bm
.repeat_mode
);
997 GET_BOOL_TOKEN(bm
.shuffle
);
999 /* extract all optional bookmark tokens */
1000 if (opt_flags
& BM_PITCH
)
1001 GET_INT_TOKEN(bm
.pitch
);
1002 if (opt_flags
& BM_SPEED
)
1003 GET_INT_TOKEN(bm
.speed
);
1010 end
= strchr(s
, ';');
1012 /* extract file names */
1013 if (parse_filenames
)
1015 size_t len
= (end
== NULL
) ? strlen(s
) : (size_t) (end
- s
);
1016 len
= MIN(TEMP_BUF_SIZE
- 1, len
);
1017 strlcpy(global_temp_buffer
, s
, len
+ 1);
1022 strlcpy(global_filename
, end
, MAX_PATH
);
1029 /* ----------------------------------------------------------------------- */
1030 /* This function is used by multiple functions and is used to generate a */
1031 /* bookmark named based off of the input. */
1032 /* Changing this function could result in how the bookmarks are stored. */
1033 /* it would be here that the centralized/decentralized bookmark code */
1034 /* could be placed. */
1035 /* ----------------------------------------------------------------------- */
1036 static bool generate_bookmark_file_name(const char *in
)
1038 int len
= strlen(in
);
1040 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1041 /* otherwise, name it based on the in variable */
1042 if (!strcmp("/", in
))
1043 strcpy(global_bookmark_file_name
, "/root_dir.bmark");
1046 #ifdef HAVE_MULTIVOLUME
1047 /* The "root" of an extra volume need special handling too. */
1048 bool volume_root
= (strip_volume(in
, global_bookmark_file_name
) &&
1049 !strcmp("/", global_bookmark_file_name
));
1052 strcpy(global_bookmark_file_name
, in
);
1053 if(global_bookmark_file_name
[len
-1] == '/')
1055 #ifdef HAVE_MULTIVOLUME
1057 strcpy(&global_bookmark_file_name
[len
], "/volume_dir.bmark");
1060 strcpy(&global_bookmark_file_name
[len
], ".bmark");
1066 /* ----------------------------------------------------------------------- */
1067 /* Returns true if a bookmark file exists for the current playlist */
1068 /* ----------------------------------------------------------------------- */
1069 bool bookmark_exists(void)
1073 if(is_bookmarkable_state())
1075 char* name
= playlist_get_name(NULL
, global_temp_buffer
,
1076 sizeof(global_temp_buffer
));
1077 if (generate_bookmark_file_name(name
))
1079 exist
= file_exists(global_bookmark_file_name
);
1086 /* ----------------------------------------------------------------------- */
1087 /* Checks the current state of the system and returns true if it is in a */
1088 /* bookmarkable state. */
1089 /* ----------------------------------------------------------------------- */
1090 static bool is_bookmarkable_state(void)
1092 int resume_index
= 0;
1094 if (!(audio_status() && audio_current_track()) ||
1095 /* no track playing */
1096 (playlist_get_resume_info(&resume_index
) == -1) ||
1097 /* invalid queue info */
1098 (playlist_modified(NULL
)))
1099 /* can't bookmark while in the queue */