Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / apps / bookmark.c
blob85570839be0259f56acdd336c73c67842d9bf858
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "action.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "system.h"
34 #include "icons.h"
35 #include "menu.h"
36 #include "lang.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "splash.h"
40 #include "yesno.h"
41 #include "list.h"
42 #include "plugin.h"
43 #include "file.h"
44 #include "filefuncs.h"
46 #define MAX_BOOKMARKS 10
47 #define MAX_BOOKMARK_SIZE 350
48 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
50 /* Used to buffer bookmarks while displaying the bookmark list. */
51 struct bookmark_list
53 const char* filename;
54 size_t buffer_size;
55 int start;
56 int count;
57 int total_count;
58 bool show_dont_resume;
59 bool reload;
60 bool show_playlist_name;
61 char* items[];
64 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
65 bool most_recent);
66 static bool check_bookmark(const char* bookmark);
67 static char* create_bookmark(void);
68 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
69 static void say_bookmark(const char* bookmark,
70 int bookmark_id, bool show_playlist_name);
71 static bool play_bookmark(const char* bookmark);
72 static bool generate_bookmark_file_name(const char *in);
73 static const char* skip_token(const char* s);
74 static const char* int_token(const char* s, int* dest);
75 static const char* long_token(const char* s, long* dest);
76 static const char* bool_token(const char* s, bool* dest);
77 static bool parse_bookmark(const char *bookmark,
78 int *resume_index,
79 int *resume_offset,
80 int *resume_seed,
81 int *resume_first_index,
82 char* resume_file,
83 unsigned int resume_file_size,
84 long* ms,
85 int * repeat_mode,
86 bool *shuffle,
87 char* file_name);
88 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
89 static const char* get_bookmark_info(int list_index,
90 void* data,
91 char *buffer,
92 size_t buffer_len);
93 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
94 static bool system_check(void);
95 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
96 static int get_bookmark_count(const char* bookmark_file_name);
98 static char global_temp_buffer[MAX_PATH+1];
99 /* File name created by generate_bookmark_file_name */
100 static char global_bookmark_file_name[MAX_PATH];
101 static char global_read_buffer[MAX_BOOKMARK_SIZE];
102 /* Bookmark created by create_bookmark*/
103 static char global_bookmark[MAX_BOOKMARK_SIZE];
104 /* Filename from parsed bookmark (can be made local where needed) */
105 static char global_filename[MAX_PATH];
107 /* ----------------------------------------------------------------------- */
108 /* This is the interface function from the main menu. */
109 /* ----------------------------------------------------------------------- */
110 bool bookmark_create_menu(void)
112 write_bookmark(true, create_bookmark());
113 return false;
116 /* ----------------------------------------------------------------------- */
117 /* This function acts as the load interface from the main menu */
118 /* This function determines the bookmark file name and then loads that file*/
119 /* for the user. The user can then select a bookmark to load. */
120 /* If no file/directory is currently playing, the menu item does not work. */
121 /* ----------------------------------------------------------------------- */
122 bool bookmark_load_menu(void)
124 if (system_check())
126 char* name = playlist_get_name(NULL, global_temp_buffer,
127 sizeof(global_temp_buffer));
128 if (generate_bookmark_file_name(name))
130 char* bookmark = select_bookmark(global_bookmark_file_name, false);
132 if (bookmark != NULL)
134 return play_bookmark(bookmark);
139 return false;
142 /* ----------------------------------------------------------------------- */
143 /* Gives the user a list of the Most Recent Bookmarks. This is an */
144 /* interface function */
145 /* ----------------------------------------------------------------------- */
146 bool bookmark_mrb_load()
148 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
150 if (bookmark != NULL)
152 return play_bookmark(bookmark);
155 return false;
158 /* ----------------------------------------------------------------------- */
159 /* This function handles an autobookmark creation. This is an interface */
160 /* function. */
161 /* ----------------------------------------------------------------------- */
162 bool bookmark_autobookmark(void)
164 char* bookmark;
165 if (!system_check())
166 return false;
168 audio_pause(); /* first pause playback */
169 bookmark = create_bookmark();
170 /* Workaround for inability to speak when paused: all callers will
171 just do audio_stop() when we return, so we can do it right
172 away. This makes it possible to speak the "Create a Bookmark?"
173 prompt and the "Bookmark Created" splash. */
174 audio_stop();
175 switch (global_settings.autocreatebookmark)
177 case BOOKMARK_YES:
178 return write_bookmark(true, bookmark);
180 case BOOKMARK_NO:
181 return false;
183 case BOOKMARK_RECENT_ONLY_YES:
184 return write_bookmark(false, bookmark);
186 #ifdef HAVE_LCD_BITMAP
187 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
188 const struct text_message message={lines, 1};
189 #else
190 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
191 str(LANG_CONFIRM_WITH_BUTTON)};
192 const struct text_message message={lines, 2};
193 #endif
195 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
197 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
198 return write_bookmark(false, bookmark);
199 else
200 return write_bookmark(true, bookmark);
202 return false;
205 /* ----------------------------------------------------------------------- */
206 /* This function takes the current current resume information and writes */
207 /* that to the beginning of the bookmark file. */
208 /* This file will contain N number of bookmarks in the following format: */
209 /* resume_index*resume_offset*resume_seed*resume_first_index* */
210 /* resume_file*milliseconds*MP3 Title* */
211 /* ------------------------------------------------------------------------*/
212 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
214 bool success=false;
215 if (!bookmark)
216 return false; /* something didn't happen correctly, do nothing */
218 if (global_settings.usemrb)
219 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
222 /* writing the bookmark */
223 if (create_bookmark_file)
225 char* name = playlist_get_name(NULL, global_temp_buffer,
226 sizeof(global_temp_buffer));
227 if (generate_bookmark_file_name(name))
229 success = add_bookmark(global_bookmark_file_name, bookmark, false);
233 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
234 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
236 return true;
239 /* ----------------------------------------------------------------------- */
240 /* This function adds a bookmark to a file. */
241 /* ------------------------------------------------------------------------*/
242 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
243 bool most_recent)
245 int temp_bookmark_file = 0;
246 int bookmark_file = 0;
247 int bookmark_count = 0;
248 char* playlist = NULL;
249 char* cp;
250 char* tmp;
251 int len = 0;
252 bool unique = false;
254 /* Opening up a temp bookmark file */
255 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
256 "%s.tmp", bookmark_file_name);
257 temp_bookmark_file = open(global_temp_buffer,
258 O_WRONLY | O_CREAT | O_TRUNC);
259 if (temp_bookmark_file < 0)
260 return false; /* can't open the temp file */
262 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
264 playlist = strchr(bookmark,'/');
265 cp = strrchr(bookmark,';');
266 len = cp - playlist;
267 unique = true;
270 /* Writing the new bookmark to the begining of the temp file */
271 write(temp_bookmark_file, bookmark, strlen(bookmark));
272 write(temp_bookmark_file, "\n", 1);
273 bookmark_count++;
275 /* Reading in the previous bookmarks and writing them to the temp file */
276 bookmark_file = open(bookmark_file_name, O_RDONLY);
277 if (bookmark_file >= 0)
279 while (read_line(bookmark_file, global_read_buffer,
280 sizeof(global_read_buffer)) > 0)
282 /* The MRB has a max of MAX_BOOKMARKS in it */
283 /* This keeps it from getting too large */
284 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
285 break;
287 cp = strchr(global_read_buffer,'/');
288 tmp = strrchr(global_read_buffer,';');
289 if (check_bookmark(global_read_buffer) &&
290 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
292 bookmark_count++;
293 write(temp_bookmark_file, global_read_buffer,
294 strlen(global_read_buffer));
295 write(temp_bookmark_file, "\n", 1);
298 close(bookmark_file);
300 close(temp_bookmark_file);
302 remove(bookmark_file_name);
303 rename(global_temp_buffer, bookmark_file_name);
305 return true;
309 /* ----------------------------------------------------------------------- */
310 /* This function takes the system resume data and formats it into a valid */
311 /* bookmark. */
312 /* ----------------------------------------------------------------------- */
313 static char* create_bookmark()
315 int resume_index = 0;
316 char *file;
318 if (!system_check())
319 return NULL; /* something didn't happen correctly, do nothing */
321 /* grab the currently playing track */
322 struct mp3entry *id3 = audio_current_track();
323 if(!id3)
324 return NULL;
326 /* Get some basic resume information */
327 /* queue_resume and queue_resume_index are not used and can be ignored.*/
328 playlist_get_resume_info(&resume_index);
330 /* Get the currently playing file minus the path */
331 /* This is used when displaying the available bookmarks */
332 file = strrchr(id3->path,'/');
333 if(NULL == file)
334 return NULL;
336 /* create the bookmark */
337 snprintf(global_bookmark, sizeof(global_bookmark),
338 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
339 resume_index,
340 id3->offset,
341 playlist_get_seed(NULL),
343 id3->elapsed,
344 global_settings.repeat_mode,
345 global_settings.playlist_shuffle,
346 playlist_get_name(NULL, global_temp_buffer,
347 sizeof(global_temp_buffer)),
348 file+1);
350 /* checking to see if the bookmark is valid */
351 if (check_bookmark(global_bookmark))
352 return global_bookmark;
353 else
354 return NULL;
357 static bool check_bookmark(const char* bookmark)
359 return parse_bookmark(bookmark,
360 NULL,NULL,NULL, NULL,
361 NULL,0,NULL,NULL,
362 NULL, NULL);
365 /* ----------------------------------------------------------------------- */
366 /* This function will determine if an autoload is necessary. This is an */
367 /* interface function. */
368 /* ------------------------------------------------------------------------*/
369 bool bookmark_autoload(const char* file)
371 if(global_settings.autoloadbookmark == BOOKMARK_NO)
372 return false;
374 /*Checking to see if a bookmark file exists.*/
375 if(!generate_bookmark_file_name(file))
377 return false;
380 if(!file_exists(global_bookmark_file_name))
381 return false;
383 if(global_settings.autoloadbookmark == BOOKMARK_YES)
385 return bookmark_load(global_bookmark_file_name, true);
387 else
389 char* bookmark = select_bookmark(global_bookmark_file_name, true);
391 if (bookmark != NULL)
393 if (!play_bookmark(bookmark))
395 /* Selected bookmark not found. */
396 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
399 /* Act as if autoload was done even if it failed, since the
400 * user did make an active selection.
402 return true;
405 return false;
409 /* ----------------------------------------------------------------------- */
410 /* This function loads the bookmark information into the resume memory. */
411 /* This is an interface function. */
412 /* ------------------------------------------------------------------------*/
413 bool bookmark_load(const char* file, bool autoload)
415 int fd;
416 char* bookmark = NULL;
418 if(autoload)
420 fd = open(file, O_RDONLY);
421 if(fd >= 0)
423 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
424 bookmark=global_read_buffer;
425 close(fd);
428 else
430 /* This is not an auto-load, so list the bookmarks */
431 bookmark = select_bookmark(file, false);
434 if (bookmark != NULL)
436 if (!play_bookmark(bookmark))
438 /* Selected bookmark not found. */
439 if (!autoload)
441 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
444 return false;
448 return true;
452 static int get_bookmark_count(const char* bookmark_file_name)
454 int read_count = 0;
455 int file = open(bookmark_file_name, O_RDONLY);
457 if(file < 0)
458 return -1;
460 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
462 read_count++;
465 close(file);
466 return read_count;
469 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
471 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
472 int read_count = 0;
473 int file = open(bookmarks->filename, O_RDONLY);
475 if (file < 0)
477 return -1;
480 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
481 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
483 /* Entire file fits in buffer */
484 first_line = 0;
487 bookmarks->start = first_line;
488 bookmarks->count = 0;
489 bookmarks->reload = false;
491 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
493 read_count++;
495 if (read_count >= first_line)
497 dest -= strlen(global_read_buffer) + 1;
499 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
500 + (sizeof(char*) * (bookmarks->count + 1)))
502 break;
505 strcpy(dest, global_read_buffer);
506 bookmarks->items[bookmarks->count] = dest;
507 bookmarks->count++;
511 close(file);
512 return bookmarks->start + bookmarks->count;
515 static const char* get_bookmark_info(int list_index,
516 void* data,
517 char *buffer,
518 size_t buffer_len)
520 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
521 int index = list_index / 2;
522 int resume_index = 0;
523 long resume_time = 0;
524 bool shuffle = false;
526 if (bookmarks->show_dont_resume)
528 if (index == 0)
530 return list_index % 2 == 0
531 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
534 index--;
537 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
538 || (index < bookmarks->start))
540 int read_index = index;
542 /* Using count as a guide on how far to move could possibly fail
543 * sometimes. Use byte count if that is a problem?
546 if (read_index != 0)
548 /* Move count * 3 / 4 items in the direction the user is moving,
549 * but don't go too close to the end.
551 int offset = bookmarks->count;
552 int max = bookmarks->total_count - (bookmarks->count / 2);
554 if (read_index < bookmarks->start)
556 offset *= 3;
559 read_index = index - offset / 4;
561 if (read_index > max)
563 read_index = max;
566 if (read_index < 0)
568 read_index = 0;
572 if (buffer_bookmarks(bookmarks, read_index) <= index)
574 return "";
578 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
579 &resume_index, NULL, NULL, NULL, global_temp_buffer,
580 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
581 global_filename))
583 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
586 if (list_index % 2 == 0)
588 char *name;
589 char *format;
590 int len = strlen(global_temp_buffer);
592 if (bookmarks->show_playlist_name && len > 0)
594 name = global_temp_buffer;
595 len--;
597 if (name[len] != '/')
599 strrsplt(name, '.');
601 else if (len > 1)
603 name[len] = '\0';
606 if (len > 1)
608 name = strrsplt(name, '/');
611 format = "%s : %s";
613 else
615 name = global_filename;
616 format = "%s";
619 strrsplt(global_filename, '.');
620 snprintf(buffer, buffer_len, format, name, global_filename);
621 return buffer;
623 else
625 char time_buf[32];
627 format_time(time_buf, sizeof(time_buf), resume_time);
628 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
629 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
630 return buffer;
634 static int bookmark_list_voice_cb(int list_index, void* data)
636 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
637 int index = list_index / 2;
639 if (bookmarks->show_dont_resume)
641 if (index == 0)
642 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
643 index--;
645 say_bookmark(bookmarks->items[index - bookmarks->start], index,
646 bookmarks->show_playlist_name);
647 return 0;
650 /* ----------------------------------------------------------------------- */
651 /* This displays a the bookmarks in a file and allows the user to */
652 /* select one to play. */
653 /* ------------------------------------------------------------------------*/
654 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
656 struct bookmark_list* bookmarks;
657 struct gui_synclist list;
658 int item = 0;
659 int action;
660 size_t size;
661 bool exit = false;
662 bool refresh = true;
664 bookmarks = plugin_get_buffer(&size);
665 bookmarks->buffer_size = size;
666 bookmarks->show_dont_resume = show_dont_resume;
667 bookmarks->filename = bookmark_file_name;
668 bookmarks->start = 0;
669 bookmarks->show_playlist_name
670 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
671 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
672 if(global_settings.talk_menu)
673 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
674 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
675 Icon_Bookmark);
677 while (!exit)
680 if (refresh)
682 int count = get_bookmark_count(bookmark_file_name);
683 bookmarks->total_count = count;
685 if (bookmarks->total_count < 1)
687 /* No more bookmarks, delete file and exit */
688 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
689 remove(bookmark_file_name);
690 return NULL;
693 if (bookmarks->show_dont_resume)
695 count++;
696 item++;
699 gui_synclist_set_nb_items(&list, count * 2);
701 if (item >= count)
703 /* Selected item has been deleted */
704 item = count - 1;
705 gui_synclist_select_item(&list, item * 2);
708 buffer_bookmarks(bookmarks, bookmarks->start);
709 gui_synclist_draw(&list);
710 cond_talk_ids_fq(VOICE_EXT_BMARK);
711 gui_synclist_speak_item(&list);
712 refresh = false;
715 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
716 &list, &action, LIST_WRAP_UNLESS_HELD);
717 item = gui_synclist_get_sel_pos(&list) / 2;
719 if (bookmarks->show_dont_resume)
721 item--;
724 if (action == ACTION_STD_CONTEXT)
726 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
727 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
728 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
729 static const int menu_actions[] =
731 ACTION_STD_OK, ACTION_BMS_DELETE
733 int selection = do_menu(&menu_items, NULL, NULL, false);
735 refresh = true;
737 if (selection >= 0 && selection <=
738 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
740 action = menu_actions[selection];
744 switch (action)
746 case ACTION_STD_OK:
747 if (item >= 0)
749 talk_shutup();
750 return bookmarks->items[item - bookmarks->start];
753 /* Else fall through */
755 case ACTION_TREE_WPS:
756 case ACTION_STD_CANCEL:
757 exit = true;
758 break;
760 case ACTION_BMS_DELETE:
761 if (item >= 0)
763 const char *lines[]={
764 ID2P(LANG_REALLY_DELETE)
766 const char *yes_lines[]={
767 ID2P(LANG_DELETING)
770 const struct text_message message={lines, 1};
771 const struct text_message yes_message={yes_lines, 1};
773 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
775 splash(0, str(LANG_DELETING));
776 delete_bookmark(bookmark_file_name, item);
777 bookmarks->reload = true;
779 refresh = true;
781 break;
783 default:
784 if (default_event_handler(action) == SYS_USB_CONNECTED)
786 exit = true;
789 break;
793 talk_shutup();
794 return NULL;
797 /* ----------------------------------------------------------------------- */
798 /* This function takes a location in a bookmark file and deletes that */
799 /* bookmark. */
800 /* ------------------------------------------------------------------------*/
801 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
803 int temp_bookmark_file = 0;
804 int bookmark_file = 0;
805 int bookmark_count = 0;
807 /* Opening up a temp bookmark file */
808 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
809 "%s.tmp", bookmark_file_name);
810 temp_bookmark_file = open(global_temp_buffer,
811 O_WRONLY | O_CREAT | O_TRUNC);
813 if (temp_bookmark_file < 0)
814 return false; /* can't open the temp file */
816 /* Reading in the previous bookmarks and writing them to the temp file */
817 bookmark_file = open(bookmark_file_name, O_RDONLY);
818 if (bookmark_file >= 0)
820 while (read_line(bookmark_file, global_read_buffer,
821 sizeof(global_read_buffer)) > 0)
823 if (bookmark_id != bookmark_count)
825 write(temp_bookmark_file, global_read_buffer,
826 strlen(global_read_buffer));
827 write(temp_bookmark_file, "\n", 1);
829 bookmark_count++;
831 close(bookmark_file);
833 close(temp_bookmark_file);
835 remove(bookmark_file_name);
836 rename(global_temp_buffer, bookmark_file_name);
838 return true;
841 /* ----------------------------------------------------------------------- */
842 /* This function parses a bookmark, says the voice UI part of it. */
843 /* ------------------------------------------------------------------------*/
844 static void say_bookmark(const char* bookmark,
845 int bookmark_id, bool show_playlist_name)
847 int resume_index;
848 long ms;
849 bool playlist_shuffle = false;
850 bool is_dir;
852 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
853 global_temp_buffer,sizeof(global_temp_buffer),
854 &ms, NULL, &playlist_shuffle,
855 global_filename))
857 talk_id(LANG_BOOKMARK_INVALID, false);
858 return;
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 */
871 if(is_dir)
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);
877 #else
878 (void)show_playlist_name;
879 #endif
881 if(playlist_shuffle)
882 talk_id(LANG_SHUFFLE, true);
884 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
885 talk_number(resume_index + 1, true);
886 talk_id(LANG_TIME, true);
887 talk_value(ms / 1000, UNIT_TIME, true);
889 #if CONFIG_CODEC == SWCODEC
890 /* Track filename */
891 if(is_dir)
892 talk_file_or_spell(global_temp_buffer, global_filename,
893 TALK_IDARRAY(VOICE_FILE), true);
894 else
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
897 .talk file. */
898 talk_id(VOICE_FILE, true);
899 talk_spell(global_filename, true);
901 #endif
904 /* ----------------------------------------------------------------------- */
905 /* This function parses a bookmark and then plays it. */
906 /* ------------------------------------------------------------------------*/
907 static bool play_bookmark(const char* bookmark)
909 int index;
910 int offset;
911 int seed;
913 if (parse_bookmark(bookmark,
914 &index,
915 &offset,
916 &seed,
917 NULL,
918 global_temp_buffer,
919 sizeof(global_temp_buffer),
920 NULL,
921 &global_settings.repeat_mode,
922 &global_settings.playlist_shuffle,
923 global_filename))
925 return bookmark_play(global_temp_buffer, index, offset, seed,
926 global_filename);
929 return false;
932 static const char* skip_token(const char* s)
934 while (*s && *s != ';')
936 s++;
939 if (*s)
941 s++;
944 return s;
947 static const char* int_token(const char* s, int* dest)
949 if (dest != NULL)
951 *dest = atoi(s);
954 return skip_token(s);
957 static const char* long_token(const char* s, long* dest)
959 if (dest != NULL)
961 *dest = atoi(s); /* Should be atol, but we don't have it. */
964 return skip_token(s);
967 static const char* bool_token(const char* s, bool* dest)
969 if (dest != NULL)
971 *dest = atoi(s) != 0;
974 return skip_token(s);
977 /* ----------------------------------------------------------------------- */
978 /* This function takes a bookmark and parses it. This function also */
979 /* validates the bookmark. Passing in NULL for an output variable */
980 /* indicates that value is not requested. */
981 /* ----------------------------------------------------------------------- */
982 static bool parse_bookmark(const char *bookmark,
983 int *resume_index,
984 int *resume_offset,
985 int *resume_seed,
986 int *resume_first_index,
987 char* resume_file,
988 unsigned int resume_file_size,
989 long* ms,
990 int * repeat_mode, bool *shuffle,
991 char* file_name)
993 const char* s = bookmark;
994 const char* end;
996 s = int_token(s, resume_index);
997 s = int_token(s, resume_offset);
998 s = int_token(s, resume_seed);
999 s = int_token(s, resume_first_index);
1000 s = long_token(s, ms);
1001 s = int_token(s, repeat_mode);
1002 s = bool_token(s, shuffle);
1004 if (*s == 0)
1006 return false;
1009 end = strchr(s, ';');
1011 if (resume_file != NULL)
1013 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1014 len = MIN(resume_file_size - 1, len);
1015 strlcpy(resume_file, s, len + 1);
1018 if (end != NULL && file_name != NULL)
1020 end++;
1021 strlcpy(file_name, end, MAX_PATH);
1024 return true;
1027 /* ----------------------------------------------------------------------- */
1028 /* This function is used by multiple functions and is used to generate a */
1029 /* bookmark named based off of the input. */
1030 /* Changing this function could result in how the bookmarks are stored. */
1031 /* it would be here that the centralized/decentralized bookmark code */
1032 /* could be placed. */
1033 /* ----------------------------------------------------------------------- */
1034 static bool generate_bookmark_file_name(const char *in)
1036 int len = strlen(in);
1038 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1039 /* otherwise, name it based on the in variable */
1040 if (!strcmp("/", in))
1041 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1042 else
1044 #ifdef HAVE_MULTIVOLUME
1045 /* The "root" of an extra volume need special handling too. */
1046 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1047 !strcmp("/", global_bookmark_file_name));
1048 #endif
1050 strcpy(global_bookmark_file_name, in);
1051 if(global_bookmark_file_name[len-1] == '/')
1052 len--;
1053 #ifdef HAVE_MULTIVOLUME
1054 if (volume_root)
1055 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1056 else
1057 #endif
1058 strcpy(&global_bookmark_file_name[len], ".bmark");
1061 return true;
1064 /* ----------------------------------------------------------------------- */
1065 /* Returns true if a bookmark file exists for the current playlist */
1066 /* ----------------------------------------------------------------------- */
1067 bool bookmark_exist(void)
1069 bool exist=false;
1071 if(system_check())
1073 char* name = playlist_get_name(NULL, global_temp_buffer,
1074 sizeof(global_temp_buffer));
1075 if (generate_bookmark_file_name(name))
1077 exist = file_exists(global_bookmark_file_name);
1081 return exist;
1084 /* ----------------------------------------------------------------------- */
1085 /* Checks the current state of the system and returns if it is in a */
1086 /* bookmarkable state. */
1087 /* ----------------------------------------------------------------------- */
1088 /* Inputs: */
1089 /* ----------------------------------------------------------------------- */
1090 /* Outputs: */
1091 /* return bool: Indicates if the system was in a bookmarkable state */
1092 /* ----------------------------------------------------------------------- */
1093 static bool system_check(void)
1095 int resume_index = 0;
1097 if (!(audio_status() && audio_current_track()))
1099 /* no track playing */
1100 return false;
1103 /* Checking to see if playing a queued track */
1104 if (playlist_get_resume_info(&resume_index) == -1)
1106 /* something bad happened while getting the queue information */
1107 return false;
1109 else if (playlist_modified(NULL))
1111 /* can't bookmark while in the queue */
1112 return false;
1115 return true;