Skip to content

Instantly share code, notes, and snippets.

@mzizzi
Created November 12, 2015 01:15
Show Gist options
  • Save mzizzi/1c5dddf90811830c3941 to your computer and use it in GitHub Desktop.
Save mzizzi/1c5dddf90811830c3941 to your computer and use it in GitHub Desktop.
Subscribe to X11 PropertyChange events
"""
Subscribe to X11 PropertyChange events and print "Window Changed!" when a
_NET_ACTIVE_WINDOW property has been changed.
Unfortunately no event is received events when _NET_WM_NAME is changed so this
would only partially solve https://github.com/dictation-toolbox/aenea/issues/103
You'll have to `pip install python-xlib` to run this script.
(Tested with the Cinnamon 2.2.16 desktop)
"""
import Xlib
from Xlib.display import Display
import Xlib.XK
from time import sleep
display = Display(':0')
NET_WM_NAME = display.intern_atom('_NET_WM_NAME')
NET_ACTIVE_WINDOW = display.intern_atom('_NET_ACTIVE_WINDOW')
root = display.screen().root
root.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
print('Running...')
while True:
while display.pending_events():
event = display.next_event()
if type(event) == Xlib.protocol.event.PropertyNotify:
atom_name = display.get_atom_name(event.atom)
if atom_name == '_NET_ACTIVE_WINDOW':
print 'Window changed!'
elif atom_name == '_NET_WM_NAME':
print 'Window name changed!'
sleep(0.1)
@scorpp
Copy link

scorpp commented Feb 6, 2016

Hi,
thanks for nice snippet! Do you have clue why not all windows report wm_name? For me Opera, HipChat and some other apps return empty string on get_wm_name()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment