Fully fill the table headers with color
[kugel-rb/myfork.git] / apps / bookmark.c
blobcee9abdfa8eaab6c3020c7e628bcddc12fc3977b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "action.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "system.h"
34 #include "icons.h"
35 #include "menu.h"
36 #include "lang.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "splash.h"
40 #include "yesno.h"
41 #include "list.h"
42 #include "plugin.h"
43 #include "backdrop.h"
44 #include "file.h"
46 #define MAX_BOOKMARKS 10
47 #define MAX_BOOKMARK_SIZE 350
48 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
50 /* Used to buffer bookmarks while displaying the bookmark list. */
51 struct bookmark_list
53 const char* filename;
54 size_t buffer_size;
55 int start;
56 int count;
57 int total_count;
58 bool show_dont_resume;
59 bool reload;
60 bool show_playlist_name;
61 char* items[];
64 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
65 bool most_recent);
66 static bool check_bookmark(const char* bookmark);
67 static char* create_bookmark(void);
68 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
69 static void say_bookmark(const char* bookmark,
70 int bookmark_id, bool show_playlist_name);
71 static bool play_bookmark(const char* bookmark);
72 static bool generate_bookmark_file_name(const char *in);
73 static const char* skip_token(const char* s);
74 static const char* int_token(const char* s, int* dest);
75 static const char* long_token(const char* s, long* dest);
76 static const char* bool_token(const char* s, bool* dest);
77 static bool parse_bookmark(const char *bookmark,
78 int *resume_index,
79 int *resume_offset,
80 int *resume_seed,
81 int *resume_first_index,
82 char* resume_file,
83 unsigned int resume_file_size,
84 long* ms,
85 int * repeat_mode,
86 bool *shuffle,
87 char* file_name);
88 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
89 static const char* get_bookmark_info(int list_index,
90 void* data,
91 char *buffer,
92 size_t buffer_len);
93 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
94 static bool system_check(void);
95 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
96 static int get_bookmark_count(const char* bookmark_file_name);
98 static char global_temp_buffer[MAX_PATH+1];
99 /* File name created by generate_bookmark_file_name */
100 static char global_bookmark_file_name[MAX_PATH];
101 static char global_read_buffer[MAX_BOOKMARK_SIZE];
102 /* Bookmark created by create_bookmark*/
103 static char global_bookmark[MAX_BOOKMARK_SIZE];
104 /* Filename from parsed bookmark (can be made local where needed) */
105 static char global_filename[MAX_PATH];
107 /* ----------------------------------------------------------------------- */
108 /* This is the interface function from the main menu. */
109 /* ----------------------------------------------------------------------- */
110 bool bookmark_create_menu(void)
112 write_bookmark(true, create_bookmark());
113 return false;
116 /* ----------------------------------------------------------------------- */
117 /* This function acts as the load interface from the main menu */
118 /* This function determines the bookmark file name and then loads that file*/
119 /* for the user. The user can then select a bookmark to load. */
120 /* If no file/directory is currently playing, the menu item does not work. */
121 /* ----------------------------------------------------------------------- */
122 bool bookmark_load_menu(void)
124 if (system_check())
126 char* name = playlist_get_name(NULL, global_temp_buffer,
127 sizeof(global_temp_buffer));
128 if (generate_bookmark_file_name(name))
130 char* bookmark = select_bookmark(global_bookmark_file_name, false);
132 if (bookmark != NULL)
134 return play_bookmark(bookmark);
139 return false;
142 /* ----------------------------------------------------------------------- */
143 /* Gives the user a list of the Most Recent Bookmarks. This is an */
144 /* interface function */
145 /* ----------------------------------------------------------------------- */
146 bool bookmark_mrb_load()
148 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
150 if (bookmark != NULL)
152 return play_bookmark(bookmark);
155 return false;
158 /* ----------------------------------------------------------------------- */
159 /* This function handles an autobookmark creation. This is an interface */
160 /* function. */
161 /* ----------------------------------------------------------------------- */
162 bool bookmark_autobookmark(void)
164 char* bookmark;
165 if (!system_check())
166 return false;
167 int i;
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 FOR_NB_SCREENS(i)
196 screens[i].backdrop_show(BACKDROP_MAIN);
198 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
200 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
201 return write_bookmark(false, bookmark);
202 else
203 return write_bookmark(true, bookmark);
205 return false;
208 /* ----------------------------------------------------------------------- */
209 /* This function takes the current current resume information and writes */
210 /* that to the beginning of the bookmark file. */
211 /* This file will contain N number of bookmarks in the following format: */
212 /* resume_index*resume_offset*resume_seed*resume_first_index* */
213 /* resume_file*milliseconds*MP3 Title* */
214 /* ------------------------------------------------------------------------*/
215 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
217 bool success=false;
218 if (!bookmark)
219 return false; /* something didn't happen correctly, do nothing */
221 if (global_settings.usemrb)
222 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
225 /* writing the bookmark */
226 if (create_bookmark_file)
228 char* name = playlist_get_name(NULL, global_temp_buffer,
229 sizeof(global_temp_buffer));
230 if (generate_bookmark_file_name(name))
232 success = add_bookmark(global_bookmark_file_name, bookmark, false);
236 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
237 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
239 return true;
242 /* ----------------------------------------------------------------------- */
243 /* This function adds a bookmark to a file. */
244 /* ------------------------------------------------------------------------*/
245 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
246 bool most_recent)
248 int temp_bookmark_file = 0;
249 int bookmark_file = 0;
250 int bookmark_count = 0;
251 char* playlist = NULL;
252 char* cp;
253 char* tmp;
254 int len = 0;
255 bool unique = false;
257 /* Opening up a temp bookmark file */
258 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
259 "%s.tmp", bookmark_file_name);
260 temp_bookmark_file = open(global_temp_buffer,
261 O_WRONLY | O_CREAT | O_TRUNC);
262 if (temp_bookmark_file < 0)
263 return false; /* can't open the temp file */
265 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
267 playlist = strchr(bookmark,'/');
268 cp = strrchr(bookmark,';');
269 len = cp - playlist;
270 unique = true;
273 /* Writing the new bookmark to the begining of the temp file */
274 write(temp_bookmark_file, bookmark, strlen(bookmark));
275 write(temp_bookmark_file, "\n", 1);
276 bookmark_count++;
278 /* Reading in the previous bookmarks and writing them to the temp file */
279 bookmark_file = open(bookmark_file_name, O_RDONLY);
280 if (bookmark_file >= 0)
282 while (read_line(bookmark_file, global_read_buffer,
283 sizeof(global_read_buffer)) > 0)
285 /* The MRB has a max of MAX_BOOKMARKS in it */
286 /* This keeps it from getting too large */
287 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
288 break;
290 cp = strchr(global_read_buffer,'/');
291 tmp = strrchr(global_read_buffer,';');
292 if (check_bookmark(global_read_buffer) &&
293 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
295 bookmark_count++;
296 write(temp_bookmark_file, global_read_buffer,
297 strlen(global_read_buffer));
298 write(temp_bookmark_file, "\n", 1);
301 close(bookmark_file);
303 close(temp_bookmark_file);
305 remove(bookmark_file_name);
306 rename(global_temp_buffer, bookmark_file_name);
308 return true;
312 /* ----------------------------------------------------------------------- */
313 /* This function takes the system resume data and formats it into a valid */
314 /* bookmark. */
315 /* ----------------------------------------------------------------------- */
316 static char* create_bookmark()
318 int resume_index = 0;
319 char *file;
321 if (!system_check())
322 return NULL; /* something didn't happen correctly, do nothing */
324 /* grab the currently playing track */
325 struct mp3entry *id3 = audio_current_track();
326 if(!id3)
327 return NULL;
329 /* Get some basic resume information */
330 /* queue_resume and queue_resume_index are not used and can be ignored.*/
331 playlist_get_resume_info(&resume_index);
333 /* Get the currently playing file minus the path */
334 /* This is used when displaying the available bookmarks */
335 file = strrchr(id3->path,'/');
336 if(NULL == file)
337 return NULL;
339 /* create the bookmark */
340 snprintf(global_bookmark, sizeof(global_bookmark),
341 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
342 resume_index,
343 id3->offset,
344 playlist_get_seed(NULL),
346 id3->elapsed,
347 global_settings.repeat_mode,
348 global_settings.playlist_shuffle,
349 playlist_get_name(NULL, global_temp_buffer,
350 sizeof(global_temp_buffer)),
351 file+1);
353 /* checking to see if the bookmark is valid */
354 if (check_bookmark(global_bookmark))
355 return global_bookmark;
356 else
357 return NULL;
360 static bool check_bookmark(const char* bookmark)
362 return parse_bookmark(bookmark,
363 NULL,NULL,NULL, NULL,
364 NULL,0,NULL,NULL,
365 NULL, NULL);
368 /* ----------------------------------------------------------------------- */
369 /* This function will determine if an autoload is necessary. This is an */
370 /* interface function. */
371 /* ------------------------------------------------------------------------*/
372 bool bookmark_autoload(const char* file)
374 if(global_settings.autoloadbookmark == BOOKMARK_NO)
375 return false;
377 /*Checking to see if a bookmark file exists.*/
378 if(!generate_bookmark_file_name(file))
380 return false;
383 if(!file_exists(global_bookmark_file_name))
384 return false;
386 if(global_settings.autoloadbookmark == BOOKMARK_YES)
388 return bookmark_load(global_bookmark_file_name, true);
390 else
392 char* bookmark = select_bookmark(global_bookmark_file_name, true);
394 if (bookmark != NULL)
396 if (!play_bookmark(bookmark))
398 /* Selected bookmark not found. */
399 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
402 /* Act as if autoload was done even if it failed, since the
403 * user did make an active selection.
405 return true;
408 return false;
412 /* ----------------------------------------------------------------------- */
413 /* This function loads the bookmark information into the resume memory. */
414 /* This is an interface function. */
415 /* ------------------------------------------------------------------------*/
416 bool bookmark_load(const char* file, bool autoload)
418 int fd;
419 char* bookmark = NULL;
421 if(autoload)
423 fd = open(file, O_RDONLY);
424 if(fd >= 0)
426 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
427 bookmark=global_read_buffer;
428 close(fd);
431 else
433 /* This is not an auto-load, so list the bookmarks */
434 bookmark = select_bookmark(file, false);
437 if (bookmark != NULL)
439 if (!play_bookmark(bookmark))
441 /* Selected bookmark not found. */
442 if (!autoload)
444 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
447 return false;
451 return true;
455 static int get_bookmark_count(const char* bookmark_file_name)
457 int read_count = 0;
458 int file = open(bookmark_file_name, O_RDONLY);
460 if(file < 0)
461 return -1;
463 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
465 read_count++;
468 close(file);
469 return read_count;
472 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
474 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
475 int read_count = 0;
476 int file = open(bookmarks->filename, O_RDONLY);
478 if (file < 0)
480 return -1;
483 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
484 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
486 /* Entire file fits in buffer */
487 first_line = 0;
490 bookmarks->start = first_line;
491 bookmarks->count = 0;
492 bookmarks->reload = false;
494 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
496 read_count++;
498 if (read_count >= first_line)
500 dest -= strlen(global_read_buffer) + 1;
502 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
503 + (sizeof(char*) * (bookmarks->count + 1)))
505 break;
508 strcpy(dest, global_read_buffer);
509 bookmarks->items[bookmarks->count] = dest;
510 bookmarks->count++;
514 close(file);
515 return bookmarks->start + bookmarks->count;
518 static const char* get_bookmark_info(int list_index,
519 void* data,
520 char *buffer,
521 size_t buffer_len)
523 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
524 int index = list_index / 2;
525 int resume_index = 0;
526 long resume_time = 0;
527 bool shuffle = false;
529 if (bookmarks->show_dont_resume)
531 if (index == 0)
533 return list_index % 2 == 0
534 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
537 index--;
540 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
541 || (index < bookmarks->start))
543 int read_index = index;
545 /* Using count as a guide on how far to move could possibly fail
546 * sometimes. Use byte count if that is a problem?
549 if (read_index != 0)
551 /* Move count * 3 / 4 items in the direction the user is moving,
552 * but don't go too close to the end.
554 int offset = bookmarks->count;
555 int max = bookmarks->total_count - (bookmarks->count / 2);
557 if (read_index < bookmarks->start)
559 offset *= 3;
562 read_index = index - offset / 4;
564 if (read_index > max)
566 read_index = max;
569 if (read_index < 0)
571 read_index = 0;
575 if (buffer_bookmarks(bookmarks, read_index) <= index)
577 return "";
581 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
582 &resume_index, NULL, NULL, NULL, global_temp_buffer,
583 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
584 global_filename))
586 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
589 if (list_index % 2 == 0)
591 char *name;
592 char *format;
593 int len = strlen(global_temp_buffer);
595 if (bookmarks->show_playlist_name && len > 0)
597 name = global_temp_buffer;
598 len--;
600 if (name[len] != '/')
602 strrsplt(name, '.');
604 else if (len > 1)
606 name[len] = '\0';
609 if (len > 1)
611 name = strrsplt(name, '/');
614 format = "%s : %s";
616 else
618 name = global_filename;
619 format = "%s";
622 strrsplt(global_filename, '.');
623 snprintf(buffer, buffer_len, format, name, global_filename);
624 return buffer;
626 else
628 char time_buf[32];
630 format_time(time_buf, sizeof(time_buf), resume_time);
631 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
632 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
633 return buffer;
637 static int bookmark_list_voice_cb(int list_index, void* data)
639 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
640 int index = list_index / 2;
642 if (bookmarks->show_dont_resume)
644 if (index == 0)
645 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
646 index--;
648 say_bookmark(bookmarks->items[index - bookmarks->start], index,
649 bookmarks->show_playlist_name);
650 return 0;
653 /* ----------------------------------------------------------------------- */
654 /* This displays a the bookmarks in a file and allows the user to */
655 /* select one to play. */
656 /* ------------------------------------------------------------------------*/
657 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
659 struct bookmark_list* bookmarks;
660 struct gui_synclist list;
661 int item = 0;
662 int action;
663 size_t size;
664 bool exit = false;
665 bool refresh = true;
667 bookmarks = plugin_get_buffer(&size);
668 bookmarks->buffer_size = size;
669 bookmarks->show_dont_resume = show_dont_resume;
670 bookmarks->filename = bookmark_file_name;
671 bookmarks->start = 0;
672 bookmarks->show_playlist_name
673 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
674 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
675 if(global_settings.talk_menu)
676 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
677 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
678 Icon_Bookmark);
680 while (!exit)
683 if (refresh)
685 int count = get_bookmark_count(bookmark_file_name);
686 bookmarks->total_count = count;
688 if (bookmarks->total_count < 1)
690 /* No more bookmarks, delete file and exit */
691 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
692 remove(bookmark_file_name);
693 return NULL;
696 if (bookmarks->show_dont_resume)
698 count++;
699 item++;
702 gui_synclist_set_nb_items(&list, count * 2);
704 if (item >= count)
706 /* Selected item has been deleted */
707 item = count - 1;
708 gui_synclist_select_item(&list, item * 2);
711 buffer_bookmarks(bookmarks, bookmarks->start);
712 gui_synclist_draw(&list);
713 cond_talk_ids_fq(VOICE_EXT_BMARK);
714 gui_synclist_speak_item(&list);
715 refresh = false;
718 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
719 &list, &action, LIST_WRAP_UNLESS_HELD);
720 item = gui_synclist_get_sel_pos(&list) / 2;
722 if (bookmarks->show_dont_resume)
724 item--;
727 if (action == ACTION_STD_CONTEXT)
729 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
730 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
731 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
732 static const int menu_actions[] =
734 ACTION_STD_OK, ACTION_BMS_DELETE
736 int selection = do_menu(&menu_items, NULL, NULL, false);
738 refresh = true;
740 if (selection >= 0 && selection <=
741 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
743 action = menu_actions[selection];
747 switch (action)
749 case ACTION_STD_OK:
750 if (item >= 0)
752 talk_shutup();
753 return bookmarks->items[item - bookmarks->start];
756 /* Else fall through */
758 case ACTION_TREE_WPS:
759 case ACTION_STD_CANCEL:
760 exit = true;
761 break;
763 case ACTION_BMS_DELETE:
764 if (item >= 0)
766 delete_bookmark(bookmark_file_name, item);
767 bookmarks->reload = true;
768 refresh = true;
770 break;
772 default:
773 if (default_event_handler(action) == SYS_USB_CONNECTED)
775 exit = true;
778 break;
782 talk_shutup();
783 return NULL;
786 /* ----------------------------------------------------------------------- */
787 /* This function takes a location in a bookmark file and deletes that */
788 /* bookmark. */
789 /* ------------------------------------------------------------------------*/
790 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
792 int temp_bookmark_file = 0;
793 int bookmark_file = 0;
794 int bookmark_count = 0;
796 /* Opening up a temp bookmark file */
797 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
798 "%s.tmp", bookmark_file_name);
799 temp_bookmark_file = open(global_temp_buffer,
800 O_WRONLY | O_CREAT | O_TRUNC);
802 if (temp_bookmark_file < 0)
803 return false; /* can't open the temp file */
805 /* Reading in the previous bookmarks and writing them to the temp file */
806 bookmark_file = open(bookmark_file_name, O_RDONLY);
807 if (bookmark_file >= 0)
809 while (read_line(bookmark_file, global_read_buffer,
810 sizeof(global_read_buffer)) > 0)
812 if (bookmark_id != bookmark_count)
814 write(temp_bookmark_file, global_read_buffer,
815 strlen(global_read_buffer));
816 write(temp_bookmark_file, "\n", 1);
818 bookmark_count++;
820 close(bookmark_file);
822 close(temp_bookmark_file);
824 remove(bookmark_file_name);
825 rename(global_temp_buffer, bookmark_file_name);
827 return true;
830 /* ----------------------------------------------------------------------- */
831 /* This function parses a bookmark, says the voice UI part of it. */
832 /* ------------------------------------------------------------------------*/
833 static void say_bookmark(const char* bookmark,
834 int bookmark_id, bool show_playlist_name)
836 int resume_index;
837 long ms;
838 bool playlist_shuffle = false;
839 bool is_dir;
841 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
842 global_temp_buffer,sizeof(global_temp_buffer),
843 &ms, NULL, &playlist_shuffle,
844 global_filename))
846 talk_id(LANG_BOOKMARK_INVALID, false);
847 return;
850 talk_number(bookmark_id + 1, false);
852 is_dir = (global_temp_buffer[0]
853 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
854 #if CONFIG_CODEC == SWCODEC
855 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
856 together, because there is no guarantee that the same mp3
857 parameters are used. */
858 if(show_playlist_name)
859 { /* It's useful to know which playlist this is */
860 if(is_dir)
861 talk_dir_or_spell(global_temp_buffer,
862 TALK_IDARRAY(VOICE_DIR), true);
863 else talk_file_or_spell(NULL, global_temp_buffer,
864 TALK_IDARRAY(LANG_PLAYLIST), true);
866 #else
867 (void)show_playlist_name;
868 #endif
870 if(playlist_shuffle)
871 talk_id(LANG_SHUFFLE, true);
873 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
874 talk_number(resume_index + 1, true);
875 talk_id(LANG_TIME, true);
876 talk_value(ms / 1000, UNIT_TIME, true);
878 #if CONFIG_CODEC == SWCODEC
879 /* Track filename */
880 if(is_dir)
881 talk_file_or_spell(global_temp_buffer, global_filename,
882 TALK_IDARRAY(VOICE_FILE), true);
883 else
884 { /* Unfortunately if this is a playlist, we do not know in which
885 directory the file is and therefore cannot find the track's
886 .talk file. */
887 talk_id(VOICE_FILE, true);
888 talk_spell(global_filename, true);
890 #endif
893 /* ----------------------------------------------------------------------- */
894 /* This function parses a bookmark and then plays it. */
895 /* ------------------------------------------------------------------------*/
896 static bool play_bookmark(const char* bookmark)
898 int index;
899 int offset;
900 int seed;
902 if (parse_bookmark(bookmark,
903 &index,
904 &offset,
905 &seed,
906 NULL,
907 global_temp_buffer,
908 sizeof(global_temp_buffer),
909 NULL,
910 &global_settings.repeat_mode,
911 &global_settings.playlist_shuffle,
912 global_filename))
914 return bookmark_play(global_temp_buffer, index, offset, seed,
915 global_filename);
918 return false;
921 static const char* skip_token(const char* s)
923 while (*s && *s != ';')
925 s++;
928 if (*s)
930 s++;
933 return s;
936 static const char* int_token(const char* s, int* dest)
938 if (dest != NULL)
940 *dest = atoi(s);
943 return skip_token(s);
946 static const char* long_token(const char* s, long* dest)
948 if (dest != NULL)
950 *dest = atoi(s); /* Should be atol, but we don't have it. */
953 return skip_token(s);
956 static const char* bool_token(const char* s, bool* dest)
958 if (dest != NULL)
960 *dest = atoi(s) != 0;
963 return skip_token(s);
966 /* ----------------------------------------------------------------------- */
967 /* This function takes a bookmark and parses it. This function also */
968 /* validates the bookmark. Passing in NULL for an output variable */
969 /* indicates that value is not requested. */
970 /* ----------------------------------------------------------------------- */
971 static bool parse_bookmark(const char *bookmark,
972 int *resume_index,
973 int *resume_offset,
974 int *resume_seed,
975 int *resume_first_index,
976 char* resume_file,
977 unsigned int resume_file_size,
978 long* ms,
979 int * repeat_mode, bool *shuffle,
980 char* file_name)
982 const char* s = bookmark;
983 const char* end;
985 s = int_token(s, resume_index);
986 s = int_token(s, resume_offset);
987 s = int_token(s, resume_seed);
988 s = int_token(s, resume_first_index);
989 s = long_token(s, ms);
990 s = int_token(s, repeat_mode);
991 s = bool_token(s, shuffle);
993 if (*s == 0)
995 return false;
998 end = strchr(s, ';');
1000 if (resume_file != NULL)
1002 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1003 len = MIN(resume_file_size - 1, len);
1004 strlcpy(resume_file, s, len + 1);
1007 if (end != NULL && file_name != NULL)
1009 end++;
1010 strlcpy(file_name, end, MAX_PATH);
1013 return true;
1016 /* ----------------------------------------------------------------------- */
1017 /* This function is used by multiple functions and is used to generate a */
1018 /* bookmark named based off of the input. */
1019 /* Changing this function could result in how the bookmarks are stored. */
1020 /* it would be here that the centralized/decentralized bookmark code */
1021 /* could be placed. */
1022 /* ----------------------------------------------------------------------- */
1023 static bool generate_bookmark_file_name(const char *in)
1025 int len = strlen(in);
1027 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1028 /* otherwise, name it based on the in variable */
1029 if (!strcmp("/", in))
1030 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1031 else
1033 #ifdef HAVE_MULTIVOLUME
1034 /* The "root" of an extra volume need special handling too. */
1035 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1036 !strcmp("/", global_bookmark_file_name));
1037 #endif
1039 strcpy(global_bookmark_file_name, in);
1040 if(global_bookmark_file_name[len-1] == '/')
1041 len--;
1042 #ifdef HAVE_MULTIVOLUME
1043 if (volume_root)
1044 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1045 else
1046 #endif
1047 strcpy(&global_bookmark_file_name[len], ".bmark");
1050 return true;
1053 /* ----------------------------------------------------------------------- */
1054 /* Returns true if a bookmark file exists for the current playlist */
1055 /* ----------------------------------------------------------------------- */
1056 bool bookmark_exist(void)
1058 bool exist=false;
1060 if(system_check())
1062 char* name = playlist_get_name(NULL, global_temp_buffer,
1063 sizeof(global_temp_buffer));
1064 if (generate_bookmark_file_name(name))
1066 exist = file_exists(global_bookmark_file_name);
1070 return exist;
1073 /* ----------------------------------------------------------------------- */
1074 /* Checks the current state of the system and returns if it is in a */
1075 /* bookmarkable state. */
1076 /* ----------------------------------------------------------------------- */
1077 /* Inputs: */
1078 /* ----------------------------------------------------------------------- */
1079 /* Outputs: */
1080 /* return bool: Indicates if the system was in a bookmarkable state */
1081 /* ----------------------------------------------------------------------- */
1082 static bool system_check(void)
1084 int resume_index = 0;
1086 if (!(audio_status() && audio_current_track()))
1088 /* no track playing */
1089 return false;
1092 /* Checking to see if playing a queued track */
1093 if (playlist_get_resume_info(&resume_index) == -1)
1095 /* something bad happened while getting the queue information */
1096 return false;
1098 else if (playlist_modified(NULL))
1100 /* can't bookmark while in the queue */
1101 return false;
1104 return true;