aboutsummaryrefslogtreecommitdiffstats
path: root/mpd-notification.c
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 /mpd-notification.c
parent95295a7dd2cf216c86218c89a7625e0654ce33d8 (diff)
downloadmpd-notification-53950b19fc05087cc90aef36ddc391d4c28eb169.tar.gz
mpd-notification-53950b19fc05087cc90aef36ddc391d4c28eb169.tar.zst
rework the notification string handling
Diffstat (limited to 'mpd-notification.c')
-rw-r--r--mpd-notification.c53
1 files changed, 37 insertions, 16 deletions
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;