summaryrefslogtreecommitdiffstats
path: root/lib/Xconfig/main.pm
blob: 7fb9c8a5b14edbf6c5308dca2c1088cefeef8301 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package Xconfig::main; # $Id$

use diagnostics;
use strict;

use Xconfig::monitor;
use Xconfig::card;
use Xconfig::plugins;
use Xconfig::resolution_and_depth;
use Xconfig::various;
use Xconfig::screen;
use Xconfig::test;
use Xconfig::xfree;
use common;


sub configure_monitor {
    my ($in) = @_;

    my $raw_X = Xconfig::xfree->read;
    Xconfig::monitor::configure($in, $raw_X, int($raw_X->get_devices)) or return;
    if ($raw_X->is_modified) {
	$raw_X->write;
	'need_restart';
    } else {
	'';
    }
}

sub configure_resolution {
    my ($in) = @_;

    my $raw_X = Xconfig::xfree->read;
    my $X = { 
	card => Xconfig::card::from_raw_X($raw_X),
	monitors => [ $raw_X->get_monitors ],
    };
    if (my $rc = Xconfig::various::handle_May_Need_ForceBIOS($in, $raw_X)) {
	return $rc;
    }

    $X->{resolutions} = Xconfig::resolution_and_depth::configure($in, $raw_X, $X->{card}, $X->{monitors}) or return;
    if ($raw_X->is_modified) {
	&write($raw_X, $X);
    } else {
	'';
    }
}


sub configure_everything_auto_install {
    my ($raw_X, $do_pkgs, $old_X, $options) = @_;
    my $X = {};
    $X->{monitors} = Xconfig::monitor::configure_auto_install($raw_X, $old_X) or return;
    $options->{VideoRam_probed} = $X->{monitors}[0]{VideoRam_probed};
    $X->{card} = Xconfig::card::configure_auto_install($raw_X, $do_pkgs, $old_X, $options) or return;
    Xconfig::screen::configure($raw_X) or return;
    $X->{resolutions} = Xconfig::resolution_and_depth::configure_auto_install($raw_X, $X->{card}, $X->{monitors}, $old_X);

    my $action = &write($raw_X, $X);

    $action;
}

sub configure_everything {
    my ($in, $raw_X, $do_pkgs, $auto, $options) = @_;
    my $X = {};
    my $ok = 1;

    my $probed_info = Xconfig::monitor::probe($raw_X->get_Driver);
    $options->{VideoRam_probed} = $probed_info->{VideoRam_probed};
    $ok &&= $X->{card} = Xconfig::card::configure($in, $raw_X, $do_pkgs, $auto, $options);
    $ok &&= $X->{monitors} = Xconfig::monitor::configure($in, $raw_X, int($raw_X->get_devices), $probed_info, $auto);
    $ok &&= Xconfig::screen::configure($raw_X);
    $ok &&= $X->{resolutions} = Xconfig::resolution_and_depth::configure($in, $raw_X, $X->{card}, $X->{monitors}, $auto);
    $ok &&= Xconfig::test::test($in, $raw_X, $X->{card}, '', 'skip_badcard') if !$auto;

    if (!$ok) {
	return if $auto;
	($ok) = configure_chooser_raw($in, $raw_X, $do_pkgs, $options, $X, 1);
    }
    may_write($in, $raw_X, $X, $ok);
}

sub configure_chooser_raw {
    my ($in, $raw_X, $do_pkgs, $options, $X, $b_modified) = @_;

    my %texts;

    my $update_texts = sub {
	$texts{card} = $X->{card} && $X->{card}{BoardName} || N("Custom");
	$texts{monitors} = $X->{monitors} && $X->{monitors}[0]{ModelName} || N("Custom");
	$texts{resolutions} = Xconfig::resolution_and_depth::to_string($X->{resolutions}[0]);

	$texts{$_} =~ s/(.{20}).*/$1.../ foreach keys %texts; #- ensure not too long
    };
    $update_texts->();

    my $may_set; 
    my $prompt_for_resolution = sub {
	$may_set->('resolutions', Xconfig::resolution_and_depth::configure($in, $raw_X, $X->{card}, $X->{monitors}));
    };
    $may_set = sub {
	my ($field, $val) = @_;
	if ($val) {
	    $X->{$field} = $val;
	    $X->{"modified_$field"} = 1;
	    $b_modified = 1;
	    $update_texts->();

	    if (member($field, 'card', 'monitors')) {
		my ($default_resolution, @other_resolutions) = Xconfig::resolution_and_depth::choices($raw_X, $X->{resolutions}[0], $X->{card}, $X->{monitors});
		if (Xconfig::resolution_and_depth::to_string($default_resolution) ne 
		    Xconfig::resolution_and_depth::to_string($X->{resolutions}[0])) {
		    $prompt_for_resolution->();
		} else {
		    Xconfig::screen::configure($raw_X);
		    $may_set->('resolutions', Xconfig::resolution_and_depth::set_resolution($raw_X, $default_resolution, @other_resolutions));
		}
	    }
	}
    };

    my $ok;
    $in->ask_from_({ interactive_help_id => 'configureX_chooser',
		     title => N("Graphic Card & Monitor Configuration"), 
		     if_($::isStandalone, ok => N("Quit")) }, 
		   [
		    { label => N("Graphic Card"), val => \$texts{card}, clicked => sub { 
			  $may_set->('card', Xconfig::card::configure($in, $raw_X, $do_pkgs, 0, $options));
		      } },
		    { label => N("Monitor"), val => \$texts{monitors}, clicked => sub { 
			  $may_set->('monitors', Xconfig::monitor::configure($in, $raw_X, int($raw_X->get_devices)));
		      } },
		    { label => N("Resolution"), val => \$texts{resolutions}, disabled => sub { !$X->{card} || !$X->{monitors} },
		      clicked => $prompt_for_resolution },
		        if_(Xconfig::card::check_bad_card($X->{card}) || $::isStandalone,
		     { val => N("Test"), disabled => sub { !$X->{card} || !$X->{monitors} },
		       clicked => sub { 
			  $ok = Xconfig::test::test($in, $raw_X, $X->{card}, 'auto', 0);
		      } },
			),
		    { val => N("Options"), clicked => sub {
			  Xconfig::various::various($in, $raw_X, $X->{card}, $options, 0, 'read_existing');
			  Xconfig::card::to_raw_X($X->{card}, $raw_X);
		      } },
		        if_(Xconfig::plugins::list(), 
		    { val => N("Plugins"), clicked => sub {
			  Xconfig::plugins::choose($in, $raw_X);
		      } },
			),
		   ]);
    $ok, $b_modified;
}

sub configure_chooser {
    my ($in, $raw_X, $do_pkgs, $options) = @_;

    my $X = {
	card => scalar eval { Xconfig::card::from_raw_X($raw_X) },
	monitors => [ $raw_X->get_monitors ],
	resolutions => [ eval { $raw_X->get_resolutions } ],
    };
    my ($ok) = configure_chooser_raw($in, $raw_X, $do_pkgs, $options, $X);

    if ($raw_X->is_modified) {
	may_write($in, $raw_X, $X, $ok);
    } else {
	'';
    }
}

sub configure_everything_or_configure_chooser {
    my ($in, $options, $auto, $o_keyboard, $o_mouse) = @_;

    my $raw_X = eval { Xconfig::xfree->read };
    my $err = $@ && formatError($@);
    $err ||= check_valid($raw_X) if $raw_X; #- that's ok if config is empty
    if ($err) {
	log::l("ERROR: bad X config file (error: $err)");
	$options->{ignore_bad_conf} or $in->ask_okcancel('',
			  N("Your Xorg configuration file is broken, we will ignore it.")) or return;
	undef $raw_X;
    }

    my $rc = 'ok';
    if (!$raw_X) {
	$raw_X = Xconfig::default::configure($in->do_pkgs, $o_keyboard, $o_mouse);
	$rc = configure_everything($in, $raw_X, $in->do_pkgs, $auto, $options);
    } elsif (!$auto) {
	$rc = Xconfig::various::handle_May_Need_ForceBIOS($in, $raw_X);
	$rc ||= configure_chooser($in, $raw_X, $in->do_pkgs, $options);
    }
    $rc && $raw_X, $rc;
}


sub may_write {
    my ($in, $raw_X, $X, $ok) = @_;

    $ok ||= $in->ask_yesorno('', N("Keep the changes?
The current configuration is:

%s", Xconfig::various::info($raw_X, $X->{card})), 1);

    $ok && &write($raw_X, $X);
}

sub write {
    my ($raw_X, $X) = @_;
    export_to_install_X($X) if $::isInstall;
    $raw_X->write;
    Xconfig::various::check_XF86Config_symlink();
    symlinkf "../../usr/bin/Xorg", "$::prefix/etc/X11/X";
    if ($X->{resolutions}[0]{bios}) {
	Xconfig::various::setupFB($X->{resolutions}[0]{bios});
	'need_reboot';
    } else {
	'need_restart';
    }
}


sub export_to_install_X {
    my ($X) = @_;

    $::o->{X}{resolution_wanted} = $X->{resolutions}[0]{X} . 'x' . $X->{resolutions}[0]{Y};
    $::o->{X}{default_depth} = $X->{resolutions}[0]{Depth};
    $::o->{X}{bios_vga_mode} = $X->{resolutions}[0]{bios};
    $::o->{X}{monitors} = $X->{monitors} if $X->{monitors}[0]{manually_chosen} && $X->{monitors}[0]{vendor} ne "Plug'n Play";
    $::o->{X}{card} = $X->{card} if $X->{card}{manually_chosen};
    $::o->{X}{Xinerama} = 1 if $X->{card}{Xinerama};
}

sub check_valid {
    my ($raw_X) = @_;

    my %sections = map { 
	my @l = $raw_X->get_Sections($_) or return "missing section $_";
	$_ => \@l;
    } qw(Files InputDevice Monitor Device Screen ServerLayout);

    $sections{Files}[0]{FontPath} or return "missing FontPath";

    '';
}

1;