-
Notifications
You must be signed in to change notification settings - Fork 0
/
focused-screen-index
executable file
·37 lines (33 loc) · 1.07 KB
/
focused-screen-index
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
#!/usr/bin/env bash
# if there is more than one monitor connected, uses
# xdotool to get current mouse location and infer the
# which monitor is currently being used.
# From: https://superuser.com/a/1238384/887225
# Prints the index of the connected monitor (zero indexed).
readonly OFFSET_RE="\+([-0-9]+)\+([-0-9]+)"
# Get the window position
# eval sets these as shell variables
eval "$(xdotool getmouselocation --shell)"
# Loop through each screen and compare the offset with the window
# coordinates.
monitor_index=0
while read -r name width height xoff yoff; do
if [ "${X}" -ge "$xoff" ] &&
[ "${Y}" -ge "$yoff" ] &&
[ "${X}" -lt "$((xoff + width))" ] &&
[ "${Y}" -lt "$((yoff + height))" ]; then
monitor="${name}"
break
fi
((monitor_index++))
done < <(xrandr | grep -w connected |
sed -r "s/^([^ ]*).*\b([-0-9]+)x([-0-9]+)$OFFSET_RE.*$/\1 \2 \3 \4 \5/" |
sort -nk4,5)
# If we found a monitor, echo it out, otherwise print an error.
if [[ -n "${monitor}" ]]; then
echo "${monitor_index}"
exit 0
else
echo "Couldn't find any monitor for the current window." >&2
exit 1
fi