diff options
author | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2009-04-07 22:48:52 +0200 |
---|---|---|
committer | Lars-Dominik Braun <PromyLOPh@lavabit.com> | 2009-04-07 22:48:52 +0200 |
commit | 575ae18234fa5f4d164457ab4f60616ad71b766a (patch) | |
tree | d22d3dcc59a32bbf9a80ca3fcb3941909e58adfc /src | |
parent | 1945a09a7b665a0c6a3980caa64c16c369bd8016 (diff) | |
download | pianobar-575ae18234fa5f4d164457ab4f60616ad71b766a.tar.gz pianobar-575ae18234fa5f4d164457ab4f60616ad71b766a.tar.bz2 pianobar-575ae18234fa5f4d164457ab4f60616ad71b766a.zip |
Initial eventcmd work
More useful events should be added...
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/settings.c | 3 | ||||
-rw-r--r-- | src/settings.h | 1 | ||||
-rw-r--r-- | src/ui.c | 62 | ||||
-rw-r--r-- | src/ui.h | 4 |
5 files changed, 74 insertions, 0 deletions
@@ -231,6 +231,10 @@ int main (int argc, char **argv) { player.gain = curSong->fileGain; player.audioFormat = curSong->audioFormat; + /* throw event */ + BarUiStartEventCmd (&settings, "songstart", curStation, + curSong); + /* start player */ pthread_create (&playerThread, NULL, BarPlayerThread, &player); diff --git a/src/settings.c b/src/settings.c index a0dabb5..786eb53 100644 --- a/src/settings.c +++ b/src/settings.c @@ -89,6 +89,7 @@ void BarSettingsDestroy (BarSettings_t *settings) { free (settings->lastfmUser); free (settings->lastfmPassword); free (settings->autostartStation); + free (settings->eventCmd); memset (settings, 0, sizeof (*settings)); } @@ -245,6 +246,8 @@ void BarSettingsRead (BarSettings_t *settings) { } } else if (strcmp ("autostart_station", key) == 0) { settings->autostartStation = strdup (val); + } else if (strcmp ("event_command", key) == 0) { + settings->eventCmd = strdup (val); } } diff --git a/src/settings.h b/src/settings.h index 1bea648..3fed541 100644 --- a/src/settings.h +++ b/src/settings.h @@ -52,6 +52,7 @@ struct BarSettings { } *keys; PianoAudioFormat_t audioFormat; char *autostartStation; + char *eventCmd; }; typedef struct BarSettings BarSettings_t; @@ -29,6 +29,11 @@ THE SOFTWARE. #include <stdlib.h> #include <string.h> #include <readline/readline.h> +#include <errno.h> + +/* waitpid () */ +#include <sys/types.h> +#include <sys/wait.h> #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); + } +} @@ -26,6 +26,8 @@ THE SOFTWARE. #include <piano.h> +#include "settings.h" + typedef enum {MSG_NONE, MSG_INFO, MSG_PLAYING, MSG_TIME, MSG_ERR, MSG_QUESTION, MSG_LIST} uiMsg_t; @@ -38,5 +40,7 @@ char *BarUiSelectMusicId (const PianoHandle_t *ph); void BarStationFromGenre (PianoHandle_t *ph); inline void BarUiPrintStation (PianoStation_t *); inline void BarUiPrintSong (PianoSong_t *, PianoStation_t *); +void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, + const PianoStation_t *curStation, const PianoSong_t *curSong); #endif /* _UI_H */ |