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