-
Notifications
You must be signed in to change notification settings - Fork 5
/
screenStayOnDaemon
executable file
·83 lines (70 loc) · 1.63 KB
/
screenStayOnDaemon
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
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
use strict;
use warnings;
use POSIX 'setsid';
my $usage = "Usage: $0 [on|off|toggle|get] {default is toggle}\n";
my $pidfile = '/tmp/screenStayOnDaemon';
my $delay = 10;
my @dbusCmd = qw(
dbus-send
--system
--type=method_call
--print-reply
--dest=com.nokia.mce
/com/nokia/mce/request
com.nokia.mce.request.req_display_blanking_pause
);
sub daemonize(){
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
}
sub getPid(){
my $pid = `cat $pidfile 2>/dev/null`;
chomp $pid;
return $pid;
}
sub setPid(){
system "echo $$ > $pidfile";
}
sub isRunning(){
my $pid = getPid;
if($pid !~ /^\d+$/){
return 0;
}
my $psLine = `ps 2>/dev/null | grep "^ *$pid "`;
if($? == 0 and $psLine =~ /$0/){
return 1;
}else{
return 0;
}
}
sub main(@){
my $arg = shift;
$arg = 'toggle' if not defined $arg;
die $usage if @_ > 0 or $arg !~ /^(on|off|toggle|get)$/;
my $running = isRunning;
$arg = $running ? 'off' : 'on' if $arg eq 'toggle';
if($arg eq 'get'){
print $running ? "ON\n" : "OFF\n";
exit 0;
}
if($running){
print "stopping daemon...\n";
system "kill", "-9", getPid;
}
if($arg eq 'on'){
print "starting daemon...\n";
daemonize;
setPid;
while(1){
system @dbusCmd;
sleep $delay;
}
}
}
&main(@ARGV);