Raise Brightness When Maximizing Terminal

brightnessgnomeprocessterminalwindow-manager

I'm looking to raise the brightness when I maximize or foreground the Terminal window which has Vim running. I would like to reset the brightness to normal when I minimize or background that process as well.

I usually need more brightness when working with the black background of Vim than the white background of Chrome so I'd like it to happen automatically.

I use this to programatically decrease the brightness:

$ echo 1 | sudo tee /sys/class/backlight/acpi_video0/brightness

Can I somehow hook into the minimize/maximize event of a GUI window and run the above as a script if the window is Terminal with Vim running?

Best Answer

You could do something like:

xdotool search --onlyvisible . behave %@ focus getwindowgeometry |
while read x id && read x && read x; do
  eval "$(xprop -notype -id "$id" \
            8s '=$0\n' WM_CLASS \
            32a '="$0+"\n' _NET_WM_STATE)"
  [ "$WM_CLASS" = gnome-terminal ] &&
    [ "$_NET_WM_STATE" = "_NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ" ]
  rc=$?
  if [ "$rc" != "$last_rc" ]; then
    if [ "$rc" -eq 0 ]; then
      echo "set high brightness"
    else
      echo "set low brightness"
    fi
    last_rc=$rc
  fi
done

Replace the echo ... with the actual command to set the brightness.

The idea is to use xdotool to get notified when the window focus changes. Then, we use xprop on the window id reported by xdotool to see if the window that currently has the focus is gnome-terminal and is maximised.

However, it doesn't work for windows that have connected after xdotool has started.

A more robust method could be to just check the current active window in a loop:

while :; do
  # wait for a focus event:
  sh -c 'exec xdotool search --onlyvisible . behave %@ focus exec kill "$$"' 2> /dev/null

  id=$(xdotool getactivewindow)
  eval "$(xprop -notype -id "$id" \
            8s '=$0\n' WM_CLASS \
            32a '="$0+"\n' _NET_WM_STATE)"
  [ "$WM_CLASS" = gnome-terminal ] &&
    [ "$_NET_WM_STATE" = "_NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ" ]
  rc=$?
  if [ "$rc" != "$last_rc" ]; then
    if [ "$rc" -eq 0 ]; then
      echo "set high brightness"
    else
      echo "set low brightness"
    fi
    last_rc=$rc
  fi
done

You can find out more details through the xdotool man page.

Related Question