set YOFS to 0 for portrait LCDs.
[kugel-rb.git] / apps / bookmark.c
blob892a3d35b2e775abadbbb7e878123a784b593815
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"
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 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
194 #if LCD_DEPTH > 1
195 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
196 #endif
197 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
198 show_remote_main_backdrop();
199 #endif
200 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
202 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
203 return write_bookmark(false, bookmark);
204 else
205 return write_bookmark(true, bookmark);
207 return false;
210 /* ----------------------------------------------------------------------- */
211 /* This function takes the current current resume information and writes */
212 /* that to the beginning of the bookmark file. */
213 /* This file will contain N number of bookmarks in the following format: */
214 /* resume_index*resume_offset*resume_seed*resume_first_index* */
215 /* resume_file*milliseconds*MP3 Title* */
216 /* ------------------------------------------------------------------------*/
217 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
219 bool success=false;
220 if (!bookmark)
221 return false; /* something didn't happen correctly, do nothing */
223 if (global_settings.usemrb)
224 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
227 /* writing the bookmark */
228 if (create_bookmark_file)
230 char* name = playlist_get_name(NULL, global_temp_buffer,
231 sizeof(global_temp_buffer));
232 if (generate_bookmark_file_name(name))
234 success = add_bookmark(global_bookmark_file_name, bookmark, false);
238 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
239 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
241 return true;
244 /* ----------------------------------------------------------------------- */
245 /* This function adds a bookmark to a file. */
246 /* ------------------------------------------------------------------------*/
247 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
248 bool most_recent)
250 int temp_bookmark_file = 0;
251 int bookmark_file = 0;
252 int bookmark_count = 0;
253 char* playlist = NULL;
254 char* cp;
255 char* tmp;
256 int len = 0;
257 bool unique = false;
259 /* Opening up a temp bookmark file */
260 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
261 "%s.tmp", bookmark_file_name);
262 temp_bookmark_file = open(global_temp_buffer,
263 O_WRONLY | O_CREAT | O_TRUNC);
264 if (temp_bookmark_file < 0)
265 return false; /* can't open the temp file */
267 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
269 playlist = strchr(bookmark,'/');
270 cp = strrchr(bookmark,';');
271 len = cp - playlist;
272 unique = true;
275 /* Writing the new bookmark to the begining of the temp file */
276 write(temp_bookmark_file, bookmark, strlen(bookmark));
277 write(temp_bookmark_file, "\n", 1);
278 bookmark_count++;
280 /* Reading in the previous bookmarks and writing them to the temp file */
281 bookmark_file = open(bookmark_file_name, O_RDONLY);
282 if (bookmark_file >= 0)
284 while (read_line(bookmark_file, global_read_buffer,
285 sizeof(global_read_buffer)) > 0)
287 /* The MRB has a max of MAX_BOOKMARKS in it */
288 /* This keeps it from getting too large */
289 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
290 break;
292 cp = strchr(global_read_buffer,'/');
293 tmp = strrchr(global_read_buffer,';');
294 if (check_bookmark(global_read_buffer) &&
295 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
297 bookmark_count++;
298 write(temp_bookmark_file, global_read_buffer,
299 strlen(global_read_buffer));
300 write(temp_bookmark_file, "\n", 1);
303 close(bookmark_file);
305 close(temp_bookmark_file);
307 remove(bookmark_file_name);
308 rename(global_temp_buffer, bookmark_file_name);
310 return true;
314 /* ----------------------------------------------------------------------- */
315 /* This function takes the system resume data and formats it into a valid */
316 /* bookmark. */
317 /* ----------------------------------------------------------------------- */
318 static char* create_bookmark()
320 int resume_index = 0;
321 char *file;
323 if (!system_check())
324 return NULL; /* something didn't happen correctly, do nothing */
326 /* grab the currently playing track */
327 struct mp3entry *id3 = audio_current_track();
328 if(!id3)
329 return NULL;
331 /* Get some basic resume information */
332 /* queue_resume and queue_resume_index are not used and can be ignored.*/
333 playlist_get_resume_info(&resume_index);
335 /* Get the currently playing file minus the path */
336 /* This is used when displaying the available bookmarks */
337 file = strrchr(id3->path,'/');
338 if(NULL == file)
339 return NULL;
341 /* create the bookmark */
342 snprintf(global_bookmark, sizeof(global_bookmark),
343 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
344 resume_index,
345 id3->offset,
346 playlist_get_seed(NULL),
348 id3->elapsed,
349 global_settings.repeat_mode,
350 global_settings.playlist_shuffle,
351 playlist_get_name(NULL, global_temp_buffer,
352 sizeof(global_temp_buffer)),
353 file+1);
355 /* checking to see if the bookmark is valid */
356 if (check_bookmark(global_bookmark))
357 return global_bookmark;
358 else
359 return NULL;
362 static bool check_bookmark(const char* bookmark)
364 return parse_bookmark(bookmark,
365 NULL,NULL,NULL, NULL,
366 NULL,0,NULL,NULL,
367 NULL, NULL);
370 /* ----------------------------------------------------------------------- */
371 /* This function will determine if an autoload is necessary. This is an */
372 /* interface function. */
373 /* ------------------------------------------------------------------------*/
374 bool bookmark_autoload(const char* file)
376 if(global_settings.autoloadbookmark == BOOKMARK_NO)
377 return false;
379 /*Checking to see if a bookmark file exists.*/
380 if(!generate_bookmark_file_name(file))
382 return false;
385 if(!file_exists(global_bookmark_file_name))
386 return false;
388 if(global_settings.autoloadbookmark == BOOKMARK_YES)
390 return bookmark_load(global_bookmark_file_name, true);
392 else
394 char* bookmark = select_bookmark(global_bookmark_file_name, true);
396 if (bookmark != NULL)
398 if (!play_bookmark(bookmark))
400 /* Selected bookmark not found. */
401 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
404 /* Act as if autoload was done even if it failed, since the
405 * user did make an active selection.
407 return true;
410 return false;
414 /* ----------------------------------------------------------------------- */
415 /* This function loads the bookmark information into the resume memory. */
416 /* This is an interface function. */
417 /* ------------------------------------------------------------------------*/
418 bool bookmark_load(const char* file, bool autoload)
420 int fd;
421 char* bookmark = NULL;
423 if(autoload)
425 fd = open(file, O_RDONLY);
426 if(fd >= 0)
428 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
429 bookmark=global_read_buffer;
430 close(fd);
433 else
435 /* This is not an auto-load, so list the bookmarks */
436 bookmark = select_bookmark(file, false);
439 if (bookmark != NULL)
441 if (!play_bookmark(bookmark))
443 /* Selected bookmark not found. */
444 if (!autoload)
446 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
449 return false;
453 return true;
457 static int get_bookmark_count(const char* bookmark_file_name)
459 int read_count = 0;
460 int file = open(bookmark_file_name, O_RDONLY);
462 if(file < 0)
463 return -1;
465 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
467 read_count++;
470 close(file);
471 return read_count;
474 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
476 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
477 int read_count = 0;
478 int file = open(bookmarks->filename, O_RDONLY);
480 if (file < 0)
482 return -1;
485 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
486 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
488 /* Entire file fits in buffer */
489 first_line = 0;
492 bookmarks->start = first_line;
493 bookmarks->count = 0;
494 bookmarks->reload = false;
496 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
498 read_count++;
500 if (read_count >= first_line)
502 dest -= strlen(global_read_buffer) + 1;
504 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
505 + (sizeof(char*) * (bookmarks->count + 1)))
507 break;
510 strcpy(dest, global_read_buffer);
511 bookmarks->items[bookmarks->count] = dest;
512 bookmarks->count++;
516 close(file);
517 return bookmarks->start + bookmarks->count;
520 static char* get_bookmark_info(int list_index,
521 void* data,
522 char *buffer,
523 size_t buffer_len)
525 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
526 int index = list_index / 2;
527 int resume_index = 0;
528 long resume_time = 0;
529 bool shuffle = false;
531 if (bookmarks->show_dont_resume)
533 if (index == 0)
535 return list_index % 2 == 0
536 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
539 index--;
542 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
543 || (index < bookmarks->start))
545 int read_index = index;
547 /* Using count as a guide on how far to move could possibly fail
548 * sometimes. Use byte count if that is a problem?
551 if (read_index != 0)
553 /* Move count * 3 / 4 items in the direction the user is moving,
554 * but don't go too close to the end.
556 int offset = bookmarks->count;
557 int max = bookmarks->total_count - (bookmarks->count / 2);
559 if (read_index < bookmarks->start)
561 offset *= 3;
564 read_index = index - offset / 4;
566 if (read_index > max)
568 read_index = max;
571 if (read_index < 0)
573 read_index = 0;
577 if (buffer_bookmarks(bookmarks, read_index) <= index)
579 return "";
583 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
584 &resume_index, NULL, NULL, NULL, global_temp_buffer,
585 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
586 global_filename))
588 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
591 if (list_index % 2 == 0)
593 char *name;
594 char *format;
595 int len = strlen(global_temp_buffer);
597 if (bookmarks->show_playlist_name && len > 0)
599 name = global_temp_buffer;
600 len--;
602 if (name[len] != '/')
604 strrsplt(name, '.');
606 else if (len > 1)
608 name[len] = '\0';
611 if (len > 1)
613 name = strrsplt(name, '/');
616 format = "%s : %s";
618 else
620 name = global_filename;
621 format = "%s";
624 strrsplt(global_filename, '.');
625 snprintf(buffer, buffer_len, format, name, global_filename);
626 return buffer;
628 else
630 char time_buf[32];
632 format_time(time_buf, sizeof(time_buf), resume_time);
633 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
634 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
635 return buffer;
639 static int bookmark_list_voice_cb(int list_index, void* data)
641 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
642 int index = list_index / 2;
644 if (bookmarks->show_dont_resume)
646 if (index == 0)
647 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
648 index--;
650 say_bookmark(bookmarks->items[index - bookmarks->start], index,
651 bookmarks->show_playlist_name);
652 return 0;
655 /* ----------------------------------------------------------------------- */
656 /* This displays a the bookmarks in a file and allows the user to */
657 /* select one to play. */
658 /* ------------------------------------------------------------------------*/
659 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
661 struct bookmark_list* bookmarks;
662 struct gui_synclist list;
663 int item = 0;
664 int action;
665 size_t size;
666 bool exit = false;
667 bool refresh = true;
669 bookmarks = plugin_get_buffer(&size);
670 bookmarks->buffer_size = size;
671 bookmarks->show_dont_resume = show_dont_resume;
672 bookmarks->filename = bookmark_file_name;
673 bookmarks->start = 0;
674 bookmarks->show_playlist_name
675 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
676 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
677 if(global_settings.talk_menu)
678 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
679 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
680 Icon_Bookmark);
682 while (!exit)
685 if (refresh)
687 int count = get_bookmark_count(bookmark_file_name);
688 bookmarks->total_count = count;
690 if (bookmarks->total_count < 1)
692 /* No more bookmarks, delete file and exit */
693 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
694 remove(bookmark_file_name);
695 return NULL;
698 if (bookmarks->show_dont_resume)
700 count++;
701 item++;
704 gui_synclist_set_nb_items(&list, count * 2);
706 if (item >= count)
708 /* Selected item has been deleted */
709 item = count - 1;
710 gui_synclist_select_item(&list, item * 2);
713 buffer_bookmarks(bookmarks, bookmarks->start);
714 gui_synclist_draw(&list);
715 cond_talk_ids_fq(VOICE_EXT_BMARK);
716 gui_synclist_speak_item(&list);
717 refresh = false;
720 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
721 &list, &action, LIST_WRAP_UNLESS_HELD);
722 item = gui_synclist_get_sel_pos(&list) / 2;
724 if (bookmarks->show_dont_resume)
726 item--;
729 if (action == ACTION_STD_CONTEXT)
731 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
732 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
733 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
734 static const int menu_actions[] =
736 ACTION_STD_OK, ACTION_BMS_DELETE
738 int selection = do_menu(&menu_items, NULL, NULL, false);
740 refresh = true;
742 if (selection >= 0 && selection <=
743 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
745 action = menu_actions[selection];
749 switch (action)
751 case ACTION_STD_OK:
752 if (item >= 0)
754 talk_shutup();
755 return bookmarks->items[item - bookmarks->start];
758 /* Else fall through */
760 case ACTION_TREE_WPS:
761 case ACTION_STD_CANCEL:
762 exit = true;
763 break;
765 case ACTION_BMS_DELETE:
766 if (item >= 0)
768 delete_bookmark(bookmark_file_name, item);
769 bookmarks->reload = true;
770 refresh = true;
772 break;
774 default:
775 if (default_event_handler(action) == SYS_USB_CONNECTED)
777 exit = true;
780 break;
784 talk_shutup();
785 return NULL;
788 /* ----------------------------------------------------------------------- */
789 /* This function takes a location in a bookmark file and deletes that */
790 /* bookmark. */
791 /* ------------------------------------------------------------------------*/
792 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
794 int temp_bookmark_file = 0;
795 int bookmark_file = 0;
796 int bookmark_count = 0;
798 /* Opening up a temp bookmark file */
799 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
800 "%s.tmp", bookmark_file_name);
801 temp_bookmark_file = open(global_temp_buffer,
802 O_WRONLY | O_CREAT | O_TRUNC);
804 if (temp_bookmark_file < 0)
805 return false; /* can't open the temp file */
807 /* Reading in the previous bookmarks and writing them to the temp file */
808 bookmark_file = open(bookmark_file_name, O_RDONLY);
809 if (bookmark_file >= 0)
811 while (read_line(bookmark_file, global_read_buffer,
812 sizeof(global_read_buffer)) > 0)
814 if (bookmark_id != bookmark_count)
816 write(temp_bookmark_file, global_read_buffer,
817 strlen(global_read_buffer));
818 write(temp_bookmark_file, "\n", 1);
820 bookmark_count++;
822 close(bookmark_file);
824 close(temp_bookmark_file);
826 remove(bookmark_file_name);
827 rename(global_temp_buffer, bookmark_file_name);
829 return true;
832 /* ----------------------------------------------------------------------- */
833 /* This function parses a bookmark, says the voice UI part of it. */
834 /* ------------------------------------------------------------------------*/
835 static void say_bookmark(const char* bookmark,
836 int bookmark_id, bool show_playlist_name)
838 int resume_index;
839 long ms;
840 bool playlist_shuffle = false;
841 bool is_dir;
843 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
844 global_temp_buffer,sizeof(global_temp_buffer),
845 &ms, NULL, &playlist_shuffle,
846 global_filename))
848 talk_id(LANG_BOOKMARK_INVALID, false);
849 return;
852 talk_number(bookmark_id + 1, false);
854 is_dir = (global_temp_buffer[0]
855 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
856 #if CONFIG_CODEC == SWCODEC
857 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
858 together, because there is no guarantee that the same mp3
859 parameters are used. */
860 if(show_playlist_name)
861 { /* It's useful to know which playlist this is */
862 if(is_dir)
863 talk_dir_or_spell(global_temp_buffer,
864 TALK_IDARRAY(VOICE_DIR), true);
865 else talk_file_or_spell(NULL, global_temp_buffer,
866 TALK_IDARRAY(LANG_PLAYLIST), true);
868 #else
869 (void)show_playlist_name;
870 #endif
872 if(playlist_shuffle)
873 talk_id(LANG_SHUFFLE, true);
875 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
876 talk_number(resume_index + 1, true);
877 talk_id(LANG_TIME, true);
878 talk_value(ms / 1000, UNIT_TIME, true);
880 #if CONFIG_CODEC == SWCODEC
881 /* Track filename */
882 if(is_dir)
883 talk_file_or_spell(global_temp_buffer, global_filename,
884 TALK_IDARRAY(VOICE_FILE), true);
885 else
886 { /* Unfortunately if this is a playlist, we do not know in which
887 directory the file is and therefore cannot find the track's
888 .talk file. */
889 talk_id(VOICE_FILE, true);
890 talk_spell(global_filename, true);
892 #endif
895 /* ----------------------------------------------------------------------- */
896 /* This function parses a bookmark and then plays it. */
897 /* ------------------------------------------------------------------------*/
898 static bool play_bookmark(const char* bookmark)
900 int index;
901 int offset;
902 int seed;
904 if (parse_bookmark(bookmark,
905 &index,
906 &offset,
907 &seed,
908 NULL,
909 global_temp_buffer,
910 sizeof(global_temp_buffer),
911 NULL,
912 &global_settings.repeat_mode,
913 &global_settings.playlist_shuffle,
914 global_filename))
916 return bookmark_play(global_temp_buffer, index, offset, seed,
917 global_filename);
920 return false;
923 static const char* skip_token(const char* s)
925 while (*s && *s != ';')
927 s++;
930 if (*s)
932 s++;
935 return s;
938 static const char* int_token(const char* s, int* dest)
940 if (dest != NULL)
942 *dest = atoi(s);
945 return skip_token(s);
948 static const char* long_token(const char* s, long* dest)
950 if (dest != NULL)
952 *dest = atoi(s); /* Should be atol, but we don't have it. */
955 return skip_token(s);
958 static const char* bool_token(const char* s, bool* dest)
960 if (dest != NULL)
962 *dest = atoi(s) != 0;
965 return skip_token(s);
968 /* ----------------------------------------------------------------------- */
969 /* This function takes a bookmark and parses it. This function also */
970 /* validates the bookmark. Passing in NULL for an output variable */
971 /* indicates that value is not requested. */
972 /* ----------------------------------------------------------------------- */
973 static bool parse_bookmark(const char *bookmark,
974 int *resume_index,
975 int *resume_offset,
976 int *resume_seed,
977 int *resume_first_index,
978 char* resume_file,
979 unsigned int resume_file_size,
980 long* ms,
981 int * repeat_mode, bool *shuffle,
982 char* file_name)
984 const char* s = bookmark;
985 const char* end;
987 s = int_token(s, resume_index);
988 s = int_token(s, resume_offset);
989 s = int_token(s, resume_seed);
990 s = int_token(s, resume_first_index);
991 s = long_token(s, ms);
992 s = int_token(s, repeat_mode);
993 s = bool_token(s, shuffle);
995 if (*s == 0)
997 return false;
1000 end = strchr(s, ';');
1002 if (resume_file != NULL)
1004 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1005 len = MIN(resume_file_size - 1, len);
1006 strlcpy(resume_file, s, len + 1);
1009 if (end != NULL && file_name != NULL)
1011 end++;
1012 strlcpy(file_name, end, MAX_PATH);
1015 return true;
1018 /* ----------------------------------------------------------------------- */
1019 /* This function is used by multiple functions and is used to generate a */
1020 /* bookmark named based off of the input. */
1021 /* Changing this function could result in how the bookmarks are stored. */
1022 /* it would be here that the centralized/decentralized bookmark code */
1023 /* could be placed. */
1024 /* ----------------------------------------------------------------------- */
1025 static bool generate_bookmark_file_name(const char *in)
1027 int len = strlen(in);
1029 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1030 /* otherwise, name it based on the in variable */
1031 if (!strcmp("/", in))
1032 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1033 else
1035 #ifdef HAVE_MULTIVOLUME
1036 /* The "root" of an extra volume need special handling too. */
1037 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1038 !strcmp("/", global_bookmark_file_name));
1039 #endif
1041 strcpy(global_bookmark_file_name, in);
1042 if(global_bookmark_file_name[len-1] == '/')
1043 len--;
1044 #ifdef HAVE_MULTIVOLUME
1045 if (volume_root)
1046 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1047 else
1048 #endif
1049 strcpy(&global_bookmark_file_name[len], ".bmark");
1052 return true;
1055 /* ----------------------------------------------------------------------- */
1056 /* Returns true if a bookmark file exists for the current playlist */
1057 /* ----------------------------------------------------------------------- */
1058 bool bookmark_exist(void)
1060 bool exist=false;
1062 if(system_check())
1064 char* name = playlist_get_name(NULL, global_temp_buffer,
1065 sizeof(global_temp_buffer));
1066 if (generate_bookmark_file_name(name))
1068 exist = file_exists(global_bookmark_file_name);
1072 return exist;
1075 /* ----------------------------------------------------------------------- */
1076 /* Checks the current state of the system and returns if it is in a */
1077 /* bookmarkable state. */
1078 /* ----------------------------------------------------------------------- */
1079 /* Inputs: */
1080 /* ----------------------------------------------------------------------- */
1081 /* Outputs: */
1082 /* return bool: Indicates if the system was in a bookmarkable state */
1083 /* ----------------------------------------------------------------------- */
1084 static bool system_check(void)
1086 int resume_index = 0;
1088 if (!(audio_status() && audio_current_track()))
1090 /* no track playing */
1091 return false;
1094 /* Checking to see if playing a queued track */
1095 if (playlist_get_resume_info(&resume_index) == -1)
1097 /* something bad happened while getting the queue information */
1098 return false;
1100 else if (playlist_modified(NULL))
1102 /* can't bookmark while in the queue */
1103 return false;
1106 return true;