From: Matthias Kruk Date: Sat, 3 Apr 2021 02:33:49 +0000 (+0900) Subject: include/xrandr: Add functions for querying the monitor state from xrandr X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=308af38a38f44a0e9bf357489e5f787cc6c900e9;p=toolbox include/xrandr: Add functions for querying the monitor state from xrandr This adds convenience functions for getting the current resolution and available resolutions for a monitor. --- diff --git a/include/xrandr.sh b/include/xrandr.sh new file mode 100644 index 0000000..40c645c --- /dev/null +++ b/include/xrandr.sh @@ -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 +}