[orca] Event Manager: Clear queue of spam during flood if given a priority event



commit 477a605a2d9b46d6dc9e5eb611004c40d0c6a9b1
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Sun Jun 13 17:13:53 2021 +0200

    Event Manager: Clear queue of spam during flood if given a priority event
    
    This commit makes it possible to specify events that, if received during
    an event flood, should trigger pruning of spam events.
    
    At the present time, the only event which triggers pruning is a focus
    claim. We might wish to expand that to include other events after we
    see that there are no unexpected side effects from this change.

 src/orca/event_manager.py | 44 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)
---
diff --git a/src/orca/event_manager.py b/src/orca/event_manager.py
index 2a29151a8..37b20df8d 100644
--- a/src/orca/event_manager.py
+++ b/src/orca/event_manager.py
@@ -331,7 +331,7 @@ class EventManager:
         if debug.debugEventQueue:
             debug.println(debug.LEVEL_ALL, "           ...released")
 
-    def _queuePrintln(self, e, isEnqueue=True):
+    def _queuePrintln(self, e, isEnqueue=True, isPrune=None):
         """Convenience method to output queue-related debugging info."""
 
         if isinstance(e, input_event.KeyboardEvent):
@@ -347,7 +347,11 @@ class EventManager:
         else:
             return
 
-        if isEnqueue:
+        if isPrune:
+            string = "EVENT MANAGER: Pruning %s %s" % (e.type, data)
+        elif isPrune is not None:
+            string = "EVENT MANAGER: Not pruning %s %s" % (e.type, data)
+        elif isEnqueue:
             string = "EVENT MANAGER: Queueing %s %s" % (e.type, data)
         else:
             string = "EVENT MANAGER: Dequeued %s %s" % (e.type, data)
@@ -384,6 +388,11 @@ class EventManager:
 
         self._queuePrintln(e)
 
+        if self._inFlood() and self._prioritizeDuringFlood(e):
+            msg = 'EVENT MANAGER: Pruning event queue due to flood.'
+            debug.println(debug.LEVEL_INFO, msg, True)
+            self._pruneEventsDuringFlood()
+
         asyncMode = self._asyncMode
         if isObjectEvent:
             app = e.source.getApplication()
@@ -751,6 +760,37 @@ class EventManager:
 
         return event.source == orca_state.locusOfFocus
 
+    def _prioritizeDuringFlood(self, event):
+        """Returns true if this event should be processed immediately during a flood."""
+
+        if event.type.startswith("object:state-changed:focused"):
+            return event.detail1
+
+        return False
+
+    def _pruneEventsDuringFlood(self):
+        """Gets rid of events we don't care about during a flood."""
+
+        oldSize = self._eventQueue.qsize()
+
+        newQueue = queue.Queue(0)
+        while not self._eventQueue.empty():
+            try:
+                event = self._eventQueue.get()
+            except Empty:
+                continue
+
+            if self._processDuringFlood(event):
+                newQueue.put(event)
+                self._queuePrintln(event, isPrune=False)
+            self._eventQueue.task_done()
+
+        self._eventQueue = newQueue
+        newSize = self._eventQueue.qsize()
+
+        msg = 'EVENT MANAGER: %i events pruned. New size: %i' % ((oldSize - newSize), newSize)
+        debug.println(debug.LEVEL_INFO, msg, True)
+
     def _inFlood(self):
         size = self._eventQueue.qsize()
         if size > 75:


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