forked from Mdlglobal-atlassian-net/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventHandler.py
executable file
·355 lines (320 loc) · 14 KB
/
eventHandler.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#eventHandler.py
#A part of NonVisual Desktop Access (NVDA)
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
#Copyright (C) 2007-2017 NV Access Limited, Babbage B.V.
import threading
from typing import Optional
import queueHandler
import api
import speech
from speech.commands import _CancellableSpeechCommand
import appModuleHandler
import treeInterceptorHandler
import globalVars
import controlTypes
from logHandler import log
import globalPluginHandler
import config
import winUser
import extensionPoints
#Some dicts to store event counts by name and or obj
_pendingEventCountsByName={}
_pendingEventCountsByObj={}
_pendingEventCountsByNameAndObj={}
# Needed to ensure updates are atomic, as these might be updated from multiple threads simultaneously.
_pendingEventCountsLock=threading.RLock()
#: the last object queued for a gainFocus event. Useful for code running outside NVDA's core queue
lastQueuedFocusObject=None
def queueEvent(eventName,obj,**kwargs):
"""Queues an NVDA event to be executed.
@param eventName: the name of the event type (e.g. 'gainFocus', 'nameChange')
@type eventName: string
"""
_trackFocusObject(eventName, obj)
with _pendingEventCountsLock:
_pendingEventCountsByName[eventName]=_pendingEventCountsByName.get(eventName,0)+1
_pendingEventCountsByObj[obj]=_pendingEventCountsByObj.get(obj,0)+1
_pendingEventCountsByNameAndObj[(eventName,obj)]=_pendingEventCountsByNameAndObj.get((eventName,obj),0)+1
queueHandler.queueFunction(queueHandler.eventQueue,_queueEventCallback,eventName,obj,kwargs)
def _queueEventCallback(eventName,obj,kwargs):
with _pendingEventCountsLock:
curCount=_pendingEventCountsByName.get(eventName,0)
if curCount>1:
_pendingEventCountsByName[eventName]=(curCount-1)
elif curCount==1:
del _pendingEventCountsByName[eventName]
curCount=_pendingEventCountsByObj.get(obj,0)
if curCount>1:
_pendingEventCountsByObj[obj]=(curCount-1)
elif curCount==1:
del _pendingEventCountsByObj[obj]
curCount=_pendingEventCountsByNameAndObj.get((eventName,obj),0)
if curCount>1:
_pendingEventCountsByNameAndObj[(eventName,obj)]=(curCount-1)
elif curCount==1:
del _pendingEventCountsByNameAndObj[(eventName,obj)]
executeEvent(eventName,obj,**kwargs)
def isPendingEvents(eventName=None,obj=None):
"""Are there currently any events queued?
@param eventName: an optional name of an event type. If given then only if there are events of this type queued will it return True.
@type eventName: string
@param obj: the NVDAObject the event is for
@type obj: L{NVDAObjects.NVDAObject}
@returns: True if there are events queued, False otherwise.
@rtype: boolean
"""
if not eventName and not obj:
return bool(len(_pendingEventCountsByName))
elif not eventName and obj:
return obj in _pendingEventCountsByObj
elif eventName and not obj:
return eventName in _pendingEventCountsByName
elif eventName and obj:
return (eventName,obj) in _pendingEventCountsByNameAndObj
class _EventExecuter(object):
"""Facilitates execution of a chain of event functions.
L{gen} generates the event functions and positional arguments.
L{next} calls the next function in the chain.
"""
def __init__(self, eventName, obj, kwargs):
self.kwargs = kwargs
self._gen = self.gen(eventName, obj)
try:
self.next()
except StopIteration:
pass
del self._gen
def next(self):
func, args = next(self._gen)
try:
return func(*args, **self.kwargs)
except TypeError:
log.warning("Could not execute function {func} defined in {module} module; kwargs: {kwargs}".format(
func=func.__name__,
module=func.__module__ or "unknown",
kwargs=self.kwargs
), exc_info=True)
return extensionPoints.callWithSupportedKwargs(func, *args, **self.kwargs)
def gen(self, eventName, obj):
funcName = "event_%s" % eventName
# Global plugin level.
for plugin in globalPluginHandler.runningPlugins:
func = getattr(plugin, funcName, None)
if func:
yield func, (obj, self.next)
# App module level.
app = obj.appModule
if app:
func = getattr(app, funcName, None)
if func:
yield func, (obj, self.next)
# Tree interceptor level.
treeInterceptor = obj.treeInterceptor
if treeInterceptor:
func = getattr(treeInterceptor, funcName, None)
if func and (getattr(func,'ignoreIsReady',False) or treeInterceptor.isReady):
yield func, (obj, self.next)
# NVDAObject level.
func = getattr(obj, funcName, None)
if func:
yield func, ()
WAS_GAIN_FOCUS_OBJ_ATTR_NAME = "wasGainFocusObj"
def _trackFocusObject(eventName, obj) -> None:
""" Keeps track of lastQueuedFocusObject and sets wasGainFocusObj attr on objects.
:param eventName: the event type, eg "gainFocus"
:param obj: the object to track if focused
"""
global lastQueuedFocusObject
if eventName == "gainFocus":
lastQueuedFocusObject = obj
setattr(obj, WAS_GAIN_FOCUS_OBJ_ATTR_NAME, obj is lastQueuedFocusObject)
def _getFocusLossCancellableSpeechCommand(
obj,
reason: controlTypes.OutputReason
) -> Optional[_CancellableSpeechCommand]:
if reason != controlTypes.REASON_FOCUS or not speech.manager._shouldCancelExpiredFocusEvents():
return None
from NVDAObjects import NVDAObject
if not isinstance(obj, NVDAObject):
log.warning("Unhandled object type. Expected all objects to be descendant from NVDAObject")
return None
previouslyHadFocus: bool = getattr(
obj,
WAS_GAIN_FOCUS_OBJ_ATTR_NAME,
False
)
def isSpeechStillValid():
from eventHandler import lastQueuedFocusObject
isLastFocusObj: bool = obj is lastQueuedFocusObject
stillValid = isLastFocusObj or not previouslyHadFocus
log._speechManagerDebug(
"checked if valid (isLast: %s, previouslyHad: %s): %s",
isLastFocusObj,
previouslyHadFocus,
obj.name
)
return stillValid
return _CancellableSpeechCommand(isSpeechStillValid)
def executeEvent(eventName, obj, **kwargs):
"""Executes an NVDA event.
@param eventName: the name of the event type (e.g. 'gainFocus', 'nameChange')
@type eventName: string
@param obj: the object the event is for
@type obj: L{NVDAObjects.NVDAObject}
@param kwargs: Additional event parameters as keyword arguments.
"""
try:
isGainFocus = eventName == "gainFocus"
# Allow NVDAObjects to redirect focus events to another object of their choosing.
if isGainFocus and obj.focusRedirect:
obj=obj.focusRedirect
sleepMode=obj.sleepMode
if isGainFocus and speech.manager._shouldCancelExpiredFocusEvents():
log._speechManagerDebug("executeEvent: Removing cancelled speech commands.")
# ask speechManager to check if any of it's queued utterances should be cancelled
speech._manager.removeCancelledSpeechCommands()
# Don't skip objects without focus here. Even if an object no longer has focus, it needs to be processed
# to capture changes in document depth. For instance jumping into a list?
# This needs further investigation; when the next object gets focus, it should
# allow us to capture this information?
if isGainFocus and not doPreGainFocus(obj, sleepMode=sleepMode):
return
elif not sleepMode and eventName=="documentLoadComplete" and not doPreDocumentLoadComplete(obj):
return
elif not sleepMode:
_EventExecuter(eventName,obj,kwargs)
except:
log.exception("error executing event: %s on %s with extra args of %s"%(eventName,obj,kwargs))
def doPreGainFocus(obj,sleepMode=False):
oldForeground=api.getForegroundObject()
oldFocus=api.getFocusObject()
oldTreeInterceptor=oldFocus.treeInterceptor if oldFocus else None
api.setFocusObject(obj)
if globalVars.focusDifferenceLevel<=1:
newForeground=api.getDesktopObject().objectInForeground()
if not newForeground:
log.debugWarning("Can not get real foreground, resorting to focus ancestors")
ancestors=api.getFocusAncestors()
if len(ancestors)>1:
newForeground=ancestors[1]
else:
newForeground=obj
api.setForegroundObject(newForeground)
executeEvent('foreground',newForeground)
if sleepMode: return True
#Fire focus entered events for all new ancestors of the focus if this is a gainFocus event
for parent in globalVars.focusAncestors[globalVars.focusDifferenceLevel:]:
executeEvent("focusEntered",parent)
if obj.treeInterceptor is not oldTreeInterceptor:
if hasattr(oldTreeInterceptor,"event_treeInterceptor_loseFocus"):
oldTreeInterceptor.event_treeInterceptor_loseFocus()
if obj.treeInterceptor and obj.treeInterceptor.isReady and hasattr(obj.treeInterceptor,"event_treeInterceptor_gainFocus"):
obj.treeInterceptor.event_treeInterceptor_gainFocus()
return True
def doPreDocumentLoadComplete(obj):
focusObject=api.getFocusObject()
if (not obj.treeInterceptor or not obj.treeInterceptor.isAlive or obj.treeInterceptor.shouldPrepare) and (obj==focusObject or obj in api.getFocusAncestors()):
ti=treeInterceptorHandler.update(obj)
if ti:
obj.treeInterceptor=ti
#Focus may be in this new treeInterceptor, so force focus to look up its treeInterceptor
focusObject.treeInterceptor=treeInterceptorHandler.getTreeInterceptor(focusObject)
return True
#: set of (eventName, processId, windowClassName) of events to accept.
_acceptEvents = set()
#: Maps process IDs to sets of events so they can be cleaned up when the process exits.
_acceptEventsByProcess = {}
def requestEvents(eventName=None, processId=None, windowClassName=None):
"""Request that particular events be accepted from a platform API.
Normally, L{shouldAcceptEvent} rejects certain events, including
most show events, events indicating changes in background processes, etc.
This function allows plugins to override this for specific cases;
e.g. to receive show events from a specific control or
to receive certain events even when in the background.
Note that NVDA may block some events at a lower level and doesn't listen for some event types at all.
In these cases, you will not be able to override this.
This should generally be called when a plugin is instantiated.
All arguments must be provided.
"""
if not eventName or not processId or not windowClassName:
raise ValueError("eventName, processId or windowClassName not specified")
entry = (eventName, processId, windowClassName)
procEvents = _acceptEventsByProcess.get(processId)
if not procEvents:
procEvents = _acceptEventsByProcess[processId] = set()
procEvents.add(entry)
_acceptEvents.add(entry)
def handleAppTerminate(appModule):
global _acceptEvents
events = _acceptEventsByProcess.pop(appModule.processID, None)
if not events:
return
_acceptEvents -= events
def shouldAcceptEvent(eventName, windowHandle=None):
"""Check whether an event should be accepted from a platform API.
Creating NVDAObjects and executing events can be expensive
and might block the main thread noticeably if the object is slow to respond.
Therefore, this should be used before NVDAObject creation to filter out any unnecessary events.
A platform API handler may do its own filtering before this.
"""
if not windowHandle:
# We can't filter without a window handle.
return True
wClass = winUser.getClassName(windowHandle)
key = (eventName,
winUser.getWindowThreadProcessID(windowHandle)[0],
wClass)
if key in _acceptEvents:
return True
if eventName == "valueChange" and config.conf["presentation"]["progressBarUpdates"]["reportBackgroundProgressBars"]:
return True
if eventName == "hide":
return False
if eventName == "show":
# Only accept 'show' events for specific cases, as otherwise we get flooded.
return wClass in (
"Frame Notification Bar", # notification bars
"tooltips_class32", # tooltips
"mscandui21.candidate", "mscandui40.candidate", "MSCandUIWindow_Candidate", # IMM candidates
"TTrayAlert", # 5405: Skype
)
if eventName == "alert" and winUser.getClassName(winUser.getAncestor(windowHandle, winUser.GA_PARENT)) == "ToastChildWindowClass":
# Toast notifications.
return True
if eventName in ("menuEnd", "switchEnd", "desktopSwitch"):
# #5302, #5462: These events can be fired on the desktop window
# or windows that would otherwise be blocked.
# Platform API handlers will translate these events to focus events anyway,
# so we must allow them here.
return True
if windowHandle == winUser.getDesktopWindow():
# #5595: Events for the cursor get mapped to the desktop window.
return True
# #6713: Edge (and soon all UWP apps) will no longer have windows as descendants of the foreground window.
# However, it does look like they are always equal to or descendants of the "active" window of the input thread.
if wClass.startswith('Windows.UI.Core'):
gi=winUser.getGUIThreadInfo(0)
if winUser.isDescendantWindow(gi.hwndActive,windowHandle):
return True
fg = winUser.getForegroundWindow()
fgClassName=winUser.getClassName(fg)
if wClass == "NetUIHWND" and fgClassName in ("Net UI Tool Window Layered","Net UI Tool Window"):
# #5504: In Office >= 2013 with the ribbon showing only tabs,
# when a tab is expanded, the window we get from the focus object is incorrect.
# This window isn't beneath the foreground window,
# so our foreground application checks fail.
# Just compare the root owners.
if winUser.getAncestor(windowHandle, winUser.GA_ROOTOWNER) == winUser.getAncestor(fg, winUser.GA_ROOTOWNER):
return True
if (winUser.isDescendantWindow(fg, windowHandle)
# #3899, #3905: Covers cases such as the Firefox Page Bookmarked window and OpenOffice/LibreOffice context menus.
or winUser.isDescendantWindow(fg, winUser.getAncestor(windowHandle, winUser.GA_ROOTOWNER))):
# This is for the foreground application.
return True
if (winUser.user32.GetWindowLongW(windowHandle, winUser.GWL_EXSTYLE) & winUser.WS_EX_TOPMOST
or winUser.user32.GetWindowLongW(winUser.getAncestor(windowHandle, winUser.GA_ROOT), winUser.GWL_EXSTYLE) & winUser.WS_EX_TOPMOST):
# This window or its root is a topmost window.
# This includes menus, combo box pop-ups and the task switching list.
return True
return False