2002-06-04 21:43:38 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by wavey@wavey.org
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "playlist.h"
|
2003-01-09 00:55:00 +00:00
|
|
|
#include "file.h"
|
2002-06-04 21:43:38 +00:00
|
|
|
#include "sprintf.h"
|
|
|
|
#include "debug.h"
|
2002-06-07 14:48:06 +00:00
|
|
|
#include "mpeg.h"
|
2002-06-13 15:16:41 +00:00
|
|
|
#include "lcd.h"
|
|
|
|
#include "kernel.h"
|
2002-06-27 10:10:30 +00:00
|
|
|
#include "settings.h"
|
2002-08-07 10:35:26 +00:00
|
|
|
#include "status.h"
|
2002-08-15 12:28:52 +00:00
|
|
|
#include "applimits.h"
|
2002-08-07 10:35:26 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
2002-08-20 19:39:55 +00:00
|
|
|
#include "icons.h"
|
|
|
|
#include "widgets.h"
|
|
|
|
#endif
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-09-18 14:08:05 +00:00
|
|
|
#include "lang.h"
|
|
|
|
|
2002-10-09 13:42:59 +00:00
|
|
|
static struct playlist_info playlist;
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
#define QUEUE_FILE ROCKBOX_DIR "/.queue_file"
|
2003-03-07 14:38:51 +00:00
|
|
|
|
|
|
|
#define PLAYLIST_BUFFER_SIZE (AVERAGE_FILENAME_LENGTH*MAX_FILES_IN_DIR)
|
|
|
|
static unsigned char playlist_buffer[PLAYLIST_BUFFER_SIZE];
|
2002-08-06 13:09:48 +00:00
|
|
|
|
2003-03-07 09:24:56 +00:00
|
|
|
extern unsigned char mp3buf[],mp3end;
|
2002-08-06 13:09:48 +00:00
|
|
|
static int playlist_end_pos = 0;
|
|
|
|
|
2002-10-09 13:42:59 +00:00
|
|
|
static char now_playing[MAX_PATH+1];
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
/*
|
|
|
|
* remove any files and indices associated with the playlist
|
|
|
|
*/
|
|
|
|
static void empty_playlist(bool queue_resume)
|
2002-08-06 13:09:48 +00:00
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
playlist.filename[0] = '\0';
|
|
|
|
playlist.index = 0;
|
|
|
|
playlist.queue_index = 0;
|
|
|
|
playlist.last_queue_index = 0;
|
|
|
|
playlist.amount = 0;
|
|
|
|
playlist.num_queued = 0;
|
|
|
|
playlist.start_queue = 0;
|
|
|
|
|
|
|
|
if (!queue_resume)
|
|
|
|
{
|
|
|
|
/* start with fresh queue file when starting new playlist */
|
|
|
|
remove(QUEUE_FILE);
|
|
|
|
fd = creat(QUEUE_FILE, 0);
|
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
2002-08-06 13:09:48 +00:00
|
|
|
}
|
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
/* update queue list after resume */
|
|
|
|
static void add_indices_to_queuelist(int seek)
|
2002-08-06 13:09:48 +00:00
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
int nread;
|
|
|
|
int fd = -1;
|
|
|
|
int i = seek;
|
|
|
|
int count = 0;
|
|
|
|
bool store_index;
|
|
|
|
char buf[MAX_PATH];
|
2002-08-06 13:09:48 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
unsigned char *p = buf;
|
2002-08-06 13:09:48 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
fd = open(QUEUE_FILE, O_RDONLY);
|
|
|
|
if(fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nread = lseek(fd, seek, SEEK_SET);
|
|
|
|
if (nread < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
store_index = true;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
nread = read(fd, buf, MAX_PATH);
|
|
|
|
if(nread <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
|
|
|
|
for(count=0; count < nread; count++,p++) {
|
|
|
|
if(*p == '\n')
|
|
|
|
store_index = true;
|
|
|
|
else if(store_index)
|
|
|
|
{
|
|
|
|
store_index = false;
|
|
|
|
|
|
|
|
playlist.queue_indices[playlist.last_queue_index] = i+count;
|
|
|
|
playlist.last_queue_index =
|
|
|
|
(playlist.last_queue_index + 1) % MAX_QUEUED_FILES;
|
|
|
|
playlist.num_queued++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += count;
|
|
|
|
}
|
2002-08-06 13:09:48 +00:00
|
|
|
}
|
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
static int get_next_index(int steps, bool *queue)
|
2002-08-28 09:22:44 +00:00
|
|
|
{
|
2002-10-06 05:50:41 +00:00
|
|
|
int current_index = playlist.index;
|
|
|
|
int next_index = -1;
|
2002-10-04 08:48:20 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
if (global_settings.repeat_mode == REPEAT_ONE)
|
2003-01-09 06:08:41 +00:00
|
|
|
{
|
|
|
|
/* this is needed for repeat one to work with queue mode */
|
2003-01-09 00:55:00 +00:00
|
|
|
steps = 0;
|
2003-01-09 06:08:41 +00:00
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
else if (steps >= 0)
|
2003-01-09 06:08:41 +00:00
|
|
|
{
|
|
|
|
/* Queue index starts from 0 which needs to be accounted for. Also,
|
|
|
|
after resume, this handles case where we want to begin with
|
|
|
|
playlist */
|
2003-01-09 00:55:00 +00:00
|
|
|
steps -= playlist.start_queue;
|
2003-01-09 06:08:41 +00:00
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
|
|
|
|
if (steps >= 0 && playlist.num_queued > 0 &&
|
|
|
|
playlist.num_queued - steps > 0)
|
|
|
|
*queue = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*queue = false;
|
|
|
|
if (playlist.num_queued)
|
|
|
|
{
|
|
|
|
if (steps >= 0)
|
2003-01-09 06:08:41 +00:00
|
|
|
{
|
|
|
|
/* skip the queued tracks */
|
2003-01-09 00:55:00 +00:00
|
|
|
steps -= (playlist.num_queued - 1);
|
2003-01-09 06:08:41 +00:00
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
else if (!playlist.start_queue)
|
2003-01-09 06:08:41 +00:00
|
|
|
{
|
|
|
|
/* previous from queue list needs to go to current track in
|
|
|
|
playlist */
|
2003-01-09 00:55:00 +00:00
|
|
|
steps += 1;
|
2003-01-09 06:08:41 +00:00
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-04 08:48:20 +00:00
|
|
|
switch (global_settings.repeat_mode)
|
|
|
|
{
|
|
|
|
case REPEAT_OFF:
|
2003-01-09 00:55:00 +00:00
|
|
|
if (*queue)
|
|
|
|
next_index = (playlist.queue_index+steps) % MAX_QUEUED_FILES;
|
2002-10-06 05:50:41 +00:00
|
|
|
else
|
2003-01-09 00:55:00 +00:00
|
|
|
{
|
|
|
|
if (current_index < playlist.first_index)
|
|
|
|
current_index += playlist.amount;
|
|
|
|
current_index -= playlist.first_index;
|
|
|
|
|
|
|
|
next_index = current_index+steps;
|
|
|
|
if ((next_index < 0) || (next_index >= playlist.amount))
|
|
|
|
next_index = -1;
|
|
|
|
else
|
|
|
|
next_index = (next_index+playlist.first_index) %
|
|
|
|
playlist.amount;
|
|
|
|
}
|
2002-10-04 08:48:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REPEAT_ONE:
|
2003-01-09 06:08:41 +00:00
|
|
|
/* if we are still in playlist when repeat one is set, don't go to
|
|
|
|
queue list */
|
2003-01-09 00:55:00 +00:00
|
|
|
if (*queue && !playlist.start_queue)
|
|
|
|
next_index = playlist.queue_index;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next_index = current_index;
|
|
|
|
*queue = false;
|
|
|
|
}
|
2002-10-04 08:48:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REPEAT_ALL:
|
|
|
|
default:
|
2003-01-09 00:55:00 +00:00
|
|
|
if (*queue)
|
|
|
|
next_index = (playlist.queue_index+steps) % MAX_QUEUED_FILES;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next_index = (current_index+steps) % playlist.amount;
|
|
|
|
while (next_index < 0)
|
|
|
|
next_index += playlist.amount;
|
|
|
|
}
|
2002-10-04 08:48:20 +00:00
|
|
|
break;
|
2002-08-28 09:22:44 +00:00
|
|
|
}
|
2002-10-04 08:48:20 +00:00
|
|
|
|
|
|
|
return next_index;
|
|
|
|
}
|
|
|
|
|
2002-10-09 13:36:49 +00:00
|
|
|
int playlist_amount(void)
|
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
return playlist.amount + playlist.num_queued;
|
2002-10-09 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2002-10-11 09:14:30 +00:00
|
|
|
int playlist_first_index(void)
|
|
|
|
{
|
|
|
|
return playlist.first_index;
|
|
|
|
}
|
|
|
|
|
2003-01-09 06:08:41 +00:00
|
|
|
/* Get resume info for current playing song. If return value is -1 then
|
|
|
|
settings shouldn't be saved (eg. when playing queued song and save queue
|
|
|
|
disabled) */
|
2003-01-09 00:55:00 +00:00
|
|
|
int playlist_get_resume_info(int *resume_index, int *queue_resume,
|
|
|
|
int *queue_resume_index)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
*resume_index = playlist.index;
|
|
|
|
|
|
|
|
if (playlist.num_queued > 0)
|
|
|
|
{
|
|
|
|
if (global_settings.save_queue_resume)
|
|
|
|
{
|
|
|
|
*queue_resume_index =
|
|
|
|
playlist.queue_indices[playlist.queue_index];
|
2003-01-09 06:08:41 +00:00
|
|
|
/* have we started playing the queue list yet? */
|
2003-01-09 00:55:00 +00:00
|
|
|
if (playlist.start_queue)
|
|
|
|
*queue_resume = QUEUE_BEGIN_PLAYLIST;
|
|
|
|
else
|
|
|
|
*queue_resume = QUEUE_BEGIN_QUEUE;
|
|
|
|
}
|
|
|
|
else if (!playlist.start_queue)
|
|
|
|
{
|
|
|
|
*queue_resume = QUEUE_OFF;
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*queue_resume = QUEUE_OFF;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-10-17 09:34:48 +00:00
|
|
|
char *playlist_name(char *buf, int buf_size)
|
2002-10-15 11:49:15 +00:00
|
|
|
{
|
2002-10-17 09:34:48 +00:00
|
|
|
char *sep;
|
2002-10-15 11:49:15 +00:00
|
|
|
|
2002-10-17 09:34:48 +00:00
|
|
|
snprintf(buf, buf_size, "%s", playlist.filename+playlist.dirlen);
|
2002-10-15 11:49:15 +00:00
|
|
|
|
2002-10-17 09:34:48 +00:00
|
|
|
if (0 == buf[0])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Remove extension */
|
|
|
|
sep = strrchr(buf, '.');
|
|
|
|
if (NULL != sep)
|
|
|
|
*sep = 0;
|
|
|
|
|
|
|
|
return buf;
|
2002-10-15 11:49:15 +00:00
|
|
|
}
|
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
void playlist_clear(void)
|
|
|
|
{
|
|
|
|
playlist_end_pos = 0;
|
|
|
|
playlist_buffer[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int playlist_add(char *filename)
|
|
|
|
{
|
|
|
|
int len = strlen(filename);
|
|
|
|
|
|
|
|
if(len+2 > PLAYLIST_BUFFER_SIZE - playlist_end_pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
strcpy(&playlist_buffer[playlist_end_pos], filename);
|
|
|
|
playlist_end_pos += len;
|
|
|
|
playlist_buffer[playlist_end_pos++] = '\n';
|
|
|
|
playlist_buffer[playlist_end_pos] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add track to queue file */
|
|
|
|
int queue_add(char *filename)
|
|
|
|
{
|
|
|
|
int fd, seek, result;
|
|
|
|
int len = strlen(filename);
|
|
|
|
|
|
|
|
if(playlist.num_queued >= MAX_QUEUED_FILES)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd = open(QUEUE_FILE, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
seek = lseek(fd, 0, SEEK_END);
|
|
|
|
if (seek < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-01-09 06:08:41 +00:00
|
|
|
/* save the file name with a trailing \n. QUEUE_FILE can be used as a
|
|
|
|
playlist if desired */
|
2003-01-09 00:55:00 +00:00
|
|
|
filename[len] = '\n';
|
|
|
|
result = write(fd, filename, len+1);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
filename[len] = '\0';
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (playlist.num_queued <= 0)
|
|
|
|
playlist.start_queue = 1;
|
|
|
|
|
|
|
|
playlist.queue_indices[playlist.last_queue_index] = seek;
|
|
|
|
playlist.last_queue_index =
|
|
|
|
(playlist.last_queue_index + 1) % MAX_QUEUED_FILES;
|
|
|
|
playlist.num_queued++;
|
|
|
|
|
2003-01-09 06:08:41 +00:00
|
|
|
/* the play order has changed */
|
2003-01-09 00:55:00 +00:00
|
|
|
mpeg_flush_and_reload_tracks();
|
|
|
|
|
|
|
|
return playlist.num_queued;
|
|
|
|
}
|
|
|
|
|
2002-10-04 08:48:20 +00:00
|
|
|
int playlist_next(int steps)
|
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
bool queue;
|
|
|
|
int index = get_next_index(steps, &queue);
|
2002-10-04 08:48:20 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
if (queue)
|
|
|
|
{
|
2003-01-09 06:08:41 +00:00
|
|
|
/* queue_diff accounts for bad songs in queue list */
|
2003-01-09 00:55:00 +00:00
|
|
|
int queue_diff = index - playlist.queue_index;
|
|
|
|
if (queue_diff < 0)
|
|
|
|
queue_diff += MAX_QUEUED_FILES;
|
|
|
|
|
|
|
|
playlist.num_queued -= queue_diff;
|
|
|
|
playlist.queue_index = index;
|
|
|
|
playlist.start_queue = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playlist.index = index;
|
|
|
|
if (playlist.num_queued > 0 && !playlist.start_queue)
|
|
|
|
{
|
|
|
|
if (steps >= 0)
|
|
|
|
{
|
2003-01-09 06:08:41 +00:00
|
|
|
/* done with queue list */
|
2003-01-09 00:55:00 +00:00
|
|
|
playlist.queue_index = playlist.last_queue_index;
|
|
|
|
playlist.num_queued = 0;
|
|
|
|
}
|
|
|
|
else
|
2003-01-09 06:08:41 +00:00
|
|
|
{
|
|
|
|
/* user requested previous. however, don't forget about queue
|
|
|
|
list */
|
|
|
|
playlist.start_queue = 1;
|
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2002-08-28 09:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* playlist_peek(int steps)
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
2002-06-26 23:25:03 +00:00
|
|
|
int seek;
|
2002-06-04 21:43:38 +00:00
|
|
|
int max;
|
|
|
|
int fd;
|
2002-06-17 10:17:50 +00:00
|
|
|
int i;
|
2002-08-06 13:09:48 +00:00
|
|
|
char *buf;
|
2002-08-01 11:37:26 +00:00
|
|
|
char dir_buf[MAX_PATH+1];
|
|
|
|
char *dir_end;
|
2002-08-28 09:22:44 +00:00
|
|
|
int index;
|
2003-01-09 00:55:00 +00:00
|
|
|
bool queue;
|
2002-06-07 14:48:06 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
index = get_next_index(steps, &queue);
|
|
|
|
if (index < 0)
|
2002-10-04 08:48:20 +00:00
|
|
|
return NULL;
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
if (queue)
|
2002-08-06 13:09:48 +00:00
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
seek = playlist.queue_indices[index];
|
|
|
|
fd = open(QUEUE_FILE, O_RDONLY);
|
2002-08-06 13:09:48 +00:00
|
|
|
if(-1 != fd)
|
|
|
|
{
|
2003-01-09 00:55:00 +00:00
|
|
|
buf = dir_buf;
|
2002-08-06 13:09:48 +00:00
|
|
|
lseek(fd, seek, SEEK_SET);
|
|
|
|
max = read(fd, buf, MAX_PATH);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-09 00:55:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
seek = playlist.indices[index];
|
|
|
|
|
|
|
|
if(playlist.in_ram)
|
|
|
|
{
|
|
|
|
buf = playlist_buffer + seek;
|
|
|
|
max = playlist_end_pos - seek;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fd = open(playlist.filename, O_RDONLY);
|
|
|
|
if(-1 != fd)
|
|
|
|
{
|
|
|
|
buf = playlist_buffer;
|
|
|
|
lseek(fd, seek, SEEK_SET);
|
|
|
|
max = read(fd, buf, MAX_PATH);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-08-16 14:41:47 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
/* Zero-terminate the file name */
|
|
|
|
seek=0;
|
|
|
|
while((buf[seek] != '\n') &&
|
|
|
|
(buf[seek] != '\r') &&
|
|
|
|
(seek < max))
|
2002-06-04 21:43:38 +00:00
|
|
|
seek++;
|
2002-06-11 07:33:51 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
/* Now work back killing white space */
|
|
|
|
while((buf[seek-1] == ' ') ||
|
|
|
|
(buf[seek-1] == '\t'))
|
|
|
|
seek--;
|
2002-06-27 08:20:09 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
buf[seek]=0;
|
2002-06-17 10:17:50 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
/* replace backslashes with forward slashes */
|
|
|
|
for ( i=0; i<seek; i++ )
|
|
|
|
if ( buf[i] == '\\' )
|
|
|
|
buf[i] = '/';
|
|
|
|
|
|
|
|
if('/' == buf[0]) {
|
|
|
|
strcpy(now_playing, &buf[0]);
|
|
|
|
}
|
|
|
|
else {
|
2002-08-08 10:29:14 +00:00
|
|
|
strncpy(dir_buf, playlist.filename, playlist.dirlen-1);
|
2002-08-08 13:58:53 +00:00
|
|
|
dir_buf[playlist.dirlen-1] = 0;
|
2002-08-06 13:09:48 +00:00
|
|
|
|
|
|
|
/* handle dos style drive letter */
|
|
|
|
if ( ':' == buf[1] ) {
|
|
|
|
strcpy(now_playing, &buf[2]);
|
|
|
|
}
|
|
|
|
else if ( '.' == buf[0] && '.' == buf[1] && '/' == buf[2] ) {
|
|
|
|
/* handle relative paths */
|
|
|
|
seek=3;
|
|
|
|
while(buf[seek] == '.' &&
|
|
|
|
buf[seek+1] == '.' &&
|
|
|
|
buf[seek+2] == '/')
|
|
|
|
seek += 3;
|
|
|
|
for (i=0; i<seek/3; i++) {
|
|
|
|
dir_end = strrchr(dir_buf, '/');
|
|
|
|
if (dir_end)
|
|
|
|
*dir_end = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
snprintf(now_playing, MAX_PATH+1, "%s/%s", dir_buf, &buf[seek]);
|
|
|
|
}
|
|
|
|
else if ( '.' == buf[0] && '/' == buf[1] ) {
|
|
|
|
snprintf(now_playing, MAX_PATH+1, "%s/%s", dir_buf, &buf[2]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(now_playing, MAX_PATH+1, "%s/%s", dir_buf, buf);
|
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
2003-01-09 00:18:47 +00:00
|
|
|
|
|
|
|
buf = now_playing;
|
|
|
|
|
|
|
|
/* remove bogus dirs from beginning of path
|
|
|
|
(workaround for buggy playlist creation tools) */
|
|
|
|
while (buf)
|
|
|
|
{
|
|
|
|
fd = open(buf, O_RDONLY);
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = strchr(buf+1, '/');
|
|
|
|
}
|
|
|
|
|
2003-01-09 06:08:41 +00:00
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
/* Even though this is an invalid file, we still need to pass a file
|
|
|
|
name to the caller because NULL is used to indicate end of
|
|
|
|
playlist */
|
|
|
|
return now_playing;
|
|
|
|
}
|
2003-01-09 00:18:47 +00:00
|
|
|
|
2003-01-09 06:08:41 +00:00
|
|
|
return buf;
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
2002-08-19 12:45:01 +00:00
|
|
|
/*
|
|
|
|
* This function is called to start playback of a given playlist. This
|
|
|
|
* playlist may be stored in RAM (when using full-dir playback).
|
2002-08-21 15:08:01 +00:00
|
|
|
*
|
|
|
|
* Return: the new index (possibly different due to shuffle)
|
2002-08-19 12:45:01 +00:00
|
|
|
*/
|
2002-08-21 15:08:01 +00:00
|
|
|
int play_list(char *dir, /* "current directory" */
|
|
|
|
char *file, /* playlist */
|
|
|
|
int start_index, /* index in the playlist */
|
|
|
|
bool shuffled_index, /* if TRUE the specified index is for the
|
|
|
|
playlist AFTER the shuffle */
|
|
|
|
int start_offset, /* offset in the file */
|
2002-10-06 05:50:41 +00:00
|
|
|
int random_seed, /* used for shuffling */
|
2003-01-09 00:55:00 +00:00
|
|
|
int first_index, /* first index of playlist */
|
|
|
|
int queue_resume, /* resume queue list? */
|
|
|
|
int queue_resume_index ) /* queue list seek pos */
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
2002-06-12 07:07:45 +00:00
|
|
|
char *sep="";
|
2002-08-06 13:09:48 +00:00
|
|
|
int dirlen;
|
2003-01-09 00:55:00 +00:00
|
|
|
empty_playlist(queue_resume);
|
2002-08-06 13:09:48 +00:00
|
|
|
|
2002-08-07 06:04:24 +00:00
|
|
|
playlist.index = start_index;
|
2002-10-06 05:50:41 +00:00
|
|
|
playlist.first_index = first_index;
|
2002-08-07 06:04:24 +00:00
|
|
|
|
2002-08-20 19:39:55 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
if(global_settings.statusbar)
|
|
|
|
lcd_setmargins(0, STATUSBAR_HEIGHT);
|
|
|
|
else
|
|
|
|
lcd_setmargins(0, 0);
|
|
|
|
#endif
|
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
/* If file is NULL, the list is in RAM */
|
|
|
|
if(file) {
|
|
|
|
lcd_clear_display();
|
2002-09-18 14:08:05 +00:00
|
|
|
lcd_puts(0,0,str(LANG_PLAYLIST_LOAD));
|
2002-08-07 10:35:26 +00:00
|
|
|
status_draw();
|
2002-08-06 13:09:48 +00:00
|
|
|
lcd_update();
|
|
|
|
playlist.in_ram = false;
|
|
|
|
} else {
|
|
|
|
/* Assign a dummy filename */
|
|
|
|
file = "";
|
|
|
|
playlist.in_ram = true;
|
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
dirlen = strlen(dir);
|
|
|
|
|
|
|
|
/* If the dir does not end in trailing slash, we use a separator.
|
2002-06-12 07:07:45 +00:00
|
|
|
Otherwise we don't. */
|
2002-08-06 13:09:48 +00:00
|
|
|
if('/' != dir[dirlen-1]) {
|
2002-06-12 07:07:45 +00:00
|
|
|
sep="/";
|
2002-08-06 13:09:48 +00:00
|
|
|
dirlen++;
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist.dirlen = dirlen;
|
2002-06-12 07:07:45 +00:00
|
|
|
|
2002-06-04 21:43:38 +00:00
|
|
|
snprintf(playlist.filename, sizeof(playlist.filename),
|
2002-06-12 07:07:45 +00:00
|
|
|
"%s%s%s",
|
|
|
|
dir, sep, file);
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* add track indices to playlist data structure */
|
2002-08-06 13:09:48 +00:00
|
|
|
add_indices_to_playlist();
|
|
|
|
|
2002-06-27 10:10:30 +00:00
|
|
|
if(global_settings.playlist_shuffle) {
|
2002-08-06 13:09:48 +00:00
|
|
|
if(!playlist.in_ram) {
|
2003-01-15 16:48:25 +00:00
|
|
|
lcd_puts(0,0,str(LANG_PLAYLIST_SHUFFLE));
|
2002-08-07 10:35:26 +00:00
|
|
|
status_draw();
|
2002-08-06 13:09:48 +00:00
|
|
|
lcd_update();
|
2002-08-19 12:45:01 +00:00
|
|
|
randomise_playlist( random_seed );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* store the seek position before the shuffle */
|
|
|
|
int seek_pos = playlist.indices[start_index];
|
|
|
|
|
|
|
|
/* now shuffle around the indices */
|
|
|
|
randomise_playlist( random_seed );
|
|
|
|
|
2002-09-18 10:13:09 +00:00
|
|
|
if(!shuffled_index && global_settings.play_selected) {
|
2002-08-21 15:08:01 +00:00
|
|
|
/* The given index was for the unshuffled list, so we need
|
|
|
|
to figure out the index AFTER the shuffle has been made.
|
|
|
|
We scan for the seek position we remmber from before. */
|
|
|
|
|
|
|
|
for(i=0; i< playlist.amount; i++) {
|
|
|
|
if(seek_pos == playlist.indices[i]) {
|
|
|
|
/* here's the start song! yiepee! */
|
|
|
|
playlist.index = i;
|
2002-10-06 05:50:41 +00:00
|
|
|
playlist.first_index = i;
|
2002-08-21 15:08:01 +00:00
|
|
|
break; /* now stop searching */
|
|
|
|
}
|
2002-08-19 12:45:01 +00:00
|
|
|
}
|
2002-08-21 15:08:01 +00:00
|
|
|
/* if we for any reason wouldn't find the index again, it
|
|
|
|
won't set the index again and we should start at index 0
|
|
|
|
instead */
|
2002-08-19 12:45:01 +00:00
|
|
|
}
|
2002-08-06 13:09:48 +00:00
|
|
|
}
|
2002-06-17 10:36:42 +00:00
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2003-01-09 00:55:00 +00:00
|
|
|
if (queue_resume)
|
|
|
|
{
|
2003-01-09 06:08:41 +00:00
|
|
|
/* update the queue indices */
|
2003-01-09 00:55:00 +00:00
|
|
|
add_indices_to_queuelist(queue_resume_index);
|
|
|
|
|
|
|
|
if (queue_resume == QUEUE_BEGIN_PLAYLIST)
|
|
|
|
{
|
2003-01-09 06:08:41 +00:00
|
|
|
/* begin with current playlist index */
|
2003-01-09 00:55:00 +00:00
|
|
|
playlist.start_queue = 1;
|
|
|
|
playlist.index++; /* so we begin at the correct track */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
if(!playlist.in_ram) {
|
2002-09-18 14:08:05 +00:00
|
|
|
lcd_puts(0,0,str(LANG_PLAYLIST_PLAY));
|
2002-08-07 10:35:26 +00:00
|
|
|
status_draw();
|
2002-08-06 13:09:48 +00:00
|
|
|
lcd_update();
|
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
/* also make the first song get playing */
|
2002-08-16 14:41:47 +00:00
|
|
|
mpeg_play(start_offset);
|
2002-08-21 15:08:01 +00:00
|
|
|
|
|
|
|
return playlist.index;
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate track offsets within a playlist file
|
|
|
|
*/
|
2002-08-06 13:09:48 +00:00
|
|
|
void add_indices_to_playlist(void)
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
|
|
|
int nread;
|
2002-08-06 13:09:48 +00:00
|
|
|
int fd = -1;
|
2002-06-11 07:30:31 +00:00
|
|
|
int i = 0;
|
|
|
|
int count = 0;
|
2002-06-13 21:27:42 +00:00
|
|
|
int next_tick = current_tick + HZ;
|
2003-03-07 14:38:51 +00:00
|
|
|
unsigned char* buffer = playlist_buffer;
|
|
|
|
int buflen = PLAYLIST_BUFFER_SIZE;
|
2002-09-12 09:20:03 +00:00
|
|
|
bool store_index;
|
2003-03-07 14:38:51 +00:00
|
|
|
unsigned char *p;
|
2002-06-13 15:16:41 +00:00
|
|
|
char line[16];
|
2002-06-11 07:33:51 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
if(!playlist.in_ram) {
|
|
|
|
fd = open(playlist.filename, O_RDONLY);
|
|
|
|
if(-1 == fd)
|
|
|
|
return; /* failure */
|
2003-03-07 14:38:51 +00:00
|
|
|
|
2003-03-07 15:09:23 +00:00
|
|
|
#ifndef SIMULATOR
|
2003-03-07 14:38:51 +00:00
|
|
|
/* use mp3 buffer for maximum load speed */
|
|
|
|
buflen = (&mp3end - &mp3buf[0]);
|
|
|
|
buffer = mp3buf;
|
2003-03-07 15:09:23 +00:00
|
|
|
#endif
|
2002-08-06 13:09:48 +00:00
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-09-12 09:11:59 +00:00
|
|
|
store_index = true;
|
2002-06-11 07:30:31 +00:00
|
|
|
|
2003-03-07 09:24:56 +00:00
|
|
|
mpeg_stop();
|
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
while(1)
|
2002-06-11 07:30:31 +00:00
|
|
|
{
|
2002-08-06 13:09:48 +00:00
|
|
|
if(playlist.in_ram) {
|
|
|
|
nread = playlist_end_pos;
|
|
|
|
} else {
|
2003-03-07 14:38:51 +00:00
|
|
|
nread = read(fd, buffer, buflen);
|
2002-08-06 13:09:48 +00:00
|
|
|
/* Terminate on EOF */
|
|
|
|
if(nread <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-03-07 14:38:51 +00:00
|
|
|
p = buffer;
|
2002-06-11 07:30:31 +00:00
|
|
|
|
|
|
|
for(count=0; count < nread; count++,p++) {
|
|
|
|
|
|
|
|
/* Are we on a new line? */
|
2002-06-13 23:06:30 +00:00
|
|
|
if((*p == '\n') || (*p == '\r'))
|
2002-06-11 07:30:31 +00:00
|
|
|
{
|
2002-09-12 09:11:59 +00:00
|
|
|
store_index = true;
|
2002-06-11 07:30:31 +00:00
|
|
|
}
|
|
|
|
else if(store_index)
|
|
|
|
{
|
2002-09-12 09:20:03 +00:00
|
|
|
store_index = false;
|
2002-09-12 09:11:59 +00:00
|
|
|
|
|
|
|
if(playlist.in_ram || (*p != '#'))
|
|
|
|
{
|
|
|
|
/* Store a new entry */
|
|
|
|
playlist.indices[ playlist.amount ] = i+count;
|
|
|
|
playlist.amount++;
|
|
|
|
if ( playlist.amount >= MAX_PLAYLIST_SIZE ) {
|
|
|
|
if(!playlist.in_ram)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
lcd_clear_display();
|
2002-09-18 14:08:05 +00:00
|
|
|
lcd_puts(0,0,str(LANG_PLAYINDICES_PLAYLIST));
|
|
|
|
lcd_puts(0,1,str(LANG_PLAYINDICES_BUFFER));
|
2002-08-06 13:09:48 +00:00
|
|
|
lcd_update();
|
2002-09-12 09:11:59 +00:00
|
|
|
sleep(HZ*2);
|
|
|
|
lcd_clear_display();
|
|
|
|
|
|
|
|
return;
|
2002-08-06 13:09:48 +00:00
|
|
|
}
|
2002-09-12 09:11:59 +00:00
|
|
|
|
|
|
|
/* Update the screen if it takes very long */
|
|
|
|
if(!playlist.in_ram) {
|
|
|
|
if ( current_tick >= next_tick ) {
|
|
|
|
next_tick = current_tick + HZ;
|
2002-09-18 14:08:05 +00:00
|
|
|
snprintf(line, sizeof line,
|
|
|
|
str(LANG_PLAYINDICES_AMOUNT),
|
2002-09-12 09:11:59 +00:00
|
|
|
playlist.amount);
|
|
|
|
lcd_puts(0,1,line);
|
|
|
|
status_draw();
|
|
|
|
lcd_update();
|
|
|
|
}
|
|
|
|
}
|
2002-06-13 15:16:41 +00:00
|
|
|
}
|
2002-06-11 07:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i+= count;
|
2002-06-04 21:43:38 +00:00
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
if(playlist.in_ram)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!playlist.in_ram) {
|
2002-09-18 14:08:05 +00:00
|
|
|
snprintf(line, sizeof line, str(LANG_PLAYINDICES_AMOUNT),
|
|
|
|
playlist.amount);
|
2002-08-20 19:39:55 +00:00
|
|
|
lcd_puts(0,1,line);
|
2002-08-07 10:35:26 +00:00
|
|
|
status_draw();
|
2002-08-06 13:09:48 +00:00
|
|
|
lcd_update();
|
|
|
|
close(fd);
|
|
|
|
}
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* randomly rearrange the array of indices for the playlist
|
|
|
|
*/
|
2002-08-06 13:09:48 +00:00
|
|
|
void randomise_playlist( unsigned int seed )
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
2002-07-22 15:36:39 +00:00
|
|
|
int count;
|
2002-06-04 21:43:38 +00:00
|
|
|
int candidate;
|
|
|
|
int store;
|
|
|
|
|
|
|
|
/* seed with the given seed */
|
2002-07-17 23:07:45 +00:00
|
|
|
srand( seed );
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* randomise entire indices list */
|
2002-08-06 13:09:48 +00:00
|
|
|
for(count = playlist.amount - 1; count >= 0; count--)
|
2002-06-04 21:43:38 +00:00
|
|
|
{
|
|
|
|
/* the rand is from 0 to RAND_MAX, so adjust to our value range */
|
2002-07-22 15:36:39 +00:00
|
|
|
candidate = rand() % (count + 1);
|
2002-06-04 21:43:38 +00:00
|
|
|
|
|
|
|
/* now swap the values at the 'count' and 'candidate' positions */
|
2002-08-06 13:09:48 +00:00
|
|
|
store = playlist.indices[candidate];
|
|
|
|
playlist.indices[candidate] = playlist.indices[count];
|
|
|
|
playlist.indices[count] = store;
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
2002-09-10 08:50:40 +00:00
|
|
|
|
|
|
|
mpeg_flush_and_reload_tracks();
|
2002-06-04 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
2002-08-02 13:20:03 +00:00
|
|
|
static int compare(const void* p1, const void* p2)
|
|
|
|
{
|
|
|
|
int* e1 = (int*) p1;
|
|
|
|
int* e2 = (int*) p2;
|
|
|
|
|
|
|
|
return *e1 - *e2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-08-26 08:25:52 +00:00
|
|
|
* Sort the array of indices for the playlist. If start_current is true then
|
|
|
|
* set the index to the new index of the current song.
|
2002-08-02 13:20:03 +00:00
|
|
|
*/
|
2002-08-26 08:25:52 +00:00
|
|
|
void sort_playlist(bool start_current)
|
2002-08-02 13:20:03 +00:00
|
|
|
{
|
2002-08-26 08:25:52 +00:00
|
|
|
int i;
|
|
|
|
int current = playlist.indices[playlist.index];
|
|
|
|
|
2002-08-06 13:09:48 +00:00
|
|
|
if (playlist.amount > 0)
|
2002-08-02 13:20:03 +00:00
|
|
|
{
|
2002-08-06 13:09:48 +00:00
|
|
|
qsort(&playlist.indices, playlist.amount, sizeof(playlist.indices[0]), compare);
|
2002-08-02 13:20:03 +00:00
|
|
|
}
|
2002-08-26 08:25:52 +00:00
|
|
|
|
|
|
|
if (start_current)
|
|
|
|
{
|
|
|
|
/* Set the index to the current song */
|
|
|
|
for (i=0; i<playlist.amount; i++)
|
|
|
|
{
|
|
|
|
if (playlist.indices[i] == current)
|
|
|
|
{
|
|
|
|
playlist.index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-10 08:50:40 +00:00
|
|
|
|
|
|
|
mpeg_flush_and_reload_tracks();
|
2002-08-02 13:20:03 +00:00
|
|
|
}
|