Hi,
I thought slstatus need some love. Find attached a patch that add mpd support.
A format string is required as argument.
Thanks to spoon [1] authors for inspiration and snippets.

I had to modify Makefile to support mpdclient library. Not sure it's
the best way to go.

Regards.

[1] : https://git.2f30.org/spoon


>From 5a3c78afc841d2676a6bb7cc5ce7bf6bfdbd9606 Mon Sep 17 00:00:00 2001
From: prx <p...@ybad.name>
Date: Mon, 28 Sep 2020 11:35:13 +0200
Subject: [PATCH] mpdonair : mpd status (artist, title, progress) This require
 to edit Makefile to uncomment appropriate lines.

---
 Makefile         |  8 +++++
 components/mpd.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
 config.def.h     |  4 +++
 slstatus.h       |  5 +++
 4 files changed, 98 insertions(+)
 create mode 100644 components/mpd.c

diff --git a/Makefile b/Makefile
index 945b5e3..0ed41f7 100644
--- a/Makefile
+++ b/Makefile
@@ -28,9 +28,17 @@ COM =\
 	components/volume\
 	components/wifi
 
+# uncomment to use mpdonair
+#COM += components/mpd
+#LDLIBS += -lmpdclient
+# uncomment on OpenBSD
+#LDFLAGS += -L/usr/local/lib
+#CPPFLAGS += -I/usr/local/include
+
 all: slstatus
 
 $(COM:=.o): config.mk $(REQ:=.h)
+
 slstatus.o: slstatus.c slstatus.h arg.h config.h config.mk $(REQ:=.h)
 
 .c.o:
diff --git a/components/mpd.c b/components/mpd.c
new file mode 100644
index 0000000..1fcf14b
--- /dev/null
+++ b/components/mpd.c
@@ -0,0 +1,81 @@
+#include <err.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <mpd/client.h>
+
+#include "../util.h"
+
+/* fmt consist of lowercase :
+ * "a" for artist,
+ * "t" for song title,
+ * "p" for time progression,
+ * "at" for song artist then title
+ * if not a, t or p, any character will be represented as separator.
+ * i.e: "a-t" gives "artist-title"
+*/
+const char *
+mpdonair(const char *fmt)
+{
+    static struct mpd_connection *conn; /* kept between calls */
+    struct mpd_song *song = NULL;
+    struct mpd_status *status = NULL;
+	unsigned int elapsed = 0, total = 0;
+
+
+	if (conn == NULL) {
+		conn = mpd_connection_new(NULL, 0, 5000);
+		if (conn == NULL) {
+			warn("MPD error: %s",mpd_connection_get_error_message(conn));
+			goto mpdout;
+		}
+	}
+
+    mpd_send_status(conn);
+	status = mpd_recv_status(conn);
+	if (status == NULL) {
+		goto mpdout;
+	}
+
+	if (mpd_status_get_state(status) == MPD_STATE_PLAY) {
+		mpd_send_current_song(conn);
+		song = mpd_recv_song(conn);
+		if (song == NULL) {
+			goto mpdout;
+		}
+
+		for (int i = 0; i < (int)strlen(fmt) ; i++) {
+			switch (fmt[i]) {
+			case 'a':
+				bprintf("%s%s", buf, mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
+				break;
+			case 't':
+				bprintf("%s%s", buf, mpd_song_get_tag(song, MPD_TAG_TITLE, 0));
+				break;
+			case 'p':
+				elapsed = mpd_status_get_elapsed_ms(status)/1000;
+				total = mpd_status_get_total_time(status);
+				bprintf("%s%.2d:%.2d/%.2d:%.2d",
+					buf,
+					elapsed/60, elapsed%60,
+					total/60, total%60);
+				break;
+			default:
+				bprintf("%s%c", buf, fmt[i]);
+				break;
+			}
+		}
+
+
+		mpd_status_free(status);
+		mpd_song_free(song);
+		mpd_response_finish(conn);
+		return(buf);
+	}
+
+mpdout:
+	mpd_response_finish(conn);
+	mpd_connection_free(conn);
+	conn = NULL;
+	return NULL;
+}
diff --git a/config.def.h b/config.def.h
index e06be66..b4677df 100644
--- a/config.def.h
+++ b/config.def.h
@@ -36,6 +36,10 @@ static const char unknown_str[] = "n/a";
  * keymap              layout (variant) of current     NULL
  *                     keymap
  * load_avg            load average                    NULL
+ * mpdonair            mpd status                      format string 
+ *                     MPD_HOST and MPD_PORT           "t - a [p]"
+ *                     env var are used.               see mpd.c
+ *                     Uncomment lines in Makefile 
  * netspeed_rx         receive network speed           interface name (wlan0)
  * netspeed_tx         transfer network speed          interface name (wlan0)
  * num_files           number of files in a directory  path
diff --git a/slstatus.h b/slstatus.h
index 08f610a..f5bf214 100644
--- a/slstatus.h
+++ b/slstatus.h
@@ -40,6 +40,9 @@ const char *keymap(void);
 /* load_avg */
 const char *load_avg(void);
 
+/* mpd */
+const char *mpdonair(const char *fmt);
+
 /* netspeeds */
 const char *netspeed_rx(const char *interface);
 const char *netspeed_tx(const char *interface);
@@ -79,3 +82,5 @@ const char *vol_perc(const char *card);
 /* wifi */
 const char *wifi_perc(const char *interface);
 const char *wifi_essid(const char *interface);
+
+
-- 
2.28.0

Reply via email to