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 );
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:45:39 +00:00
|
|
|
enum mpd_state mpdStatus( struct mpd_connection *mpd_connection ) {
|
2020-05-29 18:27:46 +00:00
|
|
|
struct mpd_status *status = mpd_run_status( mpd_connection );
|
|
|
|
if( status == NULL )
|
2020-05-29 18:45:39 +00:00
|
|
|
return -1;
|
2020-05-29 12:21:05 +00:00
|
|
|
enum mpd_state ret = mpd_status_get_state( status );
|
|
|
|
mpd_status_free( status );
|
2020-05-29 18:45:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpdRunning( struct mpd_connection *mpd_connection ) {
|
|
|
|
enum mpd_state state = mpdStatus( mpd_connection );
|
|
|
|
return state != MPD_STATE_STOP && state != MPD_STATE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpdPlaying( struct mpd_connection *mpd_connection ) {
|
|
|
|
enum mpd_state state = mpdStatus( mpd_connection );
|
|
|
|
return state == MPD_STATE_PLAY;
|
2020-05-29 12:21:05 +00:00
|
|
|
}
|