#!/bin/bash

###############################################
## rofi popup to choose between open windows ##
## finally supports multi monitor            ##
###############################################

#############################################################
## heavily commented, this was a nightmare to write        ##
## look at the git history of this file before touching it ##
#############################################################

# alt tab like behaviour with rofi
if ! [ -n "$1" ]; then
    # is multi monitor?
    if ! iconf -i right; then
        rofi -show window -kb-row-down 'Alt+Tab,Down' -kb-row-up 'Alt+Ctrl+Tab,Up' -kb-accept-entry '!Alt_L,!Alt+Tab,Return'
        exit
    else
        num=$(wmctrl -l | sed 's/  / /' | cut -d " " -f 4- | nl -w 3 -n rn | sed -r 's/^([ 0-9]+)[ \t]*(.*)$/\1 - \2/' |
            rofi -dmenu -p applications -i -me-select-entry '' -me-accept-entry 'MousePrimary' -kb-row-down 'Alt+Tab,Down' -kb-row-up 'Alt+Ctrl+Tab,Up' -kb-accept-entry '!Alt_L,!Alt+Tab,Return' | cut -d '-' -f -1)
        [[ -z "$num" ]] && exit
        WID=$(wmctrl -l | sed -n "$num p" | cut -c -10)
    fi
else
    WID="$1"
fi

echo "focus target $WID"

# focus $1, weird behaviour with multi monitor, see rest of the file
focuswin() {
    [[ $1 =~ "," ]] && echo "error focussing" && return 1
    wmctrl -i -a "$1"
}

# single monitor doesnt need fixes
if ! iconf -i right; then
    focuswin "$1"
    exit
fi

# convert decimal to hexadecimal
hex() {
    echo "0x0$(printf '%x\n' $1)"
}

# wmutils pfw sometimes returns one digit higher than xdotool
# but xdotool behaves weird with the root window in focus
pfw() {
    if command pfw | grep -q '0x000001e1'; then
        echo "0x000001e1"
        return
    else
        hex "$(xdotool getactivewindow)"
    fi
}

# warp mouse cursor to current window and get the monitor from the coordinates.
# more reliable than using window positions because of the root window always returning 0
getmonitor() {
    xdotool key 'super+W'
    XPOS=$(xdotool getmouselocation --shell | head -1 | grep -o '[0-9]*')
    if [ "$XPOS" -gt "$(iconf right)" ]; then
        echo "1"
    else
        echo "0"
    fi
}

# sometimes there are trailing zeros, this only compares everything after these
wincomp() {
    NEW=$(echo "$1" | sed 's/^0x0*//g')
    COMP=$(echo "$2" | sed 's/^0x0*//g')
    echo "NEW $NEW COMP $COMP"
    if [ "$NEW" = "$COMP" ]; then
        return 0
    else
        return 1
    fi
}

# already on focused window?
wincomp "$WID" "$(pfw)" && exit

OLD="$(pfw)"
OLDM="$(getmonitor)"

focuswin "$WID"

# solution for multi monitor glitches
if ! wincomp "$(pfw)" "$WID"; then
    if ! [ "$OLDM" = "$(getmonitor)" ]; then
        # do not attempt to focuswin the root window!!
        if ! [ "$OLD" = "0x000001e1" ]; then
            xdotool key 'super+comma'
            focuswin "$OLD"
            xdotool key 'super+comma'
        fi
        focuswin "$WID"
    else
        [ "$OLD" = "0x000001e1" ] || focuswin "$OLD"
        xdotool key 'super+comma'
        focuswin "$WID"
    fi
else
    if ! [ "$OLD" = "0x000001e1" ]; then
        if ! [ "$OLDM" = "$(getmonitor)" ]; then
            xdotool key 'super+comma'
            focuswin "$OLD"
            xdotool key 'super+comma'
        fi
    fi
fi

xdotool key 'super+W'
