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_recurse( &args, &string );
dbus_message_iter_get_basic( &string, &meta ); dbus_message_iter_get_basic( &string, &meta );
bool ret = strcmp( meta, "Playing" ) == 0; bool ret = strcmp( meta, "Playing" ) == 0;
ret |= strcmp( meta, "Paused" ) == 0;
// free reply // free reply
dbus_message_unref( msg ); dbus_message_unref( msg );
return ret; return ret;

95
main.c
View File

@ -7,33 +7,84 @@
#include "dbus_client.h" #include "dbus_client.h"
#include "mpd_client.h" #include "mpd_client.h"
int main() { #define SONG_FLAG 0x0001
DBusConnection *conn = dbusConnect(); #define META_FLAG 0x0002
#define RUNNING_FLAG 0x0004
printf( "Media players on dbus:\n" ); #define MPD_FLAG 0x0001
char **dbus_players = dbusGetMediaPlayers( conn ); #define DBUS_FLAG 0x0002
if ( dbus_players != NULL ) {
for ( char **player = dbus_players; *player; player++ ) { void parseCommandLine( int argc, char **argv, size_t *requests,
printf( "%s\n", *player ); size_t *sources ) {
struct song_metadata song = dbusGetSong( conn, *player ); 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 ); printSong( &song );
dbusPlayPause( conn, *player ); if ( requests & META_FLAG )
free( *player ); 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 );
} }
} }
}
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 ); free( dbus_players );
dbusDisconnect( conn ); dbusDisconnect( dbus_connection );
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 );
}
mpdDisconnect( mpd_connection ); mpdDisconnect( mpd_connection );
return 0; return ret;
} }

View File

@ -6,8 +6,14 @@
void printSong( struct song_metadata *song ) { void printSong( struct song_metadata *song ) {
if ( song->title == NULL ) if ( song->title == NULL )
return; return;
printf( " TITLE: %s\n ALBUM: %s\n ARTIST: %s\n", song->title, printf( "%s - %s\n", song->title, song->artist );
song->album, 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 ) { if ( song->year != NULL ) {
printf( "YEAR: %s\n", song->year ); printf( "YEAR: %s\n", song->year );
} }

View File

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

View File

@ -66,8 +66,10 @@ void mpdStop( struct mpd_connection *mpd_connection ) {
} }
bool mpdStatus( 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 ); enum mpd_state ret = mpd_status_get_state( status );
mpd_status_free( status ); mpd_status_free( status );
return ret == MPD_STATE_PLAY; return ret != MPD_STATE_STOP && ret != MPD_STATE_UNKNOWN;
} }