initial commit

This commit is contained in:
Rachel Lambda Samuelsson 2021-01-24 02:38:51 +01:00
commit dcdc795b2a
5 changed files with 115 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.mk
mpdart

BIN
.mpdart.c.swp Normal file

Binary file not shown.

11
Makefile Normal file
View File

@ -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

7
configure.sh Executable file
View File

@ -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

95
mpdart.c Normal file
View File

@ -0,0 +1,95 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <mpd/client.h>
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);
}
}