diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Xconfig/.perl_checker | 1 | ||||
-rw-r--r-- | lib/Xconfig/FILES | 6 | ||||
-rw-r--r-- | lib/Xconfig/card.pm | 521 | ||||
-rw-r--r-- | lib/Xconfig/default.pm | 39 | ||||
-rw-r--r-- | lib/Xconfig/main.pm | 253 | ||||
-rw-r--r-- | lib/Xconfig/monitor.pm | 338 | ||||
-rw-r--r-- | lib/Xconfig/parse.pm | 211 | ||||
-rw-r--r-- | lib/Xconfig/plugins.pm | 232 | ||||
-rw-r--r-- | lib/Xconfig/proprietary.pm | 118 | ||||
-rw-r--r-- | lib/Xconfig/resolution_and_depth.pm | 477 | ||||
-rw-r--r-- | lib/Xconfig/screen.pm | 29 | ||||
-rw-r--r-- | lib/Xconfig/test.pm | 137 | ||||
-rw-r--r-- | lib/Xconfig/various.pm | 411 | ||||
-rw-r--r-- | lib/Xconfig/xfree.pm | 857 | ||||
-rw-r--r-- | lib/keyboard.pm | 688 | ||||
-rw-r--r-- | lib/mouse.pm | 677 | ||||
-rw-r--r-- | lib/xf86misc/Makefile | 10 | ||||
-rw-r--r-- | lib/xf86misc/Makefile.PL | 13 | ||||
-rw-r--r-- | lib/xf86misc/main.pm | 12 | ||||
-rw-r--r-- | lib/xf86misc/main.xs | 76 |
20 files changed, 5106 insertions, 0 deletions
diff --git a/lib/Xconfig/.perl_checker b/lib/Xconfig/.perl_checker new file mode 100644 index 0000000..2326e2f --- /dev/null +++ b/lib/Xconfig/.perl_checker @@ -0,0 +1 @@ +Basedir ../.. diff --git a/lib/Xconfig/FILES b/lib/Xconfig/FILES new file mode 100644 index 0000000..f6352de --- /dev/null +++ b/lib/Xconfig/FILES @@ -0,0 +1,6 @@ + +parse --- xfree ---- default ------------------ main + \--- card -------------------/ test + \-- monitor ---------------/ various + \- screen ---------------/ proprietary + \ resolution_and_depth-/ diff --git a/lib/Xconfig/card.pm b/lib/Xconfig/card.pm new file mode 100644 index 0000000..7a10606 --- /dev/null +++ b/lib/Xconfig/card.pm @@ -0,0 +1,521 @@ +package Xconfig::card; # $Id: card.pm 266355 2010-02-22 20:51:38Z pzanoni $ + +use diagnostics; +use strict; + +use lib '/usr/lib/libDrakX'; +use detect_devices; +use Xconfig::xfree; +use modules; +use common; +use interactive; +use log; +use run_program; + +my $lib = arch() =~ /x86_64/ ? "lib64" : "lib"; + +sub modules_dir() { "/usr/$lib/xorg/modules" } + +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"), +); + +my @xfree4_Drivers = ((arch() =~ /^sparc/ ? qw(sunbw2 suncg14 suncg3 suncg6 sunffb sunleo suntcx) : + qw(apm ark ast chips cirrus i128 i740 intel mga + neomagic newport nouveau nv openchrome psb qxl + rendition s3 s3virge savage siliconmotion sis sisusb + tdfx tga trident tseng vesa vmware xgi xgixp)), + if_(arch() =~ /i.86/, qw(geode)), + qw(ati glint fbdev)); + +sub from_raw_X { + my ($raw_X) = @_; + + my ($device, @cards) = $raw_X->get_devices or die "no card configured"; + + my $card = { + use_DRI_GLX => eval { any { /dri/ } $raw_X->get_modules }, + DRI_GLX_SPECIAL => $device->{Driver} eq 'nvidia' && eval { any { $_ eq 'glx' } $raw_X->get_modules }, + %$device, + if_($device->{Driver} eq 'nvidia', + DriverVersion => + readlink("$::prefix/etc/alternatives/gl_conf") =~ m!nvidia(.*)/! ? $1 : '97xx'), + if_(@cards, cards => \@cards), + }; + add_to_card__using_Cards($card, $card->{BoardName}); + $card; +} + +sub to_raw_X { + my ($card, $raw_X) = @_; + + my @cards = ($card, @{$card->{cards} || []}); + + foreach (@cards) { + if (arch() =~ /ppc/ && ($_->{Driver} eq 'r128' || $_->{Driver} eq 'radeon')) { + $_->{UseFBDev} = 1; + } + } + + $raw_X->set_devices(@cards); + + $raw_X->get_ServerLayout->{Xinerama} = { commented => !$card->{Xinerama}, Option => 1 } + if defined $card->{Xinerama}; + + # cleanup deprecated previous special nvidia explicit libglx + $raw_X->remove_load_module(modules_dir() . "$_/libglx.so") foreach '/extensions/nvidia', '/extensions/nvidia_legacy', '/extensions'; + + # remove ModulePath that we added + $raw_X->remove_ModulePath(modules_dir() . "/extensions/$_") foreach 'nvidia97xx', 'nvidia96xx', 'nvidia71xx', 'nvidia-current'; + $raw_X->remove_ModulePath(modules_dir()); + #- if we have some special ModulePath, ensure the last one is the standard ModulePath + $raw_X->add_ModulePath(modules_dir()) if $raw_X->get_ModulePaths; + + #- un-disable modules that we previously disabled + $raw_X->remove_disable_module('glx'); + $raw_X->remove_disable_module('dri'); + + $raw_X->remove_Section('DRI'); + + $raw_X->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 = detect_devices::matching_driver__regexp('^(Card|Server|Driver):'); + + # prefer the boot device + my @cards = sort { $b->{boot_device} cmp $a->{boot_device} } map { + my @l = $_->{description} =~ /(.*?)\|(.*)/; + my $card = { + description => $_->{description}, + VendorName => $l[0], BoardName => $l[1], + BusID => "PCI:$_->{pci_bus}:$_->{pci_device}:$_->{pci_function}", + boot_device => chomp_(cat_($_->{sysfs_device} . "/boot_vga")) || 0, + }; + if (my ($card_name) = $_->{driver} =~ /Card:(.*)/) { + $card->{BoardName} = $card_name; + add_to_card__using_Cards($card, $card_name); + } elsif ($_->{driver} =~ /Driver:(.*)/) { + $card->{Driver} = $1; + } else { + internal_error(); + } + #dual head ATI card have a dummy DISPLAY_OTHER pci device because it + #was needed by Win2000, filter those out because we don't want to + #behave as if there were 2 video cards in the system in such cases + ($_->{media_type} eq 'DISPLAY_VGA') ? $card : (); + } @c; + + if (@cards >= 2 && $cards[0]{card_name} eq $cards[1]{card_name} && $cards[0]{card_name} eq 'Intel 830 - 965') { + 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 @xf4 = grep { $options->{allowFB} || $::isStandalone || $_ ne 'fbdev' } + uniq(@xfree4_Drivers, map { $_->{Driver} } values %$cards); + my @list = ( + (map { 'Vendor|' . $_ } keys %$cards), + (map { 'Xorg|' . $_ } @xf4), + ); + + my $r = exists $cards->{$card->{BoardName}} ? "Vendor|$card->{BoardName}" : 'Xorg|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::explanations("Xconfig::card: $r manually chosen"); + + $r eq "Vendor|$card->{BoardName}" and return 1; #- it is unchanged, do not modify $card + + my ($kind, $s) = $r =~ /(.*?)\|(.*)/; + + %$card = (); + if ($kind eq 'Vendor') { + add_to_card__using_Cards($card, $s); + } else { + $card->{Driver} = $s; + $card->{DRI_GLX} = 0; + } + $card->{manually_chosen} = 1; + 1; +} + +sub multi_head_choose { + my ($in, $_auto, @cards) = @_; + + my @choices = multi_head_choices('', @cards); + + my $tc = $choices[0]; + if (@choices > 1) { + $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}); + if (!$card->{Driver}) { + log::l("bad card_name $card->{card_name}, using probe"); + undef $card->{card_name}; + } + } + + my ($boot_xdriver) = cat_("/proc/cmdline") =~ /.*\bxdriver=(\S+)/; + + $options->{freedriver} = 1 if $boot_xdriver eq 'free'; + + if (!$card->{Driver} && $boot_xdriver && !member($boot_xdriver, 'auto', 'free')) { + log::explanations("using driver $boot_xdriver from kernel command line"); + $card = { + Driver => $boot_xdriver, + description => "Set by boot parameter", + VendorName => "Custom", + BoardName => "Set by boot parameter", + }; + if ($boot_xdriver =~ /^(nvidia.|fglrx)/) { + $card->{Driver} = "vesa"; + $card->{Driver2} = $boot_xdriver; + } + } + + if (!$card->{Driver}) { + my @cards = probe(); + my ($choice) = multi_head_choices($old_X->{Xinerama}, @cards); + $card = $choice ? $choice->{code}() : do { + log::explanations('no graphic card probed, try providing one using $o->{card}{Driver} or $o->{card}{card_name}. Defaulting...'); + { Driver => $options->{allowFB} ? 'fbdev' : 'vesa' }; + }; + } + + install_server($card, $options, $do_pkgs, undef) or return; + $card = configure_Driver2($card, undef); + + Xconfig::various::various_auto_install($raw_X, $card, $old_X); + set_glx_restrictions($card); + + if ($card->{needVideoRam} && !$card->{VideoRam}) { + $card->{VideoRam} = $options->{VideoRam_probed} || 4096; + log::explanations("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]{Driver}) { + if ($options->{allowFB}) { + $cards[0]{Driver} = 'fbdev'; + } + } + if (!$auto || !$cards[0]{Driver}) { + card_config__not_listed: + card_config__not_listed($in, $cards[0], $options) or return; + } + + my $card = multi_head_choose($in, $auto, @cards) or return; + + install_server($card, $options, $do_pkgs, $in) or goto card_config__not_listed; + + $card = configure_Driver2($card, $in); + + Xconfig::various::various($in, $raw_X, $card, $options, $auto); + set_glx_restrictions($card); + + if ($card->{needVideoRam} && !$card->{VideoRam}) { + $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 => 0 } ]) or return; + } + + to_raw_X($card, $raw_X); + $card; +} + +sub install_server { + my ($card, $options, $do_pkgs, $o_in) = @_; + + my @packages; + my @must_have = "x11-driver-video-$card->{Driver}"; + + if ($options->{freedriver}) { + delete $card->{Driver2}; + } + + if ($card->{Driver2}) { + require Xconfig::proprietary; + Xconfig::proprietary::handle_DRIVER2_NO_SSE($card); + my @pkgs = Xconfig::proprietary::pkgs_for_Driver2($card->{Driver2}, $do_pkgs); + if (@pkgs && (!$o_in || $o_in->ask_yesorno('', formatAlaTeX(N("There is a proprietary driver available for your video card which may support additional features. +Do you wish to use it?")), 1))) { + push @packages, @pkgs; + } else { + delete $card->{Driver2}; + } + } + + $do_pkgs->ensure_are_installed([ @must_have, @packages ], 1) or + @must_have == listlength($do_pkgs->are_installed(@must_have)) + or return; + + if ($card->{need_MATROX_HAL}) { + require Xconfig::proprietary; + Xconfig::proprietary::install_matrox_hal($::prefix); + } + 1; +} + +sub configure_Driver2 { + my ($card, $o_in) = @_; + + if ($card->{Driver2}) { + require Xconfig::proprietary; + if (my $card2 = Xconfig::proprietary::may_use_Driver2($card)) { + $card = $card2; + } else { + $o_in and $o_in->ask_warn('', formatAlaTeX(N("The proprietary driver was not properly installed, defaulting to free software driver."))); + log::l("defaulting to free software driver"); + } + } + + libgl_config_and_more($card); + $card; +} + +#- configures which libGL.so.1 to use, using update-alternatives +#- it also configures nvidia_drv.so (using a slave alternative, cf "update-alternatives --display gl_conf") +sub libgl_config_and_more { + my ($card) = @_; + + if ($card->{Driver} eq 'nvidia') { + $card->{DriverVersion} or internal_error("DriverVersion should be set for driver nvidia!"); + } + + #- ensure old deprecated conf files are not there anymore + unlink("/etc/ld.so.conf.d/$_.conf") foreach 'nvidia', 'nvidia_legacy', 'ati'; + + my %files = ( + fglrx => "/etc/ld.so.conf.d/GL/ati$card->{DriverVersion}.conf", + nvidia => "/etc/nvidia$card->{DriverVersion}/ld.so.conf", + psb => "/etc/ld.so.conf.d/GL/libdrm-psb.conf", + ); + my $wanted = $files{$card->{Driver}} || '/etc/ld.so.conf.d/GL/standard.conf'; + my $link = "$::prefix/etc/alternatives/gl_conf"; + my $need_run_ldconfig = readlink($link) ne $wanted; + -e "$::prefix$wanted" or log::l("ERROR: $wanted does not exist, linking $link to it anyway"); + common::symlinkf_update_alternatives('gl_conf', $wanted); + if ($need_run_ldconfig) { + log::explanations("ldconfig will be run because the GL library was switched to $wanted"); + run_program::rooted($::prefix, 'ldconfig', '-X'); + } + + if (member($card->{Driver}, 'fglrx', 'nvidia')) { + log::l("workaround buggy fglrx/nvidia driver: make dm restart xserver (#29550, #38297)"); + eval { common::update_gnomekderc_no_create("$::prefix/etc/kde/kdm/kdmrc", 'X-:0-Core' => ( + TerminateServer => "true", + )) }; + eval { update_gnomekderc("$::prefix/etc/X11/gdm/custom.conf", daemon => ( + AlwaysRestartServer => "true", + )) }; + } +} + +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::explanations("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; + + 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; +} + +sub set_glx_restrictions { + my ($card) = @_; + + #- 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 will not run). + if ($card->{use_DRI_GLX}) { + $card->{needVideoRam} = 1 if $card->{description} =~ /Matrox.* G[245][05]0/; + ($card->{needVideoRam}, $card->{VideoRam}) = (1, 16384) + if $card->{card_name} eq 'Intel 810 / 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 ($card->{card_name} eq 'ATI Rage 128 TV-out') { + $card->{Options}{UseCCEFor2D} = bool2text(detect_devices::probe_category('multimedia/tv')); + } + } +} + +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}; + + $card; +} + +#- needed for bad cards not restoring cleanly framebuffer, according to which version of Xorg are used. +sub check_bad_card { + my ($card) = @_; + my $bad_card = $card->{BAD_FB_RESTORE}; + $bad_card ||= $card->{Driver} eq 'intel' || $card->{Driver} eq 'fbdev'; + $bad_card ||= member($card->{Driver}, 'nvidia', 'vmware') if !$::isStandalone; #- avoid testing during install at any price. + + log::explanations("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 = 0; + my ($cmd, $val); + 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 }, + DRIVER => sub { $card->{Driver} = $val }, + DRIVER2 => sub { $card->{Driver2} = $val }, + DRIVER2_NO_SSE => sub { $card->{DRIVER2_NO_SSE} = $val }, + NEEDVIDEORAM => sub { $card->{needVideoRam} = 1 }, + DRI_GLX => sub { $card->{DRI_GLX} = 1 if $card->{Driver} }, + DRI_GLX_EXPERIMENTAL => sub { $card->{DRI_GLX_EXPERIMENTAL} = 1 if $card->{Driver} }, + MULTI_HEAD => sub { $card->{MULTI_HEAD} = $val if $card->{Driver} }, + BAD_FB_RESTORE => sub { $card->{BAD_FB_RESTORE} = 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; + diff --git a/lib/Xconfig/default.pm b/lib/Xconfig/default.pm new file mode 100644 index 0000000..f0826bc --- /dev/null +++ b/lib/Xconfig/default.pm @@ -0,0 +1,39 @@ +package Xconfig::default; # $Id: default.pm 266062 2010-02-09 18:55:05Z anssi $ + +use diagnostics; +use strict; + +use Xconfig::xfree; +use keyboard; +use common; +use mouse; +use modules::any_conf; + + +sub configure { + my ($do_pkgs, $o_keyboard, $o_mouse) = @_; + + my $keyboard = $o_keyboard || keyboard::read_or_default(); + my $mouse = $o_mouse || do { + my $mouse = mouse::read(); + add2hash($mouse, mouse::detect(modules::any_conf->read)) if !$::noauto; + $mouse; + }; + + my $raw_X = Xconfig::xfree->empty_config; + + $raw_X->add_load_module('v4l'); + + config_mouse($raw_X, $do_pkgs, $mouse); + + $raw_X; +} + +sub config_mouse { + my ($raw_X, $do_pkgs, $mouse) = @_; + mouse::set_xfree_conf($mouse, $raw_X); + mouse::various_xfree_conf($do_pkgs, $mouse); +} + +1; + diff --git a/lib/Xconfig/main.pm b/lib/Xconfig/main.pm new file mode 100644 index 0000000..d92c27f --- /dev/null +++ b/lib/Xconfig/main.pm @@ -0,0 +1,253 @@ +package Xconfig::main; # $Id: main.pm 261482 2009-10-06 10:15:34Z cfergeau $ + +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 ], + }; + + $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); + my $configured = $X->{card} && Xconfig::screen::configure($raw_X); + + #- still try to configure screen and resolution (to set default background) + #- if card configuration failed (for example if best driver is not available) + $X->{resolutions} = Xconfig::resolution_and_depth::configure_auto_install($raw_X, $X->{card} ||{}, $X->{monitors}, $old_X); + return if !$configured; + + my $action = &write($raw_X, $X, $options->{skip_fb_setup}); + + $action; +} + +sub configure_everything { + my ($in, $raw_X, $do_pkgs, $auto, $options) = @_; + my $X = {}; + my $ok = 1; + + my $probed_info = Xconfig::monitor::probe(); + $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, $X->{card}, $X->{monitors}, $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("_: This is a display device\nMonitor"), 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 = 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, $o_skip_fb_setup) = @_; + export_to_install_X($X) if $::isInstall; + my $only_resolution = $raw_X->is_only_resolution_modified; + $raw_X->write; + Xconfig::various::check_xorg_conf_symlink(); + Xconfig::various::setup_kms(); + if ($X->{resolutions}[0]{bios}) { + Xconfig::various::setupFB($X->{resolutions}[0]{bios}) if !$o_skip_fb_setup;; + 'need_reboot'; + } elsif (my $resolution = $only_resolution && eval { $raw_X->get_resolution }) { + 'need_xrandr' . sprintf(' --size %dx%d', @$resolution{'X', 'Y'}); + } else { + # Always ask for reboot as the driver could've changed. + # TODO: Do 'need_restart' instead when no reboot needed, like no driver + # change. + 'need_reboot'; + } +} + + +sub export_to_install_X { + my ($X) = @_; + + my $resolution = $X->{resolutions}[0]; + $::o->{X}{resolution_wanted} = $resolution->{automatic} ? 'automatic' : $resolution->{X} . 'x' . $resolution->{Y}; + $::o->{X}{default_depth} = $resolution->{Depth} if $resolution->{Depth}; + $::o->{X}{bios_vga_mode} = $resolution->{bios} if $resolution->{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(Device Screen ServerLayout); + + ''; +} + +1; diff --git a/lib/Xconfig/monitor.pm b/lib/Xconfig/monitor.pm new file mode 100644 index 0000000..857ac01 --- /dev/null +++ b/lib/Xconfig/monitor.pm @@ -0,0 +1,338 @@ +package Xconfig::monitor; #- $Id: monitor.pm 261588 2009-10-08 17:02:40Z blino $ + +use diagnostics; +use strict; + +use Xconfig::xfree; +use detect_devices; +use common; +use any; +use log; + + +sub good_default_monitor() { + detect_devices::is_xbox() ? 'Generic|640x480 @ 60 Hz' : + arch() =~ /ppc/ ? + (detect_devices::get_mac_model() =~ /^iBook/ ? 'Apple|iBook 800x600' : 'Apple|iMac/PowerBook 1024x768') : + (detect_devices::isLaptop() ? 'Generic|Flat Panel 1024x768' : 'Generic|1024x768 @ 60 Hz'); +} + +sub default_monitor { + my ($card_Driver) = @_; + if (detect_devices::is_virtualbox() || detect_devices::isLaptop() || ($card_Driver eq 'siliconmotion' && arch() =~ /mips/)) { + # HACK: since there is no way to get the EDID on gdium, the resolution is passed to the kernel + # so we can rely on it + # in vbox, we return Plug'n'Play because the vbox integration addons + # will take care of everything for us + # On laptops the X server should usually be able to autodetect everything. + { VendorName => "Plug'n Play" }; + } else { + good_default_monitor() =~ /(.*)\|(.*)/ or internal_error("bad good_default_monitor"); + { VendorName => $1, ModelName => $2 }; + } +} + +my @VertRefresh_ranges = ("50-70", "50-90", "50-100", "40-150"); + +my @HorizSync_ranges = ( + "31.5", + "31.5-35.1", + "31.5-37.9", + "31.5-48.5", + "31.5-57.0", + "31.5-64.3", + "31.5-79.0", + "31.5-82.0", + "31.5-88.0", + "31.5-94.0", +); + +sub configure { + my ($in, $raw_X, $nb_monitors, $o_probed_info, $b_auto) = @_; + + my $monitors = [ $raw_X->get_or_new_monitors($nb_monitors) ]; + if ($o_probed_info) { + put_in_hash($monitors->[0], $o_probed_info); + } + my $head_nb = 1; + foreach my $monitor (@$monitors) { + choose($in, $raw_X, $monitor, @$monitors > 1 ? $head_nb++ : 0, $b_auto) or return; + } + $raw_X->set_monitors(@$monitors); + $monitors; +} + +sub configure_auto_install { + my ($raw_X, $old_X) = @_; + + if ($old_X->{monitor}) { + #- keep compatibility + $old_X->{monitor}{VertRefresh} = $old_X->{monitor}{vsyncrange}; + $old_X->{monitor}{HorizSync} = $old_X->{monitor}{hsyncrange}; + + #- new name + $old_X->{monitors} = [ delete $old_X->{monitor} ]; + } + + my $monitors = [ $raw_X->get_or_new_monitors($old_X->{monitors} ? int @{$old_X->{monitors}} : 1) ]; + mapn { + my ($monitor, $auto_install_monitor) = @_; + put_in_hash($monitor, $auto_install_monitor); + configure_automatic($monitor); + } $monitors, $old_X->{monitors} if $old_X->{monitors}; + + my $card_Driver; + if (!is_valid($monitors->[0])) { + my ($first_card) = Xconfig::card::probe(); + $card_Driver = $first_card->{Driver} if $first_card; + put_in_hash($monitors->[0], probe()); + } + + foreach my $monitor (@$monitors) { + if (!is_valid($monitor)) { + put_in_hash($monitor, default_monitor($card_Driver)); + configure_automatic($monitor) or internal_error("good_default_monitor (" . good_default_monitor() . ") is unknown in MonitorsDB"); + } + } + $raw_X->set_monitors(@$monitors); + $monitors; +} + +sub choose { + my ($in, $raw_X, $monitor, $head_nb, $b_auto) = @_; + + my $ok = is_valid($monitor); + if ($b_auto && $ok) { + return $ok; + } + + my (@l_monitors, %h_monitors); + foreach (monitors_db()) { + my $s = "$_->{VendorName}|$_->{ModelName}"; + push @l_monitors, $s; + $h_monitors{$s} = $_; + } + $h_monitors{"Plug'n Play"} = {}; + + ask_monitor: + my $merge_name = sub { + my ($monitor) = @_; + $monitor->{ModelName} ? $monitor->{VendorName} . '|' . $monitor->{ModelName} : $monitor->{VendorName}; + }; + my $merged_name = do { + my $merged_name = $merge_name->($monitor); + if (!exists $h_monitors{$merged_name}) { + $merged_name = is_valid($monitor) ? 'Custom' : + $merge_name->(default_monitor($raw_X->get_Driver)); + } + $merged_name; + }; + + $in->ask_from_({ title => N("_: This is a display device\nMonitor"), + messages => $head_nb ? N("Choose a monitor for head #%d", $head_nb) : N("Choose a monitor"), + interactive_help_id => 'configureX_monitor' + }, + [ { val => \$merged_name, separator => '|', + list => ['Custom', "Plug'n Play", uniq(@l_monitors)], + format => sub { $_[0] eq 'Custom' ? N("Custom") : + $_[0] eq "Plug'n Play" ? N("Plug'n Play") . ($monitor->{VendorName} eq "Plug'n Play" ? " ($monitor->{ModelName})" : '') : + $_[0] =~ /^Generic\|(.*)/ ? N("Generic") . "|$1" : + N("Vendor") . "|$_[0]" }, + sort => !$in->isa('interactive::gtk') } ]) or return; + + if ($merged_name eq "Plug'n Play") { + local $::noauto = 0; #- hey, you asked for plug'n play, so i do probe! + delete @$monitor{'VendorName', 'ModelName', 'EISA_ID', 'HorizSync', 'VertRefresh'}; + if ($head_nb <= 1) { + if (my $probed_info = probe()) { + put_in_hash($monitor, $probed_info); + } else { + log::l("Plug'n Play probing failed, but Xorg may do better"); + $monitor->{VendorName} = "Plug'n Play"; + } + } else { + $monitor->{VendorName} = "Plug'n Play"; + } + } elsif ($merged_name eq 'Custom') { + $in->ask_from('', +N("The two critical parameters are the vertical refresh rate, which is the rate +at which the whole screen is refreshed, and most importantly the horizontal +sync rate, which is the rate at which scanlines are displayed. + +It is VERY IMPORTANT that you do not specify a monitor type with a sync range +that is beyond the capabilities of your monitor: you may damage your monitor. + If in doubt, choose a conservative setting."), + [ { val => \$monitor->{HorizSync}, list => \@HorizSync_ranges, label => N("Horizontal refresh rate"), not_edit => 0 }, + { val => \$monitor->{VertRefresh}, list => \@VertRefresh_ranges, label => N("Vertical refresh rate"), not_edit => 0 } ]) or goto &choose; + delete @$monitor{'VendorName', 'ModelName', 'EISA_ID'}; + } else { + put_in_hash($monitor, $h_monitors{$merged_name}); + } + $monitor->{manually_chosen} = 1; + 1; +} + +sub _configure_automatic_LCD { + my ($monitor) = @_; + + $monitor->{HorizSync} && $monitor->{VertRefresh} and return; + + $monitor->{preferred_resolution} + && Xconfig::xfree::resolution2ratio($monitor->{preferred_resolution}) eq '16/10' or return; + + log::l("no HorizSync nor VertRefresh, using preferred resolution (hopefully this is a flat panel)"); + add2hash($monitor, generic_flat_panel($monitor->{preferred_resolution})); + 1; +} + +sub configure_automatic { + my ($monitor) = @_; + + if ($monitor->{EISA_ID}) { + log::l("EISA_ID: $monitor->{EISA_ID}"); + if (my $mon = find { lc($_->{EISA_ID}) eq $monitor->{EISA_ID} } monitors_db()) { + add2hash($monitor, $mon); + log::l("EISA_ID corresponds to: $monitor->{ModelName}"); + } elsif (!$monitor->{HorizSync} || !$monitor->{VertRefresh}) { + log::l("unknown EISA_ID and partial DDC probe, so unknown monitor"); + delete @$monitor{'VendorName', 'ModelName', 'EISA_ID'}; + } + } elsif ($monitor->{VendorName}) { + if (my $mon = find { $_->{VendorName} eq $monitor->{VendorName} && $_->{ModelName} eq $monitor->{ModelName} } monitors_db()) { + put_in_hash($monitor, $mon); + } + } + + _configure_automatic_LCD($monitor); + + is_valid($monitor); +} + +sub is_valid { + my ($monitor) = @_; + $monitor->{HorizSync} && $monitor->{VertRefresh} || $monitor->{VendorName} eq "Plug'n Play"; +} + +sub probe { + probe_DDC() || probe_DMI(); +} + +#- some EDID are much too strict: +#- the HorizSync range is too small to allow smaller resolutions +sub adjust_HorizSync_from_edid { + my ($monitor) = @_; + + my ($hmin, $hmax) = $monitor->{HorizSync} =~ /(\d+)-(\d+)/ or return; + if ($hmin > 45) { + log::l("replacing HorizSync $hmin-$hmax with 28.8-$hmax (allow 800x480)"); + $monitor->{HorizSync} = "28.8-$hmax"; + } +} +#- the VertRefresh range is too weird +sub adjust_VertRefresh_from_edid { + my ($monitor) = @_; + + my ($vmin, $vmax) = $monitor->{VertRefresh} =~ /(\d+)-(\d+)/ or return; + if ($vmin > 60) { + log::l("replacing VertRefresh $vmin-$vmax with 60-$vmax"); + $monitor->{VertRefresh} = "60-$vmax"; + } +} + +sub probe_DDC() { + my ($edid, $vbe) = any::monitor_full_edid() or return; + my $monitor = eval($edid); + + if ($vbe =~ /Memory: (\d+)k/) { + $monitor->{VideoRam_probed} = $1; + } + use_EDID($monitor); +} + +sub use_EDID { + my ($monitor) = @_; + + adjust_HorizSync_from_edid($monitor); + adjust_VertRefresh_from_edid($monitor); + + $monitor->{ModeLine} = Xconfig::xfree::default_ModeLine(); + my $detailed_timings = $monitor->{detailed_timings} || []; + my @different_timings = uniq_ { $_->{horizontal_active} . 'x' . $_->{vertical_active} } @$detailed_timings; + foreach (grep { !$_->{bad_ratio} } @$detailed_timings) { + if (Xconfig::xfree::xorg_builtin_resolution($_->{horizontal_active}, $_->{vertical_active})) { + #- we don't want the 4/3 modelines otherwise they conflict with the Xorg builtin vesamodes + } else { + unshift @{$monitor->{ModeLine}}, + { val => $_->{ModeLine}, pre_comment => $_->{ModeLine_comment} . "\n" }; + } + + if (@different_timings == 1 && $_->{horizontal_active} >= 1024) { + #- we don't use detailed_timing when it is 640x480 or 800x600, + #- since 14" CRTs often give this even when they handle 1024x768 correctly (and desktop is no good in poor resolutions) + + #- should we care about {has_preferred_timing} ? + $monitor->{preferred_resolution} = { X => $_->{horizontal_active}, Y => $_->{vertical_active} }; + } + } + + if ($monitor->{EISA_ID}) { + $monitor->{VendorName} = "Plug'n Play"; + $monitor->{ModelName} = $monitor->{monitor_name}; + $monitor->{ModelName} =~ s/"/''/g; + $monitor->{ModelName} =~ s/[\0-\x20]/ /g; + } + configure_automatic($monitor) or return; + $monitor; +} + +sub probe_DMI() { + my $res = detect_devices::probe_unique_name('Resolution'); + $res && generic_flat_panel_txt($res); +} + +sub generic_flat_panel { + my ($resolution) = @_; + generic_flat_panel_($resolution->{X}, $resolution->{Y}); +} +sub generic_flat_panel_txt { + my ($resolution) = @_; + my ($X, $Y) = $resolution =~ /(\d+)x(\d+)/ or log::l("bad resolution $resolution"), return; + generic_flat_panel_($X, $Y); +} +sub generic_flat_panel_ { + my ($X, $Y) = @_; + { + VendorName => 'Generic', + ModelName => "Flat Panel ${X}x${Y}", + HorizSync => '28.8-' . ($X > 1920 ? '100' : '90'), VertRefresh => '60', + preferred_resolution => { X => $X, Y => $Y }, + }; +} + +my $monitors_db; +sub monitors_db() { + $monitors_db ||= readMonitorsDB("$ENV{SHARE_PATH}/ldetect-lst/MonitorsDB"); + @$monitors_db; +} +sub readMonitorsDB { + my ($file) = @_; + + my @monitors_db; + my $F = openFileMaybeCompressed($file); + local $_; + my $lineno = 0; while (<$F>) { + $lineno++; + s/\s+$//; + /^#/ and next; + /^$/ and next; + + my @fields = qw(VendorName ModelName EISA_ID HorizSync VertRefresh dpms); + my %l; @l{@fields} = split /\s*;\s*/; + push @monitors_db, \%l; + } + \@monitors_db; +} + + +1; + diff --git a/lib/Xconfig/parse.pm b/lib/Xconfig/parse.pm new file mode 100644 index 0000000..ae1f87a --- /dev/null +++ b/lib/Xconfig/parse.pm @@ -0,0 +1,211 @@ +package Xconfig::parse; # $Id: parse.pm 247269 2008-10-01 13:57:30Z pixel $ + +use diagnostics; +use strict; + +use common; + + +sub read_XF86Config { + my ($file) = @_; + my $raw = _rraw_from_file($file); + _from_rraw(@$raw); + $raw; +} + +sub prepare_write_XF86Config { + my ($raw) = @_; + map { _raw_to_string(_before_to_string({ %$_ }, 0)) } @$raw; +} + +sub write_XF86Config { + my ($raw, $file) = @_; + my @blocks = prepare_write_XF86Config($raw); + @blocks ? output($file, @blocks) : unlink $file; +} + +sub read_XF86Config_from_string { + my ($s) = @_; + my $raw = _rraw_from_file('-', [ split "\n", $s ]); + _from_rraw(@$raw); + $raw; +} + +#-############################################################################### +#- raw reading/saving +#-############################################################################### +sub _rraw_from_file { + my ($file, $o_lines) = @_; + my $rraw = []; + + my $lines = $o_lines || [ cat_($file) ]; + my $line; + + my ($comment, $obj, @objs); + + my $attach_comment = sub { + $obj || @objs or warn "$file:$line: can not attach comment\n"; + if ($comment) { + $comment =~ s/\n+$/\n/; + ($obj || $objs[0])->{$_[0] . '_comment'} = $comment; + $comment = ''; + } + }; + + foreach (@$lines) { + $line++; + s/^\s*//; s/\s*$//; + + if (/^$/) { + $comment .= "\n" if $comment; + next; + } elsif (@objs ? m/^#\W/ || /^#$/ : /^#/) { + s/^#\s+/# /; + $comment .= "$_\n"; + next; + } + + if (/^Section\s+"(.*)"/i) { + die "$file:$line: missing EndSection\n" if @objs; + my $e = { name => $1, l => [], kind => 'Section' }; + push @$rraw, $e; + unshift @objs, $e; $obj = ''; + $attach_comment->('pre'); + } elsif (/^Subsection\s+"(.*)"/i) { + die "$file:$line: missing EndSubsection\n" if @objs && $objs[0]{kind} eq 'Subsection'; + die "$file:$line: not in Section\n" if !@objs || $objs[0]{kind} ne 'Section'; + my $e = { name => $1, l => [], kind => 'Subsection' }; + push @{$objs[0]{l}}, $e; + unshift @objs, $e; $obj = ''; + $attach_comment->('pre'); + } elsif (/^EndSection/i) { + die "$file:$line: not in Section\n" if !@objs || $objs[0]{kind} ne 'Section'; + $attach_comment->('post'); + shift @objs; $obj = ''; + } elsif (/^EndSubsection/i) { + die "$file:$line: not in Subsection\n" if !@objs || $objs[0]{kind} ne 'Subsection'; + $attach_comment->('post'); + shift @objs; $obj = ''; + } else { + die "$file:$line: not in Section\n" if !@objs; + + my $commented = s/^#//; + + my $comment_on_line; + s/(\s*#.*)/$comment_on_line = $1; ''/e; + + if (/^$/) { + die "$file:$line: weird"; + } + + (my $name, my $Option, $_) = + /^Option\s*"(.*?)"(.*)/ ? ($1, 1, $2) : /^(\S+)(.*)/ ? ($1, 0, $2) : internal_error($_); + my ($val) = /(\S.*)/; + + my %e = (Option => $Option, commented => $commented, comment_on_line => $comment_on_line, pre_comment => $comment); + $comment = ''; + $obj = { name => $name, val => $val }; + $e{$_} and $obj->{$_} = $e{$_} foreach keys %e; + + push @{$objs[0]{l}}, $obj; + } + } + $rraw; +} + +sub _simple_val_to_string { + my ($name, $e) = @_; + my $key = $e->{Option} ? qq(Option "$name") : $name; + my $val = defined $e->{val} ? ($e->{Option} && $e->{val} !~ /^"/ ? qq( "$e->{val}") : qq( $e->{val})) : ''; + ($e->{commented} ? '#' : '') . $key . $val; +} + +sub _raw_to_string { + my ($e, $b_want_spacing) = @_; + my $s = do { + if ($e->{l}) { + my $inside = join('', map_index { _raw_to_string($_, $::i) } @{$e->{l}}); + $inside .= $e->{post_comment} || ''; + $inside =~ s/^/ /mg; + qq(\n$e->{kind} "$e->{name}"\n) . $inside . "End$e->{kind}"; + } else { + _simple_val_to_string($e->{name}, $e); + } + }; + ($e->{pre_comment} ? ($b_want_spacing ? "\n" : '') . $e->{pre_comment} : '') . $s . ($e->{comment_on_line} || '') . "\n" . (!$e->{l} && $e->{post_comment} || ''); +} + +#-############################################################################### +#- refine the data structure for easier use +#-############################################################################### +my %kind_names = ( + Pointer => [ qw(Protocol Device Emulate3Buttons Emulate3Timeout EmulateWheel EmulateWheelButton) ], + Mouse => [ qw(DeviceName Protocol Device AlwaysCore Emulate3Buttons Emulate3Timeout EmulateWheel EmulateWheelButton) ], # Subsection in XInput + Keyboard => [ qw(Protocol Driver XkbModel XkbLayout XkbDisable) ], + Monitor => [ qw(Identifier VendorName ModelName HorizSync VertRefresh PreferredMode) ], + Device => [ qw(Identifier VendorName BoardName Chipset Driver VideoRam Screen BusID DPMS power_saver AccelMethod MonitorLayout TwinViewOrientation BIOSHotkeys RenderAccel SWCursor XaaNoOffscreenPixmaps) ], + Display => [ qw(Depth Modes Virtual) ], # Subsection in Device + Screen => [ qw(Identifier Driver Device Monitor DefaultDepth DefaultColorDepth) ], + Extensions => [ qw(Composite) ], + InputDevice => [ qw(Identifier Driver Protocol Device Type Mode XkbModel XkbLayout XkbDisable Emulate3Buttons Emulate3Timeout EmulateWheel EmulateWheelButton) ], + WacomCursor => [ qw(Port) ], #-\ + WacomStylus => [ qw(Port) ], #--> Port must be first + WacomEraser => [ qw(Port) ], #-/ + ServerLayout => [ qw(Identifier) ], +); +my @want_string = qw(Identifier DeviceName VendorName ModelName BoardName Driver Device Chipset Monitor Protocol XkbModel XkbLayout XkbOptions XkbCompat Load Disable ModulePath BusID PreferredMode); + +%kind_names = map_each { lc $::a => [ map { lc } @$::b ] } %kind_names; +@want_string = map { lc } @want_string; + +sub _from_rraw { + sub _from_rraw__rec { + my ($current, $e) = @_; + if ($e->{l}) { + _from_rraw($e); + push @{$current->{l}{$e->{name}}}, $e; + } else { + if (member(lc $e->{name}, @want_string) || $e->{Option} && $e->{val}) { + $e->{val} =~ s/^"(.*)"$/$1/ or warn "$e->{name} $e->{val} has no quote\n"; + } + + if (member(lc $e->{name}, @{$kind_names{lc $current->{name}} || []})) { + if ($current->{l}{$e->{name}} && !$current->{l}{$e->{name}}{commented}) { + warn "skipping conflicting line for $e->{name} in $current->{name}\n" if !$e->{commented}; + } else { + $current->{l}{$e->{name}} = $e; + } + } else { + push @{$current->{l}{$e->{name}}}, $e; + } + } + delete $e->{name}; + } + + foreach my $e (@_) { + ($e->{l}, my $l) = ({}, $e->{l}); + _from_rraw__rec($e, $_) foreach @$l; + + delete $e->{kind}; + } +} + +sub _before_to_string { + my ($e, $depth) = @_; + + if ($e->{l}) { + $e->{kind} = $depth ? 'Subsection' : 'Section'; + + my %rated = map_index { $_ => $::i + 1 } @{$kind_names{lc $e->{name}} || []}; + my @sorted = sort { ($rated{lc $a} || 99) <=> ($rated{lc $b} || 99) } keys %{$e->{l}}; + $e->{l} = [ map { + my $name = $_; + map { + _before_to_string({ name => $name, %$_ }, $depth+1); + } deref_array($e->{l}{$name}); + } @sorted ]; + } elsif (member(lc $e->{name}, @want_string)) { + $e->{val} = qq("$e->{val}"); + } + $e; +} diff --git a/lib/Xconfig/plugins.pm b/lib/Xconfig/plugins.pm new file mode 100644 index 0000000..620c2fe --- /dev/null +++ b/lib/Xconfig/plugins.pm @@ -0,0 +1,232 @@ +package Xconfig::plugins; # $Id: plugins.pm 110085 2007-01-18 08:45:14Z pixel $ + +use diagnostics; +use strict; + +use Xconfig::parse; +use Xconfig::xfree; +use common; + +my $dir = '/usr/share/libDrakX/x11-plugins'; + +sub list() { + glob_("$dir/*.pl"); +} + +sub _load { + my ($plugin_pl_file) = @_; + my $plugin = eval cat_($plugin_pl_file); + $@ and die "bad $plugin_pl_file. error: $@\n"; + + #- ensure only one line + $plugin->{summary} =~ s/\n/ /g; + + eval { $plugin->{raw} = Xconfig::parse::read_XF86Config_from_string($plugin->{conf}) }; + $@ and die "bad $plugin_pl_file conf. error: $@\n"; + + $plugin; +} + +my $mark = '# Using plugin'; +sub parse_active_plugin { + my ($raw_X) = @_; + + $raw_X->{plugins} and internal_error("parse_active_plugin must be done before doing anything with plugins"); + + my $first = $raw_X->{raw}[0]; + if (my @l = $first->{pre_comment} =~ /^\Q$mark\E (.*)/gm) { + $raw_X->{plugins} = [ map { { active => 1, summary => $_ } } @l ]; + } +} +sub _mark_active_in_header { + my ($raw_X, $summary) = @_; + my $first = $raw_X->{raw}[0]; + $first->{pre_comment} =~ s/\n/\n$mark $summary\n/; +} +sub _remove_active_in_header { + my ($raw_X, $summary) = @_; + my $first = $raw_X->{raw}[0]; + $first->{pre_comment} =~ s/\Q$mark $summary\E\n//; +} + +sub load { + my ($raw_X, $plugin_pl_file) = @_; + + my $plugin = eval { _load($plugin_pl_file) }; + $@ and log::l("bad plugin $plugin_pl_file: $@"), return; + + if (my $existing = find { $_->{summary} eq $plugin->{summary} } @{$raw_X->{plugins}}) { + put_in_hash($existing, $plugin); + $existing->{updated} = 1; + } else { + push @{$raw_X->{plugins}}, $plugin; + } +} + +sub val { &Xconfig::xfree::val } + +sub apply_plugin { + my ($raw_X, $plugin) = @_; + + if ($plugin->{active}) { + $plugin->{updated} or return; + + #- removing before re-applying again + remove_plugin($raw_X, $plugin); + } + + log::l("applying plugin $plugin->{summary}"); + + foreach my $e (@{$plugin->{raw}}) { + _mark_lines_with_name($plugin->{summary}, $e); + + if (my @sections = _select_sections_to_modify($raw_X, $e)) { + #- modifying existing sections + #- if there is more than one, modify all of them! + _merge_in_section($_, $e->{l}) foreach @sections; + } else { + #- adding the section + $raw_X->add_Section($e->{name}, $e->{l}); + } + } + + _mark_active_in_header($raw_X, $plugin->{summary}); + $plugin->{active} = 1; +} + +sub _select_sections_to_modify { + my ($raw_X, $e) = @_; + + my @sections = $raw_X->get_Sections($e->{name}) or return; + + if ($e->{l}{Identifier}) { + if (my @l = grep { val($_->{Identifier}) eq $e->{l}{Identifier}{val} } @sections) { + #- only modifying the section(s) matching the Driver (useful for InputDevice) + delete $e->{l}{Identifier}; #- do not tag-with-comment this line used only to select the section + @l; + } else { + #- if no matching section, we will create it + (); + } + } elsif ($e->{l}{Driver}) { + if (my @l = grep { val($_->{Driver}) eq $e->{l}{Driver}{val} } @sections) { + #- only modifying the section(s) matching the Driver (useful for InputDevice) + delete $e->{l}{Driver}; #- do not tag-with-comment this line used only to select the section + @l; + } else { + #- hum, modifying existing sections, is that good? :-/ + @sections; + } + } else { + #- modifying existing sections + @sections; + } +} + +sub _merge_in_section { + my ($h, $h_to_add) = @_; + + foreach my $name (keys %$h_to_add) { + if (exists $h->{$name}) { + my $pre_comment = join('', map { "#HIDDEN $_->{val}\n" } deref_array($h->{$name})); + my ($first, @other) = deref_array($h_to_add->{$name}); + $first = { pre_comment => $pre_comment, %$first }; + + $h->{$name} = ref($h->{$name}) eq 'ARRAY' ? [ $first, @other ] : $first; + } else { + $h->{$name} = $h_to_add->{$name}; + } + } +} + +sub _mark_lines_with_name { + my ($summary, $e) = @_; + if ($e->{l}) { + _mark_lines_with_name($summary, $_) foreach map { deref_array($_) } values %{$e->{l}}; + } else { + $e->{comment_on_line} = " # $summary"; + } +} + +sub remove_plugin { + my ($raw_X, $plugin) = @_; + + $plugin->{active} or return; + + log::l("removing plugin $plugin->{summary}"); + + @{$raw_X->{raw}} = map { + _remove_plugin($plugin->{summary}, $_); + } @{$raw_X->{raw}}; + + _remove_active_in_header($raw_X, $plugin->{summary}); + $plugin->{active} = 0; +} + +sub _remove_plugin { + my ($summary, $e) = @_; + if ($e->{l}) { + my $removed; + foreach my $k (keys %{$e->{l}}) { + my $v = $e->{l}{$k}; + my @v = map { _remove_plugin($summary, $_) } deref_array($v); + if (@v) { + if (ref($v) eq 'ARRAY') { + @$v = @v; + } else { + $e->{l}{$k} = $v[0]; + } + } else { + $removed = 1; + delete $e->{l}{$k}; + } + } + if_(!$removed || %{$e->{l}}, $e); + } elsif ($e->{comment_on_line} eq " # $summary") { + if (my @hidden = $e->{pre_comment} =~ /^#HIDDEN (.*)/gm) { + delete $e->{comment_on_line}; + delete $e->{pre_comment}; + map { { %$e, val => $_ } } @hidden; + } else { + (); + } + } else { + $e; + } +} + +sub apply_or_remove_plugin { + my ($raw_X, $plugin, $apply) = @_; + + if ($apply) { + apply_plugin($raw_X, $plugin); + } else { + remove_plugin($raw_X, $plugin); + } +} + +sub choose { + my ($in, $raw_X) = @_; + + parse_active_plugin($raw_X) if !$raw_X->{plugins}; + + load($raw_X, $_) foreach list(); + + my $plugins = $raw_X->{plugins}; + $_->{want_active} = $_->{active} foreach @$plugins; + + $in->ask_from_({}, + [ { title => 1, label => N("Choose plugins") }, + map { + { type => 'bool', val => \$_->{want_active}, text => $_->{summary} }, + { val => formatAlaTeX($_->{description}) }; + } @$plugins ]) or return; + + foreach (@$plugins) { + apply_or_remove_plugin($raw_X, $_, $_->{want_active}) + if $_->{want_active} != $_->{active}; + } + + 1; +} +1; diff --git a/lib/Xconfig/proprietary.pm b/lib/Xconfig/proprietary.pm new file mode 100644 index 0000000..86b8a4d --- /dev/null +++ b/lib/Xconfig/proprietary.pm @@ -0,0 +1,118 @@ +package Xconfig::proprietary; # $Id: proprietary.pm 269741 2010-06-02 09:25:33Z cfergeau $ + +use diagnostics; +use strict; + +use common; +use Xconfig::card; + +my $lib = arch() =~ /x86_64/ ? "lib64" : "lib"; + +sub install_matrox_hal { + my ($prefix) = @_; + my $tmpdir = "$prefix/root/tmp"; + + my $tar = "mgadrivers-2.0.tgz"; + my $dir_in_tar = "mgadrivers"; + my $dest_dir = "$prefix/usr/lib/xorg/modules/drivers"; + + #- already installed + return if -e "$dest_dir/mga_hal_drv.o" || $::testing; + + system("wget -O $tmpdir/$tar ftp://ftp.matrox.com/pub/mga/archive/linux/2002/$tar") if !-e "$tmpdir/$tar"; + system("tar xzC $tmpdir -f $tmpdir/$tar"); + + my $src_dir = "$tmpdir/$dir_in_tar/xfree86/4.2.1/drivers"; + foreach (all($src_dir)) { + my $src = "$src_dir/$_"; + my $dest = "$dest_dir/$_"; + rename $dest, "$dest.non_hal"; + cp_af($src, $dest_dir); + } + rm_rf("$tmpdir/$tar"); + rm_rf("$tmpdir/$dir_in_tar"); +} + +sub handle_DRIVER2_NO_SSE { + my ($card) = @_; + + $card->{DRIVER2_NO_SSE} or return; + + require detect_devices; + if (!detect_devices::has_cpu_flag('sse')) { + log::l("$card->{Driver2} need a processor featuring SSE, switching back to $card->{DRIVER2_NO_SSE}"); + $card->{Driver2} = $card->{DRIVER2_NO_SSE}; + } +} + + +sub pkgs_for_Driver2 { + my ($Driver2, $do_pkgs) = @_; + + my ($pkg, $base_name) = ($Driver2 =~ /^fglrx/ || $Driver2 =~ /^nvidia/) ? + ("x11-driver-video-$Driver2", $Driver2) : () or return; + + $do_pkgs->is_installed($pkg) || $do_pkgs->is_available($pkg) or + log::l("proprietary package $pkg not available"), return; + + my $module_pkgs = $do_pkgs->check_kernel_module_packages($base_name) or + log::l("$pkg available, but no kernel module package (for installed kernels, and no dkms)"), return; + + ($pkg, @$module_pkgs); +} + +sub may_use_Driver2 { + my ($card) = @_; + + my $modules_dir = Xconfig::card::modules_dir(); + #- make sure everything is correct at this point, packages have really been installed + #- and driver and GLX extension is present. + + my $check_drv = sub { + my ($drv, $o_subdir) = @_; + my @l = (if_($o_subdir, "$modules_dir/drivers/$o_subdir/$drv.so", + "/usr/$lib/$o_subdir/xorg/$drv.so"), + "$modules_dir/drivers/$drv.so", + "$modules_dir/drivers/$drv.o"); + my $has = find { -e "$::prefix$_" } @l; + $has or log::l("proprietary $drv driver missing (we searched for: @l)"); + $has; + }; + + my $card2 = { + %$card, + $card->{Driver2} =~ /^nvidia(.*)/ ? + (Driver => 'nvidia', DriverVersion => $1) : + $card->{Driver2} =~ /^fglrx(.*)/ ? + (Driver => 'fglrx', DriverVersion => $1) : + (Driver => $card->{Driver2}), + }; + + if ($card2->{Driver} eq 'nvidia') { + $check_drv->('nvidia_drv', "nvidia$card2->{DriverVersion}") or return; + + my $libglx_path = "/usr/$lib/nvidia$card2->{DriverVersion}/xorg"; + -e "$::prefix$libglx_path/libglx.so" or log::l("special NVIDIA libglx missing"), return; + + log::explanations("Using specific NVIDIA driver and GLX extensions"); + $card2->{DRI_GLX_SPECIAL} = 1; + $card2->{Options}{IgnoreEDID} = 1 if ($card2->{DriverVersion} eq "71xx"); + $card2->{Options}{UseEDID} = 0 if ($card2->{DriverVersion} eq "96xx"); + $card2; + } elsif ($card2->{Driver} eq 'fglrx') { + $check_drv->('fglrx_drv', "fglrx$card2->{DriverVersion}") or return; + -e "$::prefix/usr/$lib/fglrx$card2->{DriverVersion}/dri/fglrx_dri.so" || + -e "$::prefix$modules_dir/dri/fglrx_dri.so" || + -e "$::prefix/usr/$lib/dri/fglrx_dri.so" or + log::l("proprietary fglrx_dri.so missing"), return; + + log::explanations("Using specific ATI fglrx and DRI drivers"); + $card2->{DRI_GLX} = 1; + $card2; + } else { + undef; + } +} + +1; + diff --git a/lib/Xconfig/resolution_and_depth.pm b/lib/Xconfig/resolution_and_depth.pm new file mode 100644 index 0000000..942e50d --- /dev/null +++ b/lib/Xconfig/resolution_and_depth.pm @@ -0,0 +1,477 @@ +package Xconfig::resolution_and_depth; # $Id: resolution_and_depth.pm 250289 2008-12-08 09:59:02Z pixel $ + +use diagnostics; +use strict; + +use common; + + +our %depth2text = ( + 8 => N_("256 colors (8 bits)"), + 15 => N_("32 thousand colors (15 bits)"), + 16 => N_("65 thousand colors (16 bits)"), + 24 => N_("16 million colors (24 bits)"), +); +our @depths_available = ikeys(%depth2text); + +my @bios_vga_modes = ( + { bios => 769, X => 640, Y => 480, Depth => 8 }, + { bios => 771, X => 800, Y => 600, Depth => 8 }, + { bios => 773, X => 1024, Y => 768, Depth => 8 }, + { bios => 775, X => 1280, Y => 1024, Depth => 8 }, + { bios => 777, X => 1600, Y => 1200, Depth => 8 }, + { bios => 784, X => 640, Y => 480, Depth => 15 }, + { bios => 787, X => 800, Y => 600, Depth => 15 }, + { bios => 790, X => 1024, Y => 768, Depth => 15 }, + { bios => 793, X => 1280, Y => 1024, Depth => 15 }, + { bios => 796, X => 1600, Y => 1200, Depth => 15 }, + { bios => 785, X => 640, Y => 480, Depth => 16 }, + { bios => 788, X => 800, Y => 600, Depth => 16 }, + { bios => 791, X => 1024, Y => 768, Depth => 16 }, + { bios => 794, X => 1280, Y => 1024, Depth => 16 }, + { bios => 797, X => 1600, Y => 1200, Depth => 16 }, +); + +sub from_bios { + my ($bios) = @_; + my $bios_int = $bios =~ /^0x(.*)/ ? hex($1) : $bios; + find { $_->{bios} == $bios_int } @bios_vga_modes; +} + +sub bios_vga_modes() { @bios_vga_modes } + +sub size2default_resolution { + my ($size) = @_; #- size in inch + + require detect_devices; + if (arch() =~ /ppc/) { + return "1024x768" if detect_devices::get_mac_model() =~ /^PowerBook|^iMac/; + } elsif (detect_devices::is_xbox()) { + return "640x480"; + } + + my %monitorSize2resolution = ( + 13 => "640x480", + 14 => "800x600", + 15 => "800x600", + 16 => "1024x768", + 17 => "1024x768", + 18 => "1024x768", + 19 => "1280x1024", + 20 => "1280x1024", + 21 => "1600x1200", + 22 => "1600x1200", + ); + $monitorSize2resolution{round($size)} || ($size < 13 ? "640x480" : "1600x1200"); +} + +sub XxY { &Xconfig::xfree::XxY } + +sub to_string { + my ($resolution) = @_; + $resolution or return ''; + + $resolution->{automatic} + ? N("Automatic") . ($resolution->{Depth} ? sprintf(" (%dbpp)", $resolution->{Depth}) : '') + : $resolution->{X} ? sprintf("%sx%s %dbpp", @$resolution{'X', 'Y', 'Depth'}) : 'frame-buffer'; +} + +sub allowed { + my ($card) = @_; + + my ($prefered_depth, @resolution_and_depth); + + if ($card->{Driver} eq 'fbdev') { + @resolution_and_depth = grep { $_->{Depth} == 16 } @bios_vga_modes; + } else { + my @depths; + if ($card->{Driver} eq 'fglrx' || $card->{Driver} eq 'savage') { + @depths = 24; + } elsif ($card->{BoardName} eq 'NVIDIA RIVA 128') { + @depths = qw(8 15 24); + } elsif ($card->{use_DRI_GLX}) { + $prefered_depth = 24; + @depths = (16, 24); + } else { + @depths = our @depths_available; + } + my @resolutions = @Xconfig::xfree::resolutions; + + push @resolution_and_depth, + map { + my $Depth = $_; + map { m/(\d+)x(\d+)/ && { X => $1, Y => $2, Depth => $Depth } } @resolutions; + } @depths; + } + $prefered_depth, @resolution_and_depth; +} + +# ($card->{VideoRam} || ($card->{server} eq 'FBDev' ? 2048 : 32768)) +sub filter_using_VideoRam { + my ($VideoRam, @resolutions) = @_; + my $mem = 1024 * $VideoRam; + grep { $_->{X} * $_->{Y} * $_->{Depth}/8 <= $mem } @resolutions; + +} +sub filter_using_HorizSync_VertRefresh { + my ($HorizSync, $VertRefresh, @resolutions) = @_; + my $max_hsync = 1000 * max(split(/[,-]/, $HorizSync)); + my ($min_vsync, $max_vsync) = (min(split(/[,-]/, $VertRefresh)), max(split(/[,-]/, $VertRefresh))); + + #- enforce at least 60Hz, if max_vsync > 100 (ie don't do it on LCDs which are ok with low vsync) + $min_vsync = max(60, $min_vsync) if $max_vsync > 100; + + #- computing with {Y} which is active sync instead of total sync, but that's ok + grep { $max_hsync / $_->{Y} > $min_vsync } @resolutions; +} + +sub choose { + my ($in, $default_resolution, @resolutions) = @_; + + my @Depths = uniq(reverse map { $_->{Depth} } @resolutions); + my $resolution = $default_resolution || {}; + $in->ask_from(N("Resolutions"), "", + [ { + val => \$resolution, type => 'list', sort => 0, + list => [ (sort { $a->{X} <=> $b->{X} } @resolutions), + (map { { automatic => 1, Depth => $_ } } @Depths) ], + format => \&to_string, + } ]) or return; + $resolution; +} + + +sub choices { + my ($_raw_X, $resolution_wanted, $card, $monitors) = @_; + $resolution_wanted ||= {}; + + my ($prefered_depth, @resolutions) = allowed($card); + + @resolutions = filter_using_HorizSync_VertRefresh($monitors->[0]{HorizSync}, $monitors->[0]{VertRefresh}, @resolutions) if $monitors->[0]{HorizSync}; + @resolutions = filter_using_VideoRam($card->{VideoRam}, @resolutions) if $card->{VideoRam}; + + #- sort it, so we can take the first one when we want the "best" + @resolutions = sort { $b->{X} <=> $a->{X} || $b->{Y} <=> $a->{Y} || $b->{Depth} <=> $a->{Depth} } @resolutions; + + $_->{ratio} ||= Xconfig::xfree::resolution2ratio($_) foreach @resolutions; + + if ($resolution_wanted->{automatic} || !$resolution_wanted->{X} && !$monitors->[0]{HorizSync}) { + return { automatic => 1, Depth => $resolution_wanted->{Depth} }, @resolutions; + } + + if ($resolution_wanted->{X} && !$resolution_wanted->{Y}) { + #- assuming ratio 4/3 + $resolution_wanted->{Y} = round($resolution_wanted->{X} * 3 / 4); + } elsif (!$resolution_wanted->{X}) { + if ($monitors->[0]{preferred_resolution}) { + put_in_hash($resolution_wanted, $monitors->[0]{preferred_resolution}); + } elsif ($monitors->[0]{ModelName} =~ /^Flat Panel (\d+)x(\d+)$/) { + put_in_hash($resolution_wanted, { X => $1, Y => $2 }); + } elsif ($monitors->[0]{diagonal_size}) { + my ($X, $Y) = split('x', size2default_resolution($monitors->[0]{diagonal_size} * 1.08)); + put_in_hash($resolution_wanted, { X => $X, Y => $Y }); + } else { + put_in_hash($resolution_wanted, { X => 1024, Y => 768 }); + } + } + my @matching = grep { $_->{X} eq $resolution_wanted->{X} && $_->{Y} eq $resolution_wanted->{Y} } @resolutions; + if (!@matching) { + #- hard choice :-( + #- first trying the greater resolution with same ratio + my $ratio = $resolution_wanted->{X} / $resolution_wanted->{Y}; + @matching = grep { abs($ratio - $_->{X} / $_->{Y}) < 0.01 } @resolutions; + } + if (!@matching) { + #- really hard choice :'-( + #- take the first available resolution <= the wanted resolution + @matching = grep { $_->{X} < $resolution_wanted->{X} } @resolutions; + } + if (!@matching) { + @matching = @resolutions; + } + + my $default_resolution; + foreach my $Depth ($resolution_wanted->{Depth}, $prefered_depth) { + $Depth and $default_resolution ||= find { $_->{Depth} eq $Depth } @matching; + } + $default_resolution ||= $matching[0]; + + $default_resolution, @resolutions; +} + +sub configure { + my ($in, $raw_X, $card, $monitors, $b_auto, $o_resolution) = @_; + + my ($default_resolution, @resolutions) = choices($raw_X, $o_resolution || $raw_X->get_resolution, $card, $monitors); + + my $resolution; + if ($b_auto) { + #- use $default_resolution + if ($card->{Driver} eq 'fglrx' && !$default_resolution->{automatic}) { + $resolution = first(find { $default_resolution->{Y} eq $_->{Y} && $_->{Depth} == 24 } + $default_resolution, @resolutions); + $resolution ||= first(find { $_->{Depth} == 24 } $resolution, @resolutions); + } + } elsif ($in->isa('interactive::gtk')) { + $resolution = choose_gtk($in, $card, $default_resolution, @resolutions) or return; + } else { + $resolution = choose($in, $default_resolution, @resolutions) or return; + } + set_resolution_($raw_X, $card, $monitors, $default_resolution, $resolution, @resolutions); +} + +sub configure_auto_install { + my ($raw_X, $card, $monitors, $old_X) = @_; + + my ($default_resolution, @resolutions); + if ($old_X->{resolution_wanted} eq 'automatic') { + $default_resolution = { automatic => 1 }; + } else { + my $resolution_wanted = $old_X->{resolution_wanted} && do { + my ($X, $Y) = split('x', $old_X->{resolution_wanted}); + { X => $X, Y => $Y, Depth => $old_X->{default_depth} }; + }; + + ($default_resolution, @resolutions) = choices($raw_X, $resolution_wanted, $card, $monitors); + $default_resolution or die "you selected an unusable depth"; + } + set_resolution($raw_X, $card, $monitors, $default_resolution, @resolutions); +} + +sub set_resolution { + my ($raw_X, $card, $monitors, $resolution, @other) = @_; + set_resolution_($raw_X, $card, $monitors, $resolution, $resolution, @other); +} +sub set_resolution_ { + my ($raw_X, $card, $monitors, $default_resolution, $resolution, @other) = @_; + + my $PreferredMode; + if (!$resolution->{automatic}) { + my $ratio = Xconfig::xfree::resolution2ratio($resolution, 'non-strict'); + @other = uniq_ { XxY($_) } @other; + @other = grep { $_->{X} < $resolution->{X} } @other; + @other = filter_on_ratio($ratio, @other); + + set_915resolution($resolution) if is_915resolution_configured(); + + if (XxY($default_resolution) ne XxY($resolution)) { + log::l("setting PreferredMode since wanted resolution (" . XxY($resolution) . ") differs from the default one (" . XxY($default_resolution) . ")"); + $PreferredMode = XxY($resolution); + } + } + if ($monitors->[0]{PreferredMode} ne $PreferredMode) { + if ($PreferredMode) { + $monitors->[0]{PreferredMode} = $PreferredMode; + } else { + delete $monitors->[0]{PreferredMode}; + } + $raw_X->set_monitors(@$monitors); + } + if ($card->{Driver} eq 'geode') { + $card->{Options}{PanelGeometry} = XxY($resolution); + Xconfig::card::to_raw_X($card, $raw_X); + } + + set_default_background($resolution); + my $resolutions = [ $resolution, @other ]; + $raw_X->set_resolutions($resolutions); + $resolutions; +} +sub set_default_background { + my ($resolution) = @_; + + $resolution->{X} && $resolution->{Y} or do { + $resolution = { X => 1024, Y => 768 }; + log::l("defaulting background resolution to $resolution->{X}x$resolution->{Y}"); + }; + + my $ratio = $resolution->{X} / $resolution->{Y}; + my $dir = "$::prefix/usr/share/mga/backgrounds"; + my %theme = getVarsFromSh("$::prefix/etc/sysconfig/bootsplash"); + + my @l = map { + if (my ($X, $Y, undef, $hour) = /^\Q$theme{THEME}\E-(\d+)x(\d+)(-(.*))?.jpg$/) { + { file => $_, X => $X, Y => $Y, hour => $hour }; + } else { () } + } all($dir); + + my ($best, $_other) = + sort { + $a->[2] <=> $b->[2] || $b->[3] <=> $a->[3] || $a->[4] <=> $b->[4]; + } map { + [ + $_->{X}, $_->{Y}, + int(abs($ratio - $_->{X} / $_->{Y}) * 100), #- we want the nearest ratio (precision .01) + $_->{X} >= $resolution->{X}, #- then we don't want a resolution smaller + abs($_->{X} - $resolution->{X}), #- the nearest resolution + ]; + } @l; + + my @wanted = grep { $best->[0] == $_->{X} && $best->[1] == $_->{Y} } @l; + + if (-e "$dir/$theme{THEME}.xml") { + symlinkf "$theme{THEME}.xml", "$dir/Mageia.xml"; + } + + foreach (@wanted) { + if ($_->{hour}) { + symlinkf $_->{file}, "$dir/$theme{THEME}-$_->{hour}.jpg"; + } else { + symlinkf $_->{file}, "$dir/default.jpg"; + } + } +} +sub is_915resolution_configured() { + my $f = "$::prefix/etc/sysconfig/915resolution"; + -e $f && { getVarsFromSh($f) }->{XRESO}; +} +sub set_915resolution { + my ($resolution) = @_; + + my $f = "$::prefix/etc/sysconfig/915resolution"; + setVarsInSh($f, { + MODE => 'best', + XRESO => $resolution->{X}, + YRESO => $resolution->{Y}, + }); + run_program::rooted($::prefix, 'service', '915resolution', 'start'); +} + +sub filter_on_ratio { + my ($ratio, @l) = @_; + grep { + !$ratio + || $_->{ratio} eq $ratio + || $ratio eq '4/3' && "$_->{X}x$_->{Y}" eq '1280x1024'; + } @l; +} + +sub choose_gtk { + my ($in, $card, $default_resolution, @resolutions) = @_; + + my $chosen_Depth = $default_resolution->{Depth}; + my $chosen_res = { X => $default_resolution->{X} || 1024, Y => $default_resolution->{Y} }; + my $chosen_ratio = Xconfig::xfree::resolution2ratio($chosen_res, 'non-strict') || '4/3'; + + my $filter_on_Depth = sub { + grep { $_->{Depth} == $chosen_Depth } @_; + }; + my $filter_on_res = sub { + grep { $_->{X} == $chosen_res->{X} && $_->{Y} == $chosen_res->{Y} } @_; + }; + my $automatic_resolution = { automatic => 1, text => N_("Automatic") }; + $chosen_res = + $default_resolution->{automatic} ? $automatic_resolution : + #- $chosen_res must be one of @resolutions, so that it has a correct {ratio} field + first($filter_on_res->(@resolutions)) || $resolutions[0]; + + require ugtk2; + mygtk2->import; + ugtk2->import(qw(:create :helpers :wrappers)); + my $W = ugtk2->new(N("Resolution"), modal => 1); + + my %monitor_images_x_res = do { + my @l = qw(640 800 1024 1152 1280 1400 1600 1920 2048); + my %h = map { $_ => ugtk2::_find_imgfile("monitor-$_.png") } @l; + + #- for the other, use the biggest smaller + foreach my $x_res (uniq map { $_->{X} } @resolutions) { + my $x_res_ = max(grep { $_ <= $x_res } @l); + $h{$x_res} ||= $h{$x_res_} || $h{640}; + } + %h; + }; + + my $res2text = sub { "$_[0]{X}x$_[0]{Y}" . ($chosen_ratio || $_[0]{ratio} =~ /other/ ? '' : " ($_[0]{ratio})") }; + my @matching_ratio; + my $proposed_resolutions = []; + my $set_proposed_resolutions = sub { + my ($suggested_res) = @_; + @matching_ratio = filter_on_ratio($chosen_ratio, @resolutions); + gtkval_modify(\$proposed_resolutions, [ + (reverse uniq_ { $res2text->($_) } @matching_ratio), + $automatic_resolution, + if_($chosen_ratio, { text => N_("Other") }), + ]); + if ($suggested_res->{automatic}) { + gtkval_modify(\$chosen_res, $automatic_resolution); + } elsif (!$filter_on_res->(@matching_ratio)) { + my $res = $suggested_res || find { $_->{X} == $chosen_res->{X} } @matching_ratio; + gtkval_modify(\$chosen_res, $res || $matching_ratio[0]); + } + }; + $set_proposed_resolutions->($chosen_res); + + my $depth_combo = gtknew('ComboBox', width => 220, + text_ref => \$chosen_Depth, + format => sub { translate($depth2text{$_[0]}) }, + list => [ uniq(reverse map { $_->{Depth} } @resolutions) ], + changed => sub { + my @matching_Depth = $filter_on_Depth->(@matching_ratio); + if (!$filter_on_res->(@matching_Depth) && !$chosen_res->{automatic}) { + gtkval_modify(\$chosen_res, $matching_Depth[0]); + } + }); + my $pix_colors = gtknew('Image', + file_ref => \$chosen_Depth, + format => sub { + $_[0] >= 24 ? "colors.png" : $_[0] >= 15 ? "colors16.png" : "colors8.png"; + }); + my $previous_res = $chosen_res; + my $res_combo = gtknew('ComboBox', + text_ref => \$chosen_res, + format => sub { $_[0]{text} ? translate($_[0]{text}) : &$res2text }, + list_ref => \$proposed_resolutions, + changed => sub { + if ($chosen_res->{text} eq 'Other') { + undef $chosen_ratio; + $set_proposed_resolutions->($previous_res); + } elsif (!$chosen_res->{automatic}) { + my @matching_res = $filter_on_res->(@matching_ratio); + if (!$filter_on_Depth->(@matching_res)) { + gtkval_modify(\$chosen_Depth, $matching_res[0]{Depth}); + } + } + $previous_res = $chosen_res; + }); + my $pixmap_mo = gtknew('Image', + file_ref => \$chosen_res, + format => sub { + my $X = $_[0]{X} || '1024'; + $monitor_images_x_res{$X} or internal_error("no image for resolution $X"); + }); + + my $help_sub = $in->interactive_help_sub_display_id('configureX_resolution'); + gtkadd($W->{window}, + gtkpack_($W->create_box_with_title(N("Choose the resolution and the color depth"), + if_($card->{BoardName}, "(" . N("Graphics card: %s", $card->{BoardName}) . ")"), + ), + 1, '', + 0, $pixmap_mo, + 0, gtknew('HBox', children => [ + 1, '', + 0, gtknew('Table', col_spacings => 5, row_spacings => 5, + children => [ + [ $res_combo, gtknew('Label', text => "") ], + [ $depth_combo, gtknew('Frame', shadow_type => 'etched_out', child => $pix_colors) ], + ]), + 1, '', + ]), + 1, '', + 0, gtkadd($W->create_okcancel(N("Ok"), N("Cancel"), '', if_($help_sub, [ N("Help"), $help_sub, 1 ]))), + )); + $W->{ok}->grab_focus; + + $W->main or return; + + if ($chosen_res->{automatic}) { + $chosen_res->{Depth} = $chosen_Depth; + $chosen_res; + } else { + find { $_->{X} == $chosen_res->{X} && + $_->{Y} == $chosen_res->{Y} && + $_->{Depth} == $chosen_Depth } @resolutions; + } +} + +1; diff --git a/lib/Xconfig/screen.pm b/lib/Xconfig/screen.pm new file mode 100644 index 0000000..ad835fa --- /dev/null +++ b/lib/Xconfig/screen.pm @@ -0,0 +1,29 @@ +package Xconfig::screen; # $Id: screen.pm 210131 2004-11-18 13:25:45Z prigaux $ + +use diagnostics; +use strict; + +use common; + + +sub configure { + my ($raw_X) = @_; + + my @devices = $raw_X->get_devices; + my @monitors = $raw_X->get_monitors; + + if (@monitors < @devices) { + $raw_X->set_monitors(@monitors, ({}) x (@devices - @monitors)); + @monitors = $raw_X->get_monitors; + } + + my @sections = mapn { + my ($device, $monitor) = @_; + { Device => $device->{Identifier}, Monitor => $monitor->{Identifier} }; + } \@devices, \@monitors; + + $raw_X->set_screens(@sections); + 1; +} + +1; diff --git a/lib/Xconfig/test.pm b/lib/Xconfig/test.pm new file mode 100644 index 0000000..f29a07f --- /dev/null +++ b/lib/Xconfig/test.pm @@ -0,0 +1,137 @@ +package Xconfig::test; # $Id: test.pm 252243 2009-01-29 17:57:45Z pixel $ + +use diagnostics; +use strict; + +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', '-noAutoAddDevices', '-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 ugtk2; #- help perl_checker + ugtk2->import(qw(:wrappers :helpers)); #- help perl_checker + use interactive::gtk; + use run_program; + use common; + + $::prefix = "%s"; + + lang::bindtextdomain(); + + $ENV{DISPLAY} = ":9"; + Gtk2->init; + + gtkset_background(200 * 257, 210 * 257, 210 * 257); + my $text = Gtk2::Label->new; + my $time = 12; + Glib::Timeout->add(1000, sub { + $text->set(sprintf(translate("%s"), $time)); + $time-- or Gtk2->main_quit; + 1; + }); + + eval { #- eval it so that missing pixmap will not break the test completely + my $root = gtkroot(); + my $gc = Gtk2::Gdk::GC->new($root); + my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file("$::prefix/usr/share/mdk/xfdrake/xfdrake-test-card.png"); + my ($w, $h) = ($pixbuf->get_width, $pixbuf->get_height); + my $pixmap = Gtk2::Gdk::Pixmap->new($root, $w, $h, $root->get_depth); + $pixbuf->render_to_drawable($pixmap, $gc, 0, 0, 0, 0, $w, $h, 'none', 0, 0); + $root->set_back_pixmap($pixmap, 0); + $root->clear; + }; + + 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; +} diff --git a/lib/Xconfig/various.pm b/lib/Xconfig/various.pm new file mode 100644 index 0000000..ab077f5 --- /dev/null +++ b/lib/Xconfig/various.pm @@ -0,0 +1,411 @@ +package Xconfig::various; # $Id: various.pm 269742 2010-06-02 09:25:35Z cfergeau $ + +use diagnostics; +use strict; + +use Xconfig::card; +use Xconfig::default; +use Xconfig::resolution_and_depth; +use common; + + +sub to_string { + my ($raw_X) = @_; + + $raw_X->is_fbdev ? 'frame-buffer' : Xconfig::resolution_and_depth::to_string($raw_X->get_resolution); +} + +sub info { + my ($raw_X, $card) = @_; + my $info; + my $keyboard = eval { $raw_X->get_keyboard } || {}; + my @monitors = eval { $raw_X->get_monitors }; + my $device = eval { $raw_X->get_device } || {}; + my $mouse = eval { first($raw_X->get_mice) } || {}; + + $info .= N("Disable Ctrl-Alt-Backspace: %s\n", configure_ServerFlag($raw_X, 'DontZap') eq 'False' ? N("no") : N("yes")); + $info .= N("3D hardware acceleration: %s\n", translate(bool2yesno($card->{use_DRI_GLX} || $card->{DRI_GLX_SPECIAL}))); + $info .= N("Keyboard layout: %s\n", $keyboard->{XkbLayout}); + $info .= N("Mouse type: %s\n", $mouse->{Protocol}); + foreach my $monitor (@monitors) { + $info .= N("Monitor: %s\n", $monitor->{ModelName}); + $info .= N("Monitor HorizSync: %s\n", $monitor->{HorizSync}); + $info .= N("Monitor VertRefresh: %s\n", $monitor->{VertRefresh}); + } + $info .= N("Graphics card: %s\n", $device->{VendorName} . ' ' . $device->{BoardName}); + $info .= N("Graphics memory: %s kB\n", $device->{VideoRam}) if $device->{VideoRam}; + if (my $resolution = eval { $raw_X->get_resolution }) { + $info .= N("Color depth: %s\n", translate($Xconfig::resolution_and_depth::depth2text{$resolution->{Depth}})) if $resolution->{Depth}; + $info .= N("Resolution: %s\n", Xconfig::resolution_and_depth::to_string($resolution)); + } + $info .= N("Xorg driver: %s\n", $device->{Driver}) if $device->{Driver}; + $info; +} + +sub default { + my ($card, $various) = @_; + + my $isLaptop = detect_devices::isLaptop(); + + add2hash_($various, { + isLaptop => $isLaptop, + xdm => 1, + DontZap => 0, + Composite => !($card->{Driver} eq 'nvidia' && $card->{DriverVersion} eq '71xx'), + if_($card->{Driver} eq 'nvidia', RenderAccel => !member($card->{DriverVersion}, qw(71xx 96xx)), + Clone => 0, ForceModeDVI => 0), + if_($card->{Driver} eq 'savage', HWCursor => 1), + if_($card->{Driver} eq 'intel' && $isLaptop, Clone => 0), + if_($card->{Driver} eq 'ati' && $isLaptop, Clone => 1, BIOSHotkeys => 0), + if_(exists $card->{DRI_GLX}, use_DRI_GLX => $card->{DRI_GLX} && !$card->{Xinerama}), + if_(member($card->{Driver}, qw(i128 ati sis trident via savage)), EXA => 0), #- list taken from http://wiki.x.org/wiki/ExaStatus + }); +} + +sub various { + my ($in, $raw_X, $card, $options, $b_auto, $b_read_existing) = @_; + + tvout($in, $card, $options) if !$b_auto; + + my $use_DRI_GLX = member('dri', $raw_X->get_modules); + + my $various = { + if_($::isStandalone, xdm => runlevel() == 5), + if_($b_read_existing, + Composite => $raw_X->get_extension('Composite') ne 'Disable', + if_($card->{Options}{MonitorLayout} eq 'NONE,CRT+LFP' || + $card->{Options}{TwinViewOrientation} eq 'Clone', + Clone => 1), + if_($card->{Options}{MonitorLayout} eq 'LVDS,NONE', + Clone => 0), + if_($card->{Options}{BIOSHotkeys}, + BIOSHotkeys => 1), + if_($card->{Options}{AccelMethod}, + EXA => $card->{Options}{AccelMethod} eq 'EXA'), + if_($card->{Options}{ModeValidation}, + ForceModeDVI => 1), + if_($card->{Driver} eq 'nvidia', + RenderAccel => !$card->{Options}{RenderAccel}, + ), + HWCursor => !$card->{Options}{SWCursor}, + DontZap => (configure_ServerFlag($raw_X, 'DontZap') eq 'False' ? 0 : 1), + if_($card->{DRI_GLX} || $use_DRI_GLX, use_DRI_GLX => $use_DRI_GLX), + ), + }; + default($card, $various); + + if (!$b_auto) { + choose($in, $various) or return; + } + + config($raw_X, $card, $various) && $various; +} + +sub various_auto_install { + my ($raw_X, $card, $old_X) = @_; + + my $various = { %$old_X }; + default($card, $various); + config($raw_X, $card, $various); + 1; +} + +sub config { + my ($raw_X, $card, $various) = @_; + + if (exists $various->{DontZap}) { + configure_ServerFlag($raw_X, 'DontZap', $various->{DontZap} eq 1 ? 'True' : 'False'); + } + if ($various->{Composite}) { + $raw_X->remove_extension('Composite'); + if ($card->{Driver} eq 'nvidia') { + $card->{Options}{AddARGBGLXVisuals} = undef; + } + } else { + $raw_X->set_extension('Composite', 'Disable'); + + if ($card->{Driver} eq 'nvidia') { + delete $card->{Options}{AddARGBGLXVisuals}; + } + } + if (exists $various->{use_DRI_GLX}) { + $card->{use_DRI_GLX} = $various->{use_DRI_GLX}; + } + + if (exists $various->{RenderAccel}) { + if ($various->{RenderAccel}) { + delete $card->{Options}{RenderAccel}; + } else { + $card->{Options}{RenderAccel} = 'false'; + } + } + + if (exists $various->{HWCursor}) { + if ($various->{HWCursor}) { + delete $card->{Options}{SWCursor}; + } else { + $card->{Options}{SWCursor} = undef; + } + } + + if (exists $various->{BIOSHotkeys}) { + if ($various->{BIOSHotkeys}) { + $card->{Options}{BIOSHotkeys} = undef; + } else { + delete $card->{Options}{BIOSHotkeys}; + } + } + + if (exists $various->{EXA}) { + if ($card->{Driver} eq 'intel') { + # the intel driver is able to automatically pick UXA/EXA + # when xorg.conf has no accel method defined, but XAA + # has to be explicitly selected, that's why the logic + # is reversed compared to the other drivers + if ($various->{EXA}) { + delete $card->{Options}{AccelMethod}; + } else { + $card->{Options}{AccelMethod} = 'XAA'; + } + } else { + if ($various->{EXA}) { + $card->{Options}{AccelMethod} = 'EXA'; + } else { + delete $card->{Options}{AccelMethod}; + } + } + } + + if (exists $various->{ForceModeDVI}) { + if ($card->{Driver} eq 'nvidia') { + if ($various->{ForceModeDVI}) { + $card->{Options}{ExactModeTimingsDVI} = undef; + $card->{Options}{ModeValidation} = 'NoWidthAlignmentCheck, NoDFPNativeResolutionCheck'; + } else { + delete $card->{Options}{ExactModeTimingsDVI}; + delete $card->{Options}{ModeValidation}; + } + } + } + + if (exists $various->{Clone}) { + if ($card->{Driver} eq 'nvidia') { + if ($various->{Clone}) { + $card->{Options}{TwinView} = undef; + $card->{Options}{TwinViewOrientation} = 'Clone'; + delete $card->{Options}{DynamicTwinView}; + } else { + delete $card->{Options}{TwinView}; + delete $card->{Options}{TwinViewOrientation}; + #- below disables runtime setting of TwinView via nvidia-settings + #- it helps on Compiz (#39171) + $card->{Options}{DynamicTwinView} = 'false'; + } + } elsif ($card->{Driver} eq 'intel') { + if ($various->{Clone}) { + $card->{Options}{MonitorLayout} = 'NONE,CRT+LFP'; + } else { + delete $card->{Options}{MonitorLayout}; + } + } elsif ($card->{Driver} eq 'ati') { + if ($various->{Clone}) { + #- the default is Clone + delete $card->{Options}{MonitorLayout}; + } else { + #- forcing no display on CRT + $card->{Options}{MonitorLayout} = 'LVDS,NONE'; + } + } + } + + Xconfig::various::runlevel($various->{xdm} ? 5 : 3); +} + +sub runlevel { + my ($o_runlevel) = @_; + my $f = "$::prefix/etc/inittab"; + -r $f or log::l("missing inittab!!!"), return; + if ($o_runlevel) { + substInFile { s/^id:\d:initdefault:\s*$/id:$o_runlevel:initdefault:\n/ } $f if !$::testing; + } else { + cat_($f) =~ /^id:(\d):initdefault:\s*$/m && $1; + } +} + +sub choose { + my ($in, $various) = @_; + + $in->ask_from_({ title => N("Xorg configuration") }, [ + { label => N("Global options"), title => 1 }, + { text => N("Disable Ctrl-Alt-Backspace"), + type => 'bool', val => \$various->{DontZap} }, + { label => N("Graphic card options"), title => 1 }, + { text => N("Enable Translucency (Composite extension)"), + type => 'bool', val => \$various->{Composite} }, + exists $various->{HWCursor} ? + { text => N("Use hardware accelerated mouse pointer"), + type => 'bool', val => \$various->{HWCursor} } : (), + exists $various->{RenderAccel} ? + { text => N("Enable RENDER Acceleration (this may cause bugs displaying text)"), + type => 'bool', val => \$various->{RenderAccel} } : (), + exists $various->{Clone} ? + { text => $various->{isLaptop} ? + N("Enable duplicate display on the external monitor") : + N("Enable duplicate display on the second display"), + type => 'bool', val => \$various->{Clone} } : (), + exists $various->{ForceModeDVI} ? + { text => N("Force display mode of DVI"), + type => 'bool', val => \$various->{ForceModeDVI} } : (), + exists $various->{BIOSHotkeys} ? + { text => N("Enable BIOS hotkey for external monitor switching"), + type => 'bool', val => \$various->{BIOSHotkeys} } : (), + exists $various->{EXA} ? + { text => N("Use EXA instead of XAA (better performance for Render and Composite)"), + type => 'bool', val => \$various->{EXA} } : (), + { label => N("Graphical interface at startup"), title => 1 }, + { text => N("Automatically start the graphical interface (Xorg) upon booting"), + type => 'bool', val => \$various->{xdm} }, + ]) or return; + + 1; +} + +sub tvout { + my ($in, $card, $options) = @_; + + $card->{FB_TVOUT} && $options->{allowFB} or return; + + $in->ask_yesorno('', N("Your graphic card seems to have a TV-OUT connector. +It can be configured to work using frame-buffer. + +For this you have to plug your graphic card to your TV before booting your computer. +Then choose the \"TVout\" entry in the bootloader + +Do you have this feature?")) or return; + + #- rough default value (rationale: http://download.nvidia.com/XFree86_40/1.0-2960/README.txt) + require timezone; + my $norm = timezone::read()->{timezone} =~ /America/ ? 'NTSC' : 'PAL'; + + $norm = $in->ask_from_list('', N("What norm is your TV using?"), [ 'NTSC', 'PAL' ], $norm) or return; + + configure_FB_TVOUT($in->do_pkgs, { norm => $norm }); +} + +sub configure_FB_TVOUT { + my ($do_pkgs, $use_FB_TVOUT) = @_; + + my $raw_X = Xconfig::default::configure($do_pkgs); + return if is_empty_array_ref($raw_X); + + $raw_X->set_monitors({ HorizSync => '30-50', VertRefresh => ($use_FB_TVOUT->{norm} eq 'NTSC' ? 60 : 50), + ModeLine => [ + { val => '"640x480" 29.50 640 675 678 944 480 530 535 625', pre_comment => "# PAL\n" }, + { val => '"800x600" 36.00 800 818 820 960 600 653 655 750' }, + { val => '"640x480" 28.195793 640 656 658 784 480 520 525 600', pre_comment => "# NTSC\n" }, + { val => '"800x600" 38.769241 800 812 814 880 600 646 649 735' }, + ] }); + $raw_X->set_devices({ Driver => 'fbdev' }); + + my ($device) = $raw_X->get_devices; + my ($monitor) = $raw_X->get_monitors; + $raw_X->set_screens({ Device => $device->{Identifier}, Monitor => $monitor->{Identifier} }); + + my $Screen = $raw_X->get_default_screen; + $Screen->{Display} = [ map { { l => { Depth => { val => $_ } } } } 8, 16 ]; + + $raw_X->write("$::prefix/etc/X11/xorg.conf.tvout"); + + check_xorg_conf_symlink(); + + { + require bootloader; + require fsedit; + require detect_devices; + my $all_hds = $::isInstall ? $::o->{all_hds} : fsedit::get_hds(); + my $bootloader = $::isInstall ? $::o->{bootloader} : bootloader::read($all_hds); + + if (my $tvout = bootloader::duplicate_kernel_entry($bootloader, 'TVout')) { + $tvout->{append} .= " XFree=tvout"; + bootloader::install($bootloader, $all_hds); + } + } +} + +sub configure_ServerFlag { + my ($raw_X, $option, $value) = @_; + my $ServerFlags = $raw_X->get_Section('ServerFlags'); + my $option_ref = $ServerFlags->{$option}->[0]; + if ($value) { + $option_ref->{val} = $value; + $option_ref->{commented} = 0; + $option_ref->{Option} = 1; + } + return undef if $option_ref->{commented} eq 1; + $option_ref->{val}; +} + +sub check_xorg_conf_symlink() { + my $f = "$::prefix/etc/X11/xorg.conf"; + if (!-l $f && -e "$f.tvout") { + rename $f, "$f.standard"; + symlink "xorg.conf.standard", $f; + } +} + +sub change_bootloader_config { + my ($do, @do_params) = @_; + + require bootloader; + my ($bootloader, $all_hds); + + if ($::isInstall && !$::globetrotter) { + ($bootloader, $all_hds) = ($::o->{bootloader}, $::o->{all_hds}); + $bootloader && $bootloader->{method} or return; + } else { + require fsedit; + require fs; + require bootloader; + $all_hds = fsedit::get_hds(); + fs::get_info_from_fstab($all_hds); + + $bootloader = bootloader::read($all_hds) or return; + } + + $do->($bootloader, @do_params) or return; + + bootloader::action($bootloader, 'write', $all_hds); + bootloader::action($bootloader, 'when_config_changed'); + 1; +} + +sub setupFB { + my ($bios_vga_mode) = @_; + + change_bootloader_config( + sub { + my ($bootloader, $bios_vga_mode) = @_; + foreach (@{$bootloader->{entries}}) { + $_->{vga} = $bios_vga_mode if $_->{vga}; #- replace existing vga= with + } + bootloader::update_splash($bootloader); + 1; + }, $bios_vga_mode); +} + +sub setup_kms { + change_bootloader_config( + sub { + my ($bootloader) = @_; + my $kms_ok = run_program::rooted($::prefix, "/sbin/display_driver_helper", "--is-kms-allowed") || 0; + return if $kms_ok != bootloader::get_append_simple($bootloader, "nokmsboot"); + if ($kms_ok) { + bootloader::remove_append_simple($bootloader, "nokmsboot"); + } else { + bootloader::set_append_simple($bootloader, "nokmsboot"); + } + 1; + }); +} + +1; diff --git a/lib/Xconfig/xfree.pm b/lib/Xconfig/xfree.pm new file mode 100644 index 0000000..6b284aa --- /dev/null +++ b/lib/Xconfig/xfree.pm @@ -0,0 +1,857 @@ +package Xconfig::xfree; # $Id: xfree.pm 262502 2009-10-21 13:46:52Z cfergeau $ + +my ($Revision) = '$Revision: 262502 $' =~ /(\d+)/; + +use diagnostics; +use strict; + +use common; +use Xconfig::parse; + +#- mostly internal only +sub new { + my ($class, $raw) = @_; + @$raw && bless { raw => $raw }, $class; +} + +sub _conf_file() { + "$::prefix/etc/X11/xorg.conf"; +} + +################################################################################ +# I/O ########################################################################## +################################################################################ +sub read { + my ($class) = @_; + my $raw_X = $class->new(Xconfig::parse::read_XF86Config(_conf_file())) or return; + $raw_X->{before} = $raw_X->prepare_write; + + if (my ($keyboard) = $raw_X->get_InputDevices('keyboard')) { + $keyboard->{Driver}{val} = 'kbd'; + } + if (my ($keyboard) = $raw_X->get_InputDevices('Keyboard')) { + $keyboard->{Driver}{val} = 'kbd'; + } + + #- ugly hack to fix empty ModeLine lines, XFdrake seems to generate some, but where??? + #- at least this allows fixing the pb by re-running XFdrake + foreach ($raw_X->get_Sections('Monitor')) { + my $l = $_->{ModeLine} or next; + @$l = grep { $_->{val} } @$l; + } + + foreach ($raw_X->get_Sections('Device')) { + $_->{Driver} && $_->{Driver}{val} eq 'i810' and $_->{Driver}{val} = 'intel'; + } + + $raw_X; +} +sub write { + my ($raw_X, $o_file) = @_; + my $file = $o_file || _conf_file(); + if (!$o_file) { + renamef($file, "$file.old"); + } + no_ModeLine_on_fglrx($raw_X); #- HACK + set_Revision($raw_X); + Xconfig::parse::write_XF86Config($raw_X->{raw}, $file); +} +sub prepare_write { + my ($raw_X) = @_; + set_Revision($raw_X); + join('', Xconfig::parse::prepare_write_XF86Config($raw_X->{raw})); +} +sub is_modified { + my ($raw_X) = @_; + $raw_X->{before} ne $raw_X->prepare_write; +} +sub is_only_resolution_modified { + my ($raw_X) = @_; + ($raw_X->{after_set_resolutions} || $raw_X->{before}) eq $raw_X->prepare_write; +} +sub empty_config { + my ($class) = @_; + $class->new(Xconfig::parse::read_XF86Config_from_string(our $default_header)); +} + +my $mark = '# File generated by XFdrake'; +sub get_Revision { + my ($raw_X) = @_; + my $first = $raw_X->{raw}[0]; + $first->{pre_comment} =~ /^\Q$mark\E \(rev (\d+)\)/ && $1; +} +sub set_Revision { + my ($raw_X) = @_; + my $first = $raw_X->{raw}[0]; + my $first_comment = $first->{pre_comment}; + + my $was_there = $first_comment =~ s/^\Q$mark\E.*\n//; + $first->{pre_comment} = "$mark (rev $Revision)\n" . ($was_there ? '' : "\n") . $first_comment; +} + +################################################################################ +# keyboard ##################################################################### +################################################################################ +my @keyboard_fields = qw(XkbLayout XkbModel XkbDisable XkbOptions XkbCompat); +sub get_keyboard { + my ($raw_X) = @_; + my $raw_kbd = _raw_get_keyboard($raw_X) or die "no keyboard section"; + raw_export_section($raw_kbd, \@keyboard_fields); +} +sub _raw_get_keyboard { + my ($raw_X) = @_; + first($raw_X->get_Sections('InputDevice', sub { + my ($entry) = @_; + my $Driver = val($entry->{Driver}); + $Driver eq 'kbd' || $Driver eq 'evdev' && val($entry->{XkbLayout}); + })); +} + +################################################################################ +# mouse ######################################################################## +################################################################################ +#- example mouse: { Protocol => 'IMPS/2', Device => '/dev/psaux', Emulate3Buttons => undef, Emulate3Timeout => 50, ZAxisMapping => [ '4 5', '6 7' ] } +#- example evdev: { bustype => '0x0003', vendor => '0x045e', product => '0x008c' } +my @mouse_fields = qw(Protocol Device ZAxisMapping Emulate3Buttons Emulate3Timeout bustype vendor product); #-); +sub get_mice { + my ($raw_X) = @_; + my @raw_mice = $raw_X->get_Sections('InputDevice', \&_is_mouse); + map { raw_export_section($_, \@mouse_fields) } @raw_mice; +} +sub set_mice { + my ($raw_X, @mice) = @_; + @mice = grep { $_->{Driver} && $_->{Driver} ne 'evdev' } @mice; + my @raw_mice = _new_mouse_sections($raw_X, map { delete $_->{Driver} } @mice); + mapn { + my ($raw_mouse, $mouse) = @_; + raw_import_section($raw_mouse, $mouse); + _set_Option('mouse', $raw_mouse, keys %$mouse); + } \@raw_mice, \@mice; +} +sub _is_mouse { + my ($entry) = @_; + my $Driver = val($entry->{Driver}); + member($Driver, qw(mouse vboxmouse vmmouse)) || $Driver eq 'evdev' && !val($entry->{XkbLayout}); +} +sub _new_mouse_sections { + my ($raw_X, @Drivers) = @_; + $raw_X->remove_InputDevices_when(\&_is_mouse); + + @Drivers or return; + + my @l = map_index { + my $h = { Identifier => { val => 'Mouse' . ($::i + 1) }, Driver => { val => $_ } }; + $raw_X->add_Section('InputDevice', $h); + } @Drivers; + + my $layout = get_ServerLayout($raw_X)->{InputDevice} ||= []; + push @$layout, { val => qq("Mouse1" "CorePointer") }; + push @$layout, { val => qq("Mouse$_" "SendCoreEvents") } foreach 2 .. @Drivers; + + @l; +} + + +################################################################################ +# resolution ################################################################### +################################################################################ +sub get_resolution { + my ($raw_X, $o_Screen) = @_; + first(get_resolutions($raw_X, $o_Screen)); +} +sub get_resolutions { + my ($raw_X, $o_Screen) = @_; + my $Screen = $o_Screen || $raw_X->get_default_screen or return {}; + + my $depth = val($Screen->{DefaultColorDepth} || $Screen->{DefaultDepth}); + my $Display = find { !$depth || val($_->{l}{Depth}) eq $depth } @{$Screen->{Display} || []} or return { automatic => 1, Depth => $depth }; + my $s = val($Display->{l}{Virtual} || $Display->{l}{Modes}); + my @l; + while ($s =~ /(\d+)(x|\s+)(\d+)/g) { + push @l, { X => $1, Y => $3, Depth => val($Display->{l}{Depth}) }; + } + @l ? @l : { automatic => 1, Depth => $depth }; +} +sub set_resolutions { + my ($raw_X, $resolutions, $o_Screen) = @_; + + my $Depth = $resolutions->[0]{Depth} eq '32' ? 24 : $resolutions->[0]{Depth}; + my $only_resolution = $raw_X->is_only_resolution_modified; + + foreach my $Screen ($o_Screen ? $o_Screen : $raw_X->get_Sections('Screen')) { + $Screen ||= $raw_X->get_default_screen or internal_error('no screen'); + + delete $Screen->{DefaultDepth}; + $only_resolution &&= $Screen->{DefaultColorDepth} && $Screen->{DefaultColorDepth}{val} eq $Depth; + if ($resolutions->[0]{X}) { + #- if the existing config is using Virtual, keep Virtual, otherwise default to Modes + my $Mode_name = (any { $_->{l}{Virtual} } @{$Screen->{Display} || []}) ? 'Virtual' : 'Modes'; + + my @l = $Mode_name eq 'Modes' ? @$resolutions : first(@$resolutions); + my @Modes = map { sprintf($Mode_name eq 'Modes' ? '"%dx%d"' : '%d %d', @$_{'X', 'Y'}) } @l; + + $Screen->{Display} = [ map { + { l => { Depth => { val => $_ }, $Mode_name => { val => join(' ', @Modes) } } }; + } 8, 15, 16, 24 ]; + } else { + delete $Screen->{Display}; + } + if ($Depth) { + $Screen->{DefaultColorDepth} = { val => $Depth }; + } else { + delete $Screen->{DefaultColorDepth}; + } + } + add_gtf_ModeLines($raw_X, $resolutions) if $resolutions->[0]{X}; + + $raw_X->{after_set_resolutions} = $only_resolution ? $raw_X->prepare_write : ''; +} + + +################################################################################ +# device ####################################################################### +################################################################################ +my @device_fields = qw(VendorName BoardName Driver VideoRam Screen BusID); #-); +sub get_device { + my ($raw_X) = @_; + first(get_devices($raw_X)); +} +sub get_devices { + my ($raw_X) = @_; + my @raw_devices = $raw_X->get_Sections('Device'); + map { + my $raw_device = $_; + my $device = raw_export_section($raw_device, [ 'Identifier', @device_fields ]); + $device->{Options} = raw_export_section($raw_device, [ grep { (deref_array($raw_device->{$_}))[0]->{Option} } keys %$raw_device ]); + $device; + } @raw_devices; +} +sub set_devices { + my ($raw_X, @devices) = @_; + my @raw_devices = _new_device_sections($raw_X, int @devices); + mapn { + my ($raw_device, $device) = @_; + my %Options = %{$device->{Options} || {}}; + raw_import_section($raw_device, $device, \@device_fields); + raw_import_section($raw_device, \%Options); + $_->{Option} = 1 foreach map { deref_array($raw_device->{$_}) } keys %Options; + $raw_device->{''} = [ { post_comment => $device->{raw_LINES} } ] if $device->{raw_LINES}; + } \@raw_devices, \@devices; +} +sub _new_device_sections { + my ($raw_X, $nb_new) = @_; + $raw_X->remove_Section('Device'); + map { $raw_X->add_Section('Device', { Identifier => { val => "device$_" }, DPMS => { Option => 1 } }) } (1 .. $nb_new); +} +sub get_Driver { + my ($raw_X) = @_; + my $card = eval { $raw_X->get_device }; + $card && $card->{Driver}; +} + +################################################################################ +# wacoms ####################################################################### +################################################################################ +sub set_wacoms { + my ($raw_X, @wacoms) = @_; + $raw_X->remove_InputDevices('wacom'); + + @wacoms or return; + + my @Modes = ('Stylus', 'Eraser', 'Cursor', 'Pad'); + + my $layout = get_ServerLayout($raw_X)->{InputDevice} ||= []; + each_index { + my $wacom = $_; + foreach (@Modes) { + next if $wacom->{USB}; + my $identifier = $_ . ($::i + 1); + my $h = { Identifier => { val => $identifier }, + Driver => { val => 'wacom' }, + Type => { val => lc $_, Option => 1 }, + Device => { val => $wacom->{Device}, Option => 1 } + }; + $raw_X->add_Section('InputDevice', $h); + push @$layout, { val => qq("$identifier" "SendCoreEvents") }; + } + } @wacoms; +} + +################################################################################ +# monitor ###################################################################### +################################################################################ +my @monitor_fields = qw(VendorName ModelName HorizSync VertRefresh PreferredMode); +sub get_monitors { + my ($raw_X) = @_; + my @raw_monitors = $raw_X->get_Sections('Monitor'); + map { + my $h = raw_export_section($_, [ 'Identifier', @monitor_fields ]); + $h->{ModeLine} = $_->{ModeLine} if $_->{ModeLine}; + $h; + } @raw_monitors; +} +sub set_monitors { + my ($raw_X, @monitors) = @_; + my @raw_monitors = _new_monitor_sections($raw_X, int @monitors); + mapn { + my ($raw_monitor, $monitor) = @_; + raw_import_section($raw_monitor, $monitor, \@monitor_fields); + $raw_monitor->{PreferredMode}{Option} = 1 if $raw_monitor->{PreferredMode}; + $raw_monitor->{ModeLine} = $monitor->{ModeLine} if $monitor->{ModeLine}; + } \@raw_monitors, \@monitors; +} +sub get_or_new_monitors { + my ($raw_X, $nb_new) = @_; + my @monitors = $raw_X->get_monitors; + + #- ensure we have exactly $nb_new monitors; + if ($nb_new > @monitors) { + @monitors, ({}) x ($nb_new - @monitors); + } else { + splice(@monitors, 0, $nb_new); + } +} +sub _new_monitor_sections { + my ($raw_X, $nb_new) = @_; + $raw_X->remove_Section('Monitor'); + map { $raw_X->add_Section('Monitor', { Identifier => { val => "monitor$_" }, ModeLine => default_ModeLine() }) } (1 .. $nb_new); +} +sub default_ModeLine() { + ModeLine_from_string(qq(Section "Monitor"\n) . (our $default_ModeLine) . qq(EndSection\n)); +} + +sub XxY { + my ($resolution) = @_; + $resolution && $resolution->{X} && $resolution->{Y} && + $resolution->{X} . 'x' . $resolution->{Y}; +} + +sub xorg_builtin_resolution { + my ($X, $Y) = @_; + my $res = $X . 'x' . $Y; + + $res eq '1280x1024' || + $res ne '1400x1050' && $res ne '1152x864' && $Xconfig::xfree::resolution2ratio{$res} eq '4/3'; +} + +sub resolution2ratio { + my ($resolution, $b_non_strict) = @_; + my $res = XxY($resolution); + $res eq '1280x1024' && $b_non_strict ? '4/3' : $Xconfig::xfree::resolution2ratio{$res}; +} + +sub add_gtf_ModeLines { + my ($raw_X, $resolutions) = @_; + + my $banner = 'modeline generated by gtf(1) [handled by XFdrake]'; + my @to_add; + if (!xorg_builtin_resolution($resolutions->[0]{X}, $resolutions->[0]{Y})) { + @to_add = map { + my $resolution = $_; + map { + my $s = run_program::rooted_get_stdout($::prefix, 'gtf', $resolution->{X}, $resolution->{Y}, $_); + if (my ($name, $val) = $s =~ /ModeLine\s*"(.*)"(.*)/i) { + chomp $val; + $name =~ s/\.00//; #- nicer that way + { val => qq("${name}"$val), pre_comment => "# $banner\n" }; + } else { () } + } reverse(sort_numbers(@Xconfig::xfree::vfreqs)); + } @$resolutions; + } + + $raw_X->set_monitors(map { + @{$_->{ModeLine}} = ( + (grep { index($_->{pre_comment}, $banner) == -1 } @{$_->{ModeLine}}), + @to_add, + ); + $_; + } $raw_X->get_monitors); + + 1; +} + +#- HACK for fglrx (#30934) +sub no_ModeLine_on_fglrx { + my ($raw_X) = @_; + + if (get_Driver($raw_X) eq 'fglrx') { + delete $_->{ModeLine} foreach $raw_X->get_Sections('Monitor'); + } +} + +################################################################################ +# screens ###################################################################### +################################################################################ +sub get_default_screen { + my ($raw_X) = @_; + my @l = $raw_X->get_Sections('Screen'); + (find { $_->{Identifier} && val($_->{Identifier}) eq 'screen1' || + $_->{Driver} && val($_->{Driver}) =~ /svga|accel/ } @l) || $l[0]; +} +sub set_screens { + my ($raw_X, @screens) = @_; + my @raw_screens = _new_screen_sections($raw_X, int @screens); + mapn { + my ($raw_screen, $screen) = @_; + raw_import_section($raw_screen, $screen); + } \@raw_screens, \@screens; +} +sub _new_screen_sections { + my ($raw_X, $nb_new) = @_; + $raw_X->remove_Section('Screen'); + my @l = map { $raw_X->add_Section('Screen', { Identifier => { val => "screen$_" } }) } (1 .. $nb_new); + + get_ServerLayout($raw_X)->{Screen} = [ + { val => qq("screen1") }, + map { { val => sprintf('"screen%d" RightOf "screen%d"', $_, $_ - 1) } } (2 .. $nb_new) + ]; + @l; +} +sub is_fbdev { + my ($raw_X, $o_Screen) = @_; + + my $Screen = $o_Screen || $raw_X->get_default_screen or return; + + my $Device = $raw_X->get_Section_by_Identifier('Device', val($Screen->{Device})) or internal_error("no device named $Screen->{Device}"); + val($Device->{Driver}) eq 'fbdev'; +} + + + + +################################################################################ +# modules ###################################################################### +################################################################################ +sub get_modules { + my ($raw_X) = @_; + my $raw_Module = $raw_X->get_Section('Module') or return; + my $Module = raw_export_section($raw_Module, ['Load']); + @{$Module->{Load} || []}; +} +sub get_disabled_modules { + my ($raw_X) = @_; + my $raw_Module = $raw_X->get_Section('Module') or return; + my $Module = raw_export_section($raw_Module, ['Disable']); + @{$Module->{Disable} || []}; +} +sub add_load_module { + my ($raw_X, $module) = @_; + my $raw_Module = $raw_X->get_Section('Module') || $raw_X->add_Section('Module', {}); + + my %load_modules_comment = ( + dbe => 'Double-Buffering Extension', + v4l => 'Video for Linux', + dri => 'direct rendering', + glx => '3D layer', + 'glx-3.so' => '3D layer', + ); + my $comment = $load_modules_comment{$module}; + push @{$raw_Module->{Load}}, { val => $module, + comment_on_line => $comment && " # $comment", + } if !member($module, $raw_X->get_modules); +} +sub add_disable_module { + my ($raw_X, $module) = @_; + my $raw_Module = $raw_X->get_Section('Module') || $raw_X->add_Section('Module', {}); + + push @{$raw_Module->{Disable}}, { val => $module } if !member($module, $raw_X->get_disabled_modules); +} +sub remove_load_module { + my ($raw_X, $module) = @_; + my $raw_Module = $raw_X->get_Section('Module') or return; + if (my @l = grep { $_->{val} ne $module } @{$raw_Module->{Load}}) { + $raw_Module->{Load} = \@l; + } else { + delete $raw_Module->{Load}; + } +} +sub remove_disable_module { + my ($raw_X, $module) = @_; + my $raw_Module = $raw_X->get_Section('Module') or return; + if (my @l = grep { $_->{val} ne $module } @{$raw_Module->{Disable}}) { + $raw_Module->{Disable} = \@l; + } else { + delete $raw_Module->{Disable}; + } +} +sub set_load_module { + my ($raw_X, $module, $bool) = @_; + if ($bool) { + remove_disable_module($raw_X, $module); + add_load_module($raw_X, $module); + } else { + remove_load_module($raw_X, $module); + add_disable_module($raw_X, $module); + } +} + + +################################################################################ +# modules ###################################################################### +################################################################################ +sub remove_extension { + my ($raw_X, $extension) = @_; + my $raw = $raw_X->get_Section('Extensions') or return; + delete $raw->{$extension}; + %$raw or $raw_X->remove_Section('Extensions'); +} +sub get_extension { + my ($raw_X, $extension) = @_; + my $raw = $raw_X->get_Section('Extensions'); + $raw && $raw->{$extension} && $raw->{$extension}{val}; +} +sub set_extension { + my ($raw_X, $extension, $val) = @_; + my $raw = $raw_X->get_Section('Extensions') || $raw_X->add_Section('Extensions', {}); + $raw->{$extension} = { 'Option' => 1, val => $val }; +} + +################################################################################ +# ModulePath ################################################################### +################################################################################ +sub get_ModulePaths { + my ($raw_X) = @_; + my $raw_Files = $raw_X->get_Section('Files') or return; + my $Files = raw_export_section($raw_Files, ['ModulePath']); + @{$Files->{ModulePath} || []}; +} +sub add_ModulePath { + my ($raw_X, $ModulePath) = @_; + my $raw_Files = $raw_X->get_Section('Files') || $raw_X->add_Section('Files', {}); + + push @{$raw_Files->{ModulePath}}, { val => $ModulePath } if !member($ModulePath, $raw_X->get_ModulePaths); +} +sub remove_ModulePath { + my ($raw_X, $ModulePath) = @_; + my $raw_Files = $raw_X->get_Section('Files') or return; + my @l = grep { $_->{val} ne $ModulePath && $_->{val} ne "$ModulePath/" } @{$raw_Files->{ModulePath}}; + $raw_Files->{ModulePath} = \@l; +} + +#-############################################################################## +#- helpers +#-############################################################################## +sub _set_Option { + my ($category, $node, @names) = @_; + + if (member($category, 'keyboard', 'mouse')) { + #- everything we export is an Option + $_->{Option} = 1 foreach map { deref_array($node->{$_}) } @names; + } +} + +sub get_InputDevices { + my ($raw_X, $Driver) = @_; + $raw_X->get_Sections('InputDevice', sub { val($_[0]{Driver}) eq $Driver }); +} +sub remove_InputDevices { + my ($raw_X, $Driver) = @_; + $raw_X->remove_InputDevices_when(sub { val($_[0]{Driver}) eq $Driver }); +} +sub remove_InputDevices_when { + my ($raw_X, $when) = @_; + my @removed = $raw_X->remove_Section('InputDevice', $when); + + my $Identifier_regexp = join('|', map { val($_->{l}{Identifier}) } @removed); + my $layout = get_ServerLayout($raw_X)->{InputDevice} ||= []; + @$layout = grep { $_->{val} !~ /^"$Identifier_regexp"/ } @$layout; +} + +sub get_ServerLayout { + my ($raw_X) = @_; + $raw_X->get_Section('ServerLayout') || + $raw_X->add_Section('ServerLayout', { Identifier => { val => 'layout1' } }); +} + +#-############################################################################## +#- helpers +#-############################################################################## +sub raw_export_section { + my ($section, $fields) = @_; + + my $export_name = sub { + my ($name) = @_; + my $h = $section->{$name} or return; + + my @l = map { if_(!$_->{commented}, $_->{val}) } deref_array($h) or return; + $name => (ref($h) eq 'ARRAY' ? \@l : $l[0]); + }; + + my %h = map { $export_name->($_) } @$fields; + \%h; +} + +sub raw_import_section { + my ($section, $h, $o_fields) = @_; + foreach ($o_fields ? grep { exists $h->{$_} } @$o_fields : sort keys %$h) { + my @l = map { ref($_) eq 'HASH' ? $_ : { val => $_ } } deref_array($h->{$_}); + $section->{$_} = (ref($h->{$_}) eq 'ARRAY' ? \@l : $l[0]); + } +} + +sub add_Section { + my ($raw_X, $Section, $h) = @_; + my @suggested_ordering = qw(Extensions Files ServerFlags Module DRI Keyboard Pointer XInput InputDevice Monitor Device Screen ServerLayout); + my %order = map_index { { lc($_) => $::i } } @suggested_ordering; + my $e = { name => $Section, l => $h }; + my $added; + my $raw = $raw_X->{raw}; + @$raw = map { + if ($order{lc $_->{name}} > $order{lc $Section} && !$added) { + $added = 1; + ($e, $_); + } else { $_ } + } @$raw; + push @$raw, $e if !$added; + $h; +} +sub remove_Section { + my ($raw_X, $Section, $o_when) = @_; + my $raw = $raw_X->{raw}; + my ($keep, $remove) = partition { $_->{name} ne $Section || $o_when && !$o_when->($_->{l}) } @$raw; + @$raw = @$keep; + @$remove; +} +sub get_Sections { + my ($raw_X, $Section, $o_when) = @_; + map { if_(lc($_->{name}) eq lc($Section) && (!$o_when || $o_when->($_->{l})), $_->{l}) } @{$raw_X->{raw}}; +} +sub get_Section { + my ($raw_X, $Section, $o_when) = @_; + my @l = get_Sections($raw_X, $Section, $o_when); + @l > 1 and log::l("Xconfig: found more than one Section $Section"); + $l[0]; +} +sub get_Section_by_Identifier { + my ($raw_X, $Section, $Identifier) = @_; + my @l = get_Sections($raw_X, $Section, sub { val($_[0]{Identifier}) eq $Identifier }); + @l > 1 and die "more than one Section $Section has Identifier $Identifier"; + $l[0]; +} + +sub val { + my ($ref) = @_; + $ref && $ref->{val}; +} + + +sub ModeLine_from_string { + my ($s) = @_; + my $raw_X_for_ModeLine = Xconfig::parse::read_XF86Config_from_string($s); + get_Section(Xconfig::xfree->new($raw_X_for_ModeLine), 'Monitor')->{ModeLine}; +} + + + +# http://home.comcast.net/~igpl/Aspect.html +# movies http://www.technosound.co.uk/nav.php?pageid=hcg_widescreen +# esp for 1360x768 http://www.winischhofer.at/linuxsispart1.shtml + +# www.dell.com/downloads/global/vectors/2003_cvt.pdf +# file vesamodes in Xorg is DMT Standard Display Modes + +# http://www.vesa.org/Public +# http://www.vesa.org/Public/EEDIDguideV1.pdf + +#- http://www.vesa.org/Public/CVT +our @CVT_ratios = qw(4/3 16/9 16/10 5/4 15/9 3/2); +our @CVT_vfreqs = qw(50 60 75 85); # and also 60Hz "reduced blanking" in CVT + +our @vfreqs = (@CVT_vfreqs, qw(100 120)); + +our %ratio2resolutions = ( + + # first all the CVT_ratios + + # 1.25 + '5/4' => [ '640x512', + '720x576', + '1280x1024', # SXGA + '1800x1440', + #'2560x2048', # QSXGA + #'5120x4096', # HSXGA + ], + + # 1.33 + '4/3' => [ '320x240', # QVGA + '480x360', + '640x480', # VGA + #'768x576', # PAL + '800x600', # SVGA + '832x624', + '1024x768', # XGA + '1152x864', + '1280x960', + '1400x1050', # SXGA+ + '1600x1200', # UXGA + #'1792x1344', + #'1800x1350', + #'1856x1392', + '1920x1440', + '2048x1536', # QXGA + '2800x2100', # QSXGA+ + '3200x2400', # QUXGA + '4096x3072', # HXGA + '6400x4800', # HUXGA + # DBLSCAN: 400x300 416x312 512x384 576x432 700x525 896x672 928x696 960x720 + ], + + # 1.5 + '3/2' => [ '360x240', + '720x480', # NTSC + '1152x768', + #'1280x854', + #'1440x960', + ], # 576x384 (DBLSCAN of 1152x768) + + # 1.6 + '16/10' => [ #'320x200', # CGA + #'640x400', + #'960x600', + '1280x800', + '1440x900', + '1600x1000', + '1680x1050', # WSXGA+ + '1920x1200', # WUXGA + '2560x1600', # WQXGA + '3840x2400', # WQUXGA + '5120x3200', # WHXGA + '7680x4800', # WHUXGA + ], + + # 1.67 + '15/9' => [ '800x480', + '1280x768', # WXGA ?? should be 1366x768 ?? + ], + + # 1.78 + '16/9' => [ #'854x480', # WVGA + #'960x540', + #'1024x576', + '1280x720', # HD 720 + '1360x765', # one kind of WXGA (rounded down) + '1366x768', # HDTV + '1600x900', # WSXGA? + '1920x1080', # HD 1080 + '7680x4320', # UHDTV + ], + + # now more weird things + + # 1.32 + # '192/145' => [ '1152x870' ], + + # 1.328 + # '85/64' => [ '1360x1024' ], + + # 1.42 + # '17/12' => [ '544x384' ] , + + # 1.56 + #'25/16' => [ '1600x1024', # WSXGA + # '3200x2048', # WQSXGA + # '6400x4096', # WHSXGA + # ], # (DBLSCAN 800x512) + + # 1.767 + # '53/30' => [ '848x480' ], + + # 1.775 + # '71/40' => [ '852x480' ], + + # 1.783 + # '107/60' => [ '856x480' ], + + N_("_:weird aspect ratio\nother") => [ + + # 1.707 = 128/75 + '1024x600', + + # 1.771 = 85/48 + '1360x768', + + # 2.13 = 32/15 + '1024x480', '1280x600', # VAIO + + # 2.67 = 8/3 + '2048x768', '2560x960', '3200x1200', + + # 4.0 = 4/1 + '3072x768', '3456x864', '3840x960', '4800x1200', + + # ?? 352x288 640x350 (DBLSCAN 320x175) 720x400 (DBLSCAN 360x200) + ], +); + +our %resolution2ratio = map_each { map { $_ => $::a } @$::b } %ratio2resolutions; +our @resolutions = map_each { @$::b } %ratio2resolutions; + +foreach my $ratio (keys %ratio2resolutions) { + if ($ratio =~ m!^(\d+)/(\d+)$!) { + my $eval = $2 / $1; + foreach (@{$ratio2resolutions{$ratio}}) { + my ($x, $y) = /(\d+)x(\d+)/; + my $y2 = round($x * $eval); + $y == $y2 or do { + my $good_ratio = (find { m!^(\d+)/(\d+)$! && $y == round($x * $2 / $1) } keys %ratio2resolutions) || '??'; + die "bad ratio $ratio for resolution $_, it should be $good_ratio\n"; + }; + } + } +} + +our $default_header = <<'END'; +# ********************************************************************** +# Refer to the xorg.conf man page for details about the format of +# this file. +# ********************************************************************** + +Section "ServerFlags" + Option "DontZap" "False" # disable <Ctrl><Alt><BS> (server abort) + #DontZoom # disable <Ctrl><Alt><KP_+>/<KP_-> (resolution switching) + AllowMouseOpenFail # allows the server to start up even if the mouse does not work +END + +require detect_devices; +$default_header .= <<'END_XBOX' if detect_devices::is_xbox(); + Option "PciProbe1" "false" + Option "PciProbe2" "false" + Option "PciForceConfig1" "false" + Option "PciForceConfig2" "false" + Option "PciOsConfig" "true" +END_XBOX + +$default_header .= <<'END'; +EndSection +END + +our $default_ModeLine = arch() =~ /ppc/ ? <<'END_PPC' : <<'END'; + # Apple iMac modes + ModeLine "1024x768" 78.525 1024 1049 1145 1312 768 769 772 800 +hsync +vsync + ModeLine "800x600" 62.357 800 821 901 1040 600 601 604 632 +hsync +vsync + ModeLine "640x480" 49.886 640 661 725 832 480 481 484 514 +hsync +vsync + # Apple monitors tend to do 832x624 + ModeLine "832x624" 57 832 876 940 1152 624 625 628 667 -hsync -vsync + # Apple PowerBook G3 + ModeLine "800x600" 100 800 816 824 840 600 616 624 640 -hsync -vsync + # Apple TI Powerbook + ModeLine "1152x768" 78.741 1152 1173 1269 1440 768 769 772 800 +vsync +vsync + # Pismo Firewire G3 + ModeLine "1024x768" 65 1024 1032 1176 1344 768 771 777 806 -hsync -vsync + # iBook2 + ModeLine "1024x768" 65 1024 1048 1184 1344 768 771 777 806 -hsync -vsync + # 17" Apple Studio Display + ModeLine "1024x768" 112.62 1024 1076 1248 1420 768 768 780 808 +hsync +vsync + # HiRes Apple Studio Display + ModeLine "1280x1024" 135 1280 1288 1392 1664 1024 1027 1030 1064 + # Another variation + ModeLine "1280x1024" 134.989 1280 1317 1429 1688 1024 1025 1028 1066 +hsync +vsync +END_PPC + # TV fullscreen mode or DVD fullscreen output. + # 768x576 @ 79 Hz, 50 kHz hsync + ModeLine "768x576" 50.00 768 832 846 1000 576 590 595 630 + # 768x576 @ 100 Hz, 61.6 kHz hsync + ModeLine "768x576" 63.07 768 800 960 1024 576 578 590 616 +END + +1; diff --git a/lib/keyboard.pm b/lib/keyboard.pm new file mode 100644 index 0000000..3355f31 --- /dev/null +++ b/lib/keyboard.pm @@ -0,0 +1,688 @@ +package keyboard; # $Id: keyboard.pm 263727 2009-11-27 09:26:40Z cfergeau $ + +use diagnostics; +use strict; + +#-###################################################################################### +#- misc imports +#-###################################################################################### +use common; +use detect_devices; +use run_program; +use lang; +use log; +use c; + + +#-###################################################################################### +#- Globals +#-###################################################################################### +my $KMAP_MAGIC = 0x8B39C07F; + +#- a best guess of the keyboard layout, based on the choosen locale +#- beware only the first 5 characters of the locale are used +our %lang2keyboard = +( + 'af' => 'us_intl', + 'am' => 'us:90', + 'ar' => 'ara:90', + 'as' => 'ben:90 ben2:80 us_intl:5', + 'ast' => 'es:85 lat:80 us_intl:50', + 'az' => 'az:90 tr_q:10 us_intl:5', +'az_IR' => 'ir:90', + 'be' => 'by:90 ru:50 ru_yawerty:40', + 'ber' => 'tifinagh:80 tifinagh_p:70', + 'bg' => 'bg_phonetic:60 bg:50', + 'bn' => 'ben:90 ben2:80 dev:20 us_intl:5', + 'bo' => 'bt', + 'br' => 'fr:90', + 'bs' => 'bs:90', + 'ca' => 'es:90 fr:15', + 'ca@valencian' => 'es', + 'chr' => 'chr:80 us:60 us_intl:60', + 'cs' => 'cz_qwerty:70 cz:50', + 'cy' => 'gb:90', + 'da' => 'dk:90', + 'de' => 'de_nodeadkeys:70 de:50 be:50 ch_de:50', + 'dz' => 'bt', + 'el' => 'gr:90', + 'en' => 'us:89 us3:80 us_intl:50 qc:50 gb:50 dvorak:10', +'en_AU' => 'us:80 us3:70 us_intl:50 gb:40 dvorak:10 dvorak_gb:5', +'en_CA' => 'us:80 us3:70 us_intl:50 qc:50 gb:40 dvorak:10', +'en_GB' => 'gb:89 us:60 us_intl:50 dvorak_gb:10', +'en_IE' => 'ie:80 gb:70 dvorak_gb:10', +'en_NG' => 'ng:80 us:60', +'en_NZ' => 'us:80 us3:70 us_intl:50 gb:40 dvorak:10 dvorak_gb:5', +'en_US' => 'us:89 us3:80 us_intl:50 dvorak:10', + 'eo' => 'us_intl:89 dvorak_eo:30 dvorak:10', + 'es' => 'es:85 lat:80 us_intl:50', + 'et' => 'ee:90', + 'eu' => 'es:90 fr:15', + 'fa' => 'ir:90', + 'fi' => 'fi:90', + 'fo' => 'fo:80 is:70 dk:60', + 'fr' => 'fr:89 qc:85 be:85 ch_fr:70 dvorak_fr:20', +'fr_CA' => 'qc:89', +'fr_CH' => 'ch_fr:89', +'fr_BE' => 'be:89', + 'fur' => 'it:90', + 'ga' => 'ie:80 gb:70 dvorak_gb:10', + 'gd' => 'gb:80 ie:70 dvorak_gb:10', + 'gl' => 'es:90', + 'gn' => 'lat:85 es:80 us_intl:50', + 'gu' => 'guj:90', + 'gv' => 'gb:80 ie:70', + 'ha' => 'ng', + 'he' => 'il:90 il_phonetic:10', + 'hi' => 'dev:90', + 'hr' => 'hr:90 si:50', + 'hu' => 'hu:90', + 'hy' => 'am:90 am_old:10 am_phonetic:5', + 'ia' => 'us:90 us_intl:20', + 'id' => 'us:90 us_intl:20', + 'ig' => 'ng', + 'is' => 'is:90', + 'it' => 'it:90 ch_fr:50 ch_de:50', + 'iu' => 'iku:90', + 'ja' => 'jp:90', + 'ka' => 'ge_la:90 ge_ru:50', + 'kl' => 'dk:80 us_intl:30', + 'kn' => 'kan:90', + 'ko' => 'kr:90 us:60', + 'ku' => 'tr_q:90 tr_f:30', +'ku_IQ' => 'kur:90', + 'kw' => 'gb:80 ie:70', + 'ky' => 'kg:90 ru_yawerty:40', + 'lb' => 'ch_fr:89 be:85 us_intl:70 fr:60 dvorak_fr:20', + 'li' => 'us_intl:80 be:70 nl:10 us:5', + 'lo' => 'lao:90', + 'lt' => 'lt:80', + 'ltg' => 'lv:90 lt:40 ee:5', + 'lv' => 'lv:90 lt:40 ee:5', + 'mi' => 'mao:90 gb:30 us_intl:20', + 'mk' => 'mk:90', + 'ml' => 'mal:90', + 'mn' => 'mn:90 ru:20 ru_yawerty:5', + 'mr' => 'dev:90', + 'ms' => 'us:90 us_intl:20', + 'mt' => 'mt:90 mt_us:35 us_intl:10', + 'my' => 'mm:90', + 'nb' => 'no:90 dvorak_no:10', + 'nds' => 'de_nodeadkeys:70 de:50 us_intl:40 nl:10 us:5', + 'ne' => 'dev:90', + 'nl' => 'us_intl:80 be:70 nl:10 us:5', + 'nn' => 'no:90 dvorak_no:10', + 'no' => 'no:90 dvorak_no:10', # for compatiblity only + 'oc' => 'fr:90', + 'or' => 'ori:90', + 'pa' => 'gur:90', + 'ph' => 'us:90 us_intl:20', + 'pl' => 'pl:90 pl2:60 dvorak_pl:10', + 'pp' => 'br:80 lat:20 pt:10 us_intl:30', + 'ps' => 'pus:80 snd:60', +'pt_BR' => 'br:90 lat:20 pt:10 us_intl:30', + 'pt' => 'pt:90', + 'ro' => 'ro:80 ro_qwertz:40 us_intl:10', + 'ru' => 'ru:85 ru_yawerty:80 ua:50', + 'sc' => 'it:90', + 'sd' => 'snd:80 ara:20', + 'se' => 'smi:70 smi_sefi:50', + 'sh' => 'yu:80', + 'si' => 'sin', + 'sk' => 'sk_qwerty:80 sk:70', + 'sl' => 'si:90 hr:50', + 'sq' => 'al:90', + 'sr' => 'srp:80', + 'ss' => 'us_intl', + 'st' => 'us_intl', + 'sv' => 'se:90 fi:30 dvorak_se:10', + 'ta' => 'tscii:80 tml:20', + 'te' => 'tel:90', + 'tg' => 'tj:90 ru_yawerty:40', + 'th' => 'th:80 th_pat:50 th_tis:60', + 'tk' => 'tm:80 tr_q:50 tr_f:40', + 'tl' => 'us:90 us_intl:20', + 'tr' => 'tr_q:90 tr_f:30', + 'tt' => 'ru:50 ru_yawerty:40', + 'uk' => 'ua:90 ru:50 ru_yawerty:40', + 'ur' => 'urd:80 snd:60 ara:20', + 'uz' => 'uz:80 ru_yawerty:40', + 'uz\@Cyrl' => 'uz:80 ru_yawerty:40', + 'uz\@Latn' => 'us:80 uz:80', + 've' => 'us_intl', + 'vi' => 'vn:80 us:70 us_intl:60', + 'wa' => 'be:90 fr:5', + 'xh' => 'us_intl', + 'yi' => 'il_phonetic:90 il:10 us_intl:10', + 'yo' => 'ng', +'zh_CN' => 'us:90', +'zh_TW' => 'us:90', + 'zu' => 'us_intl', +); + +# USB kbd table +# The numeric values are the bCountryCode field (5th byte) of HID descriptor +# NOTE: we do not trust when the layout is declared as US layout (0x21) +# as most manufacturers just use that value when selling physical devices +# with different layouts printed on the keys. +my @usb2keyboard = +( + qw(SKIP ara_SKIP be ca_SKIP qc cz dk fi fr de gr il hu us_intl it jp), +#- 0x10 + qw(kr lat nl no ir pl pt ru sk es se ch_fr ch_de ch_de tw_SKIP tr_q), +#- 0x20 + qw(gb us_SKIP yu tr_f), +#- higher codes not attribued as of 2002-02 +); + +#- key = extension for Xmodmap file, [0] = description of the keyboard, +#- [1] = name for loadkeys, [2] = name for XKB, [3] = "1" if it is +#- a multigroup layout (eg: one with latin/non-latin letters) +#- +#- note: there seems to be a limit of 4 stackable xkb layouts +my %keyboards = ( +arch() =~ /^sparc/ ? ( +#- do we still support those? all but "se" and "us" have dissapeared from +#- symbols/sun/ directory in Xorg package. + "cz" => [ N_("_: keyboard\nCzech (QWERTZ)"), "sunt5-cz-us", "cz", 0 ], + "de" => [ N_("_: keyboard\nGerman"), "sunt5-de-latin1", "de", 0 ], + "dvorak" => [ N_("_: keyboard\nDvorak"), "sundvorak", "dvorak",0 ], + "es" => [ N_("_: keyboard\nSpanish"), "sunt5-es", "es", 0 ], + "fi" => [ N_("_: keyboard\nFinnish"), "sunt5-fi-latin1", "fi", 0 ], + "fr" => [ N_("_: keyboard\nFrench"), "sunt5-fr-latin1", "fr", 0 ], + "gb" => [ N_("UK keyboard"), "sunt5-uk", "gb", 0 ], + "no" => [ N_("_: keyboard\nNorwegian"), "sunt4-no-latin1", "no", 0 ], + "pl" => [ N_("_: keyboard\nPolish"), "sun-pl-altgraph", "pl", 0 ], + "ru" => [ N_("_: keyboard\nRussian"), "sunt5-ru", "ru", 1 ], + "se" => [ N_("_: keyboard\nSwedish"), "sunt5-fi-latin1", "se", 0 ], + "us" => [ N_("US keyboard"), "sunkeymap", "us", 0 ], +) : ( + "al" => [ N_("_: keyboard\nAlbanian"), "al", "al", 0 ], + "am_old" => [ N_("_: keyboard\nArmenian (old)"), "am_old", "am(old)", 1 ], + "am" => [ N_("_: keyboard\nArmenian (typewriter)"), "am-armscii8", "am", 1 ], + "am_phonetic" => [ N_("_: keyboard\nArmenian (phonetic)"), "am_phonetic", "am(phonetic)",1 ], + "ara" => [ N_("_: keyboard\nArabic"), "us", "ara(digits)", 1 ], + "ast" => [ N_("_: keyboard\nAsturian"), "es", "es(ast)", 0 ], + "az" => [ N_("_: keyboard\nAzerbaidjani (latin)"), "az", "az", 0 ], + "be" => [ N_("_: keyboard\nBelgian"), "be2-latin1", "be", 0 ], + "ben" => [ N_("_: keyboard\nBengali (Inscript-layout)"), "us", "in(ben)", 1 ], + "ben2" => [ N_("_: keyboard\nBengali (Probhat)"), "us", "in(ben_probhat)", 1 ], +"bg_phonetic" => [ N_("_: keyboard\nBulgarian (phonetic)"), "bg", "bg(phonetic)", 1 ], + "bg" => [ N_("_: keyboard\nBulgarian (BDS)"), "bg_bds", "bg", 1 ], + "br" => [ N_("_: keyboard\nBrazilian (ABNT-2)"), "br-abnt2", "br", 0 ], + "bs" => [ N_("_: keyboard\nBosnian"), "croat", "ba", 0 ], + "bt" => [ N_("_: keyboard\nDzongkha/Tibetan"), "us", "bt", 1 ], + "by" => [ N_("_: keyboard\nBelarusian"), "by-cp1251", "by", 1 ], + "ch_de" => [ N_("_: keyboard\nSwiss (German layout)"), "sg-latin1", "ch(de)", 0 ], + "ch_fr" => [ N_("_: keyboard\nSwiss (French layout)"), "fr_CH-latin1", "ch(fr)", 0 ], +# TODO: console map + "chr" => [ N_("_: keyboard\nCherokee syllabics"), "us", "chr", 1 ], + "cz" => [ N_("_: keyboard\nCzech (QWERTZ)"), "cz", "cz", 0 ], + "cz_qwerty" => [ N_("_: keyboard\nCzech (QWERTY)"), "cz-lat2", "cz(qwerty)", 0 ], + "de" => [ N_("_: keyboard\nGerman"), "de-latin1", "de", 0 ], + "de_nodeadkeys" => [ N_("_: keyboard\nGerman (no dead keys)"), "de-latin1-nodeadkeys", "de(nodeadkeys)", 0 ], + "dev" => [ N_("_: keyboard\nDevanagari"), "us", "in(deva)", 1 ], + "dk" => [ N_("_: keyboard\nDanish"), "dk-latin1", "dk", 0 ], + "dvorak" => [ N_("_: keyboard\nDvorak (US)"), "pc-dvorak-latin1", "us(dvorak)", 0 ], + "dvorak_eo" => [ N_("_: keyboard\nDvorak (Esperanto)"), "eo-dvorak", "dvorak(eo)", 0 ], + "dvorak_fr" => [ N_("_: keyboard\nDvorak (French)"), "fr-dvorak", "fr(dvorak)", 0 ], + "dvorak_gb" => [ N_("_: keyboard\nDvorak (UK)"), "pc-dvorak-latin1", "gb(dvorak)", 0 ], + "dvorak_no" => [ N_("_: keyboard\nDvorak (Norwegian)"), "no-dvorak", "no(dvorak)", 0 ], + "dvorak_pl" => [ N_("_: keyboard\nDvorak (Polish)"), "pl-dvorak", "pl(dvorak)", 0 ], + "dvorak_se" => [ N_("_: keyboard\nDvorak (Swedish)"), "se-dvorak", "se(dvorak)", 0 ], + "ee" => [ N_("_: keyboard\nEstonian"), "ee-latin9", "ee", 0 ], + "es" => [ N_("_: keyboard\nSpanish"), "es-latin1", "es", 0 ], + "fi" => [ N_("_: keyboard\nFinnish"), "fi-latin1", "fi", 0 ], + "fo" => [ N_("_: keyboard\nFaroese"), "is-latin1", "fo", 0 ], + "fr" => [ N_("_: keyboard\nFrench"), "fr-latin1", "fr", 0 ], + "gb" => [ N_("UK keyboard"), "uk-latin1", "gb", 0 ], + "ge_ru" => [N_("_: keyboard\nGeorgian (\"Russian\" layout)"), "ge_ru-georgian_academy", "ge(ru)",1], + "ge_la" => [N_("_: keyboard\nGeorgian (\"Latin\" layout)"), "ge_la-georgian_academy", "ge(la)",1], + "gr" => [ N_("_: keyboard\nGreek"), "gr-8859_7", "gr(extended)", 1 ], + "gr_pl" => [ N_("_: keyboard\nGreek (polytonic)"), "gr-8859_7", "gr(polytonic)", 1 ], + "guj" => [ N_("_: keyboard\nGujarati"), "us", "in(guj)", 1 ], + "gur" => [ N_("_: keyboard\nGurmukhi"), "us", "in(guru)", 1 ], + "hr" => [ N_("_: keyboard\nCroatian"), "croat", "hr", 0 ], + "hu" => [ N_("_: keyboard\nHungarian"), "hu-latin2", "hu", 0 ], + "ie" => [ N_("_: keyboard\nIrish"), "uk-latin1", "ie", 0 ], + "iku" => [ N_("_: keyboard\nInuktitut"), "us", "ca(ike)", 1 ], + "il" => [ N_("_: keyboard\nIsraeli"), "il-8859_8", "il", 1 ], + "il_phonetic" => [ N_("_: keyboard\nIsraeli (phonetic)"), "hebrew", "il(phonetic)", 1 ], + "ir" => [ N_("_: keyboard\nIranian"), "ir-isiri_3342", "ir", 1 ], + "is" => [ N_("_: keyboard\nIcelandic"), "is-latin1", "is", 0 ], + "it" => [ N_("_: keyboard\nItalian"), "it-latin1", "it", 0 ], +# Japanese keyboard is dual latin/kana; but telling it here shows a +# message to choose the switching key that is misleading, as input methods +# are not automatically enabled when typing in kana + "jp" => [ N_("_: keyboard\nJapanese 106 keys"), "jp106", "jp", 0 ], + "kan" => [ N_("_: keyboard\nKannada"), "us", "in(kan)", 1 ], + "kg" => [ N_("_: keyboard\nKyrgyz"), "ky", "kg(direct)", 1 ], + "kr" => [ N_("_: keyboard\nKorean"), "us", "kr(kr104)", 0 ], +# TODO: console map + "kur" => [ N_("_: keyboard\nKurdish (arabic script)"), "us", "kur", 1 ], + "lat" => [ N_("_: keyboard\nLatin American"), "la-latin1", "latam", 0 ], +# TODO: console map + "lao" => [ N_("_: keyboard\nLaotian"), "us", "la", 1 ], + "lt" => [ N_("_: keyboard\nLithuanian"), "lt-latin7", "lt,us", 1 ], + "lv" => [ N_("_: keyboard\nLatvian"), "lv-latin7", "lv", 0 ], + "mal" => [ N_("_: keyboard\nMalayalam"), "us", "in(mal)", 1 ], + "mao" => [ N_("_: keyboard\nMaori"), "us", "mao", 0 ], + "mk" => [ N_("_: keyboard\nMacedonian"), "mk", "mkd", 1 ], + "mm" => [ N_("_: keyboard\nMyanmar (Burmese)"), "us", "mm", 1 ], + "mn" => [ N_("_: keyboard\nMongolian (cyrillic)"), "us", "mn", 1 ], + "mt" => [ N_("_: keyboard\nMaltese (UK)"), "mt", "mt", 0 ], + "mt_us" => [ N_("_: keyboard\nMaltese (US)"), "mt_us", "mt(us)", 0 ], + "ng" => [ N_("_: keyboard\nNigerian"), "us", "ng", 0 ], + "nl" => [ N_("_: keyboard\nDutch"), "nl-latin1", "nl", 0 ], + "no" => [ N_("_: keyboard\nNorwegian"), "no-latin1", "no", 0 ], + "ori" => [ N_("_: keyboard\nOriya"), "us", "in(ori)", 1 ], + "pl" => [ N_("_: keyboard\nPolish (qwerty layout)"), "pl", "pl", 0 ], + "pl2" => [ N_("_: keyboard\nPolish (qwertz layout)"), "pl-latin2", "pl(qwertz)", 0 ], +# TODO: console map + "pus" => [ N_("_: keyboard\nPashto"), "us", "pus", 1 ], + "pt" => [ N_("_: keyboard\nPortuguese"), "pt-latin1", "pt", 0 ], + "qc" => [ N_("_: keyboard\nCanadian (Quebec)"), "qc-latin1", "ca", 0 ], + "ro_qwertz" => [ N_("_: keyboard\nRomanian (qwertz)"), "ro2", "ro(winkeys)", 0 ], + "ro" => [ N_("_: keyboard\nRomanian (qwerty)"), "ro", "ro(std_cedilla)", 0 ], + "ru" => [ N_("_: keyboard\nRussian"), "ru4", "ru(winkeys)", 1 ], + "ru_yawerty" => [ N_("_: keyboard\nRussian (phonetic)"), "ru-yawerty", "ru(phonetic)", 1 ], + "se" => [ N_("_: keyboard\nSwedish"), "se-latin1", "se", 0 ], + "si" => [ N_("_: keyboard\nSlovenian"), "slovene", "si", 0 ], +# TODO: console map + "sin" => [ N_("_: keyboard\nSinhala"), "us", "lk", 1 ], + "sk" => [ N_("_: keyboard\nSlovakian (QWERTZ)"), "sk-qwertz", "sk", 0 ], + "sk_qwerty" => [ N_("_: keyboard\nSlovakian (QWERTY)"), "sk-qwerty", "sk(qwerty)", 0 ], + "smi" => [ N_("_: keyboard\nSaami (norwegian)"), "no-latin1", "no(smi)", 0 ], + "smi_sefi" => [ N_("_: keyboard\nSaami (swedish/finnish)"), "se-latin1", "se(smi)", 0 ], +# TODO: console map + "snd" => [ N_("_: keyboard\nSindhi"), "us", "snd(digits)", 1 ], +# TODO: console map + "srp" => [ N_("_: keyboard\nSerbian (cyrillic)"), "sr", "srp(basic),srp(latin)", 1 ], + "syr" => [ N_("_: keyboard\nSyriac"), "us", "sy(syc)", 1 ], + "syr_p" => [ N_("_: keyboard\nSyriac (phonetic)"), "us", "sy(syc_phonetic)", 1 ], + "tel" => [ N_("_: keyboard\nTelugu"), "us", "in(tel)", 1 ], +# no console kbd that I'm aware of + "tml" => [ N_("_: keyboard\nTamil (ISCII-layout)"), "us", "in(tam)", 1 ], + "tscii" => [ N_("_: keyboard\nTamil (Typewriter-layout)"), "us", "in(tam_unicode)", 1 ], + "th" => [ N_("_: keyboard\nThai (Kedmanee)"), "th", "th", 1 ], + "th_tis" => [ N_("_: keyboard\nThai (TIS-820)"), "th", "th(tis)", 1 ], +# TODO: console map + "th_pat" => [ N_("_: keyboard\nThai (Pattachote)"), "us", "th(pat)", 1 ], +# NOTE: we define a triple layout here + "tifinagh" => [ N_("_: keyboard\nTifinagh (moroccan layout) (+latin/arabic)"), "fr-tifinagh", "fr,tifinagh(basic),ara(azerty)", 1 ], + "tifinagh_p" => [ N_("_: keyboard\nTifinagh (phonetic) (+latin/arabic)"), "fr-tifinaghp", "fr,tifinagh(phonetic),ara(azerty)", 1 ], +# TODO: console map + "tj" => [ N_("_: keyboard\nTajik"), "ru4", "tj", 1 ], +# TODO: console map + "tm" => [ N_("_: keyboard\nTurkmen"), "us", "tm", 0 ], + "tr_f" => [ N_("_: keyboard\nTurkish (traditional \"F\" model)"), "trf", "tr(f)", 0 ], + "tr_q" => [ N_("_: keyboard\nTurkish (modern \"Q\" model)"), "tr_q-latin5", "tr", 0 ], +#-"tw => [ N_("_: keyboard\nChineses bopomofo"), "tw", "tw", 1 ], + "ua" => [ N_("_: keyboard\nUkrainian"), "ua", "ua", 1 ], +# TODO: console map + "urd" => [ N_("_: keyboard\nUrdu keyboard"), "us", "urd", 1 ], + "us" => [ N_("US keyboard"), "us", "us", 0 ], + "us_intl" => [ N_("US keyboard (international)"), "us-intl", "us(alt-intl)", 0 ], + "us3" => [ N_("ISO9995-3 (US keyboard with 3 levels per key)"), "us", "latin+level3(ralt_switch)", 0 ], + "uz" => [ N_("_: keyboard\nUzbek (cyrillic)"), "uz", "uz", 1 ], +# old XKB layout + "vn" => [ N_("_: keyboard\nVietnamese \"numeric row\" QWERTY"), "vn-tcvn", "vn", 0 ], + "yu" => [ N_("_: keyboard\nYugoslavian (latin)"), "sr", "srp(latin)", 0 ], +), +); + +#- list of possible choices for the key combinations to toggle XKB groups +#- (eg in X86Config file: XkbOptions "grp:toggle") +my %grp_toggles = ( + toggle => N_("Right Alt key"), + shifts_toggle => N_("Both Shift keys simultaneously"), + ctrl_shift_toggle => N_("Control and Shift keys simultaneously"), + caps_toggle => N_("CapsLock key"), + shift_caps_toggle => N_("Shift and CapsLock keys simultaneously"), + ctrl_alt_toggle => N_("Ctrl and Alt keys simultaneously"), + alt_shift_toggle => N_("Alt and Shift keys simultaneously"), + menu_toggle => N_("\"Menu\" key"), + lwin_toggle => N_("Left \"Windows\" key"), + rwin_toggle => N_("Right \"Windows\" key"), + ctrls_toggle => N_("Both Control keys simultaneously"), + alts_toggle => N_("Both Alt keys simultaneously"), + lshift_toggle => N_("Left Shift key"), + rshift_toggle => N_("Right Shift key"), + lalt_toggle => N_("Left Alt key"), + lctrl_toggle => N_("Left Control key"), + rctrl_toggle => N_("Right Control key"), +); + + +#-###################################################################################### +#- Functions +#-###################################################################################### +sub KEYBOARDs() { keys %keyboards } +sub KEYBOARD2text { $keyboards{$_[0]} && $keyboards{$_[0]}[0] } +sub _keyboards() { map { { KEYBOARD => $_ } } keys %keyboards } +sub _keyboard2one { + my ($keyboard, $nb) = @_; + ref $keyboard or (detect_devices::is_xbox() ? return undef : internal_error()); + my $l = $keyboards{$keyboard->{KEYBOARD}} or return; + $l->[$nb]; +} +sub keyboard2text { _keyboard2one($_[0], 0) } +sub keyboard2kmap { _keyboard2one($_[0], 1) } +sub _keyboard2xkb { _keyboard2one($_[0], 2) } + +sub xkb_models() { + my $models = _parse_xkb_rules()->{model}; + [ map { $_->[0] } @$models ], { map { @$_ } @$models }; +} + +sub _grp_toggles { + my ($keyboard) = @_; + _keyboard2one($keyboard, 3) or return; + \%grp_toggles; +} + +sub group_toggle_choose { + my ($in, $keyboard) = @_; + + if (my $grp_toggles = _grp_toggles($keyboard)) { + my $GRP_TOGGLE = $keyboard->{GRP_TOGGLE} || 'caps_toggle'; + $GRP_TOGGLE = $in->ask_from_listf('', N("Here you can choose the key or key combination that will +allow switching between the different keyboard layouts +(eg: latin and non latin)"), sub { translate($grp_toggles->{$_[0]}) }, [ sort keys %$grp_toggles ], $GRP_TOGGLE) or return; + + if ($::isInstall && $GRP_TOGGLE ne 'rctrl_toggle') { + $in->ask_warn(N("Warning"), formatAlaTeX( +N("This setting will be activated after the installation. +During installation, you will need to use the Right Control +key to switch between the different keyboard layouts."))); + } + log::l("GRP_TOGGLE: $GRP_TOGGLE"); + $keyboard->{GRP_TOGGLE} = $GRP_TOGGLE; + } else { + $keyboard->{GRP_TOGGLE} = ''; + } + 1; +} + +sub loadkeys_files { + my ($err) = @_; + my $archkbd = arch() =~ /^sparc/ ? "sun" : arch() =~ /i.86|x86_64/ ? "i386" : arch() =~ /ppc/ ? "mac" : arch(); + my $p = "/usr/lib/kbd/keymaps/$archkbd"; + my $post = ".map.gz"; + my %trans = ("cz-latin2" => "cz-lat2"); + my %find_file; + foreach my $dir (all($p)) { + $find_file{$dir} = ''; + foreach (all("$p/$dir")) { + $find_file{$_} and $err->("file $_ is both in $find_file{$_} and $dir") if $err; + $find_file{$_} = "$p/$dir/$_"; + } + } + my (@l, %l); + foreach (values %keyboards) { + local $_ = $trans{$_->[1]} || $_->[1]; + my $l = $find_file{"$_$post"} || $find_file{first(/(..)/) . $post}; + if ($l) { + push @l, $l; + foreach (`zgrep include $l | grep "^include"`) { + /include\s+"(.*)"/ or die "bad line $_"; + @l{grep { -e $_ } ("$p/$1.inc.gz")} = (); + } + } else { + $err->("invalid loadkeys keytable $_") if $err; + } + } + uniq(@l, keys %l, grep { -e $_ } map { "$p/$_.inc.gz" } qw(compose euro windowkeys linux-keys-bare)); +} + +sub _unpack_keyboards { + my ($k) = @_; $k or return; + [ grep { + my $b = $keyboards{$_->[0]}; + $b or log::l("bad keyboard $_->[0] in %keyboard::lang2keyboard"); + $b; + } map { [ split ':' ] } split ' ', $k ]; +} +sub lang2keyboards { + my @li = sort { $b->[1] <=> $a->[1] } map { @$_ } map { + my $h = lang::analyse_locale_name($_); + #- example: pt_BR and pt + my @l = (if_($h->{country}, $h->{main} . '_' . $h->{country}), $h->{main}, 'en'); + my $k = find { $_ } map { $lang2keyboard{$_} } @l; + _unpack_keyboards($k) || internal_error(); + } @_; + \@li; +} +sub lang2keyboard { + my ($l) = @_; + + my $kb = lang2keyboards($l)->[0][0]; + { KEYBOARD => $keyboards{$kb} ? $kb : 'us' }; #- handle incorrect keyboard mapping to us. +} + +sub default { + my ($o_locale) = @_; + + my $keyboard = from_usb() || lang2keyboard(($o_locale || lang::read())->{lang}); + add2hash($keyboard, from_DMI()); + $keyboard; +} + +sub from_usb() { + return if $::noauto; + my ($usb_kbd) = detect_devices::usbKeyboards() or return; + my $country_code = detect_devices::usbKeyboard2country_code($usb_kbd) or return; + my $keyboard = $usb2keyboard[$country_code]; + $keyboard !~ /SKIP/ && { KEYBOARD => $keyboard }; +} + +sub from_DMI() { + my $XkbModel = detect_devices::probe_unique_name('XkbModel'); + $XkbModel && { XkbModel => $XkbModel }; +} + +sub _builtin_loadkeys { + my ($keymap) = @_; + return if $::testing; + + my ($magic, $tables_given, @tables) = common::unpack_with_refs('I' . + 'i' . c::MAX_NR_KEYMAPS() . + 's' . c::NR_KEYS() . '*', + $keymap); + $magic != $KMAP_MAGIC and die "failed to read kmap magic"; + + sysopen(my $F, "/dev/console", 2) or die "failed to open /dev/console: $!"; + + my $i_tables = 0; + each_index { + my $table_index = $::i; + if (!$_) { + #- deallocate table + ioctl($F, c::KDSKBENT(), pack("CCS", $table_index, 0, c::K_NOSUCHMAP())) or log::l("removing table $table_index failed: $!"); + } else { + each_index { + ioctl($F, c::KDSKBENT(), pack("CCS", $table_index, $::i, $_)) or log::l("keymap ioctl failed ($table_index $::i $_): $!"); + } @{$tables[$i_tables++]}; + } + } @$tables_given; +} + +sub _parse_xkb_rules() { + my $cat; + my %l; + my $lst_file = "$::prefix/usr/share/X11/xkb/rules/xorg.lst"; + foreach (cat_($lst_file)) { + next if m!^\s*//! || m!^\s*$!; + chomp; + if (/^!\s*(\S+)$/) { + $cat = $1; + } elsif (/^\s*(\w\S*)\s+(.*)/) { + push @{$l{$cat}}, [ $1, $2 ]; + } else { + log::l("_parse_xkb_rules:$lst_file: bad line $_"); + } + } + \%l; +} + +sub default_XkbModel { + my ($keyboard) = @_; + + my $Layout = _keyboard2xkb($keyboard); + + (arch() =~ /sparc/ ? 'sun' : + $Layout eq 'jp' ? 'jp106' : + $Layout eq 'br' ? 'abnt2' : 'pc105'); +} + +sub keyboard2full_xkb { + my ($keyboard) = @_; + + my $Layout = _keyboard2xkb($keyboard) or return { XkbDisable => '' }; + if ($keyboard->{GRP_TOGGLE} && $Layout !~ /,/) { + $Layout = join(',', 'us', $Layout); + } + + my $Model = $keyboard->{XkbModel} || default_XkbModel($keyboard); + + my $Options = join(',', + if_($keyboard->{GRP_TOGGLE}, "grp:$keyboard->{GRP_TOGGLE}", 'grp_led:scroll'), + if_($keyboard->{GRP_TOGGLE} ne 'rwin_toggle', 'compose:rwin'), + ); + + { XkbModel => $Model, XkbLayout => $Layout, XkbOptions => $Options }; +} + +sub _xmodmap_file { + my ($keyboard) = @_; + my $f = "$ENV{SHARE_PATH}/xmodmap/xmodmap.$keyboard->{KEYBOARD}"; + -e $f && $f; +} + +sub _setxkbmap { + my ($keyboard) = @_; + my $xkb = keyboard2full_xkb($keyboard) or return; + run_program::run('setxkbmap', '-option', '') if $xkb->{XkbOptions}; #- need re-initialised other toggles are cumulated + run_program::run('setxkbmap', $xkb->{XkbLayout}, '-model' => $xkb->{XkbModel}, '-option' => $xkb->{XkbOptions} || '', '-compat' => $xkb->{XkbCompat} || ''); +} + +sub setup_install { + my ($keyboard) = @_; + + return if arch() =~ /^sparc/ || $::local_install; + + #- Xpmac does not map keys quite right + if (arch() =~ /ppc/ && !$::testing && $ENV{DISPLAY}) { + log::l("Fixing Mac keyboard"); + run_program::run('xmodmap', "-e", "keycode 59 = BackSpace"); + run_program::run('xmodmap', "-e", "keycode 131 = Shift_R"); + run_program::run('xmodmap', "-e", "add shift = Shift_R"); + return; + } + + my $kmap = keyboard2kmap($keyboard) or return; + + log::l("loading keymap $kmap"); + if (-e (my $f = "$ENV{SHARE_PATH}/keymaps/$kmap.bkmap")) { + _builtin_loadkeys(scalar cat_($f)); + } elsif (-x '/bin/loadkeys') { + run_program::run('loadkeys', $kmap); + } else { + log::l("ERROR: can not load keymap"); + } + + if (-x "/usr/bin/setxkbmap") { + _setxkbmap($keyboard) or log::l("setxkbmap failed"); + } else { + my $f = _xmodmap_file($keyboard); + #- timeout is needed for drakx-in-chroot to kill xmodmap when it gets crazy with: + #- please release the following keys within 2 seconds: Alt_L (keysym 0xffe9, keycode 64) + eval { run_program::raw({ timeout => 3 }, 'xmodmap', $f) } if $f && !$::testing && $ENV{DISPLAY}; + } +} + +sub write { + my ($keyboard) = @_; + log::l("keyboard::write $keyboard->{KEYBOARD}"); + + $keyboard = { %$keyboard }; + put_in_hash($keyboard, keyboard2full_xkb($keyboard)); + delete $keyboard->{unsafe}; + $keyboard->{KEYTABLE} = keyboard2kmap($keyboard); + + setVarsInSh("$::prefix/etc/sysconfig/keyboard", $keyboard); + if (arch() =~ /ppc/) { + my $s = "dev.mac_hid.keyboard_sends_linux_keycodes = 1\n"; + substInFile { + $_ = '' if /^\Qdev.mac_hid.keyboard_sends_linux_keycodes/; + $_ .= $s if eof; + } "$::prefix/etc/sysctl.conf"; + } else { + run_program::rooted($::prefix, 'dumpkeys', '>', '/etc/sysconfig/console/default.kmap') or log::l("dumpkeys failed"); + } + run_program::run('mageia-setup-keyboard'); +} + +sub configure_and_set_standalone { + my ($keyboard) = @_; + + _setxkbmap($keyboard); + run_program::run('loadkeys', keyboard2kmap($keyboard)); + + &write($keyboard); +} + +sub read() { + my %keyboard = getVarsFromSh("$::prefix/etc/sysconfig/keyboard") or return; + if (!$keyboard{KEYBOARD}) { + add2hash(\%keyboard, grep { keyboard2kmap($_) eq $keyboard{KEYTABLE} } _keyboards()); + } + keyboard2text(\%keyboard) && \%keyboard; +} +sub read_or_default() { &read() || default() } + +sub check() { + $^W = 0; + + my $not_ok = 0; + my $warn = sub { + print STDERR "$_[0]\n"; + }; + my $err = sub { + &$warn; + $not_ok = 1; + }; + + if (my @l = grep { is_empty_array_ref(lang2keyboards($_)) } lang::list_langs()) { + $warn->("no keyboard for langs " . join(" ", @l)); + } + foreach my $lang (lang::list_langs()) { + my $l = lang2keyboards($lang); + foreach (@$l) { + 0 <= $_->[1] && $_->[1] <= 100 or $err->("invalid value $_->[1] in $lang2keyboard{$lang} for $lang in \%lang2keyboard keyboard.pm"); + $keyboards{$_->[0]} or $err->("invalid keyboard $_->[0] in $lang2keyboard{$lang} for $lang in \%lang2keyboard keyboard.pm"); + } + } + /SKIP/ || $keyboards{$_} or $err->("invalid keyboard $_ in \@usb2keyboard keyboard.pm") foreach @usb2keyboard; + $usb2keyboard[0x21] eq 'us_SKIP' or $err->('@usb2keyboard is badly modified, 0x21 is not us keyboard'); + + my @xkb_groups = map { if_(/grp:(\S+)/, $1) } cat_('/usr/lib/X11/xkb/rules/xfree86.lst'); + $err->("invalid xkb group toggle '$_' in \%grp_toggles") foreach difference2([ keys %grp_toggles ], \@xkb_groups); + $warn->("unused xkb group toggle '$_'") foreach grep { !/switch/ } difference2(\@xkb_groups, [ keys %grp_toggles ]); + + my @xkb_layouts = (#- (map { (split)[0] } grep { /^! layout/ .. /^\s*$/ } cat_('/usr/lib/X11/xkb/rules/xfree86.lst')), + all('/usr/lib/X11/xkb/symbols'), + (map { (split)[2] } cat_('/usr/lib/X11/xkb/symbols.dir'))); + $err->("invalid xkb layout $_") foreach difference2([ map { _keyboard2xkb($_) } _keyboards() ], \@xkb_layouts); + + my @kmaps_available = map { if_(m|.*/(.*)\.bkmap|, $1) } `tar tfj share/keymaps.tar.bz2`; + my @kmaps_wanted = map { keyboard2kmap($_) } _keyboards(); + $err->("missing KEYTABLE $_ (either share/keymaps.tar.bz2 need updating or $_ is bad)") foreach difference2(\@kmaps_wanted, \@kmaps_available); + $err->("unused KEYTABLE $_ (update share/keymaps.tar.bz2 using share/keymaps_generate)") foreach difference2(\@kmaps_available, \@kmaps_wanted); + + loadkeys_files($err); + + exit($not_ok); +} + +1; diff --git a/lib/mouse.pm b/lib/mouse.pm new file mode 100644 index 0000000..307a5b3 --- /dev/null +++ b/lib/mouse.pm @@ -0,0 +1,677 @@ +package mouse; # $Id: mouse.pm 267529 2010-04-13 13:48:09Z pzanoni $ + +#use diagnostics; +#use strict; + +#-###################################################################################### +#- misc imports +#-###################################################################################### +use common; +use modules; +use detect_devices; +use run_program; +use devices; +use modules; +use any; +use log; + +sub _all_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") ], + [ 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' => + [ [ 'input/mice' ], + [ [ 1, 'ps/2', 'ExplorerPS/2', N_("1 button") ], + [ 2, 'ps/2', 'ExplorerPS/2', N_("Generic 2 Button Mouse") ], + [ 3, 'ps/2', 'ExplorerPS/2', N_("Generic") ], + [ 3, 'ps/2', 'ExplorerPS/2', N_("Generic 3 Button Mouse with Wheel emulation"), 'wheel' ], + [ 5, 'ps/2', 'ExplorerPS/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' ], + ] ], + + N_("Universal") => + [ [ 'input/mice' ], + [ [ 7, 'ps/2', 'ExplorerPS/2', N_("Any PS/2 & USB mice") ], + [ 7, 'ps/2', 'ExplorerPS/2', N_("Force evdev") ], #- evdev is magically handled in mouse::select() + if_(detect_devices::is_xbox(), [ 5, 'ps/2', 'IMPS/2', N_("Microsoft Xbox Controller S") ]), + if_(detect_devices::is_virtualbox(), [ 7, 'ps/2', 'vboxmouse', N_("VirtualBox mouse") ]), + if_(detect_devices::is_vmware(), [ 7, 'ps/2', 'vmmouse', N_("VMware mouse") ]), + ] ], + + N_("none") => + [ [ 'none' ], + [ [ 0, 'none', 'Microsoft', N_("No mouse") ], + ] ], +); +} + +#- Logitech MX700 +#- +#- I: Bus=0003 Vendor=046d Product=c506 Version=1600 +#- N: Name="Logitech USB Receiver" +#- P: Phys=usb-0000:00:11.3-2/input0 +#- S: Sysfs=/class/input/input5 +#- H: Handlers=mouse2 ts2 event3 +#- B: EV=7 +#- B: KEY=ffff0000 0 0 0 0 0 0 0 0 +#- B: REL=103 +#- +#- T: Bus=05 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 4 Spd=1.5 MxCh= 0 +#- D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1 +#- P: Vendor=046d ProdID=c506 Rev=16.00 +#- S: Manufacturer=Logitech +#- S: Product=USB Receiver +#- C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr= 50mA +#- I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid +#- E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=10ms + + +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]} } + +my @mouses_fields = qw(nbuttons MOUSETYPE Protocol name EmulateWheel); + +sub _raw2mouse { + my ($type, $raw) = @_; + $raw or return; + + my %l; @l{@mouses_fields} = @$raw; + +{ %l, type => $type, if_($l{nbuttons} < 3, Emulate3Buttons => 1) }; +} + +sub fullname2mouse { + my ($fname, %opts) = @_; + my ($type, @l) = split '\|', $fname; + my $name = pop @l; #- ensure we get rid of "[Other]" + my %mice = _all_mice(); + + if (my @devices = @{$mice{$type}[0]}) { + member($opts{device}, @devices) or delete $opts{device}; + $opts{device} ||= $devices[0]; + } + foreach (@{$mice{$type}[1]}) { + my $l = _raw2mouse($type, $_); + $name eq $l->{name} and return { %$l, %opts }; + } + die "$fname not found ($type, $name)"; +} + +sub read() { + my %mouse = getVarsFromSh "$::prefix/etc/sysconfig/mouse"; + eval { fullname2mouse($mouse{FULLNAME}, device => $mouse{device}) } || \%mouse; +} + +sub write { + my ($do_pkgs, $mouse) = @_; + + setVarsInSh("$::prefix/etc/sysconfig/mouse", { + device => $mouse->{device}, + MOUSETYPE => $mouse->{MOUSETYPE}, + FULLNAME => qq($mouse->{type}|$mouse->{name}), + }); + + 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 _input_entry_to_device_by_id { + my ($input) = @_; + + my $ID_SERIAL = chomp_(run_program::get_stdout('usb_id', $input->{sysfs_path})); + $ID_SERIAL =~ s/[^\w#+\-.:=@_]/_/g; #- udev do a further cleanup, eg: "Wacom_Co.,Ltd._MTE-450" => "Wacom_Co._Ltd._MTE-450". cf ALLOWED_CHARS udev.h + my $sysfs_device = "input/by-id/usb-$ID_SERIAL-event-mouse"; #- from /etc/udev/rules.d/60-persistent-input.rules + if ($::isInstall || -e "/dev/$sysfs_device") { + $sysfs_device; + } else { + log::l("$sysfs_device missing"); + (); + } +} + +sub _probe_usb_wacom_devices() { + detect_devices::hasWacom() or return; + + eval { modules::load("wacom", "evdev") }; + + map { _input_entry_to_device_by_id($_) } detect_devices::usbWacom(); +} + +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}; + + my $name = 'Generic 2 Button Mouse'; + $name = 'Microsoft IntelliMouse' if $t->{MFG} eq 'MSH' && $t->{MODEL} eq '0001'; + $name = 'Logitech MouseMan' if $t->{MFG} eq 'LGI' && $t->{MODEL} =~ /^80/; + $name = 'Genius NetMouse' if $t->{MFG} eq 'KYE' && $t->{MODEL} eq '0003'; + + $mouse ||= fullname2mouse("serial|$name", device => "ttyS$_"); + last; + } elsif ($t->{CLASS} eq "PEN" || $t->{MANUFACTURER} eq "WAC") { + push @wacom, "ttyS$_"; + } + } + $mouse, @wacom; +} + +sub _mice2evdev { + my (@mice) = @_; + + [ map { + #- we always use HWheelRelativeAxisButtons for evdev, it tells mice with no horizontal wheel to skip those buttons + #- that way we ensure 6 & 7 is always horizontal wheel + #- (cf patch skip-HWheelRelativeAxisButtons-even-if-unused in x11-driver-input-evdev) + { device => "/dev/$_", HWheelRelativeAxisButtons => "7 6" }; + } map { _input_entry_to_device_by_id($_) } @mice ]; +} + +sub _detect_evdev_mice { + my (@mice) = @_; + + foreach (@mice) { + my @l = $_->{usb} && $_->{usb}{driver} =~ /^Mouse:(.*)/ ? split('\|', $1) : (); + foreach my $opt (@l) { + if ($opt eq 'evdev') { + $_->{want_evdev} = 1; + } + } + if ($_->{HWHEEL}) { + $_->{want_evdev} = 1; + } + } + + my @evdev_mice = grep { $_->{want_evdev} } @mice; + + log::l("configuring mice for evdev (" . join(' ', map { "$_->{vendor}:$_->{product}" } @evdev_mice) . ")") if @evdev_mice; + + { evdev_mice_all => _mice2evdev(@mice), + if_(@evdev_mice, evdev_mice => _mice2evdev(@evdev_mice)) }; +} + +sub detect { + my ($modules_conf) = @_; + + # let more USB tablets and touchscreens magically work at install time + # through /dev/input/mice multiplexing: + detect_devices::probe_category('input/tablet'); + detect_devices::probe_category('input/touchscreen'); + + my @wacom = _probe_usb_wacom_devices(); + + $modules_conf->get_probeall("usb-interface") and eval { modules::load('usbhid') }; + if (detect_devices::is_virtualbox()) { + fullname2mouse("Universal|VirtualBox mouse"); + } elsif (detect_devices::is_vmware()) { + fullname2mouse("Universal|VMware mouse"); + } elsif (my @mice = grep { $_->{Handlers}{mouse} } detect_devices::getInputDevices_and_usb()) { + my @synaptics = map { + { ALPS => $_->{ALPS} }; + } grep { $_->{Synaptics} || $_->{ALPS} } @mice; + + my $evdev_opts = _detect_evdev_mice(@mice); + + my $fullname = detect_devices::is_xbox() ? + 'Universal|Microsoft Xbox Controller S' : + arch() eq "ppc" ? + 'USB|1 button' : + 'Universal|Any PS/2 & USB mice'; + + fullname2mouse($fullname, wacom => \@wacom, + synaptics => $synaptics[0], + if_($evdev_opts, %$evdev_opts)); + } elsif (arch() eq 'ppc') { + # 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. + fullname2mouse("busmouse|1 button"); + } elsif (arch() =~ /^sparc/) { + fullname2mouse("sunmouse|Sun - Mouse"); + } else { + #- 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); + } else { + fullname2mouse('Universal|Any PS/2 & USB mice', unsafe => 1); + } + } +} + +sub load_modules { + my ($mouse) = @_; + my @l; + push @l, qw(hid mousedev usbmouse) if $mouse->{type} =~ /USB/; + push @l, qw(serial) if $mouse->{type} =~ /serial/ || any { /ttyS/ } @{$mouse->{wacom}}; + push @l, qw(wacom evdev) if any { /event/ } @{$mouse->{wacom}}; + push @l, qw(evdev) if $mouse->{synaptics} || $mouse->{evdev_mice}; + + eval { modules::load(@l) }; +} + +sub set_xfree_conf { + my ($mouse, $xfree_conf, $b_keep_auxmouse_unchanged) = @_; + + my @mice = map { + { + (member($_->{Protocol}, qw(vboxmouse vmmouse)) ? "Driver" : "Protocol") => $_->{Protocol}, + Device => devices::make($_->{device}), + if_($_->{Emulate3Buttons} || $_->{EmulateWheel}, Emulate3Buttons => undef, Emulate3Timeout => 50), + if_($_->{EmulateWheel}, EmulateWheel => undef, EmulateWheelButton => 2), + }; + } $mouse; + + if ($mouse->{evdev_mice}) { + push @mice, @{$mouse->{evdev_mice}}; + } elsif (!$mouse->{synaptics} && $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 => to_bool(m|input/by-path/event|) } } @wacoms); + } +} + +sub various_xfree_conf { + my ($do_pkgs, $mouse) = @_; + + -e "/usr/bin/X" or return; + + #- we don't need this anymore. Remove it for upgrades + unlink("$::prefix/etc/X11/xinit.d/mouse_buttons"); + { + 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"); + } + } + + require Xconfig::card; + my $inputdrvpath = Xconfig::card::modules_dir() . '/input'; + my $pkgs = [ + if_($mouse->{synaptics}, ['x11-driver-input-synaptics', "$inputdrvpath/synaptics_drv.so"]), + if_($mouse->{evdev_mice}, ['x11-driver-input-evdev', "$inputdrvpath/evdev_drv.so"]), + if_($mouse->{Protocol} eq 'vboxmouse', ['x11-driver-input-vboxmouse', "$inputdrvpath/vboxmouse_drv.so"]), + if_(@{$mouse->{wacom}}, ['x11-driver-input-wacom', "$inputdrvpath/wacom_drv.so"]), + if_($mouse->{name} =~ /VMware/i, ['x11-driver-input-vmmouse', "$inputdrvpath/vmmouse_drv.so"]), + ]; + $do_pkgs->ensure_files_are_installed($pkgs, 1); + +} + +#- 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->{Protocol} : type of the mouse for X : string (eg '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" +sub write_conf { + my ($do_pkgs, $modules_conf, $mouse, $b_keep_auxmouse_unchanged) = @_; + + &write($do_pkgs, $mouse); + $modules_conf->write if $mouse->{device} eq "input/mice" && !$::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 _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; +} + +sub change_mouse_live { + my ($mouse, $old) = @_; + + my $xId = _xmouse2xId($mouse->{Protocol}); + $old->{device} ne $mouse->{device} || $xId != _xmouse2xId($old->{Protocol}) or return; + + log::l("telling X server to use another mouse ($mouse->{Protocol}, $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->{Emulate3Buttons}); + }; + } + 1; +} + +sub test_mouse_install { + my ($mouse, $x_protocol_changed) = @_; + require ugtk2; + ugtk2->import(qw(:wrappers :create)); + my $w = ugtk2->new(N("Testing the mouse"), 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, '', '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 _fullnames() { + map_each { + my $type = $::a; + grep { $_ } map { + if ($_) { + my $l = _raw2mouse($type, $_); + "$type|$l->{name}"; + } else { + $type .= "|[" . N("Other") . "]"; + ''; + } + } @{$::b->[1]}; + } _all_mice(); +} + +sub select { + my ($in, $mouse) = @_; + + my $prev = my $fullname = $mouse->{type} . '|' . $mouse->{name}; + + $in->ask_from_({ messages => N("Please choose your type of mouse."), + title => N("Mouse choice"), + interactive_help_id => 'selectMouse', + if_($mouse->{unsafe}, cancel => ''), + }, + [ { list => [ _fullnames() ], separator => '|', val => \$fullname, + format => sub { join('|', map { translate($_) } split('\|', $_[0])) } } ]) or return; + + if ($fullname ne $prev) { + my $mouse_ = fullname2mouse($fullname, device => $mouse->{device}); + if ($fullname =~ /evdev/) { + $mouse_->{evdev_mice} = $mouse_->{evdev_mice_all} = $mouse->{evdev_mice_all}; + } + %$mouse = %$mouse_; + } + + if ($mouse->{nbuttons} < 3 && $::isStandalone) { + $mouse->{Emulate3Buttons} = $in->ask_yesorno('', N("Emulate third button?"), 1); + } + + if ($mouse->{type} eq 'serial') { + $in->ask_from_({ title => N("Mouse Port"), + messages => N("Please choose which serial port your mouse is connected to."), + interactive_help_id => 'selectSerialPort', + }, [ { list => [ detect_devices::serialPorts() ], format => \&detect_devices::serialPort2text, val => \$mouse->{device} } ]) or return &select; + } + + if (arch() =~ /ppc/ && $mouse->{nbuttons} == 1) { + #- set a sane default F11/F12 + $mouse->{button2_key} = 87; + $mouse->{button3_key} = 88; + $in->ask_from('', N("Buttons emulation"), + [ + { label => N("Button 2 Emulation"), val => \$mouse->{button2_key}, list => [ _ppc_one_button_keys() ], format => \&_ppc_one_button_key2text }, + { label => N("Button 3 Emulation"), val => \$mouse->{button3_key}, list => [ _ppc_one_button_keys() ], format => \&_ppc_one_button_key2text }, + ]) or return; + } + 1; +} + +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($images{mouse}->get_height, 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->{Protocol}, '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<mouse> is a perl module used by mousedrake to detect and configure the mouse. + +=head1 COPYRIGHT + +Copyright (C) 2000-2006 Mandriva <tvignaud@mandriva.com> + +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 diff --git a/lib/xf86misc/Makefile b/lib/xf86misc/Makefile new file mode 100644 index 0000000..a645f67 --- /dev/null +++ b/lib/xf86misc/Makefile @@ -0,0 +1,10 @@ +.PHONY: clean + +main: %: %.xs + test -e Makefile_c || perl Makefile.PL + $(MAKE) -f Makefile_c LD_RUN_PATH= || $(MAKE) -f Makefile_c LD_RUN_PATH= + rm -f ../../auto/xf86misc ; ln -s ../lib/xf86misc/blib/arch/auto ../../auto/xf86misc + +clean: + test ! -e Makefile_c || $(MAKE) -f Makefile_c clean + rm -f *~ *.o diff --git a/lib/xf86misc/Makefile.PL b/lib/xf86misc/Makefile.PL new file mode 100644 index 0000000..0103325 --- /dev/null +++ b/lib/xf86misc/Makefile.PL @@ -0,0 +1,13 @@ +use ExtUtils::MakeMaker; + +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. + +WriteMakefile( + 'NAME' => 'main', + 'MAKEFILE' => 'Makefile_c', + 'OPTIMIZE' => '-Os', + 'VERSION_FROM' => 'main.pm', # finds $VERSION + 'LIBS' => ["-lX11 -lXext -lXxf86misc"], + 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' +); diff --git a/lib/xf86misc/main.pm b/lib/xf86misc/main.pm new file mode 100644 index 0000000..32cd612 --- /dev/null +++ b/lib/xf86misc/main.pm @@ -0,0 +1,12 @@ +package xf86misc::main; # $Id: main.pm 206196 2004-02-08 17:50:15Z prigaux $ + +use strict; +use vars qw($VERSION @ISA); +use DynaLoader; + +use vars qw($VERSION @ISA); +@ISA = qw(DynaLoader); +$VERSION = '0.01'; +xf86misc::main->bootstrap($VERSION); + +1; diff --git a/lib/xf86misc/main.xs b/lib/xf86misc/main.xs new file mode 100644 index 0000000..696f8dd --- /dev/null +++ b/lib/xf86misc/main.xs @@ -0,0 +1,76 @@ +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +#include <X11/Xlib.h> +#include <X11/extensions/xf86misc.h> + +#include <term.h> +#undef max_colors + +void initIMPS2() { + unsigned char imps2_s1[] = { 243, 200, 243, 100, 243, 80, }; + unsigned char imps2_s2[] = { 246, 230, 244, 243, 100, 232, 3, }; + + int fd = open("/dev/mouse", O_WRONLY); + if (fd < 0) return; + + write (fd, imps2_s1, sizeof (imps2_s1)); + usleep (30000); + write (fd, imps2_s2, sizeof (imps2_s2)); + usleep (30000); + tcflush (fd, TCIFLUSH); + tcdrain(fd); +} + +MODULE = xf86misc::main PACKAGE = xf86misc::main + +PROTOTYPES: DISABLE + + +int +Xtest(display) + char *display + CODE: + int pid; + if ((pid = fork()) == 0) { + Display *d = XOpenDisplay(display); + if (d) { + int child; + /* keep a client until some window is created, otherwise X server blinks to hell */ + if ((child = fork()) == 0) { + XEvent event; + XSelectInput(d, DefaultRootWindow(d), SubstructureNotifyMask); + do { + XNextEvent(d, &event); + } while (event.type != CreateNotify); + XCloseDisplay(d); + exit(0); + } + } + _exit(d != NULL); + } + waitpid(pid, &RETVAL, 0); + OUTPUT: + RETVAL + +void +setMouseLive(display, type, emulate3buttons) + char *display + int type + int emulate3buttons + CODE: + { + XF86MiscMouseSettings mseinfo; + Display *d = XOpenDisplay(display); + if (d) { + if (XF86MiscGetMouseSettings(d, &mseinfo) == True) { + mseinfo.type = type; + mseinfo.flags |= MF_REOPEN; + mseinfo.emulate3buttons = emulate3buttons; + XF86MiscSetMouseSettings(d, &mseinfo); + XFlush(d); + if (type == MTYPE_IMPS2) initIMPS2(); + } + } + } |