OSDN Git Service

Add MPD support
authorwifiextender <router@archlinux.info>
Fri, 22 Jul 2016 07:22:34 +0000 (09:22 +0200)
committerwifiextender <router@archlinux.info>
Fri, 22 Jul 2016 07:22:34 +0000 (09:22 +0200)
README.md
bootstrap
img/pic3.png [new file with mode: 0644]
m4/tests.m4
src/constants1.h
src/functions.c
src/functions.h
src/main.c

index 3099f34..87568ce 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,9 @@
 ![](img/pic.png)
 ![](img/pic2.png)
 
+With MPD
+![](img/pic3.png)
+
 Statusbar program for ~~dwm and xmonad~~ any WM that I've written in my very first day as dwm user.
 
 Please note that the program won't detect fans connected via molex connetor(s) or external fan controller. Also I have not tested it with fan splitter(s) either.
@@ -21,6 +24,8 @@ make -j$(grep -c '^processor' /proc/cpuinfo)
 make install
 ```
 
+To see the current playing song add **--with-mpd** to configure.
+
 Put the following in your **xinitrc** or the script used to start dwm.
 
 ```bash
@@ -51,6 +56,8 @@ make -j$(grep -c '^processor' /proc/cpuinfo)
 make install
 ```
 
+To see the current playing song add **--with-mpd** to configure.
+
 Put the following in your **xinitrc** or the script used to start xmonad.
 
 ```bash
@@ -84,6 +91,11 @@ Replace **distro** with archlinux, debian, gentoo, slackware, rhel, frugalware,
 * alsa-utils
 * alsa-lib
 
+requirements to see the current playing song:
+
+* libmpdclient
+* mpd (with properly configured config)
+
 requirements for xmonad or other non-dwm WM:
 
 * dzen2
index bf6a41b..abc8b1b 100644 (file)
--- a/bootstrap
+++ b/bootstrap
@@ -69,6 +69,7 @@ _gen_files() {
   # The linker flags tests in m4 dir
   TEST_X11
   TEST_ALSA
+  TEST_MPD
   TEST_TYPEZ
 
   AC_CONFIG_FILES([
@@ -117,7 +118,7 @@ _gen_files() {
     -Wformat=0 -Wmissing-prototypes -Wno-unused-result \
     -Wno-unused-function -pedantic -Wno-missing-field-initializers
 
-  '${prog_name}'_LDADD = $(X_LIBS) $(ALSA_LIBS)
+  '${prog_name}'_LDADD = $(X_LIBS) $(ALSA_LIBS) $(MPD_LIBS)
 
   '${prog_name}'_SOURCES = '${src_files[@]}'
   ')
diff --git a/img/pic3.png b/img/pic3.png
new file mode 100644 (file)
index 0000000..b70d1ba
Binary files /dev/null and b/img/pic3.png differ
index a3a24dd..a2ff492 100644 (file)
@@ -59,6 +59,35 @@ AC_DEFUN([TEST_X11],[
 ])
 
 
+dnl TEST_MPD() function in configure.ac
+dnl
+dnl Substitute the linker flags -lmpdclient to the
+dnl the variable 'MPD_LIBS' if the user enabled
+dnl the --with-mpd switch
+AC_DEFUN([TEST_MPD],[
+  MPD_LIBS=""
+
+  AC_ARG_WITH([mpd],
+    AS_HELP_STRING([--with-mpd],
+      [mpd linker flag to show the current playing song]),
+    [],
+    [with_mpd=no]
+  )
+
+  AS_IF([test "x$with_mpd" = "xyes"], [
+    AC_CHECK_HEADERS([mpd/client.h], [
+      MPD_LIBS="-lmpdclient"
+      ],[
+        ERR([Install libmpdclient in order to compile the program.])
+      ])
+    ]
+  )
+
+  AC_SUBST(MPD_LIBS)
+
+])
+
+
 dnl Internal function to perform
 dnl explicit data check type
 AC_DEFUN([CHECK_TYPEZ],[
index f628124..e280735 100644 (file)
@@ -33,6 +33,7 @@
 #define NAME_VAL  "\x0a%s \x0b%s"   /* STR1 STR2        */
 #define TEMP      "\x09%sC\x0b%c "  /* 32C,             */
 #define FMT_KERN  "\x09%s%c "       /* Kernel Version,  */
+#define FMT_SONG  "\x0b%s  "        /* Song             */
 
 #define CPU_STR    "CPU"
 #define RAM_STR    "RAM"
@@ -57,6 +58,7 @@
 #define NAME_VAL  BLUE"%s "PINK"%s"   /* STR1 STR2        */
 #define TEMP      YELLOW"%s^i("ICONS_DIR"/temp.xbm)"PINK"%c "  /* 32C, */
 #define FMT_KERN  YELLOW"%s%c "       /* Kernel Version,  */
+#define FMT_SONG  PINK"%s  "          /* Song             */
 
 #define CPU_STR    XBM_ICON("cpu.xbm")
 #define RAM_STR    XBM_ICON("mem.xbm")
index 028ab0b..362cd92 100644 (file)
 #include <X11/Xlib.h>
 #endif
 
+#if defined (HAVE_MPD_CLIENT_H)
+#include <mpd/client.h>
+#endif
+
 #include <alsa/asoundlib.h>
 
 #include "constants1.h"
@@ -400,3 +404,29 @@ set_status(const char *str1) {
   }
 }
 #endif
+
+
+#if defined (HAVE_MPD_CLIENT_H)
+void
+get_song(char *str1) {
+  struct mpd_connection *conn;
+  struct mpd_song *song;
+
+  if (NULL == (conn = mpd_connection_new(NULL, 0, 0))) {
+    return;
+  }
+  if (!(mpd_send_command(conn, "currentsong", NULL)) ||
+      mpd_connection_get_error(conn)) {
+    return;
+  }
+  if (NULL == (song = mpd_recv_song(conn))) {
+    if (NULL != conn) {
+      mpd_connection_free(conn);
+    }
+    return;
+  }
+  mpd_connection_free(conn);
+
+  FILL_STR_ARR(1, str1, mpd_song_get_uri(song));
+}
+#endif
index 57a678f..fbb73b4 100644 (file)
@@ -36,4 +36,8 @@ void get_volume(char *);
 void set_status(const char *);
 #endif
 
+#if defined (HAVE_MPD_CLIENT_H)
+void get_song(char *);
+#endif
+
 #endif /* FUNCTIONS_H_ */
index 28fb407..0944efc 100644 (file)
@@ -36,17 +36,20 @@ int main(void) {
   struct timespec tc = {0};
   tc.tv_nsec = sysconf(_SC_CLK_TCK) * 1000000L;
 
-  char packs[VLA], mobo[VLA], cpu[VLA], ram[VLA], ssd[VLA];
+  char packs[VLA], mobo[VLA], cpu[VLA], ram[VLA], ssd[VLA], song[VLA];
   char kern[VLA], volume[VLA], Time[VLA], combine[WHOLE_MAIN_ARR_LEN];
   char voltage[VLA], cpu_temp[VLA], mobo_temp[VLA], fans[VLA];
 
   get_cpu(cpu, cpu_temp);
-
   if (-1 == (nanosleep(&tc, NULL))) {
     printf("%s\n", "Error: nanosleep() failed");
     return EXIT_FAILURE;
   }
 
+#if defined (HAVE_MPD_CLIENT_H)
+  get_song(song);
+#endif
+
   get_cpu(cpu, cpu_temp);
   get_ram(ram);
   get_ssd(ssd);
@@ -60,10 +63,12 @@ int main(void) {
 
   snprintf(combine, WHOLE_MAIN_ARR_LEN,
     /* formatting specifiers */
+    FMT_SONG
     FMT_CPU FMT_RAM FMT_SSD FMT_PKGS FMT_KERN
     FMT_VOLT FMT_FANS FMT_MOBO FMT_VOL FMT_TIME,
 
     /* the data */
+    song,
     CPU_STR, cpu, cpu_temp, COMMA,
     RAM_STR, ram, COMMA,
     SSD_STR, ssd, COMMA,