From 6dc65287a91843805d211d5b5d653c508bb7252d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 16 Mar 2015 22:30:06 +0100 Subject: [PATCH] Discard uninteresting events early in eventFilter. Before, we ran quite a lot of code (e.g. objreg) on every event, even if it turns out to not be a keypress/release event at all. --- qutebrowser/keyinput/modeman.py | 48 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 667408842..e43b47e7b 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -103,15 +103,29 @@ class EventFilter(QObject): def eventFilter(self, obj, event): """Forward events to the correct modeman.""" - if not self._activated: - return False try: - modeman = objreg.get('mode-manager', scope='window', - window='current') - return modeman.eventFilter(obj, event) - except objreg.RegistryUnavailableError: - # No window available yet, or not a MainWindow - return False + if not self._activated: + return False + if event.type() not in [QEvent.KeyPress, QEvent.KeyRelease]: + # We're not interested in non-key-events so we pass them + # through. + return False + if not isinstance(obj, QWindow): + # We already handled this same event at some point earlier, so + # we're not interested in it anymore. + return False + if (QApplication.instance().activeWindow() not in + objreg.window_registry.values()): + # Some other window (print dialog, etc.) is focused so we pass + # the event through. + return False + try: + modeman = objreg.get('mode-manager', scope='window', + window='current') + return modeman.eventFilter(event) + except objreg.RegistryUnavailableError: + # No window available yet, or not a MainWindow + return False except: # If there is an exception in here and we leave the eventfilter # activated, we'll get an infinite loop and a stack overflow. @@ -313,7 +327,7 @@ class ModeManager(QObject): self._forward_unbound_keys = config.get( 'input', 'forward-unbound-keys') - def eventFilter(self, obj, event): + def eventFilter(self, event): """Filter all events based on the currently set mode. Also calls the real keypress handler. @@ -327,21 +341,7 @@ class ModeManager(QObject): if self.mode is None: # We got events before mode is set, so just pass them through. return False - typ = event.type() - if typ not in [QEvent.KeyPress, QEvent.KeyRelease]: - # We're not interested in non-key-events so we pass them through. - return False - if not isinstance(obj, QWindow): - # We already handled this same event at some point earlier, so - # we're not interested in it anymore. - return False - if (QApplication.instance().activeWindow() not in - objreg.window_registry.values()): - # Some other window (print dialog, etc.) is focused so we pass - # the event through. - return False - - if typ == QEvent.KeyPress: + if event.type() == QEvent.KeyPress: return self._eventFilter_keypress(event) else: return self._eventFilter_keyrelease(event)