Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created April 11, 2012 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livibetter/2362120 to your computer and use it in GitHub Desktop.
Save livibetter/2362120 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Playing sounds by tapping
# Copyright (c) 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Clip: http://youtu.be/HrSgK9pe0Ko
#
# Dependencies:
#
# * synclient with SHMConfig enabled.
# * mplayer
trap quit SIGINT
synclient TouchpadOff=1
SOUNDS=(
/usr/share/sounds/error.wav
/usr/share/sounds/email.wav
/usr/share/sounds/question.wav
/usr/share/sounds/info.wav
/usr/share/sounds/gtk-events/clicked.wav
/usr/share/sounds/gtk-events/toggled.wav
)
quit()
{
echo
synclient TouchpadOff=0
exit 0
}
clear_cells()
{
echo -e "\e[$(( YCELLS + 1 ))A"
}
print_cells()
{
local x y idx
for (( y = 0; y < YCELLS; y++ )); do
for (( x = 0; x < XCELLS; x++ )); do
(( idx = x + y * XCELLS ))
if [[ $1 == $idx ]]; then
echo -ne '\e[1;31m◾\e[0m'
else
echo -ne '\e[1;32m◽\e[0m'
fi
echo -n ' '
done
echo
done
}
echo "Press Enter and move your finger to top-left corner and bottom-right corner as much as you can within 5 seconds..."
read
LeftEdge=$(( 1<<31 ))
TopEdge=$(( 1<<31 ))
RightEdge=0
BottomEdge=0
while read _time x y z f w l r u d m multi gl gm gr gdx gdy; do
# Skip heading line
[[ $_time == "time" ]] && continue
# Timeout after 5 seconds
(( ${_time%.*} >= 5 )) && break
(( x < LeftEdge )) && LeftEdge=$x
(( y < TopEdge )) && TopEdge=$y
(( x > RightEdge )) && RightEdge=$x
(( y > BottomEdge )) && BottomEdge=$y
echo -ne "\e[2K\e[0GLeft Top Right Bottom: $LeftEdge $TopEdge $RightEdge $BottomEdge"
done < <(synclient -m 1)
XCELLS=3
YCELLS=2
(( xd = (RightEdge - LeftEdge) / 3 ))
(( yd = (BottomEdge - TopEdge) / 2 ))
echo
echo
echo 'Press Ctrl+C to exit.'
echo
print_cells
state=0
while read _time x y z f w l r u d m multi gl gm gr gdx gdy; do
# Skip heading line
[[ $_time == "time" ]] && continue
case "$state" in
0)
if (( f > 0 )); then
(( x < LeftEdge || x > RightEdge || y < TopEdge || y > BottomEdge )) && continue
(( idx = (x - LeftEdge) / xd + (y - TopEdge) / yd * XCELLS ))
mplayer -really-quiet ${SOUNDS[idx]} &>/dev/null &
clear_cells
print_cells $idx
state=1
fi
;;
1)
if (( f == 0 )); then
clear_cells
print_cells
state=0
fi
;;
esac
done < <(synclient -m 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment