Commit: d4a2564d6892003387bb3ba1c435620333c53969
Parent: 9cce7db815c25d4a86b711fde753a2ef7357d95c
Author: Randy Palamar
Date: Sun, 4 Apr 2021 16:34:33 -0600
remove buffer usage in mpd.c and move connection check to a function
Diffstat:
1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/blocks/mpd.c b/blocks/mpd.c
@@ -9,7 +9,7 @@
static struct mpd_connection *conn;
static int
-openconn(void)
+open_conn(void)
{
conn = mpd_connection_new(mpdhost, 0, 0);
if (mpd_connection_get_error(conn)
@@ -22,20 +22,25 @@ openconn(void)
return 0;
}
+static int
+check_conn(void)
+{
+ if (!mpd_connection_get_error(conn))
+ return 0;
+
+ mpd_connection_free(conn);
+ return open_conn();
+}
+
size_t
mpd(struct Block *b)
{
struct mpd_song *song = NULL;
struct mpd_status *status = NULL;
+ const char *str = "";
- if (!conn && openconn() != 0)
- return snprintf(b->curstr, LEN(b->curstr), b->fmt, "");
-
- if (mpd_connection_get_error(conn)) {
- mpd_connection_free(conn);
- if (openconn() != 0)
- return snprintf(b->curstr, LEN(b->curstr), b->fmt, "");
- }
+ if ((!conn && open_conn() != 0) || check_conn() != 0)
+ return snprintf(b->curstr, LEN(b->curstr), b->fmt, str);
mpd_run_noidle(conn);
@@ -44,19 +49,16 @@ mpd(struct Block *b)
case MPD_STATE_PAUSE:
case MPD_STATE_PLAY:
song = mpd_run_current_song(conn);
- snprintf(buf, sizeof(buf), "%s",
- mpd_song_get_tag(song, b->u.i, 0));
+ str = mpd_song_get_tag(song, b->u.i, 0);
mpd_song_free(song);
break;
case MPD_STATE_STOP:
default:
- snprintf(buf, sizeof(buf), "%s", "");
}
mpd_status_free(status);
- } else
- snprintf(buf, sizeof(buf), "%s", "");
+ }
mpd_send_idle(conn);
- return snprintf(b->curstr, LEN(b->curstr), b->fmt, buf);
+ return snprintf(b->curstr, LEN(b->curstr), b->fmt, str);
}