From ba6e36186aa8c6bd13d4162d6e16125b6616ace3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 4 Oct 2009 01:18:06 +0200 Subject: [PATCH] mpdclient: wrap access in mpdclient_get_connection() Added mpdclient_put_connection() to release the connection before control is returned to the GLib main loop. --- src/main.c | 9 +- src/mpdclient.c | 198 ++++++++++++++++++++++++------------------- src/mpdclient.h | 6 ++ src/player_command.c | 40 ++++++--- src/screen_artist.c | 4 +- src/screen_browser.c | 8 +- src/screen_client.c | 24 ++++-- src/screen_file.c | 5 +- src/screen_outputs.c | 17 ++-- src/screen_play.c | 20 +++-- src/screen_search.c | 7 +- src/screen_song.c | 7 +- src/utils.c | 2 +- 13 files changed, 208 insertions(+), 139 deletions(-) diff --git a/src/main.c b/src/main.c index fa68e8c..e5afa35 100644 --- a/src/main.c +++ b/src/main.c @@ -214,6 +214,7 @@ do_mpd_update(void) screen_update(mpd); mpd->events = 0; + mpdclient_put_connection(mpd); check_reconnect(); } @@ -225,6 +226,7 @@ static gboolean timer_reconnect(G_GNUC_UNUSED gpointer data) { bool success; + struct mpd_connection *connection; assert(!mpdclient_is_connected(mpd)); @@ -246,11 +248,13 @@ timer_reconnect(G_GNUC_UNUSED gpointer data) return FALSE; } + connection = mpdclient_get_connection(mpd); + #ifndef NCMPC_MINI /* quit if mpd is pre 0.11.0 - song id not supported by mpd */ - if (mpd_connection_cmp_server_version(mpd->connection, 0, 12, 0) < 0) { + if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) { const unsigned *version = - mpd_connection_get_server_version(mpd->connection); + mpd_connection_get_server_version(connection); screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"), version[0], version[1], version[2], "0.12.0"); @@ -312,6 +316,7 @@ void end_input_event(void) screen_update(mpd); mpd->events = 0; + mpdclient_put_connection(mpd); check_reconnect(); auto_update_timer(); } diff --git a/src/mpdclient.c b/src/mpdclient.c index 1abc529..54ce2c7 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -34,13 +34,6 @@ #define BUFSIZE 1024 -static bool -MPD_ERROR(const struct mpdclient *client) -{ - return !mpdclient_is_connected(client) || - mpd_connection_get_error(client->connection) != MPD_ERROR_SUCCESS; -} - /* sort by list-format */ gint compare_filelistentry_format(gconstpointer filelist_entry1, @@ -171,11 +164,12 @@ mpdclient_connect(struct mpdclient *c, bool mpdclient_update(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); bool retval; c->volume = -1; - if (MPD_ERROR(c)) + if (connection == NULL) return false; /* always announce these options as long as we don't have real @@ -187,7 +181,7 @@ mpdclient_update(struct mpdclient *c) mpd_status_free(c->status); /* retrieve new status */ - c->status = mpd_run_status(c->connection); + c->status = mpd_run_status(connection); if (c->status == NULL) return mpdclient_handle_error(c); @@ -225,6 +219,17 @@ mpdclient_update(struct mpdclient *c) return retval; } +struct mpd_connection * +mpdclient_get_connection(struct mpdclient *c) +{ + return c->connection; +} + +void +mpdclient_put_connection(G_GNUC_UNUSED struct mpdclient *c) +{ +} + /****************************************************************************/ /*** MPD Commands **********************************************************/ @@ -233,15 +238,16 @@ mpdclient_update(struct mpdclient *c) bool mpdclient_cmd_play(struct mpdclient *c, gint idx) { + struct mpd_connection *connection = mpdclient_get_connection(c); const struct mpd_song *song = playlist_get_song(&c->playlist, idx); - if (MPD_ERROR(c)) + if (connection == NULL) return false; if (song) - mpd_send_play_id(c->connection, mpd_song_get_id(song)); + mpd_send_play_id(connection, mpd_song_get_id(song)); else - mpd_send_play(c->connection); + mpd_send_play(connection); return mpdclient_finish_command(c); } @@ -249,14 +255,15 @@ mpdclient_cmd_play(struct mpdclient *c, gint idx) bool mpdclient_cmd_crop(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_status *status; bool playing; int length, current; - if (MPD_ERROR(c)) + if (connection == NULL) return false; - status = mpd_run_status(c->connection); + status = mpd_run_status(connection); if (status == NULL) return mpdclient_handle_error(c); @@ -270,13 +277,13 @@ mpdclient_cmd_crop(struct mpdclient *c) if (!playing || length < 2) return true; - mpd_command_list_begin(c->connection, false); + mpd_command_list_begin(connection, false); while (--length >= 0) if (length != current) - mpd_send_delete(c->connection, length); + mpd_send_delete(connection, length); - mpd_command_list_end(c->connection); + mpd_command_list_end(connection); return mpdclient_finish_command(c); } @@ -284,12 +291,13 @@ mpdclient_cmd_crop(struct mpdclient *c) bool mpdclient_cmd_clear(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); bool retval; - if (MPD_ERROR(c)) + if (connection == NULL) return false; - mpd_send_clear(c->connection); + mpd_send_clear(connection); retval = mpdclient_finish_command(c); if (retval) @@ -301,17 +309,19 @@ mpdclient_cmd_clear(struct mpdclient *c) bool mpdclient_cmd_volume(struct mpdclient *c, gint value) { - if (MPD_ERROR(c)) + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) return false; - mpd_send_set_volume(c->connection, value); + mpd_send_set_volume(connection, value); return mpdclient_finish_command(c); } bool mpdclient_cmd_volume_up(struct mpdclient *c) { - if (MPD_ERROR(c)) + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) return false; if (c->status == NULL || @@ -330,7 +340,8 @@ mpdclient_cmd_volume_up(struct mpdclient *c) bool mpdclient_cmd_volume_down(struct mpdclient *c) { - if (MPD_ERROR(c)) + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) return false; if (c->status == NULL || mpd_status_get_volume(c->status) < 0) @@ -348,56 +359,58 @@ mpdclient_cmd_volume_down(struct mpdclient *c) bool mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8) { - if (MPD_ERROR(c)) + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) return false; - mpd_send_add(c->connection, path_utf8); + mpd_send_add(connection, path_utf8); return mpdclient_finish_command(c); } bool mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_status *status; struct mpd_song *new_song; assert(c != NULL); assert(song != NULL); - if (MPD_ERROR(c) || c->status == NULL) + if (connection == NULL || c->status == NULL) return false; /* send the add command to mpd; at the same time, get the new status (to verify the new playlist id) and the last song (we hope that's the song we just added) */ - if (!mpd_command_list_begin(c->connection, true) || - !mpd_send_add(c->connection, mpd_song_get_uri(song)) || - !mpd_send_status(c->connection) || - !mpd_send_get_queue_song_pos(c->connection, + if (!mpd_command_list_begin(connection, true) || + !mpd_send_add(connection, mpd_song_get_uri(song)) || + !mpd_send_status(connection) || + !mpd_send_get_queue_song_pos(connection, playlist_length(&c->playlist)) || - !mpd_command_list_end(c->connection) || - !mpd_response_next(c->connection)) + !mpd_command_list_end(connection) || + !mpd_response_next(connection)) return mpdclient_handle_error(c); c->events |= MPD_IDLE_PLAYLIST; - status = mpd_recv_status(c->connection); + status = mpd_recv_status(connection); if (status != NULL) { if (c->status != NULL) mpd_status_free(c->status); c->status = status; } - if (!mpd_response_next(c->connection)) + if (!mpd_response_next(connection)) return mpdclient_handle_error(c); - new_song = mpd_recv_song(c->connection); - if (!mpd_response_finish(c->connection) || new_song == NULL) { + new_song = mpd_recv_song(connection); + if (!mpd_response_finish(connection) || new_song == NULL) { if (new_song != NULL) mpd_song_free(new_song); - return mpd_connection_clear_error(c->connection) || + return mpd_connection_clear_error(connection) || mpdclient_handle_error(c); } @@ -421,10 +434,11 @@ mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song) bool mpdclient_cmd_delete(struct mpdclient *c, gint idx) { + struct mpd_connection *connection = mpdclient_get_connection(c); const struct mpd_song *song; struct mpd_status *status; - if (MPD_ERROR(c) || c->status == NULL) + if (connection == NULL || c->status == NULL) return false; if (idx < 0 || (guint)idx >= playlist_length(&c->playlist)) @@ -435,22 +449,22 @@ mpdclient_cmd_delete(struct mpdclient *c, gint idx) /* send the delete command to mpd; at the same time, get the new status (to verify the playlist id) */ - if (!mpd_command_list_begin(c->connection, false) || - !mpd_send_delete_id(c->connection, mpd_song_get_id(song)) || - !mpd_send_status(c->connection) || - !mpd_command_list_end(c->connection)) + if (!mpd_command_list_begin(connection, false) || + !mpd_send_delete_id(connection, mpd_song_get_id(song)) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) return mpdclient_handle_error(c); c->events |= MPD_IDLE_PLAYLIST; - status = mpd_recv_status(c->connection); + status = mpd_recv_status(connection); if (status != NULL) { if (c->status != NULL) mpd_status_free(c->status); c->status = status; } - if (!mpd_response_finish(c->connection)) + if (!mpd_response_finish(connection)) return mpdclient_handle_error(c); if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 && @@ -480,14 +494,18 @@ static bool mpdclient_cmd_delete_range_fallback(struct mpdclient *c, unsigned start, unsigned end) { - if (!mpd_command_list_begin(c->connection, false)) + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; + + if (!mpd_command_list_begin(connection, false)) return mpdclient_handle_error(c); for (; start < end; --end) - mpd_send_delete(c->connection, start); + mpd_send_delete(connection, start); - if (!mpd_command_list_end(c->connection) || - !mpd_response_finish(c->connection)) + if (!mpd_command_list_end(connection) || + !mpd_response_finish(connection)) return mpdclient_handle_error(c); return true; @@ -496,12 +514,14 @@ mpdclient_cmd_delete_range_fallback(struct mpdclient *c, bool mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end) { + struct mpd_connection *connection; struct mpd_status *status; - if (MPD_ERROR(c)) + connection = mpdclient_get_connection(c); + if (connection == NULL) return false; - if (mpd_connection_cmp_server_version(c->connection, 0, 16, 0) < 0) + if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) return mpdclient_cmd_delete_range_fallback(c, start, end); /* MPD 0.16 supports "delete" with a range argument */ @@ -509,22 +529,22 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end) /* send the delete command to mpd; at the same time, get the new status (to verify the playlist id) */ - if (!mpd_command_list_begin(c->connection, false) || - !mpd_send_delete_range(c->connection, start, end) || - !mpd_send_status(c->connection) || - !mpd_command_list_end(c->connection)) + if (!mpd_command_list_begin(connection, false) || + !mpd_send_delete_range(connection, start, end) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) return mpdclient_handle_error(c); c->events |= MPD_IDLE_PLAYLIST; - status = mpd_recv_status(c->connection); + status = mpd_recv_status(connection); if (status != NULL) { if (c->status != NULL) mpd_status_free(c->status); c->status = status; } - if (!mpd_response_finish(c->connection)) + if (!mpd_response_finish(connection)) return mpdclient_handle_error(c); if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) && @@ -552,10 +572,11 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end) bool mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index) { + struct mpd_connection *connection = mpdclient_get_connection(c); const struct mpd_song *song1, *song2; struct mpd_status *status; - if (MPD_ERROR(c)) + if (connection == NULL) return false; if (old_index == new_index || new_index < 0 || @@ -568,23 +589,23 @@ mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index) /* send the delete command to mpd; at the same time, get the new status (to verify the playlist id) */ - if (!mpd_command_list_begin(c->connection, false) || - !mpd_send_swap_id(c->connection, mpd_song_get_id(song1), + if (!mpd_command_list_begin(connection, false) || + !mpd_send_swap_id(connection, mpd_song_get_id(song1), mpd_song_get_id(song2)) || - !mpd_send_status(c->connection) || - !mpd_command_list_end(c->connection)) + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) return mpdclient_handle_error(c); c->events |= MPD_IDLE_PLAYLIST; - status = mpd_recv_status(c->connection); + status = mpd_recv_status(connection); if (status != NULL) { if (c->status != NULL) mpd_status_free(c->status); c->status = status; } - if (!mpd_response_finish(c->connection)) + if (!mpd_response_finish(connection)) return mpdclient_handle_error(c); if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) && @@ -610,15 +631,16 @@ mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index) bool mpdclient_playlist_update(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_entity *entity; - if (MPD_ERROR(c)) + if (connection == NULL) return false; playlist_clear(&c->playlist); - mpd_send_list_queue_meta(c->connection); - while ((entity = mpd_recv_entity(c->connection))) { + mpd_send_list_queue_meta(connection); + while ((entity = mpd_recv_entity(connection))) { if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) playlist_append(&c->playlist, mpd_entity_get_song(entity)); @@ -635,15 +657,16 @@ mpdclient_playlist_update(struct mpdclient *c) bool mpdclient_playlist_update_changes(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_song *song; guint length; - if (MPD_ERROR(c)) + if (connection == NULL) return false; - mpd_send_queue_changes_meta(c->connection, c->playlist.version); + mpd_send_queue_changes_meta(connection, c->playlist.version); - while ((song = mpd_recv_song(c->connection)) != NULL) { + while ((song = mpd_recv_song(connection)) != NULL) { int pos = mpd_song_get_pos(song); if (pos >= 0 && (guint)pos < c->playlist.list->len) { @@ -681,15 +704,16 @@ mpdclient_playlist_update_changes(struct mpdclient *c) bool mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl) { + struct mpd_connection *connection = mpdclient_get_connection(c); guint i; - if (MPD_ERROR(c)) + if (connection == NULL) return false; if (filelist_is_empty(fl)) return true; - mpd_command_list_begin(c->connection, false); + mpd_command_list_begin(connection, false); for (i = 0; i < filelist_length(fl); ++i) { struct filelist_entry *entry = filelist_get(fl, i); @@ -700,30 +724,31 @@ mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl) const struct mpd_song *song = mpd_entity_get_song(entity); - mpd_send_add(c->connection, mpd_song_get_uri(song)); + mpd_send_add(connection, mpd_song_get_uri(song)); } } - mpd_command_list_end(c->connection); + mpd_command_list_end(connection); return mpdclient_finish_command(c); } GList * mpdclient_get_artists(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); GList *list = NULL; struct mpd_pair *pair; - if (MPD_ERROR(c)) - return NULL; + if (connection == NULL) + return NULL; - mpd_search_db_tags(c->connection, MPD_TAG_ARTIST); - mpd_search_commit(c->connection); + mpd_search_db_tags(connection, MPD_TAG_ARTIST); + mpd_search_commit(connection); - while ((pair = mpd_recv_pair_tag(c->connection, + while ((pair = mpd_recv_pair_tag(connection, MPD_TAG_ARTIST)) != NULL) { list = g_list_append(list, g_strdup(pair->value)); - mpd_return_pair(c->connection, pair); + mpd_return_pair(connection, pair); } if (!mpdclient_finish_command(c)) @@ -735,23 +760,24 @@ mpdclient_get_artists(struct mpdclient *c) GList * mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8) { + struct mpd_connection *connection = mpdclient_get_connection(c); GList *list = NULL; struct mpd_pair *pair; - if (MPD_ERROR(c)) - return NULL; + if (connection == NULL) + return NULL; - mpd_search_db_tags(c->connection, MPD_TAG_ALBUM); + mpd_search_db_tags(connection, MPD_TAG_ALBUM); if (artist_utf8 != NULL) - mpd_search_add_tag_constraint(c->connection, + mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ARTIST, artist_utf8); - mpd_search_commit(c->connection); + mpd_search_commit(connection); - while ((pair = mpd_recv_pair_tag(c->connection, + while ((pair = mpd_recv_pair_tag(connection, MPD_TAG_ALBUM)) != NULL) { list = g_list_append(list, g_strdup(pair->value)); - mpd_return_pair(c->connection, pair); + mpd_return_pair(connection, pair); } if (!mpdclient_finish_command(c)) diff --git a/src/mpdclient.h b/src/mpdclient.h index 789ccb4..b6027da 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -60,6 +60,12 @@ mpdclient_disconnect(struct mpdclient *c); bool mpdclient_update(struct mpdclient *c); +struct mpd_connection * +mpdclient_get_connection(struct mpdclient *c); + +void +mpdclient_put_connection(struct mpdclient *c); + /** * To be implemented by the application: mpdclient.c calls this to * display an error message. diff --git a/src/player_command.c b/src/player_command.c index 337d767..3b8476d 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -32,6 +32,8 @@ static guint seek_source_id; static void commit_seek(struct mpdclient *c) { + struct mpd_connection *connection; + if (seek_id < 0) return; @@ -40,8 +42,10 @@ commit_seek(struct mpdclient *c) return; } + connection = mpdclient_get_connection(c); + if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song)) - if (!mpd_run_seek_id(c->connection, seek_id, seek_target_time)) + if (!mpd_run_seek_id(connection, seek_id, seek_target_time)) mpdclient_handle_error(c); seek_id = -1; @@ -58,6 +62,7 @@ seek_timer(gpointer data) seek_source_id = 0; commit_seek(c); + mpdclient_put_connection(c); return false; } @@ -81,6 +86,7 @@ cancel_seek_timer(void) bool handle_player_command(struct mpdclient *c, command_t cmd) { + struct mpd_connection *connection; const struct mpd_song *song; if (!mpdclient_is_connected(c) || c->status == NULL) @@ -95,12 +101,14 @@ handle_player_command(struct mpdclient *c, command_t cmd) break; */ case CMD_PAUSE: - if (!mpd_run_pause(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_pause(connection, mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) mpdclient_handle_error(c); break; case CMD_STOP: - if (!mpd_run_stop(c->connection)) + connection = mpdclient_get_connection(c); + if (!mpd_run_stop(connection)) mpdclient_handle_error(c); break; case CMD_CROP: @@ -119,9 +127,9 @@ handle_player_command(struct mpdclient *c, command_t cmd) schedule_seek_timer(c); } break; - /* fall through... */ case CMD_TRACK_NEXT: - if (!mpd_run_next(c->connection)) + connection = mpdclient_get_connection(c); + if (!mpd_run_next(connection)) mpdclient_handle_error(c); break; case CMD_SEEK_BACKWARD: @@ -138,41 +146,49 @@ handle_player_command(struct mpdclient *c, command_t cmd) } break; case CMD_TRACK_PREVIOUS: - if (!mpd_run_previous(c->connection)) + connection = mpdclient_get_connection(c); + if (!mpd_run_previous(connection)) mpdclient_handle_error(c); break; case CMD_SHUFFLE: - if (mpd_run_shuffle(c->connection)) + connection = mpdclient_get_connection(c); + if (mpd_run_shuffle(connection)) screen_status_message(_("Shuffled playlist")); else mpdclient_handle_error(c); break; case CMD_CLEAR: + connection = mpdclient_get_connection(c); if (mpdclient_cmd_clear(c)) screen_status_message(_("Cleared playlist")); break; case CMD_REPEAT: - if (!mpd_run_repeat(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_repeat(connection, !mpd_status_get_repeat(c->status))) mpdclient_handle_error(c); break; case CMD_RANDOM: - if (!mpd_run_random(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_random(connection, !mpd_status_get_random(c->status))) mpdclient_handle_error(c); break; case CMD_SINGLE: - if (!mpd_run_single(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_single(connection, !mpd_status_get_single(c->status))) mpdclient_handle_error(c); break; case CMD_CONSUME: - if (!mpd_run_consume(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_consume(connection, !mpd_status_get_consume(c->status))) mpdclient_handle_error(c); break; case CMD_CROSSFADE: - if (!mpd_run_crossfade(c->connection, + connection = mpdclient_get_connection(c); + if (!mpd_run_crossfade(connection, mpd_status_get_crossfade(c->status) > 0 ? 0 : options.crossfade_time)) mpdclient_handle_error(c); diff --git a/src/screen_artist.c b/src/screen_artist.c index e88a04d..996a0b7 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -189,7 +189,7 @@ load_album_list(struct mpdclient *c) static void load_song_list(struct mpdclient *c) { - struct mpd_connection *connection = c->connection; + struct mpd_connection *connection = mpdclient_get_connection(c); assert(mode == LIST_SONGS); assert(artist != NULL); @@ -392,7 +392,7 @@ screen_artist_update(struct mpdclient *c) static void add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter) { - struct mpd_connection *connection = c->connection; + struct mpd_connection *connection = mpdclient_get_connection(c); char *str; struct filelist *addlist; diff --git a/src/screen_browser.c b/src/screen_browser.c index 604d424..6375ee9 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -125,9 +125,10 @@ browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_c static bool load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist) { + struct mpd_connection *connection = mpdclient_get_connection(c); char *filename = utf8_to_locale(mpd_playlist_get_path(playlist)); - if (mpd_run_load(c->connection, mpd_playlist_get_path(playlist))) { + if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) { screen_status_printf(_("Loading playlist %s..."), g_basename(filename)); c->events |= MPD_IDLE_QUEUE; @@ -141,6 +142,7 @@ load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist) static bool enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry) { + struct mpd_connection *connection = mpdclient_get_connection(c); const struct mpd_song *song = mpd_entity_get_song(entry->entity); int id; @@ -154,7 +156,7 @@ enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry) if (id < 0) { char buf[BUFSIZE]; - id = mpd_run_add_id(c->connection, mpd_song_get_uri(song)); + id = mpd_run_add_id(connection, mpd_song_get_uri(song)); if (id < 0) { mpdclient_handle_error(c); return false; @@ -167,7 +169,7 @@ enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry) screen_status_printf(_("Adding \'%s\' to playlist"), buf); } - if (!mpd_run_play_id(c->connection, id)) { + if (!mpd_run_play_id(connection, id)) { mpdclient_handle_error(c); return false; } diff --git a/src/screen_client.c b/src/screen_client.c index 9e13ba0..90364c0 100644 --- a/src/screen_client.c +++ b/src/screen_client.c @@ -27,9 +27,12 @@ static bool _screen_auth(struct mpdclient *c, gint recursion) { + struct mpd_connection *connection; char *password; - mpd_connection_clear_error(c->connection); + connection = mpdclient_get_connection(c); + + mpd_connection_clear_error(connection); if (recursion > 2) return false; @@ -37,14 +40,14 @@ _screen_auth(struct mpdclient *c, gint recursion) if (password == NULL) return false; - mpd_send_password(c->connection, password); + mpd_send_password(connection, password); g_free(password); - mpd_response_finish(c->connection); + mpd_response_finish(connection); mpdclient_update(c); - if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER && - mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PASSWORD) + if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER && + mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_PASSWORD) return _screen_auth(c, ++recursion); return true; @@ -70,16 +73,19 @@ mpdclient_ui_error(const char *message_utf8) void screen_database_update(struct mpdclient *c, const char *path) { + struct mpd_connection *connection; unsigned id; assert(c != NULL); assert(mpdclient_is_connected(c)); - id = mpd_run_update(c->connection, path); + connection = mpdclient_get_connection(c); + + id = mpd_run_update(connection, path); if (id == 0) { - if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER && - mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_UPDATE_ALREADY && - mpd_connection_clear_error(c->connection)) + if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER && + mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_UPDATE_ALREADY && + mpd_connection_clear_error(connection)) screen_status_printf(_("Database update running...")); else mpdclient_handle_error(c); diff --git a/src/screen_file.c b/src/screen_file.c index 43c28c2..b087068 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -67,7 +67,7 @@ screen_file_reload(struct mpdclient *c) if (!mpdclient_is_connected(c)) return; - connection = c->connection; + connection = mpdclient_get_connection(c); mpd_send_list_meta(connection, current_path); filelist_recv(browser.filelist, connection); @@ -198,6 +198,7 @@ handle_save(struct mpdclient *c) static int handle_delete(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct filelist_entry *entry; struct mpd_entity *entity; const struct mpd_playlist *playlist; @@ -237,7 +238,7 @@ handle_delete(struct mpdclient *c) return 0; } - if (!mpd_run_rm(c->connection, mpd_playlist_get_path(playlist))) { + if (!mpd_run_rm(connection, mpd_playlist_get_path(playlist))) { mpdclient_handle_error(c); break; } diff --git a/src/screen_outputs.c b/src/screen_outputs.c index 1d60bae..c1660b3 100644 --- a/src/screen_outputs.c +++ b/src/screen_outputs.c @@ -46,17 +46,20 @@ outputs_repaint(void) static bool toggle_output(struct mpdclient *c, unsigned int output_index) { + struct mpd_connection *connection; struct mpd_output *output; assert(mpd_outputs != NULL); - if (output_index >= mpd_outputs->len) + if (!mpdclient_is_connected(c) || + output_index >= mpd_outputs->len) return false; + connection = mpdclient_get_connection(c); output = g_ptr_array_index(mpd_outputs, output_index); if (!mpd_output_get_enabled(output)) { - if (!mpd_run_enable_output(c->connection, + if (!mpd_run_enable_output(connection, mpd_output_get_id(output))) { mpdclient_handle_error(c); return false; @@ -67,7 +70,7 @@ toggle_output(struct mpdclient *c, unsigned int output_index) screen_status_printf(_("Output '%s' enabled"), mpd_output_get_name(output)); } else { - if (!mpd_run_disable_output(c->connection, + if (!mpd_run_disable_output(connection, mpd_output_get_id(output))) { mpdclient_handle_error(c); return false; @@ -103,6 +106,7 @@ clear_outputs_list(void) static void fill_outputs_list(struct mpdclient *c) { + struct mpd_connection *connection; struct mpd_output *output; assert(mpd_outputs != NULL); @@ -110,12 +114,13 @@ fill_outputs_list(struct mpdclient *c) if (!mpdclient_is_connected(c)) return; - mpd_send_outputs(c->connection); - while ((output = mpd_recv_output(c->connection)) != NULL) { + connection = mpdclient_get_connection(c); + mpd_send_outputs(connection); + while ((output = mpd_recv_output(connection)) != NULL) { g_ptr_array_add(mpd_outputs, output); } - if (!mpd_response_finish(c->connection)) + if (!mpd_response_finish(connection)) mpdclient_handle_error(c); } diff --git a/src/screen_play.c b/src/screen_play.c index 9560b2b..632944f 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -289,6 +289,7 @@ completion_strncmp(const gchar *s1, const gchar *s2, gsize n) int playlist_save(struct mpdclient *c, char *name, char *defaultname) { + struct mpd_connection *connection; gchar *filename, *filename_utf8; #ifndef NCMPC_MINI GCompletion *gcmp; @@ -338,10 +339,11 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname) filename_utf8 = locale_to_utf8(filename); - if (!mpd_run_save(c->connection, filename_utf8)) { - if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER && - mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_EXIST && - mpd_connection_clear_error(c->connection)) { + connection = mpdclient_get_connection(c); + if (!mpd_run_save(connection, filename_utf8)) { + if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER && + mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST && + mpd_connection_clear_error(connection)) { char *buf; int key; @@ -357,8 +359,8 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname) return -1; } - if (!mpd_run_rm(c->connection, filename_utf8) || - !mpd_run_save(c->connection, filename_utf8)) { + if (!mpd_run_rm(connection, filename_utf8) || + !mpd_run_save(connection, filename_utf8)) { mpdclient_handle_error(c); g_free(filename_utf8); g_free(filename); @@ -638,6 +640,7 @@ handle_mouse_event(struct mpdclient *c) static bool screen_playlist_cmd(struct mpdclient *c, command_t cmd) { + struct mpd_connection *connection; static command_t cached_cmd = CMD_NONE; command_t prev_cmd = cached_cmd; cached_cmd = cmd; @@ -752,18 +755,17 @@ screen_playlist_cmd(struct mpdclient *c, command_t cmd) return true; case CMD_SHUFFLE: - { if(!lw->range_selection) /* No range selection, shuffle all list. */ break; - if (mpd_run_shuffle_range(c->connection, lw->selected_start, + connection = mpdclient_get_connection(c); + if (mpd_run_shuffle_range(connection, lw->selected_start, lw->selected_end + 1)) screen_status_message(_("Shuffled playlist")); else mpdclient_handle_error(c); return true; - } case CMD_LIST_MOVE_UP: if(lw->selected_start == 0) diff --git a/src/screen_search.c b/src/screen_search.c index 85f8179..b410219 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -300,18 +300,19 @@ search_advanced_query(struct mpd_connection *connection, char *query) static struct filelist * do_search(struct mpdclient *c, char *query) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct filelist *fl; - fl = search_advanced_query(c->connection, query); + fl = search_advanced_query(connection, query); if (fl != NULL) return fl; - if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) { + if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) { mpdclient_handle_error(c); return NULL; } - fl = search_simple_query(c->connection, FALSE, + fl = search_simple_query(connection, FALSE, mode[options.search_mode].table, query); if (fl == NULL) diff --git a/src/screen_song.c b/src/screen_song.c index 1de01f4..9d42eb4 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -371,10 +371,9 @@ screen_song_update(struct mpdclient *c) } /* Add some statistics about mpd */ - if (mpdclient_is_connected(c)) { - if (!screen_song_add_stats(c->connection)) - mpdclient_handle_error(c); - } + if (mpdclient_is_connected(c) && + !screen_song_add_stats(mpdclient_get_connection(c))) + mpdclient_handle_error(c); screen_song_repaint(); } diff --git a/src/utils.c b/src/utils.c index 5a43f09..6042848 100644 --- a/src/utils.c +++ b/src/utils.c @@ -82,7 +82,7 @@ gcmp_list_from_path(struct mpdclient *c, const gchar *path, if (!mpdclient_is_connected(c)) return list; - connection = c->connection; + connection = mpdclient_get_connection(c); mpd_send_list_meta(connection, path); while ((entity = mpd_recv_entity(connection)) != NULL) { -- 2.30.2