spoon

set dwm status
git clone git://git.2f30.org/spoon.git
Log | Files | Refs | LICENSE

commit 121956cfc0fa87ba9608d804f3ca044dc57c8d12
parent c9624f19c37036e05abadc0c8c978a07581cb5dd
Author: lostd <lostd@2f30.org>
Date:   Sun May 15 14:08:13 +0100

Add support for mpd current song

Diffstat:
Makefile | 6+++---
spoon.c | 34++++++++++++++++++++++++++++++++--
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ PREFIX = /usr/local -CFLAGS = -I/usr/X11R6/include -LDFLAGS = -L/usr/X11R6/lib -LDLIBS = -lxkbfile -lX11 +CFLAGS = -I/usr/X11R6/include -I/usr/local/include +LDFLAGS = -L/usr/X11R6/lib -L/usr/local/lib +LDLIBS = -lxkbfile -lX11 -lmpdclient OBJ = spoon.o BIN = spoon diff --git a/spoon.c b/spoon.c @@ -9,6 +9,8 @@ #include <X11/XKBlib.h> #include <X11/extensions/XKBrules.h> +#include <mpd/client.h> + #define LEN(x) (sizeof (x) / sizeof *(x)) int dummyread(char *buf, size_t len); @@ -40,8 +42,36 @@ dummyread(char *buf, size_t len) int mpdread(char *buf, size_t len) { - strlcpy(buf, "mpd", len); - return 0; + struct mpd_connection *conn; + struct mpd_song *song; + const char *artist, *title; + int ret = 0; + + conn = mpd_connection_new(NULL, 0, 0); + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { + warnx("cannot connect to mpd"); + return -1; + } + mpd_send_current_song(conn); + song = mpd_recv_song(conn); + if (song == NULL) { + warnx("cannot recv mpd song"); + ret = -1; + goto out; + } + artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); + title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); + if (artist != NULL && title != NULL) + snprintf(buf, len, "%s - %s", artist, title); + else if (title != NULL) + strlcpy(buf, title, len); + else + ret = -1; + mpd_song_free(song); + mpd_response_finish(conn); +out: + mpd_connection_free(conn); + return ret; } #ifdef __OpenBSD__