Code

mpdclient: check source again after mpd_glib_leave()
[ncmpc.git] / src / mpdclient.c
index 5903f1430fb3c9288c1c99d1fe208db07990e76f..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,11 +216,54 @@ 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);
 }
 
+static char *
+settings_name(const struct mpd_settings *settings)
+{
+       assert(settings != NULL);
+
+       const char *host = mpd_settings_get_host(settings);
+       if (host == NULL)
+               host = "unknown";
+
+       if (host[0] == '/')
+               return g_strdup(host);
+
+       unsigned port = mpd_settings_get_port(settings);
+       if (port == 0 || port == 6600)
+               return g_strdup(host);
+
+       return g_strdup_printf("%s:%u", host, port);
+}
+
+char *
+mpdclient_settings_name(const struct mpdclient *c)
+{
+       assert(c != NULL);
+
+#ifdef ENABLE_ASYNC_CONNECT
+       return settings_name(c->settings);
+#else
+       struct mpd_settings *settings =
+               mpd_settings_new(c->host, c->port, 0, NULL, NULL);
+       if (settings == NULL)
+               return g_strdup("unknown");
+
+       char *name = settings_name(settings);
+       mpd_settings_free(settings);
+       return name;
+#endif
+}
+
 static void
 mpdclient_status_free(struct mpdclient *c)
 {
@@ -281,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)
 {
@@ -288,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);
 }
 
@@ -298,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();
 }
@@ -307,6 +409,16 @@ static const struct aconnect_handler mpdclient_connect_handler = {
        .error = mpdclient_connect_error,
 };
 
+static void
+mpdclient_aconnect_start(struct mpdclient *c,
+                        const struct mpd_settings *settings)
+{
+       aconnect_start(&c->async_connect,
+                      mpd_settings_get_host(settings),
+                      mpd_settings_get_port(settings),
+                      &mpdclient_connect_handler, c);
+}
+
 #endif
 
 void
@@ -316,10 +428,10 @@ mpdclient_connect(struct mpdclient *c)
        mpdclient_disconnect(c);
 
 #ifdef ENABLE_ASYNC_CONNECT
-       aconnect_start(&c->async_connect,
-                      mpd_settings_get_host(c->settings),
-                      mpd_settings_get_port(c->settings),
-                      &mpdclient_connect_handler, c);
+#ifndef WIN32
+       c->connecting2 = false;
+#endif
+       mpdclient_aconnect_start(c, c->settings);
 #else
        /* connect to MPD */
        struct mpd_connection *connection =
@@ -378,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;