Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by wavey@wavey.org |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 22 | /* |
| 23 | Dynamic playlist design (based on design originally proposed by ricII) |
| 24 | |
| 25 | There are two files associated with a dynamic playlist: |
| 26 | 1. Playlist file : This file contains the initial songs in the playlist. |
| 27 | The file is created by the user and stored on the hard |
| 28 | drive. NOTE: If we are playing the contents of a |
| 29 | directory, there will be no playlist file. |
| 30 | 2. Control file : This file is automatically created when a playlist is |
| 31 | started and contains all the commands done to it. |
| 32 | |
| 33 | The first non-comment line in a control file must begin with |
| 34 | "P:VERSION:DIR:FILE" where VERSION is the playlist control file version, |
| 35 | DIR is the directory where the playlist is located and FILE is the |
| 36 | playlist filename. For dirplay, FILE will be empty. An empty playlist |
| 37 | will have both entries as null. |
| 38 | |
| 39 | Control file commands: |
| 40 | a. Add track (A:<position>:<last position>:<path to track>) |
| 41 | - Insert a track at the specified position in the current |
| 42 | playlist. Last position is used to specify where last insertion |
| 43 | occurred. |
| 44 | b. Queue track (Q:<position>:<last position>:<path to track>) |
| 45 | - Queue a track at the specified position in the current |
| 46 | playlist. Queued tracks differ from added tracks in that they |
| 47 | are deleted from the playlist as soon as they are played and |
| 48 | they are not saved to disk as part of the playlist. |
| 49 | c. Delete track (D:<position>) |
| 50 | - Delete track from specified position in the current playlist. |
| 51 | d. Shuffle playlist (S:<seed>:<index>) |
| 52 | - Shuffle entire playlist with specified seed. The index |
| 53 | identifies the first index in the newly shuffled playlist |
| 54 | (needed for repeat mode). |
| 55 | e. Unshuffle playlist (U:<index>) |
| 56 | - Unshuffle entire playlist. The index identifies the first index |
| 57 | in the newly unshuffled playlist. |
| 58 | f. Reset last insert position (R) |
| 59 | - Needed so that insertions work properly after resume |
| 60 | |
| 61 | Resume: |
| 62 | The only resume info that needs to be saved is the current index in the |
| 63 | playlist and the position in the track. When resuming, all the commands |
| 64 | in the control file will be reapplied so that the playlist indices are |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 65 | exactly the same as before shutdown. To avoid unnecessary disk |
| 66 | accesses, the shuffle mode settings are also saved in settings and only |
| 67 | flushed to disk when required. |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 68 | */ |
| 69 | |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 70 | #include <stdio.h> |
Thomas Martitz | 50a6ca3 | 2010-05-06 21:04:40 +0000 | [diff] [blame] | 71 | #include <stdlib.h> |
| 72 | #include <ctype.h> |
| 73 | #include "string-extra.h" |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 74 | #include "playlist.h" |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 75 | #include "ata_idle_notify.h" |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 76 | #include "file.h" |
Linus Nielsen Feltzing | 224c0a1 | 2006-08-15 12:27:07 +0000 | [diff] [blame] | 77 | #include "action.h" |
Thomas Martitz | 706e6b7 | 2014-02-18 07:11:11 +0100 | [diff] [blame] | 78 | #include "mv.h" |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 79 | #include "debug.h" |
Linus Nielsen Feltzing | 8a237a8 | 2005-04-04 12:06:29 +0000 | [diff] [blame] | 80 | #include "audio.h" |
Björn Stenberg | 4f00450 | 2002-06-13 15:16:41 +0000 | [diff] [blame] | 81 | #include "lcd.h" |
| 82 | #include "kernel.h" |
Björn Stenberg | 9fbdae5 | 2002-06-27 10:10:30 +0000 | [diff] [blame] | 83 | #include "settings.h" |
Markus Braun | de8fbf0 | 2002-08-07 10:35:26 +0000 | [diff] [blame] | 84 | #include "status.h" |
Linus Nielsen Feltzing | 5917e81 | 2002-08-15 12:28:52 +0000 | [diff] [blame] | 85 | #include "applimits.h" |
Robert Hak | f6ee2f6 | 2003-03-18 23:11:12 +0000 | [diff] [blame] | 86 | #include "screens.h" |
Thomas Martitz | d0b72e2 | 2011-08-30 14:01:33 +0000 | [diff] [blame] | 87 | #include "core_alloc.h" |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 88 | #include "misc.h" |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 89 | #include "pathfuncs.h" |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 90 | #include "button.h" |
Björn Stenberg | 8a5de5f | 2005-01-17 11:39:46 +0000 | [diff] [blame] | 91 | #include "filetree.h" |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 92 | #include "abrepeat.h" |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 93 | #include "thread.h" |
| 94 | #include "usb.h" |
Jonathan Gordon | 36a2e30 | 2007-04-18 13:03:01 +0000 | [diff] [blame] | 95 | #include "filetypes.h" |
Markus Braun | de8fbf0 | 2002-08-07 10:35:26 +0000 | [diff] [blame] | 96 | #ifdef HAVE_LCD_BITMAP |
Markus Braun | c41dcb9 | 2002-08-20 19:39:55 +0000 | [diff] [blame] | 97 | #include "icons.h" |
Markus Braun | c41dcb9 | 2002-08-20 19:39:55 +0000 | [diff] [blame] | 98 | #endif |
Bertrik Sikken | 66cf3a3 | 2009-03-08 16:10:40 +0000 | [diff] [blame] | 99 | #include "system.h" |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 100 | |
Björn Stenberg | 505eca7 | 2002-09-18 14:08:05 +0000 | [diff] [blame] | 101 | #include "lang.h" |
Jörg Hohensohn | 4f36ea8 | 2004-03-14 21:33:53 +0000 | [diff] [blame] | 102 | #include "talk.h" |
Kevin Ferrare | 3aa842c | 2005-11-06 17:30:53 +0000 | [diff] [blame] | 103 | #include "splash.h" |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 104 | #include "rbunicode.h" |
Stéphane Doyon | 6d08116 | 2007-10-12 04:20:20 +0000 | [diff] [blame] | 105 | #include "root_menu.h" |
Jonathan Gordon | d1fd4f0 | 2011-07-21 06:40:21 +0000 | [diff] [blame] | 106 | #include "plugin.h" /* To borrow a temp buffer to rewrite a .m3u8 file */ |
Thomas Martitz | 22e802e | 2013-05-30 11:24:16 +0200 | [diff] [blame] | 107 | #include "panic.h" |
Thomas Martitz | bebf71a | 2014-04-08 22:52:37 +0200 | [diff] [blame] | 108 | #include "logdiskf.h" |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 109 | #ifdef HAVE_DIRCACHE |
| 110 | #include "dircache.h" |
| 111 | #endif |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 112 | |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 113 | #define PLAYLIST_CONTROL_FILE_VERSION 2 |
Björn Stenberg | 728868a | 2003-03-07 14:38:51 +0000 | [diff] [blame] | 114 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 115 | /* |
| 116 | Each playlist index has a flag associated with it which identifies what |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 117 | type of track it is. These flags are stored in the 4 high order bits of |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 118 | the index. |
| 119 | |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 120 | NOTE: This limits the playlist file size to a max of 256M. |
Linus Nielsen Feltzing | d8cc285 | 2002-08-06 13:09:48 +0000 | [diff] [blame] | 121 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 122 | Bits 31-30: |
| 123 | 00 = Playlist track |
| 124 | 01 = Track was prepended into playlist |
| 125 | 10 = Track was inserted into playlist |
| 126 | 11 = Track was appended into playlist |
| 127 | Bit 29: |
| 128 | 0 = Added track |
| 129 | 1 = Queued track |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 130 | Bit 28: |
| 131 | 0 = Track entry is valid |
| 132 | 1 = Track does not exist on disk and should be skipped |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 133 | */ |
Miika Pekkarinen | 56bb45c | 2005-10-21 07:25:19 +0000 | [diff] [blame] | 134 | #define PLAYLIST_SEEK_MASK 0x0FFFFFFF |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 135 | #define PLAYLIST_INSERT_TYPE_MASK 0xC0000000 |
| 136 | #define PLAYLIST_QUEUE_MASK 0x20000000 |
| 137 | |
| 138 | #define PLAYLIST_INSERT_TYPE_PREPEND 0x40000000 |
| 139 | #define PLAYLIST_INSERT_TYPE_INSERT 0x80000000 |
| 140 | #define PLAYLIST_INSERT_TYPE_APPEND 0xC0000000 |
| 141 | |
| 142 | #define PLAYLIST_QUEUED 0x20000000 |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 143 | #define PLAYLIST_SKIPPED 0x10000000 |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 144 | |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 145 | struct directory_search_context { |
| 146 | struct playlist_info* playlist; |
| 147 | int position; |
| 148 | bool queue; |
| 149 | int count; |
| 150 | }; |
| 151 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 152 | static struct playlist_info current_playlist; |
Daniel Stenberg | 4d1fb48 | 2002-06-04 21:43:38 +0000 | [diff] [blame] | 153 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 154 | static void empty_playlist(struct playlist_info* playlist, bool resume); |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 155 | static void new_playlist(struct playlist_info* playlist, const char *dir, |
| 156 | const char *file); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 157 | static void create_control(struct playlist_info* playlist); |
| 158 | static int check_control(struct playlist_info* playlist); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 159 | static int recreate_control(struct playlist_info* playlist); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 160 | static void update_playlist_filename(struct playlist_info* playlist, |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 161 | const char *dir, const char *file); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 162 | static int add_indices_to_playlist(struct playlist_info* playlist, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 163 | char* buffer, size_t buflen); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 164 | static int add_track_to_playlist(struct playlist_info* playlist, |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 165 | const char *filename, int position, |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 166 | bool queue, int seek_pos); |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 167 | static int directory_search_callback(char* filename, void* context); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 168 | static int remove_track_from_playlist(struct playlist_info* playlist, |
| 169 | int position, bool write); |
| 170 | static int randomise_playlist(struct playlist_info* playlist, |
| 171 | unsigned int seed, bool start_current, |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 172 | bool write); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 173 | static int sort_playlist(struct playlist_info* playlist, bool start_current, |
| 174 | bool write); |
Hardeep Sidhu | 965c86a | 2005-06-25 06:28:55 +0000 | [diff] [blame] | 175 | static int get_next_index(const struct playlist_info* playlist, int steps, |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 176 | int repeat_mode); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 177 | static void find_and_set_playlist_index(struct playlist_info* playlist, |
| 178 | unsigned int seek); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 179 | static int compare(const void* p1, const void* p2); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 180 | static int get_filename(struct playlist_info* playlist, int index, int seek, |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 181 | bool control_file, char *buf, int buf_length); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 182 | static int get_next_directory(char *dir); |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 183 | static int get_next_dir(char *dir, bool is_forward); |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 184 | static int get_previous_directory(char *dir); |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 185 | static int check_subdir_for_music(char *dir, const char *subdir, bool recurse); |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 186 | static ssize_t format_track_path(char *dest, char *src, int buf_length, |
| 187 | const char *dir); |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 188 | static void display_playlist_count(int count, const unsigned char *fmt, |
| 189 | bool final); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 190 | static void display_buffer_full(void); |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 191 | static int flush_cached_control(struct playlist_info* playlist); |
| 192 | static int update_control(struct playlist_info* playlist, |
| 193 | enum playlist_command command, int i1, int i2, |
| 194 | const char* s1, const char* s2, void* data); |
| 195 | static void sync_control(struct playlist_info* playlist, bool force); |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 196 | static int rotate_index(const struct playlist_info* playlist, int index); |
Björn Stenberg | 7bb746b | 2003-04-24 17:31:36 +0000 | [diff] [blame] | 197 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 198 | #ifdef HAVE_DIRCACHE |
| 199 | #define PLAYLIST_LOAD_POINTERS 1 |
| 200 | |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 201 | static struct event_queue playlist_queue SHAREDBSS_ATTR; |
Miika Pekkarinen | cd605cf | 2006-11-30 19:42:41 +0000 | [diff] [blame] | 202 | static long playlist_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)]; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 203 | static const char playlist_thread_name[] = "playlist cachectrl"; |
| 204 | #endif |
| 205 | |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 206 | static struct mutex current_playlist_mutex SHAREDBSS_ATTR; |
| 207 | static struct mutex created_playlist_mutex SHAREDBSS_ATTR; |
| 208 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 209 | #ifdef HAVE_DIRCACHE |
| 210 | static void copy_filerefs(struct dircache_fileref *dcfto, |
| 211 | const struct dircache_fileref *dcffrom, |
| 212 | int count) |
| 213 | { |
| 214 | if (!dcfto) |
| 215 | return; |
| 216 | |
| 217 | if (dcffrom) |
| 218 | memmove(dcfto, dcffrom, count * sizeof (*dcfto)); |
| 219 | else |
| 220 | { |
| 221 | /* just initialize the destination */ |
| 222 | for (int i = 0; i < count; i++, dcfto++) |
| 223 | dircache_fileref_init(dcfto); |
| 224 | } |
| 225 | } |
| 226 | #endif /* HAVE_DIRCACHE */ |
| 227 | |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 228 | /* Check if the filename suggests M3U or M3U8 format. */ |
| 229 | static bool is_m3u8(const char* filename) |
| 230 | { |
| 231 | int len = strlen(filename); |
| 232 | |
| 233 | /* Default to M3U8 unless explicitly told otherwise. */ |
| 234 | return !(len > 4 && strcasecmp(&filename[len - 4], ".m3u") == 0); |
| 235 | } |
| 236 | |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 237 | |
| 238 | /* Convert a filename in an M3U playlist to UTF-8. |
| 239 | * |
| 240 | * buf - the filename to convert; can contain more than one line from the |
| 241 | * playlist. |
| 242 | * buf_len - amount of buf that is used. |
| 243 | * buf_max - total size of buf. |
| 244 | * temp - temporary conversion buffer, at least buf_max bytes. |
| 245 | * |
| 246 | * Returns the length of the converted filename. |
| 247 | */ |
| 248 | static int convert_m3u(char* buf, int buf_len, int buf_max, char* temp) |
| 249 | { |
| 250 | int i = 0; |
| 251 | char* dest; |
| 252 | |
| 253 | /* Locate EOL. */ |
Thomas Jarosch | f8d9e9c | 2014-12-20 13:09:22 +0100 | [diff] [blame] | 254 | while ((i < buf_len) && (buf[i] != '\n') && (buf[i] != '\r')) |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 255 | { |
| 256 | i++; |
| 257 | } |
| 258 | |
| 259 | /* Work back killing white space. */ |
| 260 | while ((i > 0) && isspace(buf[i - 1])) |
| 261 | { |
| 262 | i--; |
| 263 | } |
| 264 | |
| 265 | buf_len = i; |
| 266 | dest = temp; |
| 267 | |
| 268 | /* Convert char by char, so as to not overflow temp (iso_decode should |
| 269 | * preferably handle this). No more than 4 bytes should be generated for |
| 270 | * each input char. |
| 271 | */ |
| 272 | for (i = 0; i < buf_len && dest < (temp + buf_max - 4); i++) |
| 273 | { |
| 274 | dest = iso_decode(&buf[i], dest, -1, 1); |
| 275 | } |
| 276 | |
| 277 | *dest = 0; |
| 278 | strcpy(buf, temp); |
| 279 | return dest - temp; |
| 280 | } |
| 281 | |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 282 | /* |
| 283 | * remove any files and indices associated with the playlist |
| 284 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 285 | static void empty_playlist(struct playlist_info* playlist, bool resume) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 286 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 287 | playlist->filename[0] = '\0'; |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 288 | playlist->utf8 = true; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 289 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 290 | if(playlist->fd >= 0) |
Daniel Stenberg | 583821b | 2003-04-23 18:45:51 +0000 | [diff] [blame] | 291 | /* If there is an already open playlist, close it. */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 292 | close(playlist->fd); |
| 293 | playlist->fd = -1; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 294 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 295 | if(playlist->control_fd >= 0) |
| 296 | close(playlist->control_fd); |
| 297 | playlist->control_fd = -1; |
| 298 | playlist->control_created = false; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 299 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 300 | playlist->in_ram = false; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 301 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 302 | if (playlist->buffer) |
| 303 | playlist->buffer[0] = 0; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 304 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 305 | playlist->buffer_end_pos = 0; |
| 306 | |
| 307 | playlist->index = 0; |
| 308 | playlist->first_index = 0; |
| 309 | playlist->amount = 0; |
| 310 | playlist->last_insert_pos = -1; |
| 311 | playlist->seed = 0; |
| 312 | playlist->shuffle_modified = false; |
| 313 | playlist->deleted = false; |
| 314 | playlist->num_inserted_tracks = 0; |
Hardeep Sidhu | b79c9a6 | 2006-04-19 02:22:23 +0000 | [diff] [blame] | 315 | playlist->started = false; |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 316 | |
| 317 | playlist->num_cached = 0; |
| 318 | playlist->pending_control_sync = false; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 319 | |
| 320 | if (!resume && playlist->current) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 321 | { |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 322 | /* start with fresh playlist control file when starting new |
| 323 | playlist */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 324 | create_control(playlist); |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 328 | /* |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 329 | * Initialize a new playlist for viewing/editing/playing. dir is the |
| 330 | * directory where the playlist is located and file is the filename. |
| 331 | */ |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 332 | static void new_playlist(struct playlist_info* playlist, const char *dir, |
| 333 | const char *file) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 334 | { |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 335 | const char *fileused = file; |
| 336 | const char *dirused = dir; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 337 | empty_playlist(playlist, false); |
| 338 | |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 339 | if (!fileused) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 340 | { |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 341 | fileused = ""; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 342 | |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 343 | if (dirused && playlist->current) /* !current cannot be in_ram */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 344 | playlist->in_ram = true; |
| 345 | else |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 346 | dirused = ""; /* empty playlist */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 349 | update_playlist_filename(playlist, dirused, fileused); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 350 | |
| 351 | if (playlist->control_fd >= 0) |
| 352 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 353 | update_control(playlist, PLAYLIST_COMMAND_PLAYLIST, |
Steve Bavin | ad95df2 | 2008-05-12 17:52:50 +0000 | [diff] [blame] | 354 | PLAYLIST_CONTROL_FILE_VERSION, -1, dirused, fileused, NULL); |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 355 | sync_control(playlist, false); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * create control file for playlist |
| 361 | */ |
| 362 | static void create_control(struct playlist_info* playlist) |
| 363 | { |
Björn Stenberg | 30d8f61 | 2005-01-20 16:24:26 +0000 | [diff] [blame] | 364 | playlist->control_fd = open(playlist->control_filename, |
Thomas Martitz | 0a1d7c2 | 2010-05-06 17:35:13 +0000 | [diff] [blame] | 365 | O_CREAT|O_RDWR|O_TRUNC, 0666); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 366 | if (playlist->control_fd < 0) |
Hardeep Sidhu | 586ec82 | 2004-08-02 05:11:20 +0000 | [diff] [blame] | 367 | { |
Henrik Backe | 66b45ee | 2004-09-10 20:51:12 +0000 | [diff] [blame] | 368 | if (check_rockboxdir()) |
| 369 | { |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 370 | cond_talk_ids_fq(LANG_PLAYLIST_CONTROL_ACCESS_ERROR); |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 371 | splashf(HZ*2, (unsigned char *)"%s (%d)", |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 372 | str(LANG_PLAYLIST_CONTROL_ACCESS_ERROR), |
| 373 | playlist->control_fd); |
Henrik Backe | 66b45ee | 2004-09-10 20:51:12 +0000 | [diff] [blame] | 374 | } |
| 375 | playlist->control_created = false; |
Hardeep Sidhu | 586ec82 | 2004-08-02 05:11:20 +0000 | [diff] [blame] | 376 | } |
Henrik Backe | 66b45ee | 2004-09-10 20:51:12 +0000 | [diff] [blame] | 377 | else |
| 378 | { |
| 379 | playlist->control_created = true; |
| 380 | } |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * validate the control file. This may include creating/initializing it if |
| 385 | * necessary; |
| 386 | */ |
| 387 | static int check_control(struct playlist_info* playlist) |
| 388 | { |
| 389 | if (!playlist->control_created) |
| 390 | { |
| 391 | create_control(playlist); |
| 392 | |
| 393 | if (playlist->control_fd >= 0) |
| 394 | { |
| 395 | char* dir = playlist->filename; |
| 396 | char* file = playlist->filename+playlist->dirlen; |
| 397 | char c = playlist->filename[playlist->dirlen-1]; |
| 398 | |
| 399 | playlist->filename[playlist->dirlen-1] = '\0'; |
| 400 | |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 401 | update_control(playlist, PLAYLIST_COMMAND_PLAYLIST, |
| 402 | PLAYLIST_CONTROL_FILE_VERSION, -1, dir, file, NULL); |
| 403 | sync_control(playlist, false); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 404 | playlist->filename[playlist->dirlen-1] = c; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (playlist->control_fd < 0) |
| 409 | return -1; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 415 | * recreate the control file based on current playlist entries |
| 416 | */ |
| 417 | static int recreate_control(struct playlist_info* playlist) |
| 418 | { |
William Wilgus | 2fb6b2b | 2018-07-25 18:33:10 +0200 | [diff] [blame] | 419 | const char file_suffix[] = "_temp\0"; |
| 420 | char temp_file[MAX_PATH + sizeof(file_suffix)]; |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 421 | int temp_fd = -1; |
| 422 | int i; |
| 423 | int result = 0; |
| 424 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 425 | temp_file[0] = 0; |
| 426 | |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 427 | if(playlist->control_fd >= 0) |
| 428 | { |
| 429 | char* dir = playlist->filename; |
| 430 | char* file = playlist->filename+playlist->dirlen; |
| 431 | char c = playlist->filename[playlist->dirlen-1]; |
| 432 | |
| 433 | close(playlist->control_fd); |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 434 | playlist->control_fd = 0; |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 435 | |
William Wilgus | 2fb6b2b | 2018-07-25 18:33:10 +0200 | [diff] [blame] | 436 | snprintf(temp_file, sizeof(temp_file), "%s%s", |
| 437 | playlist->control_filename, file_suffix); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 438 | |
| 439 | if (rename(playlist->control_filename, temp_file) < 0) |
| 440 | return -1; |
| 441 | |
| 442 | temp_fd = open(temp_file, O_RDONLY); |
| 443 | if (temp_fd < 0) |
| 444 | return -1; |
| 445 | |
| 446 | playlist->control_fd = open(playlist->control_filename, |
Thomas Martitz | 0a1d7c2 | 2010-05-06 17:35:13 +0000 | [diff] [blame] | 447 | O_CREAT|O_RDWR|O_TRUNC, 0666); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 448 | if (playlist->control_fd < 0) |
Thomas Jarosch | 12ac381 | 2011-08-25 19:39:01 +0000 | [diff] [blame] | 449 | { |
| 450 | close(temp_fd); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 451 | return -1; |
Thomas Jarosch | 12ac381 | 2011-08-25 19:39:01 +0000 | [diff] [blame] | 452 | } |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 453 | |
| 454 | playlist->filename[playlist->dirlen-1] = '\0'; |
Thomas Jarosch | 12ac381 | 2011-08-25 19:39:01 +0000 | [diff] [blame] | 455 | |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 456 | /* cannot call update_control() because of mutex */ |
| 457 | result = fdprintf(playlist->control_fd, "P:%d:%s:%s\n", |
| 458 | PLAYLIST_CONTROL_FILE_VERSION, dir, file); |
| 459 | |
| 460 | playlist->filename[playlist->dirlen-1] = c; |
| 461 | |
| 462 | if (result < 0) |
| 463 | { |
| 464 | close(temp_fd); |
| 465 | return result; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | playlist->seed = 0; |
| 470 | playlist->shuffle_modified = false; |
| 471 | playlist->deleted = false; |
| 472 | playlist->num_inserted_tracks = 0; |
| 473 | |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 474 | for (i=0; i<playlist->amount; i++) |
| 475 | { |
| 476 | if (playlist->indices[i] & PLAYLIST_INSERT_TYPE_MASK) |
| 477 | { |
| 478 | bool queue = playlist->indices[i] & PLAYLIST_QUEUE_MASK; |
| 479 | char inserted_file[MAX_PATH+1]; |
| 480 | |
| 481 | lseek(temp_fd, playlist->indices[i] & PLAYLIST_SEEK_MASK, |
| 482 | SEEK_SET); |
| 483 | read_line(temp_fd, inserted_file, sizeof(inserted_file)); |
| 484 | |
| 485 | result = fdprintf(playlist->control_fd, "%c:%d:%d:", |
| 486 | queue?'Q':'A', i, playlist->last_insert_pos); |
| 487 | if (result > 0) |
| 488 | { |
| 489 | /* save the position in file where name is written */ |
| 490 | int seek_pos = lseek(playlist->control_fd, 0, SEEK_CUR); |
| 491 | |
| 492 | result = fdprintf(playlist->control_fd, "%s\n", |
| 493 | inserted_file); |
| 494 | |
| 495 | playlist->indices[i] = |
| 496 | (playlist->indices[i] & ~PLAYLIST_SEEK_MASK) | seek_pos; |
| 497 | } |
| 498 | |
| 499 | if (result < 0) |
| 500 | break; |
| 501 | |
| 502 | playlist->num_inserted_tracks++; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | close(temp_fd); |
| 507 | remove(temp_file); |
| 508 | fsync(playlist->control_fd); |
| 509 | |
| 510 | if (result < 0) |
| 511 | return result; |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 517 | * store directory and name of playlist file |
| 518 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 519 | static void update_playlist_filename(struct playlist_info* playlist, |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 520 | const char *dir, const char *file) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 521 | { |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 522 | char *sep=""; |
| 523 | int dirlen = strlen(dir); |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 524 | |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 525 | playlist->utf8 = is_m3u8(file); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 526 | |
| 527 | /* If the dir does not end in trailing slash, we use a separator. |
| 528 | Otherwise we don't. */ |
| 529 | if('/' != dir[dirlen-1]) |
| 530 | { |
| 531 | sep="/"; |
| 532 | dirlen++; |
| 533 | } |
Jens Arnold | ac9ad1a | 2004-09-01 21:34:20 +0000 | [diff] [blame] | 534 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 535 | playlist->dirlen = dirlen; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 536 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 537 | snprintf(playlist->filename, sizeof(playlist->filename), |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 538 | "%s%s%s", dir, sep, file); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* |
| 542 | * calculate track offsets within a playlist file |
| 543 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 544 | static int add_indices_to_playlist(struct playlist_info* playlist, |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 545 | char* buffer, size_t buflen) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 546 | { |
| 547 | unsigned int nread; |
| 548 | unsigned int i = 0; |
| 549 | unsigned int count = 0; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 550 | bool store_index; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 551 | unsigned char *p; |
Hardeep Sidhu | 6dd728c | 2006-04-03 14:25:47 +0000 | [diff] [blame] | 552 | int result = 0; |
Thomas Martitz | bebf71a | 2014-04-08 22:52:37 +0200 | [diff] [blame] | 553 | /* get emergency buffer so we don't fail horribly */ |
| 554 | if (!buflen) |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 555 | buffer = alloca((buflen = 64)); |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 556 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 557 | if(-1 == playlist->fd) |
Dominik Riebeling | 02103a2 | 2008-08-02 20:39:03 +0000 | [diff] [blame] | 558 | playlist->fd = open_utf8(playlist->filename, O_RDONLY); |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 559 | if(playlist->fd < 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 560 | return -1; /* failure */ |
Dominik Riebeling | e299216 | 2008-08-09 11:43:56 +0000 | [diff] [blame] | 561 | if((i = lseek(playlist->fd, 0, SEEK_CUR)) > 0) |
Dominik Riebeling | 02103a2 | 2008-08-02 20:39:03 +0000 | [diff] [blame] | 562 | playlist->utf8 = true; /* Override any earlier indication. */ |
Dominik Riebeling | e299216 | 2008-08-09 11:43:56 +0000 | [diff] [blame] | 563 | |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 564 | splash(0, ID2P(LANG_WAIT)); |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 565 | |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 566 | store_index = true; |
| 567 | |
| 568 | while(1) |
| 569 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 570 | nread = read(playlist->fd, buffer, buflen); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 571 | /* Terminate on EOF */ |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 572 | if(nread <= 0) |
| 573 | break; |
Dominik Riebeling | e299216 | 2008-08-09 11:43:56 +0000 | [diff] [blame] | 574 | |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 575 | p = (unsigned char *)buffer; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 576 | |
| 577 | for(count=0; count < nread; count++,p++) { |
| 578 | |
| 579 | /* Are we on a new line? */ |
| 580 | if((*p == '\n') || (*p == '\r')) |
| 581 | { |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 582 | store_index = true; |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 583 | } |
| 584 | else if(store_index) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 585 | { |
| 586 | store_index = false; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 587 | |
| 588 | if(*p != '#') |
| 589 | { |
Hardeep Sidhu | 6dd728c | 2006-04-03 14:25:47 +0000 | [diff] [blame] | 590 | if ( playlist->amount >= playlist->max_playlist_size ) { |
| 591 | display_buffer_full(); |
| 592 | result = -1; |
| 593 | goto exit; |
| 594 | } |
| 595 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 596 | /* Store a new entry */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 597 | playlist->indices[ playlist->amount ] = i+count; |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 598 | #ifdef HAVE_DIRCACHE |
| 599 | copy_filerefs(&playlist->dcfrefs[playlist->amount], NULL, 1); |
| 600 | #endif |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 601 | playlist->amount++; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 602 | } |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 605 | |
| 606 | i+= count; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 607 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 608 | |
Hardeep Sidhu | 6dd728c | 2006-04-03 14:25:47 +0000 | [diff] [blame] | 609 | exit: |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 610 | #ifdef HAVE_DIRCACHE |
| 611 | queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0); |
| 612 | #endif |
| 613 | |
Hardeep Sidhu | 6dd728c | 2006-04-03 14:25:47 +0000 | [diff] [blame] | 614 | return result; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 617 | /* |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 618 | * Utility function to create a new playlist, fill it with the next or |
| 619 | * previous directory, shuffle it if needed, and start playback. |
| 620 | * If play_last is true and direction zero or negative, start playing |
| 621 | * the last file in the directory, otherwise start playing the first. |
| 622 | */ |
| 623 | static int create_and_play_dir(int direction, bool play_last) |
| 624 | { |
| 625 | char dir[MAX_PATH + 1]; |
| 626 | int res; |
| 627 | int index = -1; |
| 628 | |
| 629 | if(direction > 0) |
| 630 | res = get_next_directory(dir); |
| 631 | else |
| 632 | res = get_previous_directory(dir); |
| 633 | |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 634 | if (res < 0) |
| 635 | /* return the error encountered */ |
| 636 | return res; |
| 637 | |
| 638 | if (playlist_create(dir, NULL) != -1) |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 639 | { |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 640 | ft_build_playlist(tree_get_context(), 0); |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 641 | |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 642 | if (global_settings.playlist_shuffle) |
| 643 | playlist_shuffle(current_tick, -1); |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 644 | |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 645 | if (play_last && direction <= 0) |
| 646 | index = current_playlist.amount - 1; |
| 647 | else |
| 648 | index = 0; |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 649 | |
Michael Sevakis | d5849e0 | 2011-04-09 13:07:08 +0000 | [diff] [blame] | 650 | #if (CONFIG_CODEC == SWCODEC) |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 651 | current_playlist.started = true; |
Michael Sevakis | d5849e0 | 2011-04-09 13:07:08 +0000 | [diff] [blame] | 652 | #else |
Michael Sevakis | 31b7122 | 2013-07-14 07:59:39 -0400 | [diff] [blame] | 653 | playlist_start(index, 0, 0); |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 654 | #endif |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 655 | } |
Nicolas Pennequin | ca243ce | 2008-01-09 20:37:36 +0000 | [diff] [blame] | 656 | |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 657 | /* we've overwritten the dircache when getting the next/previous dir, |
| 658 | so the tree browser context will need to be reloaded */ |
| 659 | reload_directory(); |
| 660 | |
Magnus Holmgren | ccbe242 | 2007-10-25 19:18:20 +0000 | [diff] [blame] | 661 | return index; |
| 662 | } |
| 663 | |
| 664 | /* |
Jonathan Gordon | be3f29c | 2006-12-26 13:31:04 +0000 | [diff] [blame] | 665 | * Removes all tracks, from the playlist, leaving the presently playing |
| 666 | * track queued. |
| 667 | */ |
Bertrik Sikken | 3e98eb2 | 2008-04-20 11:19:50 +0000 | [diff] [blame] | 668 | int playlist_remove_all_tracks(struct playlist_info *playlist) |
Jonathan Gordon | be3f29c | 2006-12-26 13:31:04 +0000 | [diff] [blame] | 669 | { |
| 670 | int result; |
| 671 | |
| 672 | if (playlist == NULL) |
| 673 | playlist = ¤t_playlist; |
| 674 | |
| 675 | while (playlist->index > 0) |
| 676 | if ((result = remove_track_from_playlist(playlist, 0, true)) < 0) |
| 677 | return result; |
| 678 | |
| 679 | while (playlist->amount > 1) |
| 680 | if ((result = remove_track_from_playlist(playlist, 1, true)) < 0) |
| 681 | return result; |
| 682 | |
| 683 | if (playlist->amount == 1) { |
| 684 | playlist->indices[0] |= PLAYLIST_QUEUED; |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | /* |
Dave Hooper | 494fd96 | 2009-10-28 22:27:38 +0000 | [diff] [blame] | 692 | * Add track to playlist at specified position. There are seven special |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 693 | * positions that can be specified: |
Dave Hooper | 494fd96 | 2009-10-28 22:27:38 +0000 | [diff] [blame] | 694 | * PLAYLIST_PREPEND - Add track at beginning of playlist |
| 695 | * PLAYLIST_INSERT - Add track after current song. NOTE: If |
| 696 | * there are already inserted tracks then track |
| 697 | * is added to the end of the insertion list |
| 698 | * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no |
| 699 | * matter what other tracks have been inserted |
| 700 | * PLAYLIST_INSERT_LAST - Add track to end of playlist |
| 701 | * PLAYLIST_INSERT_SHUFFLED - Add track at some random point between the |
| 702 | * current playing track and end of playlist |
| 703 | * PLAYLIST_INSERT_LAST_SHUFFLED - Add tracks in random order to the end of |
| 704 | * the playlist. |
| 705 | * PLAYLIST_REPLACE - Erase current playlist, Cue the current track |
| 706 | * and inster this track at the end. |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 707 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 708 | static int add_track_to_playlist(struct playlist_info* playlist, |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 709 | const char *filename, int position, |
| 710 | bool queue, int seek_pos) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 711 | { |
Hardeep Sidhu | 10ad0a2 | 2006-09-18 20:47:27 +0000 | [diff] [blame] | 712 | int insert_position, orig_position; |
Jean-Philippe Bernardy | 5203f06 | 2005-02-12 12:57:16 +0000 | [diff] [blame] | 713 | unsigned long flags = PLAYLIST_INSERT_TYPE_INSERT; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 714 | int i; |
| 715 | |
Hardeep Sidhu | 10ad0a2 | 2006-09-18 20:47:27 +0000 | [diff] [blame] | 716 | insert_position = orig_position = position; |
| 717 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 718 | if (playlist->amount >= playlist->max_playlist_size) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 719 | { |
| 720 | display_buffer_full(); |
| 721 | return -1; |
| 722 | } |
| 723 | |
| 724 | switch (position) |
| 725 | { |
| 726 | case PLAYLIST_PREPEND: |
Hardeep Sidhu | 685356c | 2006-06-12 23:06:51 +0000 | [diff] [blame] | 727 | position = insert_position = playlist->first_index; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 728 | break; |
| 729 | case PLAYLIST_INSERT: |
| 730 | /* if there are already inserted tracks then add track to end of |
| 731 | insertion list else add after current playing track */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 732 | if (playlist->last_insert_pos >= 0 && |
| 733 | playlist->last_insert_pos < playlist->amount && |
| 734 | (playlist->indices[playlist->last_insert_pos]& |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 735 | PLAYLIST_INSERT_TYPE_MASK) == PLAYLIST_INSERT_TYPE_INSERT) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 736 | position = insert_position = playlist->last_insert_pos+1; |
| 737 | else if (playlist->amount > 0) |
| 738 | position = insert_position = playlist->index + 1; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 739 | else |
| 740 | position = insert_position = 0; |
| 741 | |
Thomas Martitz | 87d1744 | 2009-03-08 17:18:18 +0000 | [diff] [blame] | 742 | playlist->last_insert_pos = position; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 743 | break; |
| 744 | case PLAYLIST_INSERT_FIRST: |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 745 | if (playlist->amount > 0) |
| 746 | position = insert_position = playlist->index + 1; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 747 | else |
| 748 | position = insert_position = 0; |
| 749 | |
Thomas Martitz | 4ae7415 | 2009-03-08 19:03:53 +0000 | [diff] [blame] | 750 | playlist->last_insert_pos = position; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 751 | break; |
| 752 | case PLAYLIST_INSERT_LAST: |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 753 | if (playlist->first_index > 0) |
Hardeep Sidhu | 685356c | 2006-06-12 23:06:51 +0000 | [diff] [blame] | 754 | position = insert_position = playlist->first_index; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 755 | else |
Hardeep Sidhu | 685356c | 2006-06-12 23:06:51 +0000 | [diff] [blame] | 756 | position = insert_position = playlist->amount; |
Thomas Martitz | 4ae7415 | 2009-03-08 19:03:53 +0000 | [diff] [blame] | 757 | |
| 758 | playlist->last_insert_pos = position; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 759 | break; |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 760 | case PLAYLIST_INSERT_SHUFFLED: |
| 761 | { |
Hardeep Sidhu | b79c9a6 | 2006-04-19 02:22:23 +0000 | [diff] [blame] | 762 | if (playlist->started) |
Hardeep Sidhu | 095ad1a | 2006-04-18 16:44:51 +0000 | [diff] [blame] | 763 | { |
| 764 | int offset; |
| 765 | int n = playlist->amount - |
| 766 | rotate_index(playlist, playlist->index); |
| 767 | |
| 768 | if (n > 0) |
| 769 | offset = rand() % n; |
| 770 | else |
| 771 | offset = 0; |
| 772 | |
| 773 | position = playlist->index + offset + 1; |
| 774 | if (position >= playlist->amount) |
| 775 | position -= playlist->amount; |
| 776 | |
| 777 | insert_position = position; |
| 778 | } |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 779 | else |
Hardeep Sidhu | b79c9a6 | 2006-04-19 02:22:23 +0000 | [diff] [blame] | 780 | position = insert_position = (rand() % (playlist->amount+1)); |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
Dave Hooper | 494fd96 | 2009-10-28 22:27:38 +0000 | [diff] [blame] | 783 | case PLAYLIST_INSERT_LAST_SHUFFLED: |
| 784 | { |
| 785 | position = insert_position = playlist->last_shuffled_start + |
| 786 | rand() % (playlist->amount - playlist->last_shuffled_start + 1); |
| 787 | break; |
| 788 | } |
Jonathan Gordon | be3f29c | 2006-12-26 13:31:04 +0000 | [diff] [blame] | 789 | case PLAYLIST_REPLACE: |
Bertrik Sikken | 3e98eb2 | 2008-04-20 11:19:50 +0000 | [diff] [blame] | 790 | if (playlist_remove_all_tracks(playlist) < 0) |
Jonathan Gordon | be3f29c | 2006-12-26 13:31:04 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | |
Thomas Martitz | 4ae7415 | 2009-03-08 19:03:53 +0000 | [diff] [blame] | 793 | playlist->last_insert_pos = position = insert_position = playlist->index + 1; |
Jonathan Gordon | be3f29c | 2006-12-26 13:31:04 +0000 | [diff] [blame] | 794 | break; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | if (queue) |
| 798 | flags |= PLAYLIST_QUEUED; |
| 799 | |
| 800 | /* shift indices so that track can be added */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 801 | for (i=playlist->amount; i>insert_position; i--) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 802 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 803 | playlist->indices[i] = playlist->indices[i-1]; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 804 | #ifdef HAVE_DIRCACHE |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 805 | if (playlist->dcfrefs) |
| 806 | playlist->dcfrefs[i] = playlist->dcfrefs[i-1]; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 807 | #endif |
| 808 | } |
| 809 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 810 | /* update stored indices if needed */ |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 811 | |
Jonathan Gordon | d0729ab | 2010-01-21 07:28:37 +0000 | [diff] [blame] | 812 | if (orig_position < 0) |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 813 | { |
Jonathan Gordon | d0729ab | 2010-01-21 07:28:37 +0000 | [diff] [blame] | 814 | if (playlist->amount > 0 && insert_position <= playlist->index && |
| 815 | playlist->started) |
| 816 | playlist->index++; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 817 | |
Jonathan Gordon | d0729ab | 2010-01-21 07:28:37 +0000 | [diff] [blame] | 818 | if (playlist->amount > 0 && insert_position <= playlist->first_index && |
| 819 | orig_position != PLAYLIST_PREPEND && playlist->started) |
| 820 | playlist->first_index++; |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 823 | if (insert_position < playlist->last_insert_pos || |
| 824 | (insert_position == playlist->last_insert_pos && position < 0)) |
| 825 | playlist->last_insert_pos++; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 826 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 827 | if (seek_pos < 0 && playlist->control_fd >= 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 828 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 829 | int result = update_control(playlist, |
| 830 | (queue?PLAYLIST_COMMAND_QUEUE:PLAYLIST_COMMAND_ADD), position, |
| 831 | playlist->last_insert_pos, filename, NULL, &seek_pos); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 832 | |
| 833 | if (result < 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 834 | return result; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 837 | playlist->indices[insert_position] = flags | seek_pos; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 838 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 839 | #ifdef HAVE_DIRCACHE |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 840 | copy_filerefs(&playlist->dcfrefs[insert_position], NULL, 1); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 841 | #endif |
| 842 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 843 | playlist->amount++; |
| 844 | playlist->num_inserted_tracks++; |
Andree Buschmann | 4df825b | 2011-04-07 20:33:00 +0000 | [diff] [blame] | 845 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 846 | return insert_position; |
| 847 | } |
| 848 | |
| 849 | /* |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 850 | * Callback for playlist_directory_tracksearch to insert track into |
| 851 | * playlist. |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 852 | */ |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 853 | static int directory_search_callback(char* filename, void* context) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 854 | { |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 855 | struct directory_search_context* c = |
| 856 | (struct directory_search_context*) context; |
| 857 | int insert_pos; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 858 | |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 859 | insert_pos = add_track_to_playlist(c->playlist, filename, c->position, |
| 860 | c->queue, -1); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 861 | |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 862 | if (insert_pos < 0) |
Björn Stenberg | 3c10fd5 | 2005-02-04 13:20:25 +0000 | [diff] [blame] | 863 | return -1; |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 864 | |
| 865 | (c->count)++; |
| 866 | |
| 867 | /* Make sure tracks are inserted in correct order if user requests |
| 868 | INSERT_FIRST */ |
| 869 | if (c->position == PLAYLIST_INSERT_FIRST || c->position >= 0) |
| 870 | c->position = insert_pos + 1; |
| 871 | |
| 872 | if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 873 | { |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 874 | unsigned char* count_str; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 875 | |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 876 | if (c->queue) |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 877 | count_str = ID2P(LANG_PLAYLIST_QUEUE_COUNT); |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 878 | else |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 879 | count_str = ID2P(LANG_PLAYLIST_INSERT_COUNT); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 880 | |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 881 | display_playlist_count(c->count, count_str, false); |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 882 | |
| 883 | if ((c->count) == PLAYLIST_DISPLAY_COUNT && |
| 884 | (audio_status() & AUDIO_STATUS_PLAY) && |
| 885 | c->playlist->started) |
| 886 | audio_flush_and_reload_tracks(); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Linus Nielsen Feltzing | da0525f | 2006-07-18 13:54:12 +0000 | [diff] [blame] | 889 | return 0; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* |
| 893 | * remove track at specified position |
| 894 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 895 | static int remove_track_from_playlist(struct playlist_info* playlist, |
| 896 | int position, bool write) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 897 | { |
| 898 | int i; |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 899 | bool inserted; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 900 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 901 | if (playlist->amount <= 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 902 | return -1; |
| 903 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 904 | inserted = playlist->indices[position] & PLAYLIST_INSERT_TYPE_MASK; |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 905 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 906 | /* shift indices now that track has been removed */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 907 | for (i=position; i<playlist->amount; i++) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 908 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 909 | playlist->indices[i] = playlist->indices[i+1]; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 910 | #ifdef HAVE_DIRCACHE |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 911 | if (playlist->dcfrefs) |
| 912 | playlist->dcfrefs[i] = playlist->dcfrefs[i+1]; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 913 | #endif |
| 914 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 915 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 916 | playlist->amount--; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 917 | |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 918 | if (inserted) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 919 | playlist->num_inserted_tracks--; |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 920 | else |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 921 | playlist->deleted = true; |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 922 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 923 | /* update stored indices if needed */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 924 | if (position < playlist->index) |
| 925 | playlist->index--; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 926 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 927 | if (position < playlist->first_index) |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 928 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 929 | playlist->first_index--; |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 932 | if (position <= playlist->last_insert_pos) |
| 933 | playlist->last_insert_pos--; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 934 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 935 | if (write && playlist->control_fd >= 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 936 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 937 | int result = update_control(playlist, PLAYLIST_COMMAND_DELETE, |
| 938 | position, -1, NULL, NULL, NULL); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 939 | |
| 940 | if (result < 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 941 | return result; |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 942 | |
| 943 | sync_control(playlist, false); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 944 | } |
Andree Buschmann | 4df825b | 2011-04-07 20:33:00 +0000 | [diff] [blame] | 945 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * randomly rearrange the array of indices for the playlist. If start_current |
| 951 | * is true then update the index to the new index of the current playing track |
| 952 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 953 | static int randomise_playlist(struct playlist_info* playlist, |
| 954 | unsigned int seed, bool start_current, |
| 955 | bool write) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 956 | { |
| 957 | int count; |
| 958 | int candidate; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 959 | unsigned int current = playlist->indices[playlist->index]; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 960 | |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 961 | /* seed 0 is used to identify sorted playlist for resume purposes */ |
| 962 | if (seed == 0) |
| 963 | seed = 1; |
| 964 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 965 | /* seed with the given seed */ |
| 966 | srand(seed); |
| 967 | |
| 968 | /* randomise entire indices list */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 969 | for(count = playlist->amount - 1; count >= 0; count--) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 970 | { |
| 971 | /* the rand is from 0 to RAND_MAX, so adjust to our value range */ |
Linus Nielsen Feltzing | 1c9ab1b | 2004-11-06 23:37:21 +0000 | [diff] [blame] | 972 | candidate = rand() % (count + 1); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 973 | |
| 974 | /* now swap the values at the 'count' and 'candidate' positions */ |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 975 | int indextmp = playlist->indices[candidate]; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 976 | playlist->indices[candidate] = playlist->indices[count]; |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 977 | playlist->indices[count] = indextmp; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 978 | #ifdef HAVE_DIRCACHE |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 979 | if (playlist->dcfrefs) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 980 | { |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 981 | struct dircache_fileref dcftmp = playlist->dcfrefs[candidate]; |
| 982 | playlist->dcfrefs[candidate] = playlist->dcfrefs[count]; |
| 983 | playlist->dcfrefs[count] = dcftmp; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 984 | } |
| 985 | #endif |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | if (start_current) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 989 | find_and_set_playlist_index(playlist, current); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 990 | |
| 991 | /* indices have been moved so last insert position is no longer valid */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 992 | playlist->last_insert_pos = -1; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 993 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 994 | playlist->seed = seed; |
| 995 | if (playlist->num_inserted_tracks > 0 || playlist->deleted) |
| 996 | playlist->shuffle_modified = true; |
Björn Stenberg | a108ec2 | 2004-01-14 00:13:04 +0000 | [diff] [blame] | 997 | |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 998 | if (write) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 999 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1000 | update_control(playlist, PLAYLIST_COMMAND_SHUFFLE, seed, |
| 1001 | playlist->first_index, NULL, NULL, NULL); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1002 | } |
Andree Buschmann | 4df825b | 2011-04-07 20:33:00 +0000 | [diff] [blame] | 1003 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Sort the array of indices for the playlist. If start_current is true then |
| 1009 | * set the index to the new index of the current song. |
Jonathan Gordon | a70d602 | 2010-02-05 23:33:31 +0000 | [diff] [blame] | 1010 | * Also while going to unshuffled mode set the first_index to 0. |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1011 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1012 | static int sort_playlist(struct playlist_info* playlist, bool start_current, |
| 1013 | bool write) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1014 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1015 | unsigned int current = playlist->indices[playlist->index]; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1016 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1017 | if (playlist->amount > 0) |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1018 | qsort((void*)playlist->indices, playlist->amount, |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1019 | sizeof(playlist->indices[0]), compare); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1020 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1021 | #ifdef HAVE_DIRCACHE |
| 1022 | /** We need to re-check the song names from disk because qsort can't |
| 1023 | * sort two arrays at once :/ |
| 1024 | * FIXME: Please implement a better way to do this. */ |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1025 | copy_filerefs(playlist->dcfrefs, NULL, playlist->max_playlist_size); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1026 | queue_post(&playlist_queue, PLAYLIST_LOAD_POINTERS, 0); |
| 1027 | #endif |
| 1028 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1029 | if (start_current) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1030 | find_and_set_playlist_index(playlist, current); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* indices have been moved so last insert position is no longer valid */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1033 | playlist->last_insert_pos = -1; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1034 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1035 | if (!playlist->num_inserted_tracks && !playlist->deleted) |
| 1036 | playlist->shuffle_modified = false; |
| 1037 | if (write && playlist->control_fd >= 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1038 | { |
Jonathan Gordon | a70d602 | 2010-02-05 23:33:31 +0000 | [diff] [blame] | 1039 | playlist->first_index = 0; |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1040 | update_control(playlist, PLAYLIST_COMMAND_UNSHUFFLE, |
| 1041 | playlist->first_index, -1, NULL, NULL, NULL); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1042 | } |
Andree Buschmann | 4df825b | 2011-04-07 20:33:00 +0000 | [diff] [blame] | 1043 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
| 1046 | |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1047 | /* Calculate how many steps we have to really step when skipping entries |
| 1048 | * marked as bad. |
| 1049 | */ |
| 1050 | static int calculate_step_count(const struct playlist_info *playlist, int steps) |
| 1051 | { |
| 1052 | int i, count, direction; |
| 1053 | int index; |
| 1054 | int stepped_count = 0; |
| 1055 | |
| 1056 | if (steps < 0) |
Miika Pekkarinen | caee4b3 | 2005-11-21 13:23:06 +0000 | [diff] [blame] | 1057 | { |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1058 | direction = -1; |
Miika Pekkarinen | caee4b3 | 2005-11-21 13:23:06 +0000 | [diff] [blame] | 1059 | count = -steps; |
| 1060 | } |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1061 | else |
Miika Pekkarinen | caee4b3 | 2005-11-21 13:23:06 +0000 | [diff] [blame] | 1062 | { |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1063 | direction = 1; |
Miika Pekkarinen | caee4b3 | 2005-11-21 13:23:06 +0000 | [diff] [blame] | 1064 | count = steps; |
| 1065 | } |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1066 | |
| 1067 | index = playlist->index; |
| 1068 | i = 0; |
Miika Pekkarinen | d1704f6 | 2005-11-21 11:14:51 +0000 | [diff] [blame] | 1069 | do { |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1070 | /* Boundary check */ |
| 1071 | if (index < 0) |
| 1072 | index += playlist->amount; |
| 1073 | if (index >= playlist->amount) |
| 1074 | index -= playlist->amount; |
| 1075 | |
| 1076 | /* Check if we found a bad entry. */ |
| 1077 | if (playlist->indices[index] & PLAYLIST_SKIPPED) |
| 1078 | { |
| 1079 | steps += direction; |
| 1080 | /* Are all entries bad? */ |
| 1081 | if (stepped_count++ > playlist->amount) |
| 1082 | break ; |
| 1083 | } |
| 1084 | else |
| 1085 | i++; |
Miika Pekkarinen | d1704f6 | 2005-11-21 11:14:51 +0000 | [diff] [blame] | 1086 | |
| 1087 | index += direction; |
Miika Pekkarinen | 4394cc7 | 2005-11-21 11:31:15 +0000 | [diff] [blame] | 1088 | } while (i <= count); |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1089 | |
| 1090 | return steps; |
| 1091 | } |
| 1092 | |
Boris Gjenero | bda8a96 | 2011-12-15 20:58:14 +0000 | [diff] [blame] | 1093 | #if CONFIG_CODEC == SWCODEC |
Miika Pekkarinen | 6a4bfb5 | 2005-12-01 18:44:11 +0000 | [diff] [blame] | 1094 | /* Marks the index of the track to be skipped that is "steps" away from |
| 1095 | * current playing track. |
| 1096 | */ |
| 1097 | void playlist_skip_entry(struct playlist_info *playlist, int steps) |
| 1098 | { |
| 1099 | int index; |
| 1100 | |
| 1101 | if (playlist == NULL) |
| 1102 | playlist = ¤t_playlist; |
| 1103 | |
Hardeep Sidhu | ed41ba0 | 2006-05-05 08:32:07 +0000 | [diff] [blame] | 1104 | /* need to account for already skipped tracks */ |
| 1105 | steps = calculate_step_count(playlist, steps); |
| 1106 | |
| 1107 | index = playlist->index + steps; |
| 1108 | if (index < 0) |
| 1109 | index += playlist->amount; |
| 1110 | else if (index >= playlist->amount) |
| 1111 | index -= playlist->amount; |
| 1112 | |
Miika Pekkarinen | 6a4bfb5 | 2005-12-01 18:44:11 +0000 | [diff] [blame] | 1113 | playlist->indices[index] |= PLAYLIST_SKIPPED; |
| 1114 | } |
Boris Gjenero | bda8a96 | 2011-12-15 20:58:14 +0000 | [diff] [blame] | 1115 | #endif /* CONFIG_CODEC == SWCODEC */ |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1116 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1117 | /* |
| 1118 | * returns the index of the track that is "steps" away from current playing |
| 1119 | * track. |
| 1120 | */ |
Hardeep Sidhu | 965c86a | 2005-06-25 06:28:55 +0000 | [diff] [blame] | 1121 | static int get_next_index(const struct playlist_info* playlist, int steps, |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1122 | int repeat_mode) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1123 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1124 | int current_index = playlist->index; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1125 | int next_index = -1; |
| 1126 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1127 | if (playlist->amount <= 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1128 | return -1; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1129 | |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1130 | if (repeat_mode == -1) |
| 1131 | repeat_mode = global_settings.repeat_mode; |
| 1132 | |
Jonathan Gordon | 594099f | 2006-12-27 01:51:33 +0000 | [diff] [blame] | 1133 | if (repeat_mode == REPEAT_SHUFFLE && playlist->amount <= 1) |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1134 | repeat_mode = REPEAT_ALL; |
| 1135 | |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1136 | steps = calculate_step_count(playlist, steps); |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1137 | switch (repeat_mode) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1138 | { |
Hardeep Sidhu | 74d082c | 2005-06-25 04:46:25 +0000 | [diff] [blame] | 1139 | case REPEAT_SHUFFLE: |
| 1140 | /* Treat repeat shuffle just like repeat off. At end of playlist, |
| 1141 | play will be resumed in playlist_next() */ |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1142 | case REPEAT_OFF: |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1143 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1144 | current_index = rotate_index(playlist, current_index); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1145 | next_index = current_index+steps; |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1146 | if ((next_index < 0) || (next_index >= playlist->amount)) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1147 | next_index = -1; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1148 | else |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1149 | next_index = (next_index+playlist->first_index) % |
| 1150 | playlist->amount; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1151 | |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1152 | break; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1153 | } |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1154 | |
| 1155 | case REPEAT_ONE: |
Daniel Stenberg | 0c021de | 2007-02-17 23:07:39 +0000 | [diff] [blame] | 1156 | #ifdef AB_REPEAT_ENABLE |
Linus Nielsen Feltzing | 0ad617c | 2005-08-21 23:01:12 +0000 | [diff] [blame] | 1157 | case REPEAT_AB: |
| 1158 | #endif |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1159 | next_index = current_index; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | |
| 1162 | case REPEAT_ALL: |
| 1163 | default: |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1164 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1165 | next_index = (current_index+steps) % playlist->amount; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1166 | while (next_index < 0) |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1167 | next_index += playlist->amount; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1168 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1169 | if (steps >= playlist->amount) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1170 | { |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1171 | int i, index; |
| 1172 | |
| 1173 | index = next_index; |
| 1174 | next_index = -1; |
| 1175 | |
| 1176 | /* second time around so skip the queued files */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1177 | for (i=0; i<playlist->amount; i++) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1178 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1179 | if (playlist->indices[index] & PLAYLIST_QUEUE_MASK) |
| 1180 | index = (index+1) % playlist->amount; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1181 | else |
| 1182 | { |
| 1183 | next_index = index; |
| 1184 | break; |
| 1185 | } |
| 1186 | } |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1187 | } |
| 1188 | break; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1189 | } |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Miika Pekkarinen | c52f7f1 | 2005-10-21 06:40:45 +0000 | [diff] [blame] | 1192 | /* No luck if the whole playlist was bad. */ |
| 1193 | if (playlist->indices[next_index] & PLAYLIST_SKIPPED) |
| 1194 | return -1; |
| 1195 | |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1196 | return next_index; |
| 1197 | } |
| 1198 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * Search for the seek track and set appropriate indices. Used after shuffle |
| 1201 | * to make sure the current index is still pointing to correct track. |
| 1202 | */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1203 | static void find_and_set_playlist_index(struct playlist_info* playlist, |
| 1204 | unsigned int seek) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1205 | { |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1206 | int i; |
| 1207 | |
| 1208 | /* Set the index to the current song */ |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1209 | for (i=0; i<playlist->amount; i++) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1210 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1211 | if (playlist->indices[i] == seek) |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1212 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1213 | playlist->index = playlist->first_index = i; |
| 1214 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1215 | break; |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * used to sort track indices. Sort order is as follows: |
| 1222 | * 1. Prepended tracks (in prepend order) |
| 1223 | * 2. Playlist/directory tracks (in playlist order) |
| 1224 | * 3. Inserted/Appended tracks (in insert order) |
| 1225 | */ |
| 1226 | static int compare(const void* p1, const void* p2) |
| 1227 | { |
Jean-Philippe Bernardy | 5203f06 | 2005-02-12 12:57:16 +0000 | [diff] [blame] | 1228 | unsigned long* e1 = (unsigned long*) p1; |
| 1229 | unsigned long* e2 = (unsigned long*) p2; |
| 1230 | unsigned long flags1 = *e1 & PLAYLIST_INSERT_TYPE_MASK; |
| 1231 | unsigned long flags2 = *e2 & PLAYLIST_INSERT_TYPE_MASK; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (flags1 == flags2) |
| 1234 | return (*e1 & PLAYLIST_SEEK_MASK) - (*e2 & PLAYLIST_SEEK_MASK); |
| 1235 | else if (flags1 == PLAYLIST_INSERT_TYPE_PREPEND || |
| 1236 | flags2 == PLAYLIST_INSERT_TYPE_APPEND) |
| 1237 | return -1; |
| 1238 | else if (flags1 == PLAYLIST_INSERT_TYPE_APPEND || |
| 1239 | flags2 == PLAYLIST_INSERT_TYPE_PREPEND) |
| 1240 | return 1; |
| 1241 | else if (flags1 && flags2) |
| 1242 | return (*e1 & PLAYLIST_SEEK_MASK) - (*e2 & PLAYLIST_SEEK_MASK); |
Björn Stenberg | c78e1b0 | 2003-01-09 00:55:00 +0000 | [diff] [blame] | 1243 | else |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1244 | return *e1 - *e2; |
| 1245 | } |
| 1246 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1247 | #ifdef HAVE_DIRCACHE |
| 1248 | /** |
| 1249 | * Thread to update filename pointers to dircache on background |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1250 | * without affecting playlist load up performance. This thread also flushes |
| 1251 | * any pending control commands when the disk spins up. |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1252 | */ |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 1253 | static void playlist_flush_callback(void) |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 1254 | { |
| 1255 | struct playlist_info *playlist; |
| 1256 | playlist = ¤t_playlist; |
| 1257 | if (playlist->control_fd >= 0) |
| 1258 | { |
| 1259 | if (playlist->num_cached > 0) |
| 1260 | { |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 1261 | mutex_lock(playlist->control_mutex); |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 1262 | flush_cached_control(playlist); |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 1263 | mutex_unlock(playlist->control_mutex); |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 1264 | } |
| 1265 | sync_control(playlist, true); |
| 1266 | } |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 1267 | } |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1268 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1269 | static void playlist_thread(void) |
| 1270 | { |
Michael Sevakis | a9b2fb5 | 2007-10-16 01:25:17 +0000 | [diff] [blame] | 1271 | struct queue_event ev; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1272 | bool dirty_pointers = false; |
| 1273 | static char tmp[MAX_PATH+1]; |
| 1274 | |
| 1275 | struct playlist_info *playlist; |
| 1276 | int index; |
| 1277 | int seek; |
| 1278 | bool control_file; |
| 1279 | |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1280 | int sleep_time = 5; |
| 1281 | |
Frank Gevaerts | 4657301 | 2008-10-07 19:37:33 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_DISK_STORAGE |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1283 | if (global_settings.disk_spindown > 1 && |
| 1284 | global_settings.disk_spindown <= 5) |
| 1285 | sleep_time = global_settings.disk_spindown - 1; |
Nils Wallménius | 0bfa3e7 | 2007-08-01 08:50:44 +0000 | [diff] [blame] | 1286 | #endif |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1287 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1288 | while (1) |
| 1289 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1290 | queue_wait_w_tmo(&playlist_queue, &ev, HZ*sleep_time); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1291 | |
| 1292 | switch (ev.id) |
| 1293 | { |
| 1294 | case PLAYLIST_LOAD_POINTERS: |
| 1295 | dirty_pointers = true; |
| 1296 | break ; |
| 1297 | |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1298 | /* Start the background scanning after either the disk spindown |
| 1299 | timeout or 5s, whichever is less */ |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1300 | case SYS_TIMEOUT: |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1301 | { |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1302 | playlist = ¤t_playlist; |
Jonathan Gordon | 327f845 | 2008-04-30 09:23:12 +0000 | [diff] [blame] | 1303 | if (playlist->control_fd >= 0) |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1304 | { |
| 1305 | if (playlist->num_cached > 0) |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 1306 | register_storage_idle_func(playlist_flush_callback); |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1309 | if (!playlist->dcfrefs || playlist->amount <= 0) |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1310 | break ; |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1311 | |
| 1312 | /* Check if previously loaded pointers are intact. */ |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1313 | if (!dirty_pointers) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1314 | break ; |
| 1315 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1316 | struct dircache_info info; |
| 1317 | dircache_get_info(&info); |
| 1318 | |
| 1319 | if (info.status != DIRCACHE_READY) |
| 1320 | break ; |
| 1321 | |
| 1322 | trigger_cpu_boost(); |
| 1323 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1324 | for (index = 0; index < playlist->amount |
| 1325 | && queue_empty(&playlist_queue); index++) |
| 1326 | { |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1327 | /* Process only pointers that are superficially stale. */ |
Michael Sevakis | 7373cf5 | 2017-01-18 04:39:35 -0500 | [diff] [blame] | 1328 | if (dircache_search(DCS_FILEREF, &playlist->dcfrefs[index], NULL) > 0) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1329 | continue ; |
| 1330 | |
| 1331 | control_file = playlist->indices[index] & PLAYLIST_INSERT_TYPE_MASK; |
| 1332 | seek = playlist->indices[index] & PLAYLIST_SEEK_MASK; |
| 1333 | |
| 1334 | /* Load the filename from playlist file. */ |
| 1335 | if (get_filename(playlist, index, seek, control_file, tmp, |
| 1336 | sizeof(tmp)) < 0) |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1337 | { |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1338 | break ; |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1339 | } |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1340 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1341 | /* Obtain the dircache file entry cookie. */ |
| 1342 | dircache_search(DCS_CACHED_PATH | DCS_UPDATE_FILEREF, |
| 1343 | &playlist->dcfrefs[index], tmp); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* And be on background so user doesn't notice any delays. */ |
| 1346 | yield(); |
| 1347 | } |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1348 | |
| 1349 | cancel_cpu_boost(); |
| 1350 | |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1351 | if (index == playlist->amount) |
| 1352 | dirty_pointers = false; |
| 1353 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1354 | break ; |
Miika Pekkarinen | 2bc133d | 2011-06-21 17:42:31 +0000 | [diff] [blame] | 1355 | } |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1356 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1357 | case SYS_USB_CONNECTED: |
| 1358 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 1359 | usb_wait_for_disconnect(&playlist_queue); |
| 1360 | break ; |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | #endif |
| 1365 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1366 | /* |
| 1367 | * gets pathname for track at seek index |
| 1368 | */ |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1369 | static int get_filename(struct playlist_info* playlist, int index, int seek, |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1370 | bool control_file, char *buf, int buf_length) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1371 | { |
| 1372 | int fd; |
| 1373 | int max = -1; |
| 1374 | char tmp_buf[MAX_PATH+1]; |
| 1375 | char dir_buf[MAX_PATH+1]; |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 1376 | bool utf8 = playlist->utf8; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (buf_length > MAX_PATH+1) |
| 1379 | buf_length = MAX_PATH+1; |
| 1380 | |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1381 | #ifdef HAVE_DIRCACHE |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1382 | if (playlist->dcfrefs) |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1383 | { |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1384 | max = dircache_get_fileref_path(&playlist->dcfrefs[index], |
| 1385 | tmp_buf, sizeof(tmp_buf)); |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1386 | } |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1387 | #endif /* HAVE_DIRCACHE */ |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1388 | |
| 1389 | if (playlist->in_ram && !control_file && max < 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1390 | { |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1391 | max = strlcpy(tmp_buf, (char*)&playlist->buffer[seek], sizeof(tmp_buf)); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1392 | } |
Miika Pekkarinen | 735f453 | 2005-11-17 19:31:29 +0000 | [diff] [blame] | 1393 | else if (max < 0) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1394 | { |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 1395 | mutex_lock(playlist->control_mutex); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 1396 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1397 | if (control_file) |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 1398 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1399 | fd = playlist->control_fd; |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 1400 | utf8 = true; |
| 1401 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1402 | else |
| 1403 | { |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1404 | if(-1 == playlist->fd) |
| 1405 | playlist->fd = open(playlist->filename, O_RDONLY); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1406 | |
Hardeep Sidhu | 107ebc5 | 2004-01-26 17:05:21 +0000 | [diff] [blame] | 1407 | fd = playlist->fd; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | if(-1 != fd) |
| 1411 | { |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1412 | |
Björn Stenberg | 30d8f61 | 2005-01-20 16:24:26 +0000 | [diff] [blame] | 1413 | if (lseek(fd, seek, SEEK_SET) != seek) |
| 1414 | max = -1; |
| 1415 | else |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 1416 | { |
| 1417 | max = read(fd, tmp_buf, MIN((size_t) buf_length, sizeof(tmp_buf))); |
| 1418 | |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1419 | if (max > 0) |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 1420 | { |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1421 | /* playlist file may end without a new line - terminate buffer */ |
| 1422 | tmp_buf[MIN(max, (int)sizeof(tmp_buf) - 1)] = '\0'; |
| 1423 | |
Magnus Holmgren | 6e06825 | 2007-11-13 18:49:20 +0000 | [diff] [blame] | 1424 | /* Use dir_buf as a temporary buffer. Note that dir_buf must |
| 1425 | * be as large as tmp_buf. |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 1426 | */ |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1427 | if (!utf8) |
| 1428 | max = convert_m3u(tmp_buf, max, sizeof(tmp_buf), dir_buf); |
Magnus Holmgren | a515b10 | 2006-11-29 19:22:44 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Michael Sevakis | b15aa47 | 2011-02-14 11:27:45 +0000 | [diff] [blame] | 1433 | mutex_unlock(playlist->control_mutex); |
Hardeep Sidhu | 0cca6ca | 2006-02-09 09:09:32 +0000 | [diff] [blame] | 1434 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1435 | if (max < 0) |
| 1436 | { |
William Wilgus | 929ea73 | 2018-12-26 12:39:53 -0600 | [diff] [blame] | 1437 | if (usb_detect() == USB_INSERTED) |
| 1438 | ; /* ignore error on usb plug */ |
| 1439 | else if (control_file) |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 1440 | splash(HZ*2, ID2P(LANG_PLAYLIST_CONTROL_ACCESS_ERROR)); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1441 | else |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 1442 | splash(HZ*2, ID2P(LANG_PLAYLIST_ACCESS_ERROR)); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1443 | |
| 1444 | return max; |
| 1445 | } |
| 1446 | } |
| 1447 | |
Nils Wallménius | 3d4701a | 2009-07-14 13:57:45 +0000 | [diff] [blame] | 1448 | strlcpy(dir_buf, playlist->filename, playlist->dirlen); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1449 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1450 | return format_track_path(buf, tmp_buf, buf_length, dir_buf); |
Michael Sevakis | 16a9f84 | 2017-01-17 14:45:07 -0500 | [diff] [blame] | 1451 | |
| 1452 | (void)index; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 1455 | static int get_next_directory(char *dir){ |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1456 | return get_next_dir(dir, true); |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static int get_previous_directory(char *dir){ |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1460 | return get_next_dir(dir, false); |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1463 | /* |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1464 | * search through all the directories (starting with the current) to find |
| 1465 | * one that has tracks to play |
| 1466 | */ |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1467 | static int get_next_dir(char *dir, bool is_forward) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1468 | { |
| 1469 | struct playlist_info* playlist = ¤t_playlist; |
| 1470 | int result = -1; |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1471 | char *start_dir = NULL; |
| 1472 | bool exit = false; |
| 1473 | struct tree_context* tc = tree_get_context(); |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1474 | int saved_dirfilter = *(tc->dirfilter); |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1475 | unsigned int base_len; |
| 1476 | |
| 1477 | if (global_settings.constrain_next_folder) |
| 1478 | { |
| 1479 | /* constrain results to directories below user's start directory */ |
| 1480 | strcpy(dir, global_settings.start_directory); |
| 1481 | base_len = strlen(dir); |
| 1482 | |
| 1483 | /* strip any trailing slash from base directory */ |
| 1484 | if (base_len > 0 && dir[base_len - 1] == '/') |
| 1485 | { |
| 1486 | base_len--; |
| 1487 | dir[base_len] = '\0'; |
| 1488 | } |
| 1489 | } |
| 1490 | else |
| 1491 | { |
| 1492 | /* start from root directory */ |
| 1493 | dir[0] = '\0'; |
| 1494 | base_len = 0; |
| 1495 | } |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1496 | |
| 1497 | /* process random folder advance */ |
Jonathan Gordon | 27ad51f | 2006-10-09 10:54:17 +0000 | [diff] [blame] | 1498 | if (global_settings.next_folder == FOLDER_ADVANCE_RANDOM) |
| 1499 | { |
Thomas Martitz | 2c24160 | 2010-12-06 22:26:31 +0000 | [diff] [blame] | 1500 | int fd = open(ROCKBOX_DIR "/folder_advance_list.dat", O_RDONLY); |
Jonathan Gordon | 27ad51f | 2006-10-09 10:54:17 +0000 | [diff] [blame] | 1501 | if (fd >= 0) |
| 1502 | { |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1503 | int folder_count = 0; |
Jonathan Gordon | 27ad51f | 2006-10-09 10:54:17 +0000 | [diff] [blame] | 1504 | read(fd,&folder_count,sizeof(int)); |
Jonathan Gordon | 8f3175b | 2008-06-29 02:19:53 +0000 | [diff] [blame] | 1505 | if (folder_count) |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1506 | { |
| 1507 | char buffer[MAX_PATH]; |
| 1508 | /* give up looking for a directory after we've had four |
| 1509 | times as many tries as there are directories. */ |
| 1510 | unsigned long allowed_tries = folder_count * 4; |
| 1511 | int i; |
| 1512 | srand(current_tick); |
| 1513 | *(tc->dirfilter) = SHOW_MUSIC; |
| 1514 | tc->sort_dir = global_settings.sort_dir; |
| 1515 | while (!exit && allowed_tries--) |
| 1516 | { |
| 1517 | i = rand() % folder_count; |
| 1518 | lseek(fd, sizeof(int) + (MAX_PATH * i), SEEK_SET); |
| 1519 | read(fd, buffer, MAX_PATH); |
| 1520 | /* is the current dir within our base dir and has music? */ |
| 1521 | if ((base_len == 0 || !strncmp(buffer, dir, base_len)) |
| 1522 | && check_subdir_for_music(buffer, "", false) == 0) |
| 1523 | exit = true; |
| 1524 | } |
| 1525 | close(fd); |
| 1526 | *(tc->dirfilter) = saved_dirfilter; |
| 1527 | tc->sort_dir = global_settings.sort_dir; |
| 1528 | reload_directory(); |
| 1529 | if (exit) |
| 1530 | { |
| 1531 | strcpy(dir,buffer); |
| 1532 | return 0; |
| 1533 | } |
| 1534 | } |
| 1535 | else |
| 1536 | close(fd); |
Jonathan Gordon | 27ad51f | 2006-10-09 10:54:17 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1539 | |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1540 | /* if the current file is within our base dir, use its dir instead */ |
| 1541 | if (base_len == 0 || !strncmp(playlist->filename, dir, base_len)) |
Nils Wallménius | 3d4701a | 2009-07-14 13:57:45 +0000 | [diff] [blame] | 1542 | strlcpy(dir, playlist->filename, playlist->dirlen); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1543 | |
| 1544 | /* use the tree browser dircache to load files */ |
Hardeep Sidhu | d0de1aa | 2006-04-18 16:24:12 +0000 | [diff] [blame] | 1545 | *(tc->dirfilter) = SHOW_ALL; |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1546 | |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1547 | /* set up sorting/direction */ |
| 1548 | tc->sort_dir = global_settings.sort_dir; |
| 1549 | if (!is_forward) |
| 1550 | { |
Jens Arnold | a0c91ae | 2008-10-08 17:23:59 +0000 | [diff] [blame] | 1551 | static const char sortpairs[] = |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1552 | { |
Jens Arnold | a0c91ae | 2008-10-08 17:23:59 +0000 | [diff] [blame] | 1553 | [SORT_ALPHA] = SORT_ALPHA_REVERSED, |
| 1554 | [SORT_DATE] = SORT_DATE_REVERSED, |
| 1555 | [SORT_TYPE] = SORT_TYPE_REVERSED, |
| 1556 | [SORT_ALPHA_REVERSED] = SORT_ALPHA, |
| 1557 | [SORT_DATE_REVERSED] = SORT_DATE, |
| 1558 | [SORT_TYPE_REVERSED] = SORT_TYPE, |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1559 | }; |
Jens Arnold | a0c91ae | 2008-10-08 17:23:59 +0000 | [diff] [blame] | 1560 | |
| 1561 | if ((unsigned)tc->sort_dir < sizeof(sortpairs)) |
| 1562 | tc->sort_dir = sortpairs[tc->sort_dir]; |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1565 | while (!exit) |
| 1566 | { |
| 1567 | struct entry *files; |
| 1568 | int num_files = 0; |
| 1569 | int i; |
| 1570 | |
| 1571 | if (ft_load(tc, (dir[0]=='\0')?"/":dir) < 0) |
| 1572 | { |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1573 | exit = true; |
| 1574 | result = -1; |
| 1575 | break; |
| 1576 | } |
| 1577 | |
William Wilgus | 3f110da | 2018-12-13 10:39:49 -0600 | [diff] [blame] | 1578 | tree_lock_cache(tc); |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1579 | files = tree_get_entries(tc); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1580 | num_files = tc->filesindir; |
| 1581 | |
| 1582 | for (i=0; i<num_files; i++) |
| 1583 | { |
| 1584 | /* user abort */ |
Linus Nielsen Feltzing | 224c0a1 | 2006-08-15 12:27:07 +0000 | [diff] [blame] | 1585 | if (action_userabort(TIMEOUT_NOBLOCK)) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1586 | { |
| 1587 | result = -1; |
| 1588 | exit = true; |
| 1589 | break; |
| 1590 | } |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1591 | |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1592 | if (files[i].attr & ATTR_DIRECTORY) |
| 1593 | { |
| 1594 | if (!start_dir) |
| 1595 | { |
Jonathan Gordon | f48cf62 | 2008-07-02 09:16:26 +0000 | [diff] [blame] | 1596 | result = check_subdir_for_music(dir, files[i].name, true); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1597 | if (result != -1) |
| 1598 | { |
| 1599 | exit = true; |
| 1600 | break; |
| 1601 | } |
| 1602 | } |
| 1603 | else if (!strcmp(start_dir, files[i].name)) |
| 1604 | start_dir = NULL; |
| 1605 | } |
| 1606 | } |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1607 | tree_unlock_cache(tc); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (!exit) |
| 1610 | { |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1611 | /* we've already descended to the base dir with nothing found, |
| 1612 | check whether that contains music */ |
| 1613 | if (strlen(dir) <= base_len) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1614 | { |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1615 | result = check_subdir_for_music(dir, "", true); |
| 1616 | if (result == -1) |
| 1617 | /* there's no music files in the base directory, |
| 1618 | treat as a fatal error */ |
| 1619 | result = -2; |
| 1620 | break; |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1621 | } |
| 1622 | else |
Nick Peskett | be10817 | 2012-03-19 09:56:38 +0000 | [diff] [blame] | 1623 | { |
| 1624 | /* move down to parent directory. current directory name is |
| 1625 | stored as the starting point for the search in parent */ |
| 1626 | start_dir = strrchr(dir, '/'); |
| 1627 | if (start_dir) |
| 1628 | { |
| 1629 | *start_dir = '\0'; |
| 1630 | start_dir++; |
| 1631 | } |
| 1632 | else |
| 1633 | break; |
| 1634 | } |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1638 | /* restore dirfilter */ |
| 1639 | *(tc->dirfilter) = saved_dirfilter; |
| 1640 | tc->sort_dir = global_settings.sort_dir; |
Anton Oleynikov | d102e1f | 2005-11-02 22:32:04 +0000 | [diff] [blame] | 1641 | |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1642 | return result; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Checks if there are any music files in the dir or any of its |
| 1647 | * subdirectories. May be called recursively. |
| 1648 | */ |
Nils Wallménius | 48b52ae | 2008-10-08 16:32:01 +0000 | [diff] [blame] | 1649 | static int check_subdir_for_music(char *dir, const char *subdir, bool recurse) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1650 | { |
| 1651 | int result = -1; |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1652 | size_t dirlen = strlen(dir); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1653 | int num_files = 0; |
| 1654 | int i; |
| 1655 | struct entry *files; |
| 1656 | bool has_music = false; |
| 1657 | bool has_subdir = false; |
| 1658 | struct tree_context* tc = tree_get_context(); |
| 1659 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1660 | if (path_append(dir + dirlen, PA_SEP_HARD, subdir, MAX_PATH - dirlen) >= |
| 1661 | MAX_PATH - dirlen) |
| 1662 | { |
| 1663 | return 0; |
| 1664 | } |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (ft_load(tc, dir) < 0) |
| 1667 | { |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1668 | return -2; |
| 1669 | } |
| 1670 | |
William Wilgus | 3f110da | 2018-12-13 10:39:49 -0600 | [diff] [blame] | 1671 | tree_lock_cache(tc); |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1672 | files = tree_get_entries(tc); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1673 | num_files = tc->filesindir; |
William Wilgus | 3f110da | 2018-12-13 10:39:49 -0600 | [diff] [blame] | 1674 | |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1675 | for (i=0; i<num_files; i++) |
| 1676 | { |
| 1677 | if (files[i].attr & ATTR_DIRECTORY) |
| 1678 | has_subdir = true; |
Jonathan Gordon | 36a2e30 | 2007-04-18 13:03:01 +0000 | [diff] [blame] | 1679 | else if ((files[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1680 | { |
| 1681 | has_music = true; |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (has_music) |
William Wilgus | 3f110da | 2018-12-13 10:39:49 -0600 | [diff] [blame] | 1687 | { |
| 1688 | tree_unlock_cache(tc); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1689 | return 0; |
William Wilgus | 3f110da | 2018-12-13 10:39:49 -0600 | [diff] [blame] | 1690 | } |
| 1691 | |
Jonathan Gordon | f48cf62 | 2008-07-02 09:16:26 +0000 | [diff] [blame] | 1692 | if (has_subdir && recurse) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1693 | { |
| 1694 | for (i=0; i<num_files; i++) |
| 1695 | { |
Linus Nielsen Feltzing | 224c0a1 | 2006-08-15 12:27:07 +0000 | [diff] [blame] | 1696 | if (action_userabort(TIMEOUT_NOBLOCK)) |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1697 | { |
| 1698 | result = -2; |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | if (files[i].attr & ATTR_DIRECTORY) |
| 1703 | { |
Jonathan Gordon | f48cf62 | 2008-07-02 09:16:26 +0000 | [diff] [blame] | 1704 | result = check_subdir_for_music(dir, files[i].name, true); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1705 | if (!result) |
| 1706 | break; |
| 1707 | } |
| 1708 | } |
| 1709 | } |
Thomas Martitz | baa070c | 2011-08-30 14:01:45 +0000 | [diff] [blame] | 1710 | tree_unlock_cache(tc); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (result < 0) |
| 1713 | { |
Magnus Holmgren | 97c6fd0 | 2005-07-30 18:12:25 +0000 | [diff] [blame] | 1714 | if (dirlen) |
| 1715 | { |
| 1716 | dir[dirlen] = '\0'; |
| 1717 | } |
| 1718 | else |
| 1719 | { |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1720 | strcpy(dir, PATH_ROOTSTR); |
Magnus Holmgren | 97c6fd0 | 2005-07-30 18:12:25 +0000 | [diff] [blame] | 1721 | } |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1722 | |
| 1723 | /* we now need to reload our current directory */ |
| 1724 | if(ft_load(tc, dir) < 0) |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 1725 | splash(HZ*2, ID2P(LANG_PLAYLIST_DIRECTORY_ACCESS_ERROR)); |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1726 | } |
Hardeep Sidhu | 71d2281 | 2005-07-01 11:25:16 +0000 | [diff] [blame] | 1727 | return result; |
| 1728 | } |
| 1729 | |
| 1730 | /* |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1731 | * Returns absolute path of track |
| 1732 | */ |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1733 | static ssize_t format_track_path(char *dest, char *src, int buf_length, |
| 1734 | const char *dir) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1735 | { |
Michael Sevakis | 5c6ccb4 | 2017-01-07 07:12:10 -0500 | [diff] [blame] | 1736 | size_t len = 0; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1737 | |
Michael Sevakis | 5c6ccb4 | 2017-01-07 07:12:10 -0500 | [diff] [blame] | 1738 | /* Look for the end of the string */ |
| 1739 | while (1) |
| 1740 | { |
| 1741 | int c = src[len]; |
| 1742 | if (c == '\n' || c == '\r' || c == '\0') |
| 1743 | break; |
| 1744 | len++; |
| 1745 | } |
| 1746 | |
| 1747 | /* Now work back killing white space */ |
| 1748 | while (len > 0) |
| 1749 | { |
| 1750 | int c = src[len - 1]; |
| 1751 | if (c != '\t' && c != ' ') |
| 1752 | break; |
| 1753 | len--; |
| 1754 | } |
| 1755 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1756 | src[len] = '\0'; |
Thomas Jarosch | 15a5f9c | 2011-02-18 21:39:59 +0000 | [diff] [blame] | 1757 | |
Michael Sevakis | 5c6ccb4 | 2017-01-07 07:12:10 -0500 | [diff] [blame] | 1758 | /* Replace backslashes with forward slashes */ |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1759 | path_correct_separators(src, src); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1760 | |
Michael Sevakis | 5c6ccb4 | 2017-01-07 07:12:10 -0500 | [diff] [blame] | 1761 | /* Drive letters have no meaning here; handle DOS style drive letter |
| 1762 | * and parse greedily so that: |
| 1763 | * |
| 1764 | * 1) "c:/foo" is fully qualified, use directory volume only |
| 1765 | * 2) "c:foo" is relative to current directory on C, use directory path |
| 1766 | * |
| 1767 | * Assume any volume on the beginning of the directory path is actually |
| 1768 | * the volume on which it resides. This may not be the case if the dir |
| 1769 | * param contains a path such as "/<1>/foo/../../<0>/bar", which refers |
| 1770 | * to "/<0>/bar" (aka "/bar" at this time). *fingers crossed* |
| 1771 | * |
| 1772 | * If any stripped drive spec was absolute, prepend the playlist |
| 1773 | * directory's volume spec, or root if none. Relative paths remain |
| 1774 | * relative and the playlist's directory fully qualifies them. Absolute |
| 1775 | * UNIX-style paths remain unaltered. |
| 1776 | */ |
| 1777 | if (path_strip_drive(src, (const char **)&src, true) >= 0 && |
| 1778 | src[-1] == PATH_SEPCH) |
| 1779 | { |
| 1780 | #ifdef HAVE_MULTIVOLUME |
| 1781 | const char *p; |
| 1782 | path_strip_volume(dir, &p, false); |
| 1783 | dir = strmemdupa(dir, p - dir); /* empty if no volspec on dir */ |
| 1784 | #else |
| 1785 | dir = ""; /* only volume is root */ |
| 1786 | #endif |
| 1787 | } |
Michael Giacomelli | b30edcd | 2012-06-02 00:12:54 -0400 | [diff] [blame] | 1788 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1789 | len = path_append(dest, *dir ? dir : PATH_ROOTSTR, src, buf_length); |
| 1790 | if (len >= (size_t)buf_length) |
| 1791 | return -1; /* buffer too small */ |
Michael Giacomelli | b30edcd | 2012-06-02 00:12:54 -0400 | [diff] [blame] | 1792 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 1793 | return len; |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * Display splash message showing progress of playlist/directory insertion or |
| 1798 | * save. |
| 1799 | */ |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 1800 | static void display_playlist_count(int count, const unsigned char *fmt, |
| 1801 | bool final) |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1802 | { |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 1803 | static long talked_tick = 0; |
| 1804 | long id = P2ID(fmt); |
Steve Bavin | 32a9575 | 2007-10-19 15:31:42 +0000 | [diff] [blame] | 1805 | if(global_settings.talk_menu && id>=0) |
Nils Wallménius | 5b76936 | 2007-08-06 13:08:36 +0000 | [diff] [blame] | 1806 | { |
| 1807 | if(final || (count && (talked_tick == 0 |
| 1808 | || TIME_AFTER(current_tick, talked_tick+5*HZ)))) |
| 1809 | { |
| 1810 | talked_tick = current_tick; |
| 1811 | talk_number(count, false); |
| 1812 | talk_id(id, true); |
| 1813 | } |
| 1814 | } |
| 1815 | fmt = P2STR(fmt); |
| 1816 | |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 1817 | splashf(0, fmt, count, str(LANG_OFF_ABORT)); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | /* |
| 1821 | * Display buffer full message |
| 1822 | */ |
| 1823 | static void display_buffer_full(void) |
| 1824 | { |
Nils Wallménius | 01729e7 | 2008-08-15 08:27:39 +0000 | [diff] [blame] | 1825 | splash(HZ*2, ID2P(LANG_PLAYLIST_BUFFER_FULL)); |
Hardeep Sidhu | 9e42620 | 2003-07-01 21:05:43 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1829 | * Flush any cached control commands to disk. Called when playlist is being |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 1830 | * modified. Returns 0 on success and -1 on failure. |
| 1831 | */ |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1832 | static int flush_cached_control(struct playlist_info* playlist) |
Hardeep Sidhu | 58bafee | 2003-12-09 08:18:03 +0000 | [diff] [blame] | 1833 | { |
| 1834 | int result = 0; |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1835 | int i; |
| 1836 | |
Hardeep Sidhu | cab3a16 | 2006-05-11 19:14:18 +0000 | [diff] [blame] | 1837 | if (!playlist->num_cached) |
| 1838 | return 0; |
| 1839 | |
Hardeep Sidhu | 75a60fb | 2006-02-05 18:17:41 +0000 | [diff] [blame] | 1840 |
|