-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisrightmonitorenabled
executable file
·75 lines (68 loc) · 1.73 KB
/
isrightmonitorenabled
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
#!/usr/bin/env bash
#
# File:
# isrightmonitorenabled
#
# Description:
# Check if a right monitor (a monitor positioned to the right of the
# leftmost monitor) is enabled.
#
# Returns:
# Monitor type (e.g. "DP", "HDMI", "VGA", "TV", "DVI1") if the monitor is
# enabled
#
# Exit codes:
# 0: is enabled
# 1: is not enabled
#
# Options:
# -o also output offset/starting position of right monitor (returns string
# in the format "<TYPE>,<OFFSET>")
#
# Notes:
# All monitors after the second will be ignored.
#
# The right monitor is considered the one with the lowest non-zero X offset.
# The left monitor is always the first one with a zero X offset.
#
while getopts :o OPT; do
case "${OPT}" in
o) OUTPUT_OFFSET=true;;
\?)
echo "isrightmonitorenabled: option \"${OPTARG}\" unknown" 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
IFS=$'\n'
xrandrMntrs=($(xrandr --query | grep ' connected'))
unset IFS
if [ "${#xrandrMntrs[@]}" -ge 2 ]; then
for xrandrMntr in "${xrandrMntrs[@]}"; do
vals=($(echo "${xrandrMntr}" | perl -ne \
'm/([^ ]+) connected (?:primary )?[^+]+\+(\d+).*/; print "$1 $2"'))
if [ "${#vals[@]}" -eq 2 ]; then
mntrType="${vals[0]}"
offst="${vals[1]}"
if [[ "${offst}" -gt 0 && (-z "${lwstNonZeroOffst}" || "${offst}" -lt \
"${lwstNonZeroOffst}") ]]; then
RIGHT_MONITOR_TYPE="${vals[0]}"
RIGHT_MONITOR_OFFST="${vals[1]}"
lwstNonZeroOffst="${offst}"
fi
fi
done
if [ -n "${RIGHT_MONITOR_TYPE}" ]; then
retStr="${RIGHT_MONITOR_TYPE}"
if [ "${OUTPUT_OFFSET}" = 'true' ]; then
retStr+=",${RIGHT_MONITOR_OFFST}"
fi
echo "${retStr}"
exit 0
else
exit 1
fi
else
exit 1
fi