From 3ab4903c911565a7f61435d839c0ab5df0acecbb Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 9 Feb 2014 21:12:36 +0100 Subject: [PATCH] more work --- src/ampswitch.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++- src/ampswitch.h | 5 ++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/ampswitch.cpp b/src/ampswitch.cpp index d4f48aa..d8d88bb 100644 --- a/src/ampswitch.cpp +++ b/src/ampswitch.cpp @@ -31,6 +31,7 @@ #include #include #include +#include volatile bool g_stop = false; @@ -47,6 +48,40 @@ CAmpSwitch::CAmpSwitch(int argc, char *argv[]) m_samplecounter = 0; m_samplerate = 0; m_jackshutdown = false; + m_oncommand = NULL; + m_offcommand = NULL; + + struct option longoptions[] = + { + {"on-command", required_argument, NULL, 'n'}, + {"off-command", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {0, 0, 0, 0} + }; + + const char* shortoptions = "n:f:h"; + int c; + int optionindex = 0; + while ((c = getopt_long(argc, argv, shortoptions, longoptions, &optionindex)) != -1) + { + if (c == 'n') + { + m_oncommand = optarg; + } + else if (c == 'f') + { + m_offcommand = optarg; + } + else if (c == 'h') + { + PrintHelpMessage(); + exit(1); + } + else if (c == '?') + { + exit(1); + } + } } CAmpSwitch::~CAmpSwitch() @@ -86,7 +121,18 @@ void CAmpSwitch::Process() select(m_pipe[0] + 1, &pipeset, NULL, NULL, &tv); if (FD_ISSET(m_pipe[0], &pipeset)) - printf("Current state: %s\n", m_switchedon ? "on" : "off"); + { + if (m_switchedon && m_oncommand) + { + printf("switching on, executing \"%s\"\n", m_oncommand); + system(m_oncommand); + } + else if (!m_switchedon && m_offcommand) + { + printf("switching off, executing \"%s\"\n", m_offcommand); + system(m_offcommand); + } + } uint8_t byte; while (read(m_pipe[0], &byte, 1) == 1); @@ -98,6 +144,21 @@ void CAmpSwitch::Cleanup() JackDisconnect(); } +void CAmpSwitch::PrintHelpMessage() +{ + printf( + "\n" + "usage: ampswitch [OPTION]\n" + "\n" + " options:\n" + "\n" + " -n, --on-command command to execute when switching on\n" + " -f, --off-command command to execute when switching off\n" + " -h, --help print this message\n" + "\n" + ); +} + void CAmpSwitch::Connect() { m_connected = JackConnect(); diff --git a/src/ampswitch.h b/src/ampswitch.h index c0903a8..98cda5f 100644 --- a/src/ampswitch.h +++ b/src/ampswitch.h @@ -32,6 +32,8 @@ class CAmpSwitch void Cleanup(); private: + void PrintHelpMessage(); + void Connect(); bool JackConnect(); void JackDisconnect(); @@ -57,6 +59,9 @@ class CAmpSwitch float m_switchtime; int m_samplecounter; bool m_jackshutdown; + + char* m_oncommand; + char* m_offcommand; }; #endif //AMPSWITCH_H