From: Max Kellermann Date: Fri, 17 Mar 2017 21:55:19 +0000 (+0100) Subject: mpdclient: wrap the idle callback, unified error handling X-Git-Tag: v0.26~55 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b1f342d5a7c77a5a5f413bde5e2234ac00988a6b;p=ncmpc.git mpdclient: wrap the idle callback, unified error handling --- diff --git a/src/callbacks.h b/src/callbacks.h index 4fa472f..c4f1cf9 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -26,6 +26,13 @@ struct mpdclient; +/** + * The connection to MPD was lost. If this was due to an error, then + * mpdclient_error_callback() has already been called. + */ +void +mpdclient_lost_callback(void); + /** * To be implemented by the application: mpdclient.c calls this to * display an error message. @@ -39,9 +46,6 @@ bool mpdclient_auth_callback(struct mpdclient *c); void -mpdclient_idle_callback(enum mpd_error error, - enum mpd_server_error server_error, - const char *message, enum mpd_idle events, - void *ctx); +mpdclient_idle_callback(enum mpd_idle events); #endif diff --git a/src/main.c b/src/main.c index 31112fb..fe21c6b 100644 --- a/src/main.c +++ b/src/main.c @@ -325,53 +325,29 @@ check_reconnect(void) NULL); } +void +mpdclient_lost_callback(void) +{ + assert(reconnect_source_id == 0); + + screen_update(mpd); + + reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL); +} + /** * This function is called by the gidle.c library when MPD sends us an * idle event (or when the connection dies). */ void -mpdclient_idle_callback(enum mpd_error error, - gcc_unused enum mpd_server_error server_error, - const char *message, enum mpd_idle events, - void *ctx) +mpdclient_idle_callback(gcc_unused enum mpd_idle events) { - struct mpdclient *c = ctx; - - c->idle = false; - - assert(mpdclient_is_connected(c)); - - if (error != MPD_ERROR_SUCCESS) { - char *allocated; - if (error == MPD_ERROR_SERVER) - message = allocated = utf8_to_locale(message); - else - allocated = NULL; - screen_status_message(message); - g_free(allocated); - screen_bell(); - doupdate(); - - mpdclient_disconnect(c); - screen_update(mpd); - reconnect_source_id = g_timeout_add(1000, timer_reconnect, - NULL); - return; - } - - c->events |= events; - mpdclient_update(c); - #ifndef NCMPC_MINI if (options.enable_xterm_title) update_xterm_title(); #endif screen_update(mpd); - c->events = 0; - - mpdclient_put_connection(c); - check_reconnect(); auto_update_timer(); } diff --git a/src/mpdclient.c b/src/mpdclient.c index 4c480d7..7707893 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -43,6 +43,35 @@ mpdclient_invoke_error_callback(enum mpd_error error, g_free(allocated); } +static void +mpdclient_gidle_callback(enum mpd_error error, + gcc_unused enum mpd_server_error server_error, + const char *message, enum mpd_idle events, + void *ctx) +{ + struct mpdclient *c = ctx; + + c->idle = false; + + assert(mpdclient_is_connected(c)); + + if (error != MPD_ERROR_SUCCESS) { + mpdclient_invoke_error_callback(error, message); + mpdclient_disconnect(c); + mpdclient_lost_callback(); + return; + } + + c->events |= events; + mpdclient_update(c); + + mpdclient_idle_callback(c->events); + + c->events = 0; + + mpdclient_put_connection(c); +} + /****************************************************************************/ /*** mpdclient functions ****************************************************/ /****************************************************************************/ @@ -147,7 +176,7 @@ mpdclient_connect(struct mpdclient *c, } c->source = mpd_glib_new(c->connection, - mpdclient_idle_callback, c); + mpdclient_gidle_callback, c); ++c->connection_id;