Skip to content

Instantly share code, notes, and snippets.

@joepvd
Created Jun 20, 2022
Embed
What would you like to do?
#!/usr/bin/env bash
home_monitor() {
echo "Setting single monitor">/dev/stderr
xrandr \
--output DP-1 --primary --mode 3840x2160 --pos 0x0 --rotate normal \
--output eDP-1 --off \
--output HDMI-1 --off \
--output DP-2 --off \
--output HDMI-2 --off
}
export -f home_monitor
emergency_home_monitor() {
echo "Setting single monitor">/dev/stderr
xrandr \
--output HDMI-2 --primary --mode 3840x2160 --pos 0x0 --rotate normal \
--output eDP-1 --off \
--output HDMI-1 --off \
--output DP-2 --off \
--output DP-1 --off
}
export -f emergency_home_monitor
laptop_screen() {
echo "Setting single laptop screen">/dev/stderr
xrandr \
--output eDP-1 --primary --mode 1920x1080 --pos 0x0 --rotate normal \
--output DP-1 --off \
--output HDMI-1 --off \
--output DP-2 --off \
--output HDMI-2 --off
}
export -f laptop_screen
check_state() {
xrandr --query | awk '
$2 == "connected"{
mons[$1]++
}
END {
if ("eDP-1" in mons && length(mons) == 1) {
print("laptop_screen")
} else if ("DP-1" in mons) {
print("home_monitor")
} else if ("HDMI-2" in mons) {
print("emergency_home_monitor")
} else {
print("Do not know what to do") > "/dev/stderr"
}
}'
}
fix_state() {
if [[ -z "$last_exec_time" ]] || (( last_exec_time + 2 < EPOCHSECONDS )); then
echo "Fixing state" >/dev/stderr
bash -c "$(check_state)"
last_exec_time="$EPOCHSECONDS"
else
echo "Last change was too recent, not actioning reported state change" >/dev/stderr
fi
}
fix_state
while IFS= read -r line; do
echo "Change reported" >/dev/stderr
fix_state
done < <(
i3-msg -t subscribe -m '[ "output" ]'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment