slstatus/components/mpd.c
2018-07-13 01:05:48 -04:00

54 lines
1.4 KiB
C

#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpd/client.h>
#include "../util.h"
/* add to config.mk :
# mpd
+ MPDLIB = -lmpdclient
+ MPDFLAG = -DMPD
- LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
+ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${MPDLIB}
- CPPFLAGS = -DVERSION=\"${VERSION}\"
+ CPPFLAGS = ${MPDFLAG} -DVERSION=\"${VERSION}\"
*/
/* simple function to retrieve mpd status */
const char *
mpd_song() {
int ret = -1;
struct mpd_song * song = NULL;
struct mpd_connection * conn ;
if (!(conn = mpd_connection_new("localhost", 0, 30000)) ||
mpd_connection_get_error(conn)){
return NULL;
}
mpd_command_list_begin(conn, true);
mpd_send_status(conn);
mpd_send_current_song(conn);
mpd_command_list_end(conn);
struct mpd_status* theStatus = mpd_recv_status(conn);
if ((theStatus) && (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)) {
mpd_response_next(conn);
song = mpd_recv_song(conn);
ret = esnprintf(buf, sizeof(buf), "%s", mpd_song_get_tag(song, MPD_TAG_TITLE, 0));
if (!strncmp(buf, "(null)", sizeof("(null)") - 1))
ret = esnprintf(buf, sizeof(buf), "%s", basename(mpd_song_get_uri(song)));
/*
artist = smprintf("%s",mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
elapsed = mpd_status_get_elapsed_time(theStatus);
total = mpd_status_get_total_time(theStatus);
*/
mpd_song_free(song);
}
mpd_response_finish(conn);
mpd_connection_free(conn);
return (ret < 0) ? NULL : buf;
}