more work

This commit is contained in:
Bob 2014-02-09 21:12:36 +01:00
parent dbf28d2215
commit 3ab4903c91
2 changed files with 67 additions and 1 deletions

View file

@ -31,6 +31,7 @@
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <getopt.h>
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();

View file

@ -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