#!/usr/bin/python3

import pyatspi

def onText(e):
    if e.host_application.name not in ["gnome-terminal", "gnome-terminal-server"]:
        return

    try:
        role = e.source.getRole()
    except:
        return
    if role != pyatspi.ROLE_TERMINAL:
        return

    print("Text change in %s. Source is focused: %s." % \
          (e.source, e.source.getState().contains(pyatspi.STATE_FOCUSED)))

def onFocused(e):
    if e.host_application.name not in ["gnome-terminal", "gnome-terminal-server"]:
        return

    try:
        role = e.source.getRole()
    except:
        return
    if role != pyatspi.ROLE_TERMINAL:
        return

    print("Focus change in %s. Event detail: %s. State set: %s." % \
          (e.source, bool(e.detail1), e.source.getState().contains(pyatspi.STATE_FOCUSED)))

pyatspi.Registry.registerEventListener(onText, "object:text-changed")
pyatspi.Registry.registerEventListener(onFocused, "object:state-changed:focused")
pyatspi.Registry.start()
