#!/usr/bin/python3

import pyatspi

def listener(e):
    try:
        if e.host_application.name != 'gtk3-demo':
            return
    except:
        return

    print(e.type, bool(e.detail1), e.source)

    isFocused = e.source.getState().contains(pyatspi.STATE_FOCUSED)
    if isFocused != e.detail1:
        # Just to be sure it's not AT-SPI2 holding on to a stale state.
        e.source.clearCache()
        isFocused = e.source.getState().contains(pyatspi.STATE_FOCUSED)

    if isFocused != e.detail1:
        print("\tERROR: Event state (%s) does not match object state (%s)" % (bool(e.detail1), isFocused))

pyatspi.Registry.registerEventListener(listener, "object:state-changed:focused")
pyatspi.Registry.start()
