Skip to content

Commit

Permalink
Change the Natlink engine to use window-specific rule activation
Browse files Browse the repository at this point in the history
Natlink allows activating rules globally or for specific windows.
Dragonfly now uses the latter instead of the former, since there is
no real reason why Dragonfly should use the global context; it is
enough to, at speech start, activate rules for the foreground window
only.
  • Loading branch information
drmfinlay committed Jul 19, 2022
1 parent dcd80a5 commit 3c0a531
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions dragonfly/engines/backend_natlink/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

from six import text_type, binary_type, string_types, PY2


from dragonfly.engines.base import (EngineBase, EngineError, MimicFailure,
GrammarWrapperBase)
from dragonfly.engines.backend_natlink.speaker import NatlinkSpeaker
Expand Down Expand Up @@ -148,6 +147,10 @@ def __init__(self, retain_dir=None):
self._retain_dir = None
self._log.error(err)

# Note: The default value of this variable will be overwritten when
# speech is first detected.
self._last_window_handle = 0

def apply_threading_fix(self):
"""
Start a thread and engine timer internally to allow Python threads
Expand Down Expand Up @@ -282,8 +285,15 @@ def activate_rule(self, rule, grammar):
wrapper = self._get_grammar_wrapper(grammar)
if not wrapper:
return

# Activate the rule.
# Note: The rule is only activated for the current window, but this
# should not be a problem.
grammar_object = wrapper.grammar_object
grammar_object.activate(rule.name, 0)
try:
grammar_object.activate(rule.name, self._last_window_handle)
except self.natlink.BadWindow:
pass

def deactivate_rule(self, rule, grammar):
self._log.debug("Deactivating rule %s in grammar %s." % (rule.name, grammar.name))
Expand Down Expand Up @@ -428,6 +438,7 @@ def __init__(self, grammar, grammar_object, engine, recobs_manager):
def begin_callback(self, module_info):
executable, title, handle = tuple(map_word(word)
for word in module_info)
self.engine._last_window_handle = handle
self.grammar.process_begin(executable, title, handle)

def _decode_grammar_rules(self, state, words, results, *args):
Expand Down
5 changes: 3 additions & 2 deletions dragonfly/grammar/rule_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ def process_begin(self, executable, title, handle):
if self._active:
self.deactivate()
else:
if not self._active:
self.activate()
# Always instruct the engine to activate non-contextual rules.
# This is necessary for the DNS back-end only.
self.activate()
self._process_begin()

def activate(self, force=False):
Expand Down

0 comments on commit 3c0a531

Please sign in to comment.