]> git.corax.cc Git - toolbox/commitdiff
include/xrandr: Add functions for querying the monitor state from xrandr
authorMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:33:49 +0000 (11:33 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:33:49 +0000 (11:33 +0900)
This adds convenience functions for getting the current resolution and
available resolutions for a monitor.

include/xrandr.sh [new file with mode: 0644]

diff --git a/include/xrandr.sh b/include/xrandr.sh
new file mode 100644 (file)
index 0000000..40c645c
--- /dev/null
@@ -0,0 +1,87 @@
+#/bin/bash
+
+__init() {
+       return 0
+}
+
+xrandr_get_monitors() {
+       local conns
+
+       if ! conns=$(xrandr | grep -F " connected "); then
+               return 1
+       fi
+
+       cut -d ' ' -f 1 <<< "$conns"
+       return 0
+}
+
+_xrandr_list_all() {
+       local screen
+       local monitor
+       local line
+
+       local re_screen
+       local re_monitor
+       local re_current
+
+       re_screen='Screen ([0-9]+):'
+       re_monitor='([^ ]+) (dis)*connected'
+       re_current='[0-9]+\.[0-9]+\*'
+
+       while read -r line; do
+               local resolution
+               local freqs
+
+               if [[ "$line" =~ $re_screen ]]; then
+                       screen="${BASH_REMATCH[1]}"
+                       continue
+               fi
+
+               if [[ "$line" =~ $re_monitor ]]; then
+                       monitor="${BASH_REMATCH[1]}"
+                       continue
+               fi
+
+               if read -r resolution other <<< "$line"; then
+                       local current
+
+                       if [[ "$other" =~ $re_current ]]; then
+                               current="current"
+                       else
+                               current=""
+                       fi
+
+                       echo "$screen $monitor $resolution $current"
+               fi
+       done < <(xrandr)
+
+       return 0
+}
+
+xrandr_monitor_get_resolution() {
+       local monitor="$1"
+
+       local resolution
+
+       if ! resolution=$(_xrandr_list_all | grep -F " $monitor " |
+                                 grep -F " current"); then
+               return 1
+       fi
+
+       cut -d ' ' -f 3 <<< "$resolution"
+       return 0
+}
+
+xrandr_monitor_get_resolutions() {
+       local monitor="$1"
+       local current_only="$2"
+
+       local resolutions
+
+       if ! resolutions=$(_xrandr_list_all | grep -F " $monitor "); then
+               return 1
+       fi
+
+       cut -d ' ' -f 3 <<< "$resolutions"
+       return 0
+}