-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpianoclient.pl
executable file
·63 lines (52 loc) · 1.29 KB
/
pianoclient.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl
use strict;
use Fcntl;
use Term::ReadKey;
my $LOOP_WAIT_TIME = 0.100;
my $AUTOSTART_EN = 1;
my $PIANOBAR_INPUT = '/tmp/piano_pipe_in';
my $PIANOBAR_OUTPUT = '/tmp/piano_output';
my $DAEMON_CONTROL_CHAR = '%';
my $daemon_control_mode = 0;
my $daemon_control_buffer = '';
ReadMode 'cbreak';
open(OUT_FILE, "tail -f $PIANOBAR_OUTPUT |") or die "Error: Could not open output fifo ('$PIANOBAR_OUTPUT')\n";
if($AUTOSTART_EN){
send_command("%start");
}
my $done = 0;
MAIN_LOOP:
while(!$done){
while(defined (my $key = ReadKey(-1))) {
if($key eq $DAEMON_CONTROL_CHAR){
$daemon_control_mode = 1;
}
if($daemon_control_mode){
if($key ne "\n"){
$daemon_control_buffer .= $key;
print $key; # echo the key in this mode
}else{
if($daemon_control_buffer =~ m/^%q(uit)?$/i){
$done = 1;
next MAIN_LOOP;
}
send_command($daemon_control_buffer);
$daemon_control_buffer = '';
$daemon_control_mode = 0;
}
}else{ ## Normal mode - send each char individually
send_command($key);
}
}
while(defined (my $key = ReadKey(-1, \*OUT_FILE))){
print $key;
}
sleep($LOOP_WAIT_TIME);
}
close(OUT_FILE);
ReadMode 'normal';
sub send_command {
my($cmd) = @_;
#print "Sending... '$cmd'\n";
system("echo \"$cmd\" > $PIANOBAR_INPUT");
}