OSDN Git Service

formatting and commenting
[instantos/instantOS.git] / programs / dswitch
1 #!/bin/bash
2
3 ##############################################
4 ## dwm popup to choose between open windows ##
5 ## finally supports multi monitor           ##
6 ##############################################
7
8 #############################################################
9 ## heavily commented, this was a nightmare to write        ##
10 ## look at the git history of this file before touching it ##
11 #############################################################
12
13 # alt tab like behaviour with rofi
14 if ! [ -n "$1" ]; then
15     num=$(wmctrl -l | sed 's/  / /' | cut -d " " -f 4- | nl -w 3 -n rn | sed -r 's/^([ 0-9]+)[ \t]*(.*)$/\1 - \2/' |
16         rofi -dmenu -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)
17     [[ -z "$num" ]] && exit
18     WID=$(wmctrl -l | sed -n "$num p" | cut -c -10)
19 else
20     WID="$1"
21 fi
22
23 echo "focus target $WID"
24
25 # focus $1, weird behaviour with multi monitor, see rest of the file
26 focuswin() {
27     [[ $1 =~ "," ]] && echo "error focussing" && return 1
28     wmctrl -i -a "$1"
29 }
30
31 # single monitor like on a laptop is pretty straightforward
32 if ! [ -e ~/paperbenni/ismultimonitor ]; then
33     focuswin "$WID"
34     echo "exiting"
35     exit
36 fi
37
38 # convert decimal to hexadecimal
39 hex() {
40     echo "0x0$(printf '%x\n' $1)"
41 }
42
43 # wmutils pfw sometimes returns one digit higher than xdotool
44 # but xdotool behaves weird with the root window in focus
45 pfw() {
46     if command pfw | grep -q '0x000001e1'; then
47         echo "0x000001e1"
48         return
49     else
50         hex "$(xdotool getactivewindow)"
51     fi
52 }
53
54 # warp mouse cursor to current window and get the monitor from the coordinates.
55 # more reliable than using window positions because of the root window always returning 0
56 getmonitor() {
57     xdotool key 'super+W'
58     XPOS=$(xdotool getmouselocation --shell | head -1 | grep -o '[0-9]*')
59     if [ "$XPOS" -gt "1919" ]; then
60         echo "1"
61     else
62         echo "0"
63     fi
64 }
65
66 # sometimes there are trailing zeros, this only compares everything after these
67 wincomp() {
68     NEW=$(echo "$1" | sed 's/^0x0*//g')
69     COMP=$(echo "$2" | sed 's/^0x0*//g')
70     echo "NEW $NEW COMP $COMP"
71     if [ "$NEW" = "$COMP" ]; then
72         return 0
73     else
74         return 1
75     fi
76 }
77
78 # already on focused window?
79 wincomp "$WID" "$(pfw)" && exit
80
81 OLD="$(pfw)"
82 OLDM="$(getmonitor)"
83
84 focuswin "$WID"
85
86 # solution for multi monitor glitches
87 if ! wincomp "$(pfw)" "$WID"; then
88     if ! [ "$OLDM" = "$(getmonitor)" ]; then
89         # do not attempt to focuswin the root window!!
90         if ! [ "$OLD" = "0x000001e1" ]; then
91             xdotool key 'super+comma'
92             focuswin "$OLD"
93             xdotool key 'super+comma'
94         fi
95         focuswin "$WID"
96     else
97         [ "$OLD" = "0x000001e1" ] || focuswin "$OLD"
98         xdotool key 'super+comma'
99         focuswin "$WID"
100     fi
101 else
102     if ! [ "$OLD" = "0x000001e1" ]; then
103         if ! [ "$OLDM" = "$(getmonitor)" ]; then
104             xdotool key 'super+comma'
105             focuswin "$OLD"
106             xdotool key 'super+comma'
107         fi
108     fi
109 fi
110
111 xdotool key 'super+W'