Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / apps / bookmark.c
blob39a9aa07d2ce97c4ad3245d49019190da9ebc6d6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdbool.h>
24 #include "applimits.h"
25 #include "lcd.h"
26 #include "action.h"
27 #include "usb.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "dir.h"
34 #include "status.h"
35 #include "system.h"
36 #include "errno.h"
37 #include "icons.h"
38 #include "atoi.h"
39 #include "string.h"
40 #include "menu.h"
41 #include "lang.h"
42 #include "screens.h"
43 #include "status.h"
44 #include "debug.h"
45 #include "kernel.h"
46 #include "sprintf.h"
47 #include "talk.h"
48 #include "misc.h"
49 #include "abrepeat.h"
50 #include "splash.h"
51 #include "yesno.h"
52 #include "list.h"
53 #include "plugin.h"
54 #include "backdrop.h"
56 #define MAX_BOOKMARKS 10
57 #define MAX_BOOKMARK_SIZE 350
58 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
60 /* Used to buffer bookmarks while displaying the bookmark list. */
61 struct bookmark_list
63 const char* filename;
64 size_t buffer_size;
65 int start;
66 int count;
67 int total_count;
68 bool show_dont_resume;
69 bool reload;
70 bool show_playlist_name;
71 char* items[];
74 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
75 bool most_recent);
76 static bool check_bookmark(const char* bookmark);
77 static char* create_bookmark(void);
78 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
79 static void say_bookmark(const char* bookmark,
80 int bookmark_id);
81 static bool play_bookmark(const char* bookmark);
82 static bool generate_bookmark_file_name(const char *in);
83 static const char* skip_token(const char* s);
84 static const char* int_token(const char* s, int* dest);
85 static const char* long_token(const char* s, long* dest);
86 static const char* bool_token(const char* s, bool* dest);
87 static bool parse_bookmark(const char *bookmark,
88 int *resume_index,
89 int *resume_offset,
90 int *resume_seed,
91 int *resume_first_index,
92 char* resume_file,
93 unsigned int resume_file_size,
94 long* ms,
95 int * repeat_mode,
96 bool *shuffle,
97 char* file_name);
98 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
99 static char* get_bookmark_info(int list_index, void* data, char *buffer);
100 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
101 static bool system_check(void);
102 static bool write_bookmark(bool create_bookmark_file);
103 static int get_bookmark_count(const char* bookmark_file_name);
105 static char global_temp_buffer[MAX_PATH+1];
106 /* File name created by generate_bookmark_file_name */
107 static char global_bookmark_file_name[MAX_PATH];
108 static char global_read_buffer[MAX_BOOKMARK_SIZE];
109 /* Bookmark created by create_bookmark*/
110 static char global_bookmark[MAX_BOOKMARK_SIZE];
111 /* Filename from parsed bookmark (can be made local where needed) */
112 static char global_filename[MAX_PATH];
114 /* ----------------------------------------------------------------------- */
115 /* This is the interface function from the main menu. */
116 /* ----------------------------------------------------------------------- */
117 bool bookmark_create_menu(void)
119 write_bookmark(true);
120 return false;
123 /* ----------------------------------------------------------------------- */
124 /* This function acts as the load interface from the main menu */
125 /* This function determines the bookmark file name and then loads that file*/
126 /* for the user. The user can then select a bookmark to load. */
127 /* If no file/directory is currently playing, the menu item does not work. */
128 /* ----------------------------------------------------------------------- */
129 bool bookmark_load_menu(void)
131 if (system_check())
133 char* name = playlist_get_name(NULL, global_temp_buffer,
134 sizeof(global_temp_buffer));
135 if (generate_bookmark_file_name(name))
137 char* bookmark = select_bookmark(global_bookmark_file_name, false);
139 if (bookmark != NULL)
141 return play_bookmark(bookmark);
146 return false;
149 /* ----------------------------------------------------------------------- */
150 /* Gives the user a list of the Most Recent Bookmarks. This is an */
151 /* interface function */
152 /* ----------------------------------------------------------------------- */
153 bool bookmark_mrb_load()
155 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
157 if (bookmark != NULL)
159 return play_bookmark(bookmark);
162 return false;
165 /* ----------------------------------------------------------------------- */
166 /* This function handles an autobookmark creation. This is an interface */
167 /* function. */
168 /* ----------------------------------------------------------------------- */
169 bool bookmark_autobookmark(void)
171 if (!system_check())
172 return false;
174 audio_pause(); /* first pause playback */
175 switch (global_settings.autocreatebookmark)
177 case BOOKMARK_YES:
178 return write_bookmark(true);
180 case BOOKMARK_NO:
181 return false;
183 case BOOKMARK_RECENT_ONLY_YES:
184 return write_bookmark(false);
186 #ifdef HAVE_LCD_BITMAP
187 unsigned char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
188 struct text_message message={(char **)lines, 1};
189 #else
190 unsigned char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
191 str(LANG_CONFIRM_WITH_BUTTON)};
192 struct text_message message={(char **)lines, 2};
193 #endif
194 #if LCD_DEPTH > 1
195 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
196 #endif
197 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
198 show_remote_main_backdrop();
199 #endif
200 gui_syncstatusbar_draw(&statusbars, false);
201 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
203 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
204 return write_bookmark(false);
205 else
206 return write_bookmark(true);
208 return false;
211 /* ----------------------------------------------------------------------- */
212 /* This function takes the current current resume information and writes */
213 /* that to the beginning of the bookmark file. */
214 /* This file will contain N number of bookmarks in the following format: */
215 /* resume_index*resume_offset*resume_seed*resume_first_index* */
216 /* resume_file*milliseconds*MP3 Title* */
217 /* ------------------------------------------------------------------------*/
218 static bool write_bookmark(bool create_bookmark_file)
220 bool success=false;
221 char* bookmark;
223 if (!system_check())
224 return false; /* something didn't happen correctly, do nothing */
226 bookmark = create_bookmark();
227 if (!bookmark)
228 return false; /* something didn't happen correctly, do nothing */
230 if (global_settings.usemrb)
231 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
234 /* writing the bookmark */
235 if (create_bookmark_file)
237 char* name = playlist_get_name(NULL, global_temp_buffer,
238 sizeof(global_temp_buffer));
239 if (generate_bookmark_file_name(name))
241 success = add_bookmark(global_bookmark_file_name, bookmark, false);
245 gui_syncsplash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
246 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
248 return true;
251 /* ----------------------------------------------------------------------- */
252 /* This function adds a bookmark to a file. */
253 /* ------------------------------------------------------------------------*/
254 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
255 bool most_recent)
257 int temp_bookmark_file = 0;
258 int bookmark_file = 0;
259 int bookmark_count = 0;
260 char* playlist = NULL;
261 char* cp;
262 char* tmp;
263 int len = 0;
264 bool unique = false;
266 /* Opening up a temp bookmark file */
267 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
268 "%s.tmp", bookmark_file_name);
269 temp_bookmark_file = open(global_temp_buffer,
270 O_WRONLY | O_CREAT | O_TRUNC);
271 if (temp_bookmark_file < 0)
272 return false; /* can't open the temp file */
274 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
276 playlist = strchr(bookmark,'/');
277 cp = strrchr(bookmark,';');
278 len = cp - playlist;
279 unique = true;
282 /* Writing the new bookmark to the begining of the temp file */
283 write(temp_bookmark_file, bookmark, strlen(bookmark));
284 write(temp_bookmark_file, "\n", 1);
285 bookmark_count++;
287 /* Reading in the previous bookmarks and writing them to the temp file */
288 bookmark_file = open(bookmark_file_name, O_RDONLY);
289 if (bookmark_file >= 0)
291 while (read_line(bookmark_file, global_read_buffer,
292 sizeof(global_read_buffer)) > 0)
294 /* The MRB has a max of MAX_BOOKMARKS in it */
295 /* This keeps it from getting too large */
296 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
297 break;
299 cp = strchr(global_read_buffer,'/');
300 tmp = strrchr(global_read_buffer,';');
301 if (check_bookmark(global_read_buffer) &&
302 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
304 bookmark_count++;
305 write(temp_bookmark_file, global_read_buffer,
306 strlen(global_read_buffer));
307 write(temp_bookmark_file, "\n", 1);
310 close(bookmark_file);
312 close(temp_bookmark_file);
314 remove(bookmark_file_name);
315 rename(global_temp_buffer, bookmark_file_name);
317 return true;
321 /* ----------------------------------------------------------------------- */
322 /* This function takes the system resume data and formats it into a valid */
323 /* bookmark. */
324 /* ----------------------------------------------------------------------- */
325 static char* create_bookmark()
327 int resume_index = 0;
328 char *file;
330 /* grab the currently playing track */
331 struct mp3entry *id3 = audio_current_track();
332 if(!id3)
333 return NULL;
335 /* Get some basic resume information */
336 /* queue_resume and queue_resume_index are not used and can be ignored.*/
337 playlist_get_resume_info(&resume_index);
339 /* Get the currently playing file minus the path */
340 /* This is used when displaying the available bookmarks */
341 file = strrchr(id3->path,'/');
342 if(NULL == file)
343 return NULL;
345 /* create the bookmark */
346 snprintf(global_bookmark, sizeof(global_bookmark),
347 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
348 resume_index,
349 id3->offset,
350 playlist_get_seed(NULL),
352 id3->elapsed,
353 global_settings.repeat_mode,
354 global_settings.playlist_shuffle,
355 playlist_get_name(NULL, global_temp_buffer,
356 sizeof(global_temp_buffer)),
357 file+1);
359 /* checking to see if the bookmark is valid */
360 if (check_bookmark(global_bookmark))
361 return global_bookmark;
362 else
363 return NULL;
366 static bool check_bookmark(const char* bookmark)
368 return parse_bookmark(bookmark,
369 NULL,NULL,NULL, NULL,
370 NULL,0,NULL,NULL,
371 NULL, NULL);
374 /* ----------------------------------------------------------------------- */
375 /* This function will determine if an autoload is necessary. This is an */
376 /* interface function. */
377 /* ------------------------------------------------------------------------*/
378 bool bookmark_autoload(const char* file)
380 int fd;
382 if(global_settings.autoloadbookmark == BOOKMARK_NO)
383 return false;
385 /*Checking to see if a bookmark file exists.*/
386 if(!generate_bookmark_file_name(file))
388 return false;
390 fd = open(global_bookmark_file_name, O_RDONLY);
391 if(fd<0)
392 return false;
393 close(fd);
394 if(global_settings.autoloadbookmark == BOOKMARK_YES)
396 return bookmark_load(global_bookmark_file_name, true);
398 else
400 char* bookmark = select_bookmark(global_bookmark_file_name, true);
402 if (bookmark != NULL)
404 if (!play_bookmark(bookmark))
406 /* Selected bookmark not found. */
407 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
410 /* Act as if autoload was done even if it failed, since the
411 * user did make an active selection.
413 return true;
416 return false;
420 /* ----------------------------------------------------------------------- */
421 /* This function loads the bookmark information into the resume memory. */
422 /* This is an interface function. */
423 /* ------------------------------------------------------------------------*/
424 bool bookmark_load(const char* file, bool autoload)
426 int fd;
427 char* bookmark = NULL;
429 if(autoload)
431 fd = open(file, O_RDONLY);
432 if(fd >= 0)
434 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
435 bookmark=global_read_buffer;
436 close(fd);
439 else
441 /* This is not an auto-load, so list the bookmarks */
442 bookmark = select_bookmark(file, false);
445 if (bookmark != NULL)
447 if (!play_bookmark(bookmark))
449 /* Selected bookmark not found. */
450 if (!autoload)
452 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
455 return false;
459 return true;
463 static int get_bookmark_count(const char* bookmark_file_name)
465 int read_count = 0;
466 int file = open(bookmark_file_name, O_RDONLY);
468 if(file < 0)
469 return -1;
471 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
473 read_count++;
476 close(file);
477 return read_count;
480 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
482 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
483 int read_count = 0;
484 int file = open(bookmarks->filename, O_RDONLY);
486 if (file < 0)
488 return -1;
491 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
492 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
494 /* Entire file fits in buffer */
495 first_line = 0;
498 bookmarks->start = first_line;
499 bookmarks->count = 0;
500 bookmarks->reload = false;
502 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
504 read_count++;
506 if (read_count >= first_line)
508 dest -= strlen(global_read_buffer) + 1;
510 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
511 + (sizeof(char*) * (bookmarks->count + 1)))
513 break;
516 strcpy(dest, global_read_buffer);
517 bookmarks->items[bookmarks->count] = dest;
518 bookmarks->count++;
522 close(file);
523 return bookmarks->start + bookmarks->count;
526 static char* get_bookmark_info(int list_index, void* data, char *buffer)
528 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
529 int index = list_index / 2;
530 int resume_index = 0;
531 long resume_time = 0;
532 bool shuffle = false;
534 if (bookmarks->show_dont_resume)
536 if (index == 0)
538 return list_index % 2 == 0
539 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
542 index--;
545 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
546 || (index < bookmarks->start))
548 int read_index = index;
550 /* Using count as a guide on how far to move could possibly fail
551 * sometimes. Use byte count if that is a problem?
554 if (read_index != 0)
556 /* Move count * 3 / 4 items in the direction the user is moving,
557 * but don't go too close to the end.
559 int offset = bookmarks->count;
560 int max = bookmarks->total_count - (bookmarks->count / 2);
562 if (read_index < bookmarks->start)
564 offset *= 3;
567 read_index = index - offset / 4;
569 if (read_index > max)
571 read_index = max;
574 if (read_index < 0)
576 read_index = 0;
580 if (buffer_bookmarks(bookmarks, read_index) <= index)
582 return "";
586 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
587 &resume_index, NULL, NULL, NULL, global_temp_buffer,
588 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
589 global_filename))
591 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
594 if (list_index % 2 == 0)
596 char *name;
597 char *format;
598 int len = strlen(global_temp_buffer);
600 if (bookmarks->show_playlist_name && len > 0)
602 name = global_temp_buffer;
603 len--;
605 if (name[len] != '/')
607 strrsplt(name, '.');
609 else if (len > 1)
611 name[len] = '\0';
614 if (len > 1)
616 name = strrsplt(name, '/');
619 format = "%s : %s";
621 else
623 name = global_filename;
624 format = "%s";
627 strrsplt(global_filename, '.');
628 snprintf(buffer, MAX_PATH, format, name, global_filename);
629 return buffer;
631 else
633 char time_buf[32];
635 format_time(time_buf, sizeof(time_buf), resume_time);
636 snprintf(buffer, MAX_PATH, "%s, %d%s", time_buf, resume_index + 1,
637 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
638 return buffer;
642 /* ----------------------------------------------------------------------- */
643 /* This displays a the bookmarks in a file and allows the user to */
644 /* select one to play. */
645 /* ------------------------------------------------------------------------*/
646 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
648 struct bookmark_list* bookmarks;
649 struct gui_synclist list;
650 int last_item = -2;
651 int item = 0;
652 int action;
653 size_t size;
654 bool exit = false;
655 bool refresh = true;
657 bookmarks = plugin_get_buffer(&size);
658 bookmarks->buffer_size = size;
659 bookmarks->show_dont_resume = show_dont_resume;
660 bookmarks->filename = bookmark_file_name;
661 bookmarks->start = 0;
662 bookmarks->show_playlist_name
663 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
664 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
665 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
666 Icon_Bookmark);
667 gui_syncstatusbar_draw(&statusbars, true);
669 while (!exit)
671 gui_syncstatusbar_draw(&statusbars, false);
673 if (refresh)
675 int count = get_bookmark_count(bookmark_file_name);
676 bookmarks->total_count = count;
678 if (bookmarks->total_count < 1)
680 /* No more bookmarks, delete file and exit */
681 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
682 remove(bookmark_file_name);
683 return NULL;
686 if (bookmarks->show_dont_resume)
688 count++;
689 item++;
692 gui_synclist_set_nb_items(&list, count * 2);
694 if (item >= count)
696 /* Selected item has been deleted */
697 item = count - 1;
698 gui_synclist_select_item(&list, item * 2);
701 buffer_bookmarks(bookmarks, bookmarks->start);
702 gui_synclist_draw(&list);
703 refresh = false;
706 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
707 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
708 item = gui_synclist_get_sel_pos(&list) / 2;
710 if (bookmarks->show_dont_resume)
712 item--;
715 if (item != last_item && global_settings.talk_menu)
717 last_item = item;
719 if (item == -1)
721 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
723 else
725 say_bookmark(bookmarks->items[item - bookmarks->start], item);
729 if (action == ACTION_STD_CONTEXT)
731 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
732 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
733 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
734 static const int menu_actions[] =
736 ACTION_STD_OK, ACTION_BMS_DELETE
738 int selection = do_menu(&menu_items, NULL, NULL, false);
740 refresh = true;
742 if (selection >= 0 && selection <=
743 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
745 action = menu_actions[selection];
749 switch (action)
751 case ACTION_STD_OK:
752 if (item >= 0)
754 return bookmarks->items[item - bookmarks->start];
757 /* Else fall through */
759 case ACTION_TREE_WPS:
760 case ACTION_STD_CANCEL:
761 exit = true;
762 break;
764 case ACTION_BMS_DELETE:
765 if (item >= 0)
767 delete_bookmark(bookmark_file_name, item);
768 bookmarks->reload = true;
769 refresh = true;
770 last_item = -2;
772 break;
774 default:
775 if (default_event_handler(action) == SYS_USB_CONNECTED)
777 exit = true;
780 break;
784 return NULL;
787 /* ----------------------------------------------------------------------- */
788 /* This function takes a location in a bookmark file and deletes that */
789 /* bookmark. */
790 /* ------------------------------------------------------------------------*/
791 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
793 int temp_bookmark_file = 0;
794 int bookmark_file = 0;
795 int bookmark_count = 0;
797 /* Opening up a temp bookmark file */
798 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
799 "%s.tmp", bookmark_file_name);
800 temp_bookmark_file = open(global_temp_buffer,
801 O_WRONLY | O_CREAT | O_TRUNC);
803 if (temp_bookmark_file < 0)
804 return false; /* can't open the temp file */
806 /* Reading in the previous bookmarks and writing them to the temp file */
807 bookmark_file = open(bookmark_file_name, O_RDONLY);
808 if (bookmark_file >= 0)
810 while (read_line(bookmark_file, global_read_buffer,
811 sizeof(global_read_buffer)) > 0)
813 if (bookmark_id != bookmark_count)
815 write(temp_bookmark_file, global_read_buffer,
816 strlen(global_read_buffer));
817 write(temp_bookmark_file, "\n", 1);
819 bookmark_count++;
821 close(bookmark_file);
823 close(temp_bookmark_file);
825 remove(bookmark_file_name);
826 rename(global_temp_buffer, bookmark_file_name);
828 return true;
831 /* ----------------------------------------------------------------------- */
832 /* This function parses a bookmark, says the voice UI part of it. */
833 /* ------------------------------------------------------------------------*/
834 static void say_bookmark(const char* bookmark,
835 int bookmark_id)
837 int resume_index;
838 long ms;
839 bool enqueue = false; /* only the first voice is not queued */
841 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
842 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
844 talk_id(LANG_BOOKMARK_INVALID, true);
845 return;
848 /* disabled, because transition between talkbox and voice UI clip is not nice */
849 #if 0
850 if (global_settings.talk_dir >= 3)
851 { /* "talkbox" enabled */
852 char* last = strrchr(global_temp_buffer, '/');
853 if (last)
854 { /* compose filename for talkbox */
855 strncpy(last + 1, dir_thumbnail_name,
856 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
857 talk_file(global_temp_buffer, enqueue);
858 enqueue = true;
861 #endif
862 talk_id(VOICE_EXT_BMARK, enqueue);
863 talk_number(bookmark_id + 1, true);
864 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
865 talk_number(resume_index + 1, true);
866 talk_id(LANG_TIME, true);
867 if (ms / 60000)
868 talk_value(ms / 60000, UNIT_MIN, true);
869 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
872 /* ----------------------------------------------------------------------- */
873 /* This function parses a bookmark and then plays it. */
874 /* ------------------------------------------------------------------------*/
875 static bool play_bookmark(const char* bookmark)
877 int index;
878 int offset;
879 int seed;
881 if (parse_bookmark(bookmark,
882 &index,
883 &offset,
884 &seed,
885 NULL,
886 global_temp_buffer,
887 sizeof(global_temp_buffer),
888 NULL,
889 &global_settings.repeat_mode,
890 &global_settings.playlist_shuffle,
891 global_filename))
893 return bookmark_play(global_temp_buffer, index, offset, seed,
894 global_filename);
897 return false;
900 static const char* skip_token(const char* s)
902 while (*s && *s != ';')
904 s++;
907 if (*s)
909 s++;
912 return s;
915 static const char* int_token(const char* s, int* dest)
917 if (dest != NULL)
919 *dest = atoi(s);
922 return skip_token(s);
925 static const char* long_token(const char* s, long* dest)
927 if (dest != NULL)
929 *dest = atoi(s); /* Should be atol, but we don't have it. */
932 return skip_token(s);
935 static const char* bool_token(const char* s, bool* dest)
937 if (dest != NULL)
939 *dest = atoi(s) != 0;
942 return skip_token(s);
945 /* ----------------------------------------------------------------------- */
946 /* This function takes a bookmark and parses it. This function also */
947 /* validates the bookmark. Passing in NULL for an output variable */
948 /* indicates that value is not requested. */
949 /* ----------------------------------------------------------------------- */
950 static bool parse_bookmark(const char *bookmark,
951 int *resume_index,
952 int *resume_offset,
953 int *resume_seed,
954 int *resume_first_index,
955 char* resume_file,
956 unsigned int resume_file_size,
957 long* ms,
958 int * repeat_mode, bool *shuffle,
959 char* file_name)
961 const char* s = bookmark;
962 const char* end;
964 s = int_token(s, resume_index);
965 s = int_token(s, resume_offset);
966 s = int_token(s, resume_seed);
967 s = int_token(s, resume_first_index);
968 s = long_token(s, ms);
969 s = int_token(s, repeat_mode);
970 s = bool_token(s, shuffle);
972 if (*s == 0)
974 return false;
977 end = strchr(s, ';');
979 if (resume_file != NULL)
981 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
983 len = MIN(resume_file_size - 1, len);
984 strncpy(resume_file, s, len);
985 resume_file[len] = 0;
988 if (end != NULL && file_name != NULL)
990 end++;
991 strncpy(file_name, end, MAX_PATH - 1);
992 file_name[MAX_PATH - 1] = 0;
995 return true;
998 /* ----------------------------------------------------------------------- */
999 /* This function is used by multiple functions and is used to generate a */
1000 /* bookmark named based off of the input. */
1001 /* Changing this function could result in how the bookmarks are stored. */
1002 /* it would be here that the centralized/decentralized bookmark code */
1003 /* could be placed. */
1004 /* ----------------------------------------------------------------------- */
1005 static bool generate_bookmark_file_name(const char *in)
1007 int len = strlen(in);
1009 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1010 /* otherwise, name it based on the in variable */
1011 if (!strcmp("/", in))
1012 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1013 else
1015 strcpy(global_bookmark_file_name, in);
1016 if(global_bookmark_file_name[len-1] == '/')
1017 len--;
1018 strcpy(&global_bookmark_file_name[len], ".bmark");
1021 return true;
1024 /* ----------------------------------------------------------------------- */
1025 /* Returns true if a bookmark file exists for the current playlist */
1026 /* ----------------------------------------------------------------------- */
1027 bool bookmark_exist(void)
1029 bool exist=false;
1031 if(system_check())
1033 char* name = playlist_get_name(NULL, global_temp_buffer,
1034 sizeof(global_temp_buffer));
1035 if (generate_bookmark_file_name(name))
1037 int fd=open(global_bookmark_file_name, O_RDONLY);
1038 if (fd >=0)
1040 close(fd);
1041 exist=true;
1046 return exist;
1049 /* ----------------------------------------------------------------------- */
1050 /* Checks the current state of the system and returns if it is in a */
1051 /* bookmarkable state. */
1052 /* ----------------------------------------------------------------------- */
1053 /* Inputs: */
1054 /* ----------------------------------------------------------------------- */
1055 /* Outputs: */
1056 /* return bool: Indicates if the system was in a bookmarkable state */
1057 /* ----------------------------------------------------------------------- */
1058 static bool system_check(void)
1060 int resume_index = 0;
1062 if (!(audio_status() && audio_current_track()))
1064 /* no track playing */
1065 return false;
1068 /* Checking to see if playing a queued track */
1069 if (playlist_get_resume_info(&resume_index) == -1)
1071 /* something bad happened while getting the queue information */
1072 return false;
1074 else if (playlist_modified(NULL))
1076 /* can't bookmark while in the queue */
1077 return false;
1080 return true;