Fix sansa c200 battery type comment.
[kugel-rb.git] / apps / bookmark.c
blob0c3da641cb921dacce3e94f2091132609fb76148
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "action.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "system.h"
34 #include "icons.h"
35 #include "menu.h"
36 #include "lang.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "splash.h"
40 #include "yesno.h"
41 #include "list.h"
42 #include "plugin.h"
43 #include "backdrop.h"
44 #include "file.h"
45 #include "statusbar.h"
47 #define MAX_BOOKMARKS 10
48 #define MAX_BOOKMARK_SIZE 350
49 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
51 /* Used to buffer bookmarks while displaying the bookmark list. */
52 struct bookmark_list
54 const char* filename;
55 size_t buffer_size;
56 int start;
57 int count;
58 int total_count;
59 bool show_dont_resume;
60 bool reload;
61 bool show_playlist_name;
62 char* items[];
65 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
66 bool most_recent);
67 static bool check_bookmark(const char* bookmark);
68 static char* create_bookmark(void);
69 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
70 static void say_bookmark(const char* bookmark,
71 int bookmark_id, bool show_playlist_name);
72 static bool play_bookmark(const char* bookmark);
73 static bool generate_bookmark_file_name(const char *in);
74 static const char* skip_token(const char* s);
75 static const char* int_token(const char* s, int* dest);
76 static const char* long_token(const char* s, long* dest);
77 static const char* bool_token(const char* s, bool* dest);
78 static bool parse_bookmark(const char *bookmark,
79 int *resume_index,
80 int *resume_offset,
81 int *resume_seed,
82 int *resume_first_index,
83 char* resume_file,
84 unsigned int resume_file_size,
85 long* ms,
86 int * repeat_mode,
87 bool *shuffle,
88 char* file_name);
89 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
90 static char* get_bookmark_info(int list_index,
91 void* data,
92 char *buffer,
93 size_t buffer_len);
94 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
95 static bool system_check(void);
96 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
97 static int get_bookmark_count(const char* bookmark_file_name);
99 static char global_temp_buffer[MAX_PATH+1];
100 /* File name created by generate_bookmark_file_name */
101 static char global_bookmark_file_name[MAX_PATH];
102 static char global_read_buffer[MAX_BOOKMARK_SIZE];
103 /* Bookmark created by create_bookmark*/
104 static char global_bookmark[MAX_BOOKMARK_SIZE];
105 /* Filename from parsed bookmark (can be made local where needed) */
106 static char global_filename[MAX_PATH];
108 /* ----------------------------------------------------------------------- */
109 /* This is the interface function from the main menu. */
110 /* ----------------------------------------------------------------------- */
111 bool bookmark_create_menu(void)
113 write_bookmark(true, create_bookmark());
114 return false;
117 /* ----------------------------------------------------------------------- */
118 /* This function acts as the load interface from the main menu */
119 /* This function determines the bookmark file name and then loads that file*/
120 /* for the user. The user can then select a bookmark to load. */
121 /* If no file/directory is currently playing, the menu item does not work. */
122 /* ----------------------------------------------------------------------- */
123 bool bookmark_load_menu(void)
125 if (system_check())
127 char* name = playlist_get_name(NULL, global_temp_buffer,
128 sizeof(global_temp_buffer));
129 if (generate_bookmark_file_name(name))
131 char* bookmark = select_bookmark(global_bookmark_file_name, false);
133 if (bookmark != NULL)
135 return play_bookmark(bookmark);
140 return false;
143 /* ----------------------------------------------------------------------- */
144 /* Gives the user a list of the Most Recent Bookmarks. This is an */
145 /* interface function */
146 /* ----------------------------------------------------------------------- */
147 bool bookmark_mrb_load()
149 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
151 if (bookmark != NULL)
153 return play_bookmark(bookmark);
156 return false;
159 /* ----------------------------------------------------------------------- */
160 /* This function handles an autobookmark creation. This is an interface */
161 /* function. */
162 /* ----------------------------------------------------------------------- */
163 bool bookmark_autobookmark(void)
165 char* bookmark;
166 if (!system_check())
167 return false;
169 audio_pause(); /* first pause playback */
170 bookmark = create_bookmark();
171 /* Workaround for inability to speak when paused: all callers will
172 just do audio_stop() when we return, so we can do it right
173 away. This makes it possible to speak the "Create a Bookmark?"
174 prompt and the "Bookmark Created" splash. */
175 audio_stop();
176 switch (global_settings.autocreatebookmark)
178 case BOOKMARK_YES:
179 return write_bookmark(true, bookmark);
181 case BOOKMARK_NO:
182 return false;
184 case BOOKMARK_RECENT_ONLY_YES:
185 return write_bookmark(false, bookmark);
187 #ifdef HAVE_LCD_BITMAP
188 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
189 const struct text_message message={lines, 1};
190 #else
191 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
192 str(LANG_CONFIRM_WITH_BUTTON)};
193 const struct text_message message={lines, 2};
194 #endif
195 #if LCD_DEPTH > 1
196 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
197 #endif
198 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
199 show_remote_main_backdrop();
200 #endif
201 gui_syncstatusbar_draw(&statusbars, false);
202 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
204 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
205 return write_bookmark(false, bookmark);
206 else
207 return write_bookmark(true, bookmark);
209 return false;
212 /* ----------------------------------------------------------------------- */
213 /* This function takes the current current resume information and writes */
214 /* that to the beginning of the bookmark file. */
215 /* This file will contain N number of bookmarks in the following format: */
216 /* resume_index*resume_offset*resume_seed*resume_first_index* */
217 /* resume_file*milliseconds*MP3 Title* */
218 /* ------------------------------------------------------------------------*/
219 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
221 bool success=false;
222 if (!bookmark)
223 return false; /* something didn't happen correctly, do nothing */
225 if (global_settings.usemrb)
226 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
229 /* writing the bookmark */
230 if (create_bookmark_file)
232 char* name = playlist_get_name(NULL, global_temp_buffer,
233 sizeof(global_temp_buffer));
234 if (generate_bookmark_file_name(name))
236 success = add_bookmark(global_bookmark_file_name, bookmark, false);
240 gui_syncsplash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
241 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
243 return true;
246 /* ----------------------------------------------------------------------- */
247 /* This function adds a bookmark to a file. */
248 /* ------------------------------------------------------------------------*/
249 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
250 bool most_recent)
252 int temp_bookmark_file = 0;
253 int bookmark_file = 0;
254 int bookmark_count = 0;
255 char* playlist = NULL;
256 char* cp;
257 char* tmp;
258 int len = 0;
259 bool unique = false;
261 /* Opening up a temp bookmark file */
262 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
263 "%s.tmp", bookmark_file_name);
264 temp_bookmark_file = open(global_temp_buffer,
265 O_WRONLY | O_CREAT | O_TRUNC);
266 if (temp_bookmark_file < 0)
267 return false; /* can't open the temp file */
269 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
271 playlist = strchr(bookmark,'/');
272 cp = strrchr(bookmark,';');
273 len = cp - playlist;
274 unique = true;
277 /* Writing the new bookmark to the begining of the temp file */
278 write(temp_bookmark_file, bookmark, strlen(bookmark));
279 write(temp_bookmark_file, "\n", 1);
280 bookmark_count++;
282 /* Reading in the previous bookmarks and writing them to the temp file */
283 bookmark_file = open(bookmark_file_name, O_RDONLY);
284 if (bookmark_file >= 0)
286 while (read_line(bookmark_file, global_read_buffer,
287 sizeof(global_read_buffer)) > 0)
289 /* The MRB has a max of MAX_BOOKMARKS in it */
290 /* This keeps it from getting too large */
291 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
292 break;
294 cp = strchr(global_read_buffer,'/');
295 tmp = strrchr(global_read_buffer,';');
296 if (check_bookmark(global_read_buffer) &&
297 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
299 bookmark_count++;
300 write(temp_bookmark_file, global_read_buffer,
301 strlen(global_read_buffer));
302 write(temp_bookmark_file, "\n", 1);
305 close(bookmark_file);
307 close(temp_bookmark_file);
309 remove(bookmark_file_name);
310 rename(global_temp_buffer, bookmark_file_name);
312 return true;
316 /* ----------------------------------------------------------------------- */
317 /* This function takes the system resume data and formats it into a valid */
318 /* bookmark. */
319 /* ----------------------------------------------------------------------- */
320 static char* create_bookmark()
322 int resume_index = 0;
323 char *file;
325 if (!system_check())
326 return NULL; /* something didn't happen correctly, do nothing */
328 /* grab the currently playing track */
329 struct mp3entry *id3 = audio_current_track();
330 if(!id3)
331 return NULL;
333 /* Get some basic resume information */
334 /* queue_resume and queue_resume_index are not used and can be ignored.*/
335 playlist_get_resume_info(&resume_index);
337 /* Get the currently playing file minus the path */
338 /* This is used when displaying the available bookmarks */
339 file = strrchr(id3->path,'/');
340 if(NULL == file)
341 return NULL;
343 /* create the bookmark */
344 snprintf(global_bookmark, sizeof(global_bookmark),
345 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
346 resume_index,
347 id3->offset,
348 playlist_get_seed(NULL),
350 id3->elapsed,
351 global_settings.repeat_mode,
352 global_settings.playlist_shuffle,
353 playlist_get_name(NULL, global_temp_buffer,
354 sizeof(global_temp_buffer)),
355 file+1);
357 /* checking to see if the bookmark is valid */
358 if (check_bookmark(global_bookmark))
359 return global_bookmark;
360 else
361 return NULL;
364 static bool check_bookmark(const char* bookmark)
366 return parse_bookmark(bookmark,
367 NULL,NULL,NULL, NULL,
368 NULL,0,NULL,NULL,
369 NULL, NULL);
372 /* ----------------------------------------------------------------------- */
373 /* This function will determine if an autoload is necessary. This is an */
374 /* interface function. */
375 /* ------------------------------------------------------------------------*/
376 bool bookmark_autoload(const char* file)
378 if(global_settings.autoloadbookmark == BOOKMARK_NO)
379 return false;
381 /*Checking to see if a bookmark file exists.*/
382 if(!generate_bookmark_file_name(file))
384 return false;
387 if(!file_exists(global_bookmark_file_name))
388 return false;
390 if(global_settings.autoloadbookmark == BOOKMARK_YES)
392 return bookmark_load(global_bookmark_file_name, true);
394 else
396 char* bookmark = select_bookmark(global_bookmark_file_name, true);
398 if (bookmark != NULL)
400 if (!play_bookmark(bookmark))
402 /* Selected bookmark not found. */
403 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
406 /* Act as if autoload was done even if it failed, since the
407 * user did make an active selection.
409 return true;
412 return false;
416 /* ----------------------------------------------------------------------- */
417 /* This function loads the bookmark information into the resume memory. */
418 /* This is an interface function. */
419 /* ------------------------------------------------------------------------*/
420 bool bookmark_load(const char* file, bool autoload)
422 int fd;
423 char* bookmark = NULL;
425 if(autoload)
427 fd = open(file, O_RDONLY);
428 if(fd >= 0)
430 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
431 bookmark=global_read_buffer;
432 close(fd);
435 else
437 /* This is not an auto-load, so list the bookmarks */
438 bookmark = select_bookmark(file, false);
441 if (bookmark != NULL)
443 if (!play_bookmark(bookmark))
445 /* Selected bookmark not found. */
446 if (!autoload)
448 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
451 return false;
455 return true;
459 static int get_bookmark_count(const char* bookmark_file_name)
461 int read_count = 0;
462 int file = open(bookmark_file_name, O_RDONLY);
464 if(file < 0)
465 return -1;
467 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
469 read_count++;
472 close(file);
473 return read_count;
476 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
478 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
479 int read_count = 0;
480 int file = open(bookmarks->filename, O_RDONLY);
482 if (file < 0)
484 return -1;
487 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
488 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
490 /* Entire file fits in buffer */
491 first_line = 0;
494 bookmarks->start = first_line;
495 bookmarks->count = 0;
496 bookmarks->reload = false;
498 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
500 read_count++;
502 if (read_count >= first_line)
504 dest -= strlen(global_read_buffer) + 1;
506 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
507 + (sizeof(char*) * (bookmarks->count + 1)))
509 break;
512 strcpy(dest, global_read_buffer);
513 bookmarks->items[bookmarks->count] = dest;
514 bookmarks->count++;
518 close(file);
519 return bookmarks->start + bookmarks->count;
522 static char* get_bookmark_info(int list_index,
523 void* data,
524 char *buffer,
525 size_t buffer_len)
527 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
528 int index = list_index / 2;
529 int resume_index = 0;
530 long resume_time = 0;
531 bool shuffle = false;
533 if (bookmarks->show_dont_resume)
535 if (index == 0)
537 return list_index % 2 == 0
538 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
541 index--;
544 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
545 || (index < bookmarks->start))
547 int read_index = index;
549 /* Using count as a guide on how far to move could possibly fail
550 * sometimes. Use byte count if that is a problem?
553 if (read_index != 0)
555 /* Move count * 3 / 4 items in the direction the user is moving,
556 * but don't go too close to the end.
558 int offset = bookmarks->count;
559 int max = bookmarks->total_count - (bookmarks->count / 2);
561 if (read_index < bookmarks->start)
563 offset *= 3;
566 read_index = index - offset / 4;
568 if (read_index > max)
570 read_index = max;
573 if (read_index < 0)
575 read_index = 0;
579 if (buffer_bookmarks(bookmarks, read_index) <= index)
581 return "";
585 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
586 &resume_index, NULL, NULL, NULL, global_temp_buffer,
587 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
588 global_filename))
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), resume_time);
635 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
636 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);
683 gui_syncstatusbar_draw(&statusbars, true);
685 while (!exit)
687 gui_syncstatusbar_draw(&statusbars, false);
689 if (refresh)
691 int count = get_bookmark_count(bookmark_file_name);
692 bookmarks->total_count = count;
694 if (bookmarks->total_count < 1)
696 /* No more bookmarks, delete file and exit */
697 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
698 remove(bookmark_file_name);
699 return NULL;
702 if (bookmarks->show_dont_resume)
704 count++;
705 item++;
708 gui_synclist_set_nb_items(&list, count * 2);
710 if (item >= count)
712 /* Selected item has been deleted */
713 item = count - 1;
714 gui_synclist_select_item(&list, item * 2);
717 buffer_bookmarks(bookmarks, bookmarks->start);
718 gui_synclist_draw(&list);
719 cond_talk_ids_fq(VOICE_EXT_BMARK);
720 gui_synclist_speak_item(&list);
721 refresh = false;
724 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
725 &list, &action, LIST_WRAP_UNLESS_HELD);
726 item = gui_synclist_get_sel_pos(&list) / 2;
728 if (bookmarks->show_dont_resume)
730 item--;
733 if (action == ACTION_STD_CONTEXT)
735 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
736 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
737 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
738 static const int menu_actions[] =
740 ACTION_STD_OK, ACTION_BMS_DELETE
742 int selection = do_menu(&menu_items, NULL, NULL, false);
744 refresh = true;
746 if (selection >= 0 && selection <=
747 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
749 action = menu_actions[selection];
753 switch (action)
755 case ACTION_STD_OK:
756 if (item >= 0)
758 talk_shutup();
759 return bookmarks->items[item - bookmarks->start];
762 /* Else fall through */
764 case ACTION_TREE_WPS:
765 case ACTION_STD_CANCEL:
766 exit = true;
767 break;
769 case ACTION_BMS_DELETE:
770 if (item >= 0)
772 delete_bookmark(bookmark_file_name, item);
773 bookmarks->reload = true;
774 refresh = true;
776 break;
778 default:
779 if (default_event_handler(action) == SYS_USB_CONNECTED)
781 exit = true;
784 break;
788 talk_shutup();
789 return NULL;
792 /* ----------------------------------------------------------------------- */
793 /* This function takes a location in a bookmark file and deletes that */
794 /* bookmark. */
795 /* ------------------------------------------------------------------------*/
796 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
798 int temp_bookmark_file = 0;
799 int bookmark_file = 0;
800 int bookmark_count = 0;
802 /* Opening up a temp bookmark file */
803 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
804 "%s.tmp", bookmark_file_name);
805 temp_bookmark_file = open(global_temp_buffer,
806 O_WRONLY | O_CREAT | O_TRUNC);
808 if (temp_bookmark_file < 0)
809 return false; /* can't open the temp file */
811 /* Reading in the previous bookmarks and writing them to the temp file */
812 bookmark_file = open(bookmark_file_name, O_RDONLY);
813 if (bookmark_file >= 0)
815 while (read_line(bookmark_file, global_read_buffer,
816 sizeof(global_read_buffer)) > 0)
818 if (bookmark_id != bookmark_count)
820 write(temp_bookmark_file, global_read_buffer,
821 strlen(global_read_buffer));
822 write(temp_bookmark_file, "\n", 1);
824 bookmark_count++;
826 close(bookmark_file);
828 close(temp_bookmark_file);
830 remove(bookmark_file_name);
831 rename(global_temp_buffer, bookmark_file_name);
833 return true;
836 /* ----------------------------------------------------------------------- */
837 /* This function parses a bookmark, says the voice UI part of it. */
838 /* ------------------------------------------------------------------------*/
839 static void say_bookmark(const char* bookmark,
840 int bookmark_id, bool show_playlist_name)
842 int resume_index;
843 long ms;
844 bool playlist_shuffle = false;
845 bool is_dir;
847 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
848 global_temp_buffer,sizeof(global_temp_buffer),
849 &ms, NULL, &playlist_shuffle,
850 global_filename))
852 talk_id(LANG_BOOKMARK_INVALID, false);
853 return;
856 talk_number(bookmark_id + 1, false);
858 is_dir = (global_temp_buffer[0]
859 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
860 #if CONFIG_CODEC == SWCODEC
861 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
862 together, because there is no guarantee that the same mp3
863 parameters are used. */
864 if(show_playlist_name)
865 { /* It's useful to know which playlist this is */
866 if(is_dir)
867 talk_dir_or_spell(global_temp_buffer,
868 TALK_IDARRAY(VOICE_DIR), true);
869 else talk_file_or_spell(NULL, global_temp_buffer,
870 TALK_IDARRAY(LANG_PLAYLIST), true);
872 #else
873 (void)show_playlist_name;
874 #endif
876 if(playlist_shuffle)
877 talk_id(LANG_SHUFFLE, true);
879 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
880 talk_number(resume_index + 1, true);
881 talk_id(LANG_TIME, true);
882 talk_value(ms / 1000, UNIT_TIME, true);
884 #if CONFIG_CODEC == SWCODEC
885 /* Track filename */
886 if(is_dir)
887 talk_file_or_spell(global_temp_buffer, global_filename,
888 TALK_IDARRAY(VOICE_FILE), true);
889 else
890 { /* Unfortunately if this is a playlist, we do not know in which
891 directory the file is and therefore cannot find the track's
892 .talk file. */
893 talk_id(VOICE_FILE, true);
894 talk_spell(global_filename, true);
896 #endif
899 /* ----------------------------------------------------------------------- */
900 /* This function parses a bookmark and then plays it. */
901 /* ------------------------------------------------------------------------*/
902 static bool play_bookmark(const char* bookmark)
904 int index;
905 int offset;
906 int seed;
908 if (parse_bookmark(bookmark,
909 &index,
910 &offset,
911 &seed,
912 NULL,
913 global_temp_buffer,
914 sizeof(global_temp_buffer),
915 NULL,
916 &global_settings.repeat_mode,
917 &global_settings.playlist_shuffle,
918 global_filename))
920 return bookmark_play(global_temp_buffer, index, offset, seed,
921 global_filename);
924 return false;
927 static const char* skip_token(const char* s)
929 while (*s && *s != ';')
931 s++;
934 if (*s)
936 s++;
939 return s;
942 static const char* int_token(const char* s, int* dest)
944 if (dest != NULL)
946 *dest = atoi(s);
949 return skip_token(s);
952 static const char* long_token(const char* s, long* dest)
954 if (dest != NULL)
956 *dest = atoi(s); /* Should be atol, but we don't have it. */
959 return skip_token(s);
962 static const char* bool_token(const char* s, bool* dest)
964 if (dest != NULL)
966 *dest = atoi(s) != 0;
969 return skip_token(s);
972 /* ----------------------------------------------------------------------- */
973 /* This function takes a bookmark and parses it. This function also */
974 /* validates the bookmark. Passing in NULL for an output variable */
975 /* indicates that value is not requested. */
976 /* ----------------------------------------------------------------------- */
977 static bool parse_bookmark(const char *bookmark,
978 int *resume_index,
979 int *resume_offset,
980 int *resume_seed,
981 int *resume_first_index,
982 char* resume_file,
983 unsigned int resume_file_size,
984 long* ms,
985 int * repeat_mode, bool *shuffle,
986 char* file_name)
988 const char* s = bookmark;
989 const char* end;
991 s = int_token(s, resume_index);
992 s = int_token(s, resume_offset);
993 s = int_token(s, resume_seed);
994 s = int_token(s, resume_first_index);
995 s = long_token(s, ms);
996 s = int_token(s, repeat_mode);
997 s = bool_token(s, shuffle);
999 if (*s == 0)
1001 return false;
1004 end = strchr(s, ';');
1006 if (resume_file != NULL)
1008 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1010 len = MIN(resume_file_size - 1, len);
1011 strncpy(resume_file, s, len);
1012 resume_file[len] = 0;
1015 if (end != NULL && file_name != NULL)
1017 end++;
1018 strncpy(file_name, end, MAX_PATH - 1);
1019 file_name[MAX_PATH - 1] = 0;
1022 return true;
1025 /* ----------------------------------------------------------------------- */
1026 /* This function is used by multiple functions and is used to generate a */
1027 /* bookmark named based off of the input. */
1028 /* Changing this function could result in how the bookmarks are stored. */
1029 /* it would be here that the centralized/decentralized bookmark code */
1030 /* could be placed. */
1031 /* ----------------------------------------------------------------------- */
1032 static bool generate_bookmark_file_name(const char *in)
1034 int len = strlen(in);
1036 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1037 /* otherwise, name it based on the in variable */
1038 if (!strcmp("/", in))
1039 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1040 else
1042 strcpy(global_bookmark_file_name, in);
1043 if(global_bookmark_file_name[len-1] == '/')
1044 len--;
1045 strcpy(&global_bookmark_file_name[len], ".bmark");
1048 return true;
1051 /* ----------------------------------------------------------------------- */
1052 /* Returns true if a bookmark file exists for the current playlist */
1053 /* ----------------------------------------------------------------------- */
1054 bool bookmark_exist(void)
1056 bool exist=false;
1058 if(system_check())
1060 char* name = playlist_get_name(NULL, global_temp_buffer,
1061 sizeof(global_temp_buffer));
1062 if (generate_bookmark_file_name(name))
1064 exist = file_exists(global_bookmark_file_name);
1068 return exist;
1071 /* ----------------------------------------------------------------------- */
1072 /* Checks the current state of the system and returns if it is in a */
1073 /* bookmarkable state. */
1074 /* ----------------------------------------------------------------------- */
1075 /* Inputs: */
1076 /* ----------------------------------------------------------------------- */
1077 /* Outputs: */
1078 /* return bool: Indicates if the system was in a bookmarkable state */
1079 /* ----------------------------------------------------------------------- */
1080 static bool system_check(void)
1082 int resume_index = 0;
1084 if (!(audio_status() && audio_current_track()))
1086 /* no track playing */
1087 return false;
1090 /* Checking to see if playing a queued track */
1091 if (playlist_get_resume_info(&resume_index) == -1)
1093 /* something bad happened while getting the queue information */
1094 return false;
1096 else if (playlist_modified(NULL))
1098 /* can't bookmark while in the queue */
1099 return false;
1102 return true;