package install_steps_gtk; # $Id$ use diagnostics; use strict; use vars qw(@ISA); @ISA = qw(install_steps_interactive interactive_gtk); #-###################################################################################### #- misc imports #-###################################################################################### use install_steps_interactive; use interactive_gtk; use common qw(:common :file :functional :system); use my_gtk qw(:helpers :wrappers); use Gtk; use devices; use modules; use install_gtk; use install_any; use mouse; use help; use log; #-###################################################################################### #- In/Out Steps Functions #-###################################################################################### sub new($$) { my ($type, $o) = @_; my $old = $SIG{__DIE__}; $SIG{__DIE__} = sub { $_[0] !~ /my_gtk\.pm/ and goto $old }; $ENV{DISPLAY} ||= $o->{display} || ":0"; unless ($::testing) { $my_gtk::force_focus = $ENV{DISPLAY} eq ":0"; my $f = "/tmp/Xconf"; install_gtk::createXconf($f, @{$o->{mouse}}{"XMOUSETYPE", "device"}, $o->{wacom}); devices::make("/dev/kbd"); if ($ENV{DISPLAY} eq ":0") { local (*T1, *T2); open T1, ">/dev/tty5"; open T2, ">/dev/tty6"; my $launchX = sub { my $ok = 1; local $SIG{CHLD} = sub { $ok = 0 if waitpid(-1, c::WNOHANG()) > 0 }; unless (fork) { exec $_[0], (arch() =~ /^sparc/ || arch() eq "ppc" ? () : ("-kb")), "-dpms","-s" ,"240", ($_[0] =~ /Xsun/ || $_[0] =~ /Xpmac/ ? ("-fp", "/usr/X11R6/lib/X11/fonts:unscaled") : ("-allowMouseOpenFail", "-xf86config", $f)) or exit 1; } foreach (1..60) { sleep 1; log::l("Server died"), return 0 if !$ok; return 1 if c::Xtest($ENV{DISPLAY}); } log::l("Timeout!!"); 0; }; my @servers = qw(FBDev VGA16); #-) if (arch() eq "alpha") { require Xconfigurator; my $card = Xconfigurator::cardConfigurationAuto(); Xconfigurator::updateCardAccordingName($card, $card->{type}) if $card && $card->{type}; @servers = $card->{server} || "TGA"; #-@servers = qw(SVGA 3DLabs TGA) } elsif (arch() =~ /^sparc/) { local $_ = cat_("/proc/fb"); if (/Mach64/) { @servers = qw(Mach64) } elsif (/Permedia2/) { @servers = qw(3DLabs) } else { @servers = qw(Xsun24) } } elsif (arch() eq "ppc") { @servers = qw(Xpmac); } foreach (@servers) { log::l("Trying with server $_"); my $dir = "/usr/X11R6/bin"; my $prog = /Xsun|Xpmac/ ? $_ : "XF86_$_"; unless (-x "$dir/$prog") { unlink $_ foreach glob_("$dir/X*"); install_any::getAndSaveFile("Mandrake/mdkinst$dir/$prog", "$dir/$prog") or die "failed to get server $prog: $!"; chmod 0755, "$dir/$prog"; } if (/FB/) { !$o->{vga16} && $o->{allowFB} or next; $o->{allowFB} = &$launchX($prog) #- keep in mind FB is used. and goto OK; } else { $o->{vga16} = 1 if /VGA16/; &$launchX($prog) and goto OK; } } return undef; } } OK: install_gtk::init_sizes(); install_gtk::default_theme($o); install_gtk::create_logo_window($o); $my_gtk::force_center = [ $::rootwidth - $::windowwidth, $::logoheight, $::windowwidth, $::windowheight ]; (bless {}, ref $type || $type)->SUPER::new($o); } sub enteringStep { my ($o, $step) = @_; printf "Entering step `%s'\n", $o->{steps}{$step}{text}; $o->SUPER::enteringStep($step); install_gtk::create_steps_window($o); install_gtk::create_help_window($o); } sub leavingStep { my ($o, $step) = @_; $o->SUPER::leavingStep($step); } #-###################################################################################### #- Steps Functions #-###################################################################################### sub selectLanguage { my ($o, $first_time) = @_; $o->SUPER::selectLanguage; Gtk->set_locale; install_gtk::install_theme($o); $o->ask_warn('', _("Your system is low on resource. You may have some problem installing Linux-Mandrake. If that occurs, you can try a text install instead. For this, press `F1' when booting on CDROM, then enter `text'.")) if $first_time && availableRamMB() < 60; # 60MB } #------------------------------------------------------------------------------ sub selectInstallClass1 { my ($o, $verif, $l, $def, $l2, $def2) = @_; $::live || @$l == 1 and return $o->SUPER::selectInstallClass1($verif, $l, $def, $l2, $def2); my $w = my_gtk->new(''); my ($radio, $focused); gtkadd($w->{window}, gtkpack($o->create_box_with_title(_("Please, choose one of the following classes of installation:")), (my @radios = map { $radio = new Gtk::RadioButton($_, $radio ? $radio : ()); $radio->set_active($_ eq $def); $radio } @$l), gtkadd(create_hbox(), map { my $v = $_; my $b = new Gtk::Button(translate($_)); $focused = $b if $_ eq $def2; gtksignal_connect($b, "clicked" => sub { $w->{retval} = $v; Gtk->main_quit }); } @$l2) )); $focused->grab_focus if $focused; $w->main; mapn { $verif->($_[1]) if $_[0]->active } \@radios, $l; install_gtk::create_steps_window($o); $w->{retval}; } #------------------------------------------------------------------------------ sub selectMouse { my ($o, $force) = @_; my %old = %{$o->{mouse}}; $o->SUPER::selectMouse($force); my $mouse = $o->{mouse}; $old{type} eq $mouse->{type} && $old{name} eq $mouse->{name} && $old{device} eq $mouse->{device} && !$force and return; local $my_gtk::grab = 1; #- unsure a crazy mouse don't go wild clicking everywhere while (1) { log::l("telling X server to use another mouse"); eval { modules::load('serial') } if $mouse->{device} =~ /ttyS/; if (!$::testing) { symlinkf($mouse->{device}, "/dev/mouse"); c::setMouseLive($ENV{DISPLAY}, mouse::xmouse2xId($mouse->{XMOUSETYPE}), $mouse->{nbuttons} < 3); } install_gtk::test_mouse($mouse) and return; $o->SUPER::selectMouse(1); $mouse = $o->{mouse}; } } #------------------------------------------------------------------------------ sub chooseSizeToInstall { my ($o, $packages, $min_size, $max_size_, $availableC, $individual) = @_; my $max_size = min($max_size_, $availableC); my $enough = $max_size == $max_size_; my $percentage = int 100 * $max_size / $max_size_; #- don't ask anything if the difference between min and max is too small return $max_size if $min_size && $max_size / $min_size < 1.05; log::l("choosing size to install between $min_size and $max_size"); my $w = my_gtk->new(''); my $adj = create_adjustment($percentage, $min_size * 100 / $max_size_, $percentage); my $spin = gtkset_usize(new Gtk::SpinButton($adj, 0, 0), 20, 0); my $val; require pkgs; gtkadd($w->{window}, gtkpack(new Gtk::VBox(0,20), _("The total size for the groups you have selected is approximately %d MB.\n", pkgs::correctSize($max_size_ / sqr(1024))) . ($enough ? _("If you wish to install less than this size, select the percentage of packages that you want to install. A low percentage will install only the most important packages; a percentage of 100%% will install all selected packages.") : _("You have space on your disk for only %d%% of these packages. If you wish to install less than this, select the percentage of packages that you want to install. A low percentage will install only the most important packages; a percentage of %d%% will install as many packages as possible.", $percentage, $percentage)) . ($individual ? "\n\n" . _("You will be able to choose them more specifically in the next step.") : ''), create_packtable({}, [ _("Percentage of packages to install") . ' ', $spin, "%", my $mb = new Gtk::Label ], [ undef, new Gtk::HScrollbar($adj) ], ), create_okcancel($w) ) ); $spin->signal_connect(changed => my $changed = sub { $val = $spin->get_value_as_int / 100 * $max_size_; $mb->set(sprintf("(%dMB)", pkgs::correctSize($val / sqr(1024)))); }); &$changed(); $spin->signal_connect(activate => sub { $w->{retval} = 1; Gtk->main_quit }); $spin->grab_focus(); $w->main and $val + 1; #- add a single byte (hack?) to make selection of 0 bytes ok. } sub choosePackagesTree { my ($o, $packages, $compss) = @_; $o->set_help('choosePackagesTree'); my ($curr, $parent, $info_widget, $w_size, $go, $idle, $flat, $auto_deps); my (%wtree, %ptree); my $w = my_gtk->new(''); my $details = new Gtk::VBox(0,0); my $tree = Gtk::CTree->new(3, 0); $tree->set_selection_mode('browse'); $tree->set_column_width(0, 200); $tree->set_column_auto_resize($_, 1) foreach 1..2; gtkadd($w->{window}, gtkpack_(new Gtk::VBox(0,5), 0, _("Choose the packages you want to install"), 1, gtkpack(new Gtk::HBox(0,0), createScrolledWindow($tree), gtkadd(gtkset_usize(new Gtk::Frame(_("Info")), $::windowwidth - 490, 0), createScrolledWindow($info_widget = new Gtk::Text), )), 0, my $l = new Gtk::HBox(0,15), 0, gtkpack(new Gtk::HBox(0,10), $go = gtksignal_connect(new Gtk::Button(_("Install")), "clicked" => sub { $w->{retval} = 1; Gtk->main_quit }), ) )); gtkpack__($l, my $toolbar = new Gtk::Toolbar('horizontal', 'icons')); gtkpack__($l, gtksignal_connect(new Gtk::CheckButton(_("Automatic dependencies")), clicked => sub { invbool \$auto_deps })); $l->pack_end($w_size = new Gtk::Label(''), 0, 1, 20); $w->{window}->set_usize(map { $_ - 2 * $my_gtk::border - 4 } $::windowwidth, $::windowheight); $go->grab_focus; $w->{rwindow}->show_all; my $pix_base = [ gtkcreate_xpm($w->{window}, "$ENV{SHARE_PATH}/rpm-base.xpm") ]; my $pix_selected = [ gtkcreate_xpm($w->{window}, "$ENV{SHARE_PATH}/rpm-selected.xpm") ]; my $pix_unselect = [ gtkcreate_xpm($w->{window}, "$ENV{SHARE_PATH}/rpm-unselected.xpm") ]; my $pix_semisele = [ gtkcreate_xpm($w->{window}, "$ENV{SHARE_PATH}/rpm-semiselected.xpm") ]; my $pix_installed= [ gtkcreate_xpm($w->{window}, "$ENV{SHARE_PATH}/rpm-installed.xpm") ]; my $add_parent; $add_parent = sub { $_[0] or return undef; if (my $w = $wtree{$_[0]}) { return $w } my $s; foreach (split '/', $_[0]) { my $s2 = $s ? "$s/$_" : $_; $wtree{$s2} ||= do { my $n = $tree->insert_node($s ? $add_parent->($s) : undef, undef, [$_, '', ''], 5, (undef) x 4, 0, 0); $n; }; $s = $s2; } $tree->node_set_pixmap($wtree{$s}, 1, $pix_semisele->[0], $pix_semisele->[1]); $wtree{$s}; }; my $add_node = sub { my ($leaf, $root) = @_; my $p = $packages->[0]{$leaf} or return; $p->{medium}{selected} or return; my $node = $tree->insert_node($add_parent->($root), undef, [$leaf, '', ''], 5, (undef) x 4, 1, 0); my $pix = pkgs::packageFlagBase($p) ? $pix_base : pkgs::packageFlagSelected($p) ? $pix_selected : pkgs::packageFlagInstalled($p) ? $pix_installed : $pix_unselect; $tree->node_set_pixmap($node, 1, $pix->[0], $pix->[1]); push @{$ptree{$leaf}}, $node; }; my $add_nodes = sub { %ptree = %wtree = (); $tree->freeze; while (1) { $tree->remove_node($tree->node_nth(0) || last) } my ($root, $leaf); if ($flat = $_[0]) { $add_node->($_, undef) foreach sort grep { my $pkg = pkgs::packageByName($packages, $_); $pkg->{medium}{selected} } keys %{$packages->[0]}; } else { foreach (sort @$compss) { ($root, $leaf) = m|(.*)/(.+)|o or ($root, $leaf) = ('', $_); my $pkg = pkgs::packageByName($packages, $leaf); $add_node->($leaf, $root) if $pkg->{medium}{selected}; } } $tree->thaw; }; $add_nodes->($flat); my %toolbar = my @toolbar = ( ftout => [ _("Expand Tree") , sub { $tree->expand_recursive(undef) } ], ftin => [ _("Collapse Tree") , sub { $tree->collapse_recursive(undef) } ], reload=> [ _("Toggle between flat and group sorted"), sub { $add_nodes->(!$flat) } ], ); $toolbar->set_button_relief("none"); foreach (grep_index { $::i % 2 == 0 } @toolbar) { gtksignal_connect($toolbar->append_item(undef, $toolbar{$_}[0], undef, gtkxpm($tree, "$ENV{SHARE_PATH}/$_.xpm")), clicked => $toolbar{$_}[1]); } $toolbar->set_style("icons"); my $display_info = sub { my $p = $packages->[0]{$curr} or return gtktext_insert($info_widget, ''); pkgs::extractHeaders($o->{prefix}, [$p], $p->{medium}); $p->{header} or die; my $ind = $o->{compssListLevels}{$o->{installClass}}; my $imp = translate($pkgs::compssListDesc{pkgs::packageFlagBase($p) ? 100 : round_down($p->{values}[$ind], 10)}); gtktext_insert($info_widget, $@ ? _("Bad package") : _("Name: %s\n", pkgs::packageName($p)) . _("Version: %s\n", pkgs::packageVersion($p) . '-' . pkgs::packageRelease($p)) . _("Size: %d KB\n", pkgs::packageSize($p) / 1024) . ($imp && _("Importance: %s\n", $imp)) . "\n" . formatLines(c::headerGetEntry($p->{header}, 'description'))); c::headerFree(delete $p->{header}); 0; }; my $update_size = sub { my $size = pkgs::selectedSize($packages); $w_size->set(_("Total size: %d / %d MB", pkgs::correctSize($size / sqr(1024)), install_any::getAvailableSpace($o) / sqr(1024))); }; my $select = sub { my %l; my $isSelection = !pkgs::packageFlagSelected($_[0]); foreach (@_) { pkgs::togglePackageSelection($packages, $_, my $l = {}); @l{grep {$l->{$_}} keys %$l} = (); } if (my @l = keys %l) { #- check for size before trying to select. my $size = pkgs::selectedSize($packages); foreach (@l) { my $p = $packages->[0]{$_}; pkgs::packageFlagSelected($p) or $size += pkgs::packageSize($p); } if (pkgs::correctSize($size / sqr(1024)) > install_any::getAvailableSpace($o) / sqr(1024)) { return $o->ask_warn('', _("You can't select this package as there is not enough space left to install it")); } @l > @_ && !$auto_deps and $o->ask_okcancel('', [ $isSelection ? _("The following packages are going to be installed") : _("The following packages are going to be removed"), join(", ", common::truncate_list(20, sort @l)) ], 1) || return; $isSelection ? pkgs::selectPackage($packages, $_) : pkgs::unselectPackage($packages, $_) foreach @_; foreach (@l) { my $p = $packages->[0]{$_}; my $pix = pkgs::packageFlagSelected($p) ? $pix_selected : $pix_unselect; $tree->node_set_pixmap($_, 1, $pix->[0], $pix->[1]) foreach @{$ptree{$_}}; } &$update_size; } else { $o->ask_warn('', _("You can't select/unselect this package")); } }; my $children = sub { map { $packages->[0]{($tree->node_get_pixtext($_, 0))[0]} } gtkctree_children($_[0]) }; my $toggle = sub { if (ref $curr && ! $_[0]) { $tree->toggle_expansion($curr); } else { if (ref $curr) { my @l = grep { !pkgs::packageFlagBase($_) } $children->($curr) or return; my @unsel = grep { !pkgs::packageFlagSelected($_) } @l; my @p = @unsel ? @unsel : # not all is selected, select all @l; $select->(@p); $parent = $curr; } else { my $p = $packages->[0]{$curr} or return; if (pkgs::packageFlagBase($p)) { return $o->ask_warn('', _("This is a mandatory package, it can't be unselected")); } elsif (pkgs::packageFlagInstalled($p)) { return $o->ask_warn('', _("You can't unselect this package. It is already installed")); } elsif (pkgs::packageFlagUpgrade($p)) { if ($::expert) { if (pkgs::packageFlagSelected($p)) { $o->ask_yesorno('', _("This package must be upgraded\nAre you sure you want to deselect it?")) or return; } } else { return $o->ask_warn('', _("You can't unselect this package. It must be upgraded")); } } $select->($p); } if (my @l = $children->($parent)) { my $nb = grep { pkgs::packageFlagSelected($_) } @l; my $pix = $nb==0 ? $pix_unselect : $nb<@l ? $pix_semisele : $pix_selected; $tree->node_set_pixmap($parent, 1, $pix->[0], $pix->[1]); } } }; $tree->signal_connect(key_press_event => sub { my ($w, $e) = @_; my $c = chr($e->{keyval} & 0xff); $toggle->(0) if $e->{keyval} >= 0x100 ? $c eq "\r" || $c eq "\x8d" : $c eq ' '; 1; }); $tree->signal_connect(tree_select_row => sub { Gtk->timeout_remove($idle) if $idle; if ($_[1]->row->is_leaf) { ($curr) = $tree->node_get_pixtext($_[1], 0); $parent = $_[1]->row->parent; $idle = Gtk->timeout_add(100, $display_info); } else { $curr = $_[1]; } $toggle->(1) if $_[2] == 1; }); &$update_size; $w->main; } #------------------------------------------------------------------------------ sub installPackages { my ($o, $packages) = @_; my ($current_total_size, $last_size, $nb, $total_size, $start_time, $last_dtime, $trans_progress_total); my $w = my_gtk->new(_("Installing")); $w->{window}->set_usize($::windowwidth * 0.8, 260); my $text = new Gtk::Label; my ($msg, $msg_time_remaining, $msg_time_total) = map { new Gtk::Label($_) } '', (_("Estimating")) x 2; my ($progress, $progress_total) = map { new Gtk::ProgressBar } (1..2); gtkadd($w->{window}, gtkpack(new Gtk::VBox(0,10), _("Please wait, "), $msg, $progress, create_packtable({}, [_("Time remaining "), $msg_time_remaining], [_("Total time "), $msg_time_total], ), $text, $progress_total, '', gtkadd(create_hbox(), my $cancel = new Gtk::Button(_("Cancel"))), )); $w->sync; $msg->set(_("Preparing installation")); gtkset_mousecursor_normal($cancel->window); $cancel->signal_connect(clicked => sub { $pkgs::cancel_install = 1 }); my $oldInstallCallback = \&pkgs::installCallback; local *pkgs::installCallback = sub { my $m = shift; if ($m =~ /^Starting installation/) { $nb = $_[0]; $total_size = $_[1]; $current_total_size = 0; $start_time = time(); $msg->set(_("%d packages", $nb)); $w->flush; } elsif ($m =~ /^Starting installing package/) { $progress->update(0); my $name = $_[0]; $msg->set(_("Installing package %s", $name)); $current_total_size += $last_size; $last_size = c::headerGetEntry($o->{packages}[0]{$name}{header}, 'size'); $text->set((split /\n/, c::headerGetEntry($o->{packages}[0]{$name}{header}, 'summary'))[0] || ''); $w->flush; } elsif ($m =~ /^Progressing installing package/) { $progress->update($_[2] ? $_[1] / $_[2] : 0); my $dtime = time() - $start_time; my $ratio = $total_size ? ($_[1] + $current_total_size) / $total_size : 0; my $total_time = $ratio ? $dtime / $ratio : time(); $progress_total->update($ratio); if ($dtime != $last_dtime && $current_total_size > 10 * 1024 * 1024) { $msg_time_total->set(formatTime(10 * round($total_time / 10))); $msg_time_remaining->set(formatTime(10 * round(max($total_time - $dtime, 0) / 10))); $last_dtime = $dtime; } $w->flush; } else { unshift @_, $m; goto $oldInstallCallback } }; #- the modification is not local as the box should be living for other package installation. undef *install_any::changeMedium; *install_any::changeMedium = sub { my ($method, $medium) = @_; my $msg = _("Change your Cd-Rom! Please insert the Cd-Rom labelled \"%s\" in your drive and press Ok when done. If you don't have it, press Cancel to avoid installation from this Cd-Rom.", pkgs::mediumDescr($o->{packages}, $medium)); #- if not using a cdrom medium, always abort. $method eq 'cdrom' and do { local $my_gtk::grab = 1; print "\a"; $o->ask_okcancel('', $msg); }; }; catch_cdie { $o->install_steps::installPackages($packages); } sub { if ($@ =~ /^error ordering package list: (.*)/) { $o->ask_yesorno('', [ _("There was an error ordering packages:"), $1, _("Go on anyway?") ], 1) and return 1; ${$_[0]} = "already displayed"; } elsif ($@ =~ /^error installing package list: (.*)/) { $o->ask_yesorno('', [ _("There was an error installing packages:"), $1, _("Go on anyway?") ], 1) and return 1; ${$_[0]} = "already displayed"; } 0; }; if ($pkgs::cancel_install) { $pkgs::cancel_install = 0; die "setstep choosePackages\n"; } $w->destroy; } sub set_help { my ($o, @l) = @_; $o->{current_help} = formatAlaTeX(join "\n", map { _ deref($help::steps{$_}) } @l); gtktext_insert($o->{help_window_text}, $o->{current_help}); 1; } #-###################################################################################### #- Wonderful perl :( #-###################################################################################### 1; # href='#n436'>436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
package detect_devices;

use diagnostics;
use strict;
use vars qw($pcitable_addons $usbtable_addons);

#-######################################################################################
#- misc imports
#-######################################################################################
use log;
use MDK::Common; # help perl_checker
use common;
use devices;
use run_program;
use modules;
use c;
use feature 'state';


=head1 SYNOPSYS

The B<detect_devices> modules offers a high level API for detecting devices.*
It mostly relies on the L<c> modules for gluing  libldetect back into the Perl world, and thus
being able to enumerate DMI/HID/PCI/USB devices.

Other devices are mostly detected through C</proc> & C</sys>.

Then the L<list_modules> enables to map modules into categories such as:

=over 4

=item * C<network/ethernet,>

=item * C<network/wireless,>

=item * C<network/wifi,>

=item * C<disk/sata,>

=item * C<disk/scsi,>

=item * ...

=back

This enables to detect a category by mapping drivers to categories.

=head1 Listing block devices

=cut

#-#####################################################################################
#- Globals
#-#####################################################################################
my %serialprobe;

#-######################################################################################
#- Functions
#-######################################################################################

=over

=item get()

Returns a list of all block devices (IDE, SCSI, SATA, virtual, RAID, ...)

=cut

sub get() {
    #- Detect the default BIOS boot harddrive is kind of tricky. We may have IDE,
    #- SCSI and RAID devices on the same machine. From what I see so far, the default
    #- BIOS boot harddrive will be
    #- 1. The first IDE device if IDE exists. Or
    #- 2. The first SCSI device if SCSI exists. Or
    #- 3. The first RAID device if RAID exists.

    getIDE(), getSCSI(), getMmcBlk(), getNVMe(), getXenBlk(), getVirtIO(), getDAC960(), getCompaqSmartArray();
}
sub hds()         { grep { may_be_a_hd($_) } get() }
sub tapes()       { grep { $_->{media_type} eq 'tape' } get() }
sub cdroms()      { grep { $_->{media_type} eq 'cdrom' } get() }
sub burners()     { grep { isBurner($_) } cdroms() }
sub dvdroms()     { grep { isDvdDrive($_) } cdroms() }
sub raw_zips()    { grep { member($_->{media_type}, 'fd', 'hd') && isZipDrive($_) } get() }
sub ls120s()      { grep { member($_->{media_type}, 'fd', 'hd') && isLS120Drive($_) } get() }
sub zips()        {
    map { 
	$_->{device} .= 4; 
	$_;
    } raw_zips();
}

sub floppies {
    my ($o_not_detect_legacy_floppies) = @_;
    require modules;
    state @fds;
    state $legacy_already_detected;

    $o_not_detect_legacy_floppies = 1 if arch() =~ /arm/;

    if (!$o_not_detect_legacy_floppies && !$legacy_already_detected) {
        $legacy_already_detected = 1;
        if ($::isInstall) {
            eval { modules::load("floppy") };
            system(qw(udevadm settle)) if !$@;
        }
        #- do not bother probing /dev/fd0 and loading floppy device uselessly,
        #- it takes time and it is already done by boot process (if not in install):
        #-   /dev/fd0 is created by start_udev (/etc/udev/devices.d/default.nodes)
        #-   then hal probes /dev/fd0 and triggers floppy module loading through kernel's kmod
        if (any { (split)[1] eq 'fd' } cat_("/proc/devices")) {
            @fds = map {
                my $info = c::floppy_info("/dev/fd$_");
                if_($info && $info ne '(null)', { device => "fd$_", media_type => 'fd', info => $info });
            } qw(0 1);
        }
    }
        
    my @ide = ls120s() and eval { modules::load("ide_floppy") };

    eval { modules::load("usb_storage") } if $::isInstall && usbStorage();
    my @scsi = grep { $_->{media_type} eq 'fd' } getSCSI();
    @ide, @scsi, @fds;
}
sub floppies_dev() { map { $_->{device} } floppies() }
sub floppy() { first(floppies_dev()) }
#- example ls120, model = "LS-120 SLIM 02 UHD Floppy"

sub removables() {
    floppies(), cdroms(), zips();
}

sub get_sys_cdrom_info {
    my (@drives) = @_;

    my @drives_order;
    foreach (cat_("/proc/sys/dev/cdrom/info")) {
	my ($t, $l) = split ':';
	my @l;
	@l = split(' ', $l) if $l;
	if ($t eq 'drive name') {
	    @drives_order = map {
		my $dev = $_;
		find { $_->{device} eq $dev } @drives;
	    } @l;
	} else {
	    my $capacity;
	    if ($t eq 'Can write CD-R') {
		$capacity = 'burner';
	    } elsif ($t eq 'Can read DVD') {
		$capacity = 'DVD';
	    }
	    if ($capacity) {
		each_index {
		    ($drives_order[$::i] || {})->{capacity} .= "$capacity " if $_;
		} @l;
	    }
	}
    }
}

sub complete_usb_storage_info {
    my (@l) = @_;

    my @usb = grep { exists $_->{usb_vendor} } @l;

    foreach my $usb (usb_probe()) {
	if (my $e = find { !$_->{found} && $_->{usb_vendor} == $usb->{vendor} && $_->{usb_id} == $usb->{id} } @usb) {
         my $host = get_sysfs_usbpath_for_block($e->{device});
         if ($host) {
             my $file = "/sys/block/$host/../serial";
             $e->{info} = chomp_(cat_($file)) if -e $file;
             $e->{usb_description} = join('|', 
                                          chomp_(cat_("/sys/block/$host/../manufacturer")),
                                          chomp_(cat_("/sys/block/$host/../product")));
         }
         local $e->{found} = 1;
	    $e->{"usb_$_"} ||= $usb->{$_} foreach keys %$usb;
	}
    }
}

sub isBurner { 
    my ($e) = @_;
    $e->{capacity} =~ /burner/ and return 1;
      
    #- do not work for SCSI
    my $f = tryOpen($e->{device}); #- SCSI burner are not detected this way.
    $f && c::isBurner(fileno($f));
}
sub isDvdDrive {
    my ($e) = @_;
    $e->{capacity} =~ /DVD/ || $e->{info} =~ /DVD/ and return 1;

    #- do not work for SCSI
    my $f = tryOpen($e->{device});
    $f && c::isDvdDrive(fileno($f));
}
sub isZipDrive { $_[0]{info} =~ /ZIP\s+\d+/ } #- accept ZIP 100, untested for bigger ZIP drive.
sub isLS120Drive { $_[0]{info} =~ /LS-?120|144MB/ }
sub isKeyUsb { begins_with($_[0]{usb_media_type} || '', 'Mass Storage') && $_[0]{media_type} eq 'hd' }
sub isFloppyUsb { $_[0]{usb_driver} && $_[0]{usb_driver} eq 'Removable:floppy' }
sub may_be_a_hd { 
    my ($e) = @_;
    $e->{media_type} eq 'hd' && !(
	isZipDrive($e) 
           || isLS120Drive($e)
           || begins_with($e->{usb_media_type} || '', 'Mass Storage|Floppy (UFI)')
    );
}

sub get_sysfs_field_from_link {
    my ($device, $field) = @_;
    my $l = readlink("$device/$field");
    $l =~ s!.*/!!;
    $l;
}

sub get_sysfs_usbpath_for_block {
    my ($device) = @_;
    my $host = readlink("/sys/block/$device");
    $host =~ s!/host.*!!;
    $host;
}

sub get_scsi_driver {
    my (@l) = @_;
    # find driver of host controller from sysfs:
    foreach (@l) {
	next if $_->{driver};
	my $host = get_sysfs_usbpath_for_block($_->{device});
	require list_modules;
	$_->{driver} = list_modules::filename2modname(get_sysfs_field_from_link("/sys/block/$host", 'driver'));
    }
}

=item getSCSI()

Returns a list of all SCSI device.

=cut

sub getSCSI() {
    my $dev_dir = '/sys/bus/scsi/devices';

    my @scsi_types = (
	"Direct-Access",
	"Sequential-Access",
	"Printer",
	"Processor",
	"WORM",
	"CD-ROM",
	"Scanner",
	"Optical Device",
	"Medium Changer",
	"Communications",
    );

    my @l;
    foreach (all($dev_dir)) {
	my ($host, $channel, $id, $lun) = split ':';
	defined $lun or next;

	my $dir = "$dev_dir/$_";

	# handle both old and new kernels:
	my $node =  find { -e $_ } "$dir/block", top(glob_("$dir/block*")), "$dir/tape", top(glob_("$dir/scsi_generic*"));
	my ($device) = readlink($node) =~ m!/(?:scsi_(?:generic|tape)|block)/(.*)!;
        if (!$device) {
            ($device) = top(glob_("$node/*")) =~ m!/(?:scsi_(?:generic|tape)|block)/(.*)!;
        }
	warn("cannot get info for device ($_)"), next if !$device;

	my $usb_dir = readlink($dir) =~ m!/usb! && "$dir/../../../..";
	my $get_usb = sub { chomp_(cat_("$usb_dir/$_[0]")) };

	my $get = sub {
	    my $s = cat_("$dir/$_[0]");
	    $s =~ s/\s+$//;
	    $s;
	};

	# Old HP scanners report themselves as "Processor"s
	# (see linux/include/scsi/scsi.h and sans-find-scanner.1)
	my $raw_type = $scsi_types[$get->('type')];

	my $media_type = ${{ st => 'tape', sr => 'cdrom', sd => 'hd', sg => 'generic' }}{substr($device, 0, 2)} ||
	  $raw_type =~ /Scanner|Processor/ && 'scanner';

	my ($vendor, $model) = ($get->('vendor'), $get->('model'));
	my ($v, $m) = _get_hd_vendor($model);
	if ($v && $m) {
            ($vendor, $model) = ($v, $m);
	}
	push @l, { info =>  $vendor . ' ' . $model, host => $host, channel => $channel, id => $id, lun => $lun, 
	  description => join('|', $vendor, $model),
	  bus => 'SCSI', media_type => $media_type, device => $device,
	    $usb_dir ? (
	  usb_vendor => hex($get_usb->('idVendor')), usb_id => hex($get_usb->('idProduct')),
	    ) : (),
        };
    } 

    @l = sort { $a->{host} <=> $b->{host} || $a->{channel} <=> $b->{channel} || $a->{id} <=> $b->{id} || $a->{lun} <=> $b->{lun} } @l;

    complete_usb_storage_info(@l);

    foreach (@l) {
	$_->{media_type} = 'fd' if $_->{media_type} eq 'hd' && isFloppyUsb($_);
    }

    get_sys_cdrom_info(@l);
    get_scsi_driver(@l);
    @l;
}


my %hd_vendors = (
    "ASUS" => "Asus",
    "ATA Maxtor" => "Maxtor",
    "CD-ROM CDU" => "Sony",
    "CD-ROM Drive/F5D" => "ASUSTeK",
    "Compaq" => "Compaq",
    "CONNER" => "Conner Peripherals",
    "IBM" => "IBM",
    "INTEL" => "Intel",
    "FUJITSU" => "Fujitsu",
    "HITACHI" => "Hitachi",
    "Lite-On" => "Lite-On Technology Corp.",
    "LITE-ON" => "Lite-On Technology Corp.",
    "LTN" => "Lite-On Technology Corp.",
    "IOMEGA" => "Iomega",
    "MAXTOR" => "Maxtor",
    "Maxtor" => "Maxtor",
    "Micropolis" => "Micropolis",
    "Pioneer" => "Pioneer",
    "PLEXTOR" => "Plextor",
    "QUANTUM" => "Quantum", 
    "SAMSUNG" => "Samsung",
    "Seagate " => "Seagate Technology",
    "ST3" => "Seagate Technology",
    "TEAC" => "Teac",
    "TOSHIBA" => "Toshiba",
    "WDC" => "Western Digital Corp.",
);

# return ($vendor, $model)
sub _get_hd_vendor {
    my ($info) = @_;
    foreach my $name (keys %hd_vendors) {
        next if !$name;
        return ($hd_vendors{$name}, $2) if $info =~ /^$name(-|\s)*(.*)/;
    }
    return ("Hitachi", $info) if $info =~ /^HD[ST][0-9]/;
}

=item getIDE()

Returns a list of all IDE device.

=cut

sub getIDE() {
    my @idi;

    #- what about a system with absolutely no IDE on it, like some sparc machine.
    -e "/proc/ide" or return ();

    #- Great. 2.2 kernel, things are much easier and less error prone.
    foreach my $d (sort @{[glob_('/proc/ide/hd*')]}) {
	my $t = chomp_(cat_("$d/media"));
	my $type = ${{ disk => 'hd', cdrom => 'cdrom', tape => 'tape', floppy => 'fd' }}{$t} or next;
	my $info = chomp_(cat_("$d/model")) || "(none)";

	my $num = ord(($d =~ /(.)$/)[0]) - ord 'a';
	my ($vendor, $model) = _get_hd_vendor($info);

	my $host = $num;
	($host, my $id) = divide($host, 2);
	($host, my $channel) = divide($host, 2);

	push @idi, { media_type => $type, device => basename($d), 
		     info => $info, host => $host, channel => $channel, id => $id, bus => 'ide', 
		     if_($vendor, Vendor => $vendor), if_($model, Model => $model) };
    }
    get_sys_cdrom_info(@idi);
    @idi;
}

sub block_devices() {
    -d '/sys/block' 
      ? map { s|!|/|; $_ } all('/sys/block') 
      : map { $_->{dev} } do { require fs::proc_partitions; fs::proc_partitions::read_raw() };
}

=item getCompaqSmartArray()

Returns a list of all CCISS devices (Compaq Smart Array).

=cut

sub getCompaqSmartArray() {
    my (@idi, $f);

    foreach ('array/ida', 'cpqarray/ida', 'cciss/cciss') {
	my $prefix = "/proc/driver/$_";
	my ($name) = m|/(.*)|;
	for (my $i = 0; -r ($f = "${prefix}$i"); $i++) {
	    my @raw_devices = cat_($f) =~ m|^\s*($name/.*?):|gm;

	    #- this is ugly and buggy. keeping it for 2007.0
	    #- on a cciss, cciss/cciss0 didn't contain c0d0, but cciss/cciss1 did contain c0d1
	    #- the line below adds both c0d0 and c0d1 for cciss0, and so some duplicates
	    @raw_devices or @raw_devices = grep { m!^$name/! } block_devices();

	    foreach my $raw_device (@raw_devices) {
		my $device = -d "/dev/$raw_device" ? "$raw_device/disc" : $raw_device;
		push @idi, { device => $device, prefix => $raw_device . 'p', 
			     info => "Compaq RAID logical disk",
			     media_type => 'hd', bus => $name };
	    }
	}
    }
    #- workaround the buggy code above. this should be safe though
    uniq_ { $_->{device} } @idi;
}

sub getDAC960() {
    my %idi;

    #- We are looking for lines of this format:DAC960#0:
    #- /dev/rd/c0d0: RAID-7, Online, 17928192 blocks, Write Thru0123456790123456789012
    foreach (syslog()) {
	my ($device, $info) = m|/dev/(rd/.*?): (.*?),| or next;
	$idi{$device} = { info => $info, media_type => 'hd', device => $device, bus => 'dac960' };
    }
    values %idi;
}

=item getXenBlk()

Returns a list of all Xen block devices (C</dev/xvd*>).

=cut

sub getXenBlk() {
    -d '/sys/bus/xen/devices' or return;
    map {   
            s/block://;
            { device => basename($_), info => "Xen block device", media_type => 'hd', bus => 'xen' };
    } glob("/sys/block/xvd*");
}

=item getVirtIO()

Returns a list of all VirtIO block devices (/dev/C<vd*>).

=cut

sub getVirtIO() {
    -d '/sys/bus/virtio/devices' or return;
    map {
            { device => basename($_), info => "VirtIO block device", media_type => 'hd', bus => 'virtio' };
    }
    glob("/sys/bus/virtio/devices/*/block/*");
}

sub getMmcBlk() {
    -d '/sys/bus/mmc/devices' or return;
    map {
            { device => basename($_), info => "MMC block device", media_type => 'hd', bus => 'mmc' };
    }
    glob("/sys/bus/mmc/devices/*/block/*");
}

sub getNVMe() {
    -d '/sys/class/nvme' or return;
    map {
            { device => basename($_), info => "NVMe block device", media_type => 'hd', bus => 'pci_express' };
    }
    glob("/sys/block/nvme*");
}

=item getCPUs()

Returns a list of all CPUs.

=cut

sub getCPUs() { 
    my (@cpus, $cpu);
    foreach (cat_("/proc/cpuinfo")) {
	   if (/^processor/) { # ix86 specific
		  push @cpus, $cpu if $cpu;
		  $cpu = {};
	   }
	   $cpu->{$1} = $2 if /^([^\t]+).*:\s(.*)$/;
	   $cpu->{processor}++ if $1 eq "processor";
    }
    push @cpus, $cpu;
    @cpus;
}

sub ix86_cpu_frequency() {
    cat_('/proc/cpuinfo') =~ /cpu MHz\s*:\s*(\d+)/ && $1;
}

=item probe_category($category)

Returns a list of devices which drivers are in the asked category. eg:

   my @eth_cards = probe_category('network/ethernet');

=cut

sub probe_category {
    my ($category) = @_;

    require list_modules;
    my @modules = list_modules::category2modules($category);

    grep {
	if ($category eq 'network/isdn') {
	    my $b = $_->{driver} =~ /ISDN:([^,]*),?([^,]*)(?:,firmware=(.*))?/;
	    if ($b) {
                $_->{driver} = $1;
                $_->{type} = $2;
                $_->{type} =~ s/type=//;
                $_->{firmware} = $3;
                $_->{driver} eq "hisax" and $_->{options} .= " id=HiSax";
	    }
	    $b;
	} else {
	    member($_->{driver}, @modules);
	}
    } probeall();
}

sub getSoundDevices() {
    probe_category('multimedia/sound');
}

sub isTVcardConfigurable { member($_[0]{driver}, qw(bttv cx88 saa7134)) }

sub getTVcards() { probe_category('multimedia/tv') }

sub getInputDevices() {
    my (@devices, $device);
    my $event;
    foreach (cat_('/proc/bus/input/devices')) {
        if (/^I:/) {
            $device = {};
            $device->{vendor} = /Vendor=(\w+)/ && $1;
            $device->{id} = /Product=(\w+)/ && $1;
            $device->{bustype} = /Bus=(\w+)/ && $1;
        } elsif (/N: Name="(.*)"/) {
	    my $descr = $1;
	    $device->{description} = "|$descr";

	    #- I: Bus=0011 Vendor=0002 Product=0008 Version=7321
	    #- N: Name="AlpsPS/2 ALPS GlidePoint"
	    #- P: Phys=isa0060/serio1/input0
	    #- H: Handlers=mouse1 event2 ts1
	    #- B: EV=f
	    #- B: KEY=420 0 70000 0 0 0 0 0 0 0 0 #=> BTN_LEFT BTN_RIGHT BTN_MIDDLE BTN_TOOL_FINGER BTN_TOUCH
	    #-    or B: KEY=420 0 670000 0 0 0 0 0 0 0 0 #=> same with BTN_BACK
	    #- B: REL=3       #=> X Y
	    #- B: ABS=1000003 #=> X Y PRESSURE

	    #- I: Bus=0011 Vendor=0002 Product=0008 Version=2222
	    #- N: Name="AlpsPS/2 ALPS DualPoint TouchPad"
	    #- P: Phys=isa0060/serio1/input0
	    #- S: Sysfs=/class/input/input2
	    #- H: Handlers=mouse1 ts1 event2 
	    #- B: EV=f
	    #- B: KEY=420 0 70000 0 0 0 0 0 0 0 0
	    #- B: REL=3
	    #- B: ABS=1000003

	    #- I: Bus=0011 Vendor=0002 Product=0007 Version=0000
	    #- N: Name="SynPS/2 Synaptics TouchPad"
	    #- P: Phys=isa0060/serio1/input0
	    #- S: Sysfs=/class/input/input1
	    #- H: Handlers=mouse0 event1 ts0
	    #- B: EV=b
	    #- B: KEY=6420 0 70000 0 0 0 0 0 0 0 0 #=> BTN_LEFT BTN_RIGHT BTN_MIDDLE BTN_TOOL_FINGER BTN_TOUCH BTN_TOOL_DOUBLETAP BTN_TOOL_TRIPLETAP
	    #-    or B: KEY=6420 0 670000 0 0 0 0 0 0 0 0  #=> same with BTN_BACK
	    #-    or B: KEY=420 30000 670000 0 0 0 0 0 0 0 0 #=> same without BTN_TOOL_TRIPLETAP but with BTN_B
	    #- B: ABS=11000003 #=> X Y PRESSURE TOOL_WIDTH

	    #- I: Bus=0003 Vendor=056a Product=0065 Version=0108
	    #- N: Name="Wacom Bamboo"
	    #- B: KEY=1c63 0 70033 0 0 0 0 0 0 0 0 #=> BTN_0 BTN_1 BTN_4 BTN_5 BTN_LEFT BTN_RIGHT BTN_MIDDLE TOOL_PEN TOOL_RUBBER TOOL_BRUSH TOOL_FINGER TOOL_MOUSE TOUCH STYLUS STYLUS2
            #- B: ABS=100 3000103 #=> X Y WHEEL PRESSURE DISTANCE MISC

	    #- I: Bus=0003 Vendor=056a Product=0011 Version=0201
	    #- N: Name="Wacom Graphire2 4x5"
            #- B: KEY=1c43 0 70000 0 0 0 0 0 0 0 0 #=> BTN_LEFT BTN_RIGHT BTN_MIDDLE TOOL_PEN TOOL_RUBBER TOOL_FINGER TOOL_MOUSE TOUCH STYLUS STYLUS2
            #- B: ABS=100 3000003 #=> X Y PRESSURE DISTANCE MISC

            #I: Bus=0003 Vendor=049f Product=0024 Version=0001
	    #N: Name="Compaq Compaq Numeric Keypad"
	    #P: Phys=usb-0000:00:03.2-2/input0
	    #S: Sysfs=/class/input/input7
	    #H: Handlers=kbd event3
	    #B: EV=120003
	    #B: KEY=10000 7 ff800000 7ff febeffdf ffefffff ffffffff fffffffe
	    #B: LED=1f

	    $device->{Synaptics} = $descr eq 'SynPS/2 Synaptics TouchPad';
	    $device->{ALPS} = $descr =~ m!^AlpsPS/2 ALPS!;
	    $device->{Elantech} = $descr eq 'ETPS/2 Elantech Touchpad';
	    $device->{Numpad} = $descr =~ /Numeric Keypad/;

	} elsif (/H: Handlers=(.*)/) {
	    my @l = split(' ', $1);
	    $device->{driver} = $l[0]; #- keep it for compatibility
	    $device->{Handlers} = +{ map { (/^(.*?)\d*$/ ? $1 : $_, $_) } split(' ', $1) };
	    $event = $device->{Handlers}{event};
	} elsif (/S: Sysfs=(.+)/) {
	    $device->{sysfs_path} = $1;
	} elsif (/P: Phys=(.*)/) {
            $device->{location} = $1;
            $device->{bus} = 'isa' if $device->{location} =~ /^isa/;
            $device->{bus} = 'usb' if $device->{location} =~ /^usb/i;
	} elsif (/B: REL=(.* )?(.*)/) {
	    #- REL=3   #=> X Y
	    #- REL=103 #=> X Y WHEEL
	    #- REL=143 #=> X Y HWHEEL WHEEL
	    #- REL=1c3 #=> X Y HWHEEL DIAL WHEEL
	    my $REL = hex($2);
	    $device->{HWHEEL} = 1 if $REL & (1 << 6);
	    $device->{WHEEL} = 1 if $REL & (1 << 8); #- not reliable ("Mitsumi Apple USB Mouse" says REL=103 and KEY=1f0000 ...)

	} elsif (/B: KEY=(\S+)/) {	   
	    #- some KEY explained:
	    #- (but note that BTN_MIDDLE can be reported even if missing)
	    #- (and "Mitsumi Apple USB Mouse" reports 1f0000)
	    #- KEY=30000 0 0 0 0 0 0 0 0  #=> BTN_LEFT BTN_RIGHT
	    #- KEY=70000 0 0 0 0 0 0 0 0  #=> BTN_LEFT BTN_RIGHT BTN_MIDDLE
	    #- KEY=1f0000 0 0 0 0 0 0 0 0 #=> BTN_LEFT BTN_RIGHT BTN_MIDDLE BTN_SIDE BTN_EXTRA
	    if (!$> && ! -f "/dev/input/$event") {
		    devices::make("/dev/input/$event");
	    }
	    if (-r "/dev/input/$event") {
		my @KEYS = c::EVIocGBitKey("/dev/input/$event");
		$device->{SIDE} = 1 if $KEYS[0] & (1 << 0x13);
	    } else {
		my $KEY = hex($1);
		$device->{SIDE} = 1 if $KEY & (1 << 0x13);
	    }

        } elsif (/^\s*$/) {
	    push @devices, $device if $device;
	    undef $device;
	}
    }
    @devices;
}

sub getInputDevices_and_usb() {
    my @l = getInputDevices();

    foreach my $usb (usb_probe()) {
	if (my $e = find { hex($_->{vendor}) == $usb->{vendor} && hex($_->{id}) == $usb->{id} } @l) {
	    $e->{usb} = $usb;
	}
    }

    @l;
}

sub serialPorts() { map { "ttyS$_" } 0..7 }
sub serialPort2text {
    $_[0] =~ /ttyS(\d+)/ ? "$_[0] / COM" . ($1 + 1) : $_[0];
}

=back

=head1 Network

=over

=cut

sub getSerialModem {
    my ($_modules_conf, $o_mouse) = @_;
    my $mouse = $o_mouse || {};
    $mouse->{device} = readlink "/dev/mouse";
    my $serdev = "serial";

    eval { modules::load($serdev) };

    my @modems;

    probeSerialDevices();
    foreach my $port (serialPorts()) {
	next if $mouse->{device} =~ /$port/;
     my $device = "/dev/$port";
	next if !-e $device || !hasModem($device);
     $serialprobe{$device}{device} = $device;
     push @modems, $serialprobe{$device};
    }
    my @devs = pcmcia_probe();
    foreach my $modem (@modems) {
        foreach (@devs) { $_->{device} and $modem->{device} = $_->{device} }
    }
    @modems;
}

=item getModem()