-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbacklight
executable file
·144 lines (125 loc) · 2.9 KB
/
backlight
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl -w
# Mon May 5 16:37:19 BST 2014
(my $email='XXX%YYY,ch')=~ tr/%,/@./;
use strict;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname on|off|low|verylow|mid|list|status
if no command is given, status is presumed.
'list' shows the map of names to values.
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose=0;
#our $opt_dry;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
#"dry-run"=> \$opt_dry,
) or exit 1;
usage unless @ARGV<=1;
our @bases=
(
['/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video0'
=> 'x200'], # ThinkPad
['/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-LVDS-1/intel_backlight/'
=> 'nc2400'], # Compaq
['/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/backlight/acpi_video0/'
=> 'T61'], # Thinkpad
);
our ($base,$system)=
&{sub {
for (@bases) {
my ($base,$system)=@$_;
if (-d $base) {
return @$_
}
}
die "unknown system, need to adapt"
}};
our $brightnesses=
+{
T61=> +{ #off=> -1, # nope
on=> 15,
mid=> 7,
low=> 3,
verylow=> 0 },
nc2400=> +{ off=> 0,
on=> 15625,
mid=> 12625,
low=> 10625,
verylow=> 8625 },
x200=> +{ #off=> 0,
on=> 15,
mid=> 7,
low=> 3,
verylow=> 0 },
};
#XXX lib (exist already?)
sub RevertHash ($) {
my ($h)=@_;
my $hh= +{};
for my $k (keys %$h) {
my $v= $$h{$k};
die "can't revert, double entry for '$v'"
if exists $$hh{$v};
$$hh{$v}= $k;
}
$hh
}
our $brightness= $$brightnesses{$system} or die "missing entry for '$system'";
our $rev_brightness= RevertHash $brightness;
our $brightnesslist=
[
map {
[$_, $$rev_brightness{$_}]
} sort { $a <=> $b } keys %$rev_brightness
];
our $brightnessfile="$base/brightness";
our $actual_brightnessfile="$base/actual_brightness";
our ($cmd)=@ARGV;
use Chj::xopen qw(xopen_read xopen_write);
use Chj::Chomp;
if (!defined $cmd or $cmd eq "status") {
my $v= Chomp (xopen_read ($actual_brightnessfile)->xcontent);
my $x= $$rev_brightness{$v};
if (defined $x) {
print "$x\n"
} else {
while (@$brightnesslist) {
my ($x,$n)= @{shift @$brightnesslist}; ##destructive...
if ($x < $v) {
if (my $next= $$brightnesslist[0]) {
my ($to,$ton)=@$next;
if ($v < $to) {
print "$v: between '$n' and '$ton'\n";
last;
}
} else {
print "$v: higher than '$n'\n";
last;
}
} else {
print "$v: lower than '$n'\n";
last;
}
}
}
} elsif ($cmd eq "list") {
print
map {
my ($a,$b)=@$_;
"$a => $b\n"
} @$brightnesslist;
} else {
my $v= $$brightness{$cmd};
defined $v or die "unknown command '$cmd' for system '$system'";
my $out= xopen_write $brightnessfile;
$out->xprint($v);
$out->xclose;
}
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;