diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/playlist.c | 137 | ||||
-rw-r--r-- | src/playlist_state.c | 175 | ||||
-rw-r--r-- | src/playlist_state.h | 37 |
4 files changed, 217 insertions, 134 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index de9c99f1f..b5657510b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -96,6 +96,7 @@ mpd_headers = \ playlist.h \ playlist_print.h \ playlist_save.h \ + playlist_state.h \ queue.h \ queue_print.h \ queue_save.h \ @@ -183,6 +184,7 @@ mpd_SOURCES = \ playlist.c \ playlist_print.c \ playlist_save.c \ + playlist_state.c \ queue.c \ queue_print.c \ queue_save.c \ diff --git a/src/playlist.c b/src/playlist.c index 835f6f277..97b6b0fad 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -18,8 +18,8 @@ #include "playlist.h" #include "playlist_save.h" +#include "playlist_state.h" #include "queue_print.h" -#include "queue_save.h" #include "locate.h" #include "player_control.h" #include "command.h" @@ -29,7 +29,6 @@ #include "conf.h" #include "database.h" #include "mapper.h" -#include "path.h" #include "stored_playlist.h" #include "ack.h" #include "idle.h" @@ -50,21 +49,6 @@ */ #define PLAYLIST_PREV_UNLESS_ELAPSED 10 -#define PLAYLIST_STATE_FILE_STATE "state: " -#define PLAYLIST_STATE_FILE_RANDOM "random: " -#define PLAYLIST_STATE_FILE_REPEAT "repeat: " -#define PLAYLIST_STATE_FILE_CURRENT "current: " -#define PLAYLIST_STATE_FILE_TIME "time: " -#define PLAYLIST_STATE_FILE_CROSSFADE "crossfade: " -#define PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "playlist_begin" -#define PLAYLIST_STATE_FILE_PLAYLIST_END "playlist_end" - -#define PLAYLIST_STATE_FILE_STATE_PLAY "play" -#define PLAYLIST_STATE_FILE_STATE_PAUSE "pause" -#define PLAYLIST_STATE_FILE_STATE_STOP "stop" - -#define PLAYLIST_BUFFER_SIZE 2*MPD_PATH_MAX - /** the global playlist object */ static struct playlist playlist; @@ -139,127 +123,12 @@ void clearPlaylist(void) void savePlaylistState(FILE *fp) { - fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE); - - if (playlist.playing) { - switch (getPlayerState()) { - case PLAYER_STATE_PAUSE: - fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE); - break; - default: - fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY); - } - fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT, - queue_order_to_position(&playlist.queue, - playlist.current)); - fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME, - getPlayerElapsedTime()); - } else - fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP); - - fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, - playlist.queue.random); - fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, - playlist.queue.repeat); - fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE, - (int)(getPlayerCrossFade())); - fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); - queue_save(fp, &playlist.queue); - fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END); -} - -static void loadPlaylistFromStateFile(FILE *fp, char *buffer, - int state, int current, int seek_time) -{ - int song; - - if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { - g_warning("No playlist in state file"); - return; - } - - while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) { - g_strchomp(buffer); - - song = queue_load_song(&playlist.queue, buffer); - if (song >= 0 && song == current) { - if (state != PLAYER_STATE_STOP) { - playPlaylist(queue_length(&playlist.queue) - 1); - } - if (state == PLAYER_STATE_PAUSE) { - playerPause(); - } - if (state != PLAYER_STATE_STOP) { - seekSongInPlaylist(queue_length(&playlist.queue) - 1, - seek_time); - } - } - - if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { - g_warning("'%s' not found in state file", - PLAYLIST_STATE_FILE_PLAYLIST_END); - break; - } - } + playlist_state_save(fp, &playlist); } void readPlaylistState(FILE *fp) { - int current = -1; - int seek_time = 0; - int state = PLAYER_STATE_STOP; - char buffer[PLAYLIST_BUFFER_SIZE]; - bool random_mode = false; - - while (fgets(buffer, sizeof(buffer), fp)) { - g_strchomp(buffer); - - if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_STATE)) { - if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]), - PLAYLIST_STATE_FILE_STATE_PLAY) == 0) { - state = PLAYER_STATE_PLAY; - } else - if (strcmp - (&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]), - PLAYLIST_STATE_FILE_STATE_PAUSE) - == 0) { - state = PLAYER_STATE_PAUSE; - } - } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) { - seek_time = - atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)])); - } else - if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) { - if (strcmp - (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), - "1") == 0) { - setPlaylistRepeatStatus(true); - } else - setPlaylistRepeatStatus(false); - } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { - setPlayerCrossFade(atoi - (& - (buffer - [strlen - (PLAYLIST_STATE_FILE_CROSSFADE)]))); - } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_RANDOM)) { - random_mode = - strcmp(buffer + strlen(PLAYLIST_STATE_FILE_RANDOM), - "1") == 0; - } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) { - current = atoi(&(buffer - [strlen - (PLAYLIST_STATE_FILE_CURRENT)])); - } else if (g_str_has_prefix(buffer, - PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { - if (state == PLAYER_STATE_STOP) - current = -1; - loadPlaylistFromStateFile(fp, buffer, state, - current, seek_time); - } - } - - setPlaylistRandomStatus(random_mode); + playlist_state_restore(fp, &playlist); } /** diff --git a/src/playlist_state.c b/src/playlist_state.c new file mode 100644 index 000000000..7541a353f --- /dev/null +++ b/src/playlist_state.c @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Saving and loading the playlist to/from the state file. + * + */ + +#include "playlist_state.h" +#include "playlist.h" +#include "player_control.h" +#include "queue_save.h" +#include "path.h" + +#include <string.h> +#include <stdlib.h> + +#define PLAYLIST_STATE_FILE_STATE "state: " +#define PLAYLIST_STATE_FILE_RANDOM "random: " +#define PLAYLIST_STATE_FILE_REPEAT "repeat: " +#define PLAYLIST_STATE_FILE_CURRENT "current: " +#define PLAYLIST_STATE_FILE_TIME "time: " +#define PLAYLIST_STATE_FILE_CROSSFADE "crossfade: " +#define PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "playlist_begin" +#define PLAYLIST_STATE_FILE_PLAYLIST_END "playlist_end" + +#define PLAYLIST_STATE_FILE_STATE_PLAY "play" +#define PLAYLIST_STATE_FILE_STATE_PAUSE "pause" +#define PLAYLIST_STATE_FILE_STATE_STOP "stop" + +#define PLAYLIST_BUFFER_SIZE 2*MPD_PATH_MAX + +void +playlist_state_save(FILE *fp, const struct playlist *playlist) +{ + fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE); + + if (playlist->playing) { + switch (getPlayerState()) { + case PLAYER_STATE_PAUSE: + fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE); + break; + default: + fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY); + } + fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT, + queue_order_to_position(&playlist->queue, + playlist->current)); + fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME, + getPlayerElapsedTime()); + } else + fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP); + + fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, + playlist->queue.random); + fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, + playlist->queue.repeat); + fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE, + (int)(getPlayerCrossFade())); + fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); + queue_save(fp, &playlist->queue); + fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END); +} + +static void +playlist_state_load(FILE *fp, struct playlist *playlist, + char *buffer, + int state, int current, int seek_time) +{ + int song; + + if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { + g_warning("No playlist in state file"); + return; + } + + while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) { + g_strchomp(buffer); + + song = queue_load_song(&playlist->queue, buffer); + if (song >= 0 && song == current) { + if (state != PLAYER_STATE_STOP) { + playPlaylist(queue_length(&playlist->queue) - 1); + } + if (state == PLAYER_STATE_PAUSE) { + playerPause(); + } + if (state != PLAYER_STATE_STOP) { + seekSongInPlaylist(queue_length(&playlist->queue) - 1, + seek_time); + } + } + + if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { + g_warning("'%s' not found in state file", + PLAYLIST_STATE_FILE_PLAYLIST_END); + break; + } + } +} + +void +playlist_state_restore(FILE *fp, struct playlist *playlist) +{ + int current = -1; + int seek_time = 0; + int state = PLAYER_STATE_STOP; + char buffer[PLAYLIST_BUFFER_SIZE]; + bool random_mode = false; + + while (fgets(buffer, sizeof(buffer), fp)) { + g_strchomp(buffer); + + if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_STATE)) { + if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]), + PLAYLIST_STATE_FILE_STATE_PLAY) == 0) { + state = PLAYER_STATE_PLAY; + } else + if (strcmp + (&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]), + PLAYLIST_STATE_FILE_STATE_PAUSE) + == 0) { + state = PLAYER_STATE_PAUSE; + } + } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_TIME)) { + seek_time = + atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)])); + } else + if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_REPEAT)) { + if (strcmp + (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), + "1") == 0) { + setPlaylistRepeatStatus(true); + } else + setPlaylistRepeatStatus(false); + } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { + setPlayerCrossFade(atoi + (& + (buffer + [strlen + (PLAYLIST_STATE_FILE_CROSSFADE)]))); + } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_RANDOM)) { + random_mode = + strcmp(buffer + strlen(PLAYLIST_STATE_FILE_RANDOM), + "1") == 0; + } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CURRENT)) { + current = atoi(&(buffer + [strlen + (PLAYLIST_STATE_FILE_CURRENT)])); + } else if (g_str_has_prefix(buffer, + PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { + if (state == PLAYER_STATE_STOP) + current = -1; + playlist_state_load(fp, playlist, buffer, state, + current, seek_time); + } + } + + setPlaylistRandomStatus(random_mode); +} diff --git a/src/playlist_state.h b/src/playlist_state.h new file mode 100644 index 000000000..07474c8a1 --- /dev/null +++ b/src/playlist_state.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Saving and loading the playlist to/from the state file. + * + */ + +#ifndef PLAYLIST_STATE_H +#define PLAYLIST_STATE_H + +#include <stdio.h> + +struct playlist; + +void +playlist_state_save(FILE *fp, const struct playlist *playlist); + +void +playlist_state_restore(FILE *fp, struct playlist *playlist); + +#endif |