Fix reds. No need for #ifdef to save buttons anymore.
[maemo-rb.git] / apps / bookmark.c
blob82a23fbbc9024e4ef48a6d588aaf65e170fbe0c7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "action.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "system.h"
34 #include "icons.h"
35 #include "menu.h"
36 #include "lang.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "splash.h"
40 #include "yesno.h"
41 #include "list.h"
42 #include "plugin.h"
43 #include "file.h"
44 #include "filefuncs.h"
45 #include "dsp.h"
47 #define MAX_BOOKMARKS 10
48 #define MAX_BOOKMARK_SIZE 350
49 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
51 /* Used to buffer bookmarks while displaying the bookmark list. */
52 struct bookmark_list
54 const char* filename;
55 size_t buffer_size;
56 int start;
57 int count;
58 int total_count;
59 bool show_dont_resume;
60 bool reload;
61 bool show_playlist_name;
62 char* items[];
65 /* flags for optional bookmark tokens */
66 #define BM_PITCH 0x01
67 #define BM_SPEED 0x02
69 /* bookmark values */
70 static struct {
71 int resume_index;
72 unsigned long resume_offset;
73 int resume_seed;
74 long resume_time;
75 int repeat_mode;
76 bool shuffle;
77 /* optional values */
78 int pitch;
79 int speed;
80 } bm;
82 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
83 bool most_recent);
84 static char* create_bookmark(void);
85 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
86 static void say_bookmark(const char* bookmark,
87 int bookmark_id, bool show_playlist_name);
88 static bool play_bookmark(const char* bookmark);
89 static bool generate_bookmark_file_name(const char *in);
90 static bool parse_bookmark(const char *bookmark, const bool get_filenames);
91 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
92 static const char* get_bookmark_info(int list_index,
93 void* data,
94 char *buffer,
95 size_t buffer_len);
96 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
97 static bool is_bookmarkable_state(void);
98 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
99 static int get_bookmark_count(const char* bookmark_file_name);
101 #define TEMP_BUF_SIZE (MAX_PATH + 1)
102 static char global_temp_buffer[TEMP_BUF_SIZE];
103 /* File name created by generate_bookmark_file_name */
104 static char global_bookmark_file_name[MAX_PATH];
105 static char global_read_buffer[MAX_BOOKMARK_SIZE];
106 /* Bookmark created by create_bookmark*/
107 static char global_bookmark[MAX_BOOKMARK_SIZE];
108 /* Filename from parsed bookmark (can be made local where needed) */
109 static char global_filename[MAX_PATH];
111 /* ----------------------------------------------------------------------- */
112 /* This is the interface function from the main menu. */
113 /* ----------------------------------------------------------------------- */
114 bool bookmark_create_menu(void)
116 write_bookmark(true, create_bookmark());
117 return false;
120 /* ----------------------------------------------------------------------- */
121 /* This function acts as the load interface from the main menu */
122 /* This function determines the bookmark file name and then loads that file*/
123 /* for the user. The user can then select a bookmark to load. */
124 /* If no file/directory is currently playing, the menu item does not work. */
125 /* ----------------------------------------------------------------------- */
126 bool bookmark_load_menu(void)
128 if (is_bookmarkable_state())
130 char* name = playlist_get_name(NULL, global_temp_buffer,
131 sizeof(global_temp_buffer));
132 if (generate_bookmark_file_name(name))
134 char* bookmark = select_bookmark(global_bookmark_file_name, false);
136 if (bookmark != NULL)
138 return play_bookmark(bookmark);
143 return false;
146 /* ----------------------------------------------------------------------- */
147 /* Gives the user a list of the Most Recent Bookmarks. This is an */
148 /* interface function */
149 /* ----------------------------------------------------------------------- */
150 bool bookmark_mrb_load()
152 char* bookmark;
153 bool ret = false;
155 push_current_activity(ACTIVITY_BOOKMARKSLIST);
156 bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
157 if (bookmark != NULL)
159 ret = play_bookmark(bookmark);
162 pop_current_activity();
163 return ret;
166 /* ----------------------------------------------------------------------- */
167 /* This function handles an autobookmark creation. This is an interface */
168 /* function. */
169 /* ----------------------------------------------------------------------- */
170 bool bookmark_autobookmark(bool prompt_ok)
172 char* bookmark;
173 bool update;
175 if (!is_bookmarkable_state())
176 return false;
178 audio_pause(); /* first pause playback */
179 update = (global_settings.autoupdatebookmark && bookmark_exists());
180 bookmark = create_bookmark();
181 #if CONFIG_CODEC != SWCODEC
182 /* Workaround for inability to speak when paused: all callers will
183 just do audio_stop() when we return, so we can do it right
184 away. This makes it possible to speak the "Create a Bookmark?"
185 prompt and the "Bookmark Created" splash. */
186 audio_stop();
187 #endif
189 if (update)
190 return write_bookmark(true, bookmark);
192 switch (global_settings.autocreatebookmark)
194 case BOOKMARK_YES:
195 return write_bookmark(true, bookmark);
197 case BOOKMARK_NO:
198 return false;
200 case BOOKMARK_RECENT_ONLY_YES:
201 return write_bookmark(false, bookmark);
203 #ifdef HAVE_LCD_BITMAP
204 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
205 const struct text_message message={lines, 1};
206 #else
207 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
208 str(LANG_CONFIRM_WITH_BUTTON)};
209 const struct text_message message={lines, 2};
210 #endif
212 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
214 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
215 return write_bookmark(false, bookmark);
216 else
217 return write_bookmark(true, bookmark);
219 return false;
222 /* ----------------------------------------------------------------------- */
223 /* This function takes the current current resume information and writes */
224 /* that to the beginning of the bookmark file. */
225 /* This file will contain N number of bookmarks in the following format: */
226 /* resume_index*resume_offset*resume_seed*resume_first_index* */
227 /* resume_file*milliseconds*MP3 Title* */
228 /* ------------------------------------------------------------------------*/
229 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
231 bool success=false;
232 if (!bookmark)
233 return false; /* something didn't happen correctly, do nothing */
235 if (global_settings.usemrb)
236 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
239 /* writing the bookmark */
240 if (create_bookmark_file)
242 char* name = playlist_get_name(NULL, global_temp_buffer,
243 sizeof(global_temp_buffer));
244 if (generate_bookmark_file_name(name))
246 success = add_bookmark(global_bookmark_file_name, bookmark, false);
250 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
251 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
253 return true;
256 /* ----------------------------------------------------------------------- */
257 /* This function adds a bookmark to a file. */
258 /* ------------------------------------------------------------------------*/
259 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
260 bool most_recent)
262 int temp_bookmark_file = 0;
263 int bookmark_file = 0;
264 int bookmark_count = 0;
265 char* playlist = NULL;
266 char* cp;
267 char* tmp;
268 int len = 0;
269 bool unique = false;
271 /* Opening up a temp bookmark file */
272 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
273 "%s.tmp", bookmark_file_name);
274 temp_bookmark_file = open(global_temp_buffer,
275 O_WRONLY | O_CREAT | O_TRUNC, 0666);
276 if (temp_bookmark_file < 0)
277 return false; /* can't open the temp file */
279 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
281 playlist = strchr(bookmark,'/');
282 cp = strrchr(bookmark,';');
283 len = cp - playlist;
284 unique = true;
287 /* Writing the new bookmark to the begining of the temp file */
288 write(temp_bookmark_file, bookmark, strlen(bookmark));
289 write(temp_bookmark_file, "\n", 1);
290 bookmark_count++;
292 /* Reading in the previous bookmarks and writing them to the temp file */
293 bookmark_file = open(bookmark_file_name, O_RDONLY);
294 if (bookmark_file >= 0)
296 while (read_line(bookmark_file, global_read_buffer,
297 sizeof(global_read_buffer)) > 0)
299 /* The MRB has a max of MAX_BOOKMARKS in it */
300 /* This keeps it from getting too large */
301 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
302 break;
304 cp = strchr(global_read_buffer,'/');
305 tmp = strrchr(global_read_buffer,';');
306 if (parse_bookmark(global_read_buffer, false) &&
307 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
309 bookmark_count++;
310 write(temp_bookmark_file, global_read_buffer,
311 strlen(global_read_buffer));
312 write(temp_bookmark_file, "\n", 1);
315 close(bookmark_file);
317 close(temp_bookmark_file);
319 remove(bookmark_file_name);
320 rename(global_temp_buffer, bookmark_file_name);
322 return true;
326 /* ----------------------------------------------------------------------- */
327 /* This function takes the system resume data and formats it into a valid */
328 /* bookmark. */
329 /* ----------------------------------------------------------------------- */
330 static char* create_bookmark()
332 int resume_index = 0;
333 char *file;
335 if (!is_bookmarkable_state())
336 return NULL; /* something didn't happen correctly, do nothing */
338 /* grab the currently playing track */
339 struct mp3entry *id3 = audio_current_track();
340 if(!id3)
341 return NULL;
343 /* Get some basic resume information */
344 /* queue_resume and queue_resume_index are not used and can be ignored.*/
345 playlist_get_resume_info(&resume_index);
347 /* Get the currently playing file minus the path */
348 /* This is used when displaying the available bookmarks */
349 file = strrchr(id3->path,'/');
350 if(NULL == file)
351 return NULL;
353 /* create the bookmark */
354 snprintf(global_bookmark, sizeof(global_bookmark),
355 /* new optional bookmark token descriptors should be inserted
356 just before the "%s;%s" in this line... */
357 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
358 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
359 #else
360 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
361 #endif
362 /* ... their flags should go here ... */
363 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
364 BM_PITCH | BM_SPEED,
365 #else
367 #endif
368 resume_index,
369 id3->offset,
370 playlist_get_seed(NULL),
371 id3->elapsed,
372 global_settings.repeat_mode,
373 global_settings.playlist_shuffle,
374 /* ...and their values should go here */
375 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
376 (long)sound_get_pitch(),
377 (long)dsp_get_timestretch(),
378 #endif
379 /* more mandatory tokens */
380 playlist_get_name(NULL, global_temp_buffer,
381 sizeof(global_temp_buffer)),
382 file+1);
384 /* checking to see if the bookmark is valid */
385 if (parse_bookmark(global_bookmark, false))
386 return global_bookmark;
387 else
388 return NULL;
391 /* ----------------------------------------------------------------------- */
392 /* This function will determine if an autoload is necessary. This is an */
393 /* interface function. */
394 /* ------------------------------------------------------------------------*/
395 bool bookmark_autoload(const char* file)
397 if(global_settings.autoloadbookmark == BOOKMARK_NO)
398 return false;
400 /*Checking to see if a bookmark file exists.*/
401 if(!generate_bookmark_file_name(file))
403 return false;
406 if(!file_exists(global_bookmark_file_name))
407 return false;
409 if(global_settings.autoloadbookmark == BOOKMARK_YES)
411 return bookmark_load(global_bookmark_file_name, true);
413 else
415 char* bookmark = select_bookmark(global_bookmark_file_name, true);
417 if (bookmark != NULL)
419 if (!play_bookmark(bookmark))
421 /* Selected bookmark not found. */
422 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
425 /* Act as if autoload was done even if it failed, since the
426 * user did make an active selection.
428 return true;
431 return false;
435 /* ----------------------------------------------------------------------- */
436 /* This function loads the bookmark information into the resume memory. */
437 /* This is an interface function. */
438 /* ------------------------------------------------------------------------*/
439 bool bookmark_load(const char* file, bool autoload)
441 int fd;
442 char* bookmark = NULL;
444 if(autoload)
446 fd = open(file, O_RDONLY);
447 if(fd >= 0)
449 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
450 bookmark=global_read_buffer;
451 close(fd);
454 else
456 /* This is not an auto-load, so list the bookmarks */
457 bookmark = select_bookmark(file, false);
460 if (bookmark != NULL)
462 if (!play_bookmark(bookmark))
464 /* Selected bookmark not found. */
465 if (!autoload)
467 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
470 return false;
474 return true;
478 static int get_bookmark_count(const char* bookmark_file_name)
480 int read_count = 0;
481 int file = open(bookmark_file_name, O_RDONLY);
483 if(file < 0)
484 return -1;
486 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
488 read_count++;
491 close(file);
492 return read_count;
495 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
497 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
498 int read_count = 0;
499 int file = open(bookmarks->filename, O_RDONLY);
501 if (file < 0)
503 return -1;
506 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
507 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
509 /* Entire file fits in buffer */
510 first_line = 0;
513 bookmarks->start = first_line;
514 bookmarks->count = 0;
515 bookmarks->reload = false;
517 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
519 read_count++;
521 if (read_count >= first_line)
523 dest -= strlen(global_read_buffer) + 1;
525 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
526 + (sizeof(char*) * (bookmarks->count + 1)))
528 break;
531 strcpy(dest, global_read_buffer);
532 bookmarks->items[bookmarks->count] = dest;
533 bookmarks->count++;
537 close(file);
538 return bookmarks->start + bookmarks->count;
541 static const char* get_bookmark_info(int list_index,
542 void* data,
543 char *buffer,
544 size_t buffer_len)
546 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
547 int index = list_index / 2;
549 if (bookmarks->show_dont_resume)
551 if (index == 0)
553 return list_index % 2 == 0
554 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
557 index--;
560 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
561 || (index < bookmarks->start))
563 int read_index = index;
565 /* Using count as a guide on how far to move could possibly fail
566 * sometimes. Use byte count if that is a problem?
569 if (read_index != 0)
571 /* Move count * 3 / 4 items in the direction the user is moving,
572 * but don't go too close to the end.
574 int offset = bookmarks->count;
575 int max = bookmarks->total_count - (bookmarks->count / 2);
577 if (read_index < bookmarks->start)
579 offset *= 3;
582 read_index = index - offset / 4;
584 if (read_index > max)
586 read_index = max;
589 if (read_index < 0)
591 read_index = 0;
595 if (buffer_bookmarks(bookmarks, read_index) <= index)
597 return "";
601 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
603 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
606 if (list_index % 2 == 0)
608 char *name;
609 char *format;
610 int len = strlen(global_temp_buffer);
612 if (bookmarks->show_playlist_name && len > 0)
614 name = global_temp_buffer;
615 len--;
617 if (name[len] != '/')
619 strrsplt(name, '.');
621 else if (len > 1)
623 name[len] = '\0';
626 if (len > 1)
628 name = strrsplt(name, '/');
631 format = "%s : %s";
633 else
635 name = global_filename;
636 format = "%s";
639 strrsplt(global_filename, '.');
640 snprintf(buffer, buffer_len, format, name, global_filename);
641 return buffer;
643 else
645 char time_buf[32];
647 format_time(time_buf, sizeof(time_buf), bm.resume_time);
648 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
649 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
650 return buffer;
654 static int bookmark_list_voice_cb(int list_index, void* data)
656 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
657 int index = list_index / 2;
659 if (bookmarks->show_dont_resume)
661 if (index == 0)
662 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
663 index--;
665 say_bookmark(bookmarks->items[index - bookmarks->start], index,
666 bookmarks->show_playlist_name);
667 return 0;
670 /* ----------------------------------------------------------------------- */
671 /* This displays a the bookmarks in a file and allows the user to */
672 /* select one to play. */
673 /* ------------------------------------------------------------------------*/
674 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
676 struct bookmark_list* bookmarks;
677 struct gui_synclist list;
678 int item = 0;
679 int action;
680 size_t size;
681 bool exit = false;
682 bool refresh = true;
684 bookmarks = plugin_get_buffer(&size);
685 bookmarks->buffer_size = size;
686 bookmarks->show_dont_resume = show_dont_resume;
687 bookmarks->filename = bookmark_file_name;
688 bookmarks->start = 0;
689 bookmarks->show_playlist_name
690 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
691 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
692 if(global_settings.talk_menu)
693 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
694 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
695 Icon_Bookmark);
697 while (!exit)
700 if (refresh)
702 int count = get_bookmark_count(bookmark_file_name);
703 bookmarks->total_count = count;
705 if (bookmarks->total_count < 1)
707 /* No more bookmarks, delete file and exit */
708 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
709 remove(bookmark_file_name);
710 return NULL;
713 if (bookmarks->show_dont_resume)
715 count++;
716 item++;
719 gui_synclist_set_nb_items(&list, count * 2);
721 if (item >= count)
723 /* Selected item has been deleted */
724 item = count - 1;
725 gui_synclist_select_item(&list, item * 2);
728 buffer_bookmarks(bookmarks, bookmarks->start);
729 gui_synclist_draw(&list);
730 cond_talk_ids_fq(VOICE_EXT_BMARK);
731 gui_synclist_speak_item(&list);
732 refresh = false;
735 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
736 &list, &action, LIST_WRAP_UNLESS_HELD);
737 item = gui_synclist_get_sel_pos(&list) / 2;
739 if (bookmarks->show_dont_resume)
741 item--;
744 if (action == ACTION_STD_CONTEXT)
746 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
747 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
748 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
749 static const int menu_actions[] =
751 ACTION_STD_OK, ACTION_BMS_DELETE
753 int selection = do_menu(&menu_items, NULL, NULL, false);
755 refresh = true;
757 if (selection >= 0 && selection <=
758 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
760 action = menu_actions[selection];
764 switch (action)
766 case ACTION_STD_OK:
767 if (item >= 0)
769 talk_shutup();
770 return bookmarks->items[item - bookmarks->start];
773 /* Else fall through */
775 case ACTION_TREE_WPS:
776 case ACTION_STD_CANCEL:
777 exit = true;
778 break;
780 case ACTION_BMS_DELETE:
781 if (item >= 0)
783 const char *lines[]={
784 ID2P(LANG_REALLY_DELETE)
786 const char *yes_lines[]={
787 ID2P(LANG_DELETING)
790 const struct text_message message={lines, 1};
791 const struct text_message yes_message={yes_lines, 1};
793 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
795 delete_bookmark(bookmark_file_name, item);
796 bookmarks->reload = true;
798 refresh = true;
800 break;
802 default:
803 if (default_event_handler(action) == SYS_USB_CONNECTED)
805 exit = true;
808 break;
812 talk_shutup();
813 return NULL;
816 /* ----------------------------------------------------------------------- */
817 /* This function takes a location in a bookmark file and deletes that */
818 /* bookmark. */
819 /* ------------------------------------------------------------------------*/
820 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
822 int temp_bookmark_file = 0;
823 int bookmark_file = 0;
824 int bookmark_count = 0;
826 /* Opening up a temp bookmark file */
827 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
828 "%s.tmp", bookmark_file_name);
829 temp_bookmark_file = open(global_temp_buffer,
830 O_WRONLY | O_CREAT | O_TRUNC, 0666);
832 if (temp_bookmark_file < 0)
833 return false; /* can't open the temp file */
835 /* Reading in the previous bookmarks and writing them to the temp file */
836 bookmark_file = open(bookmark_file_name, O_RDONLY);
837 if (bookmark_file >= 0)
839 while (read_line(bookmark_file, global_read_buffer,
840 sizeof(global_read_buffer)) > 0)
842 if (bookmark_id != bookmark_count)
844 write(temp_bookmark_file, global_read_buffer,
845 strlen(global_read_buffer));
846 write(temp_bookmark_file, "\n", 1);
848 bookmark_count++;
850 close(bookmark_file);
852 close(temp_bookmark_file);
854 remove(bookmark_file_name);
855 rename(global_temp_buffer, bookmark_file_name);
857 return true;
860 /* ----------------------------------------------------------------------- */
861 /* This function parses a bookmark, says the voice UI part of it. */
862 /* ------------------------------------------------------------------------*/
863 static void say_bookmark(const char* bookmark,
864 int bookmark_id, bool show_playlist_name)
866 if (!parse_bookmark(bookmark, true))
868 talk_id(LANG_BOOKMARK_INVALID, false);
869 return;
872 talk_number(bookmark_id + 1, false);
874 #if CONFIG_CODEC == SWCODEC
875 bool is_dir = (global_temp_buffer[0]
876 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
878 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
879 together, because there is no guarantee that the same mp3
880 parameters are used. */
881 if(show_playlist_name)
882 { /* It's useful to know which playlist this is */
883 if(is_dir)
884 talk_dir_or_spell(global_temp_buffer,
885 TALK_IDARRAY(VOICE_DIR), true);
886 else talk_file_or_spell(NULL, global_temp_buffer,
887 TALK_IDARRAY(LANG_PLAYLIST), true);
889 #else
890 (void)show_playlist_name;
891 #endif
893 if(bm.shuffle)
894 talk_id(LANG_SHUFFLE, true);
896 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
897 talk_number(bm.resume_index + 1, true);
898 talk_id(LANG_TIME, true);
899 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
901 #if CONFIG_CODEC == SWCODEC
902 /* Track filename */
903 if(is_dir)
904 talk_file_or_spell(global_temp_buffer, global_filename,
905 TALK_IDARRAY(VOICE_FILE), true);
906 else
907 { /* Unfortunately if this is a playlist, we do not know in which
908 directory the file is and therefore cannot find the track's
909 .talk file. */
910 talk_id(VOICE_FILE, true);
911 talk_spell(global_filename, true);
913 #endif
916 /* ----------------------------------------------------------------------- */
917 /* This function parses a bookmark and then plays it. */
918 /* ------------------------------------------------------------------------*/
919 static bool play_bookmark(const char* bookmark)
921 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
922 /* preset pitch and speed to 100% in case bookmark doesn't have info */
923 bm.pitch = sound_get_pitch();
924 bm.speed = dsp_get_timestretch();
925 #endif
927 if (parse_bookmark(bookmark, true))
929 global_settings.repeat_mode = bm.repeat_mode;
930 global_settings.playlist_shuffle = bm.shuffle;
931 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
932 sound_set_pitch(bm.pitch);
933 dsp_set_timestretch(bm.speed);
934 #endif
935 if (!warn_on_pl_erase())
936 return false;
937 return bookmark_play(global_temp_buffer, bm.resume_index,
938 bm.resume_offset, bm.resume_seed, global_filename);
941 return false;
944 static const char* skip_token(const char* s)
946 while (*s && *s != ';')
948 s++;
951 if (*s)
953 s++;
956 return s;
959 static const char* int_token(const char* s, int* dest)
961 *dest = atoi(s);
962 return skip_token(s);
965 static const char* long_token(const char* s, long* dest)
967 *dest = atoi(s); /* Should be atol, but we don't have it. */
968 return skip_token(s);
971 /* ----------------------------------------------------------------------- */
972 /* This function takes a bookmark and parses it. This function also */
973 /* validates the bookmark. The parse_filenames flag indicates whether */
974 /* the filename tokens are to be extracted. */
975 /* ----------------------------------------------------------------------- */
976 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
978 const char* s = bookmark;
979 const char* end;
981 #define GET_INT_TOKEN(var) s = int_token(s, &var)
982 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
983 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
985 /* if new format bookmark, extract the optional content flags,
986 otherwise treat as an original format bookmark */
987 int opt_flags = 0;
988 bool new_format = (strchr(s, '>') == s);
989 if (new_format)
991 s++;
992 GET_INT_TOKEN(opt_flags);
995 /* extract all original bookmark tokens */
996 GET_INT_TOKEN(bm.resume_index);
997 GET_LONG_TOKEN(bm.resume_offset);
998 GET_INT_TOKEN(bm.resume_seed);
999 if (!new_format) /* skip deprecated token */
1000 s = skip_token(s);
1001 GET_LONG_TOKEN(bm.resume_time);
1002 GET_INT_TOKEN(bm.repeat_mode);
1003 GET_BOOL_TOKEN(bm.shuffle);
1005 /* extract all optional bookmark tokens */
1006 if (opt_flags & BM_PITCH)
1007 GET_INT_TOKEN(bm.pitch);
1008 if (opt_flags & BM_SPEED)
1009 GET_INT_TOKEN(bm.speed);
1011 if (*s == 0)
1013 return false;
1016 end = strchr(s, ';');
1018 /* extract file names */
1019 if (parse_filenames)
1021 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1022 len = MIN(TEMP_BUF_SIZE - 1, len);
1023 strlcpy(global_temp_buffer, s, len + 1);
1025 if (end != NULL)
1027 end++;
1028 strlcpy(global_filename, end, MAX_PATH);
1032 return true;
1035 /* ----------------------------------------------------------------------- */
1036 /* This function is used by multiple functions and is used to generate a */
1037 /* bookmark named based off of the input. */
1038 /* Changing this function could result in how the bookmarks are stored. */
1039 /* it would be here that the centralized/decentralized bookmark code */
1040 /* could be placed. */
1041 /* ----------------------------------------------------------------------- */
1042 static bool generate_bookmark_file_name(const char *in)
1044 int len = strlen(in);
1046 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1047 /* otherwise, name it based on the in variable */
1048 if (!strcmp("/", in))
1049 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1050 else
1052 #ifdef HAVE_MULTIVOLUME
1053 /* The "root" of an extra volume need special handling too. */
1054 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1055 !strcmp("/", global_bookmark_file_name));
1056 #endif
1058 strcpy(global_bookmark_file_name, in);
1059 if(global_bookmark_file_name[len-1] == '/')
1060 len--;
1061 #ifdef HAVE_MULTIVOLUME
1062 if (volume_root)
1063 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1064 else
1065 #endif
1066 strcpy(&global_bookmark_file_name[len], ".bmark");
1069 return true;
1072 /* ----------------------------------------------------------------------- */
1073 /* Returns true if a bookmark file exists for the current playlist */
1074 /* ----------------------------------------------------------------------- */
1075 bool bookmark_exists(void)
1077 bool exist=false;
1079 if(is_bookmarkable_state())
1081 char* name = playlist_get_name(NULL, global_temp_buffer,
1082 sizeof(global_temp_buffer));
1083 if (generate_bookmark_file_name(name))
1085 exist = file_exists(global_bookmark_file_name);
1089 return exist;
1092 /* ----------------------------------------------------------------------- */
1093 /* Checks the current state of the system and returns true if it is in a */
1094 /* bookmarkable state. */
1095 /* ----------------------------------------------------------------------- */
1096 static bool is_bookmarkable_state(void)
1098 int resume_index = 0;
1100 if (!(audio_status() && audio_current_track()) ||
1101 /* no track playing */
1102 (playlist_get_resume_info(&resume_index) == -1) ||
1103 /* invalid queue info */
1104 (playlist_modified(NULL)))
1105 /* can't bookmark while in the queue */
1107 return false;
1110 return true;