From 8d3205871cf398c4e187bb0d1ead28c46a25a8c1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 4 Feb 2009 18:56:41 +0100 Subject: [PATCH] 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. --- src/Makefile.am | 1 + src/command.c | 81 +++--- src/dbUtils.c | 2 +- src/main.c | 1 - src/playlist.c | 634 +++++++++++++++++++++--------------------- src/playlist.h | 98 ++++--- src/playlist_global.c | 63 +++++ src/playlist_save.c | 6 +- src/playlist_save.h | 2 +- src/playlist_state.c | 11 +- src/update.c | 4 +- 11 files changed, 503 insertions(+), 400 deletions(-) create mode 100644 src/playlist_global.c diff --git a/src/Makefile.am b/src/Makefile.am index b5657510b..4b50c3664 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -182,6 +182,7 @@ mpd_SOURCES = \ player_thread.c \ player_control.c \ playlist.c \ + playlist_global.c \ playlist_print.c \ playlist_save.c \ playlist_state.c \ diff --git a/src/command.c b/src/command.c index 7118e979b..dd76b9b77 100644 --- a/src/command.c +++ b/src/command.c @@ -392,7 +392,7 @@ handle_play(struct client *client, int argc, char *argv[]) if (argc == 2 && !check_int(client, &song, argv[1], need_positive)) return COMMAND_RETURN_ERROR; - result = playPlaylist(song); + result = playPlaylist(&g_playlist, song); return print_playlist_result(client, result); } @@ -405,7 +405,7 @@ handle_playid(struct client *client, int argc, char *argv[]) if (argc == 2 && !check_int(client, &id, argv[1], need_positive)) return COMMAND_RETURN_ERROR; - result = playPlaylistById(id); + result = playPlaylistById(&g_playlist, id); return print_playlist_result(client, result); } @@ -413,7 +413,7 @@ static enum command_return handle_stop(G_GNUC_UNUSED struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - stopPlaylist(); + stopPlaylist(&g_playlist); return COMMAND_RETURN_OK; } @@ -421,8 +421,8 @@ static enum command_return handle_currentsong(struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - int song = getPlaylistCurrentSong(); - const struct queue *queue = playlist_get_queue(); + int song = getPlaylistCurrentSong(&g_playlist); + const struct queue *queue = playlist_get_queue(&g_playlist); if (song >= 0) queue_print_info(client, queue, song, song + 1); @@ -475,19 +475,19 @@ handle_status(struct client *client, COMMAND_STATUS_CROSSFADE ": %i\n" COMMAND_STATUS_STATE ": %s\n", volume_level_get(), - getPlaylistRepeatStatus(), - getPlaylistRandomStatus(), - getPlaylistVersion(), - getPlaylistLength(), + getPlaylistRepeatStatus(&g_playlist), + getPlaylistRandomStatus(&g_playlist), + getPlaylistVersion(&g_playlist), + getPlaylistLength(&g_playlist), (int)(getPlayerCrossFade() + 0.5), state); - song = getPlaylistCurrentSong(); + song = getPlaylistCurrentSong(&g_playlist); if (song >= 0) { client_printf(client, COMMAND_STATUS_SONG ": %i\n" COMMAND_STATUS_SONGID ": %u\n", - song, getPlaylistSongId(song)); + song, getPlaylistSongId(&g_playlist, song)); } if (getPlayerState() != PLAYER_STATE_STOP) { @@ -540,7 +540,8 @@ handle_add(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) #ifdef WIN32 result = PLAYLIST_RESULT_DENIED; #else - result = playlist_append_file(uri + 7, client_get_uid(client), + result = playlist_append_file(&g_playlist, + uri + 7, client_get_uid(client), NULL); #endif return print_playlist_result(client, result); @@ -553,7 +554,7 @@ handle_add(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - return addToPlaylist(uri, NULL); + return addToPlaylist(&g_playlist, uri, NULL); } result = addAllIn(uri); @@ -577,7 +578,7 @@ handle_addid(struct client *client, int argc, char *argv[]) #ifdef WIN32 result = PLAYLIST_RESULT_DENIED; #else - result = playlist_append_file(uri + 7, + result = playlist_append_file(&g_playlist, uri + 7, client_get_uid(client), &added_id); #endif @@ -588,7 +589,7 @@ handle_addid(struct client *client, int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - result = addToPlaylist(uri, &added_id); + result = addToPlaylist(&g_playlist, uri, &added_id); } if (result != PLAYLIST_RESULT_SUCCESS) @@ -598,11 +599,11 @@ handle_addid(struct client *client, int argc, char *argv[]) int to; if (!check_int(client, &to, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = moveSongInPlaylistById(added_id, to); + result = moveSongInPlaylistById(&g_playlist, added_id, to); if (result != PLAYLIST_RESULT_SUCCESS) { enum command_return ret = print_playlist_result(client, result); - deleteFromPlaylistById(added_id); + deleteFromPlaylistById(&g_playlist, added_id); return ret; } } @@ -620,7 +621,7 @@ handle_delete(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) if (!check_int(client, &song, argv[1], need_positive)) return COMMAND_RETURN_ERROR; - result = deleteFromPlaylist(song); + result = deleteFromPlaylist(&g_playlist, song); return print_playlist_result(client, result); } @@ -633,7 +634,7 @@ handle_deleteid(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) if (!check_int(client, &id, argv[1], need_positive)) return COMMAND_RETURN_ERROR; - result = deleteFromPlaylistById(id); + result = deleteFromPlaylistById(&g_playlist, id); return print_playlist_result(client, result); } @@ -641,7 +642,7 @@ static enum command_return handle_playlist(struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - const struct queue *queue = playlist_get_queue(); + const struct queue *queue = playlist_get_queue(&g_playlist); queue_print_uris(client, queue, 0, queue_length(queue)); return COMMAND_RETURN_OK; @@ -651,7 +652,7 @@ static enum command_return handle_shuffle(G_GNUC_UNUSED struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - shufflePlaylist(); + shufflePlaylist(&g_playlist); return COMMAND_RETURN_OK; } @@ -659,7 +660,7 @@ static enum command_return handle_clear(G_GNUC_UNUSED struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - clearPlaylist(); + clearPlaylist(&g_playlist); return COMMAND_RETURN_OK; } @@ -669,7 +670,7 @@ handle_save(struct client *client, { enum playlist_result result; - result = spl_save_queue(argv[1], playlist_get_queue()); + result = spl_save_queue(argv[1], playlist_get_queue(&g_playlist)); return print_playlist_result(client, result); } @@ -678,7 +679,7 @@ handle_load(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) { enum playlist_result result; - result = playlist_load_spl(argv[1]); + result = playlist_load_spl(&g_playlist, argv[1]); return print_playlist_result(client, result); } @@ -761,7 +762,7 @@ static enum command_return handle_plchanges(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) { uint32_t version; - const struct queue *queue = playlist_get_queue(); + const struct queue *queue = playlist_get_queue(&g_playlist); if (!check_uint32(client, &version, argv[1], need_positive)) return COMMAND_RETURN_ERROR; @@ -774,7 +775,7 @@ static enum command_return handle_plchangesposid(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) { uint32_t version; - const struct queue *queue = playlist_get_queue(); + const struct queue *queue = playlist_get_queue(&g_playlist); if (!check_uint32(client, &version, argv[1], need_positive)) return COMMAND_RETURN_ERROR; @@ -787,7 +788,7 @@ static enum command_return handle_playlistinfo(struct client *client, int argc, char *argv[]) { unsigned start = 0, end = UINT_MAX; - const struct queue *queue = playlist_get_queue(); + const struct queue *queue = playlist_get_queue(&g_playlist); if (argc == 2 && !check_range(client, &start, &end, argv[1], need_range)) @@ -809,7 +810,7 @@ handle_playlistid(struct client *client, int argc, char *argv[]) { int id = -1, start; unsigned end; - const struct queue *queue = playlist_get_queue(); + const struct queue *queue = playlist_get_queue(&g_playlist); if (argc == 2 && !check_int(client, &id, argv[1], need_positive)) return COMMAND_RETURN_ERROR; @@ -919,7 +920,7 @@ handle_playlistfind(struct client *client, int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - queue_find(client, playlist_get_queue(), list); + queue_find(client, playlist_get_queue(&g_playlist), list); locate_item_list_free(list); @@ -940,7 +941,7 @@ handle_playlistsearch(struct client *client, int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - queue_search(client, playlist_get_queue(), list); + queue_search(client, playlist_get_queue(&g_playlist), list); locate_item_list_free(list); @@ -1002,7 +1003,7 @@ static enum command_return handle_next(G_GNUC_UNUSED struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - nextSongInPlaylist(); + nextSongInPlaylist(&g_playlist); return COMMAND_RETURN_OK; } @@ -1010,7 +1011,7 @@ static enum command_return handle_previous(G_GNUC_UNUSED struct client *client, G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) { - previousSongInPlaylist(); + previousSongInPlaylist(&g_playlist); return COMMAND_RETURN_OK; } @@ -1077,7 +1078,7 @@ handle_repeat(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - setPlaylistRepeatStatus(status); + setPlaylistRepeatStatus(&g_playlist, status); return COMMAND_RETURN_OK; } @@ -1095,7 +1096,7 @@ handle_random(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; } - setPlaylistRandomStatus(status); + setPlaylistRandomStatus(&g_playlist, status); return COMMAND_RETURN_OK; } @@ -1177,7 +1178,7 @@ handle_move(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; if (!check_int(client, &to, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = moveSongInPlaylist(from, to); + result = moveSongInPlaylist(&g_playlist, from, to); return print_playlist_result(client, result); } @@ -1191,7 +1192,7 @@ handle_moveid(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; if (!check_int(client, &to, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = moveSongInPlaylistById(id, to); + result = moveSongInPlaylistById(&g_playlist, id, to); return print_playlist_result(client, result); } @@ -1205,7 +1206,7 @@ handle_swap(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; if (!check_int(client, &song2, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = swapSongsInPlaylist(song1, song2); + result = swapSongsInPlaylist(&g_playlist, song1, song2); return print_playlist_result(client, result); } @@ -1219,7 +1220,7 @@ handle_swapid(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) return COMMAND_RETURN_ERROR; if (!check_int(client, &id2, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = swapSongsInPlaylistById(id1, id2); + result = swapSongsInPlaylistById(&g_playlist, id1, id2); return print_playlist_result(client, result); } @@ -1234,7 +1235,7 @@ handle_seek(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) if (!check_int(client, &seek_time, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = seekSongInPlaylist(song, seek_time); + result = seekSongInPlaylist(&g_playlist, song, seek_time); return print_playlist_result(client, result); } @@ -1249,7 +1250,7 @@ handle_seekid(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) if (!check_int(client, &seek_time, argv[2], check_integer, argv[2])) return COMMAND_RETURN_ERROR; - result = seekSongInPlaylistById(id, seek_time); + result = seekSongInPlaylistById(&g_playlist, id, seek_time); return print_playlist_result(client, result); } diff --git a/src/dbUtils.c b/src/dbUtils.c index a850bdba8..8b3eee371 100644 --- a/src/dbUtils.c +++ b/src/dbUtils.c @@ -168,7 +168,7 @@ int printAllIn(struct client *client, const char *name) static int directoryAddSongToPlaylist(struct song *song, G_GNUC_UNUSED void *data) { - return addSongToPlaylist(song, NULL); + return addSongToPlaylist(&g_playlist, song, NULL); } struct add_data { diff --git a/src/main.c b/src/main.c index 9675074f9..7df773146 100644 --- a/src/main.c +++ b/src/main.c @@ -235,7 +235,6 @@ int main(int argc, char *argv[]) event_pipe_init(); event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted); - event_pipe_register(PIPE_EVENT_PLAYLIST, syncPlayerAndPlaylist); path_global_init(); mapper_init(); diff --git a/src/playlist.c b/src/playlist.c index 97b6b0fad..0a32ce98b 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -18,7 +18,6 @@ #include "playlist.h" #include "playlist_save.h" -#include "playlist_state.h" #include "queue_print.h" #include "locate.h" #include "player_control.h" @@ -32,7 +31,6 @@ #include "stored_playlist.h" #include "ack.h" #include "idle.h" -#include "event_pipe.h" #include @@ -49,105 +47,87 @@ */ #define PLAYLIST_PREV_UNLESS_ELAPSED 10 -/** the global playlist object */ -static struct playlist playlist; - -static void playPlaylistOrderNumber(int orderNum); - -static void incrPlaylistVersion(void) -{ - queue_increment_version(&playlist.queue); - - idle_add(IDLE_PLAYLIST); -} - -void playlistVersionChange(void) -{ - queue_modify_all(&playlist.queue); - idle_add(IDLE_PLAYLIST); -} - static void -playlist_tag_event(void) +playPlaylistOrderNumber(struct playlist *playlist, int orderNum); + +static void incrPlaylistVersion(struct playlist *playlist) { - if (!playlist.playing) + queue_increment_version(&playlist->queue); + + idle_add(IDLE_PLAYLIST); +} + +void playlistVersionChange(struct playlist *playlist) +{ + queue_modify_all(&playlist->queue); + idle_add(IDLE_PLAYLIST); +} + +void +playlist_tag_changed(struct playlist *playlist) +{ + if (!playlist->playing) return; - assert(playlist.current >= 0); + assert(playlist->current >= 0); - queue_modify(&playlist.queue, playlist.current); + queue_modify(&playlist->queue, playlist->current); idle_add(IDLE_PLAYLIST); } -void initPlaylist(void) +void +playlist_init(struct playlist *playlist) { - queue_init(&playlist.queue, + queue_init(&playlist->queue, config_get_positive(CONF_MAX_PLAYLIST_LENGTH, DEFAULT_PLAYLIST_MAX_LENGTH)); - playlist.queued = -1; - playlist.current = -1; - - event_pipe_register(PIPE_EVENT_TAG, playlist_tag_event); + playlist->queued = -1; + playlist->current = -1; } -void finishPlaylist(void) +void +playlist_finish(struct playlist *playlist) { - queue_finish(&playlist.queue); + queue_finish(&playlist->queue); } -const struct queue * -playlist_get_queue(void) +void clearPlaylist(struct playlist *playlist) { - return &playlist.queue; -} - -void clearPlaylist(void) -{ - stopPlaylist(); + stopPlaylist(playlist); /* make sure there are no references to allocated songs anymore */ - for (unsigned i = 0; i < queue_length(&playlist.queue); i++) { - const struct song *song = queue_get(&playlist.queue, i); + for (unsigned i = 0; i < queue_length(&playlist->queue); i++) { + const struct song *song = queue_get(&playlist->queue, i); if (!song_in_database(song)) pc_song_deleted(song); } - queue_clear(&playlist.queue); + queue_clear(&playlist->queue); - playlist.current = -1; + playlist->current = -1; - incrPlaylistVersion(); -} - -void savePlaylistState(FILE *fp) -{ - playlist_state_save(fp, &playlist); -} - -void readPlaylistState(FILE *fp) -{ - playlist_state_restore(fp, &playlist); + incrPlaylistVersion(playlist); } /** * Queue a song, addressed by its order number. */ static void -playlist_queue_song_order(unsigned order) +playlist_queue_song_order(struct playlist *playlist, unsigned order) { struct song *song; char *uri; - assert(queue_valid_order(&playlist.queue, order)); + assert(queue_valid_order(&playlist->queue, order)); - playlist.queued = order; + playlist->queued = order; - song = queue_get_order(&playlist.queue, order); + song = queue_get_order(&playlist->queue, order); uri = song_get_uri(song); g_debug("playlist: queue song %i:\"%s\"", - playlist.queued, uri); + playlist->queued, uri); g_free(uri); queueSong(song); @@ -157,14 +137,14 @@ playlist_queue_song_order(unsigned order) * Check if the player thread has already started playing the "queued" * song. */ -static void syncPlaylistWithQueue(void) +static void syncPlaylistWithQueue(struct playlist *playlist) { - if (pc.next_song == NULL && playlist.queued != -1) { + if (pc.next_song == NULL && playlist->queued != -1) { /* queued song has started: copy queued to current, and notify the clients */ - playlist.current = playlist.queued; - playlist.queued = -1; + playlist->current = playlist->queued; + playlist->queued = -1; idle_add(IDLE_PLAYER); } @@ -175,12 +155,12 @@ static void syncPlaylistWithQueue(void) * there is none (yet?) or if MPD isn't playing. */ static const struct song * -playlist_get_queued_song(void) +playlist_get_queued_song(struct playlist *playlist) { - if (!playlist.playing || playlist.queued < 0) + if (!playlist->playing || playlist->queued < 0) return NULL; - return queue_get_order(&playlist.queue, playlist.queued); + return queue_get_order(&playlist->queue, playlist->queued); } /** @@ -192,61 +172,62 @@ playlist_get_queued_song(void) * playlist_get_queued_song() */ static void -playlist_update_queued_song(const struct song *prev) +playlist_update_queued_song(struct playlist *playlist, const struct song *prev) { int next_order; const struct song *next_song; - if (!playlist.playing) + if (!playlist->playing) return; - assert(!queue_is_empty(&playlist.queue)); - assert((playlist.queued < 0) == (prev == NULL)); + assert(!queue_is_empty(&playlist->queue)); + assert((playlist->queued < 0) == (prev == NULL)); - next_order = playlist.current >= 0 - ? queue_next_order(&playlist.queue, playlist.current) + next_order = playlist->current >= 0 + ? queue_next_order(&playlist->queue, playlist->current) : 0; - if (next_order == 0 && playlist.queue.random) { + if (next_order == 0 && playlist->queue.random) { /* shuffle the song order again, so we get a different order each time the playlist is played completely */ unsigned current_position = - queue_order_to_position(&playlist.queue, - playlist.current); + queue_order_to_position(&playlist->queue, + playlist->current); - queue_shuffle_order(&playlist.queue); + queue_shuffle_order(&playlist->queue); - /* make sure that the playlist.current still points to + /* make sure that the playlist->current still points to the current song, after the song order has been shuffled */ - playlist.current = - queue_position_to_order(&playlist.queue, + playlist->current = + queue_position_to_order(&playlist->queue, current_position); } if (next_order >= 0) - next_song = queue_get_order(&playlist.queue, next_order); + next_song = queue_get_order(&playlist->queue, next_order); else next_song = NULL; if (prev != NULL && next_song != prev) { /* clear the currently queued song */ pc_cancel(); - playlist.queued = -1; + playlist->queued = -1; } if (next_order >= 0) { if (next_song != prev) - playlist_queue_song_order(next_order); + playlist_queue_song_order(playlist, next_order); else - playlist.queued = next_order; + playlist->queued = next_order; } } #ifndef WIN32 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) { int ret; struct stat st; @@ -268,7 +249,7 @@ playlist_append_file(const char *path, int uid, unsigned *added_id) if (song == NULL) return PLAYLIST_RESULT_NO_SUCH_SONG; - return addSongToPlaylist(song, added_id); + return addSongToPlaylist(playlist, song, added_id); } #endif @@ -287,7 +268,8 @@ song_by_url(const char *url) return NULL; } -enum playlist_result addToPlaylist(const char *url, unsigned *added_id) +enum playlist_result +addToPlaylist(struct playlist *playlist, const char *url, unsigned *added_id) { struct song *song; @@ -297,39 +279,40 @@ enum playlist_result addToPlaylist(const char *url, unsigned *added_id) if (song == NULL) return PLAYLIST_RESULT_NO_SUCH_SONG; - return addSongToPlaylist(song, added_id); + return addSongToPlaylist(playlist, song, added_id); } enum playlist_result -addSongToPlaylist(struct song *song, unsigned *added_id) +addSongToPlaylist(struct playlist *playlist, + struct song *song, unsigned *added_id) { const struct song *queued; unsigned id; - if (queue_is_full(&playlist.queue)) + if (queue_is_full(&playlist->queue)) return PLAYLIST_RESULT_TOO_LARGE; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - id = queue_append(&playlist.queue, song); + id = queue_append(&playlist->queue, song); - if (playlist.queue.random) { + if (playlist->queue.random) { /* shuffle the new song into the list of remaining songs to play */ unsigned start; - if (playlist.queued >= 0) - start = playlist.queued + 1; + if (playlist->queued >= 0) + start = playlist->queued + 1; else - start = playlist.current + 1; - if (start < queue_length(&playlist.queue)) - queue_shuffle_order_last(&playlist.queue, start, - queue_length(&playlist.queue)); + start = playlist->current + 1; + if (start < queue_length(&playlist->queue)) + queue_shuffle_order_last(&playlist->queue, start, + queue_length(&playlist->queue)); } - incrPlaylistVersion(); + incrPlaylistVersion(playlist); - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); if (added_id) *added_id = id; @@ -337,180 +320,185 @@ addSongToPlaylist(struct song *song, unsigned *added_id) return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result swapSongsInPlaylist(unsigned song1, unsigned song2) +enum playlist_result +swapSongsInPlaylist(struct playlist *playlist, unsigned song1, unsigned song2) { const struct song *queued; - if (!queue_valid_position(&playlist.queue, song1) || - !queue_valid_position(&playlist.queue, song2)) + if (!queue_valid_position(&playlist->queue, song1) || + !queue_valid_position(&playlist->queue, song2)) return PLAYLIST_RESULT_BAD_RANGE; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - queue_swap(&playlist.queue, song1, song2); + queue_swap(&playlist->queue, song1, song2); - if (playlist.queue.random) { - /* update the queue order, so that playlist.current + if (playlist->queue.random) { + /* update the queue order, so that playlist->current still points to the current song order */ - queue_swap_order(&playlist.queue, - queue_position_to_order(&playlist.queue, + queue_swap_order(&playlist->queue, + queue_position_to_order(&playlist->queue, song1), - queue_position_to_order(&playlist.queue, + queue_position_to_order(&playlist->queue, song2)); } else { /* correct the "current" song order */ - if (playlist.current == (int)song1) - playlist.current = song2; - else if (playlist.current == (int)song2) - playlist.current = song1; + if (playlist->current == (int)song1) + playlist->current = song2; + else if (playlist->current == (int)song2) + playlist->current = song1; } - incrPlaylistVersion(); + incrPlaylistVersion(playlist); - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result swapSongsInPlaylistById(unsigned id1, unsigned id2) +enum playlist_result +swapSongsInPlaylistById(struct playlist *playlist, unsigned id1, unsigned id2) { - int song1 = queue_id_to_position(&playlist.queue, id1); - int song2 = queue_id_to_position(&playlist.queue, id2); + int song1 = queue_id_to_position(&playlist->queue, id1); + int song2 = queue_id_to_position(&playlist->queue, id2); if (song1 < 0 || song2 < 0) return PLAYLIST_RESULT_NO_SUCH_SONG; - return swapSongsInPlaylist(song1, song2); + return swapSongsInPlaylist(playlist, song1, song2); } -enum playlist_result deleteFromPlaylist(unsigned song) +enum playlist_result +deleteFromPlaylist(struct playlist *playlist, unsigned song) { const struct song *queued; unsigned songOrder; - if (song >= queue_length(&playlist.queue)) + if (song >= queue_length(&playlist->queue)) return PLAYLIST_RESULT_BAD_RANGE; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - songOrder = queue_position_to_order(&playlist.queue, song); + songOrder = queue_position_to_order(&playlist->queue, song); - if (playlist.playing && playlist.current == (int)songOrder) { + if (playlist->playing && playlist->current == (int)songOrder) { bool paused = getPlayerState() == PLAYER_STATE_PAUSE; /* the current song is going to be deleted: stop the player */ playerWait(); - playlist.playing = false; + playlist->playing = false; /* see which song is going to be played instead */ - playlist.current = queue_next_order(&playlist.queue, - playlist.current); - if (playlist.current == (int)songOrder) - playlist.current = -1; + playlist->current = queue_next_order(&playlist->queue, + playlist->current); + if (playlist->current == (int)songOrder) + playlist->current = -1; - if (playlist.current >= 0 && !paused) + if (playlist->current >= 0 && !paused) /* play the song after the deleted one */ - playPlaylistOrderNumber(playlist.current); + playPlaylistOrderNumber(playlist, playlist->current); else /* no songs left to play, stop playback completely */ - stopPlaylist(); + stopPlaylist(playlist); queued = NULL; } /* now do it: remove the song */ - if (!song_in_database(queue_get(&playlist.queue, song))) - pc_song_deleted(queue_get(&playlist.queue, song)); + if (!song_in_database(queue_get(&playlist->queue, song))) + pc_song_deleted(queue_get(&playlist->queue, song)); - queue_delete(&playlist.queue, song); + queue_delete(&playlist->queue, song); - incrPlaylistVersion(); + incrPlaylistVersion(playlist); /* update the "current" and "queued" variables */ - if (playlist.current > (int)songOrder) { - playlist.current--; + if (playlist->current > (int)songOrder) { + playlist->current--; } - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result deleteFromPlaylistById(unsigned id) +enum playlist_result +deleteFromPlaylistById(struct playlist *playlist, unsigned id) { - int song = queue_id_to_position(&playlist.queue, id); + int song = queue_id_to_position(&playlist->queue, id); if (song < 0) return PLAYLIST_RESULT_NO_SUCH_SONG; - return deleteFromPlaylist(song); + return deleteFromPlaylist(playlist, song); } void -deleteASongFromPlaylist(const struct song *song) +deleteASongFromPlaylist(struct playlist *playlist, const struct song *song) { - for (int i = queue_length(&playlist.queue) - 1; i >= 0; --i) - if (song == queue_get(&playlist.queue, i)) - deleteFromPlaylist(i); + for (int i = queue_length(&playlist->queue) - 1; i >= 0; --i) + if (song == queue_get(&playlist->queue, i)) + deleteFromPlaylist(playlist, i); pc_song_deleted(song); } -void stopPlaylist(void) +void stopPlaylist(struct playlist *playlist) { - if (!playlist.playing) + if (!playlist->playing) return; - assert(playlist.current >= 0); + assert(playlist->current >= 0); g_debug("playlist: stop"); playerWait(); - playlist.queued = -1; - playlist.playing = false; + playlist->queued = -1; + playlist->playing = false; - if (playlist.queue.random) { + if (playlist->queue.random) { /* shuffle the playlist, so the next playback will result in a new random order */ unsigned current_position = - queue_order_to_position(&playlist.queue, - playlist.current); + queue_order_to_position(&playlist->queue, + playlist->current); - queue_shuffle_order(&playlist.queue); + queue_shuffle_order(&playlist->queue); /* make sure that "current" stays valid, and the next "play" command plays the same song again */ - playlist.current = - queue_position_to_order(&playlist.queue, + playlist->current = + queue_position_to_order(&playlist->queue, current_position); } } -static void playPlaylistOrderNumber(int orderNum) +static void +playPlaylistOrderNumber(struct playlist *playlist, int orderNum) { struct song *song; char *uri; - playlist.playing = true; - playlist.queued = -1; + playlist->playing = true; + playlist->queued = -1; - song = queue_get_order(&playlist.queue, orderNum); + song = queue_get_order(&playlist->queue, orderNum); uri = song_get_uri(song); g_debug("playlist: play %i:\"%s\"", orderNum, uri); g_free(uri); playerPlay(song); - playlist.current = orderNum; + playlist->current = orderNum; } -enum playlist_result playPlaylist(int song) +enum playlist_result playPlaylist(struct playlist *playlist, int song) { unsigned i = song; @@ -519,10 +507,10 @@ enum playlist_result playPlaylist(int song) if (song == -1) { /* play any song ("current" song, or the first song */ - if (queue_is_empty(&playlist.queue)) + if (queue_is_empty(&playlist->queue)) return PLAYLIST_RESULT_SUCCESS; - if (playlist.playing) { + if (playlist->playing) { /* already playing: unpause playback, just in case it was paused, and return */ playerSetPause(0); @@ -530,61 +518,63 @@ enum playlist_result playPlaylist(int song) } /* select a song: "current" song, or the first one */ - i = playlist.current >= 0 - ? playlist.current + i = playlist->current >= 0 + ? playlist->current : 0; - } else if (!queue_valid_position(&playlist.queue, song)) + } else if (!queue_valid_position(&playlist->queue, song)) return PLAYLIST_RESULT_BAD_RANGE; - if (playlist.queue.random) { + if (playlist->queue.random) { if (song >= 0) /* "i" is currently the song position (which would be equal to the order number in no-random mode); convert it to a order number, because random mode is enabled */ - i = queue_position_to_order(&playlist.queue, song); + i = queue_position_to_order(&playlist->queue, song); - if (!playlist.playing) - playlist.current = 0; + if (!playlist->playing) + playlist->current = 0; /* swap the new song with the previous "current" one, so playback continues as planned */ - queue_swap_order(&playlist.queue, - i, playlist.current); - i = playlist.current; + queue_swap_order(&playlist->queue, + i, playlist->current); + i = playlist->current; } - playlist.stop_on_error = false; - playlist.error_count = 0; + playlist->stop_on_error = false; + playlist->error_count = 0; - playPlaylistOrderNumber(i); + playPlaylistOrderNumber(playlist, i); return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result playPlaylistById(int id) +enum playlist_result +playPlaylistById(struct playlist *playlist, int id) { int song; if (id == -1) { - return playPlaylist(id); + return playPlaylist(playlist, id); } - song = queue_id_to_position(&playlist.queue, id); + song = queue_id_to_position(&playlist->queue, id); if (song < 0) return PLAYLIST_RESULT_NO_SUCH_SONG; - return playPlaylist(song); + return playPlaylist(playlist, song); } -static void playPlaylistIfPlayerStopped(void); +static void +playPlaylistIfPlayerStopped(struct playlist *playlist); /** * This is the "PLAYLIST" event handler. It is invoked by the player * thread whenever it requests a new queued song, or when it exits. */ -void syncPlayerAndPlaylist(void) +void syncPlayerAndPlaylist(struct playlist *playlist) { - if (!playlist.playing) + if (!playlist->playing) /* this event has reached us out of sync: we aren't playing anymore; ignore the event */ return; @@ -594,364 +584,378 @@ void syncPlayerAndPlaylist(void) should be restarted with the next song. That can happen if the playlist isn't filling the queue fast enough */ - playPlaylistIfPlayerStopped(); + playPlaylistIfPlayerStopped(playlist); else { /* check if the player thread has already started playing the queued song */ - syncPlaylistWithQueue(); + syncPlaylistWithQueue(playlist); /* make sure the queued song is always set (if possible) */ if (pc.next_song == NULL) - playlist_update_queued_song(NULL); + playlist_update_queued_song(playlist, NULL); } } -void nextSongInPlaylist(void) +void +nextSongInPlaylist(struct playlist *playlist) { int next_order; - if (!playlist.playing) + if (!playlist->playing) return; - assert(!queue_is_empty(&playlist.queue)); - assert(queue_valid_order(&playlist.queue, playlist.current)); + assert(!queue_is_empty(&playlist->queue)); + assert(queue_valid_order(&playlist->queue, playlist->current)); - syncPlaylistWithQueue(); + syncPlaylistWithQueue(playlist); - playlist.stop_on_error = false; + playlist->stop_on_error = false; /* determine the next song from the queue's order list */ - next_order = queue_next_order(&playlist.queue, playlist.current); + next_order = queue_next_order(&playlist->queue, playlist->current); if (next_order < 0) { /* no song after this one: stop playback */ - stopPlaylist(); + stopPlaylist(playlist); return; } - if (next_order == 0 && playlist.queue.random) { + if (next_order == 0 && playlist->queue.random) { /* The queue told us that the next song is the first song. This means we are in repeat mode. Shuffle the queue order, so this time, the user hears the songs in a different than before */ - assert(playlist.queue.repeat); + assert(playlist->queue.repeat); - queue_shuffle_order(&playlist.queue); + queue_shuffle_order(&playlist->queue); - /* note that playlist.current and playlist.queued are + /* note that playlist->current and playlist->queued are now invalid, but playPlaylistOrderNumber() will discard them anyway */ } - playPlaylistOrderNumber(next_order); + playPlaylistOrderNumber(playlist, next_order); } /** * The player has stopped for some reason. Check the error, and * decide whether to re-start playback */ -static void playPlaylistIfPlayerStopped(void) +static void +playPlaylistIfPlayerStopped(struct playlist *playlist) { enum player_error error; - assert(playlist.playing); + assert(playlist->playing); assert(getPlayerState() == PLAYER_STATE_STOP); error = getPlayerError(); if (error == PLAYER_ERROR_NOERROR) - playlist.error_count = 0; + playlist->error_count = 0; else - ++playlist.error_count; + ++playlist->error_count; - if ((playlist.stop_on_error && error != PLAYER_ERROR_NOERROR) || + if ((playlist->stop_on_error && error != PLAYER_ERROR_NOERROR) || error == PLAYER_ERROR_AUDIO || error == PLAYER_ERROR_SYSTEM || - playlist.error_count >= queue_length(&playlist.queue)) + playlist->error_count >= queue_length(&playlist->queue)) /* too many errors, or critical error: stop playback */ - stopPlaylist(); + stopPlaylist(playlist); else /* continue playback at the next song */ - nextSongInPlaylist(); + nextSongInPlaylist(playlist); } -bool getPlaylistRepeatStatus(void) +bool +getPlaylistRepeatStatus(struct playlist *playlist) { - return playlist.queue.repeat; + return playlist->queue.repeat; } -bool getPlaylistRandomStatus(void) +bool +getPlaylistRandomStatus(struct playlist *playlist) { - return playlist.queue.random; + return playlist->queue.random; } -void setPlaylistRepeatStatus(bool status) +void setPlaylistRepeatStatus(struct playlist *playlist, bool status) { - if (status == playlist.queue.repeat) + if (status == playlist->queue.repeat) return; - playlist.queue.repeat = status; + playlist->queue.repeat = status; /* if the last song is currently being played, the "next song" might change when repeat mode is toggled */ - playlist_update_queued_song(playlist_get_queued_song()); + playlist_update_queued_song(playlist, + playlist_get_queued_song(playlist)); idle_add(IDLE_OPTIONS); } -enum playlist_result moveSongInPlaylist(unsigned from, int to) +enum playlist_result +moveSongInPlaylist(struct playlist *playlist, unsigned from, int to) { const struct song *queued; int currentSong; - if (!queue_valid_position(&playlist.queue, from)) + if (!queue_valid_position(&playlist->queue, from)) return PLAYLIST_RESULT_BAD_RANGE; - if ((to >= 0 && to >= (int)queue_length(&playlist.queue)) || - (to < 0 && abs(to) > (int)queue_length(&playlist.queue))) + if ((to >= 0 && to >= (int)queue_length(&playlist->queue)) || + (to < 0 && abs(to) > (int)queue_length(&playlist->queue))) return PLAYLIST_RESULT_BAD_RANGE; if ((int)from == to) /* no-op */ return PLAYLIST_RESULT_SUCCESS; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); /* * (to < 0) => move to offset from current song * (-playlist.length == to) => move to position BEFORE current song */ - currentSong = playlist.current >= 0 - ? (int)queue_order_to_position(&playlist.queue, - playlist.current) + currentSong = playlist->current >= 0 + ? (int)queue_order_to_position(&playlist->queue, + playlist->current) : -1; - if (to < 0 && playlist.current >= 0) { + if (to < 0 && playlist->current >= 0) { if ((unsigned)currentSong == from) /* no-op, can't be moved to offset of itself */ return PLAYLIST_RESULT_SUCCESS; - to = (currentSong + abs(to)) % queue_length(&playlist.queue); + to = (currentSong + abs(to)) % queue_length(&playlist->queue); } - queue_move(&playlist.queue, from, to); + queue_move(&playlist->queue, from, to); - if (!playlist.queue.random) { + if (!playlist->queue.random) { /* update current/queued */ - if (playlist.current == (int)from) - playlist.current = to; - else if (playlist.current > (int)from && - playlist.current <= to) { - playlist.current--; - } else if (playlist.current >= to && - playlist.current < (int)from) { - playlist.current++; + if (playlist->current == (int)from) + playlist->current = to; + else if (playlist->current > (int)from && + playlist->current <= to) { + playlist->current--; + } else if (playlist->current >= to && + playlist->current < (int)from) { + playlist->current++; } } - incrPlaylistVersion(); + incrPlaylistVersion(playlist); - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result moveSongInPlaylistById(unsigned id1, int to) +enum playlist_result +moveSongInPlaylistById(struct playlist *playlist, unsigned id1, int to) { - int song = queue_id_to_position(&playlist.queue, id1); + int song = queue_id_to_position(&playlist->queue, id1); if (song < 0) return PLAYLIST_RESULT_NO_SUCH_SONG; - return moveSongInPlaylist(song, to); + return moveSongInPlaylist(playlist, song, to); } -static void orderPlaylist(void) +static void orderPlaylist(struct playlist *playlist) { - if (playlist.current >= 0) + if (playlist->current >= 0) /* update playlist.current, order==position now */ - playlist.current = queue_order_to_position(&playlist.queue, - playlist.current); + playlist->current = queue_order_to_position(&playlist->queue, + playlist->current); - queue_restore_order(&playlist.queue); + queue_restore_order(&playlist->queue); } -void setPlaylistRandomStatus(bool status) +void setPlaylistRandomStatus(struct playlist *playlist, bool status) { const struct song *queued; - if (status == playlist.queue.random) + if (status == playlist->queue.random) return; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - playlist.queue.random = status; + playlist->queue.random = status; - if (playlist.queue.random) { + if (playlist->queue.random) { /* shuffle the queue order, but preserve - playlist.current */ + playlist->current */ - int current_position = playlist.current >= 0 - ? (int)queue_order_to_position(&playlist.queue, - playlist.current) + int current_position = playlist->current >= 0 + ? (int)queue_order_to_position(&playlist->queue, + playlist->current) : -1; - queue_shuffle_order(&playlist.queue); + queue_shuffle_order(&playlist->queue); if (current_position >= 0) { /* make sure the current song is the first in the order list, so the whole rest of the playlist is played after that */ unsigned current_order = - queue_position_to_order(&playlist.queue, + queue_position_to_order(&playlist->queue, current_position); - queue_swap_order(&playlist.queue, 0, current_order); - playlist.current = 0; + queue_swap_order(&playlist->queue, 0, current_order); + playlist->current = 0; } } else - orderPlaylist(); + orderPlaylist(playlist); - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); idle_add(IDLE_OPTIONS); } -void previousSongInPlaylist(void) +void previousSongInPlaylist(struct playlist *playlist) { static time_t lastTime; time_t diff = time(NULL) - lastTime; lastTime += diff; - if (!playlist.playing) + if (!playlist->playing) return; - syncPlaylistWithQueue(); + syncPlaylistWithQueue(playlist); if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) { /* re-start playing the current song (just like the "prev" button on CD players) */ - playPlaylistOrderNumber(playlist.current); + playPlaylistOrderNumber(playlist, playlist->current); } else { - if (playlist.current > 0) { + if (playlist->current > 0) { /* play the preceding song */ - playPlaylistOrderNumber(playlist.current - 1); - } else if (playlist.queue.repeat) { + playPlaylistOrderNumber(playlist, + playlist->current - 1); + } else if (playlist->queue.repeat) { /* play the last song in "repeat" mode */ - playPlaylistOrderNumber(queue_length(&playlist.queue) - 1); + playPlaylistOrderNumber(playlist, + queue_length(&playlist->queue) - 1); } else { /* re-start playing the current song if it's the first one */ - playPlaylistOrderNumber(playlist.current); + playPlaylistOrderNumber(playlist, playlist->current); } } } -void shufflePlaylist(void) +void shufflePlaylist(struct playlist *playlist) { const struct song *queued; unsigned i; - if (queue_length(&playlist.queue) <= 1) + if (queue_length(&playlist->queue) <= 1) return; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - if (playlist.playing) { - if (playlist.current >= 0) + if (playlist->playing) { + if (playlist->current >= 0) /* put current playing song first */ - queue_swap(&playlist.queue, 0, - queue_order_to_position(&playlist.queue, - playlist.current)); + queue_swap(&playlist->queue, 0, + queue_order_to_position(&playlist->queue, + playlist->current)); - if (playlist.queue.random) { - playlist.current = - queue_position_to_order(&playlist.queue, 0); + if (playlist->queue.random) { + playlist->current = + queue_position_to_order(&playlist->queue, 0); } else - playlist.current = 0; + playlist->current = 0; /* start shuffle after the current song */ i = 1; } else { /* no playback currently: shuffle everything, and - reset playlist.current */ + reset playlist->current */ i = 0; - playlist.current = -1; + playlist->current = -1; } /* shuffle the rest of the list */ - queue_shuffle_range(&playlist.queue, i, - queue_length(&playlist.queue)); + queue_shuffle_range(&playlist->queue, i, + queue_length(&playlist->queue)); - incrPlaylistVersion(); + incrPlaylistVersion(playlist); - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); } -int getPlaylistCurrentSong(void) +int getPlaylistCurrentSong(struct playlist *playlist) { - if (playlist.current >= 0) - return queue_order_to_position(&playlist.queue, - playlist.current); + if (playlist->current >= 0) + return queue_order_to_position(&playlist->queue, + playlist->current); return -1; } -unsigned long getPlaylistVersion(void) +unsigned long +getPlaylistVersion(struct playlist *playlist) { - return playlist.queue.version; + return playlist->queue.version; } -int getPlaylistLength(void) +int +getPlaylistLength(struct playlist *playlist) { - return queue_length(&playlist.queue); + return queue_length(&playlist->queue); } -enum playlist_result seekSongInPlaylist(unsigned song, float seek_time) +enum playlist_result +seekSongInPlaylist(struct playlist *playlist, unsigned song, float seek_time) { const struct song *queued; unsigned i; int ret; - if (!queue_valid_position(&playlist.queue, song)) + if (!queue_valid_position(&playlist->queue, song)) return PLAYLIST_RESULT_BAD_RANGE; - queued = playlist_get_queued_song(); + queued = playlist_get_queued_song(playlist); - if (playlist.queue.random) - i = queue_position_to_order(&playlist.queue, song); + if (playlist->queue.random) + i = queue_position_to_order(&playlist->queue, song); else i = song; clearPlayerError(); - playlist.stop_on_error = true; - playlist.error_count = 0; + playlist->stop_on_error = true; + playlist->error_count = 0; - if (!playlist.playing || (unsigned)playlist.current != i) { + if (!playlist->playing || (unsigned)playlist->current != i) { /* seeking is not within the current song - first start playing the new song */ - playPlaylistOrderNumber(i); + playPlaylistOrderNumber(playlist, i); queued = NULL; } - playlist_update_queued_song(queued); + playlist_update_queued_song(playlist, queued); - ret = playerSeek(queue_get_order(&playlist.queue, i), seek_time); + ret = playerSeek(queue_get_order(&playlist->queue, i), seek_time); if (ret < 0) return PLAYLIST_RESULT_NOT_PLAYING; return PLAYLIST_RESULT_SUCCESS; } -enum playlist_result seekSongInPlaylistById(unsigned id, float seek_time) +enum playlist_result +seekSongInPlaylistById(struct playlist *playlist, unsigned id, float seek_time) { - int song = queue_id_to_position(&playlist.queue, id); + int song = queue_id_to_position(&playlist->queue, id); if (song < 0) return PLAYLIST_RESULT_NO_SUCH_SONG; - return seekSongInPlaylist(song, seek_time); + return seekSongInPlaylist(playlist, song, seek_time); } -unsigned getPlaylistSongId(unsigned song) +unsigned +getPlaylistSongId(struct playlist *playlist, unsigned song) { - return queue_position_to_id(&playlist.queue, song); + return queue_position_to_id(&playlist->queue, song); } diff --git a/src/playlist.h b/src/playlist.h index 632ddfc76..186ac7548 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -83,21 +83,36 @@ struct playlist { int queued; }; +/** the global playlist object */ +extern struct playlist g_playlist; + void initPlaylist(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. */ -const struct queue * -playlist_get_queue(void); +static inline const struct queue * +playlist_get_queue(const struct playlist *playlist) +{ + return &playlist->queue; +} void readPlaylistState(FILE *); void savePlaylistState(FILE *); -void clearPlaylist(void); +void clearPlaylist(struct playlist *playlist); #ifndef WIN32 /** @@ -105,66 +120,85 @@ void clearPlaylist(void); * but only if the file's owner is equal to the specified uid. */ 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 -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 -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 -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); diff --git a/src/playlist_global.c b/src/playlist_global.c new file mode 100644 index 000000000..f4c918800 --- /dev/null +++ b/src/playlist_global.c @@ -0,0 +1,63 @@ +/* + * 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); +} diff --git a/src/playlist_save.c b/src/playlist_save.c index 41538b620..f1634eb56 100644 --- a/src/playlist_save.c +++ b/src/playlist_save.c @@ -98,7 +98,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue) } enum playlist_result -playlist_load_spl(const char *name_utf8) +playlist_load_spl(struct playlist *playlist, const char *name_utf8) { GPtrArray *list; @@ -108,7 +108,7 @@ playlist_load_spl(const char *name_utf8) for (unsigned i = 0; i < list->len; ++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 */ char *temp2 = g_strdup(temp); char *p = temp2; @@ -117,7 +117,7 @@ playlist_load_spl(const char *name_utf8) *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_free(temp2); diff --git a/src/playlist_save.h b/src/playlist_save.h index 151595755..084ea16ec 100644 --- a/src/playlist_save.h +++ b/src/playlist_save.h @@ -42,6 +42,6 @@ spl_save_queue(const char *name_utf8, const struct queue *queue); * playlist. */ enum playlist_result -playlist_load_spl(const char *name_utf8); +playlist_load_spl(struct playlist *playlist, const char *name_utf8); #endif diff --git a/src/playlist_state.c b/src/playlist_state.c index 7541a353f..1f15ef0b3 100644 --- a/src/playlist_state.c +++ b/src/playlist_state.c @@ -95,13 +95,14 @@ playlist_state_load(FILE *fp, struct playlist *playlist, song = queue_load_song(&playlist->queue, buffer); if (song >= 0 && song == current) { if (state != PLAYER_STATE_STOP) { - playPlaylist(queue_length(&playlist->queue) - 1); + playPlaylist(playlist, queue_length(&playlist->queue) - 1); } if (state == PLAYER_STATE_PAUSE) { playerPause(); } if (state != PLAYER_STATE_STOP) { - seekSongInPlaylist(queue_length(&playlist->queue) - 1, + seekSongInPlaylist(playlist, + queue_length(&playlist->queue) - 1, seek_time); } } @@ -145,9 +146,9 @@ playlist_state_restore(FILE *fp, struct playlist *playlist) if (strcmp (&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]), "1") == 0) { - setPlaylistRepeatStatus(true); + setPlaylistRepeatStatus(playlist, true); } else - setPlaylistRepeatStatus(false); + setPlaylistRepeatStatus(playlist, false); } else if (g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_CROSSFADE)) { setPlayerCrossFade(atoi (& @@ -171,5 +172,5 @@ playlist_state_restore(FILE *fp, struct playlist *playlist) } } - setPlaylistRandomStatus(random_mode); + setPlaylistRandomStatus(playlist, random_mode); } diff --git a/src/update.c b/src/update.c index c8e328403..276ec5a93 100644 --- a/src/update.c +++ b/src/update.c @@ -725,7 +725,7 @@ static void song_delete_event(void) sticker_song_delete(delete); #endif - deleteASongFromPlaylist(delete); + deleteASongFromPlaylist(&g_playlist, delete); delete = NULL; notify_signal(&update_notify); @@ -742,7 +742,7 @@ static void update_finished_event(void) if (modified) { /* send "idle" events */ - playlistVersionChange(); + playlistVersionChange(&g_playlist); idle_add(IDLE_DATABASE); }