aboutsummaryrefslogtreecommitdiff
path: root/misc/bin/display_toggle.sh
diff options
context:
space:
mode:
Diffstat (limited to 'misc/bin/display_toggle.sh')
-rwxr-xr-xmisc/bin/display_toggle.sh61
1 files changed, 61 insertions, 0 deletions
diff --git a/misc/bin/display_toggle.sh b/misc/bin/display_toggle.sh
new file mode 100755
index 0000000..b2c7e5c
--- /dev/null
+++ b/misc/bin/display_toggle.sh
@@ -0,0 +1,61 @@
+#!/bin/bash
+
+# Assuming there are two displays. A small one (e.g. laptop) and a big
+# one (e.g. monitor). Identify the displays, and toggle between 3
+# states: small one only -> both with the big one to the left of the
+# small one -> big one only
+# small one: <20 inch
+# big one: >20 inch
+
+# Make sure the variables from pipe can be assigned
+# https://stackoverflow.com/questions/42963395/bash-assign-variable-from-pipe
+shopt -s lastpipe
+i=0
+regex="^([^ ]+).* .*$"
+
+# Read the output of xrandr for connected display names and sizes
+xrandr | grep " connected " | while IFS=$'\n' read -r line; do
+ if [[ $line =~ $regex ]]; then
+ name["$i"]="${BASH_REMATCH[1]}"
+ (( i++ ))
+ fi
+done
+
+if (( i == 1 )); then
+ echo "Only one connected display"
+ exit 1
+fi
+
+regex="^.*/([0-9]+)x.*/([0-9]+).* ([^ ]+)$"
+xrandr --listactivemonitors | while IFS=$'\n' read -r line; do
+ if [[ $line =~ $regex ]]; then
+ if (( "${BASH_REMATCH[1]}" > 500 )); then
+ bigger="${BASH_REMATCH[3]}"
+ if [[ $bigger == "${name[0]}" ]]; then
+ smaller=${name[1]}
+ else
+ smaller=${name[0]}
+ fi
+ else
+ smaller="${BASH_REMATCH[3]}"
+ if [[ $smaller == "${name[0]}" ]]; then
+ bigger=${name[1]}
+ else
+ bigger=${name[0]}
+ fi
+ fi
+ fi
+done
+
+# If only smaller on, then have both on
+if ! xrandr --listactivemonitors | grep -q "$bigger"; then
+ xrandr --output "$bigger" --primary --auto --output "$smaller" --right-of "$bigger" --auto
+ # if only bigger on, then only smaller on
+elif ! xrandr --listactivemonitors | grep -q "$smaller"; then
+ xrandr --output "$bigger" --auto --left-of "$smaller" --output "$smaller" --primary --auto
+ xrandr --output "$bigger" --off
+ # if both are on, then only bigger on
+else
+ xrandr --output "$bigger" --primary --auto --left-of "$smaller" --output "$smaller" --auto
+ xrandr --output "$smaller" --off
+fi