-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitchOpenWindows.sh
147 lines (127 loc) · 4.04 KB
/
SwitchOpenWindows.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
scriptdir=$(dirname "$0")
mfile+=$scriptdir/minimized.txt
# Get list of all window IDs from the NET_CLIENT_LIST_STACKING property
order=$(xprop -root | awk '/NET_CLIENT_LIST_STACKING\(WINDOW\)/ {gsub(",", ""); for(i=5;i<=NF;i++){print $i}}')
# Variables to store the focused window and the list of window IDs
focused="last"
window_ids=""
desktop=false
restore=true
# Process command-line arguments
for arg in "$@"; do
case $arg in
desktop=false)
desktop=false
;;
desktop=true)
desktop=true
;;
restore=false)
restore=false
;;
restore=true)
restore=true
;;
*)
echo " Unknown argument: $arg"
echo " Use:"
echo "desktop=false"
echo "desktop=true"
echo " to dis/enable showing the destop"
echo "restore=false"
echo "restore=true"
echo " to dis/enable restoring windows after the desktop was shown"
exit
;;
esac
done
# Loop through all windows
for w in $order
do
# Get window information using xwininfo
win=$(xwininfo -all -id $w 2>/dev/null)
# Skip if the window is the Desktop, a Notification or a Dock
if [[ $(echo $win | awk '/Window type: Dock/ || /Window type: Desktop/ || /Window type: Notification/') ]]
then
continue
fi
decimal_w=$(printf "%d" "$w")
# Store window IDs
window_ids+="$decimal_w"$'\n'
# Remember vissible windows
if [[ $(echo "$win" | awk '/Map State: IsViewable/') ]]
then
visible+="$decimal_w"$'\n'
#echo "$win"
fi
# Check if the window is focused
if [[ $(echo "$win" | awk '{if ($1 == "Focused" && NF == 1){print $1}}') ]]
then
focused=$decimal_w
fi
done
visible=$(echo "$visible" | awk 'NF')
# Sort window IDs (sorting by window ID ensures consistent order)
sorted_windows=$(echo "$window_ids" | awk 'NF' | sort -n)
# Main loop to find and focus the next window
activation=false
while [[ $activation == false ]]; do
found_focused=false
next_window=""
# Iterate through sorted windows to find the next window after the focused one
while read -r wid; do
if [[ $found_focused == true ]] && [[ $wid ]]; then
next_window=$wid
break
fi
if [[ $wid == $focused ]]; then
found_focused=true
fi
done <<< "$sorted_windows"
# If no next window is found, loop back to the first window
if [[ -z $next_window ]]; then
if [[ $desktop == true ]] && [[ $focused -ne "last" ]]
then
if [[ $restore == true ]] && [[ $desktop == true ]] && [[ $visible ]]
then
echo "$visible" > $mfile
fi
for v in $sorted_windows
do
xdotool windowminimize $v
done
activation=true
continue
elif [[ $restore == true ]] && [[ $desktop == true ]] && [[ $focused == "last" ]] && [[ -f $mfile ]]
then
visible=$(awk 'NF' $mfile)
# restore windows last visible if it was requested
if [[ $visible ]]
then
for f in $(echo "$visible" | tac)
do
xdotool windowactivate $f
done
rm $mfile
activation=true
continue
fi
rm $mfile
next_window=$(echo "$sorted_windows" | head -n 1)
else
next_window=$(echo "$sorted_windows" | head -n 1)
fi
fi
# Attempt to focus the next window
if [[ -n $next_window ]]; then
error_message=$(xdotool windowactivate "$next_window" 2>&1 >/dev/null)
# If xdotool succeeds, set activation to true
if [[ $? -eq 0 ]] && [[ -z $error_message ]]; then
activation=true
else
# If activation failed, move the focus to the next window and try again
focused=$next_window
fi
fi
done