summaryrefslogtreecommitdiff
path: root/mdm/src/discover-devices
blob: 55bf6be54c95c4be85b672a21ae75dec68cb0c6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash

# Copyright (C) 2004-2007 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This script discover evdev mice and keyboards and also discovers video cards
# on the machine.
# For input devices, it uses /proc/bus/input/devices
# For video cardes, it uses the "discover" package.

# TODO: we should stop using /proc and "discover". We should use HAL and X!

PROC_DEVICES=/proc/bus/input/devices
DISCOVER=/sbin/discover

# This function prints the physical addresses of the mice found
function discover_input () {

    # The elements of the list should contain a whole line of the file, so to
    # make the attribution work, the line can't contain spaces
    # Also, there are lines like "^P: $", and if we just grep them and cut -f2,
    # they will become empty, and will mess with our lists 

    NUMBER_OF_DEVICES=$(grep '^I:' $PROC_DEVICES | wc -l)
    for(( i = 1; i <= ${NUMBER_OF_DEVICES}; i++ )); do
	PHYS_ADDRS[i]=$(grep '^P:' $PROC_DEVICES | \
	                head -n $i | tail -n 1 | cut -d'=' -f2)
	HANDLERS[i]=$(  grep '^H:' $PROC_DEVICES | \
	                head -n $i | tail -n 1 | cut -d'=' -f2)
	NAMES[i]=$(     grep '^N:' $PROC_DEVICES | \
	                head -n $i | tail -n 1 | cut -d'=' -f2)
    done

    for (( i = 1; i <= ${#PHYS_ADDRS[@]}; i++ )); do

	#echo "PHYS_ADDR: ${PHYS_ADDRS[i]}"
	#echo "HANDLER:   ${HANDLERS[i]}"
	#echo "NAME:      ${NAMES[i]}"
	#echo "--"

	EVDEV_NODE="/dev/input/`echo ${HANDLERS[i]}| tr ' ' '\n'| grep "event"`"

	# XXX: "Speaker" should be the "PCSpeaker" device and "Button" should be
	# all those power buttons and machintosh buttons (not mice!)

	# A mouse has the physical address ending in "0" and its handler
	# contains "mouse".
        if echo ${HANDLERS[i]}   | grep -q "mouse"  && \
	   echo ${PHYS_ADDRS[i]} | grep -q ".*0$"   && \
	   echo ${NAMES[i]}      | egrep -q -v "(Speaker|Button)"; then

            echo -e "mouse\t${PHYS_ADDRS[i]}"
	    echo -e "mevdev\t${EVDEV_NODE}"
        fi

	# A keyboard has a "kbd" handler, its physical address ends in 0 and it
	# doesn't have the "Speaker" or "Button" words on its name.
        if echo ${HANDLERS[i]}   | grep -q "kbd"    && \
           echo ${PHYS_ADDRS[i]} | grep -q ".*0$"   && \
	   echo ${NAMES[i]}      | egrep -q -v "(Speaker|Button)"; then

	    echo -e "kbd\t${PHYS_ADDRS[i]}"
	    echo -e "kevdev\t${EVDEV_NODE}"
  
        fi
    done

    # if no mouse found, put a serial mouse as default 
    #if [[ ${#PHYS_ADDRS[@]} == 0 ]]; then
    #    echo -e "mouse\tserial0"
    #fi
}

# Prints bus address and drivers of the video cards.
function video_cards () {

    # calling discover is way toooooo slow!
    OUTPUT=$($DISCOVER -t display --vendor-id --model-id)
    VENDOR_IDS=($(echo "$OUTPUT" | cut -d' ' -f1))
    MODEL_IDS=($( echo "$OUTPUT" | cut -d' ' -f2))
    # There can be empty lines on "drivers"
    DRIVERS=($($DISCOVER -t display --data-path=xfree86/server/device/driver \
	       | sed 's/^$/vesa/g'))

    for (( i = 0; i < ${#VENDOR_IDS[@]}; i++)); do
        # If there are multiple cards with the same IDs, lspci will print
	# multiple lines

	# See how many times we already used these IDs:
	TIMES_USED=0
	for (( j=0; j < i; j++)); do
	    if ( [ "${VENDOR_IDS[j]}" = "${VENDOR_IDS[i]}" ] &&
	         [ "${MODEL_IDS[j]}"  = "${MODEL_IDS[i]}"  ] ); then
		 TIMES_USED=$((TIMES_USED+1))
	    fi
	done

	BUS_IDS[i]=$(lspci -d ${VENDOR_IDS[i]}:${MODEL_IDS[i]} | 
		     cut -d' ' -f1 | head -n $((TIMES_USED+1)) | tail -n 1)
    done

    for (( i=0 ; i < ${#BUS_IDS[@]}; i++ )) ; do
        # busid from lspci is in format 00:00.00
        # below we split in 00 and 00.00
        NUMS=(`echo ${BUS_IDS[$i]} |  \
              awk 'BEGIN {FS=":"}{print toupper($1), toupper($2)}'`)
	# now, we split 00.00 in 00 and 00
        SEC_NUMS=(`echo ${NUMS[1]} |  \
                  awk 'BEGIN {FS="."}{print toupper($1), toupper($2)}'`)
	# now, we convert the numbers from hexa to decimal base 
        echo -e "bus\t`echo "obase=10;ibase=16;${NUMS[0]};${SEC_NUMS[0]};${SEC_NUMS[1]};" | bc | paste -s -d":"`"

    done
    
    for i in ${DRIVERS[@]}; do
        echo -e "driver\t$i"
    done
} # video_cards

# ******************** MAIN *************************

if [[ "$#" = 0 ]]
then
    ARG=all  
else
    ARG=$1
fi


case $ARG in
    all)
        CARDS=`video_cards`
        INPUT=`discover_input`
        echo "$INPUT" | grep mouse
        echo "$INPUT" | grep mevdev
        echo "$INPUT" | grep kbd
        echo "$INPUT" | grep kevdev
        echo "$CARDS"
        ;;
    all2)
	CARDS=`video_cards`
	INPUT=`discover_input`
        echo "$INPUT"
	echo "$CARDS"
	;;

    mouse|mevdev|kbd|kevdev)
	INPUT=`discover_input`
        echo "$INPUT" | grep $ARG
        ;;
    bus|driver)
	CARDS=`video_cards`
        echo "$CARDS" | grep $ARG
        ;;
    cards)
	CARDS=`video_cards`
        echo "$CARDS"
        ;;

    #debug)
    #    echo "$INPUT" | egrep "(PHYS|HANDLER|NAME|--)"
    #	;;
    *)
        echo "$1: unrecognized argument"
	echo "Valid parameters: "
	echo "mouse:  prints physical addresses of the mice"
	echo "mevdev: prints the evdev mouse events"
	echo "kbd:    prints physical addresses of the keyboards"
	echo "kevdev: prints the evdev keyboard events"
	echo "driver: prints the video card drivers"
	echo "cards:  prints the video card bus IDs"
	echo "all:    prints everything"
        ;;
esac