From 21b0ddab32c9959300c542d438f2b91a9a9004e6 Mon Sep 17 00:00:00 2001 From: M1dgard Date: Mon, 30 Apr 2018 16:54:11 +0200 Subject: [PATCH] Add an ad-hoc screen switching system Write a script to switch monitors, and make Polybar play nice with it --- i3/config | 2 +- i3/enable-only-hdmi.sh | 6 -- i3/screen_config | 5 ++ i3/screens.sh | 135 +++++++++++++++++++++++++++++++++++++++++ polybar/config.ini | 2 +- polybar/launch.sh | 5 ++ 6 files changed, 147 insertions(+), 8 deletions(-) delete mode 100755 i3/enable-only-hdmi.sh create mode 100644 i3/screen_config create mode 100755 i3/screens.sh diff --git a/i3/config b/i3/config index 04a27a3..e5c23eb 100644 --- a/i3/config +++ b/i3/config @@ -96,7 +96,7 @@ bindsym XF86MyComputer exec $filemanager # change display layout bindsym $mod+XF86Display exec arandr -bindsym XF86Display exec --no-startup-id $HOME/.config/i3/enable-only-hdmi.sh +bindsym XF86Display exec --no-startup-id $HOME/.config/i3/screens.sh ############################# diff --git a/i3/enable-only-hdmi.sh b/i3/enable-only-hdmi.sh deleted file mode 100755 index 0798a56..0000000 --- a/i3/enable-only-hdmi.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -xrandr --output HDMI-1 --auto && - xrandr --output eDP-1 --off && - $(dirname "$0")/../polybar/launch.sh && - i3-msg 'workspace 1' diff --git a/i3/screen_config b/i3/screen_config new file mode 100644 index 0000000..a85cfbc --- /dev/null +++ b/i3/screen_config @@ -0,0 +1,5 @@ +#!/bin/false + +builtin="eDP-1" +ext1="HDMI-1" +ext2="VGA-1" diff --git a/i3/screens.sh b/i3/screens.sh new file mode 100755 index 0000000..3d85e17 --- /dev/null +++ b/i3/screens.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +scriptdir="$(dirname "$0")" + +source "$scriptdir/screen_config" +[[ -n $builtin ]] || { echo "builtin empty"; exit 1; } +[[ -n $ext1 ]] || { echo "ext1 empty"; exit 1; } +[[ -n $ext2 ]] || { echo "ext2 empty"; exit 1; } + +xq="$(xrandr --query)" +xlm="$(xrandr --listmonitors)" + +card() { echo "$xq" | grep -qF "$1"; } +conn() { echo "$xq" | grep -qF "$1 connected"; } +actv() { echo "$xlm" | grep -qF "$1"; } + +should_exist() { + for i in "$@"; do + card "$i" || { echo "$i not on video card, refusing to do anything"; exit 1; } + done +} +should_exist "$builtin" "$ext1" "$ext2" + +trigger_change_and_exit() { + "$scriptdir/../polybar/launch.sh" & disown + exit +} + +single_screen() { + one="$1" + off1="$2" + off2="$3" + + xrandr \ + --output "$one" --auto --primary \ + --output "$off1" --off \ + --output "$off2" --off + notify-send "Only $screen connected" "Disabled all outputs except for $screen." + trigger_change_and_exit +} + +toggle_two_screens() { + one="$1" + two="$2" + off="$3" + + if actv "$one" && actv "$two"; then + xrandr \ + --output "$one" --off \ + --output "$two" --auto --primary \ + --output "$off" --off + elif ! actv "$one" && actv "$two"; then + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --off \ + --output "$off" --off + else + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --auto --left-of "$builtin" \ + --output "$off" --off + fi + trigger_change_and_exit +} + +toggle_three_screens() { + one="$1" + two="$2" + three="$3" + + if actv "$one" && actv "$two" && actv "$three"; then + xrandr \ + --output "$one" --off \ + --output "$two" --auto --primary \ + --output "$three" --auto --left-of "$two" + elif ! actv "$one" && actv "$two" && actv "$three"; then + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --off \ + --output "$three" --auto --left-of "$one" + elif actv "$one" && ! actv "$two" && actv "$three"; then + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --auto --left-of "$one" \ + --output "$three" --off + elif actv "$one" && actv "$two" && ! actv "$three"; then + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --off \ + --output "$three" --off + elif actv "$one" && ! actv "$two" && ! actv "$three"; then + xrandr \ + --output "$one" --off \ + --output "$two" --auto --primary \ + --output "$three" --off + elif ! actv "$one" && actv "$two" && ! actv "$three"; then + xrandr \ + --output "$one" --off \ + --output "$two" --off \ + --output "$three" --auto --primary + else + xrandr \ + --output "$one" --auto --primary \ + --output "$two" --auto --left-of "$one" \ + --output "$three" --auto --left-of "$two" + fi + trigger_change_and_exit +} + +# Only builtin connected +if conn "$builtin" && ! conn "$ext1" && ! conn "$ext2"; then + single_screen "$builtin" "$ext1" "$ext2" +fi + +# Only builtin and ext1 connected +if conn "$builtin" && conn "$ext1" && ! conn "$ext2"; then + toggle_two_screens "$builtin" "$ext1" "$ext2" +fi + +# Only builtin and ext2 connected +if conn "$builtin" && ! conn "$ext1" && conn "$ext2"; then + toggle_two_screens "$builtin" "$ext2" "$ext1" +fi + +# Builtin, ext1 and ext2 connected +if conn "$builtin" && conn "$ext1" && conn "$ext2"; then + toggle_three_screens "$builtin" "$ext1" "$ext2" +fi + +# Other cases: something's wrong, try enabling all displays +xrandr \ + --output "$builtin" --auto --primary \ + --output "$ext1" --auto \ + --output "$ext2" --auto + diff --git a/polybar/config.ini b/polybar/config.ini index d41b606..f35b9b6 100644 --- a/polybar/config.ini +++ b/polybar/config.ini @@ -23,7 +23,7 @@ foreground-alert = #ff4e4e ; I don't use this at the moment enable-ipc = false -;monitor = ${env:MONITOR:HDMI-1} +monitor = ${env:MONITOR:eDP-1} width = 100% height = 27 fixed-center = true diff --git a/polybar/launch.sh b/polybar/launch.sh index 78cfe21..8a48b32 100755 --- a/polybar/launch.sh +++ b/polybar/launch.sh @@ -13,6 +13,11 @@ while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done source "$(dirname "$0")"/secrets.sh set +o allexport + # Get the primary monitor, or else just any active monitor + MONITOR="$(xrandr | grep " connected primary" | head -n1 | cut -d ' ' -f1)" + [[ -n $MONITOR ]] || MONITOR="$(xrandr | grep " connected" | head -n1 | cut -d ' ' -f1)" + [[ -n $MONITOR ]] && export MONITOR + polybar --config="$HOME/.config/polybar/config.ini" midbard )