From: Max Kellermann Date: Sun, 19 Mar 2017 11:20:23 +0000 (+0100) Subject: mpdclient: automatically re-enter idle mode, eliminate mpdclient_put_connection() X-Git-Tag: v0.26~24 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=51efef6302eb091d8331e0b519ece53010709048;p=ncmpc.git mpdclient: automatically re-enter idle mode, eliminate mpdclient_put_connection() --- diff --git a/src/main.c b/src/main.c index 8d07b42..8ade659 100644 --- a/src/main.c +++ b/src/main.c @@ -148,7 +148,6 @@ do_mpd_update(void) screen_update(mpd); mpd->events = 0; - mpdclient_put_connection(mpd); check_reconnect(); } @@ -308,7 +307,6 @@ 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 c1c4a31..1a059d5 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -28,6 +28,40 @@ #include +static gboolean +mpdclient_enter_idle_callback(gpointer user_data) +{ + struct mpdclient *c = user_data; + assert(c->enter_idle_source_id != 0); + assert(c->source != NULL); + assert(!c->idle); + + c->enter_idle_source_id = 0; + c->idle = mpd_glib_enter(c->source); + return false; +} + +static void +mpdclient_schedule_enter_idle(struct mpdclient *c) +{ + assert(c != NULL); + assert(c->source != NULL); + + if (c->enter_idle_source_id == 0) + /* automatically re-enter MPD "idle" mode */ + c->enter_idle_source_id = + g_idle_add(mpdclient_enter_idle_callback, c); +} + +static void +mpdclient_cancel_enter_idle(struct mpdclient *c) +{ + if (c->enter_idle_source_id != 0) { + g_source_remove(c->enter_idle_source_id); + c->enter_idle_source_id = 0; + } +} + static void mpdclient_invoke_error_callback(enum mpd_error error, const char *message) @@ -69,7 +103,8 @@ mpdclient_gidle_callback(enum mpd_error error, c->events = 0; - mpdclient_put_connection(c); + if (c->source != NULL) + mpdclient_schedule_enter_idle(c); } /****************************************************************************/ @@ -142,6 +177,8 @@ mpdclient_status_free(struct mpdclient *c) void mpdclient_disconnect(struct mpdclient *c) { + mpdclient_cancel_enter_idle(c); + if (c->source != NULL) { mpd_glib_free(c->source); c->source = NULL; @@ -194,6 +231,7 @@ mpdclient_connect(struct mpdclient *c) c->source = mpd_glib_new(c->connection, mpdclient_gidle_callback, c); + mpdclient_schedule_enter_idle(c); ++c->connection_id; @@ -248,21 +286,13 @@ mpdclient_get_connection(struct mpdclient *c) if (c->source != NULL && c->idle) { c->idle = false; mpd_glib_leave(c->source); + + mpdclient_schedule_enter_idle(c); } return c->connection; } -void -mpdclient_put_connection(struct mpdclient *c) -{ - assert(c->source == NULL || c->connection != NULL); - - if (c->source != NULL && !c->idle) { - c->idle = mpd_glib_enter(c->source); - } -} - static struct mpd_status * mpdclient_recv_status(struct mpdclient *c) { diff --git a/src/mpdclient.h b/src/mpdclient.h index 371d429..bac3338 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -23,13 +23,19 @@ struct mpdclient { /** * Tracks idle events. It is automatically called by - * mpdclient_get_connection() and mpdclient_put_connection(). + * mpdclient_get_connection(). */ struct mpd_glib_source *source; struct mpd_status *status; const struct mpd_song *song; + /** + * The GLib source id which re-enters MPD idle mode before the + * next main loop interation. + */ + unsigned enter_idle_source_id; + /** * This attribute is incremented whenever the connection changes * (i.e. on disconnection and (re-)connection). @@ -139,9 +145,6 @@ mpdclient_update(struct mpdclient *c); struct mpd_connection * mpdclient_get_connection(struct mpdclient *c); -void -mpdclient_put_connection(struct mpdclient *c); - /*** MPD Commands **********************************************************/ bool diff --git a/src/player_command.c b/src/player_command.c index f2353f7..1a06bfa 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -59,7 +59,6 @@ seek_timer(gpointer data) seek_source_id = 0; commit_seek(c); - mpdclient_put_connection(c); return false; }