#!/bin/bash
#
# This script reports the MT-capable devices with a driver present in
# your system
#

pushd . > /dev/null
cd /sys/class/input

typeset -i native_bits=`getconf LONG_BIT`
typeset -i native_nibbles=$native_bits/4

for d in event*; do
    dev=/dev/input/$d
    name=`cat $d/device/name`
    abs_words=`cat $d/device/capabilities/abs`
    typeset -i bit=0

    # for all native words in absmask
    for abs in $abs_words; do
	hex=`printf %${native_nibbles}s $abs`
	typeset -i i=0

        # collect bits into mask array
	while [ $i -lt $native_nibbles ]; do
	    valS=${hex:$i:1}
	    val10=$[0x$valS]
	    if [ "X$val10" = "X" ]; then
		valBS="0000"
	    else
		valBS=`echo "ibase=10; obase=2; $val10" | bc`
	    fi	    
	    valB=`printf %4s "$valBS"`
	    typeset -i j=0
	    while [ $j -lt 4 ]; do
		typeset -i mask[$bit]=${valB:$j:1}
		j=$j+1
		bit=$bit+1
	    done
	    i=$i+1
	done
    done

    # reverse order of bits
    typeset -i i=0
    typeset -i j=$bit-1
    while [ $i -lt $j ]; do
	typeset -i tmp=${mask[$i]}
	mask[$i]=${mask[$j]}
	mask[$j]=$tmp
	i=$i+1
	j=$j-1
    done

    typeset -i has_mt=${mask[0x35]}
    if [ $has_mt = 1 ]; then
	if [ "X$dev_detected" = "X" ]; then
		dev_detected=1
		echo "Found device(s):" >&2
	fi
	echo $name: $dev
    fi
done
if [ "X$dev_detected" = "X" ]; then
	echo "No MT-capable device found..." >&2
fi

popd > /dev/null
