#!/bin/bash
##CompizSnap LinuxUndIch.de Edition#############################################
# By Christoph Langner <mail@christoph-langner.de>
# Released under GNU General Public License 3.0
# Copyright (c) 2011
#
# Last updated on Jan/31/2011
#
##GNU GENERAL PUBLIC LICENSE 3.0################################################
# 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 3 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, see <http://www.gnu.org/licenses/> or write to the 
# Free Software Foundation, Inc., 
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
##GENERAL INFORMATION###########################################################
# This script adds window snapping functionality to compiz using the commands plugin.
# CompizSnap LinuxUndIch.de Edition is a collaborative project from people from ubuntuforums.org
# and additions from LinuxUndIch. The script is licensed under GPL v3.
#
# Help and support:
# http://wwww.ubuntuforums.org/showthread.php?t=1294661
# http://linuxundich.de/de/ubuntu/aero-snap-mit-gnome-und-compiz/
#
##END OF HEAD###################################################################

MOUSE=$(xinput -list | grep -i 'mouse' | grep -v 'Macintosh mouse button emulation' | grep id= | sed 's:.*id=\([0-9]*\).*:\1:')
TOUCHPAD=$(xinput -list | grep -i 'touchpad' | grep id= | sed 's:.*id=\([0-9]*\).*:\1:')
TRACKPAD=$(xinput -list | grep -i 'trackpoint' | grep id= | sed 's:.*id=\([0-9]*\).*:\1:')

WIDTH=$(xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 1 -d 'x')
HEIGHT=$(xdpyinfo | grep 'dimensions:' | cut -f 2 -d ':' | cut -f 2 -d 'x' | cut -f 1 -d p)
HALF=$(($WIDTH/2-10))
TEMPWIDTH=$(($WIDTH-10))


left() {
#would be nicer, but --shell option works only under maverick
#eval $(xdotool getmouselocation --shell)
X=$(xdotool getmouselocation | awk '{print $1}' | cut -d : -f 2)
if [ $X -le 10 ]
then
	wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz && wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -e 0,0,0,$HALF,-10
fi
}  

right() {
#eval $(xdotool getmouselocation --shell)
X=$(xdotool getmouselocation | awk '{print $1}' | cut -d : -f 2)
if [ $(($WIDTH-$X)) -le 10 ]
then
	wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz && wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -e 0,$HALF,0,$(($HALF+18)),-10
fi
}

max() {
#eval $(xdotool getmouselocation --shell)
Y=$(xdotool getmouselocation | awk '{print $2}' | cut -d : -f 2)
if [ $(($HEIGHT-$Y)) -ge $(($HEIGHT-$10)) ]
then
	wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz
fi
}

    
# ----- Don't edit below this line unless you know what you are doing.
if [ -n "$MOUSE" ]
then
	if xinput --query-state $MOUSE | grep down
	then
		while (xinput --query-state $MOUSE | grep down)
		do 
			echo 'mouse button pressed'
		done
		$1 $MOUSE
	fi
fi

if [ -n "$TOUCHPAD" ]
then
	if xinput --query-state $TOUCHPAD | grep down
	then
		while (xinput --query-state $TOUCHPAD | grep down)
		do 
			echo 'touchpad button pressed'
		done
		$1 $TOUCHPAD
	fi
fi

if [ -n "$TRACKPAD" ]
then
	if xinput --query-state $TRACKPAD | grep down
	then
		while (xinput --query-state $TRACKPAD | grep down)
		do 
			echo 'touchpad button pressed'
		done
		$1 $TRACKPAD
	fi
fi

