Code

mpdclient: check source again after mpd_glib_leave()
[ncmpc.git] / src / mpdclient.c
index 870839fd27dbb69ee8abd1d4b65e79d8c8567ee4..1801ad2dc7973b94e927131f5bdd1c3a95127f7d 100644 (file)
@@ -153,6 +153,25 @@ mpdclient_handle_error(struct mpdclient *c)
        return false;
 }
 
+#ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
+
+static bool
+is_local_socket(const char *host)
+{
+       return *host == '/' || *host == '@';
+}
+
+static bool
+settings_is_local_socket(const struct mpd_settings *settings)
+{
+       const char *host = mpd_settings_get_host(settings);
+       return host != NULL && is_local_socket(host);
+}
+
+#endif
+#endif
+
 struct mpdclient *
 mpdclient_new(const gchar *host, unsigned port,
              unsigned timeout_ms, const gchar *password)
@@ -164,6 +183,14 @@ mpdclient_new(const gchar *host, unsigned port,
                                       NULL, NULL);
        if (c->settings == NULL)
                g_error("Out of memory");
+
+#ifndef WIN32
+       c->settings2 = host == NULL && port == 0 &&
+               settings_is_local_socket(c->settings)
+               ? mpd_settings_new(host, 6600, timeout_ms, NULL, NULL)
+               : NULL;
+#endif
+
 #else
        c->host = host;
        c->port = port;
@@ -189,6 +216,11 @@ mpdclient_free(struct mpdclient *c)
 
 #ifdef ENABLE_ASYNC_CONNECT
        mpd_settings_free(c->settings);
+
+#ifndef WIN32
+       if (c->settings2 != NULL)
+               mpd_settings_free(c->settings2);
+#endif
 #endif
 
        g_free(c);
@@ -319,6 +351,21 @@ mpdclient_connected(struct mpdclient *c,
 
 #ifdef ENABLE_ASYNC_CONNECT
 
+static void
+mpdclient_aconnect_start(struct mpdclient *c,
+                        const struct mpd_settings *settings);
+
+static const struct mpd_settings *
+mpdclient_get_settings(const struct mpdclient *c)
+{
+#ifndef WIN32
+       if (c->connecting2)
+               return c->settings2;
+#endif
+
+       return c->settings;
+}
+
 static void
 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
 {
@@ -326,6 +373,15 @@ mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
        assert(c->async_connect != NULL);
        c->async_connect = NULL;
 
+       const char *password =
+               mpd_settings_get_password(mpdclient_get_settings(c));
+       if (password != NULL && !mpd_run_password(connection, password)) {
+               mpdclient_error_callback(mpd_connection_get_error_message(connection));
+               mpd_connection_free(connection);
+               mpdclient_failed_callback();
+               return;
+       }
+
        mpdclient_connected(c, connection);
 }
 
@@ -336,6 +392,14 @@ mpdclient_connect_error(const char *message, void *ctx)
        assert(c->async_connect != NULL);
        c->async_connect = NULL;
 
+#ifndef WIN32
+       if (!c->connecting2 && c->settings2 != NULL) {
+               c->connecting2 = true;
+               mpdclient_aconnect_start(c, c->settings2);
+               return;
+       }
+#endif
+
        mpdclient_error_callback(message);
        mpdclient_failed_callback();
 }
@@ -364,6 +428,9 @@ mpdclient_connect(struct mpdclient *c)
        mpdclient_disconnect(c);
 
 #ifdef ENABLE_ASYNC_CONNECT
+#ifndef WIN32
+       c->connecting2 = false;
+#endif
        mpdclient_aconnect_start(c, c->settings);
 #else
        /* connect to MPD */
@@ -423,7 +490,8 @@ mpdclient_get_connection(struct mpdclient *c)
                c->idle = false;
                mpd_glib_leave(c->source);
 
-               mpdclient_schedule_enter_idle(c);
+               if (c->source != NULL)
+                       mpdclient_schedule_enter_idle(c);
        }
 
        return c->connection;