libpcm: add missing #include and set svn:keywords
[kugel-rb.git] / apps / bookmark.c
blob78bbb77c86cba0a75fc1fcabd718622c8c78b198
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 "backdrop.h"
44 #include "file.h"
45 #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. */
52 struct bookmark_list
54 const char* filename;
55 size_t buffer_size;
56 int start;
57 int count;
58 int total_count;
59 bool show_dont_resume;
60 bool reload;
61 bool show_playlist_name;
62 char* items[];
65 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
66 bool most_recent);
67 static bool check_bookmark(const char* bookmark);
68 static char* create_bookmark(void);
69 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
70 static void say_bookmark(const char* bookmark,
71 int bookmark_id, bool show_playlist_name);
72 static bool play_bookmark(const char* bookmark);
73 static bool generate_bookmark_file_name(const char *in);
74 static const char* skip_token(const char* s);
75 static const char* int_token(const char* s, int* dest);
76 static const char* long_token(const char* s, long* dest);
77 static const char* bool_token(const char* s, bool* dest);
78 static bool parse_bookmark(const char *bookmark,
79 int *resume_index,
80 int *resume_offset,
81 int *resume_seed,
82 int *resume_first_index,
83 char* resume_file,
84 unsigned int resume_file_size,
85 long* ms,
86 int * repeat_mode,
87 bool *shuffle,
88 char* file_name);
89 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
90 static const char* get_bookmark_info(int list_index,
91 void* data,
92 char *buffer,
93 size_t buffer_len);
94 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
95 static bool system_check(void);
96 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
97 static int get_bookmark_count(const char* bookmark_file_name);
99 static char global_temp_buffer[MAX_PATH+1];
100 /* File name created by generate_bookmark_file_name */
101 static char global_bookmark_file_name[MAX_PATH];
102 static char global_read_buffer[MAX_BOOKMARK_SIZE];
103 /* Bookmark created by create_bookmark*/
104 static char global_bookmark[MAX_BOOKMARK_SIZE];
105 /* Filename from parsed bookmark (can be made local where needed) */
106 static char global_filename[MAX_PATH];
108 /* ----------------------------------------------------------------------- */
109 /* This is the interface function from the main menu. */
110 /* ----------------------------------------------------------------------- */
111 bool bookmark_create_menu(void)
113 write_bookmark(true, create_bookmark());
114 return false;
117 /* ----------------------------------------------------------------------- */
118 /* This function acts as the load interface from the main menu */
119 /* This function determines the bookmark file name and then loads that file*/
120 /* for the user. The user can then select a bookmark to load. */
121 /* If no file/directory is currently playing, the menu item does not work. */
122 /* ----------------------------------------------------------------------- */
123 bool bookmark_load_menu(void)
125 if (system_check())
127 char* name = playlist_get_name(NULL, global_temp_buffer,
128 sizeof(global_temp_buffer));
129 if (generate_bookmark_file_name(name))
131 char* bookmark = select_bookmark(global_bookmark_file_name, false);
133 if (bookmark != NULL)
135 return play_bookmark(bookmark);
140 return false;
143 /* ----------------------------------------------------------------------- */
144 /* Gives the user a list of the Most Recent Bookmarks. This is an */
145 /* interface function */
146 /* ----------------------------------------------------------------------- */
147 bool bookmark_mrb_load()
149 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
151 if (bookmark != NULL)
153 return play_bookmark(bookmark);
156 return false;
159 /* ----------------------------------------------------------------------- */
160 /* This function handles an autobookmark creation. This is an interface */
161 /* function. */
162 /* ----------------------------------------------------------------------- */
163 bool bookmark_autobookmark(void)
165 char* bookmark;
166 if (!system_check())
167 return false;
168 int i;
170 audio_pause(); /* first pause playback */
171 bookmark = create_bookmark();
172 /* Workaround for inability to speak when paused: all callers will
173 just do audio_stop() when we return, so we can do it right
174 away. This makes it possible to speak the "Create a Bookmark?"
175 prompt and the "Bookmark Created" splash. */
176 audio_stop();
177 switch (global_settings.autocreatebookmark)
179 case BOOKMARK_YES:
180 return write_bookmark(true, bookmark);
182 case BOOKMARK_NO:
183 return false;
185 case BOOKMARK_RECENT_ONLY_YES:
186 return write_bookmark(false, bookmark);
188 #ifdef HAVE_LCD_BITMAP
189 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
190 const struct text_message message={lines, 1};
191 #else
192 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
193 str(LANG_CONFIRM_WITH_BUTTON)};
194 const struct text_message message={lines, 2};
195 #endif
196 FOR_NB_SCREENS(i)
198 #if LCD_DEPTH > 1
199 screens[i].backdrop_show(BACKDROP_MAIN);
200 #endif
203 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
205 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
206 return write_bookmark(false, bookmark);
207 else
208 return write_bookmark(true, bookmark);
210 return false;
213 /* ----------------------------------------------------------------------- */
214 /* This function takes the current current resume information and writes */
215 /* that to the beginning of the bookmark file. */
216 /* This file will contain N number of bookmarks in the following format: */
217 /* resume_index*resume_offset*resume_seed*resume_first_index* */
218 /* resume_file*milliseconds*MP3 Title* */
219 /* ------------------------------------------------------------------------*/
220 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
222 bool success=false;
223 if (!bookmark)
224 return false; /* something didn't happen correctly, do nothing */
226 if (global_settings.usemrb)
227 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
230 /* writing the bookmark */
231 if (create_bookmark_file)
233 char* name = playlist_get_name(NULL, global_temp_buffer,
234 sizeof(global_temp_buffer));
235 if (generate_bookmark_file_name(name))
237 success = add_bookmark(global_bookmark_file_name, bookmark, false);
241 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
242 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
244 return true;
247 /* ----------------------------------------------------------------------- */
248 /* This function adds a bookmark to a file. */
249 /* ------------------------------------------------------------------------*/
250 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
251 bool most_recent)
253 int temp_bookmark_file = 0;
254 int bookmark_file = 0;
255 int bookmark_count = 0;
256 char* playlist = NULL;
257 char* cp;
258 char* tmp;
259 int len = 0;
260 bool unique = false;
262 /* Opening up a temp bookmark file */
263 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
264 "%s.tmp", bookmark_file_name);
265 temp_bookmark_file = open(global_temp_buffer,
266 O_WRONLY | O_CREAT | O_TRUNC);
267 if (temp_bookmark_file < 0)
268 return false; /* can't open the temp file */
270 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
272 playlist = strchr(bookmark,'/');
273 cp = strrchr(bookmark,';');
274 len = cp - playlist;
275 unique = true;
278 /* Writing the new bookmark to the begining of the temp file */
279 write(temp_bookmark_file, bookmark, strlen(bookmark));
280 write(temp_bookmark_file, "\n", 1);
281 bookmark_count++;
283 /* Reading in the previous bookmarks and writing them to the temp file */
284 bookmark_file = open(bookmark_file_name, O_RDONLY);
285 if (bookmark_file >= 0)
287 while (read_line(bookmark_file, global_read_buffer,
288 sizeof(global_read_buffer)) > 0)
290 /* The MRB has a max of MAX_BOOKMARKS in it */
291 /* This keeps it from getting too large */
292 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
293 break;
295 cp = strchr(global_read_buffer,'/');
296 tmp = strrchr(global_read_buffer,';');
297 if (check_bookmark(global_read_buffer) &&
298 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
300 bookmark_count++;
301 write(temp_bookmark_file, global_read_buffer,
302 strlen(global_read_buffer));
303 write(temp_bookmark_file, "\n", 1);
306 close(bookmark_file);
308 close(temp_bookmark_file);
310 remove(bookmark_file_name);
311 rename(global_temp_buffer, bookmark_file_name);
313 return true;
317 /* ----------------------------------------------------------------------- */
318 /* This function takes the system resume data and formats it into a valid */
319 /* bookmark. */
320 /* ----------------------------------------------------------------------- */
321 static char* create_bookmark()
323 int resume_index = 0;
324 char *file;
326 if (!system_check())
327 return NULL; /* something didn't happen correctly, do nothing */
329 /* grab the currently playing track */
330 struct mp3entry *id3 = audio_current_track();
331 if(!id3)
332 return NULL;
334 /* Get some basic resume information */
335 /* queue_resume and queue_resume_index are not used and can be ignored.*/
336 playlist_get_resume_info(&resume_index);
338 /* Get the currently playing file minus the path */
339 /* This is used when displaying the available bookmarks */
340 file = strrchr(id3->path,'/');
341 if(NULL == file)
342 return NULL;
344 /* create the bookmark */
345 snprintf(global_bookmark, sizeof(global_bookmark),
346 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
347 resume_index,
348 id3->offset,
349 playlist_get_seed(NULL),
351 id3->elapsed,
352 global_settings.repeat_mode,
353 global_settings.playlist_shuffle,
354 playlist_get_name(NULL, global_temp_buffer,
355 sizeof(global_temp_buffer)),
356 file+1);
358 /* checking to see if the bookmark is valid */
359 if (check_bookmark(global_bookmark))
360 return global_bookmark;
361 else
362 return NULL;
365 static bool check_bookmark(const char* bookmark)
367 return parse_bookmark(bookmark,
368 NULL,NULL,NULL, NULL,
369 NULL,0,NULL,NULL,
370 NULL, NULL);
373 /* ----------------------------------------------------------------------- */
374 /* This function will determine if an autoload is necessary. This is an */
375 /* interface function. */
376 /* ------------------------------------------------------------------------*/
377 bool bookmark_autoload(const char* file)
379 if(global_settings.autoloadbookmark == BOOKMARK_NO)
380 return false;
382 /*Checking to see if a bookmark file exists.*/
383 if(!generate_bookmark_file_name(file))
385 return false;
388 if(!file_exists(global_bookmark_file_name))
389 return false;
391 if(global_settings.autoloadbookmark == BOOKMARK_YES)
393 return bookmark_load(global_bookmark_file_name, true);
395 else
397 char* bookmark = select_bookmark(global_bookmark_file_name, true);
399 if (bookmark != NULL)
401 if (!play_bookmark(bookmark))
403 /* Selected bookmark not found. */
404 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
407 /* Act as if autoload was done even if it failed, since the
408 * user did make an active selection.
410 return true;
413 return false;
417 /* ----------------------------------------------------------------------- */
418 /* This function loads the bookmark information into the resume memory. */
419 /* This is an interface function. */
420 /* ------------------------------------------------------------------------*/
421 bool bookmark_load(const char* file, bool autoload)
423 int fd;
424 char* bookmark = NULL;
426 if(autoload)
428 fd = open(file, O_RDONLY);
429 if(fd >= 0)
431 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
432 bookmark=global_read_buffer;
433 close(fd);
436 else
438 /* This is not an auto-load, so list the bookmarks */
439 bookmark = select_bookmark(file, false);
442 if (bookmark != NULL)
444 if (!play_bookmark(bookmark))
446 /* Selected bookmark not found. */
447 if (!autoload)
449 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
452 return false;
456 return true;
460 static int get_bookmark_count(const char* bookmark_file_name)
462 int read_count = 0;
463 int file = open(bookmark_file_name, O_RDONLY);
465 if(file < 0)
466 return -1;
468 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
470 read_count++;
473 close(file);
474 return read_count;
477 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
479 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
480 int read_count = 0;
481 int file = open(bookmarks->filename, O_RDONLY);
483 if (file < 0)
485 return -1;
488 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
489 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
491 /* Entire file fits in buffer */
492 first_line = 0;
495 bookmarks->start = first_line;
496 bookmarks->count = 0;
497 bookmarks->reload = false;
499 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
501 read_count++;
503 if (read_count >= first_line)
505 dest -= strlen(global_read_buffer) + 1;
507 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
508 + (sizeof(char*) * (bookmarks->count + 1)))
510 break;
513 strcpy(dest, global_read_buffer);
514 bookmarks->items[bookmarks->count] = dest;
515 bookmarks->count++;
519 close(file);
520 return bookmarks->start + bookmarks->count;
523 static const char* get_bookmark_info(int list_index,
524 void* data,
525 char *buffer,
526 size_t buffer_len)
528 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
529 int index = list_index / 2;
530 int resume_index = 0;
531 long resume_time = 0;
532 bool shuffle = false;
534 if (bookmarks->show_dont_resume)
536 if (index == 0)
538 return list_index % 2 == 0
539 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
542 index--;
545 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
546 || (index < bookmarks->start))
548 int read_index = index;
550 /* Using count as a guide on how far to move could possibly fail
551 * sometimes. Use byte count if that is a problem?
554 if (read_index != 0)
556 /* Move count * 3 / 4 items in the direction the user is moving,
557 * but don't go too close to the end.
559 int offset = bookmarks->count;
560 int max = bookmarks->total_count - (bookmarks->count / 2);
562 if (read_index < bookmarks->start)
564 offset *= 3;
567 read_index = index - offset / 4;
569 if (read_index > max)
571 read_index = max;
574 if (read_index < 0)
576 read_index = 0;
580 if (buffer_bookmarks(bookmarks, read_index) <= index)
582 return "";
586 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
587 &resume_index, NULL, NULL, NULL, global_temp_buffer,
588 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
589 global_filename))
591 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
594 if (list_index % 2 == 0)
596 char *name;
597 char *format;
598 int len = strlen(global_temp_buffer);
600 if (bookmarks->show_playlist_name && len > 0)
602 name = global_temp_buffer;
603 len--;
605 if (name[len] != '/')
607 strrsplt(name, '.');
609 else if (len > 1)
611 name[len] = '\0';
614 if (len > 1)
616 name = strrsplt(name, '/');
619 format = "%s : %s";
621 else
623 name = global_filename;
624 format = "%s";
627 strrsplt(global_filename, '.');
628 snprintf(buffer, buffer_len, format, name, global_filename);
629 return buffer;
631 else
633 char time_buf[32];
635 format_time(time_buf, sizeof(time_buf), resume_time);
636 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
637 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
638 return buffer;
642 static int bookmark_list_voice_cb(int list_index, void* data)
644 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
645 int index = list_index / 2;
647 if (bookmarks->show_dont_resume)
649 if (index == 0)
650 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
651 index--;
653 say_bookmark(bookmarks->items[index - bookmarks->start], index,
654 bookmarks->show_playlist_name);
655 return 0;
658 /* ----------------------------------------------------------------------- */
659 /* This displays a the bookmarks in a file and allows the user to */
660 /* select one to play. */
661 /* ------------------------------------------------------------------------*/
662 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
664 struct bookmark_list* bookmarks;
665 struct gui_synclist list;
666 int item = 0;
667 int action;
668 size_t size;
669 bool exit = false;
670 bool refresh = true;
672 bookmarks = plugin_get_buffer(&size);
673 bookmarks->buffer_size = size;
674 bookmarks->show_dont_resume = show_dont_resume;
675 bookmarks->filename = bookmark_file_name;
676 bookmarks->start = 0;
677 bookmarks->show_playlist_name
678 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
679 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
680 if(global_settings.talk_menu)
681 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
682 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
683 Icon_Bookmark);
685 while (!exit)
688 if (refresh)
690 int count = get_bookmark_count(bookmark_file_name);
691 bookmarks->total_count = count;
693 if (bookmarks->total_count < 1)
695 /* No more bookmarks, delete file and exit */
696 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
697 remove(bookmark_file_name);
698 return NULL;
701 if (bookmarks->show_dont_resume)
703 count++;
704 item++;
707 gui_synclist_set_nb_items(&list, count * 2);
709 if (item >= count)
711 /* Selected item has been deleted */
712 item = count - 1;
713 gui_synclist_select_item(&list, item * 2);
716 buffer_bookmarks(bookmarks, bookmarks->start);
717 gui_synclist_draw(&list);
718 cond_talk_ids_fq(VOICE_EXT_BMARK);
719 gui_synclist_speak_item(&list);
720 refresh = false;
723 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
724 &list, &action, LIST_WRAP_UNLESS_HELD);
725 item = gui_synclist_get_sel_pos(&list) / 2;
727 if (bookmarks->show_dont_resume)
729 item--;
732 if (action == ACTION_STD_CONTEXT)
734 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
735 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
736 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
737 static const int menu_actions[] =
739 ACTION_STD_OK, ACTION_BMS_DELETE
741 int selection = do_menu(&menu_items, NULL, NULL, false);
743 refresh = true;
745 if (selection >= 0 && selection <=
746 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
748 action = menu_actions[selection];
752 switch (action)
754 case ACTION_STD_OK:
755 if (item >= 0)
757 talk_shutup();
758 return bookmarks->items[item - bookmarks->start];
761 /* Else fall through */
763 case ACTION_TREE_WPS:
764 case ACTION_STD_CANCEL:
765 exit = true;
766 break;
768 case ACTION_BMS_DELETE:
769 if (item >= 0)
771 const char *lines[]={
772 ID2P(LANG_REALLY_DELETE)
774 const char *yes_lines[]={
775 ID2P(LANG_DELETING)
778 const struct text_message message={lines, 1};
779 const struct text_message yes_message={yes_lines, 1};
781 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
783 splash(0, str(LANG_DELETING));
784 delete_bookmark(bookmark_file_name, item);
785 bookmarks->reload = true;
787 refresh = true;
789 break;
791 default:
792 if (default_event_handler(action) == SYS_USB_CONNECTED)
794 exit = true;
797 break;
801 talk_shutup();
802 return NULL;
805 /* ----------------------------------------------------------------------- */
806 /* This function takes a location in a bookmark file and deletes that */
807 /* bookmark. */
808 /* ------------------------------------------------------------------------*/
809 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
811 int temp_bookmark_file = 0;
812 int bookmark_file = 0;
813 int bookmark_count = 0;
815 /* Opening up a temp bookmark file */
816 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
817 "%s.tmp", bookmark_file_name);
818 temp_bookmark_file = open(global_temp_buffer,
819 O_WRONLY | O_CREAT | O_TRUNC);
821 if (temp_bookmark_file < 0)
822 return false; /* can't open the temp file */
824 /* Reading in the previous bookmarks and writing them to the temp file */
825 bookmark_file = open(bookmark_file_name, O_RDONLY);
826 if (bookmark_file >= 0)
828 while (read_line(bookmark_file, global_read_buffer,
829 sizeof(global_read_buffer)) > 0)
831 if (bookmark_id != bookmark_count)
833 write(temp_bookmark_file, global_read_buffer,
834 strlen(global_read_buffer));
835 write(temp_bookmark_file, "\n", 1);
837 bookmark_count++;
839 close(bookmark_file);
841 close(temp_bookmark_file);
843 remove(bookmark_file_name);
844 rename(global_temp_buffer, bookmark_file_name);
846 return true;
849 /* ----------------------------------------------------------------------- */
850 /* This function parses a bookmark, says the voice UI part of it. */
851 /* ------------------------------------------------------------------------*/
852 static void say_bookmark(const char* bookmark,
853 int bookmark_id, bool show_playlist_name)
855 int resume_index;
856 long ms;
857 bool playlist_shuffle = false;
858 bool is_dir;
860 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
861 global_temp_buffer,sizeof(global_temp_buffer),
862 &ms, NULL, &playlist_shuffle,
863 global_filename))
865 talk_id(LANG_BOOKMARK_INVALID, false);
866 return;
869 talk_number(bookmark_id + 1, false);
871 is_dir = (global_temp_buffer[0]
872 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
873 #if CONFIG_CODEC == SWCODEC
874 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
875 together, because there is no guarantee that the same mp3
876 parameters are used. */
877 if(show_playlist_name)
878 { /* It's useful to know which playlist this is */
879 if(is_dir)
880 talk_dir_or_spell(global_temp_buffer,
881 TALK_IDARRAY(VOICE_DIR), true);
882 else talk_file_or_spell(NULL, global_temp_buffer,
883 TALK_IDARRAY(LANG_PLAYLIST), true);
885 #else
886 (void)show_playlist_name;
887 #endif
889 if(playlist_shuffle)
890 talk_id(LANG_SHUFFLE, true);
892 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
893 talk_number(resume_index + 1, true);
894 talk_id(LANG_TIME, true);
895 talk_value(ms / 1000, UNIT_TIME, true);
897 #if CONFIG_CODEC == SWCODEC
898 /* Track filename */
899 if(is_dir)
900 talk_file_or_spell(global_temp_buffer, global_filename,
901 TALK_IDARRAY(VOICE_FILE), true);
902 else
903 { /* Unfortunately if this is a playlist, we do not know in which
904 directory the file is and therefore cannot find the track's
905 .talk file. */
906 talk_id(VOICE_FILE, true);
907 talk_spell(global_filename, true);
909 #endif
912 /* ----------------------------------------------------------------------- */
913 /* This function parses a bookmark and then plays it. */
914 /* ------------------------------------------------------------------------*/
915 static bool play_bookmark(const char* bookmark)
917 int index;
918 int offset;
919 int seed;
921 if (parse_bookmark(bookmark,
922 &index,
923 &offset,
924 &seed,
925 NULL,
926 global_temp_buffer,
927 sizeof(global_temp_buffer),
928 NULL,
929 &global_settings.repeat_mode,
930 &global_settings.playlist_shuffle,
931 global_filename))
933 return bookmark_play(global_temp_buffer, index, offset, seed,
934 global_filename);
937 return false;
940 static const char* skip_token(const char* s)
942 while (*s && *s != ';')
944 s++;
947 if (*s)
949 s++;
952 return s;
955 static const char* int_token(const char* s, int* dest)
957 if (dest != NULL)
959 *dest = atoi(s);
962 return skip_token(s);
965 static const char* long_token(const char* s, long* dest)
967 if (dest != NULL)
969 *dest = atoi(s); /* Should be atol, but we don't have it. */
972 return skip_token(s);
975 static const char* bool_token(const char* s, bool* dest)
977 if (dest != NULL)
979 *dest = atoi(s) != 0;
982 return skip_token(s);
985 /* ----------------------------------------------------------------------- */
986 /* This function takes a bookmark and parses it. This function also */
987 /* validates the bookmark. Passing in NULL for an output variable */
988 /* indicates that value is not requested. */
989 /* ----------------------------------------------------------------------- */
990 static bool parse_bookmark(const char *bookmark,
991 int *resume_index,
992 int *resume_offset,
993 int *resume_seed,
994 int *resume_first_index,
995 char* resume_file,
996 unsigned int resume_file_size,
997 long* ms,
998 int * repeat_mode, bool *shuffle,
999 char* file_name)
1001 const char* s = bookmark;
1002 const char* end;
1004 s = int_token(s, resume_index);
1005 s = int_token(s, resume_offset);
1006 s = int_token(s, resume_seed);
1007 s = int_token(s, resume_first_index);
1008 s = long_token(s, ms);
1009 s = int_token(s, repeat_mode);
1010 s = bool_token(s, shuffle);
1012 if (*s == 0)
1014 return false;
1017 end = strchr(s, ';');
1019 if (resume_file != NULL)
1021 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1022 len = MIN(resume_file_size - 1, len);
1023 strlcpy(resume_file, s, len + 1);
1026 if (end != NULL && file_name != NULL)
1028 end++;
1029 strlcpy(file_name, end, MAX_PATH);
1032 return true;
1035 /* ----------------------------------------------------------------------- */
1036 /* This function is used by multiple functions and is used to generate a */
1037 /* bookmark named based off of the input. */
1038 /* Changing this function could result in how the bookmarks are stored. */
1039 /* it would be here that the centralized/decentralized bookmark code */
1040 /* could be placed. */
1041 /* ----------------------------------------------------------------------- */
1042 static bool generate_bookmark_file_name(const char *in)
1044 int len = strlen(in);
1046 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1047 /* otherwise, name it based on the in variable */
1048 if (!strcmp("/", in))
1049 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1050 else
1052 #ifdef HAVE_MULTIVOLUME
1053 /* The "root" of an extra volume need special handling too. */
1054 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1055 !strcmp("/", global_bookmark_file_name));
1056 #endif
1058 strcpy(global_bookmark_file_name, in);
1059 if(global_bookmark_file_name[len-1] == '/')
1060 len--;
1061 #ifdef HAVE_MULTIVOLUME
1062 if (volume_root)
1063 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1064 else
1065 #endif
1066 strcpy(&global_bookmark_file_name[len], ".bmark");
1069 return true;
1072 /* ----------------------------------------------------------------------- */
1073 /* Returns true if a bookmark file exists for the current playlist */
1074 /* ----------------------------------------------------------------------- */
1075 bool bookmark_exist(void)
1077 bool exist=false;
1079 if(system_check())
1081 char* name = playlist_get_name(NULL, global_temp_buffer,
1082 sizeof(global_temp_buffer));
1083 if (generate_bookmark_file_name(name))
1085 exist = file_exists(global_bookmark_file_name);
1089 return exist;
1092 /* ----------------------------------------------------------------------- */
1093 /* Checks the current state of the system and returns if it is in a */
1094 /* bookmarkable state. */
1095 /* ----------------------------------------------------------------------- */
1096 /* Inputs: */
1097 /* ----------------------------------------------------------------------- */
1098 /* Outputs: */
1099 /* return bool: Indicates if the system was in a bookmarkable state */
1100 /* ----------------------------------------------------------------------- */
1101 static bool system_check(void)
1103 int resume_index = 0;
1105 if (!(audio_status() && audio_current_track()))
1107 /* no track playing */
1108 return false;
1111 /* Checking to see if playing a queued track */
1112 if (playlist_get_resume_info(&resume_index) == -1)
1114 /* something bad happened while getting the queue information */
1115 return false;
1117 else if (playlist_modified(NULL))
1119 /* can't bookmark while in the queue */
1120 return false;
1123 return true;