#!/usr/bin/python import sys import xcb from xcb.xproto import * import xcb.xselinux import xcb.xfixes #dont_care_list = [ 273, 344, 345, 386 ] dont_care_list = [ ] atom_hash = {} seln_hash = {} def tree_recurse(window): conn.core.ChangeWindowAttributes(root, CW.EventMask, [ EventMask.SubstructureNotify | EventMask.PropertyChange ]) cookie = conn.core.QueryTree(window) reply = cookie.reply() for child in reply.children: tree_recurse(child) def list_selections(): cookie = conn.xselinux.ListSelections() reply = cookie.reply() for item in reply.selections: cookie = conn.core.GetAtomName(item.name) seln_hash[item.name] = str(cookie.reply().name.buf()) conn.xfixes.SelectSelectionInput(root, item.name, 7) def op_to_string(state): if state == Property.NewValue: return "changed." else: return "went bye-bye." def win_to_string(window): cookie = conn.core.GetProperty(False, window, wm_name, 0, 0, 900) reply = cookie.reply() return str(reply.value.buf()) def show_property_event(event): if event.atom in dont_care_list: return if not event.atom in atom_hash: cookie = conn.core.GetAtomName(event.atom) reply = cookie.reply() atom_hash[event.atom] = str(reply.name.buf()) winname = win_to_string(event.window) name = atom_hash[event.atom] state = op_to_string(event.state) print "%d(%s): Property %s(%d) %s" % (event.window, winname, name, event.atom, state) def show_selection_event(event): name = seln_hash[event.selection] if event.subtype == xcb.xfixes.SelectionEvent.SetSelectionOwner: winname = win_to_string(event.owner) print "%d(%s) took ownership of %s(%d)" % (event.owner, winname, name, event.selection) else: print "Selection %s(%d) has no owner" % (name, event.selection) conn = xcb.connect() conn.xfixes = conn(xcb.xfixes.key) conn.xselinux = conn(xcb.xselinux.key) conn.xselinux.QueryVersionUnchecked(1, 1) conn.xfixes.QueryVersionUnchecked(4, 0) setup = conn.get_setup() root = setup.roots[0].root cookie = conn.core.InternAtom(False, 7, "WM_NAME") wm_name = cookie.reply().atom tree_recurse(root) list_selections() while True: try: event = conn.wait_for_event() except BadWindow: continue except xcb.ProtocolException as error: print "Protocol error %s received!" % error.__class__.__name__ print error[0].__dict__ break except: print "Error: %s" % sys.exc_info()[0].__name__ break if isinstance(event, CreateNotifyEvent): conn.core.ChangeWindowAttributes(event.window, CW.EventMask, [ EventMask.SubstructureNotify | EventMask.PropertyChange ]) print "Window %d created." % event.window elif isinstance(event, DestroyNotifyEvent): print "Window %d destroyed." % event.window elif isinstance(event, PropertyNotifyEvent): show_property_event(event) elif isinstance(event, xcb.xfixes.SelectionNotifyEvent): show_selection_event(event)