#!/usr/bin/python

import pyatspi

def listener(e):
    try:
	appName = e.host_application.name
        role = e.source.getRole()
        relations = e.source.getRelationSet()
    except:
	return

    if not (appName == 'gnome-shell' and role == pyatspi.ROLE_PUSH_BUTTON):
        return

    relations = filter(
        lambda x: x.getRelationType() == pyatspi.RELATION_LABELLED_BY, relations)
    targets = []
    for r in relations:
        targets.extend([r.getTarget(x).name for x in range(r.getNTargets())])

    print "%s" % (e.source.name or ','.join(targets))

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

