summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c4
-rw-r--r--src/settings.c3
-rw-r--r--src/settings.h1
-rw-r--r--src/ui.c62
-rw-r--r--src/ui.h4
5 files changed, 74 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 93fa317..06f5975 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
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 <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);
+ }
+}
diff --git a/src/ui.h b/src/ui.h
index 084bce0..794003d 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -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 */