summaryrefslogtreecommitdiff
path: root/src/caribou.py
blob: 4db7a170df58b27aa6afb4fbf12564d5f68f40fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pyatspi
import gtk
import keyboard
import gettext
import getopt
import sys

_ = gettext.gettext

debug = False

class Test:
    def __init__(self):
        self.__current_acc = None 

    def on_text_caret_moved(self, event):
        if self.__current_acc == event.source:
            self.__set_location(event.source)
            if debug == True:
                print "object:text-caret-moved in", event.host_application.name, event.detail1, event.source.description
    
    def __set_text_location(self, acc):
        text = acc.queryText() 
        [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
        cp.set_cursor_location(x, y + height)
        cp.show_all()
       
    def __set_entry_location(self, acc):
        text = acc.queryText()
        [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
        if x == 0 and y == 0 and width == 0 and height == 0:
            component = acc.queryComponent()
            bb = component.getExtents(pyatspi.DESKTOP_COORDS)
            cp.set_cursor_location(bb.x, bb.y + bb.height)
        else:
            cp.set_cursor_location(x, y + height)
        cp.show_all()
       
    def on_state_changed_focused(self, event):
        acc = event.source
        if pyatspi.STATE_EDITABLE in acc.getState().getStates():
            if event.source_role == pyatspi.ROLE_TEXT:
                if event.detail1 == 1:
                    self.__set_text_location(acc)
                    self.__current_acc = event.source
                    self.__set_location = self.__set_text_location
                    if debug == True:
                        print "enter text widget in", event.host_application.name
                elif event.detail1 == 0:
                    cp.hide_all()
                    self.__current_acc = None 
                    self.__set_location = None
                    if debug == True:
                        print "leave text widget in", event.host_application.name

            elif event.source_role == pyatspi.ROLE_ENTRY:
                if event.detail1 == 1:
                    self.__set_entry_location(acc)
                    self.__current_acc = event.source
                    self.__set_location = self.__set_entry_location
                    if debug == True:
                        print "enter entry widget in", event.host_application.name
                elif event.detail1 == 0:
                    cp.hide_all()
                    self.__current_acc = None 
                    self.__set_location = None
                    if debug == True:
                        print "leave entry widget in", event.host_application.name
            else:
                print _("WARNING - Caribou: unhandled editable widget:"), event.source         

        # Firefox does report leave entry widget events.
        # This could be a way to get the entry widget leave events.
        #else:
        #    if event.detail1 == 1:
        #        cp.hide_all()
        #        print "--> LEAVE EDITABLE TEXT <--"

    def on_key_down(self, event):
        # key binding for controling the row column scanning
        # TODO: needs implementing
        if event.event_string == "Control_R":
            pass 
        elif event.event_string == "Shift_R":
            if debug == True:
                print "quitting ..."
            # TODO: use for loop here? see below
            result = pyatspi.Registry.deregisterEventListener(self.on_text_caret_moved, "object:text-caret-moved")
            if debug == True:
                print "deregisterEventListener - object:text-caret-moved ...",
                if result == False:
                    print "OK"
                else:
                    print "FAIL"
            result = pyatspi.Registry.deregisterEventListener(self.on_state_changed_focused, "object:state-changed:focused")
            if debug == True:
                print "deregisterEventListener - object:state-changed:focused ...",
                if result == False:
                    print "OK"
                else:
                    print "FAIL"
            result = pyatspi.Registry.deregisterKeystrokeListener(self.on_key_down, mask = None, kind = (pyatspi.KEY_PRESSED_EVENT,))
            if debug == True:
                print "deregisterKeystrokeListener"
            gtk.main_quit()


def usage():
    """Prints out usage information."""
    print _("Usage:")
    print "  " + sys.arg[0] + _(" [OPTION...]")
    print
    print _("Help Options:")
    print "  -h, --help                       " + _("Show this help message")
    print "  -d, --debug                      " + _("Print debug messages on stdout")

if __name__ == "__main__":

    try:
        options, xargs = getopt.getopt(sys.argv[1:], "d", ["debug"])
    except getopt.GetoptError, e:
        print "Error: " + e.__str__() + "\n"
        usage()
        sys.exit(1)
 
    for opt, val in options:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)

        if opt in ("-d", "--debug"):
            debug = True

    test = Test()
    # TODO: make a for loop
    #EVENTS = ["object:state-changed:focused", "object:text-caret-moved"]
    #for f in dir(test):
    #    print f, isinstance(f, str)
    pyatspi.Registry.registerEventListener(test.on_state_changed_focused, "object:state-changed:focused")
    pyatspi.Registry.registerEventListener(test.on_text_caret_moved, "object:text-caret-moved")
    pyatspi.Registry.registerKeystrokeListener(test.on_key_down, mask = None, kind = (pyatspi.KEY_PRESSED_EVENT,))

    # TODO: move text entry detection to its own file
    cp = keyboard.CaribouKeyboard()
    cp.hide_all()
 
    gtk.main()