Commit 8d320587 authored by Max Kellermann's avatar Max Kellermann

playlist: renamed global "playlist" variable to "g_playlist"

Export the "g_playlist" variable, and pass it to all playlist functions. This way, we can split playlist.c easier into separate parts. The code which initializes the singleton variable is moved to playlist_global.c.
parent 1720c709
...@@ -182,6 +182,7 @@ mpd_SOURCES = \ ...@@ -182,6 +182,7 @@ mpd_SOURCES = \
player_thread.c \ player_thread.c \
player_control.c \ player_control.c \
playlist.c \ playlist.c \
playlist_global.c \
playlist_print.c \ playlist_print.c \
playlist_save.c \ playlist_save.c \
playlist_state.c \ playlist_state.c \
......
...@@ -168,7 +168,7 @@ int printAllIn(struct client *client, const char *name) ...@@ -168,7 +168,7 @@ int printAllIn(struct client *client, const char *name)
static int static int
directoryAddSongToPlaylist(struct song *song, G_GNUC_UNUSED void *data) directoryAddSongToPlaylist(struct song *song, G_GNUC_UNUSED void *data)
{ {
return addSongToPlaylist(song, NULL); return addSongToPlaylist(&g_playlist, song, NULL);
} }
struct add_data { struct add_data {
......
...@@ -235,7 +235,6 @@ int main(int argc, char *argv[]) ...@@ -235,7 +235,6 @@ int main(int argc, char *argv[])
event_pipe_init(); event_pipe_init();
event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted); event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted);
event_pipe_register(PIPE_EVENT_PLAYLIST, syncPlayerAndPlaylist);
path_global_init(); path_global_init();
mapper_init(); mapper_init();
......
...@@ -83,21 +83,36 @@ struct playlist { ...@@ -83,21 +83,36 @@ struct playlist {
int queued; int queued;
}; };
/** the global playlist object */
extern struct playlist g_playlist;
void initPlaylist(void); void initPlaylist(void);
void finishPlaylist(void); void finishPlaylist(void);
void
playlist_init(struct playlist *playlist);
void
playlist_finish(struct playlist *playlist);
void
playlist_tag_changed(struct playlist *playlist);
/** /**
* Returns the "queue" object of the global playlist instance. * Returns the "queue" object of the global playlist instance.
*/ */
const struct queue * static inline const struct queue *
playlist_get_queue(void); playlist_get_queue(const struct playlist *playlist)
{
return &playlist->queue;
}
void readPlaylistState(FILE *); void readPlaylistState(FILE *);
void savePlaylistState(FILE *); void savePlaylistState(FILE *);
void clearPlaylist(void); void clearPlaylist(struct playlist *playlist);
#ifndef WIN32 #ifndef WIN32
/** /**
...@@ -105,66 +120,85 @@ void clearPlaylist(void); ...@@ -105,66 +120,85 @@ void clearPlaylist(void);
* but only if the file's owner is equal to the specified uid. * but only if the file's owner is equal to the specified uid.
*/ */
enum playlist_result enum playlist_result
playlist_append_file(const char *path, int uid, unsigned *added_id); playlist_append_file(struct playlist *playlist, const char *path, int uid,
unsigned *added_id);
#endif #endif
enum playlist_result addToPlaylist(const char *file, unsigned *added_id); enum playlist_result
addToPlaylist(struct playlist *playlist, const char *file, unsigned *added_id);
enum playlist_result enum playlist_result
addSongToPlaylist(struct song *song, unsigned *added_id); addSongToPlaylist(struct playlist *playlist,
struct song *song, unsigned *added_id);
enum playlist_result deleteFromPlaylist(unsigned song); enum playlist_result
deleteFromPlaylist(struct playlist *playlist, unsigned song);
enum playlist_result deleteFromPlaylistById(unsigned song); enum playlist_result
deleteFromPlaylistById(struct playlist *playlist, unsigned song);
void stopPlaylist(void); void stopPlaylist(struct playlist *playlist);
enum playlist_result playPlaylist(int song); enum playlist_result
playPlaylist(struct playlist *playlist, int song);
enum playlist_result playPlaylistById(int song); enum playlist_result
playPlaylistById(struct playlist *playlist, int song);
void nextSongInPlaylist(void); void nextSongInPlaylist(struct playlist *playlist);
void syncPlayerAndPlaylist(void); void syncPlayerAndPlaylist(struct playlist *playlist);
void previousSongInPlaylist(void); void previousSongInPlaylist(struct playlist *playlist);
void shufflePlaylist(void); void shufflePlaylist(struct playlist *playlist);
enum playlist_result savePlaylist(const char *utf8file); enum playlist_result
savePlaylist(struct playlist *playlist, const char *utf8file);
void void
deleteASongFromPlaylist(const struct song *song); deleteASongFromPlaylist(struct playlist *playlist, const struct song *song);
enum playlist_result moveSongInPlaylist(unsigned from, int to); enum playlist_result
moveSongInPlaylist(struct playlist *playlist, unsigned from, int to);
enum playlist_result moveSongInPlaylistById(unsigned id, int to); enum playlist_result
moveSongInPlaylistById(struct playlist *playlist, unsigned id, int to);
enum playlist_result swapSongsInPlaylist(unsigned song1, unsigned song2); enum playlist_result
swapSongsInPlaylist(struct playlist *playlist, unsigned song1, unsigned song2);
enum playlist_result swapSongsInPlaylistById(unsigned id1, unsigned id2); enum playlist_result
swapSongsInPlaylistById(struct playlist *playlist, unsigned id1, unsigned id2);
bool getPlaylistRepeatStatus(void); bool
getPlaylistRepeatStatus(struct playlist *playlist);
void setPlaylistRepeatStatus(bool status); void setPlaylistRepeatStatus(struct playlist *playlist, bool status);
bool getPlaylistRandomStatus(void); bool
getPlaylistRandomStatus(struct playlist *playlist);
void setPlaylistRandomStatus(bool status); void setPlaylistRandomStatus(struct playlist *playlist, bool status);
int getPlaylistCurrentSong(void); int getPlaylistCurrentSong(struct playlist *playlist);
unsigned getPlaylistSongId(unsigned song); unsigned
getPlaylistSongId(struct playlist *playlist, unsigned song);
int getPlaylistLength(void); int getPlaylistLength(struct playlist *playlist);
unsigned long getPlaylistVersion(void); unsigned long
getPlaylistVersion(struct playlist *playlist);
enum playlist_result seekSongInPlaylist(unsigned song, float seek_time); enum playlist_result
seekSongInPlaylist(struct playlist *playlist, unsigned song, float seek_time);
enum playlist_result seekSongInPlaylistById(unsigned id, float seek_time); enum playlist_result
seekSongInPlaylistById(struct playlist *playlist,
unsigned id, float seek_time);
void playlistVersionChange(void); void playlistVersionChange(struct playlist *playlist);
int is_valid_playlist_name(const char *utf8path); int is_valid_playlist_name(const char *utf8path);
......
/*
* 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
*/
/*
* The manager of the global "struct playlist" instance (g_playlist).
*
*/
#include "playlist.h"
#include "playlist_state.h"
#include "event_pipe.h"
struct playlist g_playlist;
static void
playlist_tag_event(void)
{
playlist_tag_changed(&g_playlist);
}
static void
playlist_event(void)
{
syncPlayerAndPlaylist(&g_playlist);
}
void initPlaylist(void)
{
playlist_init(&g_playlist);
event_pipe_register(PIPE_EVENT_TAG, playlist_tag_event);
event_pipe_register(PIPE_EVENT_PLAYLIST, playlist_event);
}
void finishPlaylist(void)
{
playlist_finish(&g_playlist);
}
void savePlaylistState(FILE *fp)
{
playlist_state_save(fp, &g_playlist);
}
void readPlaylistState(FILE *fp)
{
playlist_state_restore(fp, &g_playlist);
}
...@@ -98,7 +98,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue) ...@@ -98,7 +98,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue)
} }
enum playlist_result enum playlist_result
playlist_load_spl(const char *name_utf8) playlist_load_spl(struct playlist *playlist, const char *name_utf8)
{ {
GPtrArray *list; GPtrArray *list;
...@@ -108,7 +108,7 @@ playlist_load_spl(const char *name_utf8) ...@@ -108,7 +108,7 @@ playlist_load_spl(const char *name_utf8)
for (unsigned i = 0; i < list->len; ++i) { for (unsigned i = 0; i < list->len; ++i) {
const char *temp = g_ptr_array_index(list, i); const char *temp = g_ptr_array_index(list, i);
if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { if ((addToPlaylist(playlist, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
/* for windows compatibility, convert slashes */ /* for windows compatibility, convert slashes */
char *temp2 = g_strdup(temp); char *temp2 = g_strdup(temp);
char *p = temp2; char *p = temp2;
...@@ -117,7 +117,7 @@ playlist_load_spl(const char *name_utf8) ...@@ -117,7 +117,7 @@ playlist_load_spl(const char *name_utf8)
*p = '/'; *p = '/';
p++; p++;
} }
if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { if ((addToPlaylist(playlist, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
g_warning("can't add file \"%s\"", temp2); g_warning("can't add file \"%s\"", temp2);
} }
g_free(temp2); g_free(temp2);
......
...@@ -42,6 +42,6 @@ spl_save_queue(const char *name_utf8, const struct queue *queue); ...@@ -42,6 +42,6 @@ spl_save_queue(const char *name_utf8, const struct queue *queue);
* playlist. * playlist.
*/ */
enum playlist_result enum playlist_result
playlist_load_spl(const char *name_utf8); playlist_load_spl(struct playlist *playlist, const char *name_utf8);
#endif #endif
...@@ -95,13 +95,14 @@ playlist_state_load(FILE *fp, struct playlist *playlist, ...@@ -95,13 +95,14 @@ playlist_state_load(FILE *fp, struct playlist *playlist,
song = queue_load_song(&playlist->queue, buffer); song = queue_load_song(&playlist->queue, buffer);
if (song >= 0 && song == current) { if (song >= 0 && song == current) {
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
playPlaylist(queue_length(&playlist->queue) - 1); playPlaylist(playlist, queue_length(&playlist->queue) - 1);
} }
if (state == PLAYER_STATE_PAUSE) { if (state == PLAYER_STATE_PAUSE) {
playerPause(); playerPause();
} }
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
seekSongInPlaylist(queue_length(&playlist->queue) - 1, seekSongInPlaylist(playlist,
queue_length(&playlist->queue) - 1,
seek_time); seek_time);
} }
} }
...@@ -145,9 +146,9 @@ playlist_state_restore(FILE *fp, struct playlist *playlist) ...@@ -145,9 +146,9 @@ playlist_state_restore(FILE *fp, struct playlist *playlist)
if (strcmp if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) { "1") == 0) {
setPlaylistRepeatStatus(true); setPlaylistRepeatStatus(playlist, true);
} else } else
setPlaylistRepeatStatus(false); setPlaylistRepeatStatus(playlist, false);
} else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) {
setPlayerCrossFade(atoi setPlayerCrossFade(atoi
(& (&
...@@ -171,5 +172,5 @@ playlist_state_restore(FILE *fp, struct playlist *playlist) ...@@ -171,5 +172,5 @@ playlist_state_restore(FILE *fp, struct playlist *playlist)
} }
} }
setPlaylistRandomStatus(random_mode); setPlaylistRandomStatus(playlist, random_mode);
} }
...@@ -725,7 +725,7 @@ static void song_delete_event(void) ...@@ -725,7 +725,7 @@ static void song_delete_event(void)
sticker_song_delete(delete); sticker_song_delete(delete);
#endif #endif
deleteASongFromPlaylist(delete); deleteASongFromPlaylist(&g_playlist, delete);
delete = NULL; delete = NULL;
notify_signal(&update_notify); notify_signal(&update_notify);
...@@ -742,7 +742,7 @@ static void update_finished_event(void) ...@@ -742,7 +742,7 @@ static void update_finished_event(void)
if (modified) { if (modified) {
/* send "idle" events */ /* send "idle" events */
playlistVersionChange(); playlistVersionChange(&g_playlist);
idle_add(IDLE_DATABASE); idle_add(IDLE_DATABASE);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment