#!/usr/bin/perl -w
# this reassigns the mice and keyboards into a deterministic order
## Copyright 2004 Michael Pardee copyright@digitalride.net
## 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 of the License, 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
## A copy of the GNU General Public License can be found at /usr/share/groovix/COPYING

open (ID,"/proc/bus/input/devices") || die "can't open /proc/bus/input/devices for reading\n";

$mouseprecount=1;
$kbprecount=1;


$kbi=0;
while (<ID>) {
	if ( /^P:\s+Phys=(.*)/ ) {
		$last_phys=$1;
	}
	if ( /N:\s+Name=\"(.*)\"/) {
		if (/peaker/) { 
			#print "$_ found speaker\n";
			$last_type="speaker";
		} elsif (/Button/i) { 
			#print "$_ found button\n";
			$last_type="button";
		} elsif (/emulation/i) { 
			#print "$_ found emulation\n";
			$last_type="emulation";
		} elsif (/beep/i) { 
			#print "$_ found emulation\n";
			$last_type="beep";
		} elsif (/ideo/i) { 
			#print "$_ found emulation\n";
			$last_type="video";
		} elsif ((/AT Translated/) && ($kbprecount > $mouseprecount)) {
			#print "$_ found PS2 keyboard, but using USB instead\n";
			$last_type="ps2kb";
		} else { 
			#print "$_ found keyboard\n";
			$last_type="keyboard";
		};
	}
	if ( /^H:\s+Handlers=(.*)/ ) {
		#$all_handlers=$1;
		@handlers=split(/\s+/,"$1");
		$last_kbd_handler=0;
		$last_mouse_handler=0;
		foreach $handler (@handlers) {
			if ( $last_type eq "keyboard") {
				if ($handler =~/kbd/) {
					$handler.=$kbi;
					$kbi++;
					$last_kbd_handler=$handler;
				} 
				#check all_handlers for weird keyboards that also have a false mouse handler
				#&&  !($all_handlers =~/kbd/))  {
				#override for weird mice that also have a keyboard handler!! how to tell which?
				if  (($handler =~/mouse/)) {
					$last_mouse_handler=$handler;
				}
				if ( ($last_kbd_handler && !($handler =~/mouse/)) || ($last_mouse_handler)) {
					#print "storing devs for $last_mouse_handler $handler\n";
				$devs{$handler}=$last_phys;
				#print " $last_kbd_handler or $last_mouse_handler for $handler on $all_handlers\n";
				#print "$handler $last_phys\n";
				if ($handler =~/event/ && $last_mouse_handler) {
					#print "storing evs for $last_mouse_handler $handler\n";
					$evs{$last_mouse_handler}=$handler;
				}
				if ($handler =~/event/ && $last_kbd_handler) {
					$evs{$last_kbd_handler}=$handler;
				}
				}
			}
		}
	}
}
$kbi=0;
$mi=0;
foreach $key (sort by_hash_val (keys(%devs))) {
	if ($key =~ /kbd/ ) {
		# for second virtual device on each keyboard (usually multimedia keys),
		#	make it go to same place as first device
		if ( $devs{$key}=~/input1$/ ) { # second virtual keyboard
			#$devprint=$devs{$key};
			#$devprint=~s/\//\\\//g;
			$devprint=$evs{$key};
			#ignore for finding main keyboard# print "s/GVMUextendedkb$lastkbi/$devprint/g\n";
		} else {
			#$devprint=$devs{$key};
			#$devprint=~s/\//\\\//g;
			$devprint=$evs{$key};
			#print "s/GVMUkeyboard$kbi/$devprint/g\n";
			print "/dev/input/$devprint\n";
			$lastkbi=$kbi;
			#$kbi+=2;
			$kbi++;
		}
		#$lastkbi=$kbi;
		#$lastdev=$devs{$key};
	}	
	if ($key =~ /mouse/ ) {
		#print "mouse key is $key\n";
		#print "s/GVMUmouse$mi/$evs{$key}/g\n";
		$mi++;
	}	
	#print "$key $devs{$key}\n";
}

sub by_hash_val {
	$devs{$a} cmp $devs{$b};
}
#$numkb=($kbi+1)/2;
#$numkb=$kbi;
#$numm=$mi;
#print `mkdir -p /var/lib/groovix/`;
#print "s/GVMUnummice/$numm/g\n";
#print `echo $numm >  /var/lib/groovix/number_of_mice`;
#print "s/GVMUnumkb/$numkb/g\n";
#print `echo $numkb >  /var/lib/groovix/number_of_keyboards`;

