1# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""An interface to access the local input facade."""
6
7
8import json
9import logging
10
11from autotest_lib.client.bin.input import input_event_recorder
12from autotest_lib.client.common_lib import error
13from autotest_lib.client.cros.graphics import graphics_utils
14
15
16class InputFacadeNativeError(Exception):
17    """Error in InputFacadeNative."""
18    pass
19
20
21class InputFacadeNative(object):
22    """Facade to access the record input events."""
23
24    def __init__(self):
25        """Initializes the input facade."""
26        self.recorder = None
27
28
29    def initialize_input_recorder(self, device_name):
30        """Initialize an input event recorder object.
31
32        @param device_name: the name of the input device to record.
33
34        """
35        self.recorder = input_event_recorder.InputEventRecorder(device_name)
36        logging.info('input event device: %s (%s)',
37                     self.recorder.device_name, self.recorder.device_node)
38
39
40    def clear_input_events(self):
41        """Clear the event list."""
42        if self.recorder is None:
43            raise error.TestError('input facade: input device name not given')
44        self.recorder.clear_events()
45
46
47    def start_input_recorder(self):
48        """Start the recording thread."""
49        if self.recorder is None:
50            raise error.TestError('input facade: input device name not given')
51        self.recorder.start()
52
53
54    def stop_input_recorder(self):
55        """Stop the recording thread."""
56        if self.recorder is None:
57            raise error.TestError('input facade: input device name not given')
58        self.recorder.stop()
59
60
61    def get_input_events(self):
62        """Get the bluetooth device input events.
63
64        @returns: the recorded input events.
65
66        """
67        if self.recorder is None:
68            raise error.TestError('input facade: input device name not given')
69        events = self.recorder.get_events()
70        return json.dumps(events)
71
72
73    def press_keys(self, key_list):
74        """ Simulating key press
75
76        @param key_list: A list of key strings, e.g. ['LEFTCTRL', 'F4']
77        """
78        graphics_utils.press_keys(key_list)
79