I'd like to know (programmatically) which window has current focus. Is there a window-manager independent way of discovering that?
Otherwise, how does one determine which window has focus in Compiz or Metacity?
I'd like to know (programmatically) which window has current focus. Is there a window-manager independent way of discovering that?
Otherwise, how does one determine which window has focus in Compiz or Metacity?
What you want is libwnck (if you're just interested in windows) or libbamf (if you're interested in windows and the applications that own them).
Another thing you can use is xdotool:
xdotool getwindowfocus
would return the Window ID of the focussed window, and:
xdotool getwindowfocus getwindowname
would tell you its name.
try using the wnck lib and then use this code:
import wnck
import gtk
while True:
if __name__ == '__main__':
screen = wnck.screen_get_default()
screen.force_update()
while True:
while gtk.events_pending():
gtk.main_iteration()
#time.sleep(0.5)
print screen.get_active_window().get_name()
If you're happy doing a little X11 programming, then the EWMH spec is what you're after - specifically _NET_ACTIVE_WINDOW.
Well if you can ping something back to the shell:
xdpyinfo | grep focus
Should work.
Edit: For slightly cleaner output, try this:
xdpyinfo | grep -Eo 'window 0x[^,]+' | cut -d" " -f2
From info xtool:
getactivewindow
Output the current active window. This command is often more
reliable than getwindowfocus. The result is saved to the window
stack. See "WINDOW STACK" for more details.
This is what I use for getting the title. (I an on 10.04)
xwininfo -id "$(xdotool getactivewindow)" |sed -n \
"2s/^xwininfo: Window id: \(0x[[:xdigit:]]\+\) \x22\(.*\)\x22$/\2/p"