mpdstatus.c (1840B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <mpd/client.h> 5 6 /* add to config.mk : 7 # mpd 8 + MPDLIB = -lmpdclient 9 + MPDFLAG = -DMPD 10 11 - LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 12 + LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${MPDLIB} 13 14 - CPPFLAGS = -DVERSION=\"${VERSION}\" 15 + CPPFLAGS = ${MPDFLAG} -DVERSION=\"${VERSION}\" 16 */ 17 /* simple function to retrieve mpd status */ 18 char * 19 getmpdstat() { 20 struct mpd_song * song = NULL; 21 const char * title = NULL; 22 const char * artist = NULL; 23 char * retstr = NULL; 24 int elapsed = 0, total = 0; 25 struct mpd_connection * conn ; 26 if (!(conn = mpd_connection_new("localhost", 0, 30000)) || 27 mpd_connection_get_error(conn)){ 28 return smprintf(""); 29 } 30 31 mpd_command_list_begin(conn, true); 32 mpd_send_status(conn); 33 mpd_send_current_song(conn); 34 mpd_command_list_end(conn); 35 36 struct mpd_status* theStatus = mpd_recv_status(conn); 37 if ((theStatus) && (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)) { 38 mpd_response_next(conn); 39 song = mpd_recv_song(conn); 40 title = smprintf("%s",mpd_song_get_tag(song, MPD_TAG_TITLE, 0)); 41 artist = smprintf("%s",mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)); 42 43 elapsed = mpd_status_get_elapsed_time(theStatus); 44 total = mpd_status_get_total_time(theStatus); 45 mpd_song_free(song); 46 retstr = smprintf("%.2d:%.2d/%.2d:%.2d %s - %s", 47 elapsed/60, elapsed%60, 48 total/60, total%60, 49 artist, title); 50 free((char*)title); 51 free((char*)artist); 52 } 53 else retstr = smprintf(""); 54 mpd_response_finish(conn); 55 mpd_connection_free(conn); 56 return retstr; 57 }