albertz/shairport — Issue #119: new feature: run program on first connection / run program on last closed connection
Author: stefdude
Created: 2011-10-09 22:40:01+00:00
State: open

I was looking to trigger an action when I connect to shairport.
Basically, I want to switch on the stereo when I start playing music over shairport (and I want to switch if off when I stop streaming).

Because my set-up will be different than any other set-up, I thought about making it generic.

Two new command-line options --play_prog and --stop_prog take one argument: the command line to start when the first connection is made or when the last connection quits.

```
diff --git a/shairport.pl b/shairport.pl
index 8b6fcc6..f921ae5 100755
--- a/shairport.pl
+++ b/shairport.pl
@@ -71,6 +71,9 @@ my $cliport;
my $mac;
# SB volume
my $volume;
+# custom play and stop program
+my $play_prog;
+my $stop_prog;
# output debugging information
my $verbose;
# where to write PID
@@ -97,6 +100,8 @@ GetOptions("a|apname=s" => \$apname,
"s|squeezebox" => \$squeeze,
"c|cliport=s" => \$cliport,
"m|mac=s" => \$mac,
+ "play_prog=s" => \$play_prog,
+ "stop_prog=s" => \$stop_prog,
"l|volume=s" => \$volume,
"h|help" => \$help);

@@ -117,6 +122,8 @@ sub usage {
" -c --cliport=port Sets the SBS CLI port\n",
" -m --mac=address Sets the SB target device\n",
" -l --volume=level Sets the SB volume level (in %)\n",
+ " --play_prog=cmdline Program to be started on connection\n",
+ " --stop_prog=cmdline Program to be started on disconnection\n",
" -d Daemon mode\n",
" -w --writepid=path Write PID to this location\n",
" -v --verbose Print debugging messages\n",
@@ -491,6 +498,11 @@ while (1) {
if (defined($squeeze) && $squeeze) {
&performSqueezeboxSetup();
}
+
+ # the 2nd connection is a player connection
+ if (defined($play_prog) && $sel->count() == 2) {
+ system($play_prog);
+ }
} else {
if (eof($fh)) {
print "Closed: $fh\n" if $verbose;
@@ -502,6 +514,11 @@ while (1) {
eval { kill $conns{$fh}{decoder_pid} };
}
delete $conns{$fh};
+
+ # 1 connection means no connection
+ if (defined($stop_prog) && $sel->count() == 1) {
+ system($stop_prog);
+ }
next;
}
if (exists $conns{$fh}) {
```