diff options
author | Christian Hesse <mail@eworm.de> | 2013-04-15 22:13:25 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2013-04-15 22:13:25 +0200 |
commit | c6d4e6daf03df68d4c0bb756520858ee9679712c (patch) | |
tree | 13cadc0d2837a41078df8ec8e74e9cb2c9ad906b | |
parent | 6244b57206f637557b605fe4752faf11661d88c6 (diff) | |
download | mpd-notification-c6d4e6daf03df68d4c0bb756520858ee9679712c.tar.gz mpd-notification-c6d4e6daf03df68d4c0bb756520858ee9679712c.tar.zst |
initial commit0.4.0
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | mpd-notification.c | 127 | ||||
-rw-r--r-- | mpd-notification.desktop | 8 |
4 files changed, 150 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9808650 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*.o +mpd-notification diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cf6a78 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +# mpd-notification - Notify about tracks played by mpd + +CC := gcc +CFLAGS += $(shell pkg-config --cflags --libs libmpdclient) $(shell pkg-config --cflags --libs libnotify) +VERSION = $(shell git describe --tags --long) + +all: mpd-notification.c + $(CC) $(CFLAGS) -o mpd-notification mpd-notification.c \ + -DVERSION="\"$(VERSION)\"" + +clean: + /bin/rm -f *.o *~ mpd-notification diff --git a/mpd-notification.c b/mpd-notification.c new file mode 100644 index 0000000..6fb9921 --- /dev/null +++ b/mpd-notification.c @@ -0,0 +1,127 @@ +/* + * (C) 2011-2013 by Christian Hesse <mail@eworm.de> + * + * This software may be used and distributed according to the terms + * of the GNU General Public License, incorporated herein by reference. + */ + +#include <mpd/client.h> + +#include <libnotify/notify.h> + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +#define PROGNAME "mpd-notification" + +#define NOTIFICATION_TIMEOUT 10000 +#ifndef DEBUG +#define DEBUG 0 +#endif + +#define ICON_SOUND "sound" + +#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_UNKNOWN "(unknown)" + +int main(int argc, char ** argv) { + char * album = NULL, * artist = NULL, * notifystr = NULL, * title = NULL; + GError * error = NULL; + unsigned short int errcount = 0, state = MPD_STATE_UNKNOWN; + NotifyNotification * notification = NULL; + struct mpd_connection * conn = NULL; + struct mpd_song * song = NULL; + + printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", argv[0], PROGNAME, VERSION); + + conn = mpd_connection_new(NULL, 0, 30000); + + if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { + fprintf(stderr,"%s: %s\n", argv[0], mpd_connection_get_error_message(conn)); + mpd_connection_free(conn); + exit(EXIT_FAILURE); + } + + if(!notify_init(PROGNAME)) { + fprintf(stderr, "%s: Can't create notify.\n", argv[0]); + exit(EXIT_FAILURE); + } + + notification = notify_notification_new(TEXT_TOPIC, NULL, ICON_SOUND); + + while(mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) { + mpd_command_list_begin(conn, true); + mpd_send_status(conn); + mpd_send_current_song(conn); + mpd_command_list_end(conn); + + state = mpd_status_get_state(mpd_recv_status(conn)); + if (state == MPD_STATE_PLAY) { + mpd_response_next(conn); + + song = mpd_recv_song(conn); + + if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL) + title = TEXT_UNKNOWN; + if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL) + artist = TEXT_UNKNOWN; + if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL) + album = TEXT_UNKNOWN; + + notifystr = (char *) malloc(strlen(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album)); + sprintf(notifystr, TEXT_PLAY, title, artist, album); + + mpd_song_free(song); + } else if (state == MPD_STATE_PAUSE) + notifystr = TEXT_PAUSE; + else if (state == MPD_STATE_STOP) + notifystr = TEXT_STOP; + else + notifystr = TEXT_UNKNOWN; + +#if DEBUG + printf("%s: %s\n", argv[0], notifystr); +#endif + + notify_notification_update(notification, TEXT_TOPIC, notifystr, ICON_SOUND); + + notify_notification_set_timeout(notification, NOTIFICATION_TIMEOUT); + notify_notification_set_category(notification, PROGNAME); + notify_notification_set_urgency (notification, NOTIFY_URGENCY_NORMAL); + + while(!notify_notification_show(notification, &error)) { + if (errcount > 1) { + fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", argv[0]); + exit(EXIT_FAILURE); + } else { + g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", argv[0], error->message); + errcount++; + + g_error_free(error); + error = NULL; + + notify_uninit(); + + usleep(500 * 1000); + + if(!notify_init(PROGNAME)) { + fprintf(stderr, "%s: Can't create notify.\n", argv[0]); + exit(EXIT_FAILURE); + } + } + } + errcount = 0; + + if (state == MPD_STATE_PLAY) + free(notifystr); + mpd_response_finish(conn); + } + mpd_connection_free(conn); + + return EXIT_SUCCESS; +} diff --git a/mpd-notification.desktop b/mpd-notification.desktop new file mode 100644 index 0000000..1dec877 --- /dev/null +++ b/mpd-notification.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Name=MPD Notification +Icon=sound +Exec=/usr/bin/mpd-notification +Terminal=false +Type=Application +StartupNotify=false +Categories=Utility |