#!/usr/bin/python

from gi.repository import Atspi, GLib
import sys

def print_role(event):
    acc = event.source;
    source_role = acc.get_role ();
    print(source_role)

def on_text_caret_moved(event, data):
    print_role(event)

def register_event_listeners():
    Atspi.EventListener.register_from_callback(on_text_caret_moved,
                                               None,
                                               "object:text-caret-moved")

if __name__ == "__main__":
    if Atspi.init() != 0:
        sys.stderr.write("can't initialize atspi\n")
        sys.exit(1)

    register_event_listeners()

    loop = GLib.MainLoop()
    loop.run()
