Ubuntu – How to make a window be pointer-phobic


I mean the window should move around whenever I try to move the pointer on it. I have a "Analog Clock screenlet" and "file progress dialog box" which I tweaked to stay "Always on Top" of other windows with CCSM but sometimes they get in the way of doing things.

If that's not possible then is there any method so that they hide when I move pointer on them so that I can click on the application directly below?

Further if that is not possible than can we make the windows behave as if they aren't there? I mean I will see the window but the pointer should not recognize it and should work normally on the application below it. I will change the transparency of the applications and will make it work if that's possible?

Best Answer

Bash script and xdotool == cursophobia.sh

I think I have a solution that will work for you. It is a bash script that allows you to select a window. Once a window is selected, the script continuously polls window and cursor positions at predefined intervals. If the cursor gets too close, the window moves out of the way.

This script depends on the xdotool. To install, run sudo apt-get install xdotool

The script: cursophobia.sh
Create a new bash script with the following contents and make it executable.


windowSelectionDelay=5  # How long to wait for user to select a window?
buffer=10               # How close do we need to be to border to get scared?
jump=20                 # How far do we jump away from pointer when scared?
poll=.25                # How often in seconds should we poll window and mouse?
                        # locations. Increasing poll should lighten CPU load.

# ask user which window to make phobic
for s in $(seq 0 $((windowSelectionDelay - 1)))
    echo "Activate the window that you want to be cursophobic: $((windowSelectionDelay - s))"  
    sleep 1
wID=$(xdotool getactivewindow)

# find some boundary info and adjustments
# determine where the window is now
info=$(xdotool getwindowgeometry $wID)
base=$(grep -oP "[\d]+,[\d]+" <<< "$info")

# move the window to 0 0 and get real location
xdotool windowmove $wID 0 0
info=$(xdotool getwindowgeometry $wID)
realMins=$(grep -oP "[\d]+,[\d]+" <<< "$info")
xMin=$(cut -f1 -d, <<< "$realMins")
yMin=$(cut -f2 -d, <<< "$realMins")

# find offset values for no movement. This is necessary because moving 0,0
# relative to the current position sometimes actually moves the window
xdotool windowmove --relative $wID 0 0
info=$(xdotool getwindowgeometry $wID)
diff=$(grep -oP "[\d]+,[\d]+" <<< "$info")
xOffset=$[xMin - $(cut -f1 -d, <<< "$diff")]
yOffset=$[yMin- $(cut -f2 -d, <<< "$diff")]

# move window back to original location
x=$(cut -f1 -d, <<< "$base")
y=$(cut -f2 -d, <<< "$base")
xdotool windowmove $wID $[x + xOffset] $[y + yOffset]

dispSize=$(xdotool getdisplaygeometry)
xMax=$(cut -f1 -d ' ' <<< "$dispSize")
yMax=$(cut -f2 -d ' ' <<< "$dispSize")

echo "You can minimize this window, but don't close it, or your window will overcome its cursophobia"
# start an infinite loop polling to see if we need to move the window.
while :
    # get information about where the window is
    info=$(xdotool getwindowgeometry $wID)
    position=$(grep -oP "[\d]+,[\d]+" <<< "$info")
    geometry=$(grep -oP "[\d]+x[\d]+" <<< "$info")
    height=$(cut -f2 -dx <<< "$geometry")
    width=$(cut -f1 -dx <<< "$geometry")
    top=$(cut -f2 -d, <<< "$position")
    left=$(cut -f1 -d, <<< "$position")
    bottom=$((top + height))
    right=$((left + width))

    # save mouse coordinates to x & y
    eval "$(xdotool getmouselocation | cut -f 1-2 -d ' ' | tr ' :' '\n=')"

    # If the mouse is too close to the window, move the window
    if [ $x -gt $((left - buffer)) ] && [ $x -lt $((right + buffer)) ] && [ $y -gt $((top - buffer)) ] && [ $y -lt $((bottom + buffer)) ]; then
        #figure out what side we're closest to so we know which direction to move the window
        t="$((y - top)):0 $((jump + (y - top)))"
        l="$((x - left)):$((jump + (x - left))) 0"
        b="$((bottom - y)):0 -$((jump + (bottom - y)))"
        r="$((right - x)):-$((jump + (right - x))) 0"
        coord="$(echo -e "$t\n$l\n$b\n$r" | sort -n | head -n 1 | cut -f2 -d:)"

        # set the offset values for x and y
        newX=$(cut -f1 -d ' ' <<< "$coord")
        newY=$(cut -f2 -d ' ' <<< "$coord")

        #check to make sure we're not out of bounds
        if [ $((right + newX)) -gt $xMax ]; then
            newX=$((-1 * left + xOffset))
        elif [ $((left + newX)) -lt $xMin ]; then
            newX=$((xMax - width))
        if [ $((bottom + newY)) -gt $yMax ]; then
            newY=$((-1 * top + yOffset))
        elif [ $((top + newY)) -lt $yMin ]; then
            newY=$((yMax - height))

        # move the window if it has focus
        [ $(xdotool getactivewindow) -eq $wID ] && xdotool windowmove --relative $wID $((newX + xOffset)) $((newY + yOffset))
    sleep $poll

Don't forget to edit the four variables at the very top to your liking. If this script is tasking your CPU, try increasing the poll variable to a larger value.

cursophobia.sh in action
Once you've created your script and made it executable, run it. It will ask you to select a window. Click on the window you want to be cursophobic and wait until the countdown is over. Once the countdown is over, the window you select will be cursophobic. When you're ready to help the window over come its fear of cursors, close the terminal window or kill the script from the terminal window with Ctrl+c

Multiple displays
Please note that this restricts the cursophobic window to a single display. I'm open to edits that would make it work across multiple displays.