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 10import threading 11 12from autotest_lib.client.bin.input import input_event_recorder 13from autotest_lib.client.cros.input_playback import input_playback 14from autotest_lib.client.common_lib import error 15from autotest_lib.client.cros.graphics import graphics_utils 16 17 18class InputFacadeNativeError(Exception): 19 """Error in InputFacadeNative.""" 20 pass 21 22 23class InputFacadeNative(object): 24 """Facade to access the record input events.""" 25 26 def __init__(self): 27 """Initializes the input facade.""" 28 self.recorders_lock = threading.Lock() 29 self.recorders = dict() 30 31 def initialize_input_playback(self, input_type='keyboard', property_file=None): 32 """Initialize for input events simulation. 33 34 @param input_type: the name of the input device. 35 @param property_file: Property file of device to be emulated. 36 """ 37 self._player = input_playback.InputPlayback() 38 self._player.emulate(input_type=input_type, property_file=property_file) 39 self._player.find_connected_inputs() 40 41 def initialize_input_recorder(self, device_name, uniq): 42 """Initialize an input event recorder object. 43 44 @param device_name: the name of the input device to record. 45 @param uniq: Unique address of input device (None if not used) 46 47 """ 48 with self.recorders_lock: 49 self.recorders[device_name] = \ 50 input_event_recorder.InputEventRecorder(device_name, uniq) 51 logging.info('input event device: %s [uniq=%s] (%s)', 52 self.recorders[device_name].device_name, 53 self.recorders[device_name].uniq, 54 self.recorders[device_name].device_node) 55 56 57 def clear_input_events(self, device_name): 58 """Clear the event list. 59 60 @param device_name: the name of the input device to record. 61 62 """ 63 with self.recorders_lock: 64 if self.recorders[device_name] is None: 65 raise error.TestError( 66 'input facade: input device name not given') 67 self.recorders[device_name].clear_events() 68 69 70 def start_input_recorder(self, device_name): 71 """Start the recording thread. 72 73 @param device_name: the name of the input device to record. 74 75 """ 76 with self.recorders_lock: 77 if self.recorders[device_name] is None: 78 raise error.TestError( 79 'input facade: input device name not given') 80 self.recorders[device_name].start() 81 82 83 def stop_input_recorder(self, device_name): 84 """Stop the recording thread. 85 86 @param device_name: the name of the input device to record. 87 88 """ 89 with self.recorders_lock: 90 if self.recorders[device_name] is None: 91 raise error.TestError( 92 'input facade: input device name not given') 93 self.recorders[device_name].stop() 94 95 96 def get_input_events(self, device_name): 97 """Get the bluetooth device input events. 98 99 @param device_name: the name of the input device to record. 100 101 @returns: the recorded input events. 102 103 """ 104 with self.recorders_lock: 105 if self.recorders[device_name] is None: 106 raise error.TestError( 107 'input facade: input device name not given') 108 events = self.recorders[device_name].get_events() 109 return json.dumps(events) 110 111 112 def press_keys(self, key_list): 113 """ Simulating key press 114 115 @param key_list: A list of key strings, e.g. ['LEFTCTRL', 'F4'] 116 """ 117 graphics_utils.press_keys(key_list) 118 119 120 def blocking_playback_of_default_file(self, input_type, filename): 121 """Simulate events 122 123 @param input_type: input device name 124 @param filename: input events 125 """ 126 self._player.blocking_playback_of_default_file(input_type=input_type, 127 filename=filename) 128