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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
import pyatspi
from gi.repository import GConf
from gi.repository import Gtk
from gi.repository import Gdk
import signal
from window import CaribouWindowEntry, Rectangle
from keyboard import CaribouKeyboard
from caribou.ui.i18n import _
import caribou.common.const as const
debug = False
class Caribou:
def __init__(self,
kb_factory=CaribouKeyboard,
window_factory=CaribouWindowEntry):
if not self._get_a11y_enabled():
raise Exception, "AT-SPI 1 or 2 needs to be enabled."
self.__current_acc = None
self.window_factory = window_factory
self.kb_factory = kb_factory
self.window = window_factory(kb_factory())
self.client = GConf.Client.get_default()
self._register_event_listeners()
#self.client.notify_add(const.CARIBOU_GCONF + "/layout",
# self._on_layout_changed)
signal.signal(signal.SIGINT, self.signal_handler)
def _register_event_listeners(self):
pyatspi.Registry.registerEventListener(
self.on_focus, "object:state-changed:focused")
pyatspi.Registry.registerEventListener(self.on_focus, "focus")
pyatspi.Registry.registerEventListener(
self.on_text_caret_moved, "object:text-caret-moved")
pyatspi.Registry.registerKeystrokeListener(
self.on_key_down, mask=0, kind=(pyatspi.KEY_PRESSED_EVENT,))
def _deregister_event_listeners(self):
pyatspi.Registry.deregisterEventListener(
self.on_focus, "object:state-changed:focused")
pyatspi.Registry.deregisterEventListener(self.on_focus, "focus")
pyatspi.Registry.deregisterEventListener(
self.on_text_caret_moved, "object:text-caret-moved")
pyatspi.Registry.deregisterKeystrokeListener(
self.on_key_down, mask=0, kind=pyatspi.KEY_PRESSED_EVENT)
def _on_layout_changed(self, client, connection_id, entry, args):
self._deregister_event_listeners()
self.window.destroy()
self._update_window()
self._register_event_listeners()
def _update_window(self):
self.window = self.window_factory(self.kb_factory())
def _get_a11y_enabled(self):
try:
gconfc = GConf.Client.get_default()
atspi1 = gconfc.get_bool("/desktop/gnome/interface/accessibility")
atspi2 = gconfc.get_bool("/desktop/gnome/interface/accessibility2")
return atspi1 or atspi2
except:
return False
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,
print 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)
self.window.set_cursor_location(Rectangle(x, y, width, height))
component = acc.queryComponent()
entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
self.window.set_entry_location(entry_bb)
self.window.show_all()
def __set_entry_location(self, acc):
text = acc.queryText()
cursor_bb = Rectangle(
*text.getCharacterExtents(text.caretOffset,
pyatspi.DESKTOP_COORDS))
component = acc.queryComponent()
entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
if cursor_bb == Rectangle(0, 0, 0, 0):
cursor_bb = entry_bb
self.window.set_cursor_location(cursor_bb)
self.window.set_entry_location(entry_bb)
self.window.show_all()
def on_focus(self, event):
acc = event.source
source_role = acc.getRole()
if pyatspi.STATE_EDITABLE in acc.getState().getStates() or \
source_role == pyatspi.ROLE_TERMINAL:
if source_role in (pyatspi.ROLE_TEXT,
pyatspi.ROLE_PARAGRAPH,
pyatspi.ROLE_PASSWORD_TEXT,
pyatspi.ROLE_TERMINAL):
if event.type.startswith("focus") or 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 and acc == self.__current_acc:
self.window.hide()
self.__current_acc = None
self.__set_location = None
if debug == True:
print "leave text widget in", event.host_application.name
elif source_role == pyatspi.ROLE_ENTRY:
if event.type.startswith("focus") or 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:
self.window.hide()
self.__current_acc = None
self.__set_location = None
if debug == True:
print "leave entry widget in", event.host_application.name
else:
if debug == True:
print _("WARNING - Caribou: unhandled editable widget:"), event.source
# Firefox does not report leave entry widget events.
# This could be a way to get the entry widget leave events.
#else:
# if event.detail1 == 1:
# self.window.hide()
# print "--> LEAVE EDITABLE TEXT <--"
def on_key_down(self, event):
# key binding for controlling the row column scanning
if event.event_string == "Shift_R":
# TODO: implement keyboard scanning
pass
elif event.event_string == "Control_R":
self.clean_exit()
def signal_handler(self,signal,frame):
# Clean exit pressing Control + C
self.clean_exit()
def clean_exit(self):
if debug == True:
print "quitting ..."
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_focus, "object:state-changed:focused")
if debug == True:
print "deregisterEventListener - object:state-changed:focused ...",
if result == False:
print "OK"
else:
print "FAIL"
result = pyatspi.Registry.deregisterEventListener(self.on_focus, "focus")
if debug == True:
print "deregisterEventListener - focus ...",
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()
|