-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanic.pl
73 lines (60 loc) · 1.57 KB
/
panic.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
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
use strict;
use Device::USB::PanicButton;
use Time::HiRes qw( usleep );
use Cwd qw( abs_path );
use FindBin qw($Bin);
use Getopt::Long;
use constant MPLAYER_PATH => '/usr/bin/mplayer';
use constant POLL_INTERVAL => 200000; #nanoseconds
my $nofail = '';
my $verbose = '';
my $help = '';
my $video = $Bin . "/panic-video.avi";
GetOptions('nofail' => \$nofail, 'help' => \$help, 'verbose' => \$verbose, 'video=s' => \$video);
if ($help) {
print(STDERR $0 . " [--nofail] [--verbose] [--video=/path/to/video.avi]
Options:
--nofail Persist polling for a panic button, even if none is plugged in
or if there is an error state.
--verbose Print when the button is pushed.
--video Specify the video to play instead of the default 'panic-video.avi'.
");
exit(0);
}
my $pbutton;
if ($verbose) {
print("Video path is $video \n");
}
while (1) {
if (!init_pbutton(\$pbutton)) {
if (!$nofail) {
exit(-1);
}
} else {
my $result = $pbutton->pressed();
if ($result == 1) {
if ($verbose) {
print("ALERT! PANIC!\n");
}
system(MPLAYER_PATH . " $video -ao sdl &> /dev/null");
}
}
usleep(POLL_INTERVAL);
}
sub init_pbutton {
my $pbutton = shift(@_);
if (!$$pbutton) {
$$pbutton = Device::USB::PanicButton->new();
if (!$$pbutton) {
print(STDERR "Could not create PanicButton USB object.\n");
return 0;
}
}
if ($$pbutton->error()) {
print(STDERR "PanicButton USB object is in error state: " . $$pbutton->error() . "\n");
undef $$pbutton;
return 0;
}
return 1;
}