Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 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 | #include "FakeEventHub.h" |
| 18 | |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 19 | #include <optional> |
| 20 | |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 21 | #include <android-base/thread_annotations.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <linux/input-event-codes.h> |
| 24 | |
| 25 | #include "TestConstants.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery"; |
| 30 | |
| 31 | FakeEventHub::~FakeEventHub() { |
| 32 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 33 | delete mDevices.valueAt(i); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void FakeEventHub::addDevice(int32_t deviceId, const std::string& name, |
| 38 | ftl::Flags<InputDeviceClass> classes, int bus) { |
| 39 | Device* device = new Device(classes); |
| 40 | device->identifier.name = name; |
| 41 | device->identifier.bus = bus; |
| 42 | mDevices.add(deviceId, device); |
| 43 | |
| 44 | enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0); |
| 45 | } |
| 46 | |
| 47 | void FakeEventHub::removeDevice(int32_t deviceId) { |
| 48 | delete mDevices.valueFor(deviceId); |
| 49 | mDevices.removeItem(deviceId); |
| 50 | |
| 51 | enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0); |
| 52 | } |
| 53 | |
| 54 | bool FakeEventHub::isDeviceEnabled(int32_t deviceId) const { |
| 55 | Device* device = getDevice(deviceId); |
| 56 | if (device == nullptr) { |
| 57 | ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 58 | return false; |
| 59 | } |
| 60 | return device->enabled; |
| 61 | } |
| 62 | |
| 63 | status_t FakeEventHub::enableDevice(int32_t deviceId) { |
| 64 | status_t result; |
| 65 | Device* device = getDevice(deviceId); |
| 66 | if (device == nullptr) { |
| 67 | ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 68 | return BAD_VALUE; |
| 69 | } |
| 70 | if (device->enabled) { |
| 71 | ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId); |
| 72 | return OK; |
| 73 | } |
| 74 | result = device->enable(); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | status_t FakeEventHub::disableDevice(int32_t deviceId) { |
| 79 | Device* device = getDevice(deviceId); |
| 80 | if (device == nullptr) { |
| 81 | ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 82 | return BAD_VALUE; |
| 83 | } |
| 84 | if (!device->enabled) { |
| 85 | ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId); |
| 86 | return OK; |
| 87 | } |
| 88 | return device->disable(); |
| 89 | } |
| 90 | |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 91 | void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) { |
| 92 | getDevice(deviceId)->configuration.addProperty(key, value); |
| 93 | } |
| 94 | |
| 95 | void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) { |
| 96 | getDevice(deviceId)->configuration.addAll(configuration); |
| 97 | } |
| 98 | |
| 99 | void FakeEventHub::addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue, |
| 100 | int flat, int fuzz, int resolution) { |
| 101 | Device* device = getDevice(deviceId); |
| 102 | |
| 103 | RawAbsoluteAxisInfo info; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 104 | info.minValue = minValue; |
| 105 | info.maxValue = maxValue; |
| 106 | info.flat = flat; |
| 107 | info.fuzz = fuzz; |
| 108 | info.resolution = resolution; |
| 109 | device->absoluteAxes.add(axis, info); |
| 110 | } |
| 111 | |
| 112 | void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) { |
| 113 | getDevice(deviceId)->relativeAxes.add(axis, true); |
| 114 | } |
| 115 | |
| 116 | void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) { |
| 117 | getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state); |
| 118 | } |
| 119 | |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 120 | void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) { |
| 121 | getDevice(deviceId)->layoutInfo = info; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) { |
| 125 | getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state); |
| 126 | } |
| 127 | |
| 128 | void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) { |
| 129 | getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state); |
| 130 | } |
| 131 | |
| 132 | void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) { |
| 133 | getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value); |
| 134 | } |
| 135 | |
| 136 | void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode, |
| 137 | uint32_t flags) { |
| 138 | Device* device = getDevice(deviceId); |
| 139 | KeyInfo info; |
| 140 | info.keyCode = keyCode; |
| 141 | info.flags = flags; |
| 142 | if (scanCode) { |
| 143 | device->keysByScanCode.add(scanCode, info); |
| 144 | } |
| 145 | if (usageCode) { |
| 146 | device->keysByUsageCode.add(usageCode, info); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) { |
| 151 | getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode); |
| 152 | } |
| 153 | |
Linnan Li | e5657f2 | 2024-09-13 21:54:37 +0800 | [diff] [blame] | 154 | void FakeEventHub::setKeyRemapping(int32_t deviceId, |
| 155 | const std::map<int32_t, int32_t>& keyRemapping) const { |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 156 | Device* device = getDevice(deviceId); |
Linnan Li | e5657f2 | 2024-09-13 21:54:37 +0800 | [diff] [blame] | 157 | device->keyRemapping = keyRemapping; |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 160 | void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) { |
| 161 | getDevice(deviceId)->leds.add(led, initialState); |
| 162 | } |
| 163 | |
| 164 | void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode, |
| 165 | InputDeviceSensorType sensorType, int32_t sensorDataIndex) { |
| 166 | SensorInfo info; |
| 167 | info.sensorType = sensorType; |
| 168 | info.sensorDataIndex = sensorDataIndex; |
| 169 | getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info); |
| 170 | } |
| 171 | |
| 172 | void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) { |
| 173 | typename BitArray<MSC_MAX>::Buffer buffer; |
| 174 | buffer[mscEvent / 32] = 1 << mscEvent % 32; |
| 175 | getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer); |
| 176 | } |
| 177 | |
| 178 | void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) { |
| 179 | mRawLightInfos.emplace(rawId, std::move(info)); |
| 180 | } |
| 181 | |
| 182 | void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) { |
| 183 | mLightBrightness.emplace(rawId, brightness); |
| 184 | } |
| 185 | |
| 186 | void FakeEventHub::fakeLightIntensities(int32_t rawId, |
| 187 | const std::unordered_map<LightColor, int32_t> intensities) { |
| 188 | mLightIntensities.emplace(rawId, std::move(intensities)); |
| 189 | } |
| 190 | |
| 191 | bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) { |
| 192 | return getDevice(deviceId)->leds.valueFor(led); |
| 193 | } |
| 194 | |
| 195 | std::vector<std::string>& FakeEventHub::getExcludedDevices() { |
| 196 | return mExcludedDevices; |
| 197 | } |
| 198 | |
| 199 | void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId, |
| 200 | const VirtualKeyDefinition& definition) { |
| 201 | getDevice(deviceId)->virtualKeys.push_back(definition); |
| 202 | } |
| 203 | |
| 204 | void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, |
| 205 | int32_t code, int32_t value) { |
| 206 | std::scoped_lock<std::mutex> lock(mLock); |
| 207 | RawEvent event; |
| 208 | event.when = when; |
| 209 | event.readTime = readTime; |
| 210 | event.deviceId = deviceId; |
| 211 | event.type = type; |
| 212 | event.code = code; |
| 213 | event.value = value; |
| 214 | mEvents.push_back(event); |
| 215 | |
| 216 | if (type == EV_ABS) { |
| 217 | setAbsoluteAxisValue(deviceId, code, value); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void FakeEventHub::setVideoFrames( |
| 222 | std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) { |
| 223 | mVideoFrames = std::move(videoFrames); |
| 224 | } |
| 225 | |
| 226 | void FakeEventHub::assertQueueIsEmpty() { |
| 227 | std::unique_lock<std::mutex> lock(mLock); |
| 228 | base::ScopedLockAssertion assumeLocked(mLock); |
| 229 | const bool queueIsEmpty = |
| 230 | mEventsCondition.wait_for(lock, WAIT_TIMEOUT, |
| 231 | [this]() REQUIRES(mLock) { return mEvents.size() == 0; }); |
| 232 | if (!queueIsEmpty) { |
| 233 | FAIL() << "Timed out waiting for EventHub queue to be emptied."; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const { |
| 238 | ssize_t index = mDevices.indexOfKey(deviceId); |
| 239 | return index >= 0 ? mDevices.valueAt(index) : nullptr; |
| 240 | } |
| 241 | |
| 242 | ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const { |
| 243 | Device* device = getDevice(deviceId); |
| 244 | return device ? device->classes : ftl::Flags<InputDeviceClass>(0); |
| 245 | } |
| 246 | |
| 247 | InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const { |
| 248 | Device* device = getDevice(deviceId); |
| 249 | return device ? device->identifier : InputDeviceIdentifier(); |
| 250 | } |
| 251 | |
| 252 | int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const { |
| 253 | return 0; |
| 254 | } |
| 255 | |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 256 | std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const { |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 257 | Device* device = getDevice(deviceId); |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 258 | if (device == nullptr) { |
| 259 | return {}; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 260 | } |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 261 | return device->configuration; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 264 | std::optional<RawAbsoluteAxisInfo> FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, |
| 265 | int axis) const { |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 266 | Device* device = getDevice(deviceId); |
| 267 | if (device) { |
| 268 | ssize_t index = device->absoluteAxes.indexOfKey(axis); |
| 269 | if (index >= 0) { |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 270 | return device->absoluteAxes.valueAt(index); |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
Harry Cutts | 207674d | 2024-06-06 18:53:41 +0000 | [diff] [blame] | 273 | return std::nullopt; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const { |
| 277 | Device* device = getDevice(deviceId); |
| 278 | if (device) { |
| 279 | return device->relativeAxes.indexOfKey(axis) >= 0; |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | bool FakeEventHub::hasInputProperty(int32_t, int) const { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const { |
| 289 | Device* device = getDevice(deviceId); |
| 290 | if (device) { |
| 291 | return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false; |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
| 297 | int32_t metaState, int32_t* outKeycode, int32_t* outMetaState, |
| 298 | uint32_t* outFlags) const { |
| 299 | Device* device = getDevice(deviceId); |
| 300 | if (device) { |
| 301 | const KeyInfo* key = getKey(device, scanCode, usageCode); |
| 302 | if (key) { |
| 303 | if (outKeycode) { |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 304 | auto it = device->keyRemapping.find(key->keyCode); |
| 305 | *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 306 | } |
| 307 | if (outFlags) { |
| 308 | *outFlags = key->flags; |
| 309 | } |
| 310 | if (outMetaState) { |
| 311 | *outMetaState = metaState; |
| 312 | } |
| 313 | return OK; |
| 314 | } |
| 315 | } |
| 316 | return NAME_NOT_FOUND; |
| 317 | } |
| 318 | |
| 319 | const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode, |
| 320 | int32_t usageCode) const { |
| 321 | if (usageCode) { |
| 322 | ssize_t index = device->keysByUsageCode.indexOfKey(usageCode); |
| 323 | if (index >= 0) { |
| 324 | return &device->keysByUsageCode.valueAt(index); |
| 325 | } |
| 326 | } |
| 327 | if (scanCode) { |
| 328 | ssize_t index = device->keysByScanCode.indexOfKey(scanCode); |
| 329 | if (index >= 0) { |
| 330 | return &device->keysByScanCode.valueAt(index); |
| 331 | } |
| 332 | } |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
| 336 | status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const { |
| 337 | return NAME_NOT_FOUND; |
| 338 | } |
| 339 | |
| 340 | base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor( |
| 341 | int32_t deviceId, int32_t absCode) const { |
| 342 | Device* device = getDevice(deviceId); |
| 343 | if (!device) { |
| 344 | return Errorf("Sensor device not found."); |
| 345 | } |
| 346 | auto it = device->sensorsByAbsCode.find(absCode); |
| 347 | if (it == device->sensorsByAbsCode.end()) { |
| 348 | return Errorf("Sensor map not found."); |
| 349 | } |
| 350 | const SensorInfo& info = it->second; |
| 351 | return std::make_pair(info.sensorType, info.sensorDataIndex); |
| 352 | } |
| 353 | |
| 354 | void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) { |
| 355 | mExcludedDevices = devices; |
| 356 | } |
| 357 | |
| 358 | std::vector<RawEvent> FakeEventHub::getEvents(int) { |
| 359 | std::scoped_lock lock(mLock); |
| 360 | |
| 361 | std::vector<RawEvent> buffer; |
| 362 | std::swap(buffer, mEvents); |
| 363 | |
| 364 | mEventsCondition.notify_all(); |
| 365 | return buffer; |
| 366 | } |
| 367 | |
| 368 | std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) { |
| 369 | auto it = mVideoFrames.find(deviceId); |
| 370 | if (it != mVideoFrames.end()) { |
| 371 | std::vector<TouchVideoFrame> frames = std::move(it->second); |
| 372 | mVideoFrames.erase(deviceId); |
| 373 | return frames; |
| 374 | } |
| 375 | return {}; |
| 376 | } |
| 377 | |
| 378 | int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
| 379 | Device* device = getDevice(deviceId); |
| 380 | if (device) { |
| 381 | ssize_t index = device->scanCodeStates.indexOfKey(scanCode); |
| 382 | if (index >= 0) { |
| 383 | return device->scanCodeStates.valueAt(index); |
| 384 | } |
| 385 | } |
| 386 | return AKEY_STATE_UNKNOWN; |
| 387 | } |
| 388 | |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 389 | std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const { |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 390 | Device* device = getDevice(deviceId); |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 391 | return device ? device->layoutInfo : std::nullopt; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 395 | Device* device = getDevice(deviceId); |
| 396 | if (device) { |
| 397 | ssize_t index = device->keyCodeStates.indexOfKey(keyCode); |
| 398 | if (index >= 0) { |
| 399 | return device->keyCodeStates.valueAt(index); |
| 400 | } |
| 401 | } |
| 402 | return AKEY_STATE_UNKNOWN; |
| 403 | } |
| 404 | |
| 405 | int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
| 406 | Device* device = getDevice(deviceId); |
| 407 | if (device) { |
| 408 | ssize_t index = device->switchStates.indexOfKey(sw); |
| 409 | if (index >= 0) { |
| 410 | return device->switchStates.valueAt(index); |
| 411 | } |
| 412 | } |
| 413 | return AKEY_STATE_UNKNOWN; |
| 414 | } |
| 415 | |
Harry Cutts | e2c5e20 | 2024-06-21 17:08:48 +0000 | [diff] [blame] | 416 | std::optional<int32_t> FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis) const { |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 417 | Device* device = getDevice(deviceId); |
| 418 | if (device) { |
| 419 | ssize_t index = device->absoluteAxisValue.indexOfKey(axis); |
| 420 | if (index >= 0) { |
Harry Cutts | e2c5e20 | 2024-06-21 17:08:48 +0000 | [diff] [blame] | 421 | return device->absoluteAxisValue.valueAt(index); |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
Harry Cutts | e2c5e20 | 2024-06-21 17:08:48 +0000 | [diff] [blame] | 424 | return std::nullopt; |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Arpit Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 427 | void FakeEventHub::setMtSlotValues(int32_t deviceId, int32_t axis, |
| 428 | const std::vector<int32_t>& values) { |
| 429 | Device* device = getDevice(deviceId); |
| 430 | if (!device) { |
| 431 | FAIL() << "Missing device"; |
| 432 | } |
| 433 | device->mtSlotValues[axis] = values; |
| 434 | } |
| 435 | |
| 436 | base::Result<std::vector<int32_t>> FakeEventHub::getMtSlotValues(int32_t deviceId, int32_t axis, |
| 437 | size_t slotCount) const { |
| 438 | Device* device = getDevice(deviceId); |
| 439 | if (!device) { |
| 440 | ADD_FAILURE() << "Missing device"; |
| 441 | return base::ResultError("Missing device", UNKNOWN_ERROR); |
| 442 | } |
| 443 | const auto& mtSlotValuesIterator = device->mtSlotValues.find(axis); |
| 444 | if (mtSlotValuesIterator == device->mtSlotValues.end()) { |
| 445 | return base::ResultError("axis not supported", NAME_NOT_FOUND); |
| 446 | } |
| 447 | const auto& mtSlotValues = mtSlotValuesIterator->second; |
| 448 | if (mtSlotValues.size() != slotCount) { |
| 449 | ADD_FAILURE() << "MtSlot values specified for " << mtSlotValues.size() |
| 450 | << " slots but expected for " << slotCount << " Slots"; |
| 451 | return base::ResultError("Slot count mismatch", NAME_NOT_FOUND); |
| 452 | } |
| 453 | std::vector<int32_t> outValues(slotCount + 1); |
| 454 | outValues[0] = axis; |
| 455 | std::copy(mtSlotValues.begin(), mtSlotValues.end(), outValues.begin() + 1); |
| 456 | return std::move(outValues); |
| 457 | } |
| 458 | |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 459 | int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const { |
| 460 | Device* device = getDevice(deviceId); |
| 461 | if (!device) { |
| 462 | return AKEYCODE_UNKNOWN; |
| 463 | } |
| 464 | auto it = device->keyCodeMapping.find(locationKeyCode); |
| 465 | return it != device->keyCodeMapping.end() ? it->second : locationKeyCode; |
| 466 | } |
| 467 | |
| 468 | // Return true if the device has non-empty key layout. |
| 469 | bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
| 470 | uint8_t* outFlags) const { |
| 471 | Device* device = getDevice(deviceId); |
| 472 | if (!device) return false; |
| 473 | |
| 474 | bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0; |
| 475 | for (size_t i = 0; i < keyCodes.size(); i++) { |
| 476 | for (size_t j = 0; j < device->keysByScanCode.size(); j++) { |
| 477 | if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) { |
| 478 | outFlags[i] = 1; |
| 479 | } |
| 480 | } |
| 481 | for (size_t j = 0; j < device->keysByUsageCode.size(); j++) { |
| 482 | if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) { |
| 483 | outFlags[i] = 1; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | return result; |
| 488 | } |
| 489 | |
| 490 | bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { |
| 491 | Device* device = getDevice(deviceId); |
| 492 | if (device) { |
| 493 | ssize_t index = device->keysByScanCode.indexOfKey(scanCode); |
| 494 | return index >= 0; |
| 495 | } |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const { |
| 500 | Device* device = getDevice(deviceId); |
| 501 | if (!device) { |
| 502 | return false; |
| 503 | } |
| 504 | for (size_t i = 0; i < device->keysByScanCode.size(); i++) { |
| 505 | if (keyCode == device->keysByScanCode.valueAt(i).keyCode) { |
| 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | for (size_t j = 0; j < device->keysByUsageCode.size(); j++) { |
| 510 | if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) { |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 518 | Device* device = getDevice(deviceId); |
| 519 | return device && device->leds.indexOfKey(led) >= 0; |
| 520 | } |
| 521 | |
| 522 | void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 523 | Device* device = getDevice(deviceId); |
| 524 | if (device) { |
| 525 | ssize_t index = device->leds.indexOfKey(led); |
| 526 | if (index >= 0) { |
| 527 | device->leds.replaceValueAt(led, on); |
| 528 | } else { |
| 529 | ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared " |
| 530 | "was not present. led=" |
| 531 | << led; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void FakeEventHub::getVirtualKeyDefinitions( |
| 537 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 538 | outVirtualKeys.clear(); |
| 539 | |
| 540 | Device* device = getDevice(deviceId); |
| 541 | if (device) { |
| 542 | outVirtualKeys = device->virtualKeys; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const { |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) { |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const { |
| 555 | return mVibrators; |
| 556 | } |
| 557 | |
| 558 | std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const { |
| 559 | return BATTERY_CAPACITY; |
| 560 | } |
| 561 | |
| 562 | std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const { |
| 563 | return BATTERY_STATUS; |
| 564 | } |
| 565 | |
| 566 | std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const { |
| 567 | return {DEFAULT_BATTERY}; |
| 568 | } |
| 569 | |
| 570 | std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId, |
| 571 | int32_t batteryId) const { |
| 572 | if (batteryId != DEFAULT_BATTERY) return {}; |
| 573 | static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY, |
| 574 | .name = "default battery", |
| 575 | .flags = InputBatteryClass::CAPACITY, |
| 576 | .path = BATTERY_DEVPATH}; |
| 577 | return BATTERY_INFO; |
| 578 | } |
| 579 | |
| 580 | std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const { |
| 581 | std::vector<int32_t> ids; |
| 582 | for (const auto& [rawId, info] : mRawLightInfos) { |
| 583 | ids.push_back(rawId); |
| 584 | } |
| 585 | return ids; |
| 586 | } |
| 587 | |
| 588 | std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const { |
| 589 | auto it = mRawLightInfos.find(lightId); |
| 590 | if (it == mRawLightInfos.end()) { |
| 591 | return std::nullopt; |
| 592 | } |
| 593 | return it->second; |
| 594 | } |
| 595 | |
| 596 | void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) { |
| 597 | mLightBrightness.emplace(lightId, brightness); |
| 598 | } |
| 599 | |
| 600 | void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId, |
| 601 | std::unordered_map<LightColor, int32_t> intensities) { |
| 602 | mLightIntensities.emplace(lightId, intensities); |
| 603 | }; |
| 604 | |
| 605 | std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const { |
| 606 | auto lightIt = mLightBrightness.find(lightId); |
| 607 | if (lightIt == mLightBrightness.end()) { |
| 608 | return std::nullopt; |
| 609 | } |
| 610 | return lightIt->second; |
| 611 | } |
| 612 | |
| 613 | std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities( |
| 614 | int32_t deviceId, int32_t lightId) const { |
| 615 | auto lightIt = mLightIntensities.find(lightId); |
| 616 | if (lightIt == mLightIntensities.end()) { |
| 617 | return std::nullopt; |
| 618 | } |
| 619 | return lightIt->second; |
| 620 | }; |
| 621 | |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 622 | void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const { |
| 623 | Device* device = getDevice(deviceId); |
| 624 | if (device == nullptr) { |
| 625 | return; |
| 626 | } |
| 627 | device->sysfsRootPath = sysfsRootPath; |
| 628 | } |
| 629 | |
| 630 | void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) { |
| 631 | int32_t foundDeviceId = -1; |
| 632 | Device* foundDevice = nullptr; |
| 633 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 634 | Device* d = mDevices.valueAt(i); |
| 635 | if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) { |
| 636 | foundDeviceId = mDevices.keyAt(i); |
| 637 | foundDevice = d; |
| 638 | } |
| 639 | } |
| 640 | if (foundDevice == nullptr) { |
| 641 | return; |
| 642 | } |
| 643 | // If device sysfs changed -> reopen the device |
| 644 | if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) { |
Vaibhav Devmurari | fb219eb | 2023-05-02 13:20:37 +0000 | [diff] [blame] | 645 | InputDeviceIdentifier identifier = foundDevice->identifier; |
| 646 | ftl::Flags<InputDeviceClass> classes = foundDevice->classes; |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 647 | removeDevice(foundDeviceId); |
Vaibhav Devmurari | fb219eb | 2023-05-02 13:20:37 +0000 | [diff] [blame] | 648 | addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT, |
| 649 | identifier.bus); |
Vaibhav Devmurari | 5fc7d85 | 2023-03-17 18:43:33 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Harry Cutts | a5b7129 | 2022-11-28 12:56:17 +0000 | [diff] [blame] | 653 | } // namespace android |