summaryrefslogtreecommitdiff
path: root/pairswitch
blob: 0f90183af1e63239e55ddc84e013977eb9e2246b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
# pairswitch N|+1|-1 : show virtual desktop N (1-based), or step relative to the
# current one, on the left and middle screens.
#
# E17's Desktop.Show D-Bus call has no zone argument, so it acts on the zone
# that currently holds the pointer. We warp the pointer onto each paired
# screen, switch, then restore the pointer.
#
# Relative steps are based on the desk of the paired screen the pointer is on,
# read with find_desktop (which reports the screen the pointer is on). If the
# pointer is on a non-paired screen, the first paired screen is used instead.
# Steps stop at the first/last desk. Both screens are then set to the same
# target, so a relative step also resyncs them if they had drifted apart.
#
# Screens to pair are given as monitor names (see `xrandr --listmonitors`).
# Override with: PAIR_MONITORS="DP-0 DP-2" pairswitch 2
# find_desktop location: FIND_DESKTOP=/path/to/find_desktop
set -eu

arg=${1:?usage: pairswitch N|+1|-1 (N is a 1-based desktop number)}
monitors=${PAIR_MONITORS:-"DP-0 DP-2"}
find_desktop=${FIND_DESKTOP:-$HOME/source/find_desktop/src/find_desktop}
desk_x=0

# Remember the pointer so we can put it back (and keep focus where it was).
eval "$(xdotool getmouselocation --shell)"   # sets X, Y, SCREEN, WINDOW
orig_x=$X orig_y=$Y
trap 'xdotool mousemove "$orig_x" "$orig_y"' EXIT

# Sets w h ox oy for monitor $1.
monitor_geom() {
    local geom rest
    # xrandr --listmonitors line: " 0: +*DP-0 2560/597x1440/336+0+560  DP-0"
    geom=$(xrandr --listmonitors | awk -v m="$1" '$NF == m { print $3 }')
    [ -n "$geom" ] || { echo "monitor $1 not found" >&2; return 1; }
    # geom: 2560/597x1440/336+0+560
    w=${geom%%/*}
    rest=${geom#*x}; h=${rest%%/*}
    ox=$(sed -E 's/.*\+([0-9]+)\+([0-9]+)$/\1/' <<<"$geom")
    oy=$(sed -E 's/.*\+([0-9]+)\+([0-9]+)$/\2/' <<<"$geom")
}

warp_to() {
    local w h ox oy
    monitor_geom "$1" || return 1
    xdotool mousemove $((ox + w / 2)) $((oy + h / 2))
}

# Is the original pointer on a paired monitor? Echoes its name if so.
pointer_monitor() {
    local m w h ox oy
    for m in $monitors; do
        monitor_geom "$m" || continue
        if [ "$orig_x" -ge "$ox" ] && [ "$orig_x" -lt $((ox + w)) ] &&
           [ "$orig_y" -ge "$oy" ] && [ "$orig_y" -lt $((oy + h)) ]; then
            echo "$m"; return
        fi
    done
}

case $arg in
    [+-][0-9]*)
        set -- $monitors
        base=$(pointer_monitor)
        # Read from where the pointer already is; only warp for a non-paired screen.
        [ -n "$base" ] || warp_to "$1"
        # find_desktop prefers $WINDOWID (our terminal's desk) if set.
        # E17 returns x*256+y; with a single column that is just y (0-based).
        cur=$(env -u WINDOWID "$find_desktop")
        n=$(( (cur % 256) + 1 + arg ))
        count=$(enlightenment_remote -desktops-get | awk '{ print $2 }')
        [ "$n" -lt 1 ] && n=1
        [ "$n" -gt "$count" ] && n=$count
        ;;
    [0-9]*) n=$arg ;;
    *) echo "bad argument: $arg" >&2; exit 2 ;;
esac

desk_y=$((n - 1))
for m in $monitors; do
    warp_to "$m" || continue
    enlightenment_remote -desktop-show "$desk_x" "$desk_y"
done