2007-06-28 10:47:08 -07:00
|
|
|
#include <stdio.h>
|
2006-06-16 12:10:23 -07:00
|
|
|
#include <physfs.h>
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-09-13 14:13:19 -07:00
|
|
|
#include "playlist.h"
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#define BUFFER_SIZE 2048
|
2006-07-17 13:48:39 -07:00
|
|
|
static char buffer[BUFFER_SIZE], ByteBuf='\0';
|
|
|
|
static unsigned int ByteBufPos=0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
#define NB_TRACKS 3
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char** songs;
|
|
|
|
unsigned int nb_songs;
|
|
|
|
unsigned int list_size;
|
|
|
|
BOOL shuffle;
|
|
|
|
} WZ_TRACK;
|
|
|
|
|
|
|
|
static unsigned int current_track = 0;
|
|
|
|
static unsigned int current_song = 0;
|
|
|
|
|
|
|
|
static WZ_TRACK playlist[NB_TRACKS];
|
|
|
|
|
|
|
|
#define CURRENT_TRACK playlist[current_track]
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
void PlayList_Init(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NB_TRACKS; ++i) {
|
2006-09-17 13:39:25 -07:00
|
|
|
playlist[i].songs = (char**)malloc(2*sizeof(char*));
|
2007-06-28 10:47:08 -07:00
|
|
|
playlist[i].list_size = 2;
|
|
|
|
playlist[i].nb_songs = 0;
|
2006-02-18 10:54:37 -08:00
|
|
|
memset( playlist[i].songs, 0, playlist[i].list_size*sizeof(char*) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
void PlayList_Quit(void) {
|
2006-02-18 10:54:37 -08:00
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
for( i = 0; i < NB_TRACKS; ++i ) {
|
|
|
|
for( j = 0; j < playlist[i].list_size; ++j ) {
|
|
|
|
free( playlist[i].songs[j] );
|
|
|
|
}
|
|
|
|
free( playlist[i].songs );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char PlayList_Read(const char* path) {
|
2006-06-16 12:10:23 -07:00
|
|
|
PHYSFS_file * f;
|
2007-06-28 10:47:08 -07:00
|
|
|
char* path_to_music = NULL;
|
|
|
|
|
|
|
|
sprintf(buffer, "%s/music.wpl", path);
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
f = PHYSFS_openRead(buffer);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
while (!PHYSFS_eof(f)) {
|
2007-06-28 10:47:08 -07:00
|
|
|
char* filename;
|
|
|
|
|
2006-07-17 13:48:39 -07:00
|
|
|
while( ByteBufPos < BUFFER_SIZE-1 && PHYSFS_read( f, &ByteBuf, 1, 1 ) && ByteBuf != '\n' )
|
|
|
|
{
|
|
|
|
buffer[ByteBufPos]=ByteBuf;
|
|
|
|
ByteBufPos++;
|
|
|
|
}
|
|
|
|
buffer[ByteBufPos]='\0';
|
|
|
|
ByteBufPos=0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (strncmp(buffer, "[game]", 6) == 0) {
|
|
|
|
current_track = 1;
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = NULL;
|
|
|
|
CURRENT_TRACK.shuffle = FALSE;
|
|
|
|
} else if (strncmp(buffer, "[menu]", 6) == 0) {
|
|
|
|
current_track = 2;
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = NULL;
|
|
|
|
CURRENT_TRACK.shuffle = FALSE;
|
|
|
|
} else if (strncmp(buffer, "path=", 5) == 0) {
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = strtok(buffer+5, "\n");
|
|
|
|
if (strcmp(path_to_music, ".") == 0) {
|
|
|
|
path_to_music = strdup(path);
|
|
|
|
} else {
|
|
|
|
path_to_music = strdup(path_to_music);
|
|
|
|
}
|
2006-07-17 13:48:39 -07:00
|
|
|
debug( LOG_WZ, " path = %s\n", path_to_music );
|
2007-06-28 10:47:08 -07:00
|
|
|
} else if (strncmp(buffer, "shuffle=", 8) == 0) {
|
|
|
|
if (strcmp(strtok(buffer+8, "\n"), "yes") == 0) {
|
|
|
|
CURRENT_TRACK.shuffle = TRUE;
|
|
|
|
}
|
2006-07-17 13:48:39 -07:00
|
|
|
debug( LOG_WZ, " shuffle = yes\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
} else if ( buffer[0] != '\0'
|
|
|
|
&& (filename = strtok(buffer, "\n")) != NULL
|
|
|
|
&& strlen(filename) != 0) {
|
|
|
|
char* filepath;
|
|
|
|
|
|
|
|
if (path_to_music == NULL) {
|
2006-09-17 13:39:25 -07:00
|
|
|
filepath = (char*)malloc(strlen(filename)+1);
|
2007-06-28 10:47:08 -07:00
|
|
|
sprintf(filepath, "%s", filename);
|
|
|
|
} else {
|
2006-09-17 13:39:25 -07:00
|
|
|
filepath = (char*)malloc( strlen(filename)
|
2007-06-28 10:47:08 -07:00
|
|
|
+ strlen(path_to_music)+2);
|
|
|
|
sprintf(filepath, "%s/%s", path_to_music, filename);
|
|
|
|
}
|
2006-07-17 13:48:39 -07:00
|
|
|
debug( LOG_WZ, " adding song %s\n", filepath );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (CURRENT_TRACK.nb_songs == CURRENT_TRACK.list_size) {
|
|
|
|
CURRENT_TRACK.list_size <<= 1;
|
2006-09-17 13:39:25 -07:00
|
|
|
CURRENT_TRACK.songs = (char**)realloc(CURRENT_TRACK.songs,
|
2007-06-28 10:47:08 -07:00
|
|
|
CURRENT_TRACK.list_size*sizeof(char*));
|
|
|
|
}
|
|
|
|
|
|
|
|
CURRENT_TRACK.songs[CURRENT_TRACK.nb_songs++] = filepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path_to_music);
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
PHYSFS_close(f);
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:14:47 -07:00
|
|
|
static void PlayList_Shuffle(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (CURRENT_TRACK.shuffle) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = CURRENT_TRACK.nb_songs-1; i > 0; --i) {
|
|
|
|
unsigned int j = rand() % (i + 1);
|
|
|
|
char* swap = CURRENT_TRACK.songs[j];
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
CURRENT_TRACK.songs[j] = CURRENT_TRACK.songs[i];
|
|
|
|
CURRENT_TRACK.songs[i] = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayList_SetTrack(unsigned int t) {
|
|
|
|
if (t >= 0 && t < NB_TRACKS) {
|
|
|
|
current_track = t;
|
|
|
|
} else {
|
|
|
|
current_track = 0;
|
|
|
|
}
|
|
|
|
PlayList_Shuffle();
|
|
|
|
current_song = 0;
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
char* PlayList_CurrentSong(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (current_song >= CURRENT_TRACK.nb_songs) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return CURRENT_TRACK.songs[current_song];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
char* PlayList_NextSong(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (++current_song >= CURRENT_TRACK.nb_songs) {
|
|
|
|
PlayList_Shuffle();
|
|
|
|
current_song = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CURRENT_TRACK.nb_songs == 0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return CURRENT_TRACK.songs[current_song];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
void PlayList_DeleteCurrentSong(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|