status

statusbar program for dwm
git clone anongit@rnpnr.xyz:status.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 0909d9808ba1942660ab52b2921bf70b7fa51113
Parent: c6d86b8bf3ea418081766a84c6efd83b6ab351e2
Author: Randy Palamar
Date:   Sat,  3 Apr 2021 20:01:44 -0600

keep mpd connection open for program lifetime

this has the side effect of fixing the old memory leak. that was caused
by not freeing the connection after error.

the mpd_command_list() calls were removed as well. they aren't useful
for 2 commands especially when one could potentially be skipped.

Diffstat:
Mblocks/mpd.c | 29++++++++++++++++++-----------
1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/blocks/mpd.c b/blocks/mpd.c @@ -6,27 +6,35 @@ #include "../util.h" #include "mpd.h" +static struct mpd_connection *conn; + +static int +openconn(void) +{ + conn = mpd_connection_new(mpdhost, 0, 0); + if (mpd_connection_get_error(conn)) { + mpd_connection_free(conn); + conn = NULL; + return -1; + } + return 0; +} + size_t mpd(struct Block *b) { - struct mpd_connection *conn = NULL; struct mpd_song *song = NULL; struct mpd_status *status = NULL; - conn = mpd_connection_new(mpdhost, 0, 600); - if (!conn || mpd_connection_get_error(conn)) + if (!conn && openconn() != 0) return snprintf(b->curstr, BLOCKLEN, b->fmt, ""); - mpd_command_list_begin(conn, true); - mpd_send_status(conn); - mpd_send_current_song(conn); - mpd_command_list_end(conn); - - status = mpd_recv_status(conn); + if (mpd_send_status(conn)) + status = mpd_recv_status(conn); /* >= covers both PLAY and PAUSE */ if (status && (mpd_status_get_state(status) >= MPD_STATE_PLAY)) { - mpd_response_next(conn); + mpd_send_current_song(conn); song = mpd_recv_song(conn); snprintf(buf, sizeof(buf), "%s", mpd_song_get_tag(song, b->u.i, 0)); @@ -37,7 +45,6 @@ mpd(struct Block *b) if (status) mpd_status_free(status); mpd_response_finish(conn); - mpd_connection_free(conn); return snprintf(b->curstr, BLOCKLEN, b->fmt, buf); }