Simplify touchscreen scrollbar handling code
[Rockbox.git] / apps / bookmark.c
blobf968c5e1e8eb9e2bb513d922e2f827293a928632
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);
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);
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);
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 if (!system_check())
166 return false;
168 audio_pause(); /* first pause playback */
169 switch (global_settings.autocreatebookmark)
171 case BOOKMARK_YES:
172 return write_bookmark(true);
174 case BOOKMARK_NO:
175 return false;
177 case BOOKMARK_RECENT_ONLY_YES:
178 return write_bookmark(false);
180 #ifdef HAVE_LCD_BITMAP
181 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
182 const struct text_message message={lines, 1};
183 #else
184 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
185 str(LANG_CONFIRM_WITH_BUTTON)};
186 const struct text_message message={lines, 2};
187 #endif
188 #if LCD_DEPTH > 1
189 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
190 #endif
191 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
192 show_remote_main_backdrop();
193 #endif
194 gui_syncstatusbar_draw(&statusbars, false);
195 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
197 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
198 return write_bookmark(false);
199 else
200 return write_bookmark(true);
202 return false;
205 /* ----------------------------------------------------------------------- */
206 /* This function takes the current current resume information and writes */
207 /* that to the beginning of the bookmark file. */
208 /* This file will contain N number of bookmarks in the following format: */
209 /* resume_index*resume_offset*resume_seed*resume_first_index* */
210 /* resume_file*milliseconds*MP3 Title* */
211 /* ------------------------------------------------------------------------*/
212 static bool write_bookmark(bool create_bookmark_file)
214 bool success=false;
215 char* bookmark;
217 if (!system_check())
218 return false; /* something didn't happen correctly, do nothing */
220 bookmark = create_bookmark();
221 if (!bookmark)
222 return false; /* something didn't happen correctly, do nothing */
224 if (global_settings.usemrb)
225 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
228 /* writing the bookmark */
229 if (create_bookmark_file)
231 char* name = playlist_get_name(NULL, global_temp_buffer,
232 sizeof(global_temp_buffer));
233 if (generate_bookmark_file_name(name))
235 success = add_bookmark(global_bookmark_file_name, bookmark, false);
239 gui_syncsplash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
240 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
242 return true;
245 /* ----------------------------------------------------------------------- */
246 /* This function adds a bookmark to a file. */
247 /* ------------------------------------------------------------------------*/
248 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
249 bool most_recent)
251 int temp_bookmark_file = 0;
252 int bookmark_file = 0;
253 int bookmark_count = 0;
254 char* playlist = NULL;
255 char* cp;
256 char* tmp;
257 int len = 0;
258 bool unique = false;
260 /* Opening up a temp bookmark file */
261 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
262 "%s.tmp", bookmark_file_name);
263 temp_bookmark_file = open(global_temp_buffer,
264 O_WRONLY | O_CREAT | O_TRUNC);
265 if (temp_bookmark_file < 0)
266 return false; /* can't open the temp file */
268 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
270 playlist = strchr(bookmark,'/');
271 cp = strrchr(bookmark,';');
272 len = cp - playlist;
273 unique = true;
276 /* Writing the new bookmark to the begining of the temp file */
277 write(temp_bookmark_file, bookmark, strlen(bookmark));
278 write(temp_bookmark_file, "\n", 1);
279 bookmark_count++;
281 /* Reading in the previous bookmarks and writing them to the temp file */
282 bookmark_file = open(bookmark_file_name, O_RDONLY);
283 if (bookmark_file >= 0)
285 while (read_line(bookmark_file, global_read_buffer,
286 sizeof(global_read_buffer)) > 0)
288 /* The MRB has a max of MAX_BOOKMARKS in it */
289 /* This keeps it from getting too large */
290 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
291 break;
293 cp = strchr(global_read_buffer,'/');
294 tmp = strrchr(global_read_buffer,';');
295 if (check_bookmark(global_read_buffer) &&
296 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
298 bookmark_count++;
299 write(temp_bookmark_file, global_read_buffer,
300 strlen(global_read_buffer));
301 write(temp_bookmark_file, "\n", 1);
304 close(bookmark_file);
306 close(temp_bookmark_file);
308 remove(bookmark_file_name);
309 rename(global_temp_buffer, bookmark_file_name);
311 return true;
315 /* ----------------------------------------------------------------------- */
316 /* This function takes the system resume data and formats it into a valid */
317 /* bookmark. */
318 /* ----------------------------------------------------------------------- */
319 static char* create_bookmark()
321 int resume_index = 0;
322 char *file;
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 gui_syncsplash(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 gui_syncsplash(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 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 /* ----------------------------------------------------------------------- */
638 /* This displays a the bookmarks in a file and allows the user to */
639 /* select one to play. */
640 /* ------------------------------------------------------------------------*/
641 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
643 struct bookmark_list* bookmarks;
644 struct gui_synclist list;
645 int last_item = -2;
646 int item = 0;
647 int action;
648 size_t size;
649 bool exit = false;
650 bool refresh = true;
652 bookmarks = plugin_get_buffer(&size);
653 bookmarks->buffer_size = size;
654 bookmarks->show_dont_resume = show_dont_resume;
655 bookmarks->filename = bookmark_file_name;
656 bookmarks->start = 0;
657 bookmarks->show_playlist_name
658 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
659 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
660 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
661 Icon_Bookmark);
662 gui_syncstatusbar_draw(&statusbars, true);
664 while (!exit)
666 gui_syncstatusbar_draw(&statusbars, false);
668 if (refresh)
670 int count = get_bookmark_count(bookmark_file_name);
671 bookmarks->total_count = count;
673 if (bookmarks->total_count < 1)
675 /* No more bookmarks, delete file and exit */
676 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
677 remove(bookmark_file_name);
678 return NULL;
681 if (bookmarks->show_dont_resume)
683 count++;
684 item++;
687 gui_synclist_set_nb_items(&list, count * 2);
689 if (item >= count)
691 /* Selected item has been deleted */
692 item = count - 1;
693 gui_synclist_select_item(&list, item * 2);
696 buffer_bookmarks(bookmarks, bookmarks->start);
697 gui_synclist_draw(&list);
698 refresh = false;
701 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
702 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
703 item = gui_synclist_get_sel_pos(&list) / 2;
705 if (bookmarks->show_dont_resume)
707 item--;
710 if (item != last_item && global_settings.talk_menu)
712 last_item = item;
714 if (item == -1)
716 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
718 else
720 say_bookmark(bookmarks->items[item - bookmarks->start], item);
724 if (action == ACTION_STD_CONTEXT)
726 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
727 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
728 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
729 static const int menu_actions[] =
731 ACTION_STD_OK, ACTION_BMS_DELETE
733 int selection = do_menu(&menu_items, NULL, NULL, false);
735 refresh = true;
737 if (selection >= 0 && selection <=
738 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
740 action = menu_actions[selection];
744 switch (action)
746 case ACTION_STD_OK:
747 if (item >= 0)
749 return bookmarks->items[item - bookmarks->start];
752 /* Else fall through */
754 case ACTION_TREE_WPS:
755 case ACTION_STD_CANCEL:
756 exit = true;
757 break;
759 case ACTION_BMS_DELETE:
760 if (item >= 0)
762 delete_bookmark(bookmark_file_name, item);
763 bookmarks->reload = true;
764 refresh = true;
765 last_item = -2;
767 break;
769 default:
770 if (default_event_handler(action) == SYS_USB_CONNECTED)
772 exit = true;
775 break;
779 return NULL;
782 /* ----------------------------------------------------------------------- */
783 /* This function takes a location in a bookmark file and deletes that */
784 /* bookmark. */
785 /* ------------------------------------------------------------------------*/
786 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
788 int temp_bookmark_file = 0;
789 int bookmark_file = 0;
790 int bookmark_count = 0;
792 /* Opening up a temp bookmark file */
793 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
794 "%s.tmp", bookmark_file_name);
795 temp_bookmark_file = open(global_temp_buffer,
796 O_WRONLY | O_CREAT | O_TRUNC);
798 if (temp_bookmark_file < 0)
799 return false; /* can't open the temp file */
801 /* Reading in the previous bookmarks and writing them to the temp file */
802 bookmark_file = open(bookmark_file_name, O_RDONLY);
803 if (bookmark_file >= 0)
805 while (read_line(bookmark_file, global_read_buffer,
806 sizeof(global_read_buffer)) > 0)
808 if (bookmark_id != bookmark_count)
810 write(temp_bookmark_file, global_read_buffer,
811 strlen(global_read_buffer));
812 write(temp_bookmark_file, "\n", 1);
814 bookmark_count++;
816 close(bookmark_file);
818 close(temp_bookmark_file);
820 remove(bookmark_file_name);
821 rename(global_temp_buffer, bookmark_file_name);
823 return true;
826 /* ----------------------------------------------------------------------- */
827 /* This function parses a bookmark, says the voice UI part of it. */
828 /* ------------------------------------------------------------------------*/
829 static void say_bookmark(const char* bookmark,
830 int bookmark_id)
832 int resume_index;
833 long ms;
834 bool enqueue = false; /* only the first voice is not queued */
836 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
837 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
839 talk_id(LANG_BOOKMARK_INVALID, true);
840 return;
843 /* disabled, because transition between talkbox and voice UI clip is not nice */
844 #if 0
845 if (global_settings.talk_dir >= 3)
846 { /* "talkbox" enabled */
847 char* last = strrchr(global_temp_buffer, '/');
848 if (last)
849 { /* compose filename for talkbox */
850 strncpy(last + 1, dir_thumbnail_name,
851 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
852 talk_file(global_temp_buffer, enqueue);
853 enqueue = true;
856 #endif
857 talk_id(VOICE_EXT_BMARK, enqueue);
858 talk_number(bookmark_id + 1, true);
859 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
860 talk_number(resume_index + 1, true);
861 talk_id(LANG_TIME, true);
862 if (ms / 60000)
863 talk_value(ms / 60000, UNIT_MIN, true);
864 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
867 /* ----------------------------------------------------------------------- */
868 /* This function parses a bookmark and then plays it. */
869 /* ------------------------------------------------------------------------*/
870 static bool play_bookmark(const char* bookmark)
872 int index;
873 int offset;
874 int seed;
876 if (parse_bookmark(bookmark,
877 &index,
878 &offset,
879 &seed,
880 NULL,
881 global_temp_buffer,
882 sizeof(global_temp_buffer),
883 NULL,
884 &global_settings.repeat_mode,
885 &global_settings.playlist_shuffle,
886 global_filename))
888 return bookmark_play(global_temp_buffer, index, offset, seed,
889 global_filename);
892 return false;
895 static const char* skip_token(const char* s)
897 while (*s && *s != ';')
899 s++;
902 if (*s)
904 s++;
907 return s;
910 static const char* int_token(const char* s, int* dest)
912 if (dest != NULL)
914 *dest = atoi(s);
917 return skip_token(s);
920 static const char* long_token(const char* s, long* dest)
922 if (dest != NULL)
924 *dest = atoi(s); /* Should be atol, but we don't have it. */
927 return skip_token(s);
930 static const char* bool_token(const char* s, bool* dest)
932 if (dest != NULL)
934 *dest = atoi(s) != 0;
937 return skip_token(s);
940 /* ----------------------------------------------------------------------- */
941 /* This function takes a bookmark and parses it. This function also */
942 /* validates the bookmark. Passing in NULL for an output variable */
943 /* indicates that value is not requested. */
944 /* ----------------------------------------------------------------------- */
945 static bool parse_bookmark(const char *bookmark,
946 int *resume_index,
947 int *resume_offset,
948 int *resume_seed,
949 int *resume_first_index,
950 char* resume_file,
951 unsigned int resume_file_size,
952 long* ms,
953 int * repeat_mode, bool *shuffle,
954 char* file_name)
956 const char* s = bookmark;
957 const char* end;
959 s = int_token(s, resume_index);
960 s = int_token(s, resume_offset);
961 s = int_token(s, resume_seed);
962 s = int_token(s, resume_first_index);
963 s = long_token(s, ms);
964 s = int_token(s, repeat_mode);
965 s = bool_token(s, shuffle);
967 if (*s == 0)
969 return false;
972 end = strchr(s, ';');
974 if (resume_file != NULL)
976 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
978 len = MIN(resume_file_size - 1, len);
979 strncpy(resume_file, s, len);
980 resume_file[len] = 0;
983 if (end != NULL && file_name != NULL)
985 end++;
986 strncpy(file_name, end, MAX_PATH - 1);
987 file_name[MAX_PATH - 1] = 0;
990 return true;
993 /* ----------------------------------------------------------------------- */
994 /* This function is used by multiple functions and is used to generate a */
995 /* bookmark named based off of the input. */
996 /* Changing this function could result in how the bookmarks are stored. */
997 /* it would be here that the centralized/decentralized bookmark code */
998 /* could be placed. */
999 /* ----------------------------------------------------------------------- */
1000 static bool generate_bookmark_file_name(const char *in)
1002 int len = strlen(in);
1004 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1005 /* otherwise, name it based on the in variable */
1006 if (!strcmp("/", in))
1007 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1008 else
1010 strcpy(global_bookmark_file_name, in);
1011 if(global_bookmark_file_name[len-1] == '/')
1012 len--;
1013 strcpy(&global_bookmark_file_name[len], ".bmark");
1016 return true;
1019 /* ----------------------------------------------------------------------- */
1020 /* Returns true if a bookmark file exists for the current playlist */
1021 /* ----------------------------------------------------------------------- */
1022 bool bookmark_exist(void)
1024 bool exist=false;
1026 if(system_check())
1028 char* name = playlist_get_name(NULL, global_temp_buffer,
1029 sizeof(global_temp_buffer));
1030 if (generate_bookmark_file_name(name))
1032 exist = file_exists(global_bookmark_file_name);
1036 return exist;
1039 /* ----------------------------------------------------------------------- */
1040 /* Checks the current state of the system and returns if it is in a */
1041 /* bookmarkable state. */
1042 /* ----------------------------------------------------------------------- */
1043 /* Inputs: */
1044 /* ----------------------------------------------------------------------- */
1045 /* Outputs: */
1046 /* return bool: Indicates if the system was in a bookmarkable state */
1047 /* ----------------------------------------------------------------------- */
1048 static bool system_check(void)
1050 int resume_index = 0;
1052 if (!(audio_status() && audio_current_track()))
1054 /* no track playing */
1055 return false;
1058 /* Checking to see if playing a queued track */
1059 if (playlist_get_resume_info(&resume_index) == -1)
1061 /* something bad happened while getting the queue information */
1062 return false;
1064 else if (playlist_modified(NULL))
1066 /* can't bookmark while in the queue */
1067 return false;
1070 return true;