summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2015-07-09 09:14:08 +0200
committerGravatar Christian Hesse <mail@eworm.de>2015-07-09 09:14:08 +0200
commit53950b19fc05087cc90aef36ddc391d4c28eb169 (patch)
treeb644d552cfe025cb0f21232952eb4b42375fcad8
parent95295a7dd2cf216c86218c89a7625e0654ce33d8 (diff)
downloadmpd-notification-53950b19fc05087cc90aef36ddc391d4c28eb169.tar.gz
mpd-notification-53950b19fc05087cc90aef36ddc391d4c28eb169.tar.zst
rework the notification string handling
-rw-r--r--config.def.h14
-rw-r--r--mpd-notification.c53
-rw-r--r--mpd-notification.h3
3 files changed, 48 insertions, 22 deletions
diff --git a/config.def.h b/config.def.h
index 934f64c..8e86b5a 100644
--- a/config.def.h
+++ b/config.def.h
@@ -17,12 +17,14 @@
/* strings used to display notification messages
* TEXT_PLAY needs to include three '%s', in order:
* title, artist, album */
-#define TEXT_TOPIC "MPD Notification"
-#define TEXT_PLAY "Playing <b>%s</b>\nby <i>%s</i>\nfrom <i>%s</i>"
-#define TEXT_PAUSE "Paused playback"
-#define TEXT_STOP "Stopped playback"
-#define TEXT_NONE "No action received yet."
-#define TEXT_UNKNOWN "(unknown)"
+#define TEXT_TOPIC "MPD Notification"
+#define TEXT_PLAY_TITLE "Playing <b>%s</b>"
+#define TEXT_PLAY_ARTIST "\nby <i>%s</i>"
+#define TEXT_PLAY_ALBUM "\nfrom <i>%s</i>"
+#define TEXT_PAUSE "Paused playback"
+#define TEXT_STOP "Stopped playback"
+#define TEXT_NONE "No action received yet."
+#define TEXT_UNKNOWN "(unknown)"
/* this is a regular expression that has to match image filename used
* for artwork */
diff --git a/mpd-notification.c b/mpd-notification.c
index e79bc68..862832a 100644
--- a/mpd-notification.c
+++ b/mpd-notification.c
@@ -171,9 +171,25 @@ found:
return icon;
}
+/*** append_string ***/
+char * append_string(char * string, const char * format, const char * s) {
+ char * tmp;
+
+ tmp = g_markup_escape_text(s, -1);
+
+ string = realloc(string, strlen(string) + strlen(format) + strlen(tmp));
+
+ sprintf(string + strlen(string), format, tmp);
+
+ free(tmp);
+
+ return string;
+}
+
/*** main ***/
int main(int argc, char ** argv) {
- char * album = NULL, * artist = NULL, * icon = NULL, * notifystr = NULL, * title = NULL;
+ const char * title = NULL, * artist = NULL, * album = NULL;
+ char * icon = NULL, * notifystr = NULL;
GError * error = NULL;
unsigned short int errcount = 0, state = MPD_STATE_UNKNOWN;
const char * mpd_host = MPD_HOST, * music_dir = NULL, * uri = NULL;
@@ -296,6 +312,23 @@ int main(int argc, char ** argv) {
song = mpd_recv_song(conn);
+ title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
+
+ /* ignore if we have no title */
+ if (title == NULL)
+ continue;
+
+ /* initial allocation and string termination */
+ notifystr = strdup("");
+
+ notifystr = append_string(notifystr, TEXT_PLAY_TITLE, title);
+
+ if ((artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)) != NULL)
+ notifystr = append_string(notifystr, TEXT_PLAY_ARTIST, artist);
+
+ if ((album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)) != NULL)
+ notifystr = append_string(notifystr, TEXT_PLAY_ALBUM, album);
+
uri = mpd_song_get_uri(song);
if (music_dir != NULL && uri != NULL)
@@ -304,20 +337,6 @@ int main(int argc, char ** argv) {
if (verbose > 0 && icon != NULL)
printf("%s: found icon: %s\n", program, icon);
- if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL)
- title = strdup(TEXT_UNKNOWN);
- if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL)
- artist = strdup(TEXT_UNKNOWN);
- if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL)
- album = strdup(TEXT_UNKNOWN);
-
- notifystr = malloc(sizeof(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album));
- sprintf(notifystr, TEXT_PLAY, title, artist, album);
-
- free(title);
- free(artist);
- free(album);
-
mpd_song_free(song);
} else if (state == MPD_STATE_PAUSE)
notifystr = TEXT_PAUSE;
@@ -356,8 +375,10 @@ int main(int argc, char ** argv) {
}
errcount = 0;
- if (state == MPD_STATE_PLAY)
+ if (notifystr != NULL) {
free(notifystr);
+ notifystr = NULL;
+ }
if (icon != NULL) {
free(icon);
icon = NULL;
diff --git a/mpd-notification.h b/mpd-notification.h
index 1da743d..c0e3f0f 100644
--- a/mpd-notification.h
+++ b/mpd-notification.h
@@ -40,6 +40,9 @@ char * retrieve_album_art(const char *path);
/*** get_icon ***/
char * get_icon(const char * music_dir, const char * uri);
+/*** append_string ***/
+char * append_string(char * string, const char * format, const char * s);
+
/*** main ***/
int main(int argc, char ** argv);