summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2024-06-19 21:35:11 +0200
committerGravatar Christian Hesse <mail@eworm.de>2024-06-19 21:42:47 +0200
commit8a60c98ecb161df1040ccca3259750b1aeb1d2e7 (patch)
treee4b85012df533770dd2416e63fd5305be1589d9e
parentb92cf4af44d7132f9a0e93a42068b87d0f2de614 (diff)
downloadmpd-notification-8a60c98ecb161df1040ccca3259750b1aeb1d2e7.tar.gz
mpd-notification-8a60c98ecb161df1040ccca3259750b1aeb1d2e7.tar.zst
add a format specifier for duration
-rw-r--r--README.md1
-rw-r--r--config.def.h2
-rw-r--r--mpd-notification.c14
-rw-r--r--mpd-notification.h2
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 <b>%t</b>\nby <i>%a</i>\nfrom <i>%A</i>"
#define TEXT_PAUSE "Paused <b>%t</b>\nby <i>%a</i>\nfrom <i>%A</i>"
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);