From e004c7617c0193660d837970eaecdf7c01f40c24 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 19 Mar 2017 11:55:54 +0100 Subject: [PATCH] mpdclient: pass host, port etc. to constructor --- src/main.c | 8 ++++---- src/mpdclient.c | 20 ++++++++++++-------- src/mpdclient.h | 13 ++++++++++--- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index c242da9..d9c3ddb 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/mpdclient.c b/src/mpdclient.c index 31e8a72..c1c4a31 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -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(); diff --git a/src/mpdclient.h b/src/mpdclient.h index b959f5c..371d429 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -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); -- 2.30.2