package services; # $Id$ use diagnostics; use strict; #-###################################################################################### #- misc imports #-###################################################################################### use common qw(:common :functional :system :file); use commands; use run_program; use my_gtk qw(:helpers :wrappers); sub description { my %services = ( anacron => __("Anacron a periodic command scheduler."), apmd => __("apmd is used for monitoring batery status and logging it via syslog. It can also be used for shutting down the machine when the battery is low."), atd => __("Runs commands scheduled by the at command at the time specified when at was run, and runs batch commands when the load average is low enough."), crond => __("cron is a standard UNIX program that runs user-specified programs at periodic scheduled times. vixie cron adds a number of features to the basic UNIX cron, including better security and more powerful configuration options."), gpm => __("GPM adds mouse support to text-based Linux applications such the Midnight Commander. It also allows mouse-based console cut-and-paste operations, and includes support for pop-up menus on the console."), httpd => __("Apache is a World Wide Web server. It is used to serve HTML files and CGI."), inet => __("The internet superserver daemon (commonly called inetd) starts a variety of other internet services as needed. It is responsible for starting many services, including telnet, ftp, rsh, and rlogin. Disabling inetd disables all of the services it is responsible for."), keytable => __("This package loads the selected keyboard map as set in /etc/sysconfig/keyboard. This can be selected using the kbdconfig utility. You should leave this enabled for most machines."), lpd => __("lpd is the print daemon required for lpr to work properly. It is basically a server that arbitrates print jobs to printer(s)."), named => __("named (BIND) is a Domain Name Server (DNS) that is used to resolve host names to IP addresses."), netfs => __("Mounts and unmounts all Network File System (NFS), SMB (Lan Manager/Windows), and NCP (NetWare) mount points."), network => __("Activates/Deactivates all network interfaces configured to start at boot time."), nfs => __("NFS is a popular protocol for file sharing across TCP/IP networks. This service provides NFS server functionality, which is configured via the /etc/exports file."), nfslock => __("NFS is a popular protocol for file sharing across TCP/IP networks. This service provides NFS file locking functionality."), pcmcia => __("PCMCIA support is usually to support things like ethernet and modems in laptops. It won't get started unless configured so it is safe to have it installed on machines that don't need it."), portmap => __("The portmapper manages RPC connections, which are used by protocols such as NFS and NIS. The portmap server must be running on machines which act as servers for protocols which make use of the RPC mechanism."), postfix => __("Postfix is a Mail Transport Agent, which is the program that moves mail from one machine to another."), random => __("Saves and restores system entropy pool for higher quality random number generation."), routed => __("The routed daemon allows for automatic IP router table updated via the RIP protocol. While RIP is widely used on small networks, more complex routing protocols are needed for complex networks."), rstatd => __("The rstat protocol allows users on a network to retrieve performance metrics for any machine on that network."), rusersd => __("The rusers protocol allows users on a network to identify who is logged in on other responding machines."), rwhod => __("The rwho protocol lets remote users get a list of all of the users logged into a machine running the rwho daemon (similiar to finger)."), syslog => __("Syslog is the facility by which many daemons use to log messages to various system log files. It is a good idea to always run syslog."), usb => __("This startup script try to load your modules for your usb mouse."), xfs => __("Starts and stops the X Font Server at boot time and shutdown."), ); my ($name, $prefix) = @_; my $s = $services{$name}; if ($s) { $s = translate($s); } else { ($s = cat_("$prefix/etc/rc.d/init.d/$_")) =~ s/\\\s*\n#\s*//mg; ($s) = $s =~ /^# description:\s+(.*?)^(?:[^#]|# {0,2}\S)/sm; $s =~ s/^#\s*//m; } $s =~ s/\n/ /gm; $s =~ s/\s+$//; $s; } #- returns: #--- the listref of installed services #--- the listref of "on" services sub services { my ($prefix) = @_; my $cmd = $prefix ? "chroot $prefix" : ""; my @l = map { [ /([^\s:]+)/, /\bon\b/ ] } grep { !/:$/ } sort `LANGUAGE=C $cmd chkconfig --list`; [ map { $_->[0] } @l ], [ mapgrep { $_->[1], $_->[0] } @l ]; } sub ask { my ($in, $prefix) = @_; my ($l, $on_services) = services($prefix); ref($in) !~ /gtk/ || $::isInstall and return $in->ask_many_from_list("drakxservices", _("Choose which services should be automatically started at boot time"), { list => $l, help => sub { description($_, $prefix) }, values => $on_services, sort => 1, }); my $W = my_gtk->new(_("Resolution")); my ($x, $y, $w_popup); my $nopop = sub { $w_popup and $w_popup->destroy }; my $display = sub { $nopop->(); $_[0] and gtkmove(gtkshow(gtkadd($w_popup=new Gtk::Window (-popup), gtksignal_connect(gtkadd(new Gtk::EventBox(), gtkadd(gtkset_shadow_type(new Gtk::Frame, 'etched_out'), gtkset_justify(new Gtk::Label($_[0]), 0))), button_press_event => sub { $nopop->() } ))), $x, $y) }; my $update_service = sub { my $started = -e "/var/lock/subsys/$_[0]"; my $action = $started ? "stop" : "start"; $_[1]->set($started ? _("running") : _("stopped")); $started, $action; }; my $strip = sub { my $infos_old = $_[0]; my $infos; while ($infos_old =~ s/(.{40})//) { $1 =~ /(.*) ([^ ]*)/; $infos .= "$1\n$2"; } $infos .= $infos_old; }; my $b = new Gtk::EventBox(); $b->set_events(["pointer_motion_mask"]); gtkadd($W->{window}, gtkadd($b, gtkpack_($W->create_box_with_title(_("Services and deamons")), 1, gtkset_usize(createScrolledWindow(create_packtable({ col_spacings => 10, row_spacings => 3 }, map { my $service = $_; my $infos = $strip->(description($_, $prefix)); $infos ||= _("No additionnal information\nabout this service, sorry."); my $l = new Gtk::Label(); my ($started, $action) = $update_service->($service, gtkset_justify($l, 0)); [ gtkpack__(new Gtk::HBox(0,0), $_), gtkpack__(new Gtk::HBox(0,0), $l), gtkpack__(new Gtk::HBox(0,0), gtksignal_connect(new Gtk::Button("Infos"), clicked => sub { $display->($infos) })), gtkpack__(new Gtk::HBox(0,0), gtkset_active(gtksignal_connect( new Gtk::CheckButton(_("On boot")), clicked => sub { if ($_[0]->active) { "@$on_services" =~ /$service/ or push(@$on_services,$service) } else { @$on_services = grep(!/$service/, @$on_services) }}), "@$on_services" =~ /$service/ )), map { my $a = $_; gtkpack__(new Gtk::HBox(0,0), gtksignal_connect(new Gtk::Button(_($a)), clicked => sub { my $c = "service $service " . (lc($a) eq "start" ? "restart" : lc($a)) . " 2>&1"; local $_=`$c`; s/\033\[[^mG]*[mG]//g; ($started, $action) = $update_service->($service, $l); $display->($_); } )) } ("Start", "Stop") ] } @$l)), 500, 400), 0, gtkpack(gtkset_border_width(new Gtk::HBox(0,0),5), $W->create_okcancel) )) ); $b->signal_connect( motion_notify_event => sub { my ($w, $e) = @_; my ($ox, $oy) = $w->window->get_deskrelative_origin; $x = $e->{'x'}+$ox; $y = $e->{'y'}+$oy; }); $b->signal_connect( button_press_event => sub { $nopop->()}); $::isEmbedded and Gtk->main_iteration while Gtk->events_pending; $::isEmbedded and kill (12, $::CCPID); $W->main or return; ($l, $on_services); } sub doit { my ($in, $on_services, $prefix) = @_; my ($l, $was_on_services) = services($prefix); foreach (@$l) { my $before = member($_, @$was_on_services); my $after = member($_, @$on_services); if ($before != $after) { my $script = "/etc/rc.d/init.d/$_"; run_program::rooted($prefix, "chkconfig", $after ? "--add" : "--del", $_); if ($after && cat_("$prefix$script") =~ /^#\s+chkconfig:\s+-/m) { run_program::rooted($prefix, "chkconfig", "--level", "35", $_, "on"); } if (!$after && $::isStandalone) { run_program::rooted($prefix, $script, "stop"); } } } } 1; 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
package Xconfigurator;

use diagnostics;
use strict;
use vars qw($in $install $resolution_wanted @depths %depths @resolutions @accelservers @allservers %videomemory @ramdac_name @ramdac_id @clockchip_name @clockchip_id %keymap_translate @vsync_range %standard_monitors $intro_text $finalcomment_text $s3_comment $cirrus_comment $probeonlywarning_text $monitorintro_text $hsyncintro_text $vsyncintro_text $XF86firstchunk_text $keyboardsection_start $keyboardsection_part2 $keyboardsection_end $pointersection_text1 $pointersection_text2 $monitorsection_text1 $monitorsection_text2 $monitorsection_text3 $monitorsection_text4 $modelines_text_Trident_TG_96xx $modelines_text $devicesection_text $screensection_text1);

use pci_probing::main;
use common qw(:common :file);
use log;

use Xconfigurator_consts;
use my_gtk qw(:wrappers);

my $tmpconfig = "/tmp/Xconfig";

my ($prefix, %cards, %monitors);

1;

sub setVirtual($) {
    my $vt = '';
    local *C; 
    sysopen C, "/dev/console", 2 or die "failed to open /dev/console: $!";
    ioctl(C, c::VT_GETSTATE(), $vt) or die "ioctl VT_GETSTATE failed";
    ioctl(C, c::VT_ACTIVATE(), $_[0]) or die "ioctl VT_ACTIVATE failed";
    ioctl(C, c::VT_WAITACTIVE(), $_[0]) or die "ioctl VT_WAITACTIVE failed";
    unpack "S", $vt;
}

sub readCardsDB {
    my ($file) = @_;
    my ($card);

    %cards and return;

    local *F;
    open F, $file or die "file $file not found";

    my $lineno = 0; foreach (<F>) { $lineno++;
	s/\s+$//;
	/^#/ and next;
	/^$/ and next;
	/^END/ and last;

	my ($cmd, $val) = /(\S+)\s*(.*)/ or log::l("bad line $lineno ($_)"), next;

	my $f = $ {{ 
	    LINE => sub { push @{$card->{lines}}, $val unless $val eq "VideoRam" },
	    NAME => sub {
		$cards{$card->{type}} = $card if $card;
		$card = { type => $val };
	    },
	    SEE => sub {
		my $c = $cards{$val} or die "Error in database, invalid reference $val at line $lineno";
    
		push @{$card->{lines}}, @{$c->{lines} || []};
		add2hash($card->{flags}, $c->{flags});
		add2hash($card, $c);
	    },
	    CHIPSET => sub { $card->{chipset} = $val; 
			     $card->{flags}->{needVideoRam} = 1 if member($val, qw(mgag10 mgag200 RIVA128));
			 },
	    SERVER => sub { $card->{server} = $val; },
	    RAMDAC => sub { $card->{ramdac} = $val; },
	    DACSPEED => sub { $card->{dacspeed} = $val; },
	    CLOCKCHIP => sub { $card->{clockchip} = $val; $card->{flags}->{noclockprobe} = 1; },
	    NOCLOCKPROBE => sub { $card->{flags}->{noclockprobe} = 1 },
	    UNSUPPORTED => sub { $card->{flags}->{unsupported} = 1 },
	    COMMENT => sub {},
	}}{$cmd};

	$f ? &$f() : log::l("unknown line $lineno ($_)");
    }
    push @{$cards{S3}->{lines}}, $s3_comment;
    push @{$cards{'CL-GD'}->{lines}}, $cirrus_comment;

    # this entry is broken in X11R6 cards db 
    $cards{I128}->{flags}->{noclockprobe} = 1;
}

sub readMonitorsDB {
    my ($file) = @_;

    %monitors and return;

    local *F;
    open F, $file or die "can't open monitors database ($file): $!";
    my $lineno = 0; foreach (<F>) {
	$lineno++;
	s/\s+$//;
	/^#/ and next;
	/^$/ and next;

	my @fields = qw(type bandwidth hsyncrange vsyncrange);
	my @l = split /\s*;\s*/;
	@l == @fields or log::l("bad line $lineno ($_)"), next;
	
	my %l; @l{@fields} = @l;
	$monitors{$l{type}} = \%l;
    }
    while (my ($k, $v) = each %standard_monitors) {
	$monitors{$k} = 
	  $monitors{$v->[0]} = 
	    { hsyncrange => $v->[1], vsyncrange => $v->[2] };
    }
}

sub rewriteInittab {
    my ($runlevel) = @_;
    {
	local (*F, *G);
	open F, "$prefix/etc/inittab" or die "cannot open $prefix/etc/inittab: $!";
	open G, "> $prefix/etc/inittab-" or die "cannot write in $prefix/etc/inittab-: $!";
    
	foreach (<F>) {
	    print G /^(id:)[35](:initdefault:)\s*$/ ? "$1$runlevel$2\n" : $_; # **
	}
    }
    unlink("$prefix/etc/inittab");
    rename("$prefix/etc/inittab-", "$prefix/etc/inittab");
}

sub keepOnlyLegalModes {
    my ($card) = @_;
    my $mem = 1024 * ($card->{memory} || return);

    while (my ($depth, $res) = each %{$card->{depth}}) {
	@$res = grep { $mem >= product(@$_, $depth / 8)	} @$res;
    }
}

sub cardConfigurationAuto() {
    my $card;
    if (my ($c) = pci_probing::main::probe('video')) {
	local $_;
	($card->{identifier}, $_) = @$c;
	$card->{type} = $1 if /Card:(.*)/;
	$card->{server} = $1 if /Server:(.*)/;
    }
    $card;
}

sub cardConfiguration(;$$) {
    my ($card, $noauto) = @_;
    $card ||= {};

    readCardsDB("$prefix/usr/X11R6/lib/X11/Cards");

    add2hash($card, $cards{$card->{type}}) if $card->{type}; # try to get info from given type
    $card->{type} = undef unless $card->{server}; # bad type as we can't find the server
	
    add2hash($card, cardConfigurationAuto()) unless $card->{server} || $noauto;
    add2hash($card, { type => $in->ask_from_list('', _("Choose a graphic card"), [keys %cards]) }) unless $card->{type} || $card->{server};

    add2hash($card, $cards{$card->{type}}) if $card->{type};
    add2hash($card, { vendor => "Unknown", board => "Unknown" });

    $card->{prog} = "/usr/X11R6/bin/XF86_$card->{server}";
    
    -x "$prefix$card->{prog}" or !defined $install or &$install($card->{server});
    -x "$prefix$card->{prog}" or die "server $card->{server} is not available (should be in $prefix$card->{prog})";
	
    unless ($::testing) {
	unlink("$prefix/etc/X11/X");
	symlink("../..$card->{prog}", "$prefix/etc/X11/X");
    }

    unless ($card->{type}) {
	$card->{flags}->{noclockprobe} = member($card->{server}, qw(I128 S3 S3V Mach64));
    }

    $card->{flags}->{needVideoRam} and
      $card->{memory} ||= 
	$videomemory{$in->ask_from_list_('', 
					 _("Give your graphic card memory size"), 
					 [ sort { $videomemory{$a} <=> $videomemory{$b} } 
					   keys %videomemory])};
    $card;
}

sub monitorConfiguration(;$) {
    my $monitor = shift || {};

    $monitor->{hsyncrange} && $monitor->{vsyncrange} and return $monitor;

    readMonitorsDB(-e "MonitorsDB" ? "MonitorsDB" : "/usr/share/MonitorsDB");

    add2hash($monitor, { type => $in->ask_from_list('', _("Choose a monitor"), [keys %monitors]) }) unless $monitor->{type};
    add2hash($monitor, $monitors{$monitor->{type}});
    add2hash($monitor, { type => "Unknown", vendor => "Unknown", model => "Unknown" });
    $monitor;
}

sub testConfig($) {
    my ($o) = @_;
    my ($resolutions, $clocklines);

    write_XF86Config($o, $tmpconfig);

    local *F;
    open F, "$prefix$o->{card}->{prog} :9 -probeonly -pn -xf86config $tmpconfig 2>&1 |";
    foreach (<F>) {
	$o->{card}->{memory} ||= $2 if /(videoram|Video RAM):\s*(\d*)/;

	# look for clocks
	push @$clocklines, $1 if /clocks: (.*)/ && !/(pixel |num)clocks:/;

	push @$resolutions, [ $1, $2 ] if /: Mode "(\d+)x(\d+)": mode clock/;
	print;
    }
    close F or die "X probeonly failed";

    ($resolutions, $clocklines);
}

sub testFinalConfig($;$) {
    my ($o, $auto) = @_;

    $o->{monitor}->{hsyncrange} && $o->{monitor}->{vsyncrange} or
      $in->ask_warn('', _("Monitor not configured yet")), return;

    $o->{card}->{server} or
      $in->ask_warn('', _("Graphic card not configured yet")), return;

    $o->{card}->{depth} or
      $in->ask_warn('', _("Resolutions not chosen yet")), return;

    rename("$prefix/etc/X11/XF86Config", "$prefix/etc/X11/XF86Config.old") || die "unable to make a backup of XF86Config" unless $::testing;

    write_XF86Config($o, $::testing ? $tmpconfig : "$prefix/etc/X11/XF86Config");

    $auto 
      or $in->ask_yesorno(_("Test configuration"), _("Do you want to test configuration?"))
      or return 1;

    my $pid; unless ($pid = fork) {
	my @l = "X";
	@l = ($o->{card}->{prog}, "-xf86config", $tmpconfig) if $::testing;
	chroot $prefix if $prefix;
	exec @l, ":9" or exit 1;
    }
    do { sleep 1; } until (c::Xtest(':0'));

    # 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
    unlink "/tmp/.X11-unix/X9" if $prefix;
    symlink "$prefix/tmp/.X11-unix/X9", "/tmp/.X11-unix/X9" if $prefix;

    local *F;
    open F, "|perl" or die;
    print F "use lib qw(", join(' ', @INC), ");\n";
    print F q{
	use interactive_gtk;
        use my_gtk qw(:wrappers);

	$ENV{DISPLAY} = ":9";
        gtkset_mousecursor(68);
        gtkset_background(200, 210, 210);
        my ($h, $w) = Gtk::Gdk::Window->new_foreign(Gtk::Gdk->ROOT_WINDOW)->get_size;
        $my_gtk::force_position = [ $w / 3, $h / 2.4 ];
	$my_gtk::force_focus = 1;
        my $text = Gtk::Label->new;
        my $time = 8;
        Gtk->timeout_add(1000, sub {
	    $text->set(_("(leaving in %d seconds)", $time));
	    $time-- or Gtk->main_quit;
	});

	exit (interactive_gtk->new->ask_yesorno('', [ _("Is this ok?"), $text ], 1) 
                ? 0 : 222);
    };
    my $rc = close F;
    my $err = $?;

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

    $rc || $err == 222 << 8 or $in->ask_warn('', _("An error occured, try changing some parameters"));
    $rc;
}

sub autoResolutions($;$) {
    my ($o, $nowarning) = @_;
    my $card = $o->{card};

    $nowarning || $in->ask_okcancel(_("Automatic resolutions"),
_("To find the available resolutions i will try different ones.
Your screen will blink... 
You can switch if off if you want, you'll hear a beep when it's over")) or return;

    # swith to virtual console 1 (hopefully not X :)
    my $vt = setVirtual(1);

    # Configure the modes order.
    my ($ok, $best);
    foreach (reverse @depths) {
	local $card->{default_depth} = $_;

	my ($resolutions, $clocklines) = eval { testConfig($o) };
	if ($@ || !$resolutions) {
	    delete $card->{depth}->{$_};
	} else {
	    $card->{clocklines} ||= $clocklines unless $card->{flags}->{noclockprobe};
	    $card->{depth}->{$_} = [ @$resolutions ];
	}
    }

    # restore the virtual console
    setVirtual($vt);
    print "\a"; # beeeep!
}

sub autoDefaultDepth($$) {
    my ($card, $resolution_wanted) = @_;
    my ($wres_wanted) = split 'x', $resolution_wanted;
    my ($best, $depth);

    while (my ($d, $r) = each %{$card->{depth}}) {
	$depth = $depth ? max($depth, $d) : $d;

	# try to have $resolution_wanted
	$best = $best ? max($best, $d) : $d if $r->[0][0] >= $wres_wanted;
    }
    $best || $depth or die "no valid modes";
}

sub chooseResolutions($$) {
    my ($card, $chosen_depth) = @_;

    my $W = my_gtk->new(_("Resolution"));
    my %txt2depth = reverse %depths;
    my $chosen_w = 9999999; # will be set by the combo callback
    my ($r, $depth_combo, %w2depth, %w2h, %w2widget);

    my $set_depth = sub { $depth_combo->entry->set_text(translate($depths{$chosen_depth})) };

    # the set function is usefull to toggle the CheckButton with the callback being ignored
    my $ignore;
    my $set = sub { $ignore = 1; $_[0]->set_active(1); $ignore = 0; };

    while (my ($depth, $res) = each %{$card->{depth}}) {
	foreach (@$res) {
	    $w2h{$_->[0]} = $_->[1];
	    push @{$w2depth{$_->[0]}}, $depth;
	}
    }
    while (my ($w, $h) = each %w2h) {
	my $V = $w . "x" . $h;
	$w2widget{$w} = $r = new Gtk::RadioButton($r ? ($V, $r) : $V);
	$r->signal_connect("clicked" => sub {
			       $ignore and return;
			       $chosen_w = $w;
			       unless (member($chosen_depth, @{$w2depth{$w}})) {
				   $chosen_depth = max(@{$w2depth{$w}});
				   &$set_depth();
			       }
			   });
    }

    gtkadd($W->{window},
	   gtkpack_($W->create_box_with_title(_("Choose resolution and color depth")),
		    1, gtkpack(new Gtk::HBox(0,20),
			       $depth_combo = new Gtk::Combo,
			       gtkpack_(new Gtk::VBox(0,0),
					map { 0, $w2widget{$_} } ikeys(%w2widget),
					),
			       ),
		    0, $W->create_okcancel,
		    ));
    $depth_combo->disable_activate;
    $depth_combo->set_use_arrows_always(1);
    $depth_combo->entry->set_editable(0);
    $depth_combo->set_popdown_strings(map { translate($depths{$_}) } ikeys(%{$card->{depth}}));
    $depth_combo->entry->signal_connect(changed => sub {
       $chosen_depth = $txt2depth{untranslate($depth_combo->entry->get_text, keys %txt2depth)};
       my $w = $card->{depth}->{$chosen_depth}->[0][0];
       $chosen_w > $w and &$set($w2widget{$chosen_w = $w});
    });
    &$set_depth();

    $W->main or return;

    ($chosen_depth, $chosen_w);
}


sub resolutionsConfiguration($$) {
    my ($o, $option) = @_;
    my $card = $o->{card};
    my $auto = $option eq 'auto';
    my $nowarning = $auto || $option eq 'nowarning';
    my $noauto = $option eq 'noauto';

    # For the mono and vga16 server, no further configuration is required.
    return if member($card->{server}, "Mono", "VGA16");

    # some of these guys hate to be poked               
    # if we dont know then its at the user's discretion
    #my $manual ||= 
    #  $card->{server} =~ /^(TGA|Mach32)/ || 
    #  $card->{name} =~ /^Riva 128/ ||
    #  $card->{chipset} =~ /^(RIVA128|mgag)/ ||
    #  $::expert;
    #
    #my $unknown = 
    #  member($card->{server}, qw(S3 S3V I128 Mach64)) ||
    #  member($card->{type}, 
    #	      "Matrox Millennium (MGA)",
    #	      "Matrox Millennium II",
    #	      "Matrox Millennium II AGP",
    #	      "Matrox Mystique",
    #	      "Matrox Mystique",
    #	      "S3",
    #	      "S3V",
    #	      "I128",
    #	     ) ||
    #  $card->{type} =~ /S3 ViRGE/;
    #
    #$unknown and $manual ||= !$in->ask_okcancel('', [ _("I can try to autodetect information about graphic card, but it may freeze :("),
    #						       _("Do you want to try?") ]);
    
    unless ($card->{depth}) {
	$card->{depth}->{$_} = [ map { [ split "x" ] } @resolutions ] 
	  foreach @depths;

	if ($nowarning || (!$noauto && $in->ask_okcancel(_("Automatic resolutions"), 
_("I can try to find the available resolutions (eg: 800x600).
Alas it can freeze sometimes
Do you want to try?")))) {
	    autoResolutions($o, $nowarning);
	}
    }

    # sort resolutions in each depth
    { 
	my $i;
	@$_ = grep { first($i != $_->[0], $i = $_->[0]) }
	      sort { $b->[0] <=> $a->[0] } @$_ 
		foreach values %{$card->{depth}};
    } 

    # remove unusable resolutions (based on the video memory size)
    keepOnlyLegalModes($card);

    my $res = $o->{resolution_wanted} || $resolution_wanted;
    my $depth = $card->{default_depth} || autoDefaultDepth($card, $res);

    $auto or ($depth, $res) = chooseResolutions($card, $depth) or return;

    # needed in auto mode when all has been provided by the user
    $card->{depth}->{$depth} or die "you fixed an unusable depth";

    # remove all biggest resolution (keep the small ones for ctl-alt-+)
    # otherwise there'll be a virtual screen :(
    $card->{depth}->{$depth} = [ grep { $_->[0] <= $res } @{$card->{depth}->{$depth}} ];
    $card->{default_depth} = $depth;
    1;
}


# * Create the XF86Config file. 
sub write_XF86Config {
    my ($o, $file) = @_;
    my $O;

    local *F;
    open F, ">$file" or die "can't write XF86Config in $file: $!";

    print F $XF86firstchunk_text;

    # Write keyboard section.     
    $O = $o->{keyboard};
    print F $keyboardsection_start;

    print F "    RightAlt        ", ($O->{altmeta} ? "ModeShift" : "Meta"), "\n";
    print F $keyboardsection_part2;
    print F qq(    XkbLayout       "$O->{xkb_keymap}"\n);
    print F $keyboardsection_end;

    # Write pointer section.     
    $O = $o->{mouse};
    print F $pointersection_text1;
    print F qq(    Protocol    "$O->{xtype}"\n);
    print F qq(    Device      "$O->{device}"\n);
    # this will enable the "wheel" or "knob" functionality if the mouse supports it 
    print F "    ZAxisMapping 4 5\n" if
      member($O->{xtype}, qw(IntelliMouse IMPS/2 ThinkingMousePS/2 NetScrollPS/2 NetMousePS/2 MouseManPlusPS/2));

    print F $pointersection_text2;
    print F "#" unless $O->{emulate3buttons};
    print F "    Emulate3Buttons\n";
    print F "#" unless $O->{emulate3buttons};
    print F "    Emulate3Timeout    50\n\n";
    print F "# ChordMiddle is an option for some 3-button Logitech mice\n\n";
    print F "#" unless $O->{chordmiddle};
    print F "    ChordMiddle\n\n";
    print F "    ClearDTR\n" if $O->{cleardtrrts};
    print F "    ClearRTS\n\n"  if $O->{cleardtrrts};
    print F "EndSection\n\n\n";

    # Write monitor section.     
    $O = $o->{monitor};
    print F $monitorsection_text1;
    print F qq(    Identifier "$O->{type}"\n);
    print F qq(    VendorName "$O->{vendor}"\n);
    print F qq(    ModelName  "$O->{model}"\n);
    print F "\n";
    print F $monitorsection_text2;
    print F qq(    HorizSync  $O->{hsyncrange}\n);
    print F "\n";
    print F $monitorsection_text3;
    print F qq(    VertRefresh $O->{vsyncrange}\n);
    print F "\n";
    print F $monitorsection_text4;
    print F ($o->{card}->{type} eq "TG 96" ? 
	     $modelines_text_Trident_TG_96xx :
	     $modelines_text);
    print F "EndSection\n\n\n";

    # Write Device section.     
    $O = $o->{card};
    print F $devicesection_text;
    print F qq(Section "Device"\n);
    print F qq(    Identifier  "$O->{type}"\n);
    print F qq(    VendorName  "$O->{vendor}"\n);
    print F qq(    BoardName   "$O->{board}"\n);

    print F "#" if $O->{memory} && !$O->{flags}->{needVideoRam};
    print F "    VideoRam    $O->{memory}\n" if $O->{memory};

    print F map { "    $_\n" } @{$O->{lines} || []};

    print F qq(    Ramdac      "$O->{ramdac}"\n) if $O->{ramdac};
    print F qq(    Dacspeed    "$O->{dacspeed}"\n) if $O->{dacspeed};

    if ($O->{clockchip}) {
	print F qq(    Clockchip   "$O->{clockchip}"\n);
    } else {
	print F "    # Clock lines\n";
	print F "    Clocks $_\n" foreach (@{$O->{clocklines}});
    }
    print F "EndSection\n\n\n";

    # Write Screen sections.     
    print F $screensection_text1;

    my $screen = sub {
	my ($server, $defdepth, $device, $depths) = @_;
	print F qq(

Section "Screen"
    Driver "$server"
    Device      "$device"
    Monitor     "$o->{monitor}->{type}"
);
	print F "    DefaultColorDepth $defdepth\n" if $defdepth;

        foreach (ikeys(%$depths)) {
	    my $m = join(" ", map { qq("$_->[0]x$_->[1]") } @{$depths->{$_}});
	    print F qq(    Subsection "Display"\n);
	    print F qq(        Depth       $_\n) if $_;
	    print F qq(        Modes       $m\n);
	    print F qq(        ViewPort    0 0\n);
	    print F qq(    EndSubsection\n);
	}
	print F "EndSection\n";
    };
    
    # SVGA screen section.
    print F qq(
# The Colour SVGA server
);

    if ($O->{server} eq 'SVGA') {
	&$screen("svga", $O->{default_depth}, $O->{type}, $O->{depth});
    } else {
	&$screen("svga", '', "Generic VGA", { 8 => [[ 320, 200 ]] });
    }

    &$screen("vga16", '',
	     (member($O->{server}, "Mono", "VGA16") ? $O->{type} : "Generic VGA"), 
	     { '' => [[ 640, 480 ], [ 800, 600 ]]});

    &$screen("vga2", '',
	     (member($O->{server}, "Mono", "VGA16") ? $O->{type} : "Generic VGA"), 
	     { '' => [[ 640, 480 ], [ 800, 600 ]]});

    &$screen("accel", $O->{default_depth}, $O->{type}, $O->{depth});
}

sub XF86check_link {
    my ($void) = @_;

    my $f = "$prefix/etc/X11/XF86Config";
    touch($f);

    my $l = "$prefix/usr/X11R6/lib/X11/XF86Config";

    if (-e $l && (stat($f))[1] != (stat($l))[1]) { # compare the inode, must be the sames
	-e $l and unlink($l) || die "can't remove bad $l";
	symlink "../../../../etc/X11/XF86Config", $l;
    }
}


# * Program entry point. 
sub main {
    my $o;
    ($prefix, $o, $in, $install) = @_;
    $o ||= {};

    XF86check_link();

    $o->{card} = cardConfiguration($o->{card}, $::noauto);

    $o->{monitor} = monitorConfiguration($o->{monitor});

    my $ok = resolutionsConfiguration($o, $::auto && 'auto' || $::noauto && 'noauto' || '');
    
    $ok &&= testFinalConfig($o, $::auto);

    my $quit;
    until ($ok || $quit) {

	my %c = my @c = (
	   __("Change Monitor") => sub { $o->{monitor} = monitorConfiguration() },
           __("Change Graphic card") => sub { $o->{card} = cardConfiguration('', 'noauto') },
	   __("Change Resolution") => sub { resolutionsConfiguration($o, 'noauto') },
	   __("Automaticall resolutions search") => sub { 
	       delete $o->{card}->{depth};
	       resolutionsConfiguration($o, 'nowarning');
	   },
	   __("Test again") => sub { $ok = testFinalConfig($o, 1) },
	   __("Quit") => sub { $quit = 1 },
        );
	&{$c{$in->ask_from_list_('', 
				 _("What do you want to do?"),
				 [ grep { !ref } @c ])}};
    }

    if ($ok) {
	my $run = $o->{xdm} || $::auto || $in->ask_yesorno(_("X at startup"), 
_("I can set up your computer to automatically start X upon booting.
Would you like X to start when you reboot?"));

	rewriteInittab($run ? 5 : 3) unless $::testing;

	$in->ask_warn(_("X successfully configured"),
_("Configuration file has been written. Take a look at it before running 'startx'. 
Within the server press ctrl, alt and '+' simultaneously to cycle video resolutions. 
Pressing ctrl, alt and backspace simultaneously immediately exits the server 
For further configuration, refer to /usr/X11R6/lib/X11/doc/README.Config.")) unless $::auto;
    }
}