Code

mpdclient: wrap the idle callback, unified error handling
authorMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 21:55:19 +0000 (22:55 +0100)
committerMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 22:07:41 +0000 (23:07 +0100)
src/callbacks.h
src/main.c
src/mpdclient.c

index 4fa472fc406802e4168b7c717b1613a22bdf3b38..c4f1cf929f4c47f2ba86e148f24b1b59bd573981 100644 (file)
 
 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
index 31112fb9514adfd4d4f25d7cbac5f7f879943996..fe21c6bcf6c947605c8af89a6c581bd5b2759457 100644 (file)
@@ -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();
 }
 
index 4c480d781357a72d961a854514d4258cc78954f0..7707893a2f54338178a321157dfc2ddbdac4a5a7 100644 (file)
@@ -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;