Code

mpdclient: pass host, port etc. to constructor
authorMax Kellermann <max.kellermann@gmail.com>
Sun, 19 Mar 2017 10:55:54 +0000 (11:55 +0100)
committerMax Kellermann <max.kellermann@gmail.com>
Sun, 19 Mar 2017 10:55:54 +0000 (11:55 +0100)
src/main.c
src/mpdclient.c
src/mpdclient.h

index c242da922e97e715ac88a27856f51d04e042c1a9..d9c3ddb0d28c48e7dbf7663068b9c0aabe9e3908 100644 (file)
@@ -202,9 +202,7 @@ timer_reconnect(gcc_unused gpointer data)
        doupdate();
 
        mpdclient_disconnect(mpd);
-       mpdclient_connect(mpd, options.host, options.port,
-                         options.timeout_ms,
-                         options.password);
+       mpdclient_connect(mpd);
 
        return FALSE;
 }
@@ -423,7 +421,9 @@ main(int argc, const char *argv[])
 #endif
 
        /* create mpdclient instance */
-       mpd = mpdclient_new();
+       mpd = mpdclient_new(options.host, options.port,
+                           options.timeout_ms,
+                           options.password);
 
        /* initialize curses */
        screen_init(mpd);
index 31e8a721fa517ce220486d87c8fce6612d6c4dfe..c1c4a31e6143cfb9ae200d3514d23e61c524c64e 100644 (file)
@@ -98,9 +98,16 @@ mpdclient_handle_error(struct mpdclient *c)
 }
 
 struct mpdclient *
-mpdclient_new(void)
+mpdclient_new(const gchar *host, unsigned port,
+             unsigned timeout_ms, const gchar *password)
 {
        struct mpdclient *c = g_new0(struct mpdclient, 1);
+
+       c->host = host;
+       c->port = port;
+       c->timeout_ms = timeout_ms;
+       c->password = password;
+
        playlist_init(&c->playlist);
        c->volume = -1;
        c->events = 0;
@@ -159,17 +166,13 @@ mpdclient_disconnect(struct mpdclient *c)
 }
 
 bool
-mpdclient_connect(struct mpdclient *c,
-                 const gchar *host,
-                 unsigned port,
-                 unsigned timeout_ms,
-                 const gchar *password)
+mpdclient_connect(struct mpdclient *c)
 {
        /* close any open connection */
        mpdclient_disconnect(c);
 
        /* connect to MPD */
-       c->connection = mpd_connection_new(host, port, timeout_ms);
+       c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms);
        if (c->connection == NULL)
                g_error("Out of memory");
 
@@ -181,7 +184,8 @@ mpdclient_connect(struct mpdclient *c,
        }
 
        /* send password */
-       if (password != NULL && !mpd_run_password(c->connection, password)) {
+       if (c->password != NULL &&
+           !mpd_run_password(c->connection, c->password)) {
                mpdclient_handle_error(c);
                mpdclient_disconnect(c);
                mpdclient_failed_callback();
index b959f5c426d9e36cd70138510354a7739c3ee7e3..371d42935ecff3374fe9646162b4274c0eb98fc6 100644 (file)
@@ -9,6 +9,13 @@
 struct filelist;
 
 struct mpdclient {
+       const char *host;
+       unsigned port;
+
+       unsigned timeout_ms;
+
+       const char *password;
+
        /* playlist */
        struct mpdclient_playlist playlist;
 
@@ -79,7 +86,8 @@ mpdclient_finish_command(struct mpdclient *c)
 }
 
 struct mpdclient *
-mpdclient_new(void);
+mpdclient_new(const gchar *host, unsigned port,
+             unsigned timeout_ms, const gchar *password);
 
 void mpdclient_free(struct mpdclient *c);
 
@@ -120,8 +128,7 @@ mpdclient_get_current_song(const struct mpdclient *c)
 }
 
 bool
-mpdclient_connect(struct mpdclient *c, const gchar *host, unsigned port,
-                 unsigned timeout_ms, const gchar *password);
+mpdclient_connect(struct mpdclient *c);
 
 void
 mpdclient_disconnect(struct mpdclient *c);