Add an ad-hoc screen switching system

Write a script to switch monitors, and make Polybar play nice with it
This commit is contained in:
M1dgard 2018-04-30 16:54:11 +02:00
parent 1bebb7f2ff
commit 21b0ddab32
Signed by untrusted user who does not match committer: midgard
GPG key ID: 511C112F1331BBB4
6 changed files with 147 additions and 8 deletions

View file

@ -96,7 +96,7 @@ bindsym XF86MyComputer exec $filemanager
# change display layout # change display layout
bindsym $mod+XF86Display exec arandr 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
############################# #############################

View file

@ -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'

5
i3/screen_config Normal file
View file

@ -0,0 +1,5 @@
#!/bin/false
builtin="eDP-1"
ext1="HDMI-1"
ext2="VGA-1"

135
i3/screens.sh Executable file
View file

@ -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

View file

@ -23,7 +23,7 @@ foreground-alert = #ff4e4e
; I don't use this at the moment ; I don't use this at the moment
enable-ipc = false enable-ipc = false
;monitor = ${env:MONITOR:HDMI-1} monitor = ${env:MONITOR:eDP-1}
width = 100% width = 100%
height = 27 height = 27
fixed-center = true fixed-center = true

View file

@ -13,6 +13,11 @@ while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
source "$(dirname "$0")"/secrets.sh source "$(dirname "$0")"/secrets.sh
set +o allexport 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 polybar --config="$HOME/.config/polybar/config.ini" midbard
) )