'Enable' the virtual keyboard button table in the c200 manual and correct the platfor...
[Rockbox.git] / apps / bookmark.c
blob3d4705960c5b2840898f64660c803063a0e641da
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"
55 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
56 #include "backdrop.h"
57 #endif
59 #define MAX_BOOKMARKS 10
60 #define MAX_BOOKMARK_SIZE 350
61 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
63 /* Used to buffer bookmarks while displaying the bookmark list. */
64 struct bookmark_list
66 const char* filename;
67 size_t buffer_size;
68 int start;
69 int count;
70 int total_count;
71 bool show_dont_resume;
72 bool reload;
73 bool show_playlist_name;
74 char* items[];
77 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
78 bool most_recent);
79 static bool check_bookmark(const char* bookmark);
80 static char* create_bookmark(void);
81 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
82 static void say_bookmark(const char* bookmark,
83 int bookmark_id);
84 static bool play_bookmark(const char* bookmark);
85 static bool generate_bookmark_file_name(const char *in);
86 static const char* skip_token(const char* s);
87 static const char* int_token(const char* s, int* dest);
88 static const char* long_token(const char* s, long* dest);
89 static const char* bool_token(const char* s, bool* dest);
90 static bool parse_bookmark(const char *bookmark,
91 int *resume_index,
92 int *resume_offset,
93 int *resume_seed,
94 int *resume_first_index,
95 char* resume_file,
96 unsigned int resume_file_size,
97 long* ms,
98 int * repeat_mode,
99 bool *shuffle,
100 char* file_name);
101 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
102 static char* get_bookmark_info(int list_index, void* data, char *buffer);
103 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
104 static bool system_check(void);
105 static bool write_bookmark(bool create_bookmark_file);
106 static int get_bookmark_count(const char* bookmark_file_name);
108 static char global_temp_buffer[MAX_PATH+1];
109 /* File name created by generate_bookmark_file_name */
110 static char global_bookmark_file_name[MAX_PATH];
111 static char global_read_buffer[MAX_BOOKMARK_SIZE];
112 /* Bookmark created by create_bookmark*/
113 static char global_bookmark[MAX_BOOKMARK_SIZE];
114 /* Filename from parsed bookmark (can be made local where needed) */
115 static char global_filename[MAX_PATH];
117 /* ----------------------------------------------------------------------- */
118 /* This is the interface function from the main menu. */
119 /* ----------------------------------------------------------------------- */
120 bool bookmark_create_menu(void)
122 write_bookmark(true);
123 return false;
126 /* ----------------------------------------------------------------------- */
127 /* This function acts as the load interface from the main menu */
128 /* This function determines the bookmark file name and then loads that file*/
129 /* for the user. The user can then select a bookmark to load. */
130 /* If no file/directory is currently playing, the menu item does not work. */
131 /* ----------------------------------------------------------------------- */
132 bool bookmark_load_menu(void)
134 if (system_check())
136 char* name = playlist_get_name(NULL, global_temp_buffer,
137 sizeof(global_temp_buffer));
138 if (generate_bookmark_file_name(name))
140 char* bookmark = select_bookmark(global_bookmark_file_name, false);
142 if (bookmark != NULL)
144 return play_bookmark(bookmark);
149 return false;
152 /* ----------------------------------------------------------------------- */
153 /* Gives the user a list of the Most Recent Bookmarks. This is an */
154 /* interface function */
155 /* ----------------------------------------------------------------------- */
156 bool bookmark_mrb_load()
158 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
160 if (bookmark != NULL)
162 return play_bookmark(bookmark);
165 return false;
168 /* ----------------------------------------------------------------------- */
169 /* This function handles an autobookmark creation. This is an interface */
170 /* function. */
171 /* ----------------------------------------------------------------------- */
172 bool bookmark_autobookmark(void)
174 if (!system_check())
175 return false;
177 audio_pause(); /* first pause playback */
178 switch (global_settings.autocreatebookmark)
180 case BOOKMARK_YES:
181 return write_bookmark(true);
183 case BOOKMARK_NO:
184 return false;
186 case BOOKMARK_RECENT_ONLY_YES:
187 return write_bookmark(false);
189 #ifdef HAVE_LCD_BITMAP
190 unsigned char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
191 struct text_message message={(char **)lines, 1};
192 #else
193 unsigned char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
194 str(LANG_CONFIRM_WITH_BUTTON)};
195 struct text_message message={(char **)lines, 2};
196 #endif
197 #if LCD_DEPTH > 1
198 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
199 #endif
200 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
201 show_remote_main_backdrop();
202 #endif
203 gui_syncstatusbar_draw(&statusbars, false);
204 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
206 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
207 return write_bookmark(false);
208 else
209 return write_bookmark(true);
211 return false;
214 /* ----------------------------------------------------------------------- */
215 /* This function takes the current current resume information and writes */
216 /* that to the beginning of the bookmark file. */
217 /* This file will contain N number of bookmarks in the following format: */
218 /* resume_index*resume_offset*resume_seed*resume_first_index* */
219 /* resume_file*milliseconds*MP3 Title* */
220 /* ------------------------------------------------------------------------*/
221 static bool write_bookmark(bool create_bookmark_file)
223 bool success=false;
224 char* bookmark;
226 if (!system_check())
227 return false; /* something didn't happen correctly, do nothing */
229 bookmark = create_bookmark();
230 if (!bookmark)
231 return false; /* something didn't happen correctly, do nothing */
233 if (global_settings.usemrb)
234 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
237 /* writing the bookmark */
238 if (create_bookmark_file)
240 char* name = playlist_get_name(NULL, global_temp_buffer,
241 sizeof(global_temp_buffer));
242 if (generate_bookmark_file_name(name))
244 success = add_bookmark(global_bookmark_file_name, bookmark, false);
248 gui_syncsplash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
249 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
251 return true;
254 /* ----------------------------------------------------------------------- */
255 /* This function adds a bookmark to a file. */
256 /* ------------------------------------------------------------------------*/
257 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
258 bool most_recent)
260 int temp_bookmark_file = 0;
261 int bookmark_file = 0;
262 int bookmark_count = 0;
263 char* playlist = NULL;
264 char* cp;
265 char* tmp;
266 int len = 0;
267 bool unique = false;
269 /* Opening up a temp bookmark file */
270 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
271 "%s.tmp", bookmark_file_name);
272 temp_bookmark_file = open(global_temp_buffer,
273 O_WRONLY | O_CREAT | O_TRUNC);
274 if (temp_bookmark_file < 0)
275 return false; /* can't open the temp file */
277 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
279 playlist = strchr(bookmark,'/');
280 cp = strrchr(bookmark,';');
281 len = cp - playlist;
282 unique = true;
285 /* Writing the new bookmark to the begining of the temp file */
286 write(temp_bookmark_file, bookmark, strlen(bookmark));
287 write(temp_bookmark_file, "\n", 1);
288 bookmark_count++;
290 /* Reading in the previous bookmarks and writing them to the temp file */
291 bookmark_file = open(bookmark_file_name, O_RDONLY);
292 if (bookmark_file >= 0)
294 while (read_line(bookmark_file, global_read_buffer,
295 sizeof(global_read_buffer)) > 0)
297 /* The MRB has a max of MAX_BOOKMARKS in it */
298 /* This keeps it from getting too large */
299 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
300 break;
302 cp = strchr(global_read_buffer,'/');
303 tmp = strrchr(global_read_buffer,';');
304 if (check_bookmark(global_read_buffer) &&
305 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
307 bookmark_count++;
308 write(temp_bookmark_file, global_read_buffer,
309 strlen(global_read_buffer));
310 write(temp_bookmark_file, "\n", 1);
313 close(bookmark_file);
315 close(temp_bookmark_file);
317 remove(bookmark_file_name);
318 rename(global_temp_buffer, bookmark_file_name);
320 return true;
324 /* ----------------------------------------------------------------------- */
325 /* This function takes the system resume data and formats it into a valid */
326 /* bookmark. */
327 /* ----------------------------------------------------------------------- */
328 static char* create_bookmark()
330 int resume_index = 0;
331 char *file;
333 /* grab the currently playing track */
334 struct mp3entry *id3 = audio_current_track();
335 if(!id3)
336 return NULL;
338 /* Get some basic resume information */
339 /* queue_resume and queue_resume_index are not used and can be ignored.*/
340 playlist_get_resume_info(&resume_index);
342 /* Get the currently playing file minus the path */
343 /* This is used when displaying the available bookmarks */
344 file = strrchr(id3->path,'/');
345 if(NULL == file)
346 return NULL;
348 /* create the bookmark */
349 snprintf(global_bookmark, sizeof(global_bookmark),
350 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
351 resume_index,
352 id3->offset,
353 playlist_get_seed(NULL),
355 id3->elapsed,
356 global_settings.repeat_mode,
357 global_settings.playlist_shuffle,
358 playlist_get_name(NULL, global_temp_buffer,
359 sizeof(global_temp_buffer)),
360 file+1);
362 /* checking to see if the bookmark is valid */
363 if (check_bookmark(global_bookmark))
364 return global_bookmark;
365 else
366 return NULL;
369 static bool check_bookmark(const char* bookmark)
371 return parse_bookmark(bookmark,
372 NULL,NULL,NULL, NULL,
373 NULL,0,NULL,NULL,
374 NULL, NULL);
377 /* ----------------------------------------------------------------------- */
378 /* This function will determine if an autoload is necessary. This is an */
379 /* interface function. */
380 /* ------------------------------------------------------------------------*/
381 bool bookmark_autoload(const char* file)
383 int fd;
385 if(global_settings.autoloadbookmark == BOOKMARK_NO)
386 return false;
388 /*Checking to see if a bookmark file exists.*/
389 if(!generate_bookmark_file_name(file))
391 return false;
393 fd = open(global_bookmark_file_name, O_RDONLY);
394 if(fd<0)
395 return false;
396 close(fd);
397 if(global_settings.autoloadbookmark == BOOKMARK_YES)
399 return bookmark_load(global_bookmark_file_name, true);
401 else
403 char* bookmark = select_bookmark(global_bookmark_file_name, true);
405 if (bookmark != NULL)
407 return play_bookmark(bookmark);
410 return false;
414 /* ----------------------------------------------------------------------- */
415 /* This function loads the bookmark information into the resume memory. */
416 /* This is an interface function. */
417 /* ------------------------------------------------------------------------*/
418 bool bookmark_load(const char* file, bool autoload)
420 int fd;
421 char* bookmark = NULL;;
423 if(autoload)
425 fd = open(file, O_RDONLY);
426 if(fd >= 0)
428 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
429 bookmark=global_read_buffer;
430 close(fd);
433 else
435 /* This is not an auto-load, so list the bookmarks */
436 bookmark = select_bookmark(file, false);
439 if (bookmark != NULL)
441 return play_bookmark(bookmark);
444 return true;
448 static int get_bookmark_count(const char* bookmark_file_name)
450 int read_count = 0;
451 int file = open(bookmark_file_name, O_RDONLY);
453 if(file < 0)
454 return -1;
456 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
458 read_count++;
461 close(file);
462 return read_count;
465 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
467 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
468 int read_count = 0;
469 int file = open(bookmarks->filename, O_RDONLY);
471 if (file < 0)
473 return -1;
476 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
477 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
479 /* Entire file fits in buffer */
480 first_line = 0;
483 bookmarks->start = first_line;
484 bookmarks->count = 0;
485 bookmarks->reload = false;
487 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
489 read_count++;
491 if (read_count >= first_line)
493 dest -= strlen(global_read_buffer) + 1;
495 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
496 + (sizeof(char*) * (bookmarks->count + 1)))
498 break;
501 strcpy(dest, global_read_buffer);
502 bookmarks->items[bookmarks->count] = dest;
503 bookmarks->count++;
507 close(file);
508 return bookmarks->start + bookmarks->count;
511 static char* get_bookmark_info(int list_index, void* data, char *buffer)
513 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
514 int index = list_index / 2;
515 int resume_index = 0;
516 long resume_time = 0;
517 bool shuffle = false;
519 if (bookmarks->show_dont_resume)
521 if (index == 0)
523 return list_index % 2 == 0
524 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
527 index--;
530 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
531 || (index < bookmarks->start))
533 int read_index = index;
535 /* Using count as a guide on how far to move could possibly fail
536 * sometimes. Use byte count if that is a problem?
539 if (read_index != 0)
541 /* Move count * 3 / 4 items in the direction the user is moving,
542 * but don't go too close to the end.
544 int offset = bookmarks->count;
545 int max = bookmarks->total_count - (bookmarks->count / 2);
547 if (read_index < bookmarks->start)
549 offset *= 3;
552 read_index = index - offset / 4;
554 if (read_index > max)
556 read_index = max;
559 if (read_index < 0)
561 read_index = 0;
565 if (buffer_bookmarks(bookmarks, read_index) <= index)
567 return "";
571 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
572 &resume_index, NULL, NULL, NULL, global_temp_buffer,
573 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
574 global_filename))
576 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
579 if (list_index % 2 == 0)
581 char *name;
582 char *format;
583 int len = strlen(global_temp_buffer);
585 if (bookmarks->show_playlist_name && len > 0)
587 name = global_temp_buffer;
588 len--;
590 if (name[len] != '/')
592 strrsplt(name, '.');
594 else if (len > 1)
596 name[len] = '\0';
599 if (len > 1)
601 name = strrsplt(name, '/');
604 format = "%s : %s";
606 else
608 name = global_filename;
609 format = "%s";
612 strrsplt(global_filename, '.');
613 snprintf(buffer, MAX_PATH, format, name, global_filename);
614 return buffer;
616 else
618 char time_buf[32];
620 format_time(time_buf, sizeof(time_buf), resume_time);
621 snprintf(buffer, MAX_PATH, "%s, %d%s", time_buf, resume_index + 1,
622 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
623 return buffer;
627 /* ----------------------------------------------------------------------- */
628 /* This displays a the bookmarks in a file and allows the user to */
629 /* select one to play. */
630 /* ------------------------------------------------------------------------*/
631 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
633 struct bookmark_list* bookmarks;
634 struct gui_synclist list;
635 int last_item = -2;
636 int item = 0;
637 int action;
638 size_t size;
639 bool exit = false;
640 bool refresh = true;
642 bookmarks = plugin_get_buffer(&size);
643 bookmarks->buffer_size = size;
644 bookmarks->show_dont_resume = show_dont_resume;
645 bookmarks->filename = bookmark_file_name;
646 bookmarks->start = 0;
647 bookmarks->show_playlist_name
648 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
649 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2);
650 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
651 Icon_Bookmark);
652 gui_syncstatusbar_draw(&statusbars, true);
654 while (!exit)
656 gui_syncstatusbar_draw(&statusbars, false);
658 if (refresh)
660 int count = get_bookmark_count(bookmark_file_name);
661 bookmarks->total_count = count;
663 if (bookmarks->total_count < 1)
665 /* No more bookmarks, delete file and exit */
666 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
667 remove(bookmark_file_name);
668 return NULL;
671 if (bookmarks->show_dont_resume)
673 count++;
674 item++;
677 gui_synclist_set_nb_items(&list, count * 2);
679 if (item >= count)
681 /* Selected item has been deleted */
682 item = count - 1;
683 gui_synclist_select_item(&list, item * 2);
686 buffer_bookmarks(bookmarks, bookmarks->start);
687 gui_synclist_draw(&list);
688 refresh = false;
691 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
692 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
693 item = gui_synclist_get_sel_pos(&list) / 2;
695 if (bookmarks->show_dont_resume)
697 item--;
700 if (item != last_item && global_settings.talk_menu)
702 last_item = item;
704 if (item == -1)
706 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
708 else
710 say_bookmark(bookmarks->items[item - bookmarks->start], item);
714 if (action == ACTION_STD_CONTEXT)
716 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
717 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
718 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
719 static const int menu_actions[] =
721 ACTION_STD_OK, ACTION_BMS_DELETE
723 int selection = do_menu(&menu_items, NULL);
725 refresh = true;
727 if (selection >= 0 && selection <=
728 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
730 action = menu_actions[selection];
734 switch (action)
736 case ACTION_STD_OK:
737 if (item >= 0)
739 return bookmarks->items[item - bookmarks->start];
742 /* Else fall through */
744 case ACTION_TREE_WPS:
745 case ACTION_STD_CANCEL:
746 exit = true;
747 break;
749 case ACTION_BMS_DELETE:
750 if (item >= 0)
752 delete_bookmark(bookmark_file_name, item);
753 bookmarks->reload = true;
754 refresh = true;
755 last_item = -2;
757 break;
759 default:
760 if (default_event_handler(action) == SYS_USB_CONNECTED)
762 exit = true;
765 break;
769 return NULL;
772 /* ----------------------------------------------------------------------- */
773 /* This function takes a location in a bookmark file and deletes that */
774 /* bookmark. */
775 /* ------------------------------------------------------------------------*/
776 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
778 int temp_bookmark_file = 0;
779 int bookmark_file = 0;
780 int bookmark_count = 0;
782 /* Opening up a temp bookmark file */
783 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
784 "%s.tmp", bookmark_file_name);
785 temp_bookmark_file = open(global_temp_buffer,
786 O_WRONLY | O_CREAT | O_TRUNC);
788 if (temp_bookmark_file < 0)
789 return false; /* can't open the temp file */
791 /* Reading in the previous bookmarks and writing them to the temp file */
792 bookmark_file = open(bookmark_file_name, O_RDONLY);
793 if (bookmark_file >= 0)
795 while (read_line(bookmark_file, global_read_buffer,
796 sizeof(global_read_buffer)) > 0)
798 if (bookmark_id != bookmark_count)
800 write(temp_bookmark_file, global_read_buffer,
801 strlen(global_read_buffer));
802 write(temp_bookmark_file, "\n", 1);
804 bookmark_count++;
806 close(bookmark_file);
808 close(temp_bookmark_file);
810 remove(bookmark_file_name);
811 rename(global_temp_buffer, bookmark_file_name);
813 return true;
816 /* ----------------------------------------------------------------------- */
817 /* This function parses a bookmark, says the voice UI part of it. */
818 /* ------------------------------------------------------------------------*/
819 static void say_bookmark(const char* bookmark,
820 int bookmark_id)
822 int resume_index;
823 long ms;
824 bool enqueue = false; /* only the first voice is not queued */
826 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
827 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
829 talk_id(LANG_BOOKMARK_INVALID, true);
830 return;
833 /* disabled, because transition between talkbox and voice UI clip is not nice */
834 #if 0
835 if (global_settings.talk_dir >= 3)
836 { /* "talkbox" enabled */
837 char* last = strrchr(global_temp_buffer, '/');
838 if (last)
839 { /* compose filename for talkbox */
840 strncpy(last + 1, dir_thumbnail_name,
841 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
842 talk_file(global_temp_buffer, enqueue);
843 enqueue = true;
846 #endif
847 talk_id(VOICE_EXT_BMARK, enqueue);
848 talk_number(bookmark_id + 1, true);
849 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
850 talk_number(resume_index + 1, true);
851 talk_id(LANG_TIME, true);
852 if (ms / 60000)
853 talk_value(ms / 60000, UNIT_MIN, true);
854 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
857 /* ----------------------------------------------------------------------- */
858 /* This function parses a bookmark and then plays it. */
859 /* ------------------------------------------------------------------------*/
860 static bool play_bookmark(const char* bookmark)
862 int index;
863 int offset;
864 int seed;
866 if (parse_bookmark(bookmark,
867 &index,
868 &offset,
869 &seed,
870 NULL,
871 global_temp_buffer,
872 sizeof(global_temp_buffer),
873 NULL,
874 &global_settings.repeat_mode,
875 &global_settings.playlist_shuffle,
876 global_filename))
878 bookmark_play(global_temp_buffer, index, offset, seed,
879 global_filename);
880 return true;
883 return false;
886 static const char* skip_token(const char* s)
888 while (*s && *s != ';')
890 s++;
893 if (*s)
895 s++;
898 return s;
901 static const char* int_token(const char* s, int* dest)
903 if (dest != NULL)
905 *dest = atoi(s);
908 return skip_token(s);
911 static const char* long_token(const char* s, long* dest)
913 if (dest != NULL)
915 *dest = atoi(s); /* Should be atol, but we don't have it. */
918 return skip_token(s);
921 static const char* bool_token(const char* s, bool* dest)
923 if (dest != NULL)
925 *dest = atoi(s) != 0;
928 return skip_token(s);
931 /* ----------------------------------------------------------------------- */
932 /* This function takes a bookmark and parses it. This function also */
933 /* validates the bookmark. Passing in NULL for an output variable */
934 /* indicates that value is not requested. */
935 /* ----------------------------------------------------------------------- */
936 static bool parse_bookmark(const char *bookmark,
937 int *resume_index,
938 int *resume_offset,
939 int *resume_seed,
940 int *resume_first_index,
941 char* resume_file,
942 unsigned int resume_file_size,
943 long* ms,
944 int * repeat_mode, bool *shuffle,
945 char* file_name)
947 const char* s = bookmark;
948 const char* end;
950 s = int_token(s, resume_index);
951 s = int_token(s, resume_offset);
952 s = int_token(s, resume_seed);
953 s = int_token(s, resume_first_index);
954 s = long_token(s, ms);
955 s = int_token(s, repeat_mode);
956 s = bool_token(s, shuffle);
958 if (*s == 0)
960 return false;
963 end = strchr(s, ';');
965 if (resume_file != NULL)
967 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
969 len = MIN(resume_file_size - 1, len);
970 strncpy(resume_file, s, len);
971 resume_file[len] = 0;
974 if (end != NULL && file_name != NULL)
976 end++;
977 strncpy(file_name, end, MAX_PATH - 1);
978 file_name[MAX_PATH - 1] = 0;
981 return true;
984 /* ----------------------------------------------------------------------- */
985 /* This function is used by multiple functions and is used to generate a */
986 /* bookmark named based off of the input. */
987 /* Changing this function could result in how the bookmarks are stored. */
988 /* it would be here that the centralized/decentralized bookmark code */
989 /* could be placed. */
990 /* ----------------------------------------------------------------------- */
991 static bool generate_bookmark_file_name(const char *in)
993 int len = strlen(in);
995 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
996 /* otherwise, name it based on the in variable */
997 if (!strcmp("/", in))
998 strcpy(global_bookmark_file_name, "/root_dir.bmark");
999 else
1001 strcpy(global_bookmark_file_name, in);
1002 if(global_bookmark_file_name[len-1] == '/')
1003 len--;
1004 strcpy(&global_bookmark_file_name[len], ".bmark");
1007 return true;
1010 /* ----------------------------------------------------------------------- */
1011 /* Returns true if a bookmark file exists for the current playlist */
1012 /* ----------------------------------------------------------------------- */
1013 bool bookmark_exist(void)
1015 bool exist=false;
1017 if(system_check())
1019 char* name = playlist_get_name(NULL, global_temp_buffer,
1020 sizeof(global_temp_buffer));
1021 if (generate_bookmark_file_name(name))
1023 int fd=open(global_bookmark_file_name, O_RDONLY);
1024 if (fd >=0)
1026 close(fd);
1027 exist=true;
1032 return exist;
1035 /* ----------------------------------------------------------------------- */
1036 /* Checks the current state of the system and returns if it is in a */
1037 /* bookmarkable state. */
1038 /* ----------------------------------------------------------------------- */
1039 /* Inputs: */
1040 /* ----------------------------------------------------------------------- */
1041 /* Outputs: */
1042 /* return bool: Indicates if the system was in a bookmarkable state */
1043 /* ----------------------------------------------------------------------- */
1044 static bool system_check(void)
1046 int resume_index = 0;
1048 if (!(audio_status() && audio_current_track()))
1050 /* no track playing */
1051 return false;
1054 /* Checking to see if playing a queued track */
1055 if (playlist_get_resume_info(&resume_index) == -1)
1057 /* something bad happened while getting the queue information */
1058 return false;
1060 else if (playlist_modified(NULL))
1062 /* can't bookmark while in the queue */
1063 return false;
1066 return true;