rbutil: Add quessing of the Drive letter via Windows API, also restructuring for...
[kugel-rb.git] / apps / bookmark.c
blobf36545fe3998e819c4c42d9b63ba20aeaf9a3714
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 char* items[];
76 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
77 bool most_recent);
78 static bool check_bookmark(const char* bookmark);
79 static char* create_bookmark(void);
80 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
81 static void say_bookmark(const char* bookmark,
82 int bookmark_id);
83 static bool play_bookmark(const char* bookmark);
84 static bool generate_bookmark_file_name(const char *in);
85 static const char* skip_token(const char* s);
86 static const char* int_token(const char* s, int* dest);
87 static const char* long_token(const char* s, long* dest);
88 static const char* bool_token(const char* s, bool* dest);
89 static bool parse_bookmark(const char *bookmark,
90 int *resume_index,
91 int *resume_offset,
92 int *resume_seed,
93 int *resume_first_index,
94 char* resume_file,
95 unsigned int resume_file_size,
96 long* ms,
97 int * repeat_mode,
98 bool *shuffle,
99 char* file_name);
100 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
101 static char* get_bookmark_info(int list_index, void* data, char *buffer);
102 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
103 static bool system_check(void);
104 static bool write_bookmark(bool create_bookmark_file);
105 static int get_bookmark_count(const char* bookmark_file_name);
107 static char global_temp_buffer[MAX_PATH+1];
108 /* File name created by generate_bookmark_file_name */
109 static char global_bookmark_file_name[MAX_PATH];
110 static char global_read_buffer[MAX_BOOKMARK_SIZE];
111 /* Bookmark created by create_bookmark*/
112 static char global_bookmark[MAX_BOOKMARK_SIZE];
113 /* Filename from parsed bookmark (can be made local where needed) */
114 static char global_filename[MAX_PATH];
116 /* ----------------------------------------------------------------------- */
117 /* This is the interface function from the main menu. */
118 /* ----------------------------------------------------------------------- */
119 bool bookmark_create_menu(void)
121 write_bookmark(true);
122 return false;
125 /* ----------------------------------------------------------------------- */
126 /* This function acts as the load interface from the main menu */
127 /* This function determines the bookmark file name and then loads that file*/
128 /* for the user. The user can then select a bookmark to load. */
129 /* If no file/directory is currently playing, the menu item does not work. */
130 /* ----------------------------------------------------------------------- */
131 bool bookmark_load_menu(void)
133 if (system_check())
135 char* name = playlist_get_name(NULL, global_temp_buffer,
136 sizeof(global_temp_buffer));
137 if (generate_bookmark_file_name(name))
139 char* bookmark = select_bookmark(global_bookmark_file_name, false);
141 if (bookmark != NULL)
143 return play_bookmark(bookmark);
148 return false;
151 /* ----------------------------------------------------------------------- */
152 /* Gives the user a list of the Most Recent Bookmarks. This is an */
153 /* interface function */
154 /* ----------------------------------------------------------------------- */
155 bool bookmark_mrb_load()
157 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
159 if (bookmark != NULL)
161 return play_bookmark(bookmark);
164 return false;
167 /* ----------------------------------------------------------------------- */
168 /* This function handles an autobookmark creation. This is an interface */
169 /* function. */
170 /* ----------------------------------------------------------------------- */
171 bool bookmark_autobookmark(void)
173 if (!system_check())
174 return false;
176 audio_pause(); /* first pause playback */
177 switch (global_settings.autocreatebookmark)
179 case BOOKMARK_YES:
180 return write_bookmark(true);
182 case BOOKMARK_NO:
183 return false;
185 case BOOKMARK_RECENT_ONLY_YES:
186 return write_bookmark(false);
188 #ifdef HAVE_LCD_BITMAP
189 unsigned char *lines[]={str(LANG_AUTO_BOOKMARK_QUERY)};
190 struct text_message message={(char **)lines, 1};
191 #else
192 unsigned char *lines[]={str(LANG_AUTO_BOOKMARK_QUERY),
193 str(LANG_RESUME_CONFIRM_PLAYER)};
194 struct text_message message={(char **)lines, 2};
195 #endif
196 #if LCD_DEPTH > 1
197 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
198 #endif
199 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
200 show_remote_main_backdrop();
201 #endif
202 gui_syncstatusbar_draw(&statusbars, false);
203 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
205 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
206 return write_bookmark(false);
207 else
208 return write_bookmark(true);
210 return false;
213 /* ----------------------------------------------------------------------- */
214 /* This function takes the current current resume information and writes */
215 /* that to the beginning of the bookmark file. */
216 /* This file will contain N number of bookmarks in the following format: */
217 /* resume_index*resume_offset*resume_seed*resume_first_index* */
218 /* resume_file*milliseconds*MP3 Title* */
219 /* ------------------------------------------------------------------------*/
220 static bool write_bookmark(bool create_bookmark_file)
222 bool success=false;
223 char* bookmark;
225 if (!system_check())
226 return false; /* something didn't happen correctly, do nothing */
228 bookmark = create_bookmark();
229 if (!bookmark)
230 return false; /* something didn't happen correctly, do nothing */
232 if (global_settings.usemrb)
233 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
236 /* writing the bookmark */
237 if (create_bookmark_file)
239 char* name = playlist_get_name(NULL, global_temp_buffer,
240 sizeof(global_temp_buffer));
241 if (generate_bookmark_file_name(name))
243 success = add_bookmark(global_bookmark_file_name, bookmark, false);
247 gui_syncsplash(HZ, str(success ? LANG_BOOKMARK_CREATE_SUCCESS
248 : LANG_BOOKMARK_CREATE_FAILURE));
250 return true;
253 /* ----------------------------------------------------------------------- */
254 /* This function adds a bookmark to a file. */
255 /* ------------------------------------------------------------------------*/
256 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
257 bool most_recent)
259 int temp_bookmark_file = 0;
260 int bookmark_file = 0;
261 int bookmark_count = 0;
262 char* playlist = NULL;
263 char* cp;
264 char* tmp;
265 int len = 0;
266 bool unique = false;
268 /* Opening up a temp bookmark file */
269 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
270 "%s.tmp", bookmark_file_name);
271 temp_bookmark_file = open(global_temp_buffer,
272 O_WRONLY | O_CREAT | O_TRUNC);
273 if (temp_bookmark_file < 0)
274 return false; /* can't open the temp file */
276 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
278 playlist = strchr(bookmark,'/');
279 cp = strrchr(bookmark,';');
280 len = cp - playlist;
281 unique = true;
284 /* Writing the new bookmark to the begining of the temp file */
285 write(temp_bookmark_file, bookmark, strlen(bookmark));
286 write(temp_bookmark_file, "\n", 1);
287 bookmark_count++;
289 /* Reading in the previous bookmarks and writing them to the temp file */
290 bookmark_file = open(bookmark_file_name, O_RDONLY);
291 if (bookmark_file >= 0)
293 while (read_line(bookmark_file, global_read_buffer,
294 sizeof(global_read_buffer)) > 0)
296 /* The MRB has a max of MAX_BOOKMARKS in it */
297 /* This keeps it from getting too large */
298 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
299 break;
301 cp = strchr(global_read_buffer,'/');
302 tmp = strrchr(global_read_buffer,';');
303 if (check_bookmark(global_read_buffer) &&
304 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
306 bookmark_count++;
307 write(temp_bookmark_file, global_read_buffer,
308 strlen(global_read_buffer));
309 write(temp_bookmark_file, "\n", 1);
312 close(bookmark_file);
314 close(temp_bookmark_file);
316 remove(bookmark_file_name);
317 rename(global_temp_buffer, bookmark_file_name);
319 return true;
323 /* ----------------------------------------------------------------------- */
324 /* This function takes the system resume data and formats it into a valid */
325 /* bookmark. */
326 /* ----------------------------------------------------------------------- */
327 static char* create_bookmark()
329 int resume_index = 0;
330 char *file;
332 /* grab the currently playing track */
333 struct mp3entry *id3 = audio_current_track();
334 if(!id3)
335 return NULL;
337 /* Get some basic resume information */
338 /* queue_resume and queue_resume_index are not used and can be ignored.*/
339 playlist_get_resume_info(&resume_index);
341 /* Get the currently playing file minus the path */
342 /* This is used when displaying the available bookmarks */
343 file = strrchr(id3->path,'/');
344 if(NULL == file)
345 return NULL;
347 /* create the bookmark */
348 snprintf(global_bookmark, sizeof(global_bookmark),
349 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
350 resume_index,
351 id3->offset,
352 playlist_get_seed(NULL),
354 id3->elapsed,
355 global_settings.repeat_mode,
356 global_settings.playlist_shuffle,
357 playlist_get_name(NULL, global_temp_buffer,
358 sizeof(global_temp_buffer)),
359 file+1);
361 /* checking to see if the bookmark is valid */
362 if (check_bookmark(global_bookmark))
363 return global_bookmark;
364 else
365 return NULL;
368 static bool check_bookmark(const char* bookmark)
370 return parse_bookmark(bookmark,
371 NULL,NULL,NULL, NULL,
372 NULL,0,NULL,NULL,
373 NULL, NULL);
376 /* ----------------------------------------------------------------------- */
377 /* This function will determine if an autoload is necessary. This is an */
378 /* interface function. */
379 /* ------------------------------------------------------------------------*/
380 bool bookmark_autoload(const char* file)
382 int fd;
384 if(global_settings.autoloadbookmark == BOOKMARK_NO)
385 return false;
387 /*Checking to see if a bookmark file exists.*/
388 if(!generate_bookmark_file_name(file))
390 return false;
392 fd = open(global_bookmark_file_name, O_RDONLY);
393 if(fd<0)
394 return false;
395 close(fd);
396 if(global_settings.autoloadbookmark == BOOKMARK_YES)
398 return bookmark_load(global_bookmark_file_name, true);
400 else
402 char* bookmark = select_bookmark(global_bookmark_file_name, true);
404 if (bookmark)
406 return bookmark_load(global_bookmark_file_name, true);
409 action_signalscreenchange();
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 /* Get the requested bookmark */
457 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
459 read_count++;
462 close(file);
463 return read_count;
466 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
468 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
469 int read_count = 0;
470 int file = open(bookmarks->filename, O_RDONLY);
472 if (file < 0)
474 return -1;
477 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
478 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
480 /* Entire file fits in buffer */
481 first_line = 0;
484 bookmarks->start = first_line;
485 bookmarks->count = 0;
486 bookmarks->reload = false;
488 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
490 read_count++;
492 if (read_count >= first_line)
494 dest -= strlen(global_read_buffer) + 1;
496 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
497 + (sizeof(char*) * (bookmarks->count + 1)))
499 break;
502 strcpy(dest, global_read_buffer);
503 bookmarks->items[bookmarks->count] = dest;
504 bookmarks->count++;
508 close(file);
509 return bookmarks->start + bookmarks->count;
512 static char* get_bookmark_info(int list_index, void* data, char *buffer)
514 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
515 int index = list_index / 2;
516 int resume_index = 0;
517 long resume_time = 0;
518 bool shuffle = false;
520 if (bookmarks->show_dont_resume)
522 if (index == 0)
524 return list_index % 2 == 0
525 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
528 index--;
531 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
532 || (index < bookmarks->start))
534 int read_index = index;
536 /* Using count as a guide on how far to move could possibly fail
537 * sometimes. Use byte count if that is a problem?
540 if (read_index != 0)
542 /* Move count * 3 / 4 items in the direction the user is moving,
543 * but don't go too close to the end.
545 int offset = bookmarks->count;
546 int max = bookmarks->total_count - (bookmarks->count / 2);
548 if (read_index < bookmarks->start)
550 offset *= 3;
553 read_index = index - offset / 4;
555 if (read_index > max)
557 read_index = max;
560 if (read_index < 0)
562 read_index = 0;
566 if (buffer_bookmarks(bookmarks, read_index) <= index)
568 return "";
572 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
573 &resume_index, NULL, NULL, NULL, NULL, 0, &resume_time, NULL,
574 &shuffle, global_filename))
576 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
579 if (list_index % 2 == 0)
581 char* dot = strrchr(global_filename, '.');
583 if (dot)
585 *dot = '\0';
588 return global_filename;
590 else
592 char time_buf[32];
594 format_time(time_buf, sizeof(time_buf), resume_time);
595 snprintf(buffer, MAX_PATH, "%s, %d%s", time_buf, resume_index + 1,
596 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
597 return buffer;
601 /* ----------------------------------------------------------------------- */
602 /* This displays a the bookmarks in a file and allows the user to */
603 /* select one to play. */
604 /* ------------------------------------------------------------------------*/
605 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
607 struct bookmark_list* bookmarks;
608 struct gui_synclist list;
609 int last_item = -2;
610 int item = 0;
611 int action;
612 size_t size;
613 bool exit = false;
614 bool refresh = true;
616 bookmarks = plugin_get_buffer(&size);
617 bookmarks->buffer_size = size;
618 bookmarks->show_dont_resume = show_dont_resume;
619 bookmarks->filename = bookmark_file_name;
620 bookmarks->start = 0;
621 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2);
622 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
623 Icon_Bookmark);
624 gui_syncstatusbar_draw(&statusbars, true);
625 action_signalscreenchange();
627 while (!exit)
629 gui_syncstatusbar_draw(&statusbars, false);
631 if (refresh)
633 int count = get_bookmark_count(bookmark_file_name);
634 bookmarks->total_count = count;
636 if (bookmarks->total_count < 1)
638 /* No more bookmarks, delete file and exit */
639 gui_syncsplash(HZ, str(LANG_BOOKMARK_LOAD_EMPTY));
640 remove(bookmark_file_name);
641 action_signalscreenchange();
642 return NULL;
645 if (bookmarks->show_dont_resume)
647 count++;
648 item++;
651 gui_synclist_set_nb_items(&list, count * 2);
653 if (item >= count)
655 /* Selected item has been deleted */
656 item = count - 1;
657 gui_synclist_select_item(&list, item * 2);
660 buffer_bookmarks(bookmarks, bookmarks->start);
661 gui_synclist_draw(&list);
662 refresh = false;
665 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
666 gui_synclist_do_button(&list, action, LIST_WRAP_UNLESS_HELD);
667 item = gui_synclist_get_sel_pos(&list) / 2;
669 if (bookmarks->show_dont_resume)
671 item--;
674 if (item != last_item && talk_menus_enabled())
676 last_item = item;
678 if (item == -1)
680 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
682 else
684 say_bookmark(bookmarks->items[item - bookmarks->start], item);
688 if (action == ACTION_STD_CONTEXT)
690 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
691 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
692 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
693 static const int menu_actions[] =
695 ACTION_STD_OK, ACTION_BMS_DELETE
697 int selection = do_menu(&menu_items, NULL);
699 refresh = true;
701 if (selection >= 0 && selection <=
702 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
704 action = menu_actions[selection];
708 switch (action)
710 case ACTION_STD_OK:
711 if (item >= 0)
713 action_signalscreenchange();
714 return bookmarks->items[item - bookmarks->start];
717 /* Else fall through */
719 case ACTION_TREE_WPS:
720 case ACTION_STD_CANCEL:
721 exit = true;
722 break;
724 case ACTION_BMS_DELETE:
725 if (item >= 0)
727 delete_bookmark(bookmark_file_name, item);
728 bookmarks->reload = true;
729 refresh = true;
730 last_item = -2;
732 break;
734 default:
735 if (default_event_handler(action) == SYS_USB_CONNECTED)
737 exit = true;
740 break;
744 action_signalscreenchange();
745 return NULL;
748 /* ----------------------------------------------------------------------- */
749 /* This function takes a location in a bookmark file and deletes that */
750 /* bookmark. */
751 /* ------------------------------------------------------------------------*/
752 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
754 int temp_bookmark_file = 0;
755 int bookmark_file = 0;
756 int bookmark_count = 0;
758 /* Opening up a temp bookmark file */
759 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
760 "%s.tmp", bookmark_file_name);
761 temp_bookmark_file = open(global_temp_buffer,
762 O_WRONLY | O_CREAT | O_TRUNC);
764 if (temp_bookmark_file < 0)
765 return false; /* can't open the temp file */
767 /* Reading in the previous bookmarks and writing them to the temp file */
768 bookmark_file = open(bookmark_file_name, O_RDONLY);
769 if (bookmark_file >= 0)
771 while (read_line(bookmark_file, global_read_buffer,
772 sizeof(global_read_buffer)) > 0)
774 if (bookmark_id != bookmark_count)
776 write(temp_bookmark_file, global_read_buffer,
777 strlen(global_read_buffer));
778 write(temp_bookmark_file, "\n", 1);
780 bookmark_count++;
782 close(bookmark_file);
784 close(temp_bookmark_file);
786 remove(bookmark_file_name);
787 rename(global_temp_buffer, bookmark_file_name);
789 return true;
792 /* ----------------------------------------------------------------------- */
793 /* This function parses a bookmark, says the voice UI part of it. */
794 /* ------------------------------------------------------------------------*/
795 static void say_bookmark(const char* bookmark,
796 int bookmark_id)
798 int resume_index;
799 long ms;
800 char dir[MAX_PATH];
801 bool enqueue = false; /* only the first voice is not queued */
803 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
804 dir, sizeof(dir), &ms, NULL, NULL, NULL))
806 talk_id(LANG_BOOKMARK_INVALID, true);
807 return;
810 /* disabled, because transition between talkbox and voice UI clip is not nice */
811 #if 0
812 if (global_settings.talk_dir >= 3)
813 { /* "talkbox" enabled */
814 char* last = strrchr(dir, '/');
815 if (last)
816 { /* compose filename for talkbox */
817 strncpy(last + 1, dir_thumbnail_name, sizeof(dir)-(last-dir)-1);
818 talk_file(dir, enqueue);
819 enqueue = true;
822 #endif
823 talk_id(VOICE_EXT_BMARK, enqueue);
824 talk_number(bookmark_id + 1, true);
825 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
826 talk_number(resume_index + 1, true);
827 talk_id(VOICE_BOOKMARK_SELECT_TIME_TEXT, true);
828 if (ms / 60000)
829 talk_value(ms / 60000, UNIT_MIN, true);
830 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
833 /* ----------------------------------------------------------------------- */
834 /* This function parses a bookmark and then plays it. */
835 /* ------------------------------------------------------------------------*/
836 static bool play_bookmark(const char* bookmark)
838 int index;
839 int offset;
840 int seed;
842 if (parse_bookmark(bookmark,
843 &index,
844 &offset,
845 &seed,
846 NULL,
847 global_temp_buffer,
848 sizeof(global_temp_buffer),
849 NULL,
850 &global_settings.repeat_mode,
851 &global_settings.playlist_shuffle,
852 global_filename))
854 bookmark_play(global_temp_buffer, index, offset, seed,
855 global_filename);
856 return true;
859 return false;
862 static const char* skip_token(const char* s)
864 while (*s && *s != ';')
866 s++;
869 if (*s)
871 s++;
874 return s;
877 static const char* int_token(const char* s, int* dest)
879 if (dest != NULL)
881 *dest = atoi(s);
884 return skip_token(s);
887 static const char* long_token(const char* s, long* dest)
889 if (dest != NULL)
891 *dest = atoi(s); /* Should be atol, but we don't have it. */
894 return skip_token(s);
897 static const char* bool_token(const char* s, bool* dest)
899 if (dest != NULL)
901 *dest = atoi(s) != 0;
904 return skip_token(s);
907 /* ----------------------------------------------------------------------- */
908 /* This function takes a bookmark and parses it. This function also */
909 /* validates the bookmark. Passing in NULL for an output variable */
910 /* indicates that value is not requested. */
911 /* ----------------------------------------------------------------------- */
912 static bool parse_bookmark(const char *bookmark,
913 int *resume_index,
914 int *resume_offset,
915 int *resume_seed,
916 int *resume_first_index,
917 char* resume_file,
918 unsigned int resume_file_size,
919 long* ms,
920 int * repeat_mode, bool *shuffle,
921 char* file_name)
923 const char* s = bookmark;
924 const char* end;
926 s = int_token(s, resume_index);
927 s = int_token(s, resume_offset);
928 s = int_token(s, resume_seed);
929 s = int_token(s, resume_first_index);
930 s = long_token(s, ms);
931 s = int_token(s, repeat_mode);
932 s = bool_token(s, shuffle);
934 if (*s == 0)
936 return false;
939 end = strchr(s, ';');
941 if (resume_file != NULL)
943 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
945 len = MIN(resume_file_size - 1, len);
946 strncpy(resume_file, s, len);
947 resume_file[len] = 0;
950 if (end != NULL && file_name != NULL)
952 end++;
953 strncpy(file_name, end, MAX_PATH - 1);
954 file_name[MAX_PATH - 1] = 0;
957 return true;
960 /* ----------------------------------------------------------------------- */
961 /* This function is used by multiple functions and is used to generate a */
962 /* bookmark named based off of the input. */
963 /* Changing this function could result in how the bookmarks are stored. */
964 /* it would be here that the centralized/decentralized bookmark code */
965 /* could be placed. */
966 /* ----------------------------------------------------------------------- */
967 static bool generate_bookmark_file_name(const char *in)
969 int len = strlen(in);
971 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
972 /* otherwise, name it based on the in variable */
973 if (!strcmp("/", in))
974 strcpy(global_bookmark_file_name, "/root_dir.bmark");
975 else
977 strcpy(global_bookmark_file_name, in);
978 if(global_bookmark_file_name[len-1] == '/')
979 len--;
980 strcpy(&global_bookmark_file_name[len], ".bmark");
983 return true;
986 /* ----------------------------------------------------------------------- */
987 /* Returns true if a bookmark file exists for the current playlist */
988 /* ----------------------------------------------------------------------- */
989 bool bookmark_exist(void)
991 bool exist=false;
993 if(system_check())
995 char* name = playlist_get_name(NULL, global_temp_buffer,
996 sizeof(global_temp_buffer));
997 if (generate_bookmark_file_name(name))
999 int fd=open(global_bookmark_file_name, O_RDONLY);
1000 if (fd >=0)
1002 close(fd);
1003 exist=true;
1008 return exist;
1011 /* ----------------------------------------------------------------------- */
1012 /* Checks the current state of the system and returns if it is in a */
1013 /* bookmarkable state. */
1014 /* ----------------------------------------------------------------------- */
1015 /* Inputs: */
1016 /* ----------------------------------------------------------------------- */
1017 /* Outputs: */
1018 /* return bool: Indicates if the system was in a bookmarkable state */
1019 /* ----------------------------------------------------------------------- */
1020 static bool system_check(void)
1022 int resume_index = 0;
1023 struct mp3entry *id3 = audio_current_track();
1025 if (!id3)
1027 /* no track playing */
1028 return false;
1031 /* Checking to see if playing a queued track */
1032 if (playlist_get_resume_info(&resume_index) == -1)
1034 /* something bad happened while getting the queue information */
1035 return false;
1037 else if (playlist_modified(NULL))
1039 /* can't bookmark while in the queue */
1040 return false;
1043 return true;