-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbringup
executable file
·78 lines (67 loc) · 2.83 KB
/
xbringup
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
#!/usr/bin/env bash
# Xbringup – Brings up last used <WM_CLASS> or alternatively executes a cmd.
# Copyright (C) 2022 Joonas Ojala <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
if [[ $# -eq 0 ]] || [[ $1 == "-h" ]]; then
cat <<EOF
Usage: xbringup [-h] WM_CLASS CMD ...
Xbringup – Brings up last used WM_CLASS or alternatively execs the given CMD.
Options:
-h, Show this message and exit.
Arguments:
WM_CLASS, Bring up last used given WM_CLASS, if only one instance
exists and it's currently active, minimize it.
CMD, Executes the command if no given WM_CLASS was found.
Examples:
xbringup "Navigator.firefox" "firefox"
xbringup "Sandboxed.firefox" "xdg-open /home/foobar/Desktop/sandbox.desktop"
EOF
fi
ids_found=$(wmctrl -lx | awk -v a="${1}" '$3 ~ a {print $1}' | tr -d ',')
if [[ -z "${ids_found}" ]]; then
# No ids found of the given WM_CLASS.
# Execute the given CMD.
exec ${2}
exit
else
# One or more ids found of the given WM_CLASS.
# Use cut instead of awk for performance boost in slow X servers.
id_active=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -c 28-)
if [[ 1 -eq $(echo "${ids_found}" | wc -w) ]]; then
if [[ ${id_active} -eq ${ids_found} ]]; then
# Only one instance of given WM_CLASS and it's currently active.
xdotool windowminimize "${ids_found}"
exit
else
# Only one instance of given WM_CLASS but it's not currently active.
wmctrl -ia "${ids_found}"
exit
fi
else
# Use cut instead of awk for performance boost in slow X servers.
stack=$(xprop -root _NET_CLIENT_LIST_STACKING | cut -c 48- | tr -d ",")
IFS=" " read -r -a id_stack <<<"${stack}"
# Loop the stack array and bring up the previously used WM_CLASS id.
for ((i = ${#id_stack[@]} - 1; i >= 0; i--)); do
for id in ${ids_found}; do
if [[ ${id_stack[i]} -eq ${id} ]]; then
if [[ ${id} -ne ${id_active} ]]; then
wmctrl -ia "${id}"
exit
fi
fi
done
done
fi
fi