UniversalMusicController/mpd_client.c

74 lines
2.0 KiB
C
Raw Normal View History

2020-05-29 12:21:05 +00:00
#include "mpd_client.h"
#include <mpd/player.h>
#include <mpd/song.h>
#include <mpd/status.h>
#include <string.h>
#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 ) );
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 );
}
bool mpdStatus( struct mpd_connection *mpd_connection ) {
struct mpd_status *status = mpd_recv_status( mpd_connection );
enum mpd_state ret = mpd_status_get_state( status );
mpd_status_free( status );
return ret == MPD_STATE_PLAY;
}