Skip to content

Commit

Permalink
Correct errors related to Natlink window-specific rule activation
Browse files Browse the repository at this point in the history
Some of these changes result in more calls to the activate_rule()
engine procedure.  This not appear to be a problem for the other
engines.
  • Loading branch information
drmfinlay committed Jul 23, 2022
1 parent ef28d2c commit c20c83f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
24 changes: 16 additions & 8 deletions dragonfly/engines/backend_natlink/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ 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.
# Note: These variables will be updated appropriately when speech is
# detected.
self._current_window_handle = 0
self._last_window_handle = 0

def apply_threading_fix(self):
Expand Down Expand Up @@ -295,14 +296,21 @@ def activate_rule(self, rule, 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
handle1 = self._last_window_handle
handle2 = self._current_window_handle

# Deactivate the rule on a window change, otherwise activation will
# not take place.
if handle1 != handle2:
grammar_object.deactivate(rule.name)

# Activate the rule for the current window.
self._last_window_handle = handle2
try:
grammar_object.activate(rule.name, self._last_window_handle)
grammar_object.activate(rule.name, handle2)
except self.natlink.BadWindow:
pass
grammar_object.deactivate(rule.name)

def deactivate_rule(self, rule, grammar):
self._log.debug("Deactivating rule %s in grammar %s." % (rule.name, grammar.name))
Expand Down Expand Up @@ -448,7 +456,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.engine._current_window_handle = handle
self.grammar.process_begin(executable, title, handle)

def _decode_grammar_rules(self, state, words, results, *args):
Expand Down
6 changes: 5 additions & 1 deletion dragonfly/grammar/grammar_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,13 @@ def process_begin(self, executable, title, handle):
elif not self._context \
or self._context.matches(executable, title, handle):
# Grammar is within context.
# Note: It is sometimes necessary to synchronize grammar rule
# activation. This is the case for the DNS/Natlink back-end
# when window-specific rule activation is in use.
if not self._in_context:
self._in_context = True
self.enter_context()
#self.enter_context()
self.enter_context()
self._process_begin(executable, title, handle)
for r in self._rules:
if r.exported and hasattr(r, "process_begin"):
Expand Down
23 changes: 16 additions & 7 deletions dragonfly/grammar/rule_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,22 @@ def process_begin(self, executable, title, handle):
if self._active:
self.deactivate()
return

# Note: It is sometimes necessary to synchronize grammar rule
# activation. This is the case for the DNS/Natlink back-end when
# window-specific rule activation is in use.
if self._context:
if self._context.matches(executable, title, handle):
if not self._active:
self.activate()
#if not self.active:
# self.activate()
self.activate()
self._process_begin()
else:
if self._active:
self.deactivate()
else:
# Always instruct the engine to activate non-contextual rules.
# This is necessary for the DNS back-end only.
#if not self.active:
# self.activate()
self.activate()
self._process_begin()

Expand All @@ -234,9 +239,13 @@ def activate(self, force=False):
if self._active:
self.deactivate()
return
if not self._active or force:
self._grammar.activate_rule(self)
self._active = True

# Note: It is sometimes necessary to synchronize grammar rule
# activation. This is the case for the DNS/Natlink back-end when
# window-specific rule activation is in use.
#if not self._active or force:
self._grammar.activate_rule(self)
self._active = True

def deactivate(self):
if not self._grammar:
Expand Down

0 comments on commit c20c83f

Please sign in to comment.