Moved atoi declaration to stdlib.h. Deleted atoi.h
[kugel-rb.git] / apps / bookmark.c
blobb371431259072f4cba7bbab650182e1ecd7b95aa
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 "string.h"
39 #include "menu.h"
40 #include "lang.h"
41 #include "screens.h"
42 #include "status.h"
43 #include "debug.h"
44 #include "kernel.h"
45 #include "sprintf.h"
46 #include "talk.h"
47 #include "misc.h"
48 #include "abrepeat.h"
49 #include "splash.h"
50 #include "yesno.h"
51 #include "list.h"
52 #include "plugin.h"
53 #include "backdrop.h"
55 #define MAX_BOOKMARKS 10
56 #define MAX_BOOKMARK_SIZE 350
57 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
59 /* Used to buffer bookmarks while displaying the bookmark list. */
60 struct bookmark_list
62 const char* filename;
63 size_t buffer_size;
64 int start;
65 int count;
66 int total_count;
67 bool show_dont_resume;
68 bool reload;
69 bool show_playlist_name;
70 char* items[];
73 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
74 bool most_recent);
75 static bool check_bookmark(const char* bookmark);
76 static char* create_bookmark(void);
77 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
78 static void say_bookmark(const char* bookmark,
79 int bookmark_id);
80 static bool play_bookmark(const char* bookmark);
81 static bool generate_bookmark_file_name(const char *in);
82 static const char* skip_token(const char* s);
83 static const char* int_token(const char* s, int* dest);
84 static const char* long_token(const char* s, long* dest);
85 static const char* bool_token(const char* s, bool* dest);
86 static bool parse_bookmark(const char *bookmark,
87 int *resume_index,
88 int *resume_offset,
89 int *resume_seed,
90 int *resume_first_index,
91 char* resume_file,
92 unsigned int resume_file_size,
93 long* ms,
94 int * repeat_mode,
95 bool *shuffle,
96 char* file_name);
97 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
98 static char* get_bookmark_info(int list_index,
99 void* data,
100 char *buffer,
101 size_t buffer_len);
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 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
190 const struct text_message message={lines, 1};
191 #else
192 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
193 str(LANG_CONFIRM_WITH_BUTTON)};
194 const struct text_message message={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, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
248 : ID2P(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 if(global_settings.autoloadbookmark == BOOKMARK_NO)
383 return false;
385 /*Checking to see if a bookmark file exists.*/
386 if(!generate_bookmark_file_name(file))
388 return false;
391 if(!file_exists(global_bookmark_file_name))
392 return false;
394 if(global_settings.autoloadbookmark == BOOKMARK_YES)
396 return bookmark_load(global_bookmark_file_name, true);
398 else
400 char* bookmark = select_bookmark(global_bookmark_file_name, true);
402 if (bookmark != NULL)
404 if (!play_bookmark(bookmark))
406 /* Selected bookmark not found. */
407 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
410 /* Act as if autoload was done even if it failed, since the
411 * user did make an active selection.
413 return true;
416 return false;
420 /* ----------------------------------------------------------------------- */
421 /* This function loads the bookmark information into the resume memory. */
422 /* This is an interface function. */
423 /* ------------------------------------------------------------------------*/
424 bool bookmark_load(const char* file, bool autoload)
426 int fd;
427 char* bookmark = NULL;
429 if(autoload)
431 fd = open(file, O_RDONLY);
432 if(fd >= 0)
434 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
435 bookmark=global_read_buffer;
436 close(fd);
439 else
441 /* This is not an auto-load, so list the bookmarks */
442 bookmark = select_bookmark(file, false);
445 if (bookmark != NULL)
447 if (!play_bookmark(bookmark))
449 /* Selected bookmark not found. */
450 if (!autoload)
452 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
455 return false;
459 return true;
463 static int get_bookmark_count(const char* bookmark_file_name)
465 int read_count = 0;
466 int file = open(bookmark_file_name, O_RDONLY);
468 if(file < 0)
469 return -1;
471 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
473 read_count++;
476 close(file);
477 return read_count;
480 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
482 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
483 int read_count = 0;
484 int file = open(bookmarks->filename, O_RDONLY);
486 if (file < 0)
488 return -1;
491 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
492 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
494 /* Entire file fits in buffer */
495 first_line = 0;
498 bookmarks->start = first_line;
499 bookmarks->count = 0;
500 bookmarks->reload = false;
502 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
504 read_count++;
506 if (read_count >= first_line)
508 dest -= strlen(global_read_buffer) + 1;
510 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
511 + (sizeof(char*) * (bookmarks->count + 1)))
513 break;
516 strcpy(dest, global_read_buffer);
517 bookmarks->items[bookmarks->count] = dest;
518 bookmarks->count++;
522 close(file);
523 return bookmarks->start + bookmarks->count;
526 static char* get_bookmark_info(int list_index,
527 void* data,
528 char *buffer,
529 size_t buffer_len)
531 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
532 int index = list_index / 2;
533 int resume_index = 0;
534 long resume_time = 0;
535 bool shuffle = false;
537 if (bookmarks->show_dont_resume)
539 if (index == 0)
541 return list_index % 2 == 0
542 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
545 index--;
548 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
549 || (index < bookmarks->start))
551 int read_index = index;
553 /* Using count as a guide on how far to move could possibly fail
554 * sometimes. Use byte count if that is a problem?
557 if (read_index != 0)
559 /* Move count * 3 / 4 items in the direction the user is moving,
560 * but don't go too close to the end.
562 int offset = bookmarks->count;
563 int max = bookmarks->total_count - (bookmarks->count / 2);
565 if (read_index < bookmarks->start)
567 offset *= 3;
570 read_index = index - offset / 4;
572 if (read_index > max)
574 read_index = max;
577 if (read_index < 0)
579 read_index = 0;
583 if (buffer_bookmarks(bookmarks, read_index) <= index)
585 return "";
589 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
590 &resume_index, NULL, NULL, NULL, global_temp_buffer,
591 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
592 global_filename))
594 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
597 if (list_index % 2 == 0)
599 char *name;
600 char *format;
601 int len = strlen(global_temp_buffer);
603 if (bookmarks->show_playlist_name && len > 0)
605 name = global_temp_buffer;
606 len--;
608 if (name[len] != '/')
610 strrsplt(name, '.');
612 else if (len > 1)
614 name[len] = '\0';
617 if (len > 1)
619 name = strrsplt(name, '/');
622 format = "%s : %s";
624 else
626 name = global_filename;
627 format = "%s";
630 strrsplt(global_filename, '.');
631 snprintf(buffer, buffer_len, format, name, global_filename);
632 return buffer;
634 else
636 char time_buf[32];
638 format_time(time_buf, sizeof(time_buf), resume_time);
639 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
640 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
641 return buffer;
645 /* ----------------------------------------------------------------------- */
646 /* This displays a the bookmarks in a file and allows the user to */
647 /* select one to play. */
648 /* ------------------------------------------------------------------------*/
649 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
651 struct bookmark_list* bookmarks;
652 struct gui_synclist list;
653 int last_item = -2;
654 int item = 0;
655 int action;
656 size_t size;
657 bool exit = false;
658 bool refresh = true;
660 bookmarks = plugin_get_buffer(&size);
661 bookmarks->buffer_size = size;
662 bookmarks->show_dont_resume = show_dont_resume;
663 bookmarks->filename = bookmark_file_name;
664 bookmarks->start = 0;
665 bookmarks->show_playlist_name
666 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
667 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
668 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
669 Icon_Bookmark);
670 gui_syncstatusbar_draw(&statusbars, true);
672 while (!exit)
674 gui_syncstatusbar_draw(&statusbars, false);
676 if (refresh)
678 int count = get_bookmark_count(bookmark_file_name);
679 bookmarks->total_count = count;
681 if (bookmarks->total_count < 1)
683 /* No more bookmarks, delete file and exit */
684 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
685 remove(bookmark_file_name);
686 return NULL;
689 if (bookmarks->show_dont_resume)
691 count++;
692 item++;
695 gui_synclist_set_nb_items(&list, count * 2);
697 if (item >= count)
699 /* Selected item has been deleted */
700 item = count - 1;
701 gui_synclist_select_item(&list, item * 2);
704 buffer_bookmarks(bookmarks, bookmarks->start);
705 gui_synclist_draw(&list);
706 refresh = false;
709 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
710 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
711 item = gui_synclist_get_sel_pos(&list) / 2;
713 if (bookmarks->show_dont_resume)
715 item--;
718 if (item != last_item && global_settings.talk_menu)
720 last_item = item;
722 if (item == -1)
724 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
726 else
728 say_bookmark(bookmarks->items[item - bookmarks->start], item);
732 if (action == ACTION_STD_CONTEXT)
734 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
735 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
736 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
737 static const int menu_actions[] =
739 ACTION_STD_OK, ACTION_BMS_DELETE
741 int selection = do_menu(&menu_items, NULL, NULL, false);
743 refresh = true;
745 if (selection >= 0 && selection <=
746 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
748 action = menu_actions[selection];
752 switch (action)
754 case ACTION_STD_OK:
755 if (item >= 0)
757 return bookmarks->items[item - bookmarks->start];
760 /* Else fall through */
762 case ACTION_TREE_WPS:
763 case ACTION_STD_CANCEL:
764 exit = true;
765 break;
767 case ACTION_BMS_DELETE:
768 if (item >= 0)
770 delete_bookmark(bookmark_file_name, item);
771 bookmarks->reload = true;
772 refresh = true;
773 last_item = -2;
775 break;
777 default:
778 if (default_event_handler(action) == SYS_USB_CONNECTED)
780 exit = true;
783 break;
787 return NULL;
790 /* ----------------------------------------------------------------------- */
791 /* This function takes a location in a bookmark file and deletes that */
792 /* bookmark. */
793 /* ------------------------------------------------------------------------*/
794 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
796 int temp_bookmark_file = 0;
797 int bookmark_file = 0;
798 int bookmark_count = 0;
800 /* Opening up a temp bookmark file */
801 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
802 "%s.tmp", bookmark_file_name);
803 temp_bookmark_file = open(global_temp_buffer,
804 O_WRONLY | O_CREAT | O_TRUNC);
806 if (temp_bookmark_file < 0)
807 return false; /* can't open the temp file */
809 /* Reading in the previous bookmarks and writing them to the temp file */
810 bookmark_file = open(bookmark_file_name, O_RDONLY);
811 if (bookmark_file >= 0)
813 while (read_line(bookmark_file, global_read_buffer,
814 sizeof(global_read_buffer)) > 0)
816 if (bookmark_id != bookmark_count)
818 write(temp_bookmark_file, global_read_buffer,
819 strlen(global_read_buffer));
820 write(temp_bookmark_file, "\n", 1);
822 bookmark_count++;
824 close(bookmark_file);
826 close(temp_bookmark_file);
828 remove(bookmark_file_name);
829 rename(global_temp_buffer, bookmark_file_name);
831 return true;
834 /* ----------------------------------------------------------------------- */
835 /* This function parses a bookmark, says the voice UI part of it. */
836 /* ------------------------------------------------------------------------*/
837 static void say_bookmark(const char* bookmark,
838 int bookmark_id)
840 int resume_index;
841 long ms;
842 bool enqueue = false; /* only the first voice is not queued */
844 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
845 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
847 talk_id(LANG_BOOKMARK_INVALID, true);
848 return;
851 /* disabled, because transition between talkbox and voice UI clip is not nice */
852 #if 0
853 if (global_settings.talk_dir >= 3)
854 { /* "talkbox" enabled */
855 char* last = strrchr(global_temp_buffer, '/');
856 if (last)
857 { /* compose filename for talkbox */
858 strncpy(last + 1, dir_thumbnail_name,
859 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
860 talk_file(global_temp_buffer, enqueue);
861 enqueue = true;
864 #endif
865 talk_id(VOICE_EXT_BMARK, enqueue);
866 talk_number(bookmark_id + 1, true);
867 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
868 talk_number(resume_index + 1, true);
869 talk_id(LANG_TIME, true);
870 if (ms / 60000)
871 talk_value(ms / 60000, UNIT_MIN, true);
872 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
875 /* ----------------------------------------------------------------------- */
876 /* This function parses a bookmark and then plays it. */
877 /* ------------------------------------------------------------------------*/
878 static bool play_bookmark(const char* bookmark)
880 int index;
881 int offset;
882 int seed;
884 if (parse_bookmark(bookmark,
885 &index,
886 &offset,
887 &seed,
888 NULL,
889 global_temp_buffer,
890 sizeof(global_temp_buffer),
891 NULL,
892 &global_settings.repeat_mode,
893 &global_settings.playlist_shuffle,
894 global_filename))
896 return bookmark_play(global_temp_buffer, index, offset, seed,
897 global_filename);
900 return false;
903 static const char* skip_token(const char* s)
905 while (*s && *s != ';')
907 s++;
910 if (*s)
912 s++;
915 return s;
918 static const char* int_token(const char* s, int* dest)
920 if (dest != NULL)
922 *dest = atoi(s);
925 return skip_token(s);
928 static const char* long_token(const char* s, long* dest)
930 if (dest != NULL)
932 *dest = atoi(s); /* Should be atol, but we don't have it. */
935 return skip_token(s);
938 static const char* bool_token(const char* s, bool* dest)
940 if (dest != NULL)
942 *dest = atoi(s) != 0;
945 return skip_token(s);
948 /* ----------------------------------------------------------------------- */
949 /* This function takes a bookmark and parses it. This function also */
950 /* validates the bookmark. Passing in NULL for an output variable */
951 /* indicates that value is not requested. */
952 /* ----------------------------------------------------------------------- */
953 static bool parse_bookmark(const char *bookmark,
954 int *resume_index,
955 int *resume_offset,
956 int *resume_seed,
957 int *resume_first_index,
958 char* resume_file,
959 unsigned int resume_file_size,
960 long* ms,
961 int * repeat_mode, bool *shuffle,
962 char* file_name)
964 const char* s = bookmark;
965 const char* end;
967 s = int_token(s, resume_index);
968 s = int_token(s, resume_offset);
969 s = int_token(s, resume_seed);
970 s = int_token(s, resume_first_index);
971 s = long_token(s, ms);
972 s = int_token(s, repeat_mode);
973 s = bool_token(s, shuffle);
975 if (*s == 0)
977 return false;
980 end = strchr(s, ';');
982 if (resume_file != NULL)
984 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
986 len = MIN(resume_file_size - 1, len);
987 strncpy(resume_file, s, len);
988 resume_file[len] = 0;
991 if (end != NULL && file_name != NULL)
993 end++;
994 strncpy(file_name, end, MAX_PATH - 1);
995 file_name[MAX_PATH - 1] = 0;
998 return true;
1001 /* ----------------------------------------------------------------------- */
1002 /* This function is used by multiple functions and is used to generate a */
1003 /* bookmark named based off of the input. */
1004 /* Changing this function could result in how the bookmarks are stored. */
1005 /* it would be here that the centralized/decentralized bookmark code */
1006 /* could be placed. */
1007 /* ----------------------------------------------------------------------- */
1008 static bool generate_bookmark_file_name(const char *in)
1010 int len = strlen(in);
1012 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1013 /* otherwise, name it based on the in variable */
1014 if (!strcmp("/", in))
1015 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1016 else
1018 strcpy(global_bookmark_file_name, in);
1019 if(global_bookmark_file_name[len-1] == '/')
1020 len--;
1021 strcpy(&global_bookmark_file_name[len], ".bmark");
1024 return true;
1027 /* ----------------------------------------------------------------------- */
1028 /* Returns true if a bookmark file exists for the current playlist */
1029 /* ----------------------------------------------------------------------- */
1030 bool bookmark_exist(void)
1032 bool exist=false;
1034 if(system_check())
1036 char* name = playlist_get_name(NULL, global_temp_buffer,
1037 sizeof(global_temp_buffer));
1038 if (generate_bookmark_file_name(name))
1040 exist = file_exists(global_bookmark_file_name);
1044 return exist;
1047 /* ----------------------------------------------------------------------- */
1048 /* Checks the current state of the system and returns if it is in a */
1049 /* bookmarkable state. */
1050 /* ----------------------------------------------------------------------- */
1051 /* Inputs: */
1052 /* ----------------------------------------------------------------------- */
1053 /* Outputs: */
1054 /* return bool: Indicates if the system was in a bookmarkable state */
1055 /* ----------------------------------------------------------------------- */
1056 static bool system_check(void)
1058 int resume_index = 0;
1060 if (!(audio_status() && audio_current_track()))
1062 /* no track playing */
1063 return false;
1066 /* Checking to see if playing a queued track */
1067 if (playlist_get_resume_info(&resume_index) == -1)
1069 /* something bad happened while getting the queue information */
1070 return false;
1072 else if (playlist_modified(NULL))
1074 /* can't bookmark while in the queue */
1075 return false;
1078 return true;