Example #1
0
def print_all_window_details():
    for window in Window.get_all_windows():
        if window.is_visible:
            window = Window.get_foreground()
            logger.info('Window details')
            logger.info('title=' + window.title)
            logger.info('executable=' + window.executable)
Example #2
0
    def process_grammars_context(self, window=None):
        """
            Enable/disable grammars & rules based on their current contexts.

            This must be done preemptively for some SR engine back-ends,
            such as WSR, that don't apply context changes upon/after the
            utterance start has been detected. The WSR engine should call
            this automatically whenever the foreground application (or its
            title) changes. The user may want to call this manually to
            update when custom contexts.

            The *window* parameter is optional window information, which can
            be passed in as an optimization if it has already been gathered.

        """

        if window is None:
            from dragonfly.windows.window import Window
            window = Window.get_foreground()
        for grammar in self.grammars:
            # Prevent 'notify_begin()' from being called.
            if grammar.name == "_recobs_grammar":
                continue
            grammar.process_begin(window.executable, window.title,
                                  window.handle)
Example #3
0
 def focus_was_success(title, executable):
     w=Window.get_foreground()
     success=True
     if title!=None:
         success=title in w.title
     if success and executable!=None:
         success=executable in w.executable
     return success
Example #4
0
def _ensure_execution_context(data):
    '''Populates the data field of execute with context information if
    not present.'''
    if data is None:
        data = {}
    if '_context' not in data:
        data['_context'] = Window.get_foreground()
    return data
Example #5
0
 def callback(hWinEventHook, event, hwnd, idObject, idChild,
              dwEventThread, dwmsEventTime):
     window = Window.get_foreground()
     # Note: hwnd doesn't always match window.handle, even when
     # foreground window changed (and sometimes it didn't change)
     window_changed = (
         window != self._last_foreground_window
         or window == self._last_foreground_window
         and window.title != self._last_foreground_window_title)
     if window_changed:
         self.process_grammars_context(window)
         self._last_foreground_window = window
         self._last_foreground_window_title = window.title
Example #6
0
    def _focus_window_after_starting(self, pid):
        timeout = 1.0
        exe = self._args[0]

        # Wait for the window to appear for N seconds.
        action = WaitWindow(executable=exe, timeout=timeout)
        if action.execute():
            # Bring the window to the foreground.
            Window.get_foreground().set_foreground()

        # The application window wasn't focused, so try to focus it.
        else:
            target = pid
            start = time.time()
            while time.time() - start < timeout:
                found = False
                for window in Window.get_matching_windows(exe):
                    if target is None or window.pid == target:
                        window.set_foreground()
                        found = True
                        break
                if found:
                    break
 def _execute(self, data=None):
     self._log.debug("Waiting for window context: %s" % self)
     start_time = time.time()
     while 1:
         foreground = Window.get_foreground()
         mismatch = False
         for match_name in self._match_functions:
             match_func = getattr(self, match_name)
             if not match_func(foreground):
                 mismatch = True
                 break
         if not mismatch:
             return
         if time.time() - start_time > self._timeout:
             raise ActionError("Timeout while waiting for window context: %s" % self)
Example #8
0
 def _compute_kaldi_rules_activity(self, phrase_start=True):
     window_info = {}
     if phrase_start:
         fg_window = Window.get_foreground()
         window_info = {
             "executable": fg_window.executable,
             "title": fg_window.title,
             "handle": fg_window.handle,
         }
         for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically():
             grammar_wrapper.phrase_start_callback(**window_info)
     self.prepare_for_recognition()
     self._active_kaldi_rules = set()
     self._kaldi_rules_activity = [False] * self._compiler.num_kaldi_rules
     for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically():
         if grammar_wrapper.active and (not self._any_exclusive_grammars or grammar_wrapper.exclusive):
             for kaldi_rule in grammar_wrapper.kaldi_rule_by_rule_dict.values():
                 if kaldi_rule.active:
                     self._active_kaldi_rules.add(kaldi_rule)
                     self._kaldi_rules_activity[kaldi_rule.id] = True
     self._log.debug("active kaldi_rules (from window %s): %s", window_info, [kr.name for kr in self._active_kaldi_rules])
     return self._kaldi_rules_activity
Example #9
0
def get_active_window_path():
    return Window.get_foreground().executable
Example #10
0
 def _process_recognition(self, node, extras):
     window=Window.get_foreground()
     extras['operation'](window)
Example #11
0
 def _process_recognition(self, node, extras):
     monitor_number = int(extras['monitor']) - 1
     self.move_to_monitor(Window.get_foreground(), monitor_number)
Example #12
0
 def _process_recognition(self, node, extras):
     self.snap_window(Window.get_foreground(),extras['region'])
Example #13
0
 def _process_recognition(self,node,extras):
     window=Window.get_foreground()
     print 'Window details'
     print 'title=',window.title
     print 'executable=',window.executable
     print 'window=',window
Example #14
0
 def _process_recognition(self, node, extras):
     window = Window.get_foreground()
     extras['operation'](window)
Example #15
0
 def _process_recognition(self, node, extras):
     self.snap_window(Window.get_foreground(), extras['region'])
Example #16
0
 def _process_recognition(self, node, extras):
     window = Window.get_foreground()
     logger.info('Window details')
     logger.info('title=' + window.title)
     logger.info('executable=' + window.executable)
Example #17
0
 def _process_recognition(self, node, extras):
     monitor_number=int(extras['monitor'])-1
     self.move_to_monitor(Window.get_foreground(),monitor_number)
Example #18
0
    def mimic(self, words, **kwargs):
        """
        Mimic a recognition of the given *words*.

        :param words: words to mimic
        :type words: str|iter
        :Keyword Arguments:

           optional *executable*, *title* and/or *handle* keyword arguments
           may be used to simulate a specific foreground window context. The
           current foreground window attributes will be used instead for any
           keyword arguments not present.

        .. note::

           Any dictation words should be all uppercase, e.g. "HELLO WORLD".
           Dictation words not in uppercase will result in the engine
           **not** decoding and recognizing the command!
        """
        # Handle string input.
        if isinstance(words, string_types):
            words = words.split()

        # Don't allow non-iterable objects.
        if not iter(words):
            raise TypeError("%r is not a string or other iterable object" %
                            words)

        # Fail on empty input.
        if not words:
            raise MimicFailure("Invalid mimic input %r" % words)

        # Notify observers that a recognition has begun.
        self._recognition_observer_manager.notify_begin()

        # Generate the input for process_words.
        words_rules = self.generate_words_rules(words)

        w = Window.get_foreground()
        process_args = {
            "executable": w.executable,
            "title": w.title,
            "handle": w.handle,
        }
        # Allows optional passing of window attributes to mimic
        process_args.update(kwargs)

        # Call process_begin() for each grammar wrapper. Use a copy of
        # _grammar_wrappers in case it changes.
        for wrapper in self._grammar_wrappers.copy().values():
            wrapper.process_begin(**process_args)

        # Take another copy of _grammar_wrappers to use for processing.
        grammar_wrappers = self._grammar_wrappers.copy().values()

        # Count exclusive grammars.
        exclusive_count = 0
        for wrapper in grammar_wrappers:
            if wrapper.exclusive:
                exclusive_count += 1

        # Call process_words() for each grammar wrapper, stopping early if
        # processing occurred.
        processing_occurred = False
        for wrapper in grammar_wrappers:
            # Skip non-exclusive grammars if there are one or more exclusive
            # grammars.
            if exclusive_count > 0 and not wrapper.exclusive:
                continue

            # Process the grammar.
            processing_occurred = wrapper.process_words(words_rules)
            if processing_occurred:
                break

        # If no processing occurred, then the mimic failed.
        if not processing_occurred:
            self._recognition_observer_manager.notify_failure(None)
            raise MimicFailure("No matching rule found for words %r." %
                               (words, ))
 def _execute(self, data=None):
     foreground = Window.get_foreground()
     if foreground.title.find("Emacs editor") != -1:
         Key("c-y").execute()
     else:
         Key("c-v").execute()
Example #20
0
 def phrase_start_callback(self, stream_number, stream_position):
     window = Window.get_foreground()
     self.grammar.process_begin(window.executable, window.title,
                                window.handle)
Example #21
0
 def _execute_events(self, events):
     """ Send events. """
     window = Window.get_foreground()
     for event in events:
         event.execute(window)
Example #22
0
def get_active_window_path():
    return Window.get_foreground().executable
 def _execute(self, data=None):
     foreground = Window.get_foreground()
     if foreground.title.find("Emacs editor") != -1:
         Key("c-y").execute()
     else:
         Key("c-v").execute()