commit dcdc795b2a8a4854f54bd958594ff386c5e9ac52 Author: depsterr Date: Sun Jan 24 02:38:51 2021 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8ac3a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.mk +mpdart diff --git a/.mpdart.c.swp b/.mpdart.c.swp new file mode 100644 index 0000000..872bd28 Binary files /dev/null and b/.mpdart.c.swp differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d11d416 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.POSIX: +include config.mk +all: mpdart config.mk +mpdart: mpdart.c + $(CC) $(CONFIG) $(CFLAGS) $(CXXFLAGS) -o $@ mpdart.c +clean: + rm -f mpdart +install: mpdart + install -Dm755 mpdart $(DESTDIR)$(PREFIX)/bin/mpdart +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/mpdart diff --git a/configure.sh b/configure.sh new file mode 100755 index 0000000..b84e430 --- /dev/null +++ b/configure.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +if command -V pkg-config >/dev/null 2>&1; then + printf '%s\n' "CONFIG=$(pkg-config libmpdclient --libs --cflags)" > config.mk +else + printf '%s\n' "CONFIG=-lmpdclient" > config.mk +fi diff --git a/mpdart.c b/mpdart.c new file mode 100644 index 0000000..d9280c0 --- /dev/null +++ b/mpdart.c @@ -0,0 +1,95 @@ +#include +#include +#include + +#include + +#include + +char* mpd_host = 0; +unsigned mpd_port = 0; +unsigned mpd_timeout = 0; +struct mpd_connection* connection = 0; + +char* mpd_db_dir = 0; + +int main(int argc, char** argv) { + + /* parse args */ + while (*++argv) { + if (!strcmp(*argv, "-d")) + mpd_db_dir = *++argv; + else if (!strcmp(*argv, "-h")) + mpd_host = *++argv; + else if (!strcmp(*argv, "-p") && argv[1]) + mpd_port = atoi(*++argv); + else if (!strcmp(*argv, "-t") && argv[1]) + mpd_timeout = atoi(*++argv); + } + + if (!mpd_db_dir) { + fprintf(stderr, "Please specify mpd music directory with -d\n"); + exit(1); + } + + + /* strip all '/'es from the end of it */ + { + int i = 0; + for (; mpd_db_dir[i]; i++); + while (mpd_db_dir[--i] == '/'); + mpd_db_dir[i+1] = '\0'; + } + + connection = mpd_connection_new(mpd_host, mpd_port, mpd_timeout); + + if (!connection) { + fprintf(stderr, "Unable to allocate memory for mpd_connection struct\n"); + exit(1); + } + + if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) { + fprintf(stderr, "%s\n", mpd_connection_get_error_message(connection)); + exit(1); + } + + const int* version = mpd_connection_get_server_version(connection); + printf("Connected to mpd server version %d.%d.%d\n", version[0], version[1], version[2]); + + int song_id; + int old_song_id = -1; + + while (1) { + + struct mpd_song* song; + + mpd_send_current_song(connection); + song = mpd_recv_song(connection); + + if (!song) { + fprintf(stderr, "failed to get mpd song\n"); + /* TODO clear image */ + goto wait; + } + + song_id = mpd_song_get_id(song); + + if (song_id != old_song_id) { + /* TODO render image */ + printf("Now playing: '%s' by '%s' from '%s/%s'\n", + mpd_song_get_tag(song, MPD_TAG_TITLE, 0), + mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), + mpd_db_dir, mpd_song_get_uri(song)); + } + + old_song_id = song_id; + + mpd_song_free(song); + +wait: + mpd_send_idle_mask(connection, MPD_IDLE_PLAYER); + while (!mpd_recv_idle(connection, 1)) + sleep(1); + } + +}