1684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos# Capture and display input events and coordinates
2684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos#
3684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos# Usage: ./capture.sh
4684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos#
5684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
6684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos# do a throw-away adb in case the server is out-of-date
7684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosadb devices -l 2>&1 >/dev/null
8684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
9ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjoswhile [ $# -gt 0 ]
10ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjosdo
11ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	case "$1" in
12ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	(-d) DEVICE=$2; shift;;
13ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	(*)
14ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos		echo Unknown option $1
15ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos		exit 1;;
16ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	esac
17ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	shift
18ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjosdone
19684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
20ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjosif [ "$DEVICE" = "" ]; then
21ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	devInfo=$(adb devices -l | grep -v ^List | head -1)
22ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	set -- $devInfo
23ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	echo devInfo=$devInfo
24ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos	DEVICE=$(echo $4 | sed 's/product://')
25ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjosfi
26684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
27684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosfunction convert {
28684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	in=$1
29684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	max=$2
30684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	scale=$3
31684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	if [ $max -eq 0 ]; then
32684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos		echo $in
33684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	else
34684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos		((out=in*scale/max))
35684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos		echo $out
36684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	fi
37684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos}
38684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
39684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
40684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjoscase $DEVICE in
41ae1f93cc3cef9c8c790330dd09f2ff87bf38eabcTodd Kjos(shamu|hammerhead|bullhead)
42684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	# no scaling necessary
43684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	xmax=0
44684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	ymax=0;;
45684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos(volantis)
46684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	xmax=3060
47684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	xscale=1500
48684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	ymax=2304
49684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	yscale=1950;;
50684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos(*)
51684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	echo "Error: No display information available for $DEVICE"
52684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	exit 1;;
53684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosesac
54684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos
55684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosecho Capturing input for $DEVICE...
56684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosstdbuf -o0 adb shell getevent -t |
57684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	stdbuf -o0 grep "event.: 0003" |
58684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	stdbuf -o0 grep "0003 003[0156a9]" |
59684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	stdbuf -o0 tr ':[]
60684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos' ' ' | while read line
61684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjosdo
62684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	set -- $line
63684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	code=$4
64684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	value=$((16#$5))
65684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	case $code in
66684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(0035) x=$(convert $value $xmax $xscale);;
67684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(0036) y=$(convert $value $ymax $yscale);;
68684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(0030) tag="majorTouch";;
69684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(0031) tag="minorTouch";;
70684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(003a) tag="pressure";;
71684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(0039) tag="trackingId";;
72684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	(--) echo unknown code=$code;;
73684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	esac
74684f65f8602d1e1a79170984acc18421fb8f4c98Todd Kjos	printf "%-10s %-4d  %-4d\n" $tag $x $y
75done
76