From 575ae18234fa5f4d164457ab4f60616ad71b766a Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 7 Apr 2009 22:48:52 +0200 Subject: Initial eventcmd work More useful events should be added... --- src/ui.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index 694e06c..6154371 100644 --- a/src/ui.c +++ b/src/ui.c @@ -29,6 +29,11 @@ THE SOFTWARE. #include #include #include +#include + +/* waitpid () */ +#include +#include #include "ui.h" @@ -398,3 +403,60 @@ inline void BarUiPrintSong (PianoSong_t *song, PianoStation_t *station) { station != NULL ? " @ " : "", station != NULL ? station->name : ""); } + +/* Excute external event handler + * @param settings containing the cmdline + * @param event type + * @param current station + * @param current song + * @param piano error-code + */ +void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, + const PianoStation_t *curStation, const PianoSong_t *curSong) { + pid_t chld; + char pipeBuf[1024]; + int pipeFd[2]; + + if (settings->eventCmd == NULL) { + /* nothing to do... */ + return; + } + + /* prepare stdin content */ + memset (pipeBuf, 0, sizeof (pipeBuf)); + snprintf (pipeBuf, sizeof (pipeBuf), + "artist=%s\n" + "title=%s\n" + "album=%s\n" + "stationName=%s\n", + curSong->artist, + curSong->title, + curSong->album, + curStation->name); + + if (pipe (pipeFd) == -1) { + BarUiMsg (MSG_ERR, "Cannot create eventcmd pipe. (%s)\n", strerror (errno)); + return; + } + + chld = fork (); + if (chld == 0) { + /* child */ + close (pipeFd[1]); + dup2 (pipeFd[0], fileno (stdin)); + execl (settings->eventCmd, settings->eventCmd, type, NULL); + BarUiMsg (MSG_ERR, "Cannot start eventcmd. (%s)\n", strerror (errno)); + close (pipeFd[0]); + exit (1); + } else if (chld == -1) { + BarUiMsg (MSG_ERR, "Cannot fork eventcmd. (%s)\n", strerror (errno)); + } else { + /* parent */ + int status; + close (pipeFd[0]); + write (pipeFd[1], pipeBuf, strlen (pipeBuf)); + close (pipeFd[1]); + /* wait to get rid of the zombie */ + waitpid (chld, &status, 0); + } +} -- cgit v1.2.3