woops
[kugel-rb.git] / apps / bookmark.c
blob083942c3423bca31a358bfd73017eee94595975c
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"
45 #include "dsp.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 /* flags for optional bookmark tokens */
66 #define BM_PITCH 0x01
67 #define BM_SPEED 0x02
69 /* bookmark values */
70 static struct {
71 int resume_index;
72 unsigned long resume_offset;
73 int resume_seed;
74 long resume_time;
75 int repeat_mode;
76 bool shuffle;
77 /* optional values */
78 int pitch;
79 int speed;
80 } bm;
82 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
83 bool most_recent);
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,
93 void* data,
94 char *buffer,
95 size_t buffer_len);
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());
117 return false;
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);
143 return false;
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;
153 bool ret = false;
155 push_current_activity(ACTIVITY_BOOKMARKSLIST);
156 bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
157 if (bookmark != NULL)
159 ret = play_bookmark(bookmark);
162 pop_current_activity();
163 return ret;
166 /* ----------------------------------------------------------------------- */
167 /* This function handles an autobookmark creation. This is an interface */
168 /* function. */
169 /* ----------------------------------------------------------------------- */
170 bool bookmark_autobookmark(bool prompt_ok)
172 char* bookmark;
173 bool update;
175 if (!is_bookmarkable_state())
176 return false;
178 audio_pause(); /* first pause playback */
179 update = (global_settings.autoupdatebookmark && bookmark_exists());
180 bookmark = create_bookmark();
181 /* Workaround for inability to speak when paused: all callers will
182 just do audio_stop() when we return, so we can do it right
183 away. This makes it possible to speak the "Create a Bookmark?"
184 prompt and the "Bookmark Created" splash. */
185 audio_stop();
187 if (update)
188 return write_bookmark(true, bookmark);
190 switch (global_settings.autocreatebookmark)
192 case BOOKMARK_YES:
193 return write_bookmark(true, bookmark);
195 case BOOKMARK_NO:
196 return false;
198 case BOOKMARK_RECENT_ONLY_YES:
199 return write_bookmark(false, bookmark);
201 #ifdef HAVE_LCD_BITMAP
202 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
203 const struct text_message message={lines, 1};
204 #else
205 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
206 str(LANG_CONFIRM_WITH_BUTTON)};
207 const struct text_message message={lines, 2};
208 #endif
210 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
212 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
213 return write_bookmark(false, bookmark);
214 else
215 return write_bookmark(true, bookmark);
217 return false;
220 /* ----------------------------------------------------------------------- */
221 /* This function takes the current current resume information and writes */
222 /* that to the beginning of the bookmark file. */
223 /* This file will contain N number of bookmarks in the following format: */
224 /* resume_index*resume_offset*resume_seed*resume_first_index* */
225 /* resume_file*milliseconds*MP3 Title* */
226 /* ------------------------------------------------------------------------*/
227 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
229 bool success=false;
230 if (!bookmark)
231 return false; /* something didn't happen correctly, do nothing */
233 if (global_settings.usemrb)
234 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
237 /* writing the bookmark */
238 if (create_bookmark_file)
240 char* name = playlist_get_name(NULL, global_temp_buffer,
241 sizeof(global_temp_buffer));
242 if (generate_bookmark_file_name(name))
244 success = add_bookmark(global_bookmark_file_name, bookmark, false);
248 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
249 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
251 return true;
254 /* ----------------------------------------------------------------------- */
255 /* This function adds a bookmark to a file. */
256 /* ------------------------------------------------------------------------*/
257 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
258 bool most_recent)
260 int temp_bookmark_file = 0;
261 int bookmark_file = 0;
262 int bookmark_count = 0;
263 char* playlist = NULL;
264 char* cp;
265 char* tmp;
266 int len = 0;
267 bool unique = false;
269 /* Opening up a temp bookmark file */
270 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
271 "%s.tmp", bookmark_file_name);
272 temp_bookmark_file = open(global_temp_buffer,
273 O_WRONLY | O_CREAT | O_TRUNC, 0666);
274 if (temp_bookmark_file < 0)
275 return false; /* can't open the temp file */
277 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
279 playlist = strchr(bookmark,'/');
280 cp = strrchr(bookmark,';');
281 len = cp - playlist;
282 unique = true;
285 /* Writing the new bookmark to the begining of the temp file */
286 write(temp_bookmark_file, bookmark, strlen(bookmark));
287 write(temp_bookmark_file, "\n", 1);
288 bookmark_count++;
290 /* Reading in the previous bookmarks and writing them to the temp file */
291 bookmark_file = open(bookmark_file_name, O_RDONLY);
292 if (bookmark_file >= 0)
294 while (read_line(bookmark_file, global_read_buffer,
295 sizeof(global_read_buffer)) > 0)
297 /* The MRB has a max of MAX_BOOKMARKS in it */
298 /* This keeps it from getting too large */
299 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
300 break;
302 cp = strchr(global_read_buffer,'/');
303 tmp = strrchr(global_read_buffer,';');
304 if (parse_bookmark(global_read_buffer, false) &&
305 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
307 bookmark_count++;
308 write(temp_bookmark_file, global_read_buffer,
309 strlen(global_read_buffer));
310 write(temp_bookmark_file, "\n", 1);
313 close(bookmark_file);
315 close(temp_bookmark_file);
317 remove(bookmark_file_name);
318 rename(global_temp_buffer, bookmark_file_name);
320 return true;
324 /* ----------------------------------------------------------------------- */
325 /* This function takes the system resume data and formats it into a valid */
326 /* bookmark. */
327 /* ----------------------------------------------------------------------- */
328 static char* create_bookmark()
330 int resume_index = 0;
331 char *file;
333 if (!is_bookmarkable_state())
334 return NULL; /* something didn't happen correctly, do nothing */
336 /* grab the currently playing track */
337 struct mp3entry *id3 = audio_current_track();
338 if(!id3)
339 return NULL;
341 /* Get some basic resume information */
342 /* queue_resume and queue_resume_index are not used and can be ignored.*/
343 playlist_get_resume_info(&resume_index);
345 /* Get the currently playing file minus the path */
346 /* This is used when displaying the available bookmarks */
347 file = strrchr(id3->path,'/');
348 if(NULL == file)
349 return NULL;
351 /* create the bookmark */
352 snprintf(global_bookmark, sizeof(global_bookmark),
353 /* new optional bookmark token descriptors should be inserted
354 just before the "%s;%s" in this line... */
355 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
356 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
357 #else
358 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
359 #endif
360 /* ... their flags should go here ... */
361 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
362 BM_PITCH | BM_SPEED,
363 #else
365 #endif
366 resume_index,
367 id3->offset,
368 playlist_get_seed(NULL),
369 id3->elapsed,
370 global_settings.repeat_mode,
371 global_settings.playlist_shuffle,
372 /* ...and their values should go here */
373 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
374 (long)sound_get_pitch(),
375 (long)dsp_get_timestretch(),
376 #endif
377 /* more mandatory tokens */
378 playlist_get_name(NULL, global_temp_buffer,
379 sizeof(global_temp_buffer)),
380 file+1);
382 /* checking to see if the bookmark is valid */
383 if (parse_bookmark(global_bookmark, false))
384 return global_bookmark;
385 else
386 return NULL;
389 /* ----------------------------------------------------------------------- */
390 /* This function will determine if an autoload is necessary. This is an */
391 /* interface function. */
392 /* ------------------------------------------------------------------------*/
393 bool bookmark_autoload(const char* file)
395 if(global_settings.autoloadbookmark == BOOKMARK_NO)
396 return false;
398 /*Checking to see if a bookmark file exists.*/
399 if(!generate_bookmark_file_name(file))
401 return false;
404 if(!file_exists(global_bookmark_file_name))
405 return false;
407 if(global_settings.autoloadbookmark == BOOKMARK_YES)
409 return bookmark_load(global_bookmark_file_name, true);
411 else
413 char* bookmark = select_bookmark(global_bookmark_file_name, true);
415 if (bookmark != NULL)
417 if (!play_bookmark(bookmark))
419 /* Selected bookmark not found. */
420 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
423 /* Act as if autoload was done even if it failed, since the
424 * user did make an active selection.
426 return true;
429 return false;
433 /* ----------------------------------------------------------------------- */
434 /* This function loads the bookmark information into the resume memory. */
435 /* This is an interface function. */
436 /* ------------------------------------------------------------------------*/
437 bool bookmark_load(const char* file, bool autoload)
439 int fd;
440 char* bookmark = NULL;
442 if(autoload)
444 fd = open(file, O_RDONLY);
445 if(fd >= 0)
447 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
448 bookmark=global_read_buffer;
449 close(fd);
452 else
454 /* This is not an auto-load, so list the bookmarks */
455 bookmark = select_bookmark(file, false);
458 if (bookmark != NULL)
460 if (!play_bookmark(bookmark))
462 /* Selected bookmark not found. */
463 if (!autoload)
465 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
468 return false;
472 return true;
476 static int get_bookmark_count(const char* bookmark_file_name)
478 int read_count = 0;
479 int file = open(bookmark_file_name, O_RDONLY);
481 if(file < 0)
482 return -1;
484 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
486 read_count++;
489 close(file);
490 return read_count;
493 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
495 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
496 int read_count = 0;
497 int file = open(bookmarks->filename, O_RDONLY);
499 if (file < 0)
501 return -1;
504 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
505 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
507 /* Entire file fits in buffer */
508 first_line = 0;
511 bookmarks->start = first_line;
512 bookmarks->count = 0;
513 bookmarks->reload = false;
515 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
517 read_count++;
519 if (read_count >= first_line)
521 dest -= strlen(global_read_buffer) + 1;
523 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
524 + (sizeof(char*) * (bookmarks->count + 1)))
526 break;
529 strcpy(dest, global_read_buffer);
530 bookmarks->items[bookmarks->count] = dest;
531 bookmarks->count++;
535 close(file);
536 return bookmarks->start + bookmarks->count;
539 static const char* get_bookmark_info(int list_index,
540 void* data,
541 char *buffer,
542 size_t buffer_len)
544 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
545 int index = list_index / 2;
547 if (bookmarks->show_dont_resume)
549 if (index == 0)
551 return list_index % 2 == 0
552 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
555 index--;
558 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
559 || (index < bookmarks->start))
561 int read_index = index;
563 /* Using count as a guide on how far to move could possibly fail
564 * sometimes. Use byte count if that is a problem?
567 if (read_index != 0)
569 /* Move count * 3 / 4 items in the direction the user is moving,
570 * but don't go too close to the end.
572 int offset = bookmarks->count;
573 int max = bookmarks->total_count - (bookmarks->count / 2);
575 if (read_index < bookmarks->start)
577 offset *= 3;
580 read_index = index - offset / 4;
582 if (read_index > max)
584 read_index = max;
587 if (read_index < 0)
589 read_index = 0;
593 if (buffer_bookmarks(bookmarks, read_index) <= index)
595 return "";
599 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
601 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
604 if (list_index % 2 == 0)
606 char *name;
607 char *format;
608 int len = strlen(global_temp_buffer);
610 if (bookmarks->show_playlist_name && len > 0)
612 name = global_temp_buffer;
613 len--;
615 if (name[len] != '/')
617 strrsplt(name, '.');
619 else if (len > 1)
621 name[len] = '\0';
624 if (len > 1)
626 name = strrsplt(name, '/');
629 format = "%s : %s";
631 else
633 name = global_filename;
634 format = "%s";
637 strrsplt(global_filename, '.');
638 snprintf(buffer, buffer_len, format, name, global_filename);
639 return buffer;
641 else
643 char time_buf[32];
645 format_time(time_buf, sizeof(time_buf), bm.resume_time);
646 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
647 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
648 return buffer;
652 static int bookmark_list_voice_cb(int list_index, void* data)
654 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
655 int index = list_index / 2;
657 if (bookmarks->show_dont_resume)
659 if (index == 0)
660 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
661 index--;
663 say_bookmark(bookmarks->items[index - bookmarks->start], index,
664 bookmarks->show_playlist_name);
665 return 0;
668 /* ----------------------------------------------------------------------- */
669 /* This displays a the bookmarks in a file and allows the user to */
670 /* select one to play. */
671 /* ------------------------------------------------------------------------*/
672 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
674 struct bookmark_list* bookmarks;
675 struct gui_synclist list;
676 int item = 0;
677 int action;
678 size_t size;
679 bool exit = false;
680 bool refresh = true;
682 bookmarks = plugin_get_buffer(&size);
683 bookmarks->buffer_size = size;
684 bookmarks->show_dont_resume = show_dont_resume;
685 bookmarks->filename = bookmark_file_name;
686 bookmarks->start = 0;
687 bookmarks->show_playlist_name
688 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
689 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
690 if(global_settings.talk_menu)
691 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
692 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
693 Icon_Bookmark);
695 while (!exit)
698 if (refresh)
700 int count = get_bookmark_count(bookmark_file_name);
701 bookmarks->total_count = count;
703 if (bookmarks->total_count < 1)
705 /* No more bookmarks, delete file and exit */
706 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
707 remove(bookmark_file_name);
708 return NULL;
711 if (bookmarks->show_dont_resume)
713 count++;
714 item++;
717 gui_synclist_set_nb_items(&list, count * 2);
719 if (item >= count)
721 /* Selected item has been deleted */
722 item = count - 1;
723 gui_synclist_select_item(&list, item * 2);
726 buffer_bookmarks(bookmarks, bookmarks->start);
727 gui_synclist_draw(&list);
728 cond_talk_ids_fq(VOICE_EXT_BMARK);
729 gui_synclist_speak_item(&list);
730 refresh = false;
733 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
734 &list, &action, LIST_WRAP_UNLESS_HELD);
735 item = gui_synclist_get_sel_pos(&list) / 2;
737 if (bookmarks->show_dont_resume)
739 item--;
742 if (action == ACTION_STD_CONTEXT)
744 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
745 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
746 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
747 static const int menu_actions[] =
749 ACTION_STD_OK, ACTION_BMS_DELETE
751 int selection = do_menu(&menu_items, NULL, NULL, false);
753 refresh = true;
755 if (selection >= 0 && selection <=
756 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
758 action = menu_actions[selection];
762 switch (action)
764 case ACTION_STD_OK:
765 if (item >= 0)
767 talk_shutup();
768 return bookmarks->items[item - bookmarks->start];
771 /* Else fall through */
773 case ACTION_TREE_WPS:
774 case ACTION_STD_CANCEL:
775 exit = true;
776 break;
778 case ACTION_BMS_DELETE:
779 if (item >= 0)
781 const char *lines[]={
782 ID2P(LANG_REALLY_DELETE)
784 const char *yes_lines[]={
785 ID2P(LANG_DELETING)
788 const struct text_message message={lines, 1};
789 const struct text_message yes_message={yes_lines, 1};
791 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
793 delete_bookmark(bookmark_file_name, item);
794 bookmarks->reload = true;
796 refresh = true;
798 break;
800 default:
801 if (default_event_handler(action) == SYS_USB_CONNECTED)
803 exit = true;
806 break;
810 talk_shutup();
811 return NULL;
814 /* ----------------------------------------------------------------------- */
815 /* This function takes a location in a bookmark file and deletes that */
816 /* bookmark. */
817 /* ------------------------------------------------------------------------*/
818 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
820 int temp_bookmark_file = 0;
821 int bookmark_file = 0;
822 int bookmark_count = 0;
824 /* Opening up a temp bookmark file */
825 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
826 "%s.tmp", bookmark_file_name);
827 temp_bookmark_file = open(global_temp_buffer,
828 O_WRONLY | O_CREAT | O_TRUNC, 0666);
830 if (temp_bookmark_file < 0)
831 return false; /* can't open the temp file */
833 /* Reading in the previous bookmarks and writing them to the temp file */
834 bookmark_file = open(bookmark_file_name, O_RDONLY);
835 if (bookmark_file >= 0)
837 while (read_line(bookmark_file, global_read_buffer,
838 sizeof(global_read_buffer)) > 0)
840 if (bookmark_id != bookmark_count)
842 write(temp_bookmark_file, global_read_buffer,
843 strlen(global_read_buffer));
844 write(temp_bookmark_file, "\n", 1);
846 bookmark_count++;
848 close(bookmark_file);
850 close(temp_bookmark_file);
852 remove(bookmark_file_name);
853 rename(global_temp_buffer, bookmark_file_name);
855 return true;
858 /* ----------------------------------------------------------------------- */
859 /* This function parses a bookmark, says the voice UI part of it. */
860 /* ------------------------------------------------------------------------*/
861 static void say_bookmark(const char* bookmark,
862 int bookmark_id, bool show_playlist_name)
864 if (!parse_bookmark(bookmark, true))
866 talk_id(LANG_BOOKMARK_INVALID, false);
867 return;
870 talk_number(bookmark_id + 1, false);
872 #if CONFIG_CODEC == SWCODEC
873 bool is_dir = (global_temp_buffer[0]
874 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
876 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
877 together, because there is no guarantee that the same mp3
878 parameters are used. */
879 if(show_playlist_name)
880 { /* It's useful to know which playlist this is */
881 if(is_dir)
882 talk_dir_or_spell(global_temp_buffer,
883 TALK_IDARRAY(VOICE_DIR), true);
884 else talk_file_or_spell(NULL, global_temp_buffer,
885 TALK_IDARRAY(LANG_PLAYLIST), true);
887 #else
888 (void)show_playlist_name;
889 #endif
891 if(bm.shuffle)
892 talk_id(LANG_SHUFFLE, true);
894 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
895 talk_number(bm.resume_index + 1, true);
896 talk_id(LANG_TIME, true);
897 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
899 #if CONFIG_CODEC == SWCODEC
900 /* Track filename */
901 if(is_dir)
902 talk_file_or_spell(global_temp_buffer, global_filename,
903 TALK_IDARRAY(VOICE_FILE), true);
904 else
905 { /* Unfortunately if this is a playlist, we do not know in which
906 directory the file is and therefore cannot find the track's
907 .talk file. */
908 talk_id(VOICE_FILE, true);
909 talk_spell(global_filename, true);
911 #endif
914 /* ----------------------------------------------------------------------- */
915 /* This function parses a bookmark and then plays it. */
916 /* ------------------------------------------------------------------------*/
917 static bool play_bookmark(const char* bookmark)
919 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
920 /* preset pitch and speed to 100% in case bookmark doesn't have info */
921 bm.pitch = sound_get_pitch();
922 bm.speed = dsp_get_timestretch();
923 #endif
925 if (parse_bookmark(bookmark, true))
927 global_settings.repeat_mode = bm.repeat_mode;
928 global_settings.playlist_shuffle = bm.shuffle;
929 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
930 sound_set_pitch(bm.pitch);
931 dsp_set_timestretch(bm.speed);
932 #endif
933 if (!warn_on_pl_erase())
934 return false;
935 return bookmark_play(global_temp_buffer, bm.resume_index,
936 bm.resume_offset, bm.resume_seed, global_filename);
939 return false;
942 static const char* skip_token(const char* s)
944 while (*s && *s != ';')
946 s++;
949 if (*s)
951 s++;
954 return s;
957 static const char* int_token(const char* s, int* dest)
959 *dest = atoi(s);
960 return skip_token(s);
963 static const char* long_token(const char* s, long* dest)
965 *dest = atoi(s); /* Should be atol, but we don't have it. */
966 return skip_token(s);
969 /* ----------------------------------------------------------------------- */
970 /* This function takes a bookmark and parses it. This function also */
971 /* validates the bookmark. The parse_filenames flag indicates whether */
972 /* the filename tokens are to be extracted. */
973 /* ----------------------------------------------------------------------- */
974 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
976 const char* s = bookmark;
977 const char* end;
979 #define GET_INT_TOKEN(var) s = int_token(s, &var)
980 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
981 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
983 /* if new format bookmark, extract the optional content flags,
984 otherwise treat as an original format bookmark */
985 int opt_flags = 0;
986 bool new_format = (strchr(s, '>') == s);
987 if (new_format)
989 s++;
990 GET_INT_TOKEN(opt_flags);
993 /* extract all original bookmark tokens */
994 GET_INT_TOKEN(bm.resume_index);
995 GET_LONG_TOKEN(bm.resume_offset);
996 GET_INT_TOKEN(bm.resume_seed);
997 if (!new_format) /* skip deprecated token */
998 s = skip_token(s);
999 GET_LONG_TOKEN(bm.resume_time);
1000 GET_INT_TOKEN(bm.repeat_mode);
1001 GET_BOOL_TOKEN(bm.shuffle);
1003 /* extract all optional bookmark tokens */
1004 if (opt_flags & BM_PITCH)
1005 GET_INT_TOKEN(bm.pitch);
1006 if (opt_flags & BM_SPEED)
1007 GET_INT_TOKEN(bm.speed);
1009 if (*s == 0)
1011 return false;
1014 end = strchr(s, ';');
1016 /* extract file names */
1017 if (parse_filenames)
1019 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1020 len = MIN(TEMP_BUF_SIZE - 1, len);
1021 strlcpy(global_temp_buffer, s, len + 1);
1023 if (end != NULL)
1025 end++;
1026 strlcpy(global_filename, end, MAX_PATH);
1030 return true;
1033 /* ----------------------------------------------------------------------- */
1034 /* This function is used by multiple functions and is used to generate a */
1035 /* bookmark named based off of the input. */
1036 /* Changing this function could result in how the bookmarks are stored. */
1037 /* it would be here that the centralized/decentralized bookmark code */
1038 /* could be placed. */
1039 /* ----------------------------------------------------------------------- */
1040 static bool generate_bookmark_file_name(const char *in)
1042 int len = strlen(in);
1044 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1045 /* otherwise, name it based on the in variable */
1046 if (!strcmp("/", in))
1047 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1048 else
1050 #ifdef HAVE_MULTIVOLUME
1051 /* The "root" of an extra volume need special handling too. */
1052 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1053 !strcmp("/", global_bookmark_file_name));
1054 #endif
1056 strcpy(global_bookmark_file_name, in);
1057 if(global_bookmark_file_name[len-1] == '/')
1058 len--;
1059 #ifdef HAVE_MULTIVOLUME
1060 if (volume_root)
1061 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1062 else
1063 #endif
1064 strcpy(&global_bookmark_file_name[len], ".bmark");
1067 return true;
1070 /* ----------------------------------------------------------------------- */
1071 /* Returns true if a bookmark file exists for the current playlist */
1072 /* ----------------------------------------------------------------------- */
1073 bool bookmark_exists(void)
1075 bool exist=false;
1077 if(is_bookmarkable_state())
1079 char* name = playlist_get_name(NULL, global_temp_buffer,
1080 sizeof(global_temp_buffer));
1081 if (generate_bookmark_file_name(name))
1083 exist = file_exists(global_bookmark_file_name);
1087 return exist;
1090 /* ----------------------------------------------------------------------- */
1091 /* Checks the current state of the system and returns true if it is in a */
1092 /* bookmarkable state. */
1093 /* ----------------------------------------------------------------------- */
1094 static bool is_bookmarkable_state(void)
1096 int resume_index = 0;
1098 if (!(audio_status() && audio_current_track()) ||
1099 /* no track playing */
1100 (playlist_get_resume_info(&resume_index) == -1) ||
1101 /* invalid queue info */
1102 (playlist_modified(NULL)))
1103 /* can't bookmark while in the queue */
1105 return false;
1108 return true;