Commit: 0277eba1d73570d8bbb38deb8494a866762a055a
Author: opask
Date: Thu, 8 Nov 2018 21:24:38 -0700
initial commit
Diffstat:
A | Makefile | | | 23 | +++++++++++++++++++++++ |
A | config.mk | | | 6 | ++++++ |
A | status.c | | | 123 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,23 @@
+include config.mk
+
+SRC = status.c
+OBJ = $(SRC:.c=.o)
+
+all: status
+
+.c.o:
+ $(CC) $(CFLAGS) -c $<
+
+status: $(OBJ)
+ $(CC) -o $@ $(OBJ) $(LDFLAGS)
+
+clean:
+ rm -f *.o status
+
+install: all
+ mkdir -p $(DESTDIR)$(PREFIX)/bin
+ cp -f status $(DESTDIR)$(PREFIX)/bin
+ chmod 755 $(DESTDIR)$(PREFIX)/bin/status
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/status
diff --git a/config.mk b/config.mk
@@ -0,0 +1,6 @@
+PREFIX = /
+
+LIBS = -lmpdclient -lX11
+
+CFLAGS = -O2 -std=c99 -Wall -pedantic
+LDFLAGS = $(LIBS)
diff --git a/status.c b/status.c
@@ -0,0 +1,123 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <mpd/client.h>
+#include <X11/Xlib.h>
+
+static char buf[1024];
+
+static Display *dpy;
+
+static void
+die(const char *errstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, errstr);
+ vfprintf(stderr, errstr, ap);
+ va_end(ap);
+ exit(1);
+}
+
+static char *
+smprintf(const char *fmt, ...)
+{
+ va_list ap;
+ char *ret;
+ int len;
+
+ va_start(ap, fmt);
+ len = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+
+ if (!(ret = malloc(++len)))
+ die("malloc\n");
+
+ va_start(ap, fmt);
+ vsnprintf(ret, len, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+static void
+setstatus(char *str)
+{
+ XStoreName(dpy, DefaultRootWindow(dpy), str);
+ XSync(dpy, False);
+}
+
+static char *
+gettime(const char *fmt)
+{
+ const time_t t = time(NULL);
+ if (!(strftime(buf, sizeof(buf), fmt, localtime(&t))))
+ return smprintf("");
+
+ return smprintf("%s", buf);
+}
+
+static char *
+mpd(enum mpd_tag_type type)
+{
+ struct mpd_connection *conn = NULL;
+ struct mpd_song *song = NULL;
+ struct mpd_status *status = NULL;
+ char *ret = NULL;
+
+ conn = mpd_connection_new("localhost", 0, 60);
+ if (!conn || mpd_connection_get_error(conn))
+ return smprintf("");
+
+ 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 (status && (mpd_status_get_state(status) == MPD_STATE_PLAY)) {
+ mpd_response_next(conn);
+ song = mpd_recv_song(conn);
+ ret = smprintf("%s", mpd_song_get_tag(song, type, 0));
+ mpd_song_free(song);
+ } else
+ ret = smprintf("");
+
+ mpd_response_finish(conn);
+ mpd_connection_free(conn);
+
+ return ret;
+}
+
+int
+main(void)
+{
+ char *status;
+ char *time;
+ char *song;
+ char *artist;
+
+ if (!(dpy = XOpenDisplay(NULL)))
+ die("XOpenDisplay: can't open display\n");
+
+ for (;; sleep(1)) {
+ time = gettime("%Y年%m月%d日 ♦ %R");
+ song = mpd(MPD_TAG_TITLE);
+ artist = mpd(MPD_TAG_ARTIST);
+
+ status = smprintf("[ %s - %s ][ %s ]", song, artist, time);
+ setstatus(status);
+ free(time);
+ free(song);
+ free(artist);
+ free(status);
+ }
+
+ XStoreName(dpy, DefaultRootWindow(dpy), NULL);
+ XCloseDisplay(dpy);
+
+ return 0;
+}