summaryrefslogtreecommitdiffstats
path: root/lib/Xconfig/test.pm
blob: f0a06355c7e0b4e7e49353f1124e8d96f38284cc (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
package Xconfig::test; # $Id: test.pm 252243 2009-01-29 17:57:45Z pixel $

use diagnostics;
use strict;

use lib qw(/usr/lib/libDrakX); # for perl_checker
use Xconfig::card;
use run_program;
use common;
use log;


my $tmpconfig = "/tmp/Xconfig";


sub xtest {
    my ($display) = @_;
    eval {
	require xf86misc::main;
	xf86misc::main::Xtest($display);
    };
}

sub test {
    my ($in, $raw_X, $card, $auto, $skip_badcard) = @_;

    my $bad_card = !Xconfig::card::check_bad_card($card);
    return 1 if $skip_badcard && $bad_card;

    if ($bad_card || !$auto) {
	$in->ask_yesorno(N("Test of the configuration"), 
			 N("Do you want to test the configuration?") . ($bad_card ? "\n" . N("Warning: testing this graphic card may freeze your computer") : ''),
			 !$bad_card) or return 1;
    }

    unlink "$::prefix/tmp/.X9-lock";

    #- create a link from the non-prefixed /tmp/.X11-unix/X9 to the prefixed one
    #- that way, you can talk to :9 without doing a chroot
    #- but take care of non X11 install :-)
    if (-d "/tmp/.X11-unix") {
	symlinkf "$::prefix/tmp/.X11-unix/X9", "/tmp/.X11-unix/X9" if $::prefix;
    } else {
	symlinkf "$::prefix/tmp/.X11-unix", "/tmp/.X11-unix" if $::prefix;
    }

    my $f = $::testing ? $tmpconfig : "/etc/X11/xorg.conf.test";
    $raw_X->write("$::prefix/$f");

    my $f_err = common::secured_file($::prefix . ($ENV{TMPDIR} || "$ENV{HOME}/tmp") . '/.drakx.Xoutput');
    
    my $pid;
    unless ($pid = fork()) {
	system("xauth add :9 . `mcookie`");
	open STDERR, ">$f_err";
	chroot $::prefix if $::prefix;
	exec 'Xorg', '-xf86config', $f, ":9" or c::_exit(0);
    }

    do { sleep 1 } until xtest(":9") || waitpid($pid, c::WNOHANG());

    my $_b = before_leaving { unlink $f_err };

    my $warn_error = sub {
	my ($error_msg) = @_;
	$in->ask_warn('', [ N("An error occurred:\n%s\nTry to change some parameters", $error_msg) ]);
    };

    if (!xtest(":9")) {
	open(my $F, $f_err);

	local $_;
      i: while (<$F>) {
	    if (/^\(EE\)/ && !/Disabling/ || /^Fatal\b/) {
		my @msg = !/error/ && $_;
		local $_;
		while (<$F>) {
		    /reporting a problem/ and last;
		    $warn_error->(join('', @msg, $_));
		    return 0;
		}
	    }
	}
    }

    open(my $F, "|perl 2>/dev/null");
    printf $F q(
        use lib qw(%s);
        BEGIN { $::no_ugtk_init = 1 }
        BEGIN { unshift @::textdomains, 'drakx-kbd-mouse-x11' }
        require lang;
        require ugtk3; #- help perl_checker
        ugtk3->import(qw(:wrappers :helpers)); #- help perl_checker
	use interactive::gtk;
        use run_program;
        use common;

        $::prefix = "%s";

        lang::bindtextdomain();

	$ENV{DISPLAY} = ":9";
        Gtk3->init;

        my $text = Gtk3::Label->new;
        my $time = 12;
        Glib::Timeout->add(1000, sub {
	    $text->set(sprintf(translate("%s"), $time));
	    $time-- or Gtk3->main_quit;
            1;
	});

        my $root_w = Gtk3::Window->new;
        $root_w->set_name("back");
        my $pl = Gtk3::CssProvider->new;
        $pl->load_from_data("#back {
background-image: url('$::prefix/usr/share/mga/xfdrake/xfdrake-test-card.png');
background-repeat: repeat;
}");
        Gtk3::StyleContext::add_provider_for_screen(Gtk3::Gdk::Screen::get_default(), $pl, Gtk3::STYLE_PROVIDER_PRIORITY_APPLICATION);

        # maximize:
        $root_w->set_default_size(Gtk3::Gdk::Screen::width, Gtk3::Gdk::Screen::height);
        $root_w->show_all;

        my $in = interactive::gtk->new;
	$in->exit($in->ask_yesorno('', [ translate("%s"), $text ], 0) ? 0 : 222);
    ), join(' ', @INC), $::prefix, N_("Leaving in %d seconds"), N_("Is this the correct setting?");
    my $rc = close $F;
    my $err = $?;

    $rc || $err == 222 << 8 or $warn_error->('');

    unlink "$::prefix/$f", "$::prefix/$f-4";
    unlink "/tmp/.X11-unix/X9" if $::prefix;
    kill 2, $pid;

    $rc;
}