2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2009-02-10 10:01:48 -08:00
|
|
|
Copyright (C) 2005-2009 Warzone Resurrection Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2008-03-16 05:39:08 -07:00
|
|
|
#include "lib/framework/file.h"
|
2009-02-10 09:23:09 -08:00
|
|
|
#include "lib/framework/string_ext.h"
|
|
|
|
#include "lib/framework/stdio_ext.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-09-13 14:13:19 -07:00
|
|
|
#include "playlist.h"
|
2008-02-04 08:46:55 -08:00
|
|
|
#include "cdaudio.h"
|
2006-09-13 14:13:19 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#define BUFFER_SIZE 2048
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
typedef struct _wzTrack
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
struct _wzTrack *next;
|
2007-06-28 10:47:08 -07:00
|
|
|
} WZ_TRACK;
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
static WZ_TRACK *currentSong = NULL;
|
|
|
|
static int numSongs = 0;
|
|
|
|
static WZ_TRACK *songList = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-04 08:14:24 -08:00
|
|
|
void PlayList_Init()
|
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
songList = NULL;
|
|
|
|
currentSong = NULL;
|
|
|
|
numSongs = 0;
|
2006-02-18 10:54:37 -08:00
|
|
|
}
|
|
|
|
|
2007-11-04 08:14:24 -08:00
|
|
|
void PlayList_Quit()
|
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
WZ_TRACK *list = songList;
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
while (list)
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
WZ_TRACK *next = list->next;
|
2007-11-04 08:14:24 -08:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
free(list);
|
|
|
|
list = next;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-06-21 08:40:56 -07:00
|
|
|
|
|
|
|
PlayList_Init();
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-03-16 05:39:08 -07:00
|
|
|
bool PlayList_Read(const char* path)
|
2007-01-10 14:50:09 -08:00
|
|
|
{
|
2008-06-21 14:51:26 -07:00
|
|
|
WZ_TRACK** last = &songList;
|
2007-11-04 07:24:37 -08:00
|
|
|
PHYSFS_file* fileHandle;
|
2008-06-21 08:40:56 -07:00
|
|
|
char listName[PATH_MAX];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-04 07:24:37 -08:00
|
|
|
// Construct file name
|
2008-06-21 08:40:56 -07:00
|
|
|
ssprintf(listName, "%s/music.wpl", path);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-04 07:24:37 -08:00
|
|
|
// Attempt to open the playlist file
|
2008-06-21 08:40:56 -07:00
|
|
|
fileHandle = PHYSFS_openRead(listName);
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(listName), listName);
|
2007-11-04 07:24:37 -08:00
|
|
|
if (fileHandle == NULL)
|
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_ERROR, "PHYSFS_openRead(\"%s\") failed with error: %s\n", listName, PHYSFS_getLastError());
|
2007-11-04 07:24:37 -08:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-06-21 14:51:26 -07:00
|
|
|
// Find the end of the songList
|
|
|
|
for (; *last; last = &(*last)->next);
|
|
|
|
|
2007-11-04 07:24:37 -08:00
|
|
|
while (!PHYSFS_eof(fileHandle))
|
|
|
|
{
|
2008-06-21 14:51:29 -07:00
|
|
|
WZ_TRACK* song;
|
2008-06-21 14:51:21 -07:00
|
|
|
char filename[BUFFER_SIZE];
|
2007-11-04 07:24:37 -08:00
|
|
|
size_t buf_pos = 0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 14:51:21 -07:00
|
|
|
// Read a single line
|
|
|
|
while (buf_pos < sizeof(filename) - 1
|
|
|
|
&& PHYSFS_read(fileHandle, &filename[buf_pos], 1, 1)
|
|
|
|
&& filename[buf_pos] != '\n'
|
|
|
|
&& filename[buf_pos] != '\r')
|
2006-07-17 13:48:39 -07:00
|
|
|
{
|
2007-11-04 07:24:37 -08:00
|
|
|
++buf_pos;
|
2006-07-17 13:48:39 -07:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 14:51:21 -07:00
|
|
|
// Nul-terminate string, and trim line endings ('\n' and '\r')
|
|
|
|
filename[buf_pos] = '\0';
|
2007-11-04 07:24:37 -08:00
|
|
|
|
2008-06-21 14:51:29 -07:00
|
|
|
// Don't add empty filenames to the playlist
|
|
|
|
if (filename[0] == '\0' /* strlen(filename) == 0 */)
|
2007-11-04 07:24:37 -08:00
|
|
|
{
|
2008-06-21 14:51:29 -07:00
|
|
|
continue;
|
|
|
|
}
|
2008-06-21 08:40:56 -07:00
|
|
|
|
2008-06-21 14:51:29 -07:00
|
|
|
song = malloc(sizeof(*songList));
|
2008-06-21 14:51:35 -07:00
|
|
|
if (song == NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "Out of memory!");
|
|
|
|
PHYSFS_close(fileHandle);
|
|
|
|
abort();
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 14:51:29 -07:00
|
|
|
sstrcpy(song->path, path);
|
|
|
|
sstrcat(song->path, "/");
|
|
|
|
sstrcat(song->path, filename);
|
|
|
|
song->next = NULL;
|
2007-11-04 08:05:11 -08:00
|
|
|
|
2008-06-21 14:51:29 -07:00
|
|
|
// Append this song to the list
|
|
|
|
*last = song;
|
|
|
|
last = &song->next;
|
|
|
|
|
|
|
|
numSongs++;
|
|
|
|
debug(LOG_SOUND, "Added song %s to playlist", filename);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-11-04 07:24:37 -08:00
|
|
|
PHYSFS_close(fileHandle);
|
2008-06-21 08:40:56 -07:00
|
|
|
currentSong = songList;
|
2006-06-16 12:10:23 -07:00
|
|
|
|
2007-11-04 07:24:37 -08:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
const char* PlayList_CurrentSong()
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
if (currentSong)
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
return currentSong->path;
|
2007-11-04 08:14:24 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
return NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
const char* PlayList_NextSong()
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-25 13:15:20 -07:00
|
|
|
// If there's a next song in the playlist select it
|
|
|
|
if (currentSong
|
|
|
|
&& currentSong->next)
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
currentSong = currentSong->next;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-06-25 13:15:20 -07:00
|
|
|
// Otherwise jump to the start of the playlist
|
2008-06-25 07:01:18 -07:00
|
|
|
else
|
2008-06-21 08:40:56 -07:00
|
|
|
{
|
|
|
|
currentSong = songList;
|
|
|
|
}
|
2008-06-25 07:01:18 -07:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
return PlayList_CurrentSong();
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
void playListTest()
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-05-13 13:03:44 -07:00
|
|
|
for (i = 0; i < 10; i++)
|
2007-11-04 08:14:24 -08:00
|
|
|
{
|
2008-07-09 09:19:57 -07:00
|
|
|
const char *cur, *next;
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
PlayList_Quit();
|
|
|
|
PlayList_Init();
|
|
|
|
PlayList_Read("music");
|
|
|
|
if (numSongs != 2)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "Use the default playlist for selftest!");
|
|
|
|
}
|
2008-07-09 09:19:57 -07:00
|
|
|
cur = PlayList_CurrentSong();
|
|
|
|
next = PlayList_NextSong();
|
|
|
|
assert(cur != NULL && next != NULL && cur != next);
|
|
|
|
next = PlayList_NextSong();
|
|
|
|
assert(cur == next); // loop around
|
2008-06-21 08:40:56 -07:00
|
|
|
assert(songList);
|
|
|
|
assert(numSongs == 2);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-07-09 12:07:42 -07:00
|
|
|
fprintf(stdout, "\tPlaylist self-test: PASSED\n");
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|