aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/detect-resolution
blob: 708766524ab5f8eb5d2f068fe4f8cd47c6b82f15 (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
# -*- Mode: cperl -*-
# Copyright (C) 2002 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
#
# Detect vga resolution we want.
#
# add better detection thanks to Olivier Blin idea to use argument from
# make_boot_splash.

use strict;
my (%main, %entry, $vga, $initrd);

if ($ARGV[0]) {
    $initrd = $ARGV[0];
    $initrd =~ s,.*/,,
} else {
    $vga = detect_from_cmdline(cat_('/proc/cmdline'));
    undef $vga if $vga =~ /x/; #lilo cmdline can't convert
}

detect_from_loader() if !$vga;
if ($vga) {
    chomp $vga;
    $vga =~ s/vga=//;
    $vga = convert_vgamode();
}
print $vga, "\n" if $vga;

sub detect_from_cmdline {return (grep /vga=\d+/, split("[ \t]", shift))[0];}
sub detect_from_loader {
    my $loader = `detectloader -q`;chomp $loader;
    if ($loader =~ m/lilo/i ) {
	parse_lilo_conf();
    } elsif ($loader =~ /grub/i ) {
	parse_grub_conf();
    }
    if ($initrd) {
	$vga = $main{initrd}{$initrd}
    }
    $vga ||= $entry{$main{default}}{vga};
    $vga ||= $main{vga}
}
sub parse_grub_conf {
    my ($title);
    my $cnt = 0;
    my $grub_conf = "/boot/grub/menu.lst";
    my $vga;
    return if not -f $grub_conf;
    open F, $grub_conf or die "Can't open $grub_conf\n";

    while (<F>) {
	next if /^\s*#/;
	$main{default} = $1 if /^default (\d+)/;
	if (/^title\s+(.*)/) {
	    $vga=0;
	    $title=$1;
	    $entry{$title}{cnt} = $cnt;
	    $main{default} = $title if $entry{$title}{cnt} eq $main{default};
	    $cnt++;
	}
	if (m/kernel\s+\(.*\)[^\s]+/) {
	    $vga = $1 if /vga=(\w*)/;
	    $entry{$title}{vga} = $vga if ($title and $vga);
	}
	if (m,initrd\s+\(.*\)(?:.*/)?([^\s]+),){
	    $entry{$title}{initrd} = $1;
	    $main{initrd}{$1} = $entry{$title}{vga}
	}
    }
    close F;
}

sub parse_lilo_conf {
    my $lilo_conf="/etc/lilo.conf";
    my ($vga, $label, $loc_initrd);
    return if not -f $lilo_conf;
    open F, $lilo_conf or die "Can't open $lilo_conf\n";
    while (<F>) {
	next if /^\s*#/;
	$main{default} = remove_quotes($1) if m/^default=(.*)/;
	$main{vga} = $1 if m/^vga=(.*)/;
	$vga = $1 if /vga=(.*)/;
	$label = remove_quotes($1) if /label=(.*)/;
	$loc_initrd = $1 if m,initrd=(?:.*/)?(.*),;
	$entry{$label}{vga} = $vga if ($label && $vga);
	if (/image/) {
	    if ($loc_initrd && $label && $vga) {
		$entry{$label}{initrd} = $loc_initrd;
		if (!$main{initrd}{$loc_initrd} || $main{default} eq $label) {
		    $main{initrd}{$loc_initrd} = $vga
		}
	    }
	    undef $vga,$label;
	}
    }
    if ($loc_initrd && $label && $vga) {
	$entry{$label}{initrd} = $loc_initrd;
	if (!$main{initrd}{$loc_initrd} || $main{default} eq $label) {
	    $main{initrd}{$loc_initrd} = $vga
	}
    }
    close F;
}

sub convert_vgamode {
    my $v;
    #From drakx Xconfigurator_consts.pm
    my %vgamodes = (
		    '640x480x8'    => [ '769', '0x301' ],
		    '800x600x8'    => [ '771', '0x303' ],
		    '1024x768x8'   => [ '773', '0x305' ],
		    '1280x1024x8'  => [ '775', '0x307' ],
		    '1600x1200x8'  => [ '777', '0x309' ],
		    '640x480x15'   => [ '784', '0x310' ],
		    '800x600x15'   => [ '787', '0x313' ],
		    '1024x768x15'  => [ '790', '0x316' ],
		    '1280x1024x15' => [ '793', '0x319' ],
		    '1600x1200x15' => [ '796', '0x31C' ],
		    '640x480x16'   => [ '785', '0x311' ],
		    '800x600x16'   => [ '788', '0x314' ],
		    '1024x768x16'  => [ '791', '0x317' ],
		    '1280x1024x16' => [ '794', '0x31A' ],
		    '1600x1200x16' => [ '797', '0x31D' ],
		    #-    '640xx24'      => 786, #- there is a problem with these resolutions since the BIOS may take 24 or 32 planes.
		    #-    '640x480x24'   => 786,
		    #-    '800xx24'      => 789,
		    #-    '800x600x24'   => 789,
		    #-    '1024xx24'     => 792,
		    #-    '1024x768x24'  => 792,
		    #-    '1280xx24'     => 795,
		    #-    '1280x1024x24' => 795,
		   );
    foreach my $k (keys %vgamodes) { $v = $k if grep { $_ eq $vga } @{$vgamodes{$k}} }
    undef $vga if not $v;return $v;
}
sub cat_ { local *F; open F, $_[0] or $_[1] ? die "cat of file $_[0] failed: $!\n" : return; my @l = <F>; wantarray ? @l : join '', @l }
sub remove_quotes { local $_ = $_[0]; s/^"(.*)"$/$1/g; s/\\"/"/g; $_ }