Kill warnings that annoy me when building for Gigabeat S.
[kugel-rb.git] / apps / bookmark.c
blob363660306c7f8d21565c77571eea6247614d167f
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,
100 void* data,
101 char *buffer,
102 size_t buffer_len);
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 if (!play_bookmark(bookmark))
409 /* Selected bookmark not found. */
410 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
413 /* Act as if autoload was done even if it failed, since the
414 * user did make an active selection.
416 return true;
419 return false;
423 /* ----------------------------------------------------------------------- */
424 /* This function loads the bookmark information into the resume memory. */
425 /* This is an interface function. */
426 /* ------------------------------------------------------------------------*/
427 bool bookmark_load(const char* file, bool autoload)
429 int fd;
430 char* bookmark = NULL;
432 if(autoload)
434 fd = open(file, O_RDONLY);
435 if(fd >= 0)
437 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
438 bookmark=global_read_buffer;
439 close(fd);
442 else
444 /* This is not an auto-load, so list the bookmarks */
445 bookmark = select_bookmark(file, false);
448 if (bookmark != NULL)
450 if (!play_bookmark(bookmark))
452 /* Selected bookmark not found. */
453 if (!autoload)
455 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
458 return false;
462 return true;
466 static int get_bookmark_count(const char* bookmark_file_name)
468 int read_count = 0;
469 int file = open(bookmark_file_name, O_RDONLY);
471 if(file < 0)
472 return -1;
474 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
476 read_count++;
479 close(file);
480 return read_count;
483 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
485 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
486 int read_count = 0;
487 int file = open(bookmarks->filename, O_RDONLY);
489 if (file < 0)
491 return -1;
494 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
495 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
497 /* Entire file fits in buffer */
498 first_line = 0;
501 bookmarks->start = first_line;
502 bookmarks->count = 0;
503 bookmarks->reload = false;
505 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
507 read_count++;
509 if (read_count >= first_line)
511 dest -= strlen(global_read_buffer) + 1;
513 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
514 + (sizeof(char*) * (bookmarks->count + 1)))
516 break;
519 strcpy(dest, global_read_buffer);
520 bookmarks->items[bookmarks->count] = dest;
521 bookmarks->count++;
525 close(file);
526 return bookmarks->start + bookmarks->count;
529 static char* get_bookmark_info(int list_index,
530 void* data,
531 char *buffer,
532 size_t buffer_len)
534 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
535 int index = list_index / 2;
536 int resume_index = 0;
537 long resume_time = 0;
538 bool shuffle = false;
540 if (bookmarks->show_dont_resume)
542 if (index == 0)
544 return list_index % 2 == 0
545 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
548 index--;
551 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
552 || (index < bookmarks->start))
554 int read_index = index;
556 /* Using count as a guide on how far to move could possibly fail
557 * sometimes. Use byte count if that is a problem?
560 if (read_index != 0)
562 /* Move count * 3 / 4 items in the direction the user is moving,
563 * but don't go too close to the end.
565 int offset = bookmarks->count;
566 int max = bookmarks->total_count - (bookmarks->count / 2);
568 if (read_index < bookmarks->start)
570 offset *= 3;
573 read_index = index - offset / 4;
575 if (read_index > max)
577 read_index = max;
580 if (read_index < 0)
582 read_index = 0;
586 if (buffer_bookmarks(bookmarks, read_index) <= index)
588 return "";
592 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
593 &resume_index, NULL, NULL, NULL, global_temp_buffer,
594 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
595 global_filename))
597 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
600 if (list_index % 2 == 0)
602 char *name;
603 char *format;
604 int len = strlen(global_temp_buffer);
606 if (bookmarks->show_playlist_name && len > 0)
608 name = global_temp_buffer;
609 len--;
611 if (name[len] != '/')
613 strrsplt(name, '.');
615 else if (len > 1)
617 name[len] = '\0';
620 if (len > 1)
622 name = strrsplt(name, '/');
625 format = "%s : %s";
627 else
629 name = global_filename;
630 format = "%s";
633 strrsplt(global_filename, '.');
634 snprintf(buffer, buffer_len, format, name, global_filename);
635 return buffer;
637 else
639 char time_buf[32];
641 format_time(time_buf, sizeof(time_buf), resume_time);
642 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
643 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
644 return buffer;
648 /* ----------------------------------------------------------------------- */
649 /* This displays a the bookmarks in a file and allows the user to */
650 /* select one to play. */
651 /* ------------------------------------------------------------------------*/
652 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
654 struct bookmark_list* bookmarks;
655 struct gui_synclist list;
656 int last_item = -2;
657 int item = 0;
658 int action;
659 size_t size;
660 bool exit = false;
661 bool refresh = true;
663 bookmarks = plugin_get_buffer(&size);
664 bookmarks->buffer_size = size;
665 bookmarks->show_dont_resume = show_dont_resume;
666 bookmarks->filename = bookmark_file_name;
667 bookmarks->start = 0;
668 bookmarks->show_playlist_name
669 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
670 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
671 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
672 Icon_Bookmark);
673 gui_syncstatusbar_draw(&statusbars, true);
675 while (!exit)
677 gui_syncstatusbar_draw(&statusbars, false);
679 if (refresh)
681 int count = get_bookmark_count(bookmark_file_name);
682 bookmarks->total_count = count;
684 if (bookmarks->total_count < 1)
686 /* No more bookmarks, delete file and exit */
687 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
688 remove(bookmark_file_name);
689 return NULL;
692 if (bookmarks->show_dont_resume)
694 count++;
695 item++;
698 gui_synclist_set_nb_items(&list, count * 2);
700 if (item >= count)
702 /* Selected item has been deleted */
703 item = count - 1;
704 gui_synclist_select_item(&list, item * 2);
707 buffer_bookmarks(bookmarks, bookmarks->start);
708 gui_synclist_draw(&list);
709 refresh = false;
712 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
713 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
714 item = gui_synclist_get_sel_pos(&list) / 2;
716 if (bookmarks->show_dont_resume)
718 item--;
721 if (item != last_item && global_settings.talk_menu)
723 last_item = item;
725 if (item == -1)
727 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
729 else
731 say_bookmark(bookmarks->items[item - bookmarks->start], item);
735 if (action == ACTION_STD_CONTEXT)
737 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
738 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
739 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
740 static const int menu_actions[] =
742 ACTION_STD_OK, ACTION_BMS_DELETE
744 int selection = do_menu(&menu_items, NULL, NULL, false);
746 refresh = true;
748 if (selection >= 0 && selection <=
749 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
751 action = menu_actions[selection];
755 switch (action)
757 case ACTION_STD_OK:
758 if (item >= 0)
760 return bookmarks->items[item - bookmarks->start];
763 /* Else fall through */
765 case ACTION_TREE_WPS:
766 case ACTION_STD_CANCEL:
767 exit = true;
768 break;
770 case ACTION_BMS_DELETE:
771 if (item >= 0)
773 delete_bookmark(bookmark_file_name, item);
774 bookmarks->reload = true;
775 refresh = true;
776 last_item = -2;
778 break;
780 default:
781 if (default_event_handler(action) == SYS_USB_CONNECTED)
783 exit = true;
786 break;
790 return NULL;
793 /* ----------------------------------------------------------------------- */
794 /* This function takes a location in a bookmark file and deletes that */
795 /* bookmark. */
796 /* ------------------------------------------------------------------------*/
797 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
799 int temp_bookmark_file = 0;
800 int bookmark_file = 0;
801 int bookmark_count = 0;
803 /* Opening up a temp bookmark file */
804 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
805 "%s.tmp", bookmark_file_name);
806 temp_bookmark_file = open(global_temp_buffer,
807 O_WRONLY | O_CREAT | O_TRUNC);
809 if (temp_bookmark_file < 0)
810 return false; /* can't open the temp file */
812 /* Reading in the previous bookmarks and writing them to the temp file */
813 bookmark_file = open(bookmark_file_name, O_RDONLY);
814 if (bookmark_file >= 0)
816 while (read_line(bookmark_file, global_read_buffer,
817 sizeof(global_read_buffer)) > 0)
819 if (bookmark_id != bookmark_count)
821 write(temp_bookmark_file, global_read_buffer,
822 strlen(global_read_buffer));
823 write(temp_bookmark_file, "\n", 1);
825 bookmark_count++;
827 close(bookmark_file);
829 close(temp_bookmark_file);
831 remove(bookmark_file_name);
832 rename(global_temp_buffer, bookmark_file_name);
834 return true;
837 /* ----------------------------------------------------------------------- */
838 /* This function parses a bookmark, says the voice UI part of it. */
839 /* ------------------------------------------------------------------------*/
840 static void say_bookmark(const char* bookmark,
841 int bookmark_id)
843 int resume_index;
844 long ms;
845 bool enqueue = false; /* only the first voice is not queued */
847 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
848 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
850 talk_id(LANG_BOOKMARK_INVALID, true);
851 return;
854 /* disabled, because transition between talkbox and voice UI clip is not nice */
855 #if 0
856 if (global_settings.talk_dir >= 3)
857 { /* "talkbox" enabled */
858 char* last = strrchr(global_temp_buffer, '/');
859 if (last)
860 { /* compose filename for talkbox */
861 strncpy(last + 1, dir_thumbnail_name,
862 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
863 talk_file(global_temp_buffer, enqueue);
864 enqueue = true;
867 #endif
868 talk_id(VOICE_EXT_BMARK, enqueue);
869 talk_number(bookmark_id + 1, true);
870 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
871 talk_number(resume_index + 1, true);
872 talk_id(LANG_TIME, true);
873 if (ms / 60000)
874 talk_value(ms / 60000, UNIT_MIN, true);
875 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
878 /* ----------------------------------------------------------------------- */
879 /* This function parses a bookmark and then plays it. */
880 /* ------------------------------------------------------------------------*/
881 static bool play_bookmark(const char* bookmark)
883 int index;
884 int offset;
885 int seed;
887 if (parse_bookmark(bookmark,
888 &index,
889 &offset,
890 &seed,
891 NULL,
892 global_temp_buffer,
893 sizeof(global_temp_buffer),
894 NULL,
895 &global_settings.repeat_mode,
896 &global_settings.playlist_shuffle,
897 global_filename))
899 return bookmark_play(global_temp_buffer, index, offset, seed,
900 global_filename);
903 return false;
906 static const char* skip_token(const char* s)
908 while (*s && *s != ';')
910 s++;
913 if (*s)
915 s++;
918 return s;
921 static const char* int_token(const char* s, int* dest)
923 if (dest != NULL)
925 *dest = atoi(s);
928 return skip_token(s);
931 static const char* long_token(const char* s, long* dest)
933 if (dest != NULL)
935 *dest = atoi(s); /* Should be atol, but we don't have it. */
938 return skip_token(s);
941 static const char* bool_token(const char* s, bool* dest)
943 if (dest != NULL)
945 *dest = atoi(s) != 0;
948 return skip_token(s);
951 /* ----------------------------------------------------------------------- */
952 /* This function takes a bookmark and parses it. This function also */
953 /* validates the bookmark. Passing in NULL for an output variable */
954 /* indicates that value is not requested. */
955 /* ----------------------------------------------------------------------- */
956 static bool parse_bookmark(const char *bookmark,
957 int *resume_index,
958 int *resume_offset,
959 int *resume_seed,
960 int *resume_first_index,
961 char* resume_file,
962 unsigned int resume_file_size,
963 long* ms,
964 int * repeat_mode, bool *shuffle,
965 char* file_name)
967 const char* s = bookmark;
968 const char* end;
970 s = int_token(s, resume_index);
971 s = int_token(s, resume_offset);
972 s = int_token(s, resume_seed);
973 s = int_token(s, resume_first_index);
974 s = long_token(s, ms);
975 s = int_token(s, repeat_mode);
976 s = bool_token(s, shuffle);
978 if (*s == 0)
980 return false;
983 end = strchr(s, ';');
985 if (resume_file != NULL)
987 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
989 len = MIN(resume_file_size - 1, len);
990 strncpy(resume_file, s, len);
991 resume_file[len] = 0;
994 if (end != NULL && file_name != NULL)
996 end++;
997 strncpy(file_name, end, MAX_PATH - 1);
998 file_name[MAX_PATH - 1] = 0;
1001 return true;
1004 /* ----------------------------------------------------------------------- */
1005 /* This function is used by multiple functions and is used to generate a */
1006 /* bookmark named based off of the input. */
1007 /* Changing this function could result in how the bookmarks are stored. */
1008 /* it would be here that the centralized/decentralized bookmark code */
1009 /* could be placed. */
1010 /* ----------------------------------------------------------------------- */
1011 static bool generate_bookmark_file_name(const char *in)
1013 int len = strlen(in);
1015 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1016 /* otherwise, name it based on the in variable */
1017 if (!strcmp("/", in))
1018 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1019 else
1021 strcpy(global_bookmark_file_name, in);
1022 if(global_bookmark_file_name[len-1] == '/')
1023 len--;
1024 strcpy(&global_bookmark_file_name[len], ".bmark");
1027 return true;
1030 /* ----------------------------------------------------------------------- */
1031 /* Returns true if a bookmark file exists for the current playlist */
1032 /* ----------------------------------------------------------------------- */
1033 bool bookmark_exist(void)
1035 bool exist=false;
1037 if(system_check())
1039 char* name = playlist_get_name(NULL, global_temp_buffer,
1040 sizeof(global_temp_buffer));
1041 if (generate_bookmark_file_name(name))
1043 int fd=open(global_bookmark_file_name, O_RDONLY);
1044 if (fd >=0)
1046 close(fd);
1047 exist=true;
1052 return exist;
1055 /* ----------------------------------------------------------------------- */
1056 /* Checks the current state of the system and returns if it is in a */
1057 /* bookmarkable state. */
1058 /* ----------------------------------------------------------------------- */
1059 /* Inputs: */
1060 /* ----------------------------------------------------------------------- */
1061 /* Outputs: */
1062 /* return bool: Indicates if the system was in a bookmarkable state */
1063 /* ----------------------------------------------------------------------- */
1064 static bool system_check(void)
1066 int resume_index = 0;
1068 if (!(audio_status() && audio_current_track()))
1070 /* no track playing */
1071 return false;
1074 /* Checking to see if playing a queued track */
1075 if (playlist_get_resume_info(&resume_index) == -1)
1077 /* something bad happened while getting the queue information */
1078 return false;
1080 else if (playlist_modified(NULL))
1082 /* can't bookmark while in the queue */
1083 return false;
1086 return true;