1 is the correct value here. Not that it makes any difference... :)
[kugel-rb.git] / apps / bookmark.c
blob06a2ed616fcab6fca169617c1d8433cbc8293799
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 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdbool.h>
25 #include "applimits.h"
26 #include "lcd.h"
27 #include "action.h"
28 #include "usb.h"
29 #include "audio.h"
30 #include "playlist.h"
31 #include "settings.h"
32 #include "tree.h"
33 #include "bookmark.h"
34 #include "dir.h"
35 #include "status.h"
36 #include "system.h"
37 #include "errno.h"
38 #include "icons.h"
39 #include "atoi.h"
40 #include "string.h"
41 #include "menu.h"
42 #include "lang.h"
43 #include "screens.h"
44 #include "status.h"
45 #include "debug.h"
46 #include "kernel.h"
47 #include "sprintf.h"
48 #include "talk.h"
49 #include "misc.h"
50 #include "abrepeat.h"
51 #include "splash.h"
52 #include "yesno.h"
54 #ifdef HAVE_LCD_COLOR
55 #include "backdrop.h"
56 #endif
58 #define MAX_BOOKMARKS 10
59 #define MAX_BOOKMARK_SIZE 350
60 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
62 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark);
63 static bool check_bookmark(const char* bookmark);
64 static char* create_bookmark(void);
65 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
66 static void display_bookmark(const char* bookmark,
67 int bookmark_id,
68 int bookmark_count);
69 static void say_bookmark(const char* bookmark,
70 int bookmark_id);
71 static bool generate_bookmark_file_name(const char *in);
72 static char* get_bookmark(const char* bookmark_file, int bookmark_count);
73 static bool parse_bookmark(const char *bookmark,
74 int *resume_index,
75 int *resume_offset,
76 int *resume_seed,
77 int *resume_first_index,
78 char* resume_file,
79 unsigned int resume_file_size,
80 long* ms,
81 int * repeat_mode,
82 bool *shuffle,
83 char* file_name);
84 static char* select_bookmark(const char* bookmark_file_name);
85 static bool system_check(void);
86 static bool write_bookmark(bool create_bookmark_file);
87 static int get_bookmark_count(const char* bookmark_file_name);
89 static char global_temp_buffer[MAX_PATH+1];
90 static char global_bookmark_file_name[MAX_PATH];
91 static char global_read_buffer[MAX_BOOKMARK_SIZE];
92 static char global_bookmark[MAX_BOOKMARK_SIZE];
93 static char global_filename[MAX_PATH];
95 /* ----------------------------------------------------------------------- */
96 /* This is the interface function from the main menu. */
97 /* ----------------------------------------------------------------------- */
98 bool bookmark_create_menu(void)
100 write_bookmark(true);
101 return false;
104 /* ----------------------------------------------------------------------- */
105 /* This function acts as the load interface from the main menu */
106 /* This function determines the bookmark file name and then loads that file*/
107 /* for the user. The user can then select a bookmark to load. */
108 /* If no file/directory is currently playing, the menu item does not work. */
109 /* ----------------------------------------------------------------------- */
110 bool bookmark_load_menu(void)
112 bool success = true;
113 int offset;
114 int seed;
115 int index;
116 char* bookmark;
118 if(!system_check())
119 return false;
120 else
122 char* name = playlist_get_name(NULL, global_temp_buffer,
123 sizeof(global_temp_buffer));
124 if (generate_bookmark_file_name(name))
126 bookmark = select_bookmark(global_bookmark_file_name);
127 if (!bookmark)
128 return false; /* User exited without selecting a bookmark */
130 success = parse_bookmark(bookmark,
131 &index,
132 &offset,
133 &seed,
134 NULL,
135 global_temp_buffer,
136 sizeof(global_temp_buffer),
137 NULL,
138 &global_settings.repeat_mode,
139 &global_settings.playlist_shuffle,
140 global_filename);
142 else
144 /* something bad happened while creating bookmark name*/
145 success = false;
148 if (success)
149 bookmark_play(global_temp_buffer, index, offset, seed,
150 global_filename);
153 return success;
156 /* ----------------------------------------------------------------------- */
157 /* Gives the user a list of the Most Recent Bookmarks. This is an */
158 /* interface function */
159 /* ----------------------------------------------------------------------- */
160 bool bookmark_mrb_load()
162 bool success = true;
163 int offset;
164 int seed;
165 int index;
166 char* bookmark;
168 bookmark = select_bookmark(RECENT_BOOKMARK_FILE);
169 if (!bookmark)
170 return false; /* User exited without selecting a bookmark */
172 success = parse_bookmark(bookmark,
173 &index,
174 &offset,
175 &seed,
176 NULL,
177 global_temp_buffer,
178 sizeof(global_temp_buffer),
179 NULL,
180 &global_settings.repeat_mode,
181 &global_settings.playlist_shuffle,
182 global_filename);
184 if (success)
185 bookmark_play(global_temp_buffer, index, offset, seed,
186 global_filename);
188 return success;
192 /* ----------------------------------------------------------------------- */
193 /* This function handles an autobookmark creation. This is an interface */
194 /* function. */
195 /* ----------------------------------------------------------------------- */
196 bool bookmark_autobookmark(void)
198 if (!system_check())
199 return false;
201 audio_pause(); /* first pause playback */
202 switch (global_settings.autocreatebookmark)
204 case BOOKMARK_YES:
205 return write_bookmark(true);
207 case BOOKMARK_NO:
208 return false;
210 case BOOKMARK_RECENT_ONLY_YES:
211 return write_bookmark(false);
213 #ifdef HAVE_LCD_BITMAP
214 unsigned char *lines[]={str(LANG_AUTO_BOOKMARK_QUERY)};
215 struct text_message message={(char **)lines, 1};
216 #else
217 unsigned char *lines[]={str(LANG_AUTO_BOOKMARK_QUERY),
218 str(LANG_RESUME_CONFIRM_PLAYER)};
219 struct text_message message={(char **)lines, 2};
220 #endif
221 #ifdef HAVE_LCD_COLOR
222 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
223 #endif
224 gui_syncstatusbar_draw(&statusbars, false);
225 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
227 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
228 return write_bookmark(false);
229 else
230 return write_bookmark(true);
232 return false;
235 /* ----------------------------------------------------------------------- */
236 /* This function takes the current current resume information and writes */
237 /* that to the beginning of the bookmark file. */
238 /* This file will contain N number of bookmarks in the following format: */
239 /* resume_index*resume_offset*resume_seed*resume_first_index* */
240 /* resume_file*milliseconds*MP3 Title* */
241 /* ------------------------------------------------------------------------*/
242 static bool write_bookmark(bool create_bookmark_file)
244 bool success=false;
245 char* bookmark;
247 if (!system_check())
248 return false; /* something didn't happen correctly, do nothing */
250 bookmark = create_bookmark();
251 if (!bookmark)
252 return false; /* something didn't happen correctly, do nothing */
254 if (global_settings.usemrb)
255 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark);
258 /* writing the bookmark */
259 if (create_bookmark_file)
261 char* name = playlist_get_name(NULL, global_temp_buffer,
262 sizeof(global_temp_buffer));
263 if (generate_bookmark_file_name(name))
265 success = add_bookmark(global_bookmark_file_name, bookmark);
269 if (success)
270 gui_syncsplash(HZ, true, str(LANG_BOOKMARK_CREATE_SUCCESS));
271 else
272 gui_syncsplash(HZ, true, str(LANG_BOOKMARK_CREATE_FAILURE));
274 return true;
277 /* ----------------------------------------------------------------------- */
278 /* This function adds a bookmark to a file. */
279 /* ------------------------------------------------------------------------*/
280 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark)
282 int temp_bookmark_file = 0;
283 int bookmark_file = 0;
284 int bookmark_count = 0;
285 char* playlist = NULL;
286 char* cp;
287 char* tmp;
288 int len = 0;
289 bool unique = false;
291 /* Opening up a temp bookmark file */
292 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
293 "%s.tmp", bookmark_file_name);
294 temp_bookmark_file = open(global_temp_buffer,
295 O_WRONLY | O_CREAT | O_TRUNC);
296 if (temp_bookmark_file < 0)
297 return false; /* can't open the temp file */
299 if (!strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE) &&
300 (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
302 playlist = strchr(bookmark,'/');
303 cp = strrchr(bookmark,';');
304 len = cp - playlist;
305 unique = true;
308 /* Writing the new bookmark to the begining of the temp file */
309 write(temp_bookmark_file, bookmark, strlen(bookmark));
310 write(temp_bookmark_file, "\n", 1);
311 bookmark_count++;
313 /* Reading in the previous bookmarks and writing them to the temp file */
314 bookmark_file = open(bookmark_file_name, O_RDONLY);
315 if (bookmark_file >= 0)
317 while (read_line(bookmark_file, global_read_buffer,
318 sizeof(global_read_buffer)))
320 /* The MRB has a max of MAX_BOOKMARKS in it */
321 /* This keeps it from getting too large */
322 if ((strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE)==0))
324 if(bookmark_count >= MAX_BOOKMARKS)
325 break;
328 cp = strchr(global_read_buffer,'/');
329 tmp = strrchr(global_read_buffer,';');
330 if (check_bookmark(global_read_buffer) &&
331 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
333 bookmark_count++;
334 write(temp_bookmark_file, global_read_buffer,
335 strlen(global_read_buffer));
336 write(temp_bookmark_file, "\n", 1);
339 close(bookmark_file);
341 close(temp_bookmark_file);
343 remove(bookmark_file_name);
344 rename(global_temp_buffer, bookmark_file_name);
346 return true;
350 /* ----------------------------------------------------------------------- */
351 /* This function takes the system resume data and formats it into a valid */
352 /* bookmark. */
353 /* ----------------------------------------------------------------------- */
354 static char* create_bookmark()
356 int resume_index = 0;
357 char *file;
359 /* grab the currently playing track */
360 struct mp3entry *id3 = audio_current_track();
361 if(!id3)
362 return NULL;
364 /* Get some basic resume information */
365 /* queue_resume and queue_resume_index are not used and can be ignored.*/
366 playlist_get_resume_info(&resume_index);
368 /* Get the currently playing file minus the path */
369 /* This is used when displaying the available bookmarks */
370 file = strrchr(id3->path,'/');
371 if(NULL == file)
372 return NULL;
374 /* create the bookmark */
375 snprintf(global_bookmark, sizeof(global_bookmark),
376 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
377 resume_index,
378 id3->offset,
379 playlist_get_seed(NULL),
381 id3->elapsed,
382 global_settings.repeat_mode,
383 global_settings.playlist_shuffle,
384 playlist_get_name(NULL, global_temp_buffer,
385 sizeof(global_temp_buffer)),
386 file+1);
388 /* checking to see if the bookmark is valid */
389 if (check_bookmark(global_bookmark))
390 return global_bookmark;
391 else
392 return NULL;
395 static bool check_bookmark(const char* bookmark)
397 return parse_bookmark(bookmark,
398 NULL,NULL,NULL, NULL,
399 NULL,0,NULL,NULL,
400 NULL, NULL);
403 /* ----------------------------------------------------------------------- */
404 /* This function will determine if an autoload is necessary. This is an */
405 /* interface function. */
406 /* ------------------------------------------------------------------------*/
407 bool bookmark_autoload(const char* file)
409 int key;
410 int fd;
411 int i;
413 if(global_settings.autoloadbookmark == BOOKMARK_NO)
414 return false;
416 /*Checking to see if a bookmark file exists.*/
417 if(!generate_bookmark_file_name(file))
419 return false;
421 fd = open(global_bookmark_file_name, O_RDONLY);
422 if(fd<0)
423 return false;
424 if(-1 == lseek(fd, 0, SEEK_END))
426 close(fd);
427 return false;
429 close(fd);
430 if(global_settings.autoloadbookmark == BOOKMARK_YES)
432 return bookmark_load(global_bookmark_file_name, true);
434 else
436 /* Prompting user to confirm bookmark load */
437 FOR_NB_SCREENS(i)
438 screens[i].clear_display();
440 gui_syncstatusbar_draw(&statusbars, false);
442 FOR_NB_SCREENS(i)
444 #ifdef HAVE_LCD_BITMAP
445 screens[i].setmargins(0, STATUSBAR_HEIGHT);
446 screens[i].puts_scroll(0,0, str(LANG_BOOKMARK_AUTOLOAD_QUERY));
447 screens[i].puts(0,1, str(LANG_CONFIRM_WITH_PLAY_RECORDER));
448 screens[i].puts(0,2, str(LANG_BOOKMARK_SELECT_LIST_BOOKMARKS));
449 screens[i].puts(0,3, str(LANG_CANCEL_WITH_ANY_RECORDER));
450 screens[i].update();
451 #else
452 screens[i].puts_scroll(0,0, str(LANG_BOOKMARK_AUTOLOAD_QUERY));
453 screens[i].puts(0,1,str(LANG_RESUME_CONFIRM_PLAYER));
454 #endif
457 /* Wait for a key to be pushed */
458 key = get_action(CONTEXT_BOOKMARKSCREEN,TIMEOUT_BLOCK);
459 switch(key)
461 #ifdef HAVE_LCD_BITMAP
462 case ACTION_STD_NEXT:
463 return bookmark_load(global_bookmark_file_name, false);
464 #endif
465 case ACTION_BMS_SELECT:
466 return bookmark_load(global_bookmark_file_name, true);
468 default:
469 break;
472 action_signalscreenchange();
473 return false;
477 /* ----------------------------------------------------------------------- */
478 /* This function loads the bookmark information into the resume memory. */
479 /* This is an interface function. */
480 /* ------------------------------------------------------------------------*/
481 bool bookmark_load(const char* file, bool autoload)
483 int fd;
484 bool success = true;
485 int offset;
486 int seed;
487 int index;
488 char* bookmark = NULL;;
490 if(autoload)
492 fd = open(file, O_RDONLY);
493 if(fd >= 0)
495 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)))
496 bookmark=global_read_buffer;
497 close(fd);
500 else
502 /* This is not an auto-load, so list the bookmarks */
503 bookmark=select_bookmark(file);
504 if(!bookmark)
505 return true; /* User exited without selecting a bookmark */
508 if(bookmark)
510 success = parse_bookmark(bookmark,
511 &index,
512 &offset,
513 &seed,
514 NULL,
515 global_temp_buffer,
516 sizeof(global_temp_buffer),
517 NULL,
518 &global_settings.repeat_mode,
519 &global_settings.playlist_shuffle,
520 global_filename);
524 if(success)
525 bookmark_play(global_temp_buffer, index, offset, seed,
526 global_filename);
528 return success;
532 static int get_bookmark_count(const char* bookmark_file_name)
534 int read_count = 0;
535 int file = open(bookmark_file_name, O_RDONLY);
537 if(file < 0)
538 return -1;
540 /* Get the requested bookmark */
541 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)))
543 if(check_bookmark(global_read_buffer))
544 read_count++;
547 close(file);
548 return read_count;
553 /* ----------------------------------------------------------------------- */
554 /* This displays a the bookmarks in a file and allows the user to */
555 /* select one to play. */
556 /* ------------------------------------------------------------------------*/
557 static char* select_bookmark(const char* bookmark_file_name)
559 int bookmark_id = 0;
560 int bookmark_id_prev = -1;
561 int key;
562 char* bookmark = NULL;
563 int bookmark_count = 0;
565 #ifdef HAVE_LCD_BITMAP
566 int i;
567 int x = lcd_getxmargin();
568 int y = lcd_getymargin();
569 FOR_NB_SCREENS(i)
570 screens[i].setmargins(0, 0);
571 #endif
573 bookmark_count = get_bookmark_count(bookmark_file_name);
574 action_signalscreenchange();
575 while(true)
577 if(bookmark_id < 0)
578 bookmark_id = bookmark_count -1;
579 if(bookmark_id >= bookmark_count)
580 bookmark_id = 0;
582 if (bookmark_id != bookmark_id_prev)
584 bookmark = get_bookmark(bookmark_file_name, bookmark_id);
585 bookmark_id_prev = bookmark_id;
588 if (!bookmark)
590 /* if there were no bookmarks in the file, delete the file and exit. */
591 if(bookmark_id <= 0)
593 gui_syncsplash(HZ, true, str(LANG_BOOKMARK_LOAD_EMPTY));
594 remove(bookmark_file_name);
595 action_signalscreenchange();
596 return NULL;
598 else
600 bookmark_id_prev = bookmark_id;
601 bookmark_id--;
604 else
606 display_bookmark(bookmark, bookmark_id, bookmark_count);
607 if (global_settings.talk_menu) /* for voice UI */
608 say_bookmark(bookmark, bookmark_id);
611 /* waiting for the user to click a button */
612 key = get_action(CONTEXT_BOOKMARKSCREEN,TIMEOUT_BLOCK);
613 switch(key)
615 case ACTION_BMS_SELECT:
616 /* User wants to use this bookmark */
617 #ifdef HAVE_LCD_BITMAP
618 if (global_settings.statusbar)
620 FOR_NB_SCREENS(i)
621 screens[i].setmargins(0, STATUSBAR_HEIGHT);
623 else
625 FOR_NB_SCREENS(i)
626 screens[i].setmargins(0, 0);
628 #endif
629 action_signalscreenchange();
630 return bookmark;
632 case ACTION_BMS_DELETE:
633 /* User wants to delete this bookmark */
634 delete_bookmark(bookmark_file_name, bookmark_id);
635 bookmark_id_prev=-2;
636 bookmark_count--;
637 if(bookmark_id >= bookmark_count)
638 bookmark_id = bookmark_count -1;
639 break;
641 case ACTION_STD_PREV:
642 case ACTION_STD_PREVREPEAT:
643 bookmark_id--;
644 break;
646 case ACTION_STD_NEXT:
647 case ACTION_STD_NEXTREPEAT:
648 bookmark_id++;
649 break;
651 case ACTION_BMS_EXIT:
652 #ifdef HAVE_LCD_BITMAP
653 FOR_NB_SCREENS(i)
654 screens[i].setmargins(x, y);
655 #endif
656 action_signalscreenchange();
657 return NULL;
659 default:
660 if(default_event_handler(key) == SYS_USB_CONNECTED)
662 action_signalscreenchange();
663 return NULL;
665 break;
668 action_signalscreenchange();
669 return NULL;
673 /* ----------------------------------------------------------------------- */
674 /* This function takes a location in a bookmark file and deletes that */
675 /* bookmark. */
676 /* ------------------------------------------------------------------------*/
677 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
679 int temp_bookmark_file = 0;
680 int bookmark_file = 0;
681 int bookmark_count = 0;
683 /* Opening up a temp bookmark file */
684 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
685 "%s.tmp", bookmark_file_name);
686 temp_bookmark_file = open(global_temp_buffer,
687 O_WRONLY | O_CREAT | O_TRUNC);
688 bookmark_file = open(bookmark_file_name, O_RDONLY);
690 if (temp_bookmark_file < 0 || bookmark_file < 0)
691 return false; /* can't open one of the files */
693 /* Reading in the previous bookmarks and writing them to the temp file */
694 while (read_line(bookmark_file, global_read_buffer,
695 sizeof(global_read_buffer)))
697 /* The MRB has a max of MAX_BOOKMARKS in it */
698 /* This keeps it from getting too large */
699 if ((strcmp(bookmark_file_name,RECENT_BOOKMARK_FILE)==0))
701 if(bookmark_count >= MAX_BOOKMARKS)
702 break;
705 if (check_bookmark(global_read_buffer))
707 if (bookmark_id != bookmark_count)
709 write(temp_bookmark_file, global_read_buffer,
710 strlen(global_read_buffer));
711 write(temp_bookmark_file, "\n", 1);
713 bookmark_count++;
717 close(bookmark_file);
718 close(temp_bookmark_file);
720 remove(bookmark_file_name);
721 rename(global_temp_buffer, bookmark_file_name);
723 return true;
726 /* ----------------------------------------------------------------------- */
727 /* This function parses a bookmark and displays it for the user. */
728 /* ------------------------------------------------------------------------*/
729 static void display_bookmark(const char* bookmark,
730 int bookmark_id,
731 int bookmark_count)
733 int resume_index = 0;
734 long ms = 0;
735 int repeat_mode = 0;
736 bool playlist_shuffle = false;
737 int len;
738 char *dot;
739 int i;
741 /* getting the index and the time into the file */
742 parse_bookmark(bookmark,
743 &resume_index, NULL, NULL, NULL, NULL, 0,
744 &ms, &repeat_mode, &playlist_shuffle,
745 global_filename);
747 FOR_NB_SCREENS(i)
749 screens[i].clear_display();
750 screens[i].stop_scroll();
753 #ifdef HAVE_LCD_BITMAP
754 /* bookmark shuffle and repeat states*/
755 switch (repeat_mode)
757 #if (AB_REPEAT_ENABLE == 1)
758 case REPEAT_AB:
759 statusbar_icon_play_mode(Icon_RepeatAB);
760 break;
761 #endif
763 case REPEAT_ONE:
764 statusbar_icon_play_mode(Icon_RepeatOne);
765 break;
767 case REPEAT_ALL:
768 statusbar_icon_play_mode(Icon_Repeat);
769 break;
771 if(playlist_shuffle)
772 statusbar_icon_shuffle();
774 /* File Name */
775 len=strlen(global_filename);
776 if (len>3)
777 dot=strrchr(global_filename + len - 4, '.');
778 else
779 dot=NULL;
780 if (dot)
781 *dot='\0';
782 FOR_NB_SCREENS(i)
783 screens[i].puts_scroll(0, 0, (unsigned char *)global_filename);
784 if (dot)
785 *dot='.';
787 /* bookmark number */
788 snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %2d/%2d",
789 str(LANG_BOOKMARK_SELECT_BOOKMARK_TEXT),
790 bookmark_id + 1, bookmark_count);
791 FOR_NB_SCREENS(i)
792 screens[i].puts_scroll(0, 1, (unsigned char *)global_temp_buffer);
794 /* bookmark resume index */
795 snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %2d",
796 str(LANG_BOOKMARK_SELECT_INDEX_TEXT), resume_index+1);
797 FOR_NB_SCREENS(i)
798 screens[i].puts_scroll(0, 2, (unsigned char *)global_temp_buffer);
800 /* elapsed time*/
801 if ( ms < 3600000 )
803 snprintf(global_temp_buffer, sizeof(global_temp_buffer), "%s: %ld:%02d",
804 str(LANG_BOOKMARK_SELECT_TIME_TEXT),
805 ms / 60000,
806 (unsigned int)(ms % 60000) / 1000);
807 /* unsigned int: hinting for 16bits archs */
809 else
811 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
812 "%s: %ld:%02ld:%02d",
813 str(LANG_BOOKMARK_SELECT_TIME_TEXT),
814 ms / 3600000,
815 ms % 3600000 / 60000,
816 (unsigned int)(ms % 60000) / 1000);
818 FOR_NB_SCREENS(i)
819 screens[i].puts_scroll(0, 3, (unsigned char *)global_temp_buffer);
821 /* commands */
822 FOR_NB_SCREENS(i)
824 screens[i].puts_scroll(0, 4, str(LANG_BOOKMARK_SELECT_PLAY));
825 screens[i].puts_scroll(0, 5, str(LANG_BOOKMARK_SELECT_EXIT));
826 screens[i].puts_scroll(0, 6, str(LANG_BOOKMARK_SELECT_DELETE));
828 #else
829 (void)bookmark_id;
830 len=strlen(global_filename);
831 if (len>3)
832 dot=strrchr(global_filename+len-4,'.');
833 else
834 dot=NULL;
835 if (dot)
836 *dot='\0';
837 if ( ms < 3600000 )
839 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
840 "%2d, %ld:%02ld, %s,",
841 (bookmark_count+1),
842 ms / 60000,
843 ms % 60000 / 1000,
844 global_filename);
846 else
848 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
849 "%2d, %ld:%02ld:%02ld, %s,",
850 (bookmark_count+1),
851 ms / 60000,
852 ms % 3600000 / 60000,
853 ms % 60000 / 1000,
854 global_filename);
857 gui_syncstatusbar_draw(&statusbars, false);
859 FOR_NB_SCREENS(i)
861 screens[i].puts_scroll(0,0,global_temp_buffer);
862 screens[i].puts(0,1,str(LANG_RESUME_CONFIRM_PLAYER));
864 if (dot)
865 *dot='.';
866 #endif
868 #ifdef HAVE_LCD_BITMAP
869 FOR_NB_SCREENS(i)
870 screens[i].update();
871 #endif
875 /* ----------------------------------------------------------------------- */
876 /* This function parses a bookmark, says the voice UI part of it. */
877 /* ------------------------------------------------------------------------*/
878 static void say_bookmark(const char* bookmark,
879 int bookmark_id)
881 int resume_index;
882 long ms;
883 char dir[MAX_PATH];
884 bool enqueue = false; /* only the first voice is not queued */
886 parse_bookmark(bookmark,
887 &resume_index,
888 NULL, NULL, NULL,
889 dir, sizeof(dir),
890 &ms, NULL, NULL,
891 NULL);
892 /* disabled, because transition between talkbox and voice UI clip is not nice */
893 #if 0
894 if (global_settings.talk_dir >= 3)
895 { /* "talkbox" enabled */
896 char* last = strrchr(dir, '/');
897 if (last)
898 { /* compose filename for talkbox */
899 strncpy(last + 1, dir_thumbnail_name, sizeof(dir)-(last-dir)-1);
900 talk_file(dir, enqueue);
901 enqueue = true;
904 #endif
905 talk_id(VOICE_EXT_BMARK, enqueue);
906 talk_number(bookmark_id + 1, true);
907 talk_id(LANG_BOOKMARK_SELECT_INDEX_TEXT, true);
908 talk_number(resume_index + 1, true);
909 talk_id(LANG_BOOKMARK_SELECT_TIME_TEXT, true);
910 if (ms / 60000)
911 talk_value(ms / 60000, UNIT_MIN, true);
912 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
916 /* ----------------------------------------------------------------------- */
917 /* This function retrieves a given bookmark from a file. */
918 /* If the bookmark requested is beyond the number of bookmarks available */
919 /* in the file, it will return the last one. */
920 /* It also returns the index number of the bookmark in the file */
921 /* ------------------------------------------------------------------------*/
922 static char* get_bookmark(const char* bookmark_file, int bookmark_count)
924 int read_count = -1;
925 int result = 0;
926 int file = open(bookmark_file, O_RDONLY);
928 if (file < 0)
929 return NULL;
931 if (bookmark_count < 0)
932 return NULL;
934 /* Get the requested bookmark */
935 while (read_count < bookmark_count)
937 /*Reading in a single bookmark */
938 result = read_line(file,
939 global_read_buffer,
940 sizeof(global_read_buffer));
942 /* Reading past the last bookmark in the file
943 causes the loop to stop */
944 if (result <= 0)
945 break;
947 read_count++;
950 close(file);
951 if (read_count == bookmark_count)
952 return global_read_buffer;
953 else
954 return NULL;
957 /* ----------------------------------------------------------------------- */
958 /* This function takes a bookmark and parses it. This function also */
959 /* validates the bookmark. Passing in NULL for an output variable */
960 /* indicates that value is not requested. */
961 /* ----------------------------------------------------------------------- */
962 static bool parse_bookmark(const char *bookmark,
963 int *resume_index,
964 int *resume_offset,
965 int *resume_seed,
966 int *resume_first_index,
967 char* resume_file,
968 unsigned int resume_file_size,
969 long* ms,
970 int * repeat_mode, bool *shuffle,
971 char* file_name)
973 /* First check to see if a valid line was passed in. */
974 int bookmark_len = strlen(bookmark);
975 int local_resume_index = 0;
976 int local_resume_offset = 0;
977 int local_resume_seed = 0;
978 int local_resume_first_index = 0;
979 int local_mS = 0;
980 int local_shuffle = 0;
981 int local_repeat_mode = 0;
982 char* local_resume_file = NULL;
983 char* local_file_name = NULL;
984 char* field;
985 char* end;
986 static char bookmarkcopy[MAX_BOOKMARK_SIZE];
988 /* Don't do anything if the bookmark length is 0 */
989 if (bookmark_len <= 0)
990 return false;
992 /* Making a dup of the bookmark to use with strtok_r */
993 strncpy(bookmarkcopy, bookmark, sizeof(bookmarkcopy));
994 bookmarkcopy[sizeof(bookmarkcopy) - 1] = 0;
996 /* resume_index */
997 if ((field = strtok_r(bookmarkcopy, ";", &end)))
998 local_resume_index = atoi(field);
999 else
1000 return false;
1002 /* resume_offset */
1003 if ((field = strtok_r(NULL, ";", &end)))
1004 local_resume_offset = atoi(field);
1005 else
1006 return false;
1008 /* resume_seed */
1009 if ((field = strtok_r(NULL, ";", &end)))
1010 local_resume_seed = atoi(field);
1011 else
1012 return false;
1014 /* resume_first_index */
1015 if ((field = strtok_r(NULL, ";", &end)))
1016 local_resume_first_index = atoi(field);
1017 else
1018 return false;
1020 /* Milliseconds into MP3. Used for the bookmark select menu */
1021 if ((field = strtok_r(NULL, ";", &end)))
1022 local_mS = atoi(field);
1023 else
1024 return false;
1026 /* repeat_mode */
1027 if ((field = strtok_r(NULL, ";", &end)))
1028 local_repeat_mode = atoi(field);
1029 else
1030 return false;
1032 /* shuffle mode */
1033 if ((field = strtok_r(NULL, ";", &end)))
1034 local_shuffle = atoi(field);
1035 else
1036 return false;
1038 /* resume_file & file_name (for the bookmark select menu)*/
1039 if (end)
1041 local_resume_file = strtok_r(NULL, ";", &end);
1043 if (end)
1044 local_file_name = strtok_r(NULL, ";", &end);
1046 else
1047 return false;
1049 /* Only return the values the calling function wants */
1050 if (resume_index)
1051 *resume_index = local_resume_index;
1053 if (resume_offset)
1054 *resume_offset = local_resume_offset;
1056 if (resume_seed)
1057 *resume_seed = local_resume_seed;
1059 if (resume_first_index)
1060 *resume_first_index = local_resume_first_index;
1062 if (resume_file && local_resume_file)
1064 strncpy(resume_file, local_resume_file,
1065 MIN(strlen(local_resume_file), resume_file_size-1));
1066 resume_file[MIN(strlen(local_resume_file), resume_file_size-1)]=0;
1069 if (ms)
1070 *ms = local_mS;
1072 if (shuffle)
1073 *shuffle = local_shuffle;
1075 if (repeat_mode)
1076 *repeat_mode = local_repeat_mode;
1078 if (file_name && local_file_name)
1080 strncpy(file_name, local_file_name,MAX_PATH-1);
1081 file_name[MAX_PATH-1] = 0;
1084 return true;
1087 /* ----------------------------------------------------------------------- */
1088 /* This function is used by multiple functions and is used to generate a */
1089 /* bookmark named based off of the input. */
1090 /* Changing this function could result in how the bookmarks are stored. */
1091 /* it would be here that the centralized/decentralized bookmark code */
1092 /* could be placed. */
1093 /* ----------------------------------------------------------------------- */
1094 static bool generate_bookmark_file_name(const char *in)
1096 int len = strlen(in);
1098 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1099 /* otherwise, name it based on the in variable */
1100 if (!strcmp("/", in))
1101 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1102 else
1104 strcpy(global_bookmark_file_name, in);
1105 if(global_bookmark_file_name[len-1] == '/')
1106 len--;
1107 strcpy(&global_bookmark_file_name[len], ".bmark");
1110 return true;
1113 /* ----------------------------------------------------------------------- */
1114 /* Returns the bookmark name for the current playlist */
1115 /* ----------------------------------------------------------------------- */
1116 bool bookmark_exist(void)
1118 bool exist=false;
1120 if(system_check())
1122 char* name = playlist_get_name(NULL, global_temp_buffer,
1123 sizeof(global_temp_buffer));
1124 if (generate_bookmark_file_name(name))
1126 int fd=open(global_bookmark_file_name, O_RDONLY);
1127 if (fd >=0)
1129 close(fd);
1130 exist=true;
1135 return exist;
1138 /* ----------------------------------------------------------------------- */
1139 /* Checks the current state of the system and returns if it is in a */
1140 /* bookmarkable state. */
1141 /* ----------------------------------------------------------------------- */
1142 /* Inputs: */
1143 /* ----------------------------------------------------------------------- */
1144 /* Outputs: */
1145 /* return bool: Indicates if the system was in a bookmarkable state */
1146 /* ----------------------------------------------------------------------- */
1147 static bool system_check(void)
1149 int resume_index = 0;
1150 struct mp3entry *id3 = audio_current_track();
1152 if (!id3)
1154 /* no track playing */
1155 return false;
1158 /* Checking to see if playing a queued track */
1159 if (playlist_get_resume_info(&resume_index) == -1)
1161 /* something bad happened while getting the queue information */
1162 return false;
1164 else if (playlist_modified(NULL))
1166 /* can't bookmark while in the queue */
1167 return false;
1170 return true;