From 8a60c98ecb161df1040ccca3259750b1aeb1d2e7 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 19 Jun 2024 21:35:11 +0200 Subject: add a format specifier for duration --- README.md | 1 + config.def.h | 2 +- mpd-notification.c | 14 +++++++++++--- mpd-notification.h | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5130f8a..965e4cc 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ these specifiers: * *%t*: title * *%a*: artist * *%A*: album +* *%d*: duration Artwork ------- diff --git a/config.def.h b/config.def.h index f262cfe..b8ef018 100644 --- a/config.def.h +++ b/config.def.h @@ -27,7 +27,7 @@ /* strings used to display notification messages * TEXT_PLAY & TEXT_PAUSE can include several specifiers: - * %t for title, %a for artist and %A for album */ + * %t for title, %a for artist, %A for album and %d for duration */ #define TEXT_TOPIC "MPD Notification" #define TEXT_PLAY "Playing %t\nby %a\nfrom %A" #define TEXT_PAUSE "Paused %t\nby %a\nfrom %A" diff --git a/mpd-notification.c b/mpd-notification.c index f455e0f..7e7f302 100644 --- a/mpd-notification.c +++ b/mpd-notification.c @@ -213,7 +213,7 @@ done: } /*** format_text ***/ -char * format_text(const char* format, const char* title, const char* artist, const char* album) { +char * format_text(const char* format, const char* title, const char* artist, const char* album, unsigned int duration) { char * formatted, * tmp = NULL; size_t len; @@ -236,6 +236,13 @@ char * format_text(const char* format, const char* title, const char* artist, co tmp = g_markup_escape_text(album, -1); break; + case 'd': + size_t size; + size = snprintf(tmp, 0, "%d:%02d", duration / 60, duration % 60) + 1; + tmp = malloc(size); + snprintf(tmp, size, "%d:%02d", duration / 60, duration % 60); + break; + case 't': tmp = g_markup_escape_text(title, -1); break; @@ -285,7 +292,7 @@ int main(int argc, char ** argv) { * text_play = TEXT_PLAY, * text_pause = TEXT_PAUSE, * text_stop = TEXT_STOP, * uri = NULL; unsigned mpd_port = MPD_PORT, mpd_timeout = MPD_TIMEOUT, notification_timeout = NOTIFICATION_TIMEOUT; struct mpd_song * song = NULL; - unsigned int i, version = 0, help = 0, scale = 0, file_workaround = 0; + unsigned int i, version = 0, help = 0, scale = 0, file_workaround = 0, duration; int rc = EXIT_FAILURE; program = argv[0]; @@ -475,6 +482,7 @@ int main(int argc, char ** argv) { title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0); + duration = mpd_song_get_duration(song); /* ignore if we have no title */ if (title == NULL) @@ -486,7 +494,7 @@ int main(int argc, char ** argv) { /* get the formatted notification string */ notifystr = format_text(state == MPD_STATE_PLAY ? text_play : text_pause, - title, artist ? artist : "unknown artist", album ? album : "unknown album"); + title, artist ? artist : "unknown artist", album ? album : "unknown album", duration); uri = mpd_song_get_uri(song); diff --git a/mpd-notification.h b/mpd-notification.h index 6a07efc..d11e822 100644 --- a/mpd-notification.h +++ b/mpd-notification.h @@ -57,7 +57,7 @@ void received_signal(int signal); GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri); /*** format_text ***/ -char * format_text(const char* format, const char* title, const char* artist, const char* album); +char * format_text(const char* format, const char* title, const char* artist, const char* album, unsigned int duration); /*** main ***/ int main(int argc, char ** argv); -- cgit v1.2.3-70-g09d2