[orca] Event manager improvements



commit 2366d4c24ef29016781fd161f033375f22cdd8ff
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Mon Dec 26 15:00:57 2011 -0500

    Event manager improvements
    
    * The event manager should handle all event registration/deregistration
    * The event manager should make it possible for non-script orca modules
      to register/deregister events

 src/orca/event_manager.py   |   70 ++++++++++++++++++++++++++++++++----------
 src/orca/mouse_review.py    |   11 ++++---
 src/orca/orca.py            |   48 +-----------------------------
 src/orca/script_manager.py  |   10 +++---
 src/orca/scripts/default.py |   52 +++++++++++++++++---------------
 5 files changed, 93 insertions(+), 98 deletions(-)
---
diff --git a/src/orca/event_manager.py b/src/orca/event_manager.py
index 4a203ac..7772e7f 100644
--- a/src/orca/event_manager.py
+++ b/src/orca/event_manager.py
@@ -46,7 +46,7 @@ class EventManager:
 
     def __init__(self):
 
-        self._listenerCounts = {}
+        self._scriptListenerCounts = {}
         self.registry = pyatspi.Registry
         self._enqueueCount = 0
         self._dequeueCount = 0
@@ -67,7 +67,8 @@ class EventManager:
 
         self._registerListener("window:activate")
         self._registerListener("window:deactivate")
-        self._registerListener("object:children-changed:remove")
+        self._registerListener("object:children-changed")
+        self._registerListener("mouse:button")
 
         win = orca_state.activeScript.utilities.activeWindow()
         if win:
@@ -96,9 +97,9 @@ class EventManager:
     def deactivate(self):
         """Called when this event manager is deactivated."""
 
-        for eventType in self._listenerCounts.keys():
+        for eventType in self._scriptListenerCounts.keys():
             self.registry.deregisterEventListener(self._enqueue, eventType)
-        self._listenerCounts = {}
+        self._scriptListenerCounts = {}
 
     def _ignore(self, event):
         """Returns True if this event should be ignored."""
@@ -297,11 +298,11 @@ class EventManager:
         - eventType: the event type.
         """
 
-        if eventType in self._listenerCounts:
-            self._listenerCounts[eventType] += 1
+        if eventType in self._scriptListenerCounts:
+            self._scriptListenerCounts[eventType] += 1
         else:
             self.registry.registerEventListener(self._enqueue, eventType)
-            self._listenerCounts[eventType] = 1
+            self._scriptListenerCounts[eventType] = 1
 
     def _deregisterListener(self, eventType):
         """Tells this module to stop listening for the given event type.
@@ -310,17 +311,17 @@ class EventManager:
         - eventType: the event type.
         """
 
-        if not eventType in self._listenerCounts:
+        if not eventType in self._scriptListenerCounts:
             return
 
-        self._listenerCounts[eventType] -= 1
-        if self._listenerCounts[eventType] == 0:
+        self._scriptListenerCounts[eventType] -= 1
+        if self._scriptListenerCounts[eventType] == 0:
             self.registry.deregisterEventListener(self._enqueue, eventType)
-            del self._listenerCounts[eventType]
+            del self._scriptListenerCounts[eventType]
 
-    def registerListeners(self, script):
-        """Tells the FocusTrackingPresenter to listen for all
-        the event types of interest to the script.
+    def registerScriptListeners(self, script):
+        """Tells the event manager to start listening for all the event types
+        of interest to the script.
 
         Arguments:
         - script: the script.
@@ -329,9 +330,9 @@ class EventManager:
         for eventType in script.listeners.keys():
             self._registerListener(eventType)
 
-    def deregisterListeners(self, script):
-        """Tells the FocusTrackingPresenter to stop listening for all the
-        event types of interest to the script.
+    def deregisterScriptListeners(self, script):
+        """Tells the event manager to stop listening for all the event types
+        of interest to the script.
 
         Arguments:
         - script: the script.
@@ -340,6 +341,41 @@ class EventManager:
         for eventType in script.listeners.keys():
             self._deregisterListener(eventType)
 
+    def registerModuleListeners(self, listeners):
+        """Register the listeners on behalf of the caller."""
+
+        for eventType, function in listeners.items():
+            self.registry.registerEventListener(function, eventType)
+
+    def deregisterModuleListeners(self, listeners):
+        """Deegister the listeners on behalf of the caller."""
+
+        for eventType, function in listeners.items():
+            self.registry.deregisterEventListener(function, eventType)
+
+    def registerKeystrokeListener(self, function, mask=None, kind=None):
+        """Register the keystroke listener on behalf of the caller."""
+
+        if mask == None:
+            mask = range(256)
+
+        if kind == None:
+            kind = (pyatspi.KEY_PRESSED_EVENT, pyatspi.KEY_RELEASED_EVENT)
+
+        self.registry.registerKeystrokeListener(function, mask=mask, kind=kind)
+
+    def deregisterKeystrokeListener(self, function, mask=None, kind=None):
+        """Deregister the keystroke listener on behalf of the caller."""
+
+        if mask == None:
+            mask = range(256)
+
+        if kind == None:
+            kind = (pyatspi.KEY_PRESSED_EVENT, pyatspi.KEY_RELEASED_EVENT)
+
+        self.registry.deregisterKeystrokeListener(
+            function, mask=mask, kind=kind)
+
     def _processInputEvent(self, event):
         """Processes the given input event based on the keybinding from the
         currently-active script.
diff --git a/src/orca/mouse_review.py b/src/orca/mouse_review.py
index 2b27a4b..aec3ea9 100644
--- a/src/orca/mouse_review.py
+++ b/src/orca/mouse_review.py
@@ -35,16 +35,17 @@ except:
                   "Python module wnck not found, mouse review not available.")
     _mouseReviewCapable = False
 
+import pyatspi
 from gi.repository import Gdk
 from gi.repository import GObject
 
 import orca
-import pyatspi
 import speech
 import braille
 import settings
 
 _scriptManager = getattr(orca, '_scriptManager')
+_eventManager = getattr(orca, '_eventManager')
 
 class BoundingBox:
     """A bounding box, currently it is used to test if a given point is
@@ -166,11 +167,11 @@ class MouseReviewer:
         if on is None:
             on = not self.active
         if on and not self.active:
-            pyatspi.Registry.registerEventListener(self._onMouseMoved,
-                                                   "mouse:abs")
+            _eventManager.registerModuleListeners(
+                {"mouse:abs":self._onMouseMoved})
         elif not on and self.active:
-            pyatspi.Registry.deregisterEventListener(self._onMouseMoved,
-                                                     "mouse:abs")
+            _eventManager.deregisterModuleListeners(
+                {"mouse:abs":self._onMouseMoved})
         self.active = on
 
     def _onMouseMoved(self, event):
diff --git a/src/orca/orca.py b/src/orca/orca.py
index f95e1a2..ca34087 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -548,7 +548,6 @@ import notification_messages
 
 from input_event import BrailleEvent
 from input_event import KeyboardEvent
-from input_event import MouseButtonEvent
 from input_event import keyEventToString
 
 import gc
@@ -683,24 +682,6 @@ def _onChildrenChanged(e):
             shutdown()
             return
 
-def _onMouseButton(e):
-    """Tracks mouse button events, stopping any speech in progress.
-
-    Arguments:
-    - e: at-spi event from the at-api registry
-    """
-
-    mouse_event = MouseButtonEvent(e)
-    orca_state.lastInputEvent = mouse_event
-
-    # A mouse button event looks like: mouse:button:1p, where the
-    # number is the button number and the 'p' is either 'p' or 'r',
-    # meaning pressed or released.  We only want to stop speech on
-    # button presses.
-    #
-    if mouse_event.pressed:
-        speech.stop()
-
 ########################################################################
 #                                                                      #
 # DEBUG support.                                                       #
@@ -1706,28 +1687,8 @@ def init(registry):
     #
     _keyBindings = keybindings.KeyBindings()
 
-    # Create and load an app's script when it is added to the desktop
-    #
-    registry.registerEventListener(_onChildrenChanged,
-                                   "object:children-changed")
-
-    # We also want to stop speech when a mouse button is pressed.
-    #
-    registry.registerEventListener(_onMouseButton,
-                                   "mouse:button")
-
     loadUserSettings()
-
-    masks = []
-    mask = 0
-    # Cover all masks in 8 bits.
-    while mask <= 255:
-        masks.append(mask)
-        mask += 1
-    pyatspi.Registry.registerKeystrokeListener(
-        _processKeyboardEvent,
-        mask=masks,
-        kind=(pyatspi.KEY_PRESSED_EVENT, pyatspi.KEY_RELEASED_EVENT))
+    _eventManager.registerKeystrokeListener(_processKeyboardEvent)
 
     if settings.timeoutCallback and (settings.timeoutTime > 0):
         signal.alarm(0)
@@ -1797,13 +1758,6 @@ def shutdown(script=None, inputEvent=None):
     #
     orca_state.activeScript.presentMessage(_("Goodbye."))
 
-    # Deregister our event listeners
-    #
-    pyatspi.Registry.deregisterEventListener(_onChildrenChanged,
-                                             "object:children-changed")
-    pyatspi.Registry.deregisterEventListener(_onMouseButton,
-                                             "mouse:button")
-
     _eventManager.deactivate()
     _scriptManager.deactivate()
 
diff --git a/src/orca/script_manager.py b/src/orca/script_manager.py
index 70b0d91..aed231c 100644
--- a/src/orca/script_manager.py
+++ b/src/orca/script_manager.py
@@ -199,7 +199,7 @@ class ScriptManager:
 
         import scripts.default as default
         script = default.Script(app)
-        _eventManager.registerListeners(script)
+        _eventManager.registerScriptListeners(script)
 
         if not app:
             self._defaultScript = script
@@ -226,7 +226,7 @@ class ScriptManager:
             if not toolkitScript:
                 toolkitScript = self._createScript(app, obj)
                 toolkitScripts[objToolkit] = toolkitScript
-                _eventManager.registerListeners(toolkitScript)
+                _eventManager.registerScriptListeners(toolkitScript)
             self.toolkitScripts[app] = toolkitScripts
 
         if not app:
@@ -236,7 +236,7 @@ class ScriptManager:
         else:
             appScript = self._createScript(app, None)
             self.appScripts[app] = appScript
-            _eventManager.registerListeners(appScript)
+            _eventManager.registerScriptListeners(appScript)
 
         # Only defer to the toolkit script for this object if the app script
         # is based on a different toolkit.
@@ -285,7 +285,7 @@ class ScriptManager:
         appList = filter(lambda a: a!= None and a not in desktop, appList)
         for app in appList:
             appScript = self.appScripts.pop(app)
-            _eventManager.deregisterListeners(appScript)
+            _eventManager.deregisterScriptListeners(appScript)
             del appScript
 
             try:
@@ -294,7 +294,7 @@ class ScriptManager:
                 pass
             else:
                 for toolkitScript in toolkitScripts.values():
-                    _eventManager.deregisterListeners(toolkitScript)
+                    _eventManager.deregisterScriptListeners(toolkitScript)
                     del toolkitScript
 
             del app
diff --git a/src/orca/scripts/default.py b/src/orca/scripts/default.py
index 86bb95c..91c5227 100644
--- a/src/orca/scripts/default.py
+++ b/src/orca/scripts/default.py
@@ -3119,30 +3119,36 @@ class Script(script.Script):
         - event: the Event
         """
 
+        mouseEvent = input_event.MouseButtonEvent(event)
+        orca_state.lastInputEvent = mouseEvent
+
+        if mouseEvent.pressed:
+            speech.stop()
+            return
+
         # If we've received a mouse button released event, then check if
         # there are and text selections for the locus of focus and speak
         # them.
         #
-        state = event.type[-1]
-        if state == "r":
-            obj = orca_state.locusOfFocus
-            try:
-                text = obj.queryText()
-            except:
-                pass
-            else:
-                [textContents, startOffset, endOffset] = \
-                    self.utilities.allSelectedText(obj)
-                if textContents:
-                    utterances = []
-                    utterances.append(textContents)
-
-                    # Translators: when the user selects (highlights) text in
-                    # a document, Orca lets them know this.
-                    #
-                    utterances.append(C_("text", "selected"))
-                    speech.speak(utterances)
-                self.updateBraille(orca_state.locusOfFocus)
+        obj = orca_state.locusOfFocus
+        try:
+            text = obj.queryText()
+        except:
+            return
+
+        self.updateBraille(orca_state.locusOfFocus)
+        textContents = self.utilities.allSelectedText(obj)[0]
+        if not textContents:
+            return
+
+        utterances = []
+        utterances.append(textContents)
+
+        # Translators: when the user selects (highlights) text in
+        # a document, Orca lets them know this.
+        #
+        utterances.append(C_("text", "selected"))
+        speech.speak(utterances)
 
     def onNameChanged(self, event):
         """Called whenever a property on an object changes.
@@ -3506,10 +3512,8 @@ class Script(script.Script):
         #
         string = event.any_data
         speakThis = False
-        if isinstance(orca_state.lastInputEvent, input_event.MouseButtonEvent) \
-           and orca_state.lastInputEvent.button == "2":
-            speakThis = True
-
+        if isinstance(orca_state.lastInputEvent, input_event.MouseButtonEvent):
+            speakThis = orca_state.lastInputEvent.button == "2"
         else:
             keyString, mods = self.utilities.lastKeyAndModifiers()
             wasCommand = mods & settings.COMMAND_MODIFIER_MASK



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]