#include "mpd_client.h" #include #include #include #include #include "metadata.h" struct mpd_connection *mpdConnect( int port ) { struct mpd_connection *ret = mpd_connection_new( "localhost", port, 1000 ); if ( ret == NULL ) return NULL; if ( mpd_connection_get_error( ret ) != MPD_ERROR_SUCCESS ) return NULL; return ret; } void mpdDisconnect( struct mpd_connection *mpd_connection ) { if ( mpd_connection != NULL ) mpd_connection_free( mpd_connection ); } struct song_metadata mpdGetSong( struct mpd_connection *mpd_connection ) { struct song_metadata ret = { 0 }; struct mpd_song *song = mpd_run_current_song( mpd_connection ); if ( song == NULL ) goto end; const char *copy = mpd_song_get_tag( song, MPD_TAG_TITLE, 0 ); if ( copy == NULL ) goto endfree; // TODO check return values ret.title = strdup( copy ); copy = mpd_song_get_tag( song, MPD_TAG_ARTIST, 0 ); if ( copy != NULL ) ret.artist = strdup( copy ); copy = mpd_song_get_tag( song, MPD_TAG_ALBUM, 0 ); if ( copy != NULL ) ret.album = strdup( copy ); copy = mpd_song_get_tag( song, MPD_TAG_DATE, 0 ); if ( copy != NULL ) ret.year = strdup( copy ); ret.file = strdup( mpd_song_get_uri( song ) ); struct mpd_status *status = mpd_run_status( mpd_connection ); if ( status == NULL ) goto endfree; ret.duration = mpd_status_get_total_time( status ); ret.position = mpd_status_get_elapsed_time( status ); mpd_status_free( status ); endfree: mpd_song_free( song ); end: return ret; } void mpdPlayPause( struct mpd_connection *mpd_connection ) { mpd_send_toggle_pause( mpd_connection ); } void mpdNext( struct mpd_connection *mpd_connection ) { mpd_send_next( mpd_connection ); } void mpdPrev( struct mpd_connection *mpd_connection ) { mpd_send_previous( mpd_connection ); } void mpdStop( struct mpd_connection *mpd_connection ) { mpd_send_stop( mpd_connection ); } enum mpd_state mpdStatus( struct mpd_connection *mpd_connection ) { if( mpd_connection == NULL ) return -1; struct mpd_status *status = mpd_run_status( mpd_connection ); if ( status == NULL ) return -1; enum mpd_state ret = mpd_status_get_state( status ); mpd_status_free( status ); return ret; } bool mpdRunning( struct mpd_connection *mpd_connection ) { enum mpd_state state = mpdStatus( mpd_connection ); return state == MPD_STATE_PAUSE || state == MPD_STATE_PLAY; } bool mpdPlaying( struct mpd_connection *mpd_connection ) { enum mpd_state state = mpdStatus( mpd_connection ); return state == MPD_STATE_PLAY; }