4

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?

1 Answer 1

1

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.

5
  • can you explain how to use this? I'm getting this: "xdotool: Unknown command: getwindowgeometry"
    – slm
    Jun 4, 2013 at 14:11
  • @slm, you need xdotool 2.20110530 or newer for the getwindowgeometry command. Jun 4, 2013 at 14:32
  • Thanks for the detailed response! In both examples I'm getting this error: line 7: WM_CLASS:: command not found. Here's my xprop output if it helps.
    – Maros
    Jun 5, 2013 at 8:04
  • @MarosHluska, what do you see if you replace eval with echo above or if you run that code with sh -x? What version of xdotool do you have? Jun 5, 2013 at 9:01
  • @StephaneChazelas if I replace eval with echo I get a lot of repeated output like this: _NET_WM_STATE="_NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_FOCUSED" WM_CLASS="gnome-terminal". There is a newline before WM_CLASS=.... My xdotool version is 2.20110530.1
    – Maros
    Jun 5, 2013 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .