1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <optional> 22 #include <unordered_map> 23 #include <vector> 24 25 #include <EventHub.h> 26 #include <InputDevice.h> 27 #include <ftl/flags.h> 28 #include <input/PropertyMap.h> 29 #include <input/VirtualKeyMap.h> 30 #include <utils/Errors.h> 31 #include <utils/KeyedVector.h> 32 33 namespace android { 34 35 class FakeEventHub : public EventHubInterface { 36 struct KeyInfo { 37 int32_t keyCode; 38 uint32_t flags; 39 }; 40 41 struct SensorInfo { 42 InputDeviceSensorType sensorType; 43 int32_t sensorDataIndex; 44 }; 45 46 struct Device { 47 InputDeviceIdentifier identifier; 48 ftl::Flags<InputDeviceClass> classes; 49 PropertyMap configuration; 50 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes; 51 KeyedVector<int, bool> relativeAxes; 52 KeyedVector<int32_t, int32_t> keyCodeStates; 53 KeyedVector<int32_t, int32_t> scanCodeStates; 54 KeyedVector<int32_t, int32_t> switchStates; 55 KeyedVector<int32_t, int32_t> absoluteAxisValue; 56 KeyedVector<int32_t, KeyInfo> keysByScanCode; 57 KeyedVector<int32_t, KeyInfo> keysByUsageCode; 58 std::unordered_map<int32_t, int32_t> keyRemapping; 59 KeyedVector<int32_t, bool> leds; 60 // fake mapping which would normally come from keyCharacterMap 61 std::unordered_map<int32_t, int32_t> keyCodeMapping; 62 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode; 63 BitArray<MSC_MAX> mscBitmask; 64 std::vector<VirtualKeyDefinition> virtualKeys; 65 bool enabled; 66 std::optional<RawLayoutInfo> layoutInfo; 67 std::string sysfsRootPath; 68 std::unordered_map<int32_t, std::vector<int32_t>> mtSlotValues; 69 enableDevice70 status_t enable() { 71 enabled = true; 72 return OK; 73 } 74 disableDevice75 status_t disable() { 76 enabled = false; 77 return OK; 78 } 79 DeviceDevice80 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {} 81 }; 82 83 std::mutex mLock; 84 std::condition_variable mEventsCondition; 85 86 KeyedVector<int32_t, Device*> mDevices; 87 std::vector<std::string> mExcludedDevices; 88 std::vector<RawEvent> mEvents GUARDED_BY(mLock); 89 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames; 90 std::vector<int32_t> mVibrators = {0, 1}; 91 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos; 92 // Simulates a device light brightness, from light id to light brightness. 93 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness; 94 // Simulates a device light intensities, from light id to light intensities map. 95 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>> 96 mLightIntensities; 97 98 public: 99 static constexpr int32_t DEFAULT_BATTERY = 1; 100 static constexpr int32_t BATTERY_STATUS = 4; 101 static constexpr int32_t BATTERY_CAPACITY = 66; 102 static const std::string BATTERY_DEVPATH; 103 104 virtual ~FakeEventHub(); FakeEventHub()105 FakeEventHub() {} 106 107 void addDevice(int32_t deviceId, const std::string& name, ftl::Flags<InputDeviceClass> classes, 108 int bus = 0); 109 void removeDevice(int32_t deviceId); 110 111 bool isDeviceEnabled(int32_t deviceId) const override; 112 status_t enableDevice(int32_t deviceId) override; 113 status_t disableDevice(int32_t deviceId) override; 114 115 void finishDeviceScan(); 116 117 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value); 118 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration); 119 120 void addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue, int flat, 121 int fuzz, int resolution = 0); 122 void addRelativeAxis(int32_t deviceId, int32_t axis); 123 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value); 124 125 void setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info); 126 127 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state); 128 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state); 129 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state); 130 131 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode, 132 uint32_t flags); 133 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode); 134 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const; 135 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition); 136 137 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType, 138 int32_t sensorDataIndex); 139 140 void setMscEvent(int32_t deviceId, int32_t mscEvent); 141 142 void addLed(int32_t deviceId, int32_t led, bool initialState); 143 void addRawLightInfo(int32_t rawId, RawLightInfo&& info); 144 void fakeLightBrightness(int32_t rawId, int32_t brightness); 145 void fakeLightIntensities(int32_t rawId, 146 const std::unordered_map<LightColor, int32_t> intensities); 147 bool getLedState(int32_t deviceId, int32_t led); 148 149 std::vector<std::string>& getExcludedDevices(); 150 151 void setVideoFrames( 152 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames); 153 154 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code, 155 int32_t value); 156 void assertQueueIsEmpty(); 157 void setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const; 158 // Populate fake slot values to be returned by the getter, size of the values should be equal to 159 // the slot count 160 void setMtSlotValues(int32_t deviceId, int32_t axis, const std::vector<int32_t>& values); 161 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis, 162 size_t slotCount) const override; 163 164 private: 165 Device* getDevice(int32_t deviceId) const; 166 167 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override; 168 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override; 169 int32_t getDeviceControllerNumber(int32_t) const override; 170 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override; 171 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 172 RawAbsoluteAxisInfo* outAxisInfo) const override; 173 bool hasRelativeAxis(int32_t deviceId, int axis) const override; 174 bool hasInputProperty(int32_t, int) const override; 175 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final; 176 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, 177 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override; 178 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const; 179 180 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override; 181 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( 182 int32_t deviceId, int32_t absCode) const override; 183 void setExcludedDevices(const std::vector<std::string>& devices) override; 184 std::vector<RawEvent> getEvents(int) override; 185 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override; 186 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override; 187 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override; 188 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override; 189 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override; 190 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const override; 191 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override; 192 193 // Return true if the device has non-empty key layout. 194 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, 195 uint8_t* outFlags) const override; 196 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override; 197 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override; 198 bool hasLed(int32_t deviceId, int32_t led) const override; 199 void setLedState(int32_t deviceId, int32_t led, bool on) override; 200 void getVirtualKeyDefinitions(int32_t deviceId, 201 std::vector<VirtualKeyDefinition>& outVirtualKeys) const override; 202 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override; 203 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override; 204 vibrate(int32_t,const VibrationElement &)205 void vibrate(int32_t, const VibrationElement&) override {} cancelVibrate(int32_t)206 void cancelVibrate(int32_t) override {} 207 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override; 208 209 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override; 210 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override; 211 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override; 212 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, 213 int32_t batteryId) const override; 214 215 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override; 216 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override; 217 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override; 218 void setLightIntensities(int32_t deviceId, int32_t lightId, 219 std::unordered_map<LightColor, int32_t> intensities) override; 220 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override; 221 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( 222 int32_t deviceId, int32_t lightId) const override; 223 void sysfsNodeChanged(const std::string& sysfsNodePath) override; dump(std::string &)224 void dump(std::string&) const override {} monitor()225 void monitor() const override {} requestReopenDevices()226 void requestReopenDevices() override {} wake()227 void wake() override {} 228 }; 229 230 } // namespace android 231