CLI options, fix status

This commit is contained in:
zvon 2020-05-29 20:27:46 +02:00
parent 2abd02f88f
commit e9b63f5727
5 changed files with 89 additions and 28 deletions

View File

@ -209,6 +209,7 @@ bool dbusStatus( DBusConnection *conn, const char *player ) {
dbus_message_iter_recurse( &args, &string );
dbus_message_iter_get_basic( &string, &meta );
bool ret = strcmp( meta, "Playing" ) == 0;
ret |= strcmp( meta, "Paused" ) == 0;
// free reply
dbus_message_unref( msg );
return ret;

93
main.c
View File

@ -7,33 +7,84 @@
#include "dbus_client.h"
#include "mpd_client.h"
int main() {
DBusConnection *conn = dbusConnect();
#define SONG_FLAG 0x0001
#define META_FLAG 0x0002
#define RUNNING_FLAG 0x0004
printf( "Media players on dbus:\n" );
char **dbus_players = dbusGetMediaPlayers( conn );
if ( dbus_players != NULL ) {
for ( char **player = dbus_players; *player; player++ ) {
printf( "%s\n", *player );
struct song_metadata song = dbusGetSong( conn, *player );
printSong( &song );
dbusPlayPause( conn, *player );
free( *player );
#define MPD_FLAG 0x0001
#define DBUS_FLAG 0x0002
void parseCommandLine( int argc, char **argv, size_t *requests,
size_t *sources ) {
for ( int i = 1; i < argc; i++ ) {
if ( !strcmp( argv[i], "song" ) )
*requests |= SONG_FLAG;
else if ( !strcmp( argv[i], "info" ) )
*requests |= META_FLAG;
else if ( !strcmp( argv[i], "status" ) )
*requests |= RUNNING_FLAG;
else if ( !strcmp( argv[i], "mpd" ) )
*sources |= MPD_FLAG;
else if ( !strcmp( argv[i], "dbus" ) )
*sources |= DBUS_FLAG;
}
if ( *sources == 0 )
*sources = MPD_FLAG | DBUS_FLAG;
}
int main( int argc, char **argv ) {
size_t requests = 0, sources = 0;
int ret = EXIT_SUCCESS;
parseCommandLine( argc, argv, &requests, &sources );
DBusConnection *dbus_connection = NULL;
char **dbus_players = NULL;
struct mpd_connection *mpd_connection = NULL;
if ( sources & MPD_FLAG )
mpd_connection = mpdConnect( 6600 );
if ( sources & DBUS_FLAG ) {
dbus_connection = dbusConnect();
dbus_players = dbusGetMediaPlayers( dbus_connection );
}
if ( requests & SONG_FLAG || requests & META_FLAG ) {
struct song_metadata song = { 0 };
if ( sources & MPD_FLAG ) {
song = mpdGetSong( mpd_connection );
if ( requests & SONG_FLAG )
printSong( &song );
if ( requests & META_FLAG )
printMeta( &song );
}
if ( sources & DBUS_FLAG ) {
for ( char **player = dbus_players; *player; ++player ) {
song = dbusGetSong( dbus_connection, *player );
if ( requests & SONG_FLAG )
printSong( &song );
if ( requests & META_FLAG )
printMeta( &song );
}
}
}
free( dbus_players );
dbusDisconnect( conn );
struct mpd_connection *mpd_connection = mpdConnect( 6600 );
if ( mpd_connection != NULL ) {
printf( "MPD is running!\n" );
struct song_metadata song = mpdGetSong( mpd_connection );
printSong( &song );
mpdPlayPause( mpd_connection );
if( requests & RUNNING_FLAG ) {
ret = EXIT_FAILURE;
if( sources & MPD_FLAG && mpdStatus( mpd_connection ) )
ret = EXIT_SUCCESS;
if( sources & DBUS_FLAG ) {
for( char **player = dbus_players; *player; ++player ) {
if( dbusStatus( dbus_connection, *player ) )
ret = EXIT_SUCCESS;
}
}
}
free( dbus_players );
dbusDisconnect( dbus_connection );
mpdDisconnect( mpd_connection );
return 0;
return ret;
}

View File

@ -6,13 +6,19 @@
void printSong( struct song_metadata *song ) {
if ( song->title == NULL )
return;
printf( " TITLE: %s\n ALBUM: %s\n ARTIST: %s\n", song->title,
song->album, song->artist );
printf( "%s - %s\n", song->title, song->artist );
}
void printMeta( struct song_metadata *song ) {
if ( song->title == NULL )
return;
printf( "TITLE: %s\nALBUM: %s\nARTIST: %s\n", song->title, song->album,
song->artist );
if ( song->year != NULL ) {
printf( " YEAR: %s\n", song->year );
printf( "YEAR: %s\n", song->year );
}
printf( " FILE: %s\n", song->file );
printf( "FILE: %s\n", song->file );
if ( song->art_uri != NULL ) {
printf( " ART: %s\n", song->art_uri );
printf( "ART: %s\n", song->art_uri );
}
}

View File

@ -11,5 +11,6 @@ struct song_metadata {
};
void printSong( struct song_metadata *song );
void printMeta( struct song_metadata *song );
#endif

View File

@ -66,8 +66,10 @@ void mpdStop( struct mpd_connection *mpd_connection ) {
}
bool mpdStatus( struct mpd_connection *mpd_connection ) {
struct mpd_status *status = mpd_recv_status( mpd_connection );
struct mpd_status *status = mpd_run_status( mpd_connection );
if( status == NULL )
return false;
enum mpd_state ret = mpd_status_get_state( status );
mpd_status_free( status );
return ret == MPD_STATE_PLAY;
return ret != MPD_STATE_STOP && ret != MPD_STATE_UNKNOWN;
}