#!/bin/sh
######################
# Gestures v1.1
######################

# we have 10 gestures + 10 dedicated for app
#
# first four
# pinch + up
# pinch + down
# pinch + left
# pinch + right
#
# second four
# stretch + up
# stretch + down
# stretch + left
# stretch + right
#
# long tap
# pinch + down + up    - emergency assistance :)


usleep 1100000	# -  how long wait for end of gesture

if [ $(pidof xtestlab126 | wc -w) == "1" ]; then	changemode=10	# set 'changemode' for particular app
else												changemode=0
fi

#if [ $(pidof app | wc -w) == "1" ]; then	changemode=20	# set 'changemode' for particular app
#else										changemode=0
#fi

# 3 - up; 4 - down; 5 - left; 6 - right; 7 - pinch; 8 - stretch

run=`/usr/bin/awk -v ch=$changemode '
BEGIN {pinch=0; stretch=0; third=0}
{
	if ($1==7) pinch=1 					# -  7 can occur many times
	if ($1==3 && third==1) print 999+ch 
	if ($1==4 && pinch==1) third=1
	if ($1==8) stretch=1				# -  8 can occur many times
}
END{
	if (pinch==1){
		if ($1==3 && third==0) print 1+ch ;
		if ($1==5) print 3+ch ;
		if ($1==6) print 4+ch ;
	}
	if (stretch==1){
		if ($1==3) print 5+ch ;
		if ($1==4) print 6+ch ;
		if ($1==5) print 7+ch ;
		if ($1==6) print 8+ch ;
	}
	if ($1==4 && third==1) print 2+ch
	if ($1==9) print 9+ch
}' /tmp/gesture`

if [ "$run" != "" ] && [ "$run" != "9" ]; then	aplay ding1.wav	&>/dev/null &	# beep from chess
fi	

ext=/mnt/us/extensions

case "$run" in

	# -  pinch + up
	1)
	$ext/gestures/bin/graphicnotes start & 
	$ext/gestures/bin/graphickill & ;;
#	/mnt/us/bin/dict;;

	# -  pinch + down
	2)
	if [ $(pidof leafpad) ]; then killall leafpad	# -  when it's running, kill it
	else $ext/leafpad/bin/leafpad.sh &
	fi;;
	
	# -  pinch + left
	3)
	$ext/kterm/bin/kterm.sh 1 &;;
	
	# -  pinch + right
	4)
	$ext/kterm/bin/kterm.sh &;;

	# -  stretch + up
	5)
	v=`/usr/bin/lipc-get-prop -i com.lab126.audio Volume`
	/usr/bin/lipc-set-prop -i com.lab126.audio Volume $(($v + 1)) ;;
	
	# -  stretch + down
	6)
	v=`/usr/bin/lipc-get-prop -i com.lab126.audio Volume`
	/usr/bin/lipc-set-prop -i com.lab126.audio Volume $(($v - 1)) ;;

	# -  stretch + left
	7)
	/mnt/us/koreader/koreader.sh /mnt/us/documents & ;;
	
	# -  stretch + right
	8)
	$ext/midori/runit.sh &;;

	# -  long tap
	9)
	;;

	# -  pinch + down + up
	999)
	restart framework &;;

	# -  xt pinch + up
	11)
	$ext/gestures/bin/graphicnotes info & ;;	

	# -  xt pinch + down
	12)
	$ext/gestures/bin/graphicnotes show & ;;	

	# -  xt pinch + left
	13)
	$ext/gestures/bin/graphicnotes next 1 & ;;	

	# -  xt pinch + right
	14)
	$ext/gestures/bin/graphicnotes prev 1 & ;;

	# -  xt stretch + up
	15)
	$ext/gestures/bin/graphicnotes next 100 & ;;

	# -  xt stretch + down
	16)
	$ext/gestures/bin/graphicnotes background & ;;	# work only in portrait background

	# -  xt stretch + left
	17)
	$ext/gestures/bin/graphicnotes next 5 & ;;

	# -  xt stretch + right
	18)
	$ext/gestures/bin/graphicnotes prev 5 & ;;

	# -  xt long tap
	19)
	$ext/gestures/bin/graphicnotes tap &
	 ;;

	# -  xt long tap
	1009)
	$ext/gestures/bin/graphicnotes review &
	 ;;

esac

/bin/rm /tmp/gesture.lock
echo "" > /tmp/gesture


