Fix some greedy sed changes in imported code. Also provide a sys/types.h for compatib...
[kugel-rb.git] / apps / bookmark.c
bloba3559be67b175d0cdfd3a7cf707a449299089d2e
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 system_check(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 (system_check())
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 = select_bookmark(RECENT_BOOKMARK_FILE, false);
154 if (bookmark != NULL)
156 return play_bookmark(bookmark);
159 return false;
162 /* ----------------------------------------------------------------------- */
163 /* This function handles an autobookmark creation. This is an interface */
164 /* function. */
165 /* ----------------------------------------------------------------------- */
166 bool bookmark_autobookmark(bool prompt_ok)
168 char* bookmark;
169 if (!system_check())
170 return false;
172 audio_pause(); /* first pause playback */
173 bookmark = create_bookmark();
174 /* Workaround for inability to speak when paused: all callers will
175 just do audio_stop() when we return, so we can do it right
176 away. This makes it possible to speak the "Create a Bookmark?"
177 prompt and the "Bookmark Created" splash. */
178 audio_stop();
179 switch (global_settings.autocreatebookmark)
181 case BOOKMARK_YES:
182 return write_bookmark(true, bookmark);
184 case BOOKMARK_NO:
185 return false;
187 case BOOKMARK_RECENT_ONLY_YES:
188 return write_bookmark(false, bookmark);
190 #ifdef HAVE_LCD_BITMAP
191 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
192 const struct text_message message={lines, 1};
193 #else
194 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
195 str(LANG_CONFIRM_WITH_BUTTON)};
196 const struct text_message message={lines, 2};
197 #endif
199 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
201 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
202 return write_bookmark(false, bookmark);
203 else
204 return write_bookmark(true, bookmark);
206 return false;
209 /* ----------------------------------------------------------------------- */
210 /* This function takes the current current resume information and writes */
211 /* that to the beginning of the bookmark file. */
212 /* This file will contain N number of bookmarks in the following format: */
213 /* resume_index*resume_offset*resume_seed*resume_first_index* */
214 /* resume_file*milliseconds*MP3 Title* */
215 /* ------------------------------------------------------------------------*/
216 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
218 bool success=false;
219 if (!bookmark)
220 return false; /* something didn't happen correctly, do nothing */
222 if (global_settings.usemrb)
223 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
226 /* writing the bookmark */
227 if (create_bookmark_file)
229 char* name = playlist_get_name(NULL, global_temp_buffer,
230 sizeof(global_temp_buffer));
231 if (generate_bookmark_file_name(name))
233 success = add_bookmark(global_bookmark_file_name, bookmark, false);
237 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
238 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
240 return true;
243 /* ----------------------------------------------------------------------- */
244 /* This function adds a bookmark to a file. */
245 /* ------------------------------------------------------------------------*/
246 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
247 bool most_recent)
249 int temp_bookmark_file = 0;
250 int bookmark_file = 0;
251 int bookmark_count = 0;
252 char* playlist = NULL;
253 char* cp;
254 char* tmp;
255 int len = 0;
256 bool unique = false;
258 /* Opening up a temp bookmark file */
259 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
260 "%s.tmp", bookmark_file_name);
261 temp_bookmark_file = open(global_temp_buffer,
262 O_WRONLY | O_CREAT | O_TRUNC);
263 if (temp_bookmark_file < 0)
264 return false; /* can't open the temp file */
266 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
268 playlist = strchr(bookmark,'/');
269 cp = strrchr(bookmark,';');
270 len = cp - playlist;
271 unique = true;
274 /* Writing the new bookmark to the begining of the temp file */
275 write(temp_bookmark_file, bookmark, strlen(bookmark));
276 write(temp_bookmark_file, "\n", 1);
277 bookmark_count++;
279 /* Reading in the previous bookmarks and writing them to the temp file */
280 bookmark_file = open(bookmark_file_name, O_RDONLY);
281 if (bookmark_file >= 0)
283 while (read_line(bookmark_file, global_read_buffer,
284 sizeof(global_read_buffer)) > 0)
286 /* The MRB has a max of MAX_BOOKMARKS in it */
287 /* This keeps it from getting too large */
288 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
289 break;
291 cp = strchr(global_read_buffer,'/');
292 tmp = strrchr(global_read_buffer,';');
293 if (parse_bookmark(global_read_buffer, false) &&
294 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
296 bookmark_count++;
297 write(temp_bookmark_file, global_read_buffer,
298 strlen(global_read_buffer));
299 write(temp_bookmark_file, "\n", 1);
302 close(bookmark_file);
304 close(temp_bookmark_file);
306 remove(bookmark_file_name);
307 rename(global_temp_buffer, bookmark_file_name);
309 return true;
313 /* ----------------------------------------------------------------------- */
314 /* This function takes the system resume data and formats it into a valid */
315 /* bookmark. */
316 /* ----------------------------------------------------------------------- */
317 static char* create_bookmark()
319 int resume_index = 0;
320 char *file;
322 if (!system_check())
323 return NULL; /* something didn't happen correctly, do nothing */
325 /* grab the currently playing track */
326 struct mp3entry *id3 = audio_current_track();
327 if(!id3)
328 return NULL;
330 /* Get some basic resume information */
331 /* queue_resume and queue_resume_index are not used and can be ignored.*/
332 playlist_get_resume_info(&resume_index);
334 /* Get the currently playing file minus the path */
335 /* This is used when displaying the available bookmarks */
336 file = strrchr(id3->path,'/');
337 if(NULL == file)
338 return NULL;
340 /* create the bookmark */
341 snprintf(global_bookmark, sizeof(global_bookmark),
342 /* new optional bookmark token descriptors should be inserted
343 just before the "%s;%s" in this line... */
344 #if CONFIG_CODEC == SWCODEC
345 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
346 #else
347 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
348 #endif
349 /* ... their flags should go here ... */
350 #if CONFIG_CODEC == SWCODEC
351 BM_PITCH | BM_SPEED,
352 #else
354 #endif
355 resume_index,
356 id3->offset,
357 playlist_get_seed(NULL),
358 id3->elapsed,
359 global_settings.repeat_mode,
360 global_settings.playlist_shuffle,
361 /* ...and their values should go here */
362 #if CONFIG_CODEC == SWCODEC
363 (long)sound_get_pitch(),
364 (long)dsp_get_timestretch(),
365 #endif
366 /* more mandatory tokens */
367 playlist_get_name(NULL, global_temp_buffer,
368 sizeof(global_temp_buffer)),
369 file+1);
371 /* checking to see if the bookmark is valid */
372 if (parse_bookmark(global_bookmark, false))
373 return global_bookmark;
374 else
375 return NULL;
378 /* ----------------------------------------------------------------------- */
379 /* This function will determine if an autoload is necessary. This is an */
380 /* interface function. */
381 /* ------------------------------------------------------------------------*/
382 bool bookmark_autoload(const char* file)
384 if(global_settings.autoloadbookmark == BOOKMARK_NO)
385 return false;
387 /*Checking to see if a bookmark file exists.*/
388 if(!generate_bookmark_file_name(file))
390 return false;
393 if(!file_exists(global_bookmark_file_name))
394 return false;
396 if(global_settings.autoloadbookmark == BOOKMARK_YES)
398 return bookmark_load(global_bookmark_file_name, true);
400 else
402 char* bookmark = select_bookmark(global_bookmark_file_name, true);
404 if (bookmark != NULL)
406 if (!play_bookmark(bookmark))
408 /* Selected bookmark not found. */
409 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
412 /* Act as if autoload was done even if it failed, since the
413 * user did make an active selection.
415 return true;
418 return false;
422 /* ----------------------------------------------------------------------- */
423 /* This function loads the bookmark information into the resume memory. */
424 /* This is an interface function. */
425 /* ------------------------------------------------------------------------*/
426 bool bookmark_load(const char* file, bool autoload)
428 int fd;
429 char* bookmark = NULL;
431 if(autoload)
433 fd = open(file, O_RDONLY);
434 if(fd >= 0)
436 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
437 bookmark=global_read_buffer;
438 close(fd);
441 else
443 /* This is not an auto-load, so list the bookmarks */
444 bookmark = select_bookmark(file, false);
447 if (bookmark != NULL)
449 if (!play_bookmark(bookmark))
451 /* Selected bookmark not found. */
452 if (!autoload)
454 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
457 return false;
461 return true;
465 static int get_bookmark_count(const char* bookmark_file_name)
467 int read_count = 0;
468 int file = open(bookmark_file_name, O_RDONLY);
470 if(file < 0)
471 return -1;
473 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
475 read_count++;
478 close(file);
479 return read_count;
482 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
484 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
485 int read_count = 0;
486 int file = open(bookmarks->filename, O_RDONLY);
488 if (file < 0)
490 return -1;
493 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
494 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
496 /* Entire file fits in buffer */
497 first_line = 0;
500 bookmarks->start = first_line;
501 bookmarks->count = 0;
502 bookmarks->reload = false;
504 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
506 read_count++;
508 if (read_count >= first_line)
510 dest -= strlen(global_read_buffer) + 1;
512 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
513 + (sizeof(char*) * (bookmarks->count + 1)))
515 break;
518 strcpy(dest, global_read_buffer);
519 bookmarks->items[bookmarks->count] = dest;
520 bookmarks->count++;
524 close(file);
525 return bookmarks->start + bookmarks->count;
528 static const char* get_bookmark_info(int list_index,
529 void* data,
530 char *buffer,
531 size_t buffer_len)
533 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
534 int index = list_index / 2;
536 if (bookmarks->show_dont_resume)
538 if (index == 0)
540 return list_index % 2 == 0
541 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
544 index--;
547 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
548 || (index < bookmarks->start))
550 int read_index = index;
552 /* Using count as a guide on how far to move could possibly fail
553 * sometimes. Use byte count if that is a problem?
556 if (read_index != 0)
558 /* Move count * 3 / 4 items in the direction the user is moving,
559 * but don't go too close to the end.
561 int offset = bookmarks->count;
562 int max = bookmarks->total_count - (bookmarks->count / 2);
564 if (read_index < bookmarks->start)
566 offset *= 3;
569 read_index = index - offset / 4;
571 if (read_index > max)
573 read_index = max;
576 if (read_index < 0)
578 read_index = 0;
582 if (buffer_bookmarks(bookmarks, read_index) <= index)
584 return "";
588 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
590 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
593 if (list_index % 2 == 0)
595 char *name;
596 char *format;
597 int len = strlen(global_temp_buffer);
599 if (bookmarks->show_playlist_name && len > 0)
601 name = global_temp_buffer;
602 len--;
604 if (name[len] != '/')
606 strrsplt(name, '.');
608 else if (len > 1)
610 name[len] = '\0';
613 if (len > 1)
615 name = strrsplt(name, '/');
618 format = "%s : %s";
620 else
622 name = global_filename;
623 format = "%s";
626 strrsplt(global_filename, '.');
627 snprintf(buffer, buffer_len, format, name, global_filename);
628 return buffer;
630 else
632 char time_buf[32];
634 format_time(time_buf, sizeof(time_buf), bm.resume_time);
635 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
636 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
637 return buffer;
641 static int bookmark_list_voice_cb(int list_index, void* data)
643 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
644 int index = list_index / 2;
646 if (bookmarks->show_dont_resume)
648 if (index == 0)
649 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
650 index--;
652 say_bookmark(bookmarks->items[index - bookmarks->start], index,
653 bookmarks->show_playlist_name);
654 return 0;
657 /* ----------------------------------------------------------------------- */
658 /* This displays a the bookmarks in a file and allows the user to */
659 /* select one to play. */
660 /* ------------------------------------------------------------------------*/
661 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
663 struct bookmark_list* bookmarks;
664 struct gui_synclist list;
665 int item = 0;
666 int action;
667 size_t size;
668 bool exit = false;
669 bool refresh = true;
671 bookmarks = plugin_get_buffer(&size);
672 bookmarks->buffer_size = size;
673 bookmarks->show_dont_resume = show_dont_resume;
674 bookmarks->filename = bookmark_file_name;
675 bookmarks->start = 0;
676 bookmarks->show_playlist_name
677 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
678 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
679 if(global_settings.talk_menu)
680 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
681 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
682 Icon_Bookmark);
684 while (!exit)
687 if (refresh)
689 int count = get_bookmark_count(bookmark_file_name);
690 bookmarks->total_count = count;
692 if (bookmarks->total_count < 1)
694 /* No more bookmarks, delete file and exit */
695 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
696 remove(bookmark_file_name);
697 return NULL;
700 if (bookmarks->show_dont_resume)
702 count++;
703 item++;
706 gui_synclist_set_nb_items(&list, count * 2);
708 if (item >= count)
710 /* Selected item has been deleted */
711 item = count - 1;
712 gui_synclist_select_item(&list, item * 2);
715 buffer_bookmarks(bookmarks, bookmarks->start);
716 gui_synclist_draw(&list);
717 cond_talk_ids_fq(VOICE_EXT_BMARK);
718 gui_synclist_speak_item(&list);
719 refresh = false;
722 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
723 &list, &action, LIST_WRAP_UNLESS_HELD);
724 item = gui_synclist_get_sel_pos(&list) / 2;
726 if (bookmarks->show_dont_resume)
728 item--;
731 if (action == ACTION_STD_CONTEXT)
733 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
734 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
735 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
736 static const int menu_actions[] =
738 ACTION_STD_OK, ACTION_BMS_DELETE
740 int selection = do_menu(&menu_items, NULL, NULL, false);
742 refresh = true;
744 if (selection >= 0 && selection <=
745 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
747 action = menu_actions[selection];
751 switch (action)
753 case ACTION_STD_OK:
754 if (item >= 0)
756 talk_shutup();
757 return bookmarks->items[item - bookmarks->start];
760 /* Else fall through */
762 case ACTION_TREE_WPS:
763 case ACTION_STD_CANCEL:
764 exit = true;
765 break;
767 case ACTION_BMS_DELETE:
768 if (item >= 0)
770 const char *lines[]={
771 ID2P(LANG_REALLY_DELETE)
773 const char *yes_lines[]={
774 ID2P(LANG_DELETING)
777 const struct text_message message={lines, 1};
778 const struct text_message yes_message={yes_lines, 1};
780 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
782 delete_bookmark(bookmark_file_name, item);
783 bookmarks->reload = true;
785 refresh = true;
787 break;
789 default:
790 if (default_event_handler(action) == SYS_USB_CONNECTED)
792 exit = true;
795 break;
799 talk_shutup();
800 return NULL;
803 /* ----------------------------------------------------------------------- */
804 /* This function takes a location in a bookmark file and deletes that */
805 /* bookmark. */
806 /* ------------------------------------------------------------------------*/
807 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
809 int temp_bookmark_file = 0;
810 int bookmark_file = 0;
811 int bookmark_count = 0;
813 /* Opening up a temp bookmark file */
814 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
815 "%s.tmp", bookmark_file_name);
816 temp_bookmark_file = open(global_temp_buffer,
817 O_WRONLY | O_CREAT | O_TRUNC);
819 if (temp_bookmark_file < 0)
820 return false; /* can't open the temp file */
822 /* Reading in the previous bookmarks and writing them to the temp file */
823 bookmark_file = open(bookmark_file_name, O_RDONLY);
824 if (bookmark_file >= 0)
826 while (read_line(bookmark_file, global_read_buffer,
827 sizeof(global_read_buffer)) > 0)
829 if (bookmark_id != bookmark_count)
831 write(temp_bookmark_file, global_read_buffer,
832 strlen(global_read_buffer));
833 write(temp_bookmark_file, "\n", 1);
835 bookmark_count++;
837 close(bookmark_file);
839 close(temp_bookmark_file);
841 remove(bookmark_file_name);
842 rename(global_temp_buffer, bookmark_file_name);
844 return true;
847 /* ----------------------------------------------------------------------- */
848 /* This function parses a bookmark, says the voice UI part of it. */
849 /* ------------------------------------------------------------------------*/
850 static void say_bookmark(const char* bookmark,
851 int bookmark_id, bool show_playlist_name)
853 bool is_dir;
855 if (!parse_bookmark(bookmark, true))
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(bm.shuffle)
882 talk_id(LANG_SHUFFLE, true);
884 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
885 talk_number(bm.resume_index + 1, true);
886 talk_id(LANG_TIME, true);
887 talk_value(bm.resume_time / 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 #if CONFIG_CODEC == SWCODEC
910 /* preset pitch and speed to 100% in case bookmark doesn't have info */
911 bm.pitch = sound_get_pitch();
912 bm.speed = dsp_get_timestretch();
913 #endif
915 if (parse_bookmark(bookmark, true))
917 global_settings.repeat_mode = bm.repeat_mode;
918 global_settings.playlist_shuffle = bm.shuffle;
919 #if CONFIG_CODEC == SWCODEC
920 sound_set_pitch(bm.pitch);
921 dsp_set_timestretch(bm.speed);
922 #endif
923 return bookmark_play(global_temp_buffer, bm.resume_index,
924 bm.resume_offset, bm.resume_seed, global_filename);
927 return false;
930 static const char* skip_token(const char* s)
932 while (*s && *s != ';')
934 s++;
937 if (*s)
939 s++;
942 return s;
945 static const char* int_token(const char* s, int* dest)
947 *dest = atoi(s);
948 return skip_token(s);
951 static const char* long_token(const char* s, long* dest)
953 *dest = atoi(s); /* Should be atol, but we don't have it. */
954 return skip_token(s);
957 /* ----------------------------------------------------------------------- */
958 /* This function takes a bookmark and parses it. This function also */
959 /* validates the bookmark. The parse_filenames flag indicates whether */
960 /* the filename tokens are to be extracted. */
961 /* ----------------------------------------------------------------------- */
962 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
964 const char* s = bookmark;
965 const char* end;
967 #define GET_INT_TOKEN(var) s = int_token(s, &var)
968 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
969 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
971 /* if new format bookmark, extract the optional content flags,
972 otherwise treat as an original format bookmark */
973 int opt_flags = 0;
974 bool new_format = (strchr(s, '>') == s);
975 if (new_format)
977 s++;
978 GET_INT_TOKEN(opt_flags);
981 /* extract all original bookmark tokens */
982 GET_INT_TOKEN(bm.resume_index);
983 GET_LONG_TOKEN(bm.resume_offset);
984 GET_INT_TOKEN(bm.resume_seed);
985 if (!new_format) /* skip deprecated token */
986 s = skip_token(s);
987 GET_LONG_TOKEN(bm.resume_time);
988 GET_INT_TOKEN(bm.repeat_mode);
989 GET_BOOL_TOKEN(bm.shuffle);
991 /* extract all optional bookmark tokens */
992 if (opt_flags & BM_PITCH)
993 GET_INT_TOKEN(bm.pitch);
994 if (opt_flags & BM_SPEED)
995 GET_INT_TOKEN(bm.speed);
997 if (*s == 0)
999 return false;
1002 end = strchr(s, ';');
1004 /* extract file names */
1005 if (parse_filenames)
1007 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1008 len = MIN(TEMP_BUF_SIZE - 1, len);
1009 strlcpy(global_temp_buffer, s, len + 1);
1011 if (end != NULL)
1013 end++;
1014 strlcpy(global_filename, end, MAX_PATH);
1018 return true;
1021 /* ----------------------------------------------------------------------- */
1022 /* This function is used by multiple functions and is used to generate a */
1023 /* bookmark named based off of the input. */
1024 /* Changing this function could result in how the bookmarks are stored. */
1025 /* it would be here that the centralized/decentralized bookmark code */
1026 /* could be placed. */
1027 /* ----------------------------------------------------------------------- */
1028 static bool generate_bookmark_file_name(const char *in)
1030 int len = strlen(in);
1032 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1033 /* otherwise, name it based on the in variable */
1034 if (!strcmp("/", in))
1035 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1036 else
1038 #ifdef HAVE_MULTIVOLUME
1039 /* The "root" of an extra volume need special handling too. */
1040 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1041 !strcmp("/", global_bookmark_file_name));
1042 #endif
1044 strcpy(global_bookmark_file_name, in);
1045 if(global_bookmark_file_name[len-1] == '/')
1046 len--;
1047 #ifdef HAVE_MULTIVOLUME
1048 if (volume_root)
1049 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1050 else
1051 #endif
1052 strcpy(&global_bookmark_file_name[len], ".bmark");
1055 return true;
1058 /* ----------------------------------------------------------------------- */
1059 /* Returns true if a bookmark file exists for the current playlist */
1060 /* ----------------------------------------------------------------------- */
1061 bool bookmark_exist(void)
1063 bool exist=false;
1065 if(system_check())
1067 char* name = playlist_get_name(NULL, global_temp_buffer,
1068 sizeof(global_temp_buffer));
1069 if (generate_bookmark_file_name(name))
1071 exist = file_exists(global_bookmark_file_name);
1075 return exist;
1078 /* ----------------------------------------------------------------------- */
1079 /* Checks the current state of the system and returns true if it is in a */
1080 /* bookmarkable state. */
1081 /* ----------------------------------------------------------------------- */
1082 static bool system_check(void)
1084 int resume_index = 0;
1086 if (!(audio_status() && audio_current_track()) ||
1087 /* no track playing */
1088 (playlist_get_resume_info(&resume_index) == -1) ||
1089 /* invalid queue info */
1090 (playlist_modified(NULL)))
1091 /* can't bookmark while in the queue */
1093 return false;
1096 return true;