Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / bookmark.c
blob8de06999f43b1ad981b836b8da685db99a4ac478
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 = 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 bool update;
171 if (!is_bookmarkable_state())
172 return false;
174 audio_pause(); /* first pause playback */
175 update = (global_settings.autoupdatebookmark && bookmark_exists());
176 bookmark = create_bookmark();
177 /* Workaround for inability to speak when paused: all callers will
178 just do audio_stop() when we return, so we can do it right
179 away. This makes it possible to speak the "Create a Bookmark?"
180 prompt and the "Bookmark Created" splash. */
181 audio_stop();
183 if (update)
184 return write_bookmark(true, bookmark);
186 switch (global_settings.autocreatebookmark)
188 case BOOKMARK_YES:
189 return write_bookmark(true, bookmark);
191 case BOOKMARK_NO:
192 return false;
194 case BOOKMARK_RECENT_ONLY_YES:
195 return write_bookmark(false, bookmark);
197 #ifdef HAVE_LCD_BITMAP
198 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
199 const struct text_message message={lines, 1};
200 #else
201 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
202 str(LANG_CONFIRM_WITH_BUTTON)};
203 const struct text_message message={lines, 2};
204 #endif
206 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
208 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
209 return write_bookmark(false, bookmark);
210 else
211 return write_bookmark(true, bookmark);
213 return false;
216 /* ----------------------------------------------------------------------- */
217 /* This function takes the current current resume information and writes */
218 /* that to the beginning of the bookmark file. */
219 /* This file will contain N number of bookmarks in the following format: */
220 /* resume_index*resume_offset*resume_seed*resume_first_index* */
221 /* resume_file*milliseconds*MP3 Title* */
222 /* ------------------------------------------------------------------------*/
223 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
225 bool success=false;
226 if (!bookmark)
227 return false; /* something didn't happen correctly, do nothing */
229 if (global_settings.usemrb)
230 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
233 /* writing the bookmark */
234 if (create_bookmark_file)
236 char* name = playlist_get_name(NULL, global_temp_buffer,
237 sizeof(global_temp_buffer));
238 if (generate_bookmark_file_name(name))
240 success = add_bookmark(global_bookmark_file_name, bookmark, false);
244 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
245 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
247 return true;
250 /* ----------------------------------------------------------------------- */
251 /* This function adds a bookmark to a file. */
252 /* ------------------------------------------------------------------------*/
253 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
254 bool most_recent)
256 int temp_bookmark_file = 0;
257 int bookmark_file = 0;
258 int bookmark_count = 0;
259 char* playlist = NULL;
260 char* cp;
261 char* tmp;
262 int len = 0;
263 bool unique = false;
265 /* Opening up a temp bookmark file */
266 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
267 "%s.tmp", bookmark_file_name);
268 temp_bookmark_file = open(global_temp_buffer,
269 O_WRONLY | O_CREAT | O_TRUNC, 0666);
270 if (temp_bookmark_file < 0)
271 return false; /* can't open the temp file */
273 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
275 playlist = strchr(bookmark,'/');
276 cp = strrchr(bookmark,';');
277 len = cp - playlist;
278 unique = true;
281 /* Writing the new bookmark to the begining of the temp file */
282 write(temp_bookmark_file, bookmark, strlen(bookmark));
283 write(temp_bookmark_file, "\n", 1);
284 bookmark_count++;
286 /* Reading in the previous bookmarks and writing them to the temp file */
287 bookmark_file = open(bookmark_file_name, O_RDONLY);
288 if (bookmark_file >= 0)
290 while (read_line(bookmark_file, global_read_buffer,
291 sizeof(global_read_buffer)) > 0)
293 /* The MRB has a max of MAX_BOOKMARKS in it */
294 /* This keeps it from getting too large */
295 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
296 break;
298 cp = strchr(global_read_buffer,'/');
299 tmp = strrchr(global_read_buffer,';');
300 if (parse_bookmark(global_read_buffer, false) &&
301 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
303 bookmark_count++;
304 write(temp_bookmark_file, global_read_buffer,
305 strlen(global_read_buffer));
306 write(temp_bookmark_file, "\n", 1);
309 close(bookmark_file);
311 close(temp_bookmark_file);
313 remove(bookmark_file_name);
314 rename(global_temp_buffer, bookmark_file_name);
316 return true;
320 /* ----------------------------------------------------------------------- */
321 /* This function takes the system resume data and formats it into a valid */
322 /* bookmark. */
323 /* ----------------------------------------------------------------------- */
324 static char* create_bookmark()
326 int resume_index = 0;
327 char *file;
329 if (!is_bookmarkable_state())
330 return NULL; /* something didn't happen correctly, do nothing */
332 /* grab the currently playing track */
333 struct mp3entry *id3 = audio_current_track();
334 if(!id3)
335 return NULL;
337 /* Get some basic resume information */
338 /* queue_resume and queue_resume_index are not used and can be ignored.*/
339 playlist_get_resume_info(&resume_index);
341 /* Get the currently playing file minus the path */
342 /* This is used when displaying the available bookmarks */
343 file = strrchr(id3->path,'/');
344 if(NULL == file)
345 return NULL;
347 /* create the bookmark */
348 snprintf(global_bookmark, sizeof(global_bookmark),
349 /* new optional bookmark token descriptors should be inserted
350 just before the "%s;%s" in this line... */
351 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
352 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
353 #else
354 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
355 #endif
356 /* ... their flags should go here ... */
357 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
358 BM_PITCH | BM_SPEED,
359 #else
361 #endif
362 resume_index,
363 id3->offset,
364 playlist_get_seed(NULL),
365 id3->elapsed,
366 global_settings.repeat_mode,
367 global_settings.playlist_shuffle,
368 /* ...and their values should go here */
369 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
370 (long)sound_get_pitch(),
371 (long)dsp_get_timestretch(),
372 #endif
373 /* more mandatory tokens */
374 playlist_get_name(NULL, global_temp_buffer,
375 sizeof(global_temp_buffer)),
376 file+1);
378 /* checking to see if the bookmark is valid */
379 if (parse_bookmark(global_bookmark, false))
380 return global_bookmark;
381 else
382 return NULL;
385 /* ----------------------------------------------------------------------- */
386 /* This function will determine if an autoload is necessary. This is an */
387 /* interface function. */
388 /* ------------------------------------------------------------------------*/
389 bool bookmark_autoload(const char* file)
391 if(global_settings.autoloadbookmark == BOOKMARK_NO)
392 return false;
394 /*Checking to see if a bookmark file exists.*/
395 if(!generate_bookmark_file_name(file))
397 return false;
400 if(!file_exists(global_bookmark_file_name))
401 return false;
403 if(global_settings.autoloadbookmark == BOOKMARK_YES)
405 return bookmark_load(global_bookmark_file_name, true);
407 else
409 char* bookmark = select_bookmark(global_bookmark_file_name, true);
411 if (bookmark != NULL)
413 if (!play_bookmark(bookmark))
415 /* Selected bookmark not found. */
416 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
419 /* Act as if autoload was done even if it failed, since the
420 * user did make an active selection.
422 return true;
425 return false;
429 /* ----------------------------------------------------------------------- */
430 /* This function loads the bookmark information into the resume memory. */
431 /* This is an interface function. */
432 /* ------------------------------------------------------------------------*/
433 bool bookmark_load(const char* file, bool autoload)
435 int fd;
436 char* bookmark = NULL;
438 if(autoload)
440 fd = open(file, O_RDONLY);
441 if(fd >= 0)
443 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
444 bookmark=global_read_buffer;
445 close(fd);
448 else
450 /* This is not an auto-load, so list the bookmarks */
451 bookmark = select_bookmark(file, false);
454 if (bookmark != NULL)
456 if (!play_bookmark(bookmark))
458 /* Selected bookmark not found. */
459 if (!autoload)
461 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
464 return false;
468 return true;
472 static int get_bookmark_count(const char* bookmark_file_name)
474 int read_count = 0;
475 int file = open(bookmark_file_name, O_RDONLY);
477 if(file < 0)
478 return -1;
480 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
482 read_count++;
485 close(file);
486 return read_count;
489 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
491 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
492 int read_count = 0;
493 int file = open(bookmarks->filename, O_RDONLY);
495 if (file < 0)
497 return -1;
500 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
501 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
503 /* Entire file fits in buffer */
504 first_line = 0;
507 bookmarks->start = first_line;
508 bookmarks->count = 0;
509 bookmarks->reload = false;
511 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
513 read_count++;
515 if (read_count >= first_line)
517 dest -= strlen(global_read_buffer) + 1;
519 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
520 + (sizeof(char*) * (bookmarks->count + 1)))
522 break;
525 strcpy(dest, global_read_buffer);
526 bookmarks->items[bookmarks->count] = dest;
527 bookmarks->count++;
531 close(file);
532 return bookmarks->start + bookmarks->count;
535 static const char* get_bookmark_info(int list_index,
536 void* data,
537 char *buffer,
538 size_t buffer_len)
540 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
541 int index = list_index / 2;
543 if (bookmarks->show_dont_resume)
545 if (index == 0)
547 return list_index % 2 == 0
548 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
551 index--;
554 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
555 || (index < bookmarks->start))
557 int read_index = index;
559 /* Using count as a guide on how far to move could possibly fail
560 * sometimes. Use byte count if that is a problem?
563 if (read_index != 0)
565 /* Move count * 3 / 4 items in the direction the user is moving,
566 * but don't go too close to the end.
568 int offset = bookmarks->count;
569 int max = bookmarks->total_count - (bookmarks->count / 2);
571 if (read_index < bookmarks->start)
573 offset *= 3;
576 read_index = index - offset / 4;
578 if (read_index > max)
580 read_index = max;
583 if (read_index < 0)
585 read_index = 0;
589 if (buffer_bookmarks(bookmarks, read_index) <= index)
591 return "";
595 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
597 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
600 if (list_index % 2 == 0)
602 char *name;
603 char *format;
604 int len = strlen(global_temp_buffer);
606 if (bookmarks->show_playlist_name && len > 0)
608 name = global_temp_buffer;
609 len--;
611 if (name[len] != '/')
613 strrsplt(name, '.');
615 else if (len > 1)
617 name[len] = '\0';
620 if (len > 1)
622 name = strrsplt(name, '/');
625 format = "%s : %s";
627 else
629 name = global_filename;
630 format = "%s";
633 strrsplt(global_filename, '.');
634 snprintf(buffer, buffer_len, format, name, global_filename);
635 return buffer;
637 else
639 char time_buf[32];
641 format_time(time_buf, sizeof(time_buf), bm.resume_time);
642 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
643 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
644 return buffer;
648 static int bookmark_list_voice_cb(int list_index, void* data)
650 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
651 int index = list_index / 2;
653 if (bookmarks->show_dont_resume)
655 if (index == 0)
656 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
657 index--;
659 say_bookmark(bookmarks->items[index - bookmarks->start], index,
660 bookmarks->show_playlist_name);
661 return 0;
664 /* ----------------------------------------------------------------------- */
665 /* This displays a the bookmarks in a file and allows the user to */
666 /* select one to play. */
667 /* ------------------------------------------------------------------------*/
668 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
670 struct bookmark_list* bookmarks;
671 struct gui_synclist list;
672 int item = 0;
673 int action;
674 size_t size;
675 bool exit = false;
676 bool refresh = true;
678 bookmarks = plugin_get_buffer(&size);
679 bookmarks->buffer_size = size;
680 bookmarks->show_dont_resume = show_dont_resume;
681 bookmarks->filename = bookmark_file_name;
682 bookmarks->start = 0;
683 bookmarks->show_playlist_name
684 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
685 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
686 if(global_settings.talk_menu)
687 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
688 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
689 Icon_Bookmark);
691 while (!exit)
694 if (refresh)
696 int count = get_bookmark_count(bookmark_file_name);
697 bookmarks->total_count = count;
699 if (bookmarks->total_count < 1)
701 /* No more bookmarks, delete file and exit */
702 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
703 remove(bookmark_file_name);
704 return NULL;
707 if (bookmarks->show_dont_resume)
709 count++;
710 item++;
713 gui_synclist_set_nb_items(&list, count * 2);
715 if (item >= count)
717 /* Selected item has been deleted */
718 item = count - 1;
719 gui_synclist_select_item(&list, item * 2);
722 buffer_bookmarks(bookmarks, bookmarks->start);
723 gui_synclist_draw(&list);
724 cond_talk_ids_fq(VOICE_EXT_BMARK);
725 gui_synclist_speak_item(&list);
726 refresh = false;
729 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
730 &list, &action, LIST_WRAP_UNLESS_HELD);
731 item = gui_synclist_get_sel_pos(&list) / 2;
733 if (bookmarks->show_dont_resume)
735 item--;
738 if (action == ACTION_STD_CONTEXT)
740 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
741 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
742 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
743 static const int menu_actions[] =
745 ACTION_STD_OK, ACTION_BMS_DELETE
747 int selection = do_menu(&menu_items, NULL, NULL, false);
749 refresh = true;
751 if (selection >= 0 && selection <=
752 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
754 action = menu_actions[selection];
758 switch (action)
760 case ACTION_STD_OK:
761 if (item >= 0)
763 talk_shutup();
764 return bookmarks->items[item - bookmarks->start];
767 /* Else fall through */
769 case ACTION_TREE_WPS:
770 case ACTION_STD_CANCEL:
771 exit = true;
772 break;
774 case ACTION_BMS_DELETE:
775 if (item >= 0)
777 const char *lines[]={
778 ID2P(LANG_REALLY_DELETE)
780 const char *yes_lines[]={
781 ID2P(LANG_DELETING)
784 const struct text_message message={lines, 1};
785 const struct text_message yes_message={yes_lines, 1};
787 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
789 delete_bookmark(bookmark_file_name, item);
790 bookmarks->reload = true;
792 refresh = true;
794 break;
796 default:
797 if (default_event_handler(action) == SYS_USB_CONNECTED)
799 exit = true;
802 break;
806 talk_shutup();
807 return NULL;
810 /* ----------------------------------------------------------------------- */
811 /* This function takes a location in a bookmark file and deletes that */
812 /* bookmark. */
813 /* ------------------------------------------------------------------------*/
814 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
816 int temp_bookmark_file = 0;
817 int bookmark_file = 0;
818 int bookmark_count = 0;
820 /* Opening up a temp bookmark file */
821 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
822 "%s.tmp", bookmark_file_name);
823 temp_bookmark_file = open(global_temp_buffer,
824 O_WRONLY | O_CREAT | O_TRUNC, 0666);
826 if (temp_bookmark_file < 0)
827 return false; /* can't open the temp file */
829 /* Reading in the previous bookmarks and writing them to the temp file */
830 bookmark_file = open(bookmark_file_name, O_RDONLY);
831 if (bookmark_file >= 0)
833 while (read_line(bookmark_file, global_read_buffer,
834 sizeof(global_read_buffer)) > 0)
836 if (bookmark_id != bookmark_count)
838 write(temp_bookmark_file, global_read_buffer,
839 strlen(global_read_buffer));
840 write(temp_bookmark_file, "\n", 1);
842 bookmark_count++;
844 close(bookmark_file);
846 close(temp_bookmark_file);
848 remove(bookmark_file_name);
849 rename(global_temp_buffer, bookmark_file_name);
851 return true;
854 /* ----------------------------------------------------------------------- */
855 /* This function parses a bookmark, says the voice UI part of it. */
856 /* ------------------------------------------------------------------------*/
857 static void say_bookmark(const char* bookmark,
858 int bookmark_id, bool show_playlist_name)
860 bool is_dir;
862 if (!parse_bookmark(bookmark, true))
864 talk_id(LANG_BOOKMARK_INVALID, false);
865 return;
868 talk_number(bookmark_id + 1, false);
870 is_dir = (global_temp_buffer[0]
871 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
872 #if CONFIG_CODEC == SWCODEC
873 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
874 together, because there is no guarantee that the same mp3
875 parameters are used. */
876 if(show_playlist_name)
877 { /* It's useful to know which playlist this is */
878 if(is_dir)
879 talk_dir_or_spell(global_temp_buffer,
880 TALK_IDARRAY(VOICE_DIR), true);
881 else talk_file_or_spell(NULL, global_temp_buffer,
882 TALK_IDARRAY(LANG_PLAYLIST), true);
884 #else
885 (void)show_playlist_name;
886 #endif
888 if(bm.shuffle)
889 talk_id(LANG_SHUFFLE, true);
891 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
892 talk_number(bm.resume_index + 1, true);
893 talk_id(LANG_TIME, true);
894 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
896 #if CONFIG_CODEC == SWCODEC
897 /* Track filename */
898 if(is_dir)
899 talk_file_or_spell(global_temp_buffer, global_filename,
900 TALK_IDARRAY(VOICE_FILE), true);
901 else
902 { /* Unfortunately if this is a playlist, we do not know in which
903 directory the file is and therefore cannot find the track's
904 .talk file. */
905 talk_id(VOICE_FILE, true);
906 talk_spell(global_filename, true);
908 #endif
911 /* ----------------------------------------------------------------------- */
912 /* This function parses a bookmark and then plays it. */
913 /* ------------------------------------------------------------------------*/
914 static bool play_bookmark(const char* bookmark)
916 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
917 /* preset pitch and speed to 100% in case bookmark doesn't have info */
918 bm.pitch = sound_get_pitch();
919 bm.speed = dsp_get_timestretch();
920 #endif
922 if (parse_bookmark(bookmark, true))
924 global_settings.repeat_mode = bm.repeat_mode;
925 global_settings.playlist_shuffle = bm.shuffle;
926 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
927 sound_set_pitch(bm.pitch);
928 dsp_set_timestretch(bm.speed);
929 #endif
930 if (!warn_on_pl_erase())
931 return false;
932 return bookmark_play(global_temp_buffer, bm.resume_index,
933 bm.resume_offset, bm.resume_seed, global_filename);
936 return false;
939 static const char* skip_token(const char* s)
941 while (*s && *s != ';')
943 s++;
946 if (*s)
948 s++;
951 return s;
954 static const char* int_token(const char* s, int* dest)
956 *dest = atoi(s);
957 return skip_token(s);
960 static const char* long_token(const char* s, long* dest)
962 *dest = atoi(s); /* Should be atol, but we don't have it. */
963 return skip_token(s);
966 /* ----------------------------------------------------------------------- */
967 /* This function takes a bookmark and parses it. This function also */
968 /* validates the bookmark. The parse_filenames flag indicates whether */
969 /* the filename tokens are to be extracted. */
970 /* ----------------------------------------------------------------------- */
971 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
973 const char* s = bookmark;
974 const char* end;
976 #define GET_INT_TOKEN(var) s = int_token(s, &var)
977 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
978 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
980 /* if new format bookmark, extract the optional content flags,
981 otherwise treat as an original format bookmark */
982 int opt_flags = 0;
983 bool new_format = (strchr(s, '>') == s);
984 if (new_format)
986 s++;
987 GET_INT_TOKEN(opt_flags);
990 /* extract all original bookmark tokens */
991 GET_INT_TOKEN(bm.resume_index);
992 GET_LONG_TOKEN(bm.resume_offset);
993 GET_INT_TOKEN(bm.resume_seed);
994 if (!new_format) /* skip deprecated token */
995 s = skip_token(s);
996 GET_LONG_TOKEN(bm.resume_time);
997 GET_INT_TOKEN(bm.repeat_mode);
998 GET_BOOL_TOKEN(bm.shuffle);
1000 /* extract all optional bookmark tokens */
1001 if (opt_flags & BM_PITCH)
1002 GET_INT_TOKEN(bm.pitch);
1003 if (opt_flags & BM_SPEED)
1004 GET_INT_TOKEN(bm.speed);
1006 if (*s == 0)
1008 return false;
1011 end = strchr(s, ';');
1013 /* extract file names */
1014 if (parse_filenames)
1016 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1017 len = MIN(TEMP_BUF_SIZE - 1, len);
1018 strlcpy(global_temp_buffer, s, len + 1);
1020 if (end != NULL)
1022 end++;
1023 strlcpy(global_filename, end, MAX_PATH);
1027 return true;
1030 /* ----------------------------------------------------------------------- */
1031 /* This function is used by multiple functions and is used to generate a */
1032 /* bookmark named based off of the input. */
1033 /* Changing this function could result in how the bookmarks are stored. */
1034 /* it would be here that the centralized/decentralized bookmark code */
1035 /* could be placed. */
1036 /* ----------------------------------------------------------------------- */
1037 static bool generate_bookmark_file_name(const char *in)
1039 int len = strlen(in);
1041 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1042 /* otherwise, name it based on the in variable */
1043 if (!strcmp("/", in))
1044 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1045 else
1047 #ifdef HAVE_MULTIVOLUME
1048 /* The "root" of an extra volume need special handling too. */
1049 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1050 !strcmp("/", global_bookmark_file_name));
1051 #endif
1053 strcpy(global_bookmark_file_name, in);
1054 if(global_bookmark_file_name[len-1] == '/')
1055 len--;
1056 #ifdef HAVE_MULTIVOLUME
1057 if (volume_root)
1058 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1059 else
1060 #endif
1061 strcpy(&global_bookmark_file_name[len], ".bmark");
1064 return true;
1067 /* ----------------------------------------------------------------------- */
1068 /* Returns true if a bookmark file exists for the current playlist */
1069 /* ----------------------------------------------------------------------- */
1070 bool bookmark_exists(void)
1072 bool exist=false;
1074 if(is_bookmarkable_state())
1076 char* name = playlist_get_name(NULL, global_temp_buffer,
1077 sizeof(global_temp_buffer));
1078 if (generate_bookmark_file_name(name))
1080 exist = file_exists(global_bookmark_file_name);
1084 return exist;
1087 /* ----------------------------------------------------------------------- */
1088 /* Checks the current state of the system and returns true if it is in a */
1089 /* bookmarkable state. */
1090 /* ----------------------------------------------------------------------- */
1091 static bool is_bookmarkable_state(void)
1093 int resume_index = 0;
1095 if (!(audio_status() && audio_current_track()) ||
1096 /* no track playing */
1097 (playlist_get_resume_info(&resume_index) == -1) ||
1098 /* invalid queue info */
1099 (playlist_modified(NULL)))
1100 /* can't bookmark while in the queue */
1102 return false;
1105 return true;