Skip to content

Instantly share code, notes, and snippets.

@petrmvala
Created October 23, 2020 15:48
Show Gist options
  • Save petrmvala/db2fbcef1ee6a2aab37b9859ba6a4c18 to your computer and use it in GitHub Desktop.
Save petrmvala/db2fbcef1ee6a2aab37b9859ba6a4c18 to your computer and use it in GitHub Desktop.
Turn off individual inactive monitors in multi-monitor setup
#!/bin/bash
function get_monitors() {
xrandr --listactivemonitors | awk 'NR!=1{print $4}'
}
function dim_monitor() {
xrandr --output $1 --brightness 0
}
function brighten_monitor() {
xrandr --output $1 --brightness 1
}
function get_pointer_monitor() {
local X Y SCREEN WINDOW monitor size
declare -a size
# get mousepointer location and fill in vars X, Y, SCREEN, WINDOW
eval "$(xdotool getmouselocation -shell 2>/dev/null)"
for monitor in $(get_monitors); do
# populate array size=(xmin xmax ymin ymax)
size=($(xrandr \
| awk "/${monitor}/" \
| awk '{print $3}' \
| awk -F'[x+]' '{print $3 " " $1+$3 " " $4 " " $2+$4}'))
# figure out if pointer is in this monitor
if (( $X >= ${size[0]} )) \
&& (( $X < ${size[1]} )) \
&& (( $Y >= ${size[2]} )) \
&& (( $Y < ${size[3]} ));
then echo ${monitor}; fi
done
}
# turn off monitor after threshold of seconds without pointer
export threshhold=600 # 10 minutes
# declare array such as [monitor_name]=seconds_since_cursor_was_present
declare -A counters
for m in $(get_monitors); do
counters[$m]=0
done
while true; do
# each second evaluate on which monitor is present pointer
sleep 1
current=$(get_pointer_monitor)
for monitor in ${!counters[@]}; do
if [[ $monitor == $current ]]; then
# set counter to 0 and turn on where present
((counters[$monitor]=0))
brighten_monitor $monitor
else
# increase counter where not present
((counters[$monitor]++))
fi
# turn off monitors after threshold
(( ${counters[$monitor]} > $threshhold )) && dim_monitor $monitor
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment