aboutsummaryrefslogtreecommitdiffstats
path: root/monitor-edid
blob: 3f4d7a223e4ca3128b15dd69714688ea9b63bac2 (plain)
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
#!/usr/bin/perl

# monitor-edid
# Copyright (C) 2005-2008 Mandriva
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use Getopt::Long;

my %opt = ('acpi-dir' => "/proc/acpi/video");
my @common_options = ('verbose', 'try-in-console', 'no-vbe', 'acpi-dir=s', 'vbe-port=i', 'max-vbe-port=i');
my $common_options_usage = '[-v] [--acpi-dir <dir>] [--try-in-console] [--vbe-port <0-3>] [--max-vbe-port <0-3>]';

if ($0 =~ /monitor-get-edid/) {
    GetOptions_(@common_options) or die "usage: monitor-get-edid $common_options_usage\n";

    if (my @edids = get_edids(1)) {
	print $edids[0][1];
	exit 0;
    } else {
	exit 1;
    }
} else {
    GetOptions_(@common_options, 'MonitorsDB', 'perl')
      or die "usage: monitor-edid $common_options_usage [--perl] [--MonitorsDB]\n";

    my $err = 1;

    if (my @edids = get_edids()) {
	print "(\n" if $opt{perl};
	foreach (@edids) {
	    my ($f, $edid) = @$_;
	    warn "parsing EDID from $f\n" if $opt{verbose};
	    if ($f =~ m!^/!) {
		system(parse_edid() . " $f");
		$err = 0 if $? == 0;
	    } else {
		open(my $F, '|' . parse_edid());
		print $F $edid;
		close $F and $err = 0;
	    }
	    print ",\n" if $opt{perl};
	}
	print ")\n" if $opt{perl};
    }
    exit $err;
}

sub GetOptions_ {
    my (@l) = @_;
    GetOptions('v' => \$opt{verbose}, map { $_ => \$opt{/([^=]*)/ && $1} } @l);
}

sub propagate_options {
    my (@l) = @_;
    map { $_ eq 'verbose' ? '-v' : "--$_" } grep { $opt{$_} } @l;
}

sub get_using_vbe() {
    my $prog = '/usr/sbin/monitor-get-edid-using-vbe';
    -x $prog && join(' ', $prog, propagate_options('verbose', 'try-in-console'));
}

sub parse_edid() {
    join(' ', 'monitor-parse-edid', propagate_options('verbose', 'MonitorsDB', 'perl'));
}

sub get_edids {
    my ($b_get_first) = @_;
    my @l = map { 
	my $s = slurp($_); 
	is_edid_possibly_valid($s) ? [ $_ => $s ] : ();
    } get_edid_files();
    
    if (!@l || !$b_get_first && $< == 0) {
	if (my $cmd = get_using_vbe()) {
	    my $min_port = $opt{'vbe-port'} || 0;
	    my $max_port = $opt{'max-vbe-port'} || $opt{'vbe-port'} || 2;
	    foreach my $port ($min_port .. $max_port) {
		warn "probing EDID using VBE (port $port)\n" if $opt{verbose};
		my $edid = `$cmd --port $port`;
		is_edid_possibly_valid($edid) or next;

		if (grep { $_->[1] eq $edid } @l) {
		    # already found, forget it
		} else {
		    push @l, [ vbe => $edid ];
		    last;
		}
	    }
	}
    }
    @l;
}

sub get_edid_files() {
    my @l1 = find_EDID("/proc/device-tree");
    my @l2 = grep {
	(my $state_f = $_) =~ s/EDID$/state/;
	my ($state) = slurp($state_f) =~ /state:\s*0x(\w+)/;
	hex($state) & 2; # bit 1 is "Output is activated"
    } find_EDID($opt{'acpi-dir'});
    my @l3 = grep {
	(my $state_f = $_) =~ s/edid$/enabled/;
	slurp($state_f) =~ /enabled/;
    } glob("/sys/class/drm/card*-*/edid");
    (@l1, @l2, @l3);
}

sub find_EDID {
    my ($dir) = @_;
    my @l;
    require File::Find;
    File::Find::find(sub { $_ eq 'EDID' and push @l, $File::Find::name }, $dir);
    @l;
}

sub is_edid_possibly_valid {
    my ($edid) = @_;
    length($edid) >= 128;
}

sub slurp { 
    my ($f) = @_; 
    open(my $F, '<', $f) or return;
    local $/;
    scalar <$F>;
}