package mouse; # $Id$ #use diagnostics; #use strict; #-###################################################################################### #- misc imports #-###################################################################################### use common; use modules; use detect_devices; use run_program; use devices; use modules; use any; use log; my @mouses_fields = qw(nbuttons MOUSETYPE XMOUSETYPE name EMULATEWHEEL); my %mice = arch() =~ /^sparc/ ? ( 'sunmouse' => [ [ 'sunmouse' ], [ [ 3, 'sun', 'sun', N_("Sun - Mouse") ] ] ] ) : ( 'PS/2' => [ [ 'psaux' ], [ [ 2, 'ps/2', 'PS/2', N_("Standard") ], [ 5, 'ps/2', 'MouseManPlusPS/2', N_("Logitech MouseMan+") ], [ 5, 'imps2', 'IMPS/2', N_("Generic PS2 Wheel Mouse") ], [ 5, 'ps/2', 'GlidePointPS/2', N_("GlidePoint") ], if_(c::kernel_version() !~ /^\Q2.6/, [ 5, 'imps2', 'auto', N_("Automatic") ] ), '', [ 5, 'ps/2', 'ThinkingMousePS/2', N_("Kensington Thinking Mouse") ], [ 5, 'netmouse', 'NetMousePS/2', N_("Genius NetMouse") ], [ 5, 'netmouse', 'NetScrollPS/2', N_("Genius NetScroll") ], [ 7, 'ps/2', 'ExplorerPS/2', N_("Microsoft Explorer") ], ] ], 'USB' => [ [ 'usbmouse' ], [ [ 1, 'ps/2', 'IMPS/2', N_("1 button") ], [ 2, 'ps/2', 'IMPS/2', N_("Generic 2 Button Mouse") ], [ 3, 'ps/2', 'IMPS/2', N_("Generic") ], [ 3, 'ps/2', 'IMPS/2', N_("Generic 3 Button Mouse with Wheel emulation"), 'wheel' ], [ 5, 'ps/2', 'IMPS/2', N_("Wheel") ], [ 7, 'ps/2', 'ExplorerPS/2', N_("Microsoft Explorer") ], ] ], N_("serial") => [ [ map { "ttyS$_" } 0..3 ], [ [ 2, 'Microsoft', 'Microsoft', N_("Generic 2 Button Mouse") ], [ 3, 'Microsoft', 'Microsoft', N_("Generic 3 Button Mouse") ], [ 3, 'Microsoft', 'Microsoft', N_("Generic 3 Button Mouse with Wheel emulation"), 'wheel' ], [ 5, 'ms3', 'IntelliMouse', N_("Microsoft IntelliMouse") ], [ 3, 'MouseMan', 'MouseMan', N_("Logitech MouseMan") ], [ 3, 'MouseMan', 'MouseMan', N_("Logitech MouseMan with Wheel emulation"), 'wheel' ], [ 2, 'MouseSystems', 'MouseSystems', N_("Mouse Systems") ], '', [ 3, 'logim', 'MouseMan', N_("Logitech CC Series") ], [ 3, 'logim', 'MouseMan', N_("Logitech CC Series with Wheel emulation"), 'wheel' ], [ 5, 'pnp', 'IntelliMouse', N_("Logitech MouseMan+/FirstMouse+") ], [ 5, 'ms3', 'IntelliMouse', N_("Genius NetMouse") ], [ 2, 'MMSeries', 'MMSeries', N_("MM Series") ], [ 2, 'MMHitTab', 'MMHittab', N_("MM HitTablet") ], [ 3, 'Logitech', 'Logitech', N_("Logitech Mouse (serial, old C7 type)") ], [ 3, 'Logitech', 'Logitech', N_("Logitech Mouse (serial, old C7 type) with Wheel emulation"), 'wheel' ], [ 3, 'Microsoft', 'ThinkingMouse', N_("Kensington Thinking Mouse") ], [ 3, 'Microsoft', 'ThinkingMouse', N_("Kensington Thinking Mouse with Wheel emulation"), 'wheel' ], ] ], N_("busmouse") => [ [ arch() eq 'ppc' ? 'adbmouse' : ('atibm', 'inportbm', 'logibm') ], [ if_(arch() eq 'ppc', [ 1, 'Busmouse', 'BusMouse', N_("1 button") ]), [ 2, 'Busmouse', 'BusMouse', N_("2 buttons") ], [ 3, 'Busmouse', 'BusMouse', N_("3 buttons") ], [ 3, 'Busmouse', 'BusMouse', N_("3 buttons with Wheel emulation"), 'wheel' ], ] ], if_(c::kernel_version() =~ /^\Q2.6/, N_("Universal") => [ [ 'input/mice' ], [ [ 7, 'ps/2', 'ExplorerPS/2', N_("Any PS/2 & USB mice") ], if_(detect_devices::is_xbox(), [ 5, 'ps/2', 'IMPS/2', N_("Microsoft Xbox Controller S") ]), ] ], ), N_("none") => [ [ 'none' ], [ [ 0, 'none', 'Microsoft', N_("No mouse") ], ] ], ); sub xmouse2xId { #- xmousetypes must be sorted as found in /usr/include/X11/extensions/xf86misc.h #- so that first mean "0", etc my @xmousetypes = ( "Microsoft", "MouseSystems", "MMSeries", "Logitech", "BusMouse", #MouseMan, "Logitech", "PS/2", "MMHittab", "GlidePoint", "IntelliMouse", "ThinkingMouse", "IMPS/2", "ThinkingMousePS/2", "MouseManPlusPS/2", "GlidePointPS/2", "NetMousePS/2", "NetScrollPS/2", "SysMouse", "Auto", "AceCad", "ExplorerPS/2", "USB", ); my ($id) = @_; $id = 'BusMouse' if $id eq 'MouseMan'; $id = 'IMPS/2' if $id eq 'ExplorerPS/2' && $::isInstall; eval { find_index { $_ eq $id } @xmousetypes } || 0; } my %mouse_btn_keymap = ( 0 => "NONE", 67 => "F9", 68 => "F10", 87 => "F11", 88 => "F12", 85 => "F13", 89 => "F14", 90 => "F15", 56 => "L-Option/Alt", 125 => "L-Command (Apple)", 98 => "Num: /", 55 => "Num: *", 117 => "Num: =", 96 => "Enter", ); sub ppc_one_button_keys() { keys %mouse_btn_keymap } sub ppc_one_button_key2text { $mouse_btn_keymap{$_[0]} } sub raw2mouse { my ($type, $raw) = @_; $raw or return; my %l; @l{@mouses_fields} = @$raw; +{ %l, type => $type }; } sub fullnames() { map_each { my $type = $::a; grep { $_ } map { if ($_) { my $l = raw2mouse($type, $_); "$type|$l->{name}"; } else { $type .= "|[" . N("Other") . "]"; ''; } } @{$::b->[1]}; } %mice; } sub fullname2mouse { my ($fname, %opts) = @_; my ($type, @l) = split '\|', $fname; my $name = pop @l; search: $opts{device} ||= $mice{$type}[0][0]; foreach (@{$mice{$type}[1]}) { my $l = raw2mouse($type, $_); $name eq $l->{name} and return { %$l, %opts }; } if ($name eq '1 Button' || $name eq '1 button') { $name = "Generic 2 Button Mouse"; goto search; } die "$fname not found ($type, $name)"; } sub serial_ports() { map { "ttyS$_" } 0..7 } sub serial_port2text { $_[0] =~ /ttyS(\d+)/ ? "$_[0] / COM" . ($1 + 1) : $_[0]; } sub read() { my %mouse = getVarsFromSh "$::prefix/etc/sysconfig/mouse"; eval { add2hash_(\%mouse, fullname2mouse($mouse{FULLNAME})) }; $mouse{nbuttons} ||= $mouse{XEMU3} eq "yes" ? 2 : $mouse{WHEEL} eq "yes" ? 5 : 3; \%mouse; } sub write { my ($do_pkgs, $mouse) = @_; local $mouse->{FULLNAME} = qq("$mouse->{type}|$mouse->{name}"); #-" local $mouse->{XEMU3} = bool2yesno($mouse->{nbuttons} < 3); local $mouse->{WHEEL} = bool2yesno($mouse->{nbuttons} > 3); setVarsInSh("$::prefix/etc/sysconfig/mouse", $mouse, qw(MOUSETYPE XMOUSETYPE FULLNAME XEMU3 WHEEL device)); any::devfssymlinkf($mouse, 'mouse'); #- we should be using input/mice directly instead of usbmouse, but legacy... symlinkf 'input/mice', "$::prefix/dev/usbmouse" if $mouse->{device} eq "usbmouse"; any::devfssymlinkf($mouse->{auxmouse}, 'mouse1') if $mouse->{auxmouse}; various_xfree_conf($do_pkgs, $mouse); if (arch() =~ /ppc/) { my $s = join('', "dev.mac_hid.mouse_button_emulation = " . to_bool($mouse->{button2_key} || $mouse->{button3_key}) . "\n", if_($mouse->{button2_key}, "dev.mac_hid.mouse_button2_keycode = $mouse->{button2_key}\n"), if_($mouse->{button3_key}, "dev.mac_hid.mouse_button3_keycode = $mouse->{button3_key}\n"), ); substInFile { $_ = '' if /^\Qdev.mac_hid.mouse_button/; $_ .= $s if eof; } "$::prefix/etc/sysctl.conf"; } } sub probe_wacom_devices { my ($modules_conf) = @_; $modules_conf->get_probeall("usb-interface") or return; my (@l) = detect_devices::usbWacom() or return; log::l("found usb wacom $_->{driver} $_->{description} ($_->{type})") foreach @l; my @wacom = eval { modules::load("wacom", "evdev"); grep { detect_devices::tryOpen($_) } map_index { "input/event$::i" } @l; }; @wacom or eval { modules::unload("evdev", "wacom") }; @wacom; } sub detect_serial() { my ($t, $mouse, @wacom); #- Whouah! probing all devices from ttyS0 to ttyS3 once a time! detect_devices::probeSerialDevices(); #- check new probing methods keep everything used here intact! foreach (0..3) { $t = detect_devices::probeSerial("/dev/ttyS$_") or next; if ($t->{CLASS} eq 'MOUSE') { $t->{MFG} ||= $t->{MANUFACTURER}; $mouse = fullname2mouse("serial|Microsoft IntelliMouse") if $t->{MFG} eq 'MSH' && $t->{MODEL} eq '0001'; $mouse = fullname2mouse("serial|Logitech MouseMan") if $t->{MFG} eq 'LGI' && $t->{MODEL} =~ /^80/; $mouse = fullname2mouse("serial|Genius NetMouse") if $t->{MFG} eq 'KYE' && $t->{MODEL} eq '0003'; $mouse ||= fullname2mouse("serial|Generic 2 Button Mouse"); #- generic by default. $mouse->{device} = "ttyS$_"; last; } elsif ($t->{CLASS} eq "PEN" || $t->{MANUFACTURER} eq "WAC") { push @wacom, "ttyS$_"; } } $mouse, @wacom; } sub detect { my ($modules_conf) = @_; # let more USB tablets and touchscreens magically work at install time # through /dev/input/mice multiplexing: modules::probe_category('input/tablet'); modules::probe_category('input/touchscreen'); if (arch() =~ /^sparc/) { return fullname2mouse("sunmouse|Sun - Mouse"); } if (arch() eq "ppc") { return fullname2mouse(detect_devices::hasMousePS2("usbmouse") ? "USB|1 button" : # No need to search for an ADB mouse. If I did, the PPC kernel would # find one whether or not I had one installed! So.. default to it. "busmouse|1 button"); } my @wacom = probe_wacom_devices($modules_conf); if (c::kernel_version() =~ /^\Q2.6/) { $modules_conf->get_probeall("usb-interface") and eval { modules::load('usbhid') }; if (cat_('/proc/bus/input/devices') =~ /^H: Handlers=mouse/m) { if (detect_devices::is_xbox()) { return fullname2mouse('Universal|Microsoft Xbox Controller S'); } my $univ_mouse = fullname2mouse('Universal|Any PS/2 & USB mice', wacom => \@wacom); if (my ($synaptics_touchpad) = detect_devices::getSynapticsTouchpads()) { $univ_mouse->{auxmouse} = { name => N_("Synaptics Touchpad"), device => 'input/mice', XMOUSETYPE => 'auto-dev', ALPS => $synaptics_touchpad->{description} =~ /ALPS/, }; } return $univ_mouse; } } else { my $ps2_mouse = detect_devices::hasMousePS2("psaux") && fullname2mouse("PS/2|Automatic", unsafe => 0); #- workaround for some special case were mouse is openable 1/2. if (!$ps2_mouse) { $ps2_mouse = detect_devices::hasMousePS2("psaux") && fullname2mouse("PS/2|Automatic", unsafe => 0); $ps2_mouse and detect_devices::hasMousePS2("psaux"); #- fake another open in order for XFree to see the mouse. } if ($modules_conf->get_probeall("usb-interface")) { sleep 2; if (my (@l) = detect_devices::usbMice()) { log::l(join('', "found usb mouse $_->{driver} $_->{description} (", if_($_->{type}, $_->{type}), ")")) foreach @l; if (eval { modules::load(qw(hid mousedev usbmouse)); detect_devices::tryOpen("usbmouse") }) { return fullname2mouse($l[0]{driver} =~ /Mouse:(.*)/ ? $1 : "USB|Wheel", if_($ps2_mouse, auxmouse => $ps2_mouse), #- for laptop, we kept the PS/2 as secondary (symbolic). wacom => \@wacom); } eval { modules::unload(qw(usbmouse mousedev hid)) }; } } else { log::l("no usb interface found for mice"); } if ($ps2_mouse) { return { wacom => \@wacom, %$ps2_mouse }; } } #- probe serial device to make sure a wacom has been detected. eval { modules::load("serial") }; my ($serial_mouse, @serial_wacom) = detect_serial(); push @wacom, @serial_wacom; if ($serial_mouse) { { wacom => \@wacom, %$serial_mouse }; } elsif (@wacom) { #- in case only a wacom has been found, assume an inexistant mouse (necessary). fullname2mouse('none|No mouse', wacom => \@wacom); } elsif (c::kernel_version() =~ /^\Q2.6/) { fullname2mouse('Universal|Any PS/2 & USB mice', unsafe => 1); } else { fullname2mouse("PS/2|Automatic", unsafe => 1); } } sub load_modules { my ($mouse) = @_; my @l; for ($mouse->{type}) { /serial/ and @l = qw(serial); /USB/ and @l = qw(hid mousedev usbmouse); } foreach (@{$mouse->{wacom}}) { /ttyS/ and push @l, qw(serial); /event/ and push @l, qw(wacom evdev); } if ($mouse->{auxmouse} && $mouse->{auxmouse}{name} eq N_("Synaptics Touchpad")) { push @l, qw(evdev); } eval { modules::load(@l) }; } sub set_xfree_conf { my ($mouse, $xfree_conf, $b_keep_auxmouse_unchanged) = @_; my ($synaptics, $mouse_) = partition { $_->{name} eq N_("Synaptics Touchpad") } ($mouse, if_($mouse->{auxmouse}, $mouse->{auxmouse})); my @mice = map { { Protocol => $_->{XMOUSETYPE}, Device => "/dev/mouse", if_($_->{nbuttons} > 3, ZAxisMapping => [ $_->{nbuttons} > 5 ? '6 7' : '4 5' ]), if_($_->{nbuttons} < 3, Emulate3Buttons => undef, Emulate3Timeout => 50), if_($_->{EMULATEWHEEL}, Emulate3Buttons => undef, Emulate3Timeout => 50, EmulateWheel => undef, EmulateWheelButton => 2), }; } @$mouse_; if (!$mouse->{auxmouse} && $b_keep_auxmouse_unchanged) { my (undef, @l) = $xfree_conf->get_mice; push @mice, @l; } $xfree_conf->set_mice(@mice); if (my @wacoms = @{$mouse->{wacom} || []}) { $xfree_conf->set_wacoms(map { { Device => "/dev/$_", USB => m|input/event| } } @wacoms); } $synaptics and $xfree_conf->set_synaptics(map { { Device => "/dev/$_->{device}", Protocol => $_->{XMOUSETYPE}, Primary => 0, ALPS => $_->{ALPS}, } } @$synaptics); } sub various_xfree_conf { my ($do_pkgs, $mouse) = @_; { my $f = "$::prefix/etc/X11/xinit.d/mouse_buttons"; if ($mouse->{nbuttons} <= 5) { unlink($f); } else { output_with_perm($f, 0755, "xmodmap -e 'pointer = 1 2 3 6 7 4 5'\n"); } } { my $f = "$::prefix/etc/X11/xinit.d/auxmouse_buttons"; if (!$mouse->{auxmouse} || $mouse->{auxmouse}{nbuttons} <= 5) { unlink($f); } else { $do_pkgs->install('xinput'); output_with_perm($f, 0755, "xinput set-button-map Mouse2 1 2 3 6 7 4 5\n"); } } { my $f = "$::prefix/etc/X11/xinit.d/xpad"; if ($mouse->{name} !~ /^Microsoft Xbox Controller/) { unlink($f); } else { output_with_perm($f, 0755, "xset m 1/8 1\n"); } } if (member(N_("Synaptics Touchpad"), $mouse->{name}, $mouse->{auxmouse} && $mouse->{auxmouse}{name})) { $do_pkgs->install("synaptics"); } } #- write_conf : write the mouse infos into the Xconfig files. #- input : #- $mouse : the hashtable containing the informations #- $mouse input #- $mouse->{nbuttons} : number of buttons : integer #- $mouse->{device} : device of the mouse : string : ex 'psaux' #- $mouse->{XMOUSETYPE} : type of the mouse for gpm : string : ex 'PS/2' #- $mouse->{type} : type (generic ?) of the mouse : string : ex 'PS/2' #- $mouse->{name} : name of the mouse : string : ex 'Standard' #- $mouse->{MOUSETYPE} : type of the mouse : string : ex "ps/2" #- $mouse->{XEMU3} : emulate 3rd button : string : 'yes' or 'no' sub write_conf { my ($do_pkgs, $modules_conf, $mouse, $b_keep_auxmouse_unchanged) = @_; &write($do_pkgs, $mouse); $modules_conf->write if $mouse->{device} eq "usbmouse" && !$::testing; eval { require Xconfig::xfree; my $xfree_conf = Xconfig::xfree->read; set_xfree_conf($mouse, $xfree_conf, $b_keep_auxmouse_unchanged); $xfree_conf->write; }; } sub change_mouse_live { my ($mouse, $old) = @_; my $xId = xmouse2xId($mouse->{XMOUSETYPE}); $old->{device} ne $mouse->{device} || $xId != xmouse2xId($old->{XMOUSETYPE}) or return; log::l("telling X server to use another mouse ($mouse->{XMOUSETYPE}, $xId)"); eval { modules::load('serial') } if $mouse->{device} =~ /ttyS/; if (!$::testing) { devices::make($mouse->{device}); symlinkf($mouse->{device}, "/dev/mouse"); eval { require xf86misc::main; xf86misc::main::setMouseLive($ENV{DISPLAY}, $xId, $mouse->{nbuttons} < 3); }; } 1; } sub test_mouse_install { my ($mouse, $x_protocol_changed) = @_; require ugtk2; ugtk2->import(qw(:wrappers :create)); my $w = ugtk2->new('', disallow_big_help => 1); my $darea = Gtk2::DrawingArea->new; $darea->set_events([ 'button_press_mask', 'button_release_mask' ]); #$darea must be unrealized. gtkadd($w->{window}, gtkpack(my $vbox_grab = Gtk2::VBox->new(0, 0), $darea, gtkset_sensitive(create_okcancel($w, undef, undef, 'edge'), 1) ), ); test_mouse($mouse, $darea, $x_protocol_changed); $w->sync; # HACK Gtk2::Gdk->pointer_grab($vbox_grab->window, 1, 'pointer_motion_mask', $vbox_grab->window, undef, 0); my $r = $w->main; Gtk2::Gdk->pointer_ungrab(0); $r; } sub test_mouse_standalone { my ($mouse, $hbox) = @_; require ugtk2; ugtk2->import(qw(:wrappers)); my $darea = Gtk2::DrawingArea->new; $darea->set_events([ 'button_press_mask', 'button_release_mask' ]); #$darea must be unrealized. gtkpack($hbox, gtkpack(gtkset_border_width(Gtk2::VBox->new(0, 10), 10), $darea)); test_mouse($mouse, $darea); } sub test_mouse { my ($mouse, $darea, $b_x_protocol_changed) = @_; require ugtk2; ugtk2->import(qw(:wrappers)); my $suffix = $mouse->{nbuttons} <= 2 ? '2b' : $mouse->{nbuttons} == 3 ? '3b' : '3b+'; my %offsets = (mouse_2b_right => [ 93, 0 ], mouse_3b_right => [ 117, 0 ], mouse_2b_middle => [ 82, 80 ], mouse_3b_middle => [ 68, 0 ], 'mouse_3b+_middle' => [ 85, 67 ]); my %image_files = ( mouse => "mouse_$suffix", left => 'mouse_' . ($suffix eq '3b+' ? '3b' : $suffix) . '_left', right => 'mouse_' . ($suffix eq '3b+' ? '3b' : $suffix) . '_right', if_($mouse->{nbuttons} > 2, middle => 'mouse_' . $suffix . '_middle'), up => 'arrow_up', down => 'arrow_down'); my %images = map { $_ => ugtk2::gtkcreate_pixbuf("$image_files{$_}.png") } keys %image_files; my $width = $images{mouse}->get_width; my $height = round_up(min($images{mouse}->get_height, $::windowheight - 150), 6); my $draw_text = sub { my ($t, $y) = @_; my $layout = $darea->create_pango_layout($t); my ($w) = $layout->get_pixel_size; $darea->window->draw_layout($darea->style->black_gc, ($darea->allocation->width-$w)/2, ($darea->allocation->height-$height)/2 + $y, $layout); }; my $draw_pixbuf = sub { my ($p, $x, $y, $w, $h) = @_; $w = $p->get_width; $h = $p->get_height; $p->render_to_drawable($darea->window, $darea->style->bg_gc('normal'), 0, 0, ($darea->allocation->width-$width)/2 + $x, ($darea->allocation->height-$height)/2 + $y, $w, $h, 'none', 0, 0); }; my $draw_by_name = sub { my ($name) = @_; my $file = $image_files{$name}; my ($x, $y) = @{$offsets{$file} || [ 0, 0 ]}; $draw_pixbuf->($images{$name}, $x, $y); }; my $drawarea = sub { $draw_by_name->('mouse'); if ($::isInstall || 1) { $draw_text->(N("Please test the mouse"), 200); if ($b_x_protocol_changed && $mouse->{nbuttons} > 3 && $mouse->{device} eq 'psaux' && member($mouse->{XMOUSETYPE}, 'IMPS/2', 'ExplorerPS/2')) { $draw_text->(N("To activate the mouse,"), 240); $draw_text->(N("MOVE YOUR WHEEL!"), 260); } } }; my $timeout; my $paintButton = sub { my ($nb) = @_; $timeout or $drawarea->(); if ($nb == 0) { $draw_by_name->('left'); } elsif ($nb == 2) { $draw_by_name->('right'); } elsif ($nb == 1) { if ($mouse->{nbuttons} >= 3) { $draw_by_name->('middle'); } else { my ($x, $y) = @{$offsets{mouse_2b_middle}}; $darea->window->draw_arc($darea->style->black_gc, 1, ($darea->allocation->width-$width)/2 + $x, ($darea->allocation->height-$height)/2 + $y, 20, 25, 0, 360 * 64); } } elsif ($mouse->{nbuttons} > 3) { my ($x, $y) = @{$offsets{$image_files{middle}}}; if ($nb == 3) { $draw_pixbuf->($images{up}, $x+6, $y-10); } elsif ($nb == 4) { $draw_pixbuf->($images{down}, $x+6, $y + $images{middle}->get_height + 2); } $draw_by_name->('middle'); $timeout and Glib::Source->remove($timeout); $timeout = Glib::Timeout->add(100, sub { $drawarea->(); $timeout = 0; 0 }); } }; $darea->signal_connect(button_press_event => sub { $paintButton->($_[1]->button - 1) }); $darea->signal_connect(scroll_event => sub { $paintButton->($_[1]->direction eq 'up' ? 3 : 4) }); $darea->signal_connect(button_release_event => $drawarea); $darea->signal_connect(expose_event => $drawarea); $darea->set_size_request($width, $height); } =begin =head1 NAME mouse - Perl functions to handle mice =head1 SYNOPSYS require modules; require mouse; mouse::detect(modules::any_conf->read); =head1 DESCRIPTION C is a perl module used by mousedrake to detect and configure the mouse. =head1 COPYRIGHT Copyright (C) 2000-2002 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 2, 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. =cut 6'>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
package Xconfig::card; # $Id$

use diagnostics;
use strict;

use detect_devices;
use modules;
use common;
use log;


my $force_xf4 = arch() =~ /ppc|ia64/;


my %VideoRams = (
     256 => N_("256 kB"),
     512 => N_("512 kB"),
    1024 => N_("1 MB"),
    2048 => N_("2 MB"),
    4096 => N_("4 MB"),
    8192 => N_("8 MB"),
   16384 => N_("16 MB"),
   32768 => N_("32 MB"),
   65536 => N_("64 MB or more"),
);

our %serversdriver = arch() =~ /^sparc/ ? (
    'Mach64'    => "accel",
    '3DLabs'    => "accel",
    'Sun'       => "fbdev",
    'Sun24'     => "fbdev",
    'SunMono'   => "fbdev",
    'VGA16'     => "vga16",
    'FBDev'     => "fbdev",
) : (
    'SVGA'      => "svga",
    'S3'        => "accel",
    'Mach32'    => "accel",
    'Mach8'     => "accel",
    '8514'      => "accel",
    'P9000'     => "accel",
    'AGX'       => "accel",
    'W32'       => "accel",
    'Mach64'    => "accel",
    'I128'      => "accel",
    'S3V'       => "accel",
    '3DLabs'    => "accel",
    'VGA16'     => "vga16",
    'FBDev'     => "fbdev",
);
my @allbutfbservers = grep { arch() =~ /^sparc/ || $serversdriver{$_} ne "fbdev" } keys(%serversdriver);
my @allservers = keys(%serversdriver);

my @xfree4_Drivers = ((arch() =~ /^sparc/ ? qw(sunbw2 suncg14 suncg3 suncg6 sunffb sunleo suntcx) :
		    qw(apm ark chips cirrus cyrix glide i128 i740 i810 imstt 
                       mga neomagic newport nv rendition r128 radeon vesa
                       s3 s3virge savage siliconmotion sis tdfx tga trident tseng vmware)), 
		    qw(ati glint vga fbdev));


#- using XF4 if {Driver} && !{prefer_xf3} otherwise using XF3
#- error if $force_xf4 && !{Driver} || !{Driver} && !{server}
#- internal error if $force_xf4 && {prefer_xf3} || {prefer_xf3} && !{server}

sub using_xf4 {
    my ($card) = @_;
    $card->{Driver} && !$card->{prefer_xf3};
}

sub server_binary {
    my ($card) = @_;
    "/usr/X11R6/bin/" . 
      (using_xf4($card) ? 'XFree86' : 
       $card->{server} =~ /Sun(.*)/ ? "Xsun$1" : 
       $card->{server} eq 'Xpmac' ? 'Xpmac' :
       "XF86_$card->{server}");
}

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

    my $device = $raw_X->get_device or die "no card configured";

    my ($xfree3_server) = readlink("$::prefix/etc/X11/X") =~ /XF86_(.*)/;

    my $card = {
	use_UTAH_GLX => (any { /glx/ } $raw_X->{xfree3}->get_modules),
	use_DRI_GLX  => (any { /dri/ } $raw_X->{xfree4}->get_modules),
	server => $xfree3_server,
	prefer_xf3 => $xfree3_server && !$force_xf4,
	%$device,
    };
    add_to_card__using_Cards($card, $card->{BoardName});
    $card->{prog} = server_binary($card);
    $card;
}

sub to_raw_X {
    my ($card, $raw_X) = @_;

    $raw_X->set_devices($card, @{$card->{cards} || []});

    $raw_X->{xfree4}->get_ServerLayout->{Xinerama} = { commented => !$card->{Xinerama}, Option => 1 }
      if defined $card->{Xinerama};

    $raw_X->{xfree3}->set_load_module('glx-3.so', $card->{use_UTAH_GLX}); #- glx.so may clash with server version 4.

    $raw_X->{xfree4}->set_load_module('glx', !$card->{DRI_GLX_SPECIAL}); #- glx for everyone, except proprietary nvidia
    $raw_X->{xfree4}->set_load_module('dri', $card->{use_DRI_GLX} && !$card->{DRI_GLX_SPECIAL});

    # This loads the NVIDIA GLX extension module.
    # IT IS IMPORTANT TO KEEP NAME AS FULL PATH TO libglx.so ELSE
    # IT WILL LOAD XFree86 glx module and the server will crash.
    $raw_X->{xfree4}->set_load_module('/usr/X11R6/lib/modules/extensions/libglx.so', $card->{DRI_GLX_SPECIAL}); 

    $raw_X->{xfree4}->remove_Section('DRI');
    $raw_X->{xfree4}->add_Section('DRI', { Mode => { val => '0666' } }) if $card->{use_DRI_GLX};

    $raw_X->{xfree4}->remove_load_module('v4l') if $card->{use_DRI_GLX} && $card->{Driver} eq 'r128';
}

sub probe() {
#-for Pixel tests
#-    my @c = { driver => 'Card:Matrox Millennium G400 DualHead', description => 'Matrox|Millennium G400 Dual HeadCard' };
    my @c = grep { $_->{driver} =~ /(Card|Server|Driver):/ } detect_devices::probeall();

    my @cards = map {
	my @l = $_->{description} =~ /(.*?)\|(.*)/;
	my $card = { 
	    description => $_->{description},
	    VendorName => $l[0], BoardName => $l[1],
	    BusID => "PCI:$_->{pci_bus}:$_->{pci_device}:$_->{pci_function}",
	};
	if    ($_->{driver} =~ /Card:(.*)/)   { $card->{BoardName} = $1; add_to_card__using_Cards($card, $1) }
	elsif ($_->{driver} =~ /Server:(.*)/) { $card->{server} = $1 }
	elsif ($_->{driver} =~ /Driver:(.*)/) { $card->{Driver} = $1 }
	else { internal_error() }

	$_->{VideoRam} = 4096 if $_->{Driver} eq 'i810';
	$_->{Options_xfree4}{UseFBDev} = undef if arch() =~ /ppc/ && $_->{Driver} eq 'r128';

	$card;
    } @c;

    if (@cards >= 2 && $cards[0]{card_name} eq $cards[1]{card_name} && $cards[0]{card_name} eq 'Intel 830') {
	shift @cards;
    }
    #- take a default on sparc if nothing has been found.
    if (arch() =~ /^sparc/ && !@cards) {
        log::l("Using probe with /proc/fb as nothing has been found!");
	my $s = cat_("/proc/fb");
	@cards = { server => $s =~ /Mach64/ ? "Mach64" : $s =~ /Permedia2/ ? "3DLabs" : "Sun24" };
    }

    #- disabling MULTI_HEAD when not available
    foreach (@cards) { 
	$_->{MULTI_HEAD} && $_->{card_name} =~ /G[24]00/ or next;
	if ($ENV{MATROX_HAL}) {
	    $_->{need_MATROX_HAL} = 1;
	} else {
	    delete $_->{MULTI_HEAD};
	}
    }

    #- in case of only one cards, remove all BusID reference, this will avoid
    #- need of change of it if the card is moved.
    #- on many PPC machines, card is on-board, BusID is important, leave?
    if (@cards == 1 && !$cards[0]{MULTI_HEAD} && arch() !~ /ppc/) {
	delete $cards[0]{BusID};
    }

    @cards;
}

sub card_config__not_listed {
    my ($in, $card, $options) = @_;

    my $vendors_regexp = join '|', map { quotemeta } (
        '3Dlabs',
        'AOpen', 'ASUS', 'ATI', 'Ark Logic', 'Avance Logic',
        'Cardex', 'Chaintech', 'Chips & Technologies', 'Cirrus Logic', 'Compaq', 'Creative Labs',
        'Dell', 'Diamond', 'Digital',
        'ET', 'Elsa',
        'Genoa', 'Guillemot', 'Hercules', 'Intel', 'Leadtek',
        'Matrox', 'Miro', 'NVIDIA', 'NeoMagic', 'Number Nine',
        'Oak', 'Orchid',
        'RIVA', 'Rendition Verite',
        'S3', 'Silicon Motion', 'STB', 'SiS', 'Sun',
        'Toshiba', 'Trident',
        'VideoLogic',
    );
    my $cards = readCardsDB("$ENV{SHARE_PATH}/ldetect-lst/Cards+");

    my @xf3 = $options->{allowFB} ? @allservers : @allbutfbservers;
    my @xf4 = grep { $options->{allowFB} || $_ ne 'fbdev' } @xfree4_Drivers;
    my @list = (
	(map { 'Vendor|' . $_ } keys %$cards),
        if_(!$force_xf4, map { 'XFree 3|' . $_ } @xf3), 
	(map { 'XFree 4|' . $_ } @xf4),
    );

    my $r = exists $cards->{$card->{BoardName}} ? "Vendor|$card->{BoardName}" : 'XFree 4|vesa';
    $in->ask_from_({ title => N("X server"), 
		     messages => N("Choose an X server"),
		     interactive_help_id => 'configureX_card_list',
		   },
		   [ { val => \$r, separator => '|', list => \@list, sort => 1,
		       format => sub { $_[0] =~ /^Vendor\|($vendors_regexp)\s*-?(.*)/ ? "Vendor|$1|$2" : 
				       $_[0] =~ /^Vendor\|(.*)/ ? "Vendor|Other|$1" : $_[0] } } ]) or return;

    log::l("Xconfig::card: $r manually chosen");

    $r eq "Vendor|$card->{BoardName}" and return 1; #- it is unchanged, don't modify $card

    my ($kind, $s) = $r =~ /(.*?)\|(.*)/;

    %$card = ();
    if ($kind eq 'Vendor') {
	add_to_card__using_Cards($card, $s);
    } elsif ($kind eq 'XFree 3') {
	$card->{server} = $s;
    } else {
	$card->{Driver} = $s;
    }
    $card->{manually_chosen} = 1;
    1;
}

sub multi_head_choose {
    my ($in, $auto, @cards) = @_;

    my @choices = multi_head_choices('', @cards);

    my $tc = $choices[0];
    if (!$auto) {
	$tc = $in->ask_from_listf(N("Multi-head configuration"),
				  N("Your system supports multiple head configuration.
What do you want to do?"), sub { $_[0]{text} }, \@choices) or return;
    }
    $tc->{code} or die internal_error();
    return $tc->{code}();
}

sub configure_auto_install {
    my ($raw_X, $do_pkgs, $old_X, $options) = @_;

    {
	my $card = $old_X->{card} || {};
	if ($card->{card_name}) {
	    #- try to get info from given card_name
	    add_to_card__using_Cards($card, $card->{card_name});
	    undef $card->{card_name} if !$card->{server} && !$card->{Driver}; #- bad card_name as we can't find the server
	}
	return if $card->{server} || $card->{Driver};
    }

    my @cards = probe();
    my ($choice) = multi_head_choices($old_X->{Xinerama}, @cards) or log::l('no graphic card probed, try providing one using $o->{card}{Driver} or $o->{card}{server} or $o->{card}{card_name}'), return;
    my $card = $choice->{code}();

    my ($glx_choice) = xfree_and_glx_choices($card);
    log::l("Using $glx_choice->{text}");
    $glx_choice->{code}();
    set_glx_restrictions($card);

    $card->{prog} = install_server($card, $options, $do_pkgs);
    if ($card->{needVideoRam} && !$card->{VideoRam}) {
	$card->{VideoRam} = $options->{VideoRam_probed} || 4096;
	log::l("argh, I need to know VideoRam! Taking " . ($options->{probed_VideoRam} ? "the probed" : "a default") . " value: VideoRam = $card->{VideoRam}");
    }
    to_raw_X($card, $raw_X);
    $card;
}

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

    my @cards = probe();
    @cards or @cards = {};

    if (!$cards[0]{server} && !$cards[0]{Driver}) {
	if ($options->{allowFB}) {
	    $cards[0]{Driver} = 'fbdev';
	} elsif ($auto) {
	    log::l("Xconfig::card: auto failed (unknown card and no allowFB)");
	    return 0;
	}
    }
    if (!$auto) {
	card_config__not_listed($in, $cards[0], $options) or return;
    }

    my $card = multi_head_choose($in, $auto, @cards) or return;

    xfree_and_glx_choose($in, $card, $auto) or return;

    if (member($card->{card_name}, 'Intel 845', 'Intel 85x', 'Intel 865')) {
	$raw_X->{xfree4}->get_Section('ServerFlags')->{DontVTSwitch} = [ { val => 'yes', Option => 1 } ];
    }

    $card->{prog} = install_server($card, $options, $do_pkgs);
    
    if ($card->{needVideoRam} && !$card->{VideoRam}) {
	if ($auto) {
	    log::l("Xconfig::card: auto failed (needVideoRam)");
	    return;
	}
	$card->{VideoRam} = (find { $_ <= $options->{VideoRam_probed} } reverse ikeys %VideoRams) || 4096;
	$in->ask_from('', N("Select the memory size of your graphics card"),
		      [ { val => \$card->{VideoRam},
			  type => 'list',
			  list => [ ikeys %VideoRams ],
			  format => sub { translate($VideoRams{$_[0]}) },
			  not_edit => !$::expert } ]) or return;
    }

    to_raw_X($card, $raw_X);
    $card;
}

sub install_server {
    my ($card, $options, $do_pkgs) = @_;

    my $prog = server_binary($card);

    my @packages;
    push @packages, using_xf4($card) ? 'XFree86-server' : "XFree86-$card->{server}" if ! -x "$::prefix$prog";

    #- additional packages to install according available card.
    #- add XFree86-libs-DRI here if using DRI (future split of XFree86 TODO)
    if ($card->{use_DRI_GLX}) {
	push @packages, 'Glide_V5' if $card->{card_name} eq 'Voodoo5 (generic)';
	push @packages, 'Glide_V3-DRI' if member($card->{card_name}, 'Voodoo3 (generic)', 'Voodoo Banshee (generic)');
	push @packages, 'XFree86-glide-module' if $card->{card_name} =~ /Voodoo/;
    }
    if ($card->{use_UTAH_GLX}) {
	push @packages, 'Mesa';
    }
    #- 3D acceleration configuration for XFree 4 using NVIDIA driver (TNT, TN2 and GeForce cards only).
    push @packages, @{$options->{allowNVIDIA_rpms}} if $card->{Driver2} eq 'nvidia' && $options->{allowNVIDIA_rpms};

    $do_pkgs->install(@packages) if @packages;
    -x "$::prefix$prog" or die "server $card->{server} is not available (should be in $::prefix$prog)";

    #- make sure everything is correct at this point, packages have really been installed
    #- and driver and GLX extension is present.
    if ($card->{Driver2} eq 'nvidia' &&
	-e "$::prefix/usr/X11R6/lib/modules/drivers/nvidia_drv.o" &&
	-e "$::prefix/usr/X11R6/lib/modules/extensions/libglx.so") {
	log::l("Using specific NVIDIA driver and GLX extensions");
	$card->{Driver} = 'nvidia';
	$card->{DRI_GLX_SPECIAL} = 1;
    }

    if ($card->{need_MATROX_HAL}) {
	require Xconfig::proprietary;
	Xconfig::proprietary::install_matrox_hal($::prefix);
    }

    $prog;
}

sub xfree_and_glx_choose {
    my ($in, $card, $auto) = @_;

    my @choices = xfree_and_glx_choices($card);

    my $tc = 
      $auto ? $choices[0] :
	$in->ask_from_listf_raw({ title => N("XFree configuration"), 
				  messages => formatAlaTeX(join("\n\n\n", (grep { $_ } map { $_->{more_messages} } @choices),
								N("Which configuration of XFree do you want to have?"))), 
				  interactive_help_id => 'configureX_xfree_and_glx',
				},
				sub { $_[0]{text} }, \@choices) or return;
    log::l("Using $tc->{text}");
    $tc->{code}();
    set_glx_restrictions($card);
    1;
}

sub multi_head_choices {
    my ($want_Xinerama, @cards) = @_;
    my @choices;

    my $has_multi_head = @cards > 1 || @cards && $cards[0]{MULTI_HEAD} > 1;
    my $disable_multi_head = any { 
	$_->{Driver} or log::l("found card $_->{description} not supported by XF4, disabling multi-head support");
	!$_->{Driver};
    } @cards;

    if ($has_multi_head && !$disable_multi_head) {
	my $configure_multi_head = sub {

	    #- special case for multi head card using only one BusID.
	    @cards = map {
		map_index { { Screen => $::i, %$_ } } ($_) x ($_->{MULTI_HEAD} || 1);
	    } @cards;

	    delete $_->{server} foreach @cards; #- XFree 3 doesn't handle multi head (?)
	    my $card = shift @cards; #- assume good default.
	    $card->{cards} = \@cards;
	    $card->{Xinerama} = $_[0];
	    $card;
	};
	my $independent = { text => N("Configure all heads independently"), code => sub { $configure_multi_head->('') } };
	my $xinerama    = { text => N("Use Xinerama extension"),            code => sub { $configure_multi_head->(1) } };
	push @choices, $want_Xinerama ? ($xinerama, $independent) : ($independent, $xinerama);
    }

    foreach my $c (@cards) {
	push @choices, { text => N("Configure only card \"%s\"%s", $c->{description}, $c->{BusID} && " ($c->{BusID})"),
			 code => sub { $c } };
    }
    @choices;
}

#- XFree version available, it would be better to parse available package and get version from it.
sub xfree4_version() { '4.3' }
sub xfree3_version() { '3.3.6' }

sub xfree_and_glx_choices {
    my ($card) = @_;

    my $xf3 = if_($card->{server}, { text => N("XFree %s", xfree3_version()), code => sub { $card->{prefer_xf3} = 1 } });
    my $xf4 = if_($card->{Driver}, { text => N("XFree %s", xfree4_version()), code => sub { $card->{prefer_xf3} = 0 } });

    #- no XFree3 with multi-head
    my @choices = grep { $_ } ($card->{cards} ? $xf4 : $card->{prefer_xf3} ? ($xf3, $xf4) : ($xf4, $xf3));

    #- no GLX with Xinerama
    return @choices if $card->{Xinerama};

    #- try to figure if 3D acceleration is supported
    #- by XFree 3.3 but not XFree 4 then ask user to keep XFree 3.3 ?
    if ($card->{UTAH_GLX}) {
	my $e = { text => N("XFree %s with 3D hardware acceleration", xfree3_version()),
			    code => sub { $card->{prefer_xf3} = 1; $card->{use_UTAH_GLX} = 1 },
			    more_messages => ($card->{Driver} && !$card->{DRI_GLX} ?
N("Your card can have 3D hardware acceleration support but only with XFree %s.
Your card is supported by XFree %s which may have a better support in 2D.", xfree3_version(), xfree4_version()) :
N("Your card can have 3D hardware acceleration support with XFree %s.", xfree3_version())),
			  };
	$card->{prefer_xf3} ? unshift(@choices, $e) : push(@choices, $e);
    }

    #- an expert user may want to try to use an EXPERIMENTAL 3D acceleration, currenlty
    #- this is with Utah GLX and so, it can provide a way of testing.
    if ($card->{UTAH_GLX_EXPERIMENTAL} && $::expert) {
	push @choices, { text => N("XFree %s with EXPERIMENTAL 3D hardware acceleration", xfree3_version()),
			 code => sub { $card->{prefer_xf3} = 1; $card->{use_UTAH_GLX} = 1 },
			 more_messages => (using_xf4($card) && !$card->{DRI_GLX} ?
N("Your card can have 3D hardware acceleration support but only with XFree %s,
NOTE THIS IS EXPERIMENTAL SUPPORT AND MAY FREEZE YOUR COMPUTER.
Your card is supported by XFree %s which may have a better support in 2D.", xfree3_version(), xfree4_version()) :
N("Your card can have 3D hardware acceleration support with XFree %s,
NOTE THIS IS EXPERIMENTAL SUPPORT AND MAY FREEZE YOUR COMPUTER.", xfree3_version())),
		       };
    }

    #- ask the expert or any user on second pass user to enable or not hardware acceleration support.
    if ($card->{DRI_GLX}) {
	unshift @choices, { text => N("XFree %s with 3D hardware acceleration", xfree4_version()),
			    code => sub { $card->{prefer_xf3} = 0; $card->{use_DRI_GLX} = 1 },
			    more_messages => N("Your card can have 3D hardware acceleration support with XFree %s.", xfree4_version()),
			  };
    }

    #- an expert user may want to try to use an EXPERIMENTAL 3D acceleration.
    if ($card->{DRI_GLX_EXPERIMENTAL} && $::expert) {
	push @choices, { text => N("XFree %s with EXPERIMENTAL 3D hardware acceleration", xfree4_version()),
			 code => sub { $card->{prefer_xf3} = 0; $card->{use_DRI_GLX} = 1 },
			 more_messages => N("Your card can have 3D hardware acceleration support with XFree %s,
NOTE THIS IS EXPERIMENTAL SUPPORT AND MAY FREEZE YOUR COMPUTER.", xfree4_version()),
		       };
    }

    if (arch() =~ /ppc/ && $ENV{DISPLAY}) {
	push @choices, { text => N("Xpmac (installation display driver)"), code => sub { 
			     #- HACK: re-allowing XFree 3
			     $::force_xf4 = 0;
			     $card->{server} = "Xpmac";
			     $card->{prefer_xf3} = 1;
			 } };
    }
    @choices;
}

sub set_glx_restrictions {
    my ($card) = @_;

    #- hack for ATI Mach64 cards where two options should be used if using Utah-GLX.
    if (member($card->{card_name}, 'ATI Mach64 Utah', 'ATI Rage Mobility')) {
	$card->{Options_xfree3}{no_font_cache} = undef if $card->{use_UTAH_GLX};
	$card->{Options_xfree3}{no_pixmap_cache} = undef if $card->{use_UTAH_GLX};
    }
    #- hack for SiS cards where an option should be used if using Utah-GLX.
    if (member($card->{card_name}, 'SiS 6326', 'SiS 630')) {
	$card->{Options_xfree3}{no_pixmap_cache} = undef if $card->{use_UTAH_GLX};
    }

    #- 3D acceleration configuration for XFree 4 using DRI, this is enabled by default
    #- but for some there is a need to specify VideoRam (else it won't run).
    if ($card->{use_DRI_GLX}) {
	$card->{needVideoRam} = 1 if $card->{description} =~ /Matrox.* G[245][05]0/;
	($card->{needVideoRam}, $card->{VideoRam}) = (1, 16384)
	  if member($card->{card_name}, 'Intel 810', 'Intel 815');

	#- hack for ATI Rage 128 card using a bttv or peripheral with PCI bus mastering exchange
	#- AND using DRI at the same time.
	if (member($card->{card_name}, 'ATI Rage 128', 'ATI Rage 128 TVout', 'ATI Rage 128 Mobility')) {
	    $card->{Options_xfree4}{UseCCEFor2D} = bool2text(modules::probe_category('multimedia/tv'));
	}
    }

    #- check for Matrox G200 PCI cards, disable AGP in such cases, causes black screen else.
    if (member($card->{card_name}, 'Matrox Millennium 200', 'Matrox Millennium 200', 'Matrox Mystique') && $card->{description} !~ /AGP/) {
	log::l("disabling AGP mode for Matrox card, as it seems to be a PCI card");
	log::l("this is only used for XFree 3.3.6, see /etc/X11/glx.conf");
	substInFile { s/^\s*#*\s*mga_dma\s*=\s*\d+\s*$/mga_dma = 0\n/ } "$::prefix/etc/X11/glx.conf";
    }
}

sub add_to_card__using_Cards {
    my ($card, $name) = @_;
    my $cards = readCardsDB("$ENV{SHARE_PATH}/ldetect-lst/Cards+");
    add2hash($card, $cards->{$name});
    $card->{BoardName} = $card->{card_name};

    delete @$card{'server'} if $force_xf4;

    delete @$card{'UTAH_GLX', 'UTAH_GLX_EXPERIMENTAL'} 
      if $force_xf4 || availableRamMB() > 800; #- no Utah GLX if more than 800 Mb (server, or kernel-enterprise, Utha GLX does not work with latest).

    $card;
}

#- needed for bad cards not restoring cleanly framebuffer, according to which version of XFree are used.
sub check_bad_card {
    my ($card) = @_;
    my $bad_card = using_xf4($card) ? $card->{BAD_FB_RESTORE} : $card->{BAD_FB_RESTORE_XF3};
    $bad_card ||= $card->{Driver} eq 'i810' || $card->{Driver} eq 'fbdev';
    $bad_card ||= $card->{Driver} eq 's3virge' if $::live;
    $bad_card ||= $card->{Driver} eq 'nvidia' if !$::isStandalone; #- avoid testing during install at any price.
    $bad_card ||= $card->{server} =~ /FBDev|Sun/ if !using_xf4($card);

    log::l("the graphics card does not like X in framebuffer") if $bad_card;

    !$bad_card;
}

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

    my $F = openFileMaybeCompressed($file);

    my ($lineno, $cmd, $val) = 0;
    my $fs = {
	NAME => sub {
	    $cards{$card->{card_name}} = $card if $card;
	    $card = { card_name => $val };
	},
	SEE => sub {
	    my $c = $cards{$val} or die "Error in database, invalid reference $val at line $lineno";
	    add2hash($card, $c);
	},
        LINE => sub { $val =~ s/^\s*//; $card->{raw_LINES} .= "$val\n" },
	CHIPSET => sub { $card->{Chipset} = $val },
	SERVER => sub { $card->{server} = $val },
	DRIVER => sub { $card->{Driver} = $val },
	DRIVER2 => sub { $card->{Driver2} = $val },
	NEEDVIDEORAM => sub { $card->{needVideoRam} = 1 },
	DRI_GLX => sub { $card->{DRI_GLX} = 1 if $card->{Driver} },
	UTAH_GLX => sub { $card->{UTAH_GLX} = 1 if $card->{server} },
	DRI_GLX_EXPERIMENTAL => sub { $card->{DRI_GLX_EXPERIMENTAL} = 1 if $card->{Driver} },
	UTAH_GLX_EXPERIMENTAL => sub { $card->{UTAH_GLX_EXPERIMENTAL} = 1 if $card->{server} },
	MULTI_HEAD => sub { $card->{MULTI_HEAD} = $val if $card->{Driver} },
	BAD_FB_RESTORE => sub { $card->{BAD_FB_RESTORE} = 1 },
	BAD_FB_RESTORE_XF3 => sub { $card->{BAD_FB_RESTORE_XF3} = 1 },
	FB_TVOUT => sub { $card->{FB_TVOUT} = 1 },
	UNSUPPORTED => sub { delete $card->{Driver} },

	COMMENT => sub {},
    };

    local $_;
    while (<$F>) { $lineno++;
	s/\s+$//;
	/^#/ and next;
	/^$/ and next;
	/^END/ and do { $cards{$card->{card_name}} = $card if $card; last };

	($cmd, $val) = /(\S+)\s*(.*)/ or next;

	my $f = $fs->{$cmd};

	$f ? $f->() : log::l("unknown line $lineno ($_)");
    }
    \%cards;
}
1;