blob: 567f98ac296b0245c89bc2bc0a6b9773c9da900e [file] [log] [blame]
Jean-Philippe Bernardy0f6b3792005-02-09 10:56:54 +00001/***************************************************************************
Björn Stenberga108ec22004-01-14 00:13:04 +00002 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
Jean-Philippe Bernardy0f6b3792005-02-09 10:56:54 +00009 * Copyright (C)2003 by Benjamin Metzler
Björn Stenberga108ec22004-01-14 00:13:04 +000010 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000011 * 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.
Björn Stenberga108ec22004-01-14 00:13:04 +000015 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
Björn Stenberga108ec22004-01-14 00:13:04 +000021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdbool.h>
25
Bertrik Sikkenef1ce432008-04-28 16:28:21 +000026#include "config.h"
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +000027#include "action.h"
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +000028#include "audio.h"
Christi Scarborough4c0b83f2005-11-17 20:14:59 +000029#include "playlist.h"
Björn Stenberga108ec22004-01-14 00:13:04 +000030#include "settings.h"
Jens Arnold34232592005-05-17 23:34:16 +000031#include "tree.h"
Björn Stenberga108ec22004-01-14 00:13:04 +000032#include "bookmark.h"
Björn Stenberga108ec22004-01-14 00:13:04 +000033#include "system.h"
Björn Stenberga108ec22004-01-14 00:13:04 +000034#include "icons.h"
Björn Stenberga108ec22004-01-14 00:13:04 +000035#include "menu.h"
36#include "lang.h"
Jörg Hohensohnfa97f162004-03-19 22:15:53 +000037#include "talk.h"
Linus Nielsen Feltzingade5d7b2004-07-26 16:06:59 +000038#include "misc.h"
Kevin Ferraree991bee2005-11-16 15:12:15 +000039#include "splash.h"
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000040#include "yesno.h"
Magnus Holmgrene676b812007-05-26 10:44:38 +000041#include "list.h"
42#include "plugin.h"
Bertrik Sikkenef1ce432008-04-28 16:28:21 +000043#include "file.h"
Michael Sevakis7d1a47c2013-08-05 22:02:45 -040044#include "pathfuncs.h"
Peter D'Hoyee60cb432006-07-15 14:40:44 +000045
Linus Nielsen Feltzing19480d52004-03-02 13:50:35 +000046#define MAX_BOOKMARKS 10
Björn Stenberga108ec22004-01-14 00:13:04 +000047#define MAX_BOOKMARK_SIZE 350
48#define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
49
Magnus Holmgrene676b812007-05-26 10:44:38 +000050/* Used to buffer bookmarks while displaying the bookmark list. */
51struct bookmark_list
52{
53 const char* filename;
54 size_t buffer_size;
55 int start;
56 int count;
57 int total_count;
58 bool show_dont_resume;
59 bool reload;
Magnus Holmgren47167e72007-11-18 12:59:08 +000060 bool show_playlist_name;
Magnus Holmgrene676b812007-05-26 10:44:38 +000061 char* items[];
62};
63
Jeffrey Goode55064f72010-04-10 21:17:09 +000064/* flags for optional bookmark tokens */
65#define BM_PITCH 0x01
66#define BM_SPEED 0x02
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000067
Jeffrey Goodeb2ba1122010-04-06 21:49:33 +000068/* bookmark values */
69static struct {
70 int resume_index;
71 unsigned long resume_offset;
72 int resume_seed;
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000073 long resume_time;
74 int repeat_mode;
75 bool shuffle;
Jeffrey Goode55064f72010-04-10 21:17:09 +000076 /* optional values */
77 int pitch;
78 int speed;
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000079} bm;
80
Magnus Holmgren12bf6252007-02-20 21:08:54 +000081static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
82 bool most_recent);
Björn Stenberga108ec22004-01-14 00:13:04 +000083static char* create_bookmark(void);
Jens Arnold8fb33612004-08-18 01:09:31 +000084static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
Jens Arnold8fb33612004-08-18 01:09:31 +000085static void say_bookmark(const char* bookmark,
Stéphane Doyonbd067692008-07-15 16:10:15 +000086 int bookmark_id, bool show_playlist_name);
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000087static bool play_bookmark(const char* bookmark);
Jens Arnold8fb33612004-08-18 01:09:31 +000088static bool generate_bookmark_file_name(const char *in);
Jeffrey Goode55064f72010-04-10 21:17:09 +000089static bool parse_bookmark(const char *bookmark, const bool get_filenames);
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000090static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
Nils Wallménius3200d042009-08-20 16:47:44 +000091static const char* get_bookmark_info(int list_index,
92 void* data,
93 char *buffer,
94 size_t buffer_len);
Osborne Jacobsfb30d012012-03-11 00:47:28 -050095static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark);
Steve Bavineb66b552008-07-16 06:10:26 +000096static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
Jens Arnold8fb33612004-08-18 01:09:31 +000097static int get_bookmark_count(const char* bookmark_file_name);
Björn Stenberga108ec22004-01-14 00:13:04 +000098
Jeffrey Goode7209c5d2010-04-05 20:53:04 +000099#define TEMP_BUF_SIZE (MAX_PATH + 1)
100static char global_temp_buffer[TEMP_BUF_SIZE];
Magnus Holmgrene676b812007-05-26 10:44:38 +0000101/* File name created by generate_bookmark_file_name */
Björn Stenberga108ec22004-01-14 00:13:04 +0000102static char global_bookmark_file_name[MAX_PATH];
103static char global_read_buffer[MAX_BOOKMARK_SIZE];
Magnus Holmgrene676b812007-05-26 10:44:38 +0000104/* Bookmark created by create_bookmark*/
Björn Stenberga108ec22004-01-14 00:13:04 +0000105static char global_bookmark[MAX_BOOKMARK_SIZE];
Magnus Holmgrene676b812007-05-26 10:44:38 +0000106/* Filename from parsed bookmark (can be made local where needed) */
Linus Nielsen Feltzing8f032ae2004-12-07 14:20:37 +0000107static char global_filename[MAX_PATH];
Björn Stenberga108ec22004-01-14 00:13:04 +0000108
109/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500110/* This is an interface function from the context menu. */
111/* Returns true on successful bookmark creation. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000112/* ----------------------------------------------------------------------- */
113bool bookmark_create_menu(void)
114{
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500115 return write_bookmark(true, create_bookmark());
Björn Stenberga108ec22004-01-14 00:13:04 +0000116}
117
118/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500119/* This function acts as the load interface from the context menu. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000120/* This function determines the bookmark file name and then loads that file*/
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500121/* for the user. The user can then select or delete previous bookmarks. */
122/* This function returns BOOKMARK_SUCCESS on the selection of a track to */
123/* resume, BOOKMARK_FAIL if the menu is exited without a selection and */
124/* BOOKMARK_USB_CONNECTED if the menu is forced to exit due to a USB */
125/* connection. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000126/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500127int bookmark_load_menu(void)
Björn Stenberga108ec22004-01-14 00:13:04 +0000128{
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500129 char* bookmark;
130 int ret = BOOKMARK_FAIL;
Osborne Jacobsbcfa7832012-03-02 22:34:14 -0500131
132 push_current_activity(ACTIVITY_BOOKMARKSLIST);
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500133
134 char* name = playlist_get_name(NULL, global_temp_buffer,
Björn Stenberga108ec22004-01-14 00:13:04 +0000135 sizeof(global_temp_buffer));
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500136 if (generate_bookmark_file_name(name))
137 {
138 ret = select_bookmark(global_bookmark_file_name, false, &bookmark);
139 if (bookmark != NULL)
Björn Stenberga108ec22004-01-14 00:13:04 +0000140 {
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500141 ret = play_bookmark(bookmark) ? BOOKMARK_SUCCESS : BOOKMARK_FAIL;
Björn Stenberga108ec22004-01-14 00:13:04 +0000142 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000143 }
144
Osborne Jacobsbcfa7832012-03-02 22:34:14 -0500145 pop_current_activity();
146 return ret;
Björn Stenberga108ec22004-01-14 00:13:04 +0000147}
148
149/* ----------------------------------------------------------------------- */
150/* Gives the user a list of the Most Recent Bookmarks. This is an */
151/* interface function */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500152/* Returns true on the successful selection of a recent bookmark. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000153/* ----------------------------------------------------------------------- */
154bool bookmark_mrb_load()
155{
Jonathan Gordon835683b2011-08-07 08:39:56 +0000156 char* bookmark;
157 bool ret = false;
Björn Stenberga108ec22004-01-14 00:13:04 +0000158
Jonathan Gordon835683b2011-08-07 08:39:56 +0000159 push_current_activity(ACTIVITY_BOOKMARKSLIST);
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500160 select_bookmark(RECENT_BOOKMARK_FILE, false, &bookmark);
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000161 if (bookmark != NULL)
162 {
Jonathan Gordon835683b2011-08-07 08:39:56 +0000163 ret = play_bookmark(bookmark);
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000164 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000165
Jonathan Gordon835683b2011-08-07 08:39:56 +0000166 pop_current_activity();
Jonathan Gordon0bba82b2011-08-07 09:35:50 +0000167 return ret;
Björn Stenberga108ec22004-01-14 00:13:04 +0000168}
169
Björn Stenberga108ec22004-01-14 00:13:04 +0000170/* ----------------------------------------------------------------------- */
171/* This function handles an autobookmark creation. This is an interface */
172/* function. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500173/* Returns true on successful bookmark creation. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000174/* ----------------------------------------------------------------------- */
Torne Wuff05ace8e2010-03-28 23:27:49 +0000175bool bookmark_autobookmark(bool prompt_ok)
Björn Stenberga108ec22004-01-14 00:13:04 +0000176{
Stéphane Doyon431c8952008-07-15 19:50:22 +0000177 char* bookmark;
Alexander Levin2c56ac82010-07-06 17:03:45 +0000178 bool update;
179
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500180 if (!bookmark_is_bookmarkable_state())
Björn Stenberga108ec22004-01-14 00:13:04 +0000181 return false;
182
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000183 audio_pause(); /* first pause playback */
Alexander Levin2c56ac82010-07-06 17:03:45 +0000184 update = (global_settings.autoupdatebookmark && bookmark_exists());
Stéphane Doyon431c8952008-07-15 19:50:22 +0000185 bookmark = create_bookmark();
Michael Sevakis29b5b322011-08-21 21:21:40 +0000186#if CONFIG_CODEC != SWCODEC
Stéphane Doyon431c8952008-07-15 19:50:22 +0000187 /* Workaround for inability to speak when paused: all callers will
188 just do audio_stop() when we return, so we can do it right
189 away. This makes it possible to speak the "Create a Bookmark?"
190 prompt and the "Bookmark Created" splash. */
191 audio_stop();
Michael Sevakis29b5b322011-08-21 21:21:40 +0000192#endif
Alexander Levinf8e78702010-07-07 17:30:53 +0000193
Torne Wuffdb1b8232010-07-05 16:39:00 +0000194 if (update)
195 return write_bookmark(true, bookmark);
Alexander Levinf8e78702010-07-07 17:30:53 +0000196
Björn Stenberga108ec22004-01-14 00:13:04 +0000197 switch (global_settings.autocreatebookmark)
198 {
199 case BOOKMARK_YES:
Stéphane Doyon431c8952008-07-15 19:50:22 +0000200 return write_bookmark(true, bookmark);
Björn Stenberga108ec22004-01-14 00:13:04 +0000201
202 case BOOKMARK_NO:
203 return false;
204
205 case BOOKMARK_RECENT_ONLY_YES:
Stéphane Doyon431c8952008-07-15 19:50:22 +0000206 return write_bookmark(false, bookmark);
Björn Stenberga108ec22004-01-14 00:13:04 +0000207 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000208#ifdef HAVE_LCD_BITMAP
Nils Wallménius33c44462008-04-26 09:30:24 +0000209 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
210 const struct text_message message={lines, 1};
Björn Stenberga108ec22004-01-14 00:13:04 +0000211#else
Nils Wallménius33c44462008-04-26 09:30:24 +0000212 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
Nils Wallméniusb3113672007-08-05 19:19:39 +0000213 str(LANG_CONFIRM_WITH_BUTTON)};
Nils Wallménius33c44462008-04-26 09:30:24 +0000214 const struct text_message message={lines, 2};
Björn Stenberga108ec22004-01-14 00:13:04 +0000215#endif
Thomas Martitz4764ee02009-08-06 00:14:40 +0000216
Torne Wuff05ace8e2010-03-28 23:27:49 +0000217 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
Björn Stenberga108ec22004-01-14 00:13:04 +0000218 {
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000219 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
Stéphane Doyon431c8952008-07-15 19:50:22 +0000220 return write_bookmark(false, bookmark);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000221 else
Stéphane Doyon431c8952008-07-15 19:50:22 +0000222 return write_bookmark(true, bookmark);
Björn Stenberga108ec22004-01-14 00:13:04 +0000223 }
Jens Arnold4dab0d22004-10-24 21:45:37 +0000224 return false;
Björn Stenberga108ec22004-01-14 00:13:04 +0000225}
226
227/* ----------------------------------------------------------------------- */
228/* This function takes the current current resume information and writes */
229/* that to the beginning of the bookmark file. */
230/* This file will contain N number of bookmarks in the following format: */
231/* resume_index*resume_offset*resume_seed*resume_first_index* */
232/* resume_file*milliseconds*MP3 Title* */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500233/* Returns true on successful bookmark write. */
234/* Returns false if any part of the bookmarking process fails. It is */
235/* possible that a bookmark is successfully added to the most recent */
236/* bookmark list but fails to be added to the bookmark file or vice versa. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000237/* ------------------------------------------------------------------------*/
Steve Bavineb66b552008-07-16 06:10:26 +0000238static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
Björn Stenberga108ec22004-01-14 00:13:04 +0000239{
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500240 bool ret=true;
241
Björn Stenberga108ec22004-01-14 00:13:04 +0000242 if (!bookmark)
Björn Stenberga108ec22004-01-14 00:13:04 +0000243 {
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500244 ret = false; /* something didn't happen correctly, do nothing */
245 }
246 else
247 {
248 if (global_settings.usemrb)
249 ret = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
250
251
252 /* writing the bookmark */
253 if (create_bookmark_file)
Björn Stenberga108ec22004-01-14 00:13:04 +0000254 {
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500255 char* name = playlist_get_name(NULL, global_temp_buffer,
256 sizeof(global_temp_buffer));
257 if (generate_bookmark_file_name(name))
258 {
259 ret = ret & add_bookmark(global_bookmark_file_name, bookmark, false);
260 }
261 else
262 {
263 ret = false; /* generating bookmark file failed */
264 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000265 }
266 }
267
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500268 splash(HZ, ret ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
Nils Wallménius01729e72008-08-15 08:27:39 +0000269 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
Björn Stenberga108ec22004-01-14 00:13:04 +0000270
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500271 return ret;
Björn Stenberga108ec22004-01-14 00:13:04 +0000272}
273
274/* ----------------------------------------------------------------------- */
275/* This function adds a bookmark to a file. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500276/* Returns true on successful bookmark add. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000277/* ------------------------------------------------------------------------*/
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000278static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
279 bool most_recent)
Björn Stenberga108ec22004-01-14 00:13:04 +0000280{
281 int temp_bookmark_file = 0;
282 int bookmark_file = 0;
283 int bookmark_count = 0;
284 char* playlist = NULL;
285 char* cp;
Henrik Backe0e90bc32004-03-21 13:42:18 +0000286 char* tmp;
Björn Stenberga108ec22004-01-14 00:13:04 +0000287 int len = 0;
288 bool unique = false;
289
290 /* Opening up a temp bookmark file */
291 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
292 "%s.tmp", bookmark_file_name);
293 temp_bookmark_file = open(global_temp_buffer,
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000294 O_WRONLY | O_CREAT | O_TRUNC, 0666);
Björn Stenberga108ec22004-01-14 00:13:04 +0000295 if (temp_bookmark_file < 0)
296 return false; /* can't open the temp file */
297
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000298 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
Björn Stenberga108ec22004-01-14 00:13:04 +0000299 {
300 playlist = strchr(bookmark,'/');
301 cp = strrchr(bookmark,';');
302 len = cp - playlist;
303 unique = true;
304 }
305
306 /* Writing the new bookmark to the begining of the temp file */
307 write(temp_bookmark_file, bookmark, strlen(bookmark));
308 write(temp_bookmark_file, "\n", 1);
Linus Nielsen Feltzing19480d52004-03-02 13:50:35 +0000309 bookmark_count++;
Björn Stenberga108ec22004-01-14 00:13:04 +0000310
311 /* Reading in the previous bookmarks and writing them to the temp file */
312 bookmark_file = open(bookmark_file_name, O_RDONLY);
313 if (bookmark_file >= 0)
314 {
315 while (read_line(bookmark_file, global_read_buffer,
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000316 sizeof(global_read_buffer)) > 0)
Björn Stenberga108ec22004-01-14 00:13:04 +0000317 {
Linus Nielsen Feltzing19480d52004-03-02 13:50:35 +0000318 /* The MRB has a max of MAX_BOOKMARKS in it */
319 /* This keeps it from getting too large */
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000320 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
Linus Nielsen Feltzing19480d52004-03-02 13:50:35 +0000321 break;
Linus Nielsen Feltzing19480d52004-03-02 13:50:35 +0000322
Henrik Backe0e90bc32004-03-21 13:42:18 +0000323 cp = strchr(global_read_buffer,'/');
324 tmp = strrchr(global_read_buffer,';');
Jeffrey Goode55064f72010-04-10 21:17:09 +0000325 if (parse_bookmark(global_read_buffer, false) &&
Henrik Backe0e90bc32004-03-21 13:42:18 +0000326 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
Björn Stenberga108ec22004-01-14 00:13:04 +0000327 {
Henrik Backe0e90bc32004-03-21 13:42:18 +0000328 bookmark_count++;
329 write(temp_bookmark_file, global_read_buffer,
330 strlen(global_read_buffer));
331 write(temp_bookmark_file, "\n", 1);
Björn Stenberga108ec22004-01-14 00:13:04 +0000332 }
333 }
334 close(bookmark_file);
335 }
336 close(temp_bookmark_file);
337
338 remove(bookmark_file_name);
339 rename(global_temp_buffer, bookmark_file_name);
340
341 return true;
342}
343
344
345/* ----------------------------------------------------------------------- */
346/* This function takes the system resume data and formats it into a valid */
347/* bookmark. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500348/* Returns not NULL on successful bookmark format. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000349/* ----------------------------------------------------------------------- */
350static char* create_bookmark()
351{
352 int resume_index = 0;
353 char *file;
354
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500355 if (!bookmark_is_bookmarkable_state())
Stéphane Doyon431c8952008-07-15 19:50:22 +0000356 return NULL; /* something didn't happen correctly, do nothing */
357
Björn Stenberga108ec22004-01-14 00:13:04 +0000358 /* grab the currently playing track */
Linus Nielsen Feltzing8a237a82005-04-04 12:06:29 +0000359 struct mp3entry *id3 = audio_current_track();
Björn Stenberga108ec22004-01-14 00:13:04 +0000360 if(!id3)
361 return NULL;
362
363 /* Get some basic resume information */
364 /* queue_resume and queue_resume_index are not used and can be ignored.*/
365 playlist_get_resume_info(&resume_index);
366
367 /* Get the currently playing file minus the path */
368 /* This is used when displaying the available bookmarks */
369 file = strrchr(id3->path,'/');
370 if(NULL == file)
371 return NULL;
372
373 /* create the bookmark */
374 snprintf(global_bookmark, sizeof(global_bookmark),
Jeffrey Goode55064f72010-04-10 21:17:09 +0000375 /* new optional bookmark token descriptors should be inserted
376 just before the "%s;%s" in this line... */
Nils Wallméniusd29a11b2012-05-08 16:34:26 +0200377#if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
Thomas Martitz50a6ca32010-05-06 21:04:40 +0000378 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
Jeffrey Goodebbbf5292010-04-11 02:32:58 +0000379#else
380 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
Jeffrey Goode4f3f7dd2010-04-10 21:41:01 +0000381#endif
Jeffrey Goode55064f72010-04-10 21:17:09 +0000382 /* ... their flags should go here ... */
Nils Wallméniusd29a11b2012-05-08 16:34:26 +0200383#if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
Jeffrey Goode55064f72010-04-10 21:17:09 +0000384 BM_PITCH | BM_SPEED,
Jeffrey Goode4f3f7dd2010-04-10 21:41:01 +0000385#else
386 0,
387#endif
Björn Stenberga108ec22004-01-14 00:13:04 +0000388 resume_index,
389 id3->offset,
Hardeep Sidhu107ebc52004-01-26 17:05:21 +0000390 playlist_get_seed(NULL),
Björn Stenberga108ec22004-01-14 00:13:04 +0000391 id3->elapsed,
392 global_settings.repeat_mode,
393 global_settings.playlist_shuffle,
Jeffrey Goode55064f72010-04-10 21:17:09 +0000394 /* ...and their values should go here */
Nils Wallméniusd29a11b2012-05-08 16:34:26 +0200395#if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
Thomas Martitz50a6ca32010-05-06 21:04:40 +0000396 (long)sound_get_pitch(),
397 (long)dsp_get_timestretch(),
Jeffrey Goode4f3f7dd2010-04-10 21:41:01 +0000398#endif
Jeffrey Goode55064f72010-04-10 21:17:09 +0000399 /* more mandatory tokens */
Hardeep Sidhu107ebc52004-01-26 17:05:21 +0000400 playlist_get_name(NULL, global_temp_buffer,
401 sizeof(global_temp_buffer)),
Björn Stenberga108ec22004-01-14 00:13:04 +0000402 file+1);
403
404 /* checking to see if the bookmark is valid */
Jeffrey Goode55064f72010-04-10 21:17:09 +0000405 if (parse_bookmark(global_bookmark, false))
Björn Stenberga108ec22004-01-14 00:13:04 +0000406 return global_bookmark;
407 else
408 return NULL;
409}
410
Björn Stenberga108ec22004-01-14 00:13:04 +0000411/* ----------------------------------------------------------------------- */
412/* This function will determine if an autoload is necessary. This is an */
413/* interface function. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500414/* Returns true on bookmark load or bookmark selection. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000415/* ------------------------------------------------------------------------*/
Jens Arnold8fb33612004-08-18 01:09:31 +0000416bool bookmark_autoload(const char* file)
Björn Stenberga108ec22004-01-14 00:13:04 +0000417{
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500418 char* bookmark;
419
Björn Stenberga108ec22004-01-14 00:13:04 +0000420 if(global_settings.autoloadbookmark == BOOKMARK_NO)
421 return false;
422
423 /*Checking to see if a bookmark file exists.*/
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +0000424 if(!generate_bookmark_file_name(file))
Björn Stenberga108ec22004-01-14 00:13:04 +0000425 {
426 return false;
427 }
Nils Wallméniusa0199642008-04-16 19:51:43 +0000428
429 if(!file_exists(global_bookmark_file_name))
Björn Stenberga108ec22004-01-14 00:13:04 +0000430 return false;
Nils Wallméniusa0199642008-04-16 19:51:43 +0000431
Björn Stenberga108ec22004-01-14 00:13:04 +0000432 if(global_settings.autoloadbookmark == BOOKMARK_YES)
433 {
434 return bookmark_load(global_bookmark_file_name, true);
435 }
436 else
437 {
Richard Quirkdcba7412013-03-05 19:31:36 +0100438 int ret = select_bookmark(global_bookmark_file_name, true, &bookmark);
Martin Scarratt9c43c382006-07-18 19:11:56 +0000439
Magnus Holmgren9824dac2007-08-11 14:44:05 +0000440 if (bookmark != NULL)
Martin Scarratt9c43c382006-07-18 19:11:56 +0000441 {
Magnus Holmgren04dc8282008-02-10 12:16:27 +0000442 if (!play_bookmark(bookmark))
443 {
444 /* Selected bookmark not found. */
Nils Wallménius01729e72008-08-15 08:27:39 +0000445 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
Magnus Holmgren04dc8282008-02-10 12:16:27 +0000446 }
447
448 /* Act as if autoload was done even if it failed, since the
449 * user did make an active selection.
450 */
451 return true;
Björn Stenberga108ec22004-01-14 00:13:04 +0000452 }
Martin Scarratt7847dde2006-08-20 17:25:56 +0000453
Richard Quirkdcba7412013-03-05 19:31:36 +0100454 return ret != BOOKMARK_SUCCESS;
Björn Stenberga108ec22004-01-14 00:13:04 +0000455 }
456}
457
458/* ----------------------------------------------------------------------- */
459/* This function loads the bookmark information into the resume memory. */
460/* This is an interface function. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500461/* Returns true on successful bookmark load. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000462/* ------------------------------------------------------------------------*/
Jens Arnold8fb33612004-08-18 01:09:31 +0000463bool bookmark_load(const char* file, bool autoload)
Björn Stenberga108ec22004-01-14 00:13:04 +0000464{
465 int fd;
Magnus Holmgren04dc8282008-02-10 12:16:27 +0000466 char* bookmark = NULL;
Björn Stenberga108ec22004-01-14 00:13:04 +0000467
468 if(autoload)
469 {
470 fd = open(file, O_RDONLY);
471 if(fd >= 0)
472 {
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000473 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
Björn Stenberga108ec22004-01-14 00:13:04 +0000474 bookmark=global_read_buffer;
475 close(fd);
476 }
477 }
478 else
479 {
480 /* This is not an auto-load, so list the bookmarks */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500481 select_bookmark(file, false, &bookmark);
Björn Stenberga108ec22004-01-14 00:13:04 +0000482 }
483
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000484 if (bookmark != NULL)
Björn Stenberga108ec22004-01-14 00:13:04 +0000485 {
Magnus Holmgren04dc8282008-02-10 12:16:27 +0000486 if (!play_bookmark(bookmark))
487 {
488 /* Selected bookmark not found. */
Magnus Holmgren932ef002008-02-21 21:01:42 +0000489 if (!autoload)
490 {
Nils Wallménius01729e72008-08-15 08:27:39 +0000491 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
Magnus Holmgren932ef002008-02-21 21:01:42 +0000492 }
493
Magnus Holmgren04dc8282008-02-10 12:16:27 +0000494 return false;
495 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000496 }
497
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000498 return true;
Björn Stenberga108ec22004-01-14 00:13:04 +0000499}
500
501
Jens Arnold8fb33612004-08-18 01:09:31 +0000502static int get_bookmark_count(const char* bookmark_file_name)
Björn Stenberga108ec22004-01-14 00:13:04 +0000503{
504 int read_count = 0;
505 int file = open(bookmark_file_name, O_RDONLY);
506
507 if(file < 0)
508 return -1;
509
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000510 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
Björn Stenberga108ec22004-01-14 00:13:04 +0000511 {
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000512 read_count++;
Björn Stenberga108ec22004-01-14 00:13:04 +0000513 }
514
515 close(file);
516 return read_count;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000517}
518
519static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
520{
521 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
522 int read_count = 0;
523 int file = open(bookmarks->filename, O_RDONLY);
524
525 if (file < 0)
526 {
527 return -1;
528 }
529
530 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
531 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
532 {
533 /* Entire file fits in buffer */
534 first_line = 0;
535 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000536
Magnus Holmgrene676b812007-05-26 10:44:38 +0000537 bookmarks->start = first_line;
538 bookmarks->count = 0;
539 bookmarks->reload = false;
540
541 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
542 {
543 read_count++;
544
545 if (read_count >= first_line)
546 {
547 dest -= strlen(global_read_buffer) + 1;
548
549 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
550 + (sizeof(char*) * (bookmarks->count + 1)))
551 {
552 break;
553 }
554
555 strcpy(dest, global_read_buffer);
556 bookmarks->items[bookmarks->count] = dest;
557 bookmarks->count++;
558 }
559 }
560
561 close(file);
562 return bookmarks->start + bookmarks->count;
563}
564
Nils Wallménius3200d042009-08-20 16:47:44 +0000565static const char* get_bookmark_info(int list_index,
566 void* data,
567 char *buffer,
568 size_t buffer_len)
Magnus Holmgrene676b812007-05-26 10:44:38 +0000569{
570 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
571 int index = list_index / 2;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000572
573 if (bookmarks->show_dont_resume)
574 {
575 if (index == 0)
576 {
577 return list_index % 2 == 0
578 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
579 }
580
581 index--;
582 }
583
584 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
585 || (index < bookmarks->start))
586 {
587 int read_index = index;
588
589 /* Using count as a guide on how far to move could possibly fail
590 * sometimes. Use byte count if that is a problem?
591 */
592
593 if (read_index != 0)
594 {
595 /* Move count * 3 / 4 items in the direction the user is moving,
596 * but don't go too close to the end.
597 */
598 int offset = bookmarks->count;
599 int max = bookmarks->total_count - (bookmarks->count / 2);
600
601 if (read_index < bookmarks->start)
602 {
603 offset *= 3;
604 }
605
606 read_index = index - offset / 4;
607
608 if (read_index > max)
609 {
610 read_index = max;
611 }
612
613 if (read_index < 0)
614 {
615 read_index = 0;
616 }
617 }
618
619 if (buffer_bookmarks(bookmarks, read_index) <= index)
620 {
621 return "";
622 }
623 }
624
Jeffrey Goode55064f72010-04-10 21:17:09 +0000625 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
Magnus Holmgrene676b812007-05-26 10:44:38 +0000626 {
627 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
628 }
629
630 if (list_index % 2 == 0)
631 {
Magnus Holmgren47167e72007-11-18 12:59:08 +0000632 char *name;
633 char *format;
634 int len = strlen(global_temp_buffer);
635
636 if (bookmarks->show_playlist_name && len > 0)
Magnus Holmgrene676b812007-05-26 10:44:38 +0000637 {
Magnus Holmgren47167e72007-11-18 12:59:08 +0000638 name = global_temp_buffer;
639 len--;
640
641 if (name[len] != '/')
642 {
643 strrsplt(name, '.');
644 }
645 else if (len > 1)
646 {
647 name[len] = '\0';
648 }
Magnus Holmgrene676b812007-05-26 10:44:38 +0000649
Magnus Holmgren47167e72007-11-18 12:59:08 +0000650 if (len > 1)
651 {
652 name = strrsplt(name, '/');
653 }
654
655 format = "%s : %s";
656 }
657 else
658 {
659 name = global_filename;
660 format = "%s";
661 }
662
663 strrsplt(global_filename, '.');
Nils Wallménius68489612008-04-09 15:25:17 +0000664 snprintf(buffer, buffer_len, format, name, global_filename);
Magnus Holmgren47167e72007-11-18 12:59:08 +0000665 return buffer;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000666 }
667 else
668 {
669 char time_buf[32];
670
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000671 format_time(time_buf, sizeof(time_buf), bm.resume_time);
672 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
673 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
Magnus Holmgrene676b812007-05-26 10:44:38 +0000674 return buffer;
675 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000676}
677
Stéphane Doyonbd067692008-07-15 16:10:15 +0000678static int bookmark_list_voice_cb(int list_index, void* data)
679{
680 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
681 int index = list_index / 2;
682
683 if (bookmarks->show_dont_resume)
684 {
685 if (index == 0)
686 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
687 index--;
688 }
689 say_bookmark(bookmarks->items[index - bookmarks->start], index,
690 bookmarks->show_playlist_name);
691 return 0;
692}
693
Björn Stenberga108ec22004-01-14 00:13:04 +0000694/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500695/* This displays the bookmarks in a file and allows the user to */
Björn Stenberga108ec22004-01-14 00:13:04 +0000696/* select one to play. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500697/* *selected_bookmark contains a non NULL value on successful bookmark */
698/* selection. */
699/* Returns BOOKMARK_SUCCESS on successful bookmark selection, BOOKMARK_FAIL*/
700/* if no selection was made and BOOKMARK_USB_CONNECTED if the selection */
701/* menu is forced to exit due to a USB connection. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000702/* ------------------------------------------------------------------------*/
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500703static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark)
Björn Stenberga108ec22004-01-14 00:13:04 +0000704{
Magnus Holmgrene676b812007-05-26 10:44:38 +0000705 struct bookmark_list* bookmarks;
706 struct gui_synclist list;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000707 int item = 0;
708 int action;
709 size_t size;
710 bool exit = false;
711 bool refresh = true;
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500712 int ret = BOOKMARK_FAIL;
Björn Stenberga108ec22004-01-14 00:13:04 +0000713
Magnus Holmgrene676b812007-05-26 10:44:38 +0000714 bookmarks = plugin_get_buffer(&size);
715 bookmarks->buffer_size = size;
716 bookmarks->show_dont_resume = show_dont_resume;
717 bookmarks->filename = bookmark_file_name;
718 bookmarks->start = 0;
Magnus Holmgren47167e72007-11-18 12:59:08 +0000719 bookmarks->show_playlist_name
720 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000721 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
Stéphane Doyonbd067692008-07-15 16:10:15 +0000722 if(global_settings.talk_menu)
723 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
Magnus Holmgrene676b812007-05-26 10:44:38 +0000724 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
725 Icon_Bookmark);
Björn Stenberga108ec22004-01-14 00:13:04 +0000726
Magnus Holmgrene676b812007-05-26 10:44:38 +0000727 while (!exit)
728 {
Björn Stenberga108ec22004-01-14 00:13:04 +0000729
Magnus Holmgrene676b812007-05-26 10:44:38 +0000730 if (refresh)
Björn Stenberga108ec22004-01-14 00:13:04 +0000731 {
Magnus Holmgrene676b812007-05-26 10:44:38 +0000732 int count = get_bookmark_count(bookmark_file_name);
733 bookmarks->total_count = count;
734
735 if (bookmarks->total_count < 1)
Björn Stenberga108ec22004-01-14 00:13:04 +0000736 {
Magnus Holmgrene676b812007-05-26 10:44:38 +0000737 /* No more bookmarks, delete file and exit */
Nils Wallménius01729e72008-08-15 08:27:39 +0000738 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
Björn Stenberga108ec22004-01-14 00:13:04 +0000739 remove(bookmark_file_name);
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500740 *selected_bookmark = NULL;
741 return BOOKMARK_FAIL;
Björn Stenberga108ec22004-01-14 00:13:04 +0000742 }
Magnus Holmgrene676b812007-05-26 10:44:38 +0000743
744 if (bookmarks->show_dont_resume)
745 {
746 count++;
747 item++;
748 }
749
750 gui_synclist_set_nb_items(&list, count * 2);
751
752 if (item >= count)
753 {
754 /* Selected item has been deleted */
755 item = count - 1;
756 gui_synclist_select_item(&list, item * 2);
757 }
758
759 buffer_bookmarks(bookmarks, bookmarks->start);
760 gui_synclist_draw(&list);
Stéphane Doyonbd067692008-07-15 16:10:15 +0000761 cond_talk_ids_fq(VOICE_EXT_BMARK);
762 gui_synclist_speak_item(&list);
Magnus Holmgrene676b812007-05-26 10:44:38 +0000763 refresh = false;
764 }
765
Stéphane Doyonbd067692008-07-15 16:10:15 +0000766 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
767 &list, &action, LIST_WRAP_UNLESS_HELD);
Magnus Holmgrene676b812007-05-26 10:44:38 +0000768 item = gui_synclist_get_sel_pos(&list) / 2;
769
770 if (bookmarks->show_dont_resume)
771 {
772 item--;
773 }
Stéphane Doyonbd067692008-07-15 16:10:15 +0000774
Magnus Holmgrene676b812007-05-26 10:44:38 +0000775 if (action == ACTION_STD_CONTEXT)
Björn Stenberga108ec22004-01-14 00:13:04 +0000776 {
Magnus Holmgrene676b812007-05-26 10:44:38 +0000777 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
778 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
779 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
780 static const int menu_actions[] =
781 {
782 ACTION_STD_OK, ACTION_BMS_DELETE
783 };
Jonathan Gordon5ca15392008-03-26 03:35:24 +0000784 int selection = do_menu(&menu_items, NULL, NULL, false);
Magnus Holmgrene676b812007-05-26 10:44:38 +0000785
786 refresh = true;
787
788 if (selection >= 0 && selection <=
789 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
790 {
791 action = menu_actions[selection];
792 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000793 }
794
Magnus Holmgrene676b812007-05-26 10:44:38 +0000795 switch (action)
Björn Stenberga108ec22004-01-14 00:13:04 +0000796 {
Magnus Holmgrene676b812007-05-26 10:44:38 +0000797 case ACTION_STD_OK:
798 if (item >= 0)
799 {
Stéphane Doyonbd067692008-07-15 16:10:15 +0000800 talk_shutup();
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500801 *selected_bookmark = bookmarks->items[item - bookmarks->start];
802 return BOOKMARK_SUCCESS;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000803 }
Richard Quirkdcba7412013-03-05 19:31:36 +0100804 exit = true;
805 ret = BOOKMARK_SUCCESS;
806 break;
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000807
Magnus Holmgrene676b812007-05-26 10:44:38 +0000808 case ACTION_TREE_WPS:
809 case ACTION_STD_CANCEL:
810 exit = true;
811 break;
Björn Stenberga108ec22004-01-14 00:13:04 +0000812
Magnus Holmgrene676b812007-05-26 10:44:38 +0000813 case ACTION_BMS_DELETE:
814 if (item >= 0)
Jonathan Gordonb9aabcb2009-12-16 08:18:40 +0000815 {
816 const char *lines[]={
Jonathan Gordon4e890252009-12-16 08:38:27 +0000817 ID2P(LANG_REALLY_DELETE)
Jonathan Gordonb9aabcb2009-12-16 08:18:40 +0000818 };
819 const char *yes_lines[]={
Jonathan Gordon4e890252009-12-16 08:38:27 +0000820 ID2P(LANG_DELETING)
Jonathan Gordonb9aabcb2009-12-16 08:18:40 +0000821 };
822
Jonathan Gordon4e890252009-12-16 08:38:27 +0000823 const struct text_message message={lines, 1};
824 const struct text_message yes_message={yes_lines, 1};
Jonathan Gordonb9aabcb2009-12-16 08:18:40 +0000825
826 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
827 {
Jonathan Gordonb9aabcb2009-12-16 08:18:40 +0000828 delete_bookmark(bookmark_file_name, item);
829 bookmarks->reload = true;
830 }
Magnus Holmgrene676b812007-05-26 10:44:38 +0000831 refresh = true;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000832 }
833 break;
Jens Arnold34232592005-05-17 23:34:16 +0000834
Magnus Holmgrene676b812007-05-26 10:44:38 +0000835 default:
836 if (default_event_handler(action) == SYS_USB_CONNECTED)
837 {
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500838 ret = BOOKMARK_USB_CONNECTED;
Magnus Holmgrene676b812007-05-26 10:44:38 +0000839 exit = true;
840 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000841
Magnus Holmgrene676b812007-05-26 10:44:38 +0000842 break;
Björn Stenberga108ec22004-01-14 00:13:04 +0000843 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000844 }
Magnus Holmgrene676b812007-05-26 10:44:38 +0000845
Stéphane Doyonbd067692008-07-15 16:10:15 +0000846 talk_shutup();
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500847 *selected_bookmark = NULL;
848 return ret;
Björn Stenberga108ec22004-01-14 00:13:04 +0000849}
850
Björn Stenberga108ec22004-01-14 00:13:04 +0000851/* ----------------------------------------------------------------------- */
852/* This function takes a location in a bookmark file and deletes that */
853/* bookmark. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500854/* Returns true on successful bookmark deletion. */
Björn Stenberga108ec22004-01-14 00:13:04 +0000855/* ------------------------------------------------------------------------*/
Jens Arnold8fb33612004-08-18 01:09:31 +0000856static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
Björn Stenberga108ec22004-01-14 00:13:04 +0000857{
858 int temp_bookmark_file = 0;
859 int bookmark_file = 0;
860 int bookmark_count = 0;
861
862 /* Opening up a temp bookmark file */
863 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
864 "%s.tmp", bookmark_file_name);
865 temp_bookmark_file = open(global_temp_buffer,
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000866 O_WRONLY | O_CREAT | O_TRUNC, 0666);
Björn Stenberga108ec22004-01-14 00:13:04 +0000867
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000868 if (temp_bookmark_file < 0)
869 return false; /* can't open the temp file */
Björn Stenberga108ec22004-01-14 00:13:04 +0000870
871 /* Reading in the previous bookmarks and writing them to the temp file */
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000872 bookmark_file = open(bookmark_file_name, O_RDONLY);
873 if (bookmark_file >= 0)
Björn Stenberga108ec22004-01-14 00:13:04 +0000874 {
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000875 while (read_line(bookmark_file, global_read_buffer,
876 sizeof(global_read_buffer)) > 0)
Björn Stenberga108ec22004-01-14 00:13:04 +0000877 {
878 if (bookmark_id != bookmark_count)
879 {
880 write(temp_bookmark_file, global_read_buffer,
881 strlen(global_read_buffer));
882 write(temp_bookmark_file, "\n", 1);
883 }
884 bookmark_count++;
885 }
Magnus Holmgren12bf6252007-02-20 21:08:54 +0000886 close(bookmark_file);
Björn Stenberga108ec22004-01-14 00:13:04 +0000887 }
Björn Stenberga108ec22004-01-14 00:13:04 +0000888 close(temp_bookmark_file);
889
890 remove(bookmark_file_name);
891 rename(global_temp_buffer, bookmark_file_name);
892
893 return true;
894}
895
896/* ----------------------------------------------------------------------- */
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000897/* This function parses a bookmark, says the voice UI part of it. */
898/* ------------------------------------------------------------------------*/
Jens Arnold8fb33612004-08-18 01:09:31 +0000899static void say_bookmark(const char* bookmark,
Stéphane Doyonbd067692008-07-15 16:10:15 +0000900 int bookmark_id, bool show_playlist_name)
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000901{
Jeffrey Goode55064f72010-04-10 21:17:09 +0000902 if (!parse_bookmark(bookmark, true))
Magnus Holmgrene676b812007-05-26 10:44:38 +0000903 {
Stéphane Doyonbd067692008-07-15 16:10:15 +0000904 talk_id(LANG_BOOKMARK_INVALID, false);
Magnus Holmgrene676b812007-05-26 10:44:38 +0000905 return;
906 }
907
Stéphane Doyonbd067692008-07-15 16:10:15 +0000908 talk_number(bookmark_id + 1, false);
909
Stéphane Doyonbd067692008-07-15 16:10:15 +0000910#if CONFIG_CODEC == SWCODEC
Nils Wallménius2e3162f2011-05-24 10:56:01 +0000911 bool is_dir = (global_temp_buffer[0]
912 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
913
Stéphane Doyonbd067692008-07-15 16:10:15 +0000914 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
915 together, because there is no guarantee that the same mp3
916 parameters are used. */
917 if(show_playlist_name)
918 { /* It's useful to know which playlist this is */
919 if(is_dir)
920 talk_dir_or_spell(global_temp_buffer,
921 TALK_IDARRAY(VOICE_DIR), true);
922 else talk_file_or_spell(NULL, global_temp_buffer,
923 TALK_IDARRAY(LANG_PLAYLIST), true);
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000924 }
Stéphane Doyonbd067692008-07-15 16:10:15 +0000925#else
926 (void)show_playlist_name;
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000927#endif
Stéphane Doyonbd067692008-07-15 16:10:15 +0000928
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000929 if(bm.shuffle)
Stéphane Doyonbd067692008-07-15 16:10:15 +0000930 talk_id(LANG_SHUFFLE, true);
931
Magnus Holmgrenf9f3cad2007-05-26 11:59:24 +0000932 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000933 talk_number(bm.resume_index + 1, true);
Nils Wallméniusb3113672007-08-05 19:19:39 +0000934 talk_id(LANG_TIME, true);
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000935 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
Stéphane Doyonbd067692008-07-15 16:10:15 +0000936
937#if CONFIG_CODEC == SWCODEC
938 /* Track filename */
939 if(is_dir)
940 talk_file_or_spell(global_temp_buffer, global_filename,
941 TALK_IDARRAY(VOICE_FILE), true);
942 else
943 { /* Unfortunately if this is a playlist, we do not know in which
944 directory the file is and therefore cannot find the track's
945 .talk file. */
946 talk_id(VOICE_FILE, true);
947 talk_spell(global_filename, true);
948 }
949#endif
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000950}
951
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000952/* ----------------------------------------------------------------------- */
953/* This function parses a bookmark and then plays it. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -0500954/* Returns true on successful bookmark play. */
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000955/* ------------------------------------------------------------------------*/
956static bool play_bookmark(const char* bookmark)
957{
Nils Wallméniusd29a11b2012-05-08 16:34:26 +0200958#if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
Jeffrey Goode55064f72010-04-10 21:17:09 +0000959 /* preset pitch and speed to 100% in case bookmark doesn't have info */
960 bm.pitch = sound_get_pitch();
961 bm.speed = dsp_get_timestretch();
Jeffrey Goode4f3f7dd2010-04-10 21:41:01 +0000962#endif
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000963
Jeffrey Goode55064f72010-04-10 21:17:09 +0000964 if (parse_bookmark(bookmark, true))
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000965 {
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000966 global_settings.repeat_mode = bm.repeat_mode;
967 global_settings.playlist_shuffle = bm.shuffle;
Nils Wallméniusd29a11b2012-05-08 16:34:26 +0200968#if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
Jeffrey Goode55064f72010-04-10 21:17:09 +0000969 sound_set_pitch(bm.pitch);
970 dsp_set_timestretch(bm.speed);
Jeffrey Goode4f3f7dd2010-04-10 21:41:01 +0000971#endif
Bertrik Sikken1ca28a42010-09-26 21:13:25 +0000972 if (!warn_on_pl_erase())
973 return false;
Jeffrey Goode7209c5d2010-04-05 20:53:04 +0000974 return bookmark_play(global_temp_buffer, bm.resume_index,
Michael Sevakis31b71222013-07-14 07:59:39 -0400975 bm.resume_time, bm.resume_offset, bm.resume_seed, global_filename);
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000976 }
977
978 return false;
979}
Jörg Hohensohnf2d8b0e2004-04-12 12:29:44 +0000980
Magnus Holmgren375b7e02007-02-15 22:13:14 +0000981static const char* skip_token(const char* s)
982{
983 while (*s && *s != ';')
984 {
985 s++;
986 }
987
988 if (*s)
989 {
990 s++;
991 }
992
993 return s;
994}
995
Bertrik Sikkenab99e942011-05-08 20:35:29 +0000996static const char* int_token(const char* s, int* dest)
997{
Jeffrey Goodee2eff492010-04-10 21:53:55 +0000998 *dest = atoi(s);
Bertrik Sikkenab99e942011-05-08 20:35:29 +0000999 return skip_token(s);
1000}
Jeffrey Goodee2eff492010-04-10 21:53:55 +00001001
Bertrik Sikkenab99e942011-05-08 20:35:29 +00001002static const char* long_token(const char* s, long* dest)
1003{
Jeffrey Goodee2eff492010-04-10 21:53:55 +00001004 *dest = atoi(s); /* Should be atol, but we don't have it. */
Bertrik Sikkenab99e942011-05-08 20:35:29 +00001005 return skip_token(s);
1006}
Jeffrey Goodebec92322010-04-06 22:49:06 +00001007
Björn Stenberga108ec22004-01-14 00:13:04 +00001008/* ----------------------------------------------------------------------- */
1009/* This function takes a bookmark and parses it. This function also */
Jeffrey Goode55064f72010-04-10 21:17:09 +00001010/* validates the bookmark. The parse_filenames flag indicates whether */
1011/* the filename tokens are to be extracted. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001012/* Returns true on successful bookmark parse. */
Björn Stenberga108ec22004-01-14 00:13:04 +00001013/* ----------------------------------------------------------------------- */
Jeffrey Goode55064f72010-04-10 21:17:09 +00001014static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
Björn Stenberga108ec22004-01-14 00:13:04 +00001015{
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001016 const char* s = bookmark;
1017 const char* end;
1018
Jeffrey Goodee2eff492010-04-10 21:53:55 +00001019#define GET_INT_TOKEN(var) s = int_token(s, &var)
1020#define GET_LONG_TOKEN(var) s = long_token(s, &var)
Jeffrey Goodeb2ba1122010-04-06 21:49:33 +00001021#define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001022
Jeffrey Goode55064f72010-04-10 21:17:09 +00001023 /* if new format bookmark, extract the optional content flags,
1024 otherwise treat as an original format bookmark */
1025 int opt_flags = 0;
1026 bool new_format = (strchr(s, '>') == s);
1027 if (new_format)
1028 {
1029 s++;
1030 GET_INT_TOKEN(opt_flags);
1031 }
1032
1033 /* extract all original bookmark tokens */
Jeffrey Goodeb2ba1122010-04-06 21:49:33 +00001034 GET_INT_TOKEN(bm.resume_index);
Jeffrey Goodee2eff492010-04-10 21:53:55 +00001035 GET_LONG_TOKEN(bm.resume_offset);
Jeffrey Goodeb2ba1122010-04-06 21:49:33 +00001036 GET_INT_TOKEN(bm.resume_seed);
Jeffrey Goode55064f72010-04-10 21:17:09 +00001037 if (!new_format) /* skip deprecated token */
1038 s = skip_token(s);
Jeffrey Goodee2eff492010-04-10 21:53:55 +00001039 GET_LONG_TOKEN(bm.resume_time);
Jeffrey Goodeb2ba1122010-04-06 21:49:33 +00001040 GET_INT_TOKEN(bm.repeat_mode);
1041 GET_BOOL_TOKEN(bm.shuffle);
1042
Jeffrey Goode55064f72010-04-10 21:17:09 +00001043 /* extract all optional bookmark tokens */
1044 if (opt_flags & BM_PITCH)
1045 GET_INT_TOKEN(bm.pitch);
1046 if (opt_flags & BM_SPEED)
1047 GET_INT_TOKEN(bm.speed);
1048
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001049 if (*s == 0)
Björn Stenberga108ec22004-01-14 00:13:04 +00001050 {
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001051 return false;
Björn Stenberga108ec22004-01-14 00:13:04 +00001052 }
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001053
1054 end = strchr(s, ';');
Björn Stenberga108ec22004-01-14 00:13:04 +00001055
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001056 /* extract file names */
Jeffrey Goode55064f72010-04-10 21:17:09 +00001057 if (parse_filenames)
Björn Stenberga108ec22004-01-14 00:13:04 +00001058 {
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001059 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001060 len = MIN(TEMP_BUF_SIZE - 1, len);
1061 strlcpy(global_temp_buffer, s, len + 1);
1062
1063 if (end != NULL)
1064 {
1065 end++;
1066 strlcpy(global_filename, end, MAX_PATH);
1067 }
1068 }
Magnus Holmgren375b7e02007-02-15 22:13:14 +00001069
Björn Stenberga108ec22004-01-14 00:13:04 +00001070 return true;
1071}
1072
1073/* ----------------------------------------------------------------------- */
1074/* This function is used by multiple functions and is used to generate a */
1075/* bookmark named based off of the input. */
1076/* Changing this function could result in how the bookmarks are stored. */
1077/* it would be here that the centralized/decentralized bookmark code */
1078/* could be placed. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001079/* Always returns true */
Björn Stenberga108ec22004-01-14 00:13:04 +00001080/* ----------------------------------------------------------------------- */
Jens Arnold8fb33612004-08-18 01:09:31 +00001081static bool generate_bookmark_file_name(const char *in)
Björn Stenberga108ec22004-01-14 00:13:04 +00001082{
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001083 int len = strlen(in);
Björn Stenberga108ec22004-01-14 00:13:04 +00001084
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001085 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
Björn Stenberga108ec22004-01-14 00:13:04 +00001086 /* otherwise, name it based on the in variable */
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001087 if (!strcmp("/", in))
1088 strcpy(global_bookmark_file_name, "/root_dir.bmark");
Björn Stenberga108ec22004-01-14 00:13:04 +00001089 else
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001090 {
Magnus Holmgren30f38552009-03-08 13:57:10 +00001091#ifdef HAVE_MULTIVOLUME
1092 /* The "root" of an extra volume need special handling too. */
Michael Sevakis7d1a47c2013-08-05 22:02:45 -04001093 const char *filename;
1094 path_strip_volume(in, &filename, true);
1095 bool volume_root = *filename == '\0';
Magnus Holmgren30f38552009-03-08 13:57:10 +00001096#endif
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001097 strcpy(global_bookmark_file_name, in);
1098 if(global_bookmark_file_name[len-1] == '/')
1099 len--;
Magnus Holmgren30f38552009-03-08 13:57:10 +00001100#ifdef HAVE_MULTIVOLUME
1101 if (volume_root)
1102 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1103 else
1104#endif
1105 strcpy(&global_bookmark_file_name[len], ".bmark");
Linus Nielsen Feltzing09076312004-06-22 06:23:14 +00001106 }
Björn Stenberga108ec22004-01-14 00:13:04 +00001107
1108 return true;
1109}
1110
1111/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001112/* Returns true if a bookmark file exists for the current playlist. */
1113/* This is an interface function. */
Linus Nielsen Feltzing6e0436f2005-06-23 01:31:26 +00001114/* ----------------------------------------------------------------------- */
Alexander Levin7d4c0c52010-07-06 16:53:52 +00001115bool bookmark_exists(void)
Linus Nielsen Feltzing6e0436f2005-06-23 01:31:26 +00001116{
1117 bool exist=false;
1118
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001119 char* name = playlist_get_name(NULL, global_temp_buffer,
1120 sizeof(global_temp_buffer));
1121 if (generate_bookmark_file_name(name))
Linus Nielsen Feltzing6e0436f2005-06-23 01:31:26 +00001122 {
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001123 exist = file_exists(global_bookmark_file_name);
Linus Nielsen Feltzing6e0436f2005-06-23 01:31:26 +00001124 }
Linus Nielsen Feltzing6e0436f2005-06-23 01:31:26 +00001125 return exist;
1126}
1127
1128/* ----------------------------------------------------------------------- */
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001129/* Checks the current state of the system and returns true if it is in a */
Björn Stenberga108ec22004-01-14 00:13:04 +00001130/* bookmarkable state. */
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001131/* This is an interface funtion. */
Björn Stenberga108ec22004-01-14 00:13:04 +00001132/* ----------------------------------------------------------------------- */
Osborne Jacobsfb30d012012-03-11 00:47:28 -05001133bool bookmark_is_bookmarkable_state(void)
Björn Stenberga108ec22004-01-14 00:13:04 +00001134{
1135 int resume_index = 0;
Björn Stenberga108ec22004-01-14 00:13:04 +00001136
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001137 if (!(audio_status() && audio_current_track()) ||
Björn Stenberga108ec22004-01-14 00:13:04 +00001138 /* no track playing */
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001139 (playlist_get_resume_info(&resume_index) == -1) ||
1140 /* invalid queue info */
1141 (playlist_modified(NULL)))
Björn Stenberga108ec22004-01-14 00:13:04 +00001142 /* can't bookmark while in the queue */
Jeffrey Goode7209c5d2010-04-05 20:53:04 +00001143 {
Björn Stenberga108ec22004-01-14 00:13:04 +00001144 return false;
1145 }
1146
1147 return true;
1148}
1149