Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "Macros.h" |
| 18 | |
| 19 | #include "InputDevice.h" |
| 20 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | |
Harry Cutts | 8984462 | 2022-12-02 15:02:26 +0000 | [diff] [blame] | 23 | #include <android/sysprop/InputProperties.sysprop.h> |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 24 | #include <ftl/flags.h> |
| 25 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 26 | #include "CursorInputMapper.h" |
| 27 | #include "ExternalStylusInputMapper.h" |
| 28 | #include "InputReaderContext.h" |
| 29 | #include "JoystickInputMapper.h" |
| 30 | #include "KeyboardInputMapper.h" |
| 31 | #include "MultiTouchInputMapper.h" |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 32 | #include "PeripheralController.h" |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 33 | #include "RotaryEncoderInputMapper.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 34 | #include "SensorInputMapper.h" |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 35 | #include "SingleTouchInputMapper.h" |
| 36 | #include "SwitchInputMapper.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 37 | #include "TouchpadInputMapper.h" |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 38 | #include "VibratorInputMapper.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 43 | const InputDeviceIdentifier& identifier) |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 44 | : mContext(context), |
| 45 | mId(id), |
| 46 | mGeneration(generation), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 47 | mControllerNumber(0), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 48 | mIdentifier(identifier), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 49 | mClasses(0), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 50 | mSources(0), |
| 51 | mIsExternal(false), |
| 52 | mHasMic(false), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 53 | mDropUntilNextSync(false) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 54 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 55 | InputDevice::~InputDevice() {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | |
| 57 | bool InputDevice::isEnabled() { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 58 | if (!hasEventHubDevices()) { |
| 59 | return false; |
| 60 | } |
Chris Ye | e14523a | 2020-12-19 13:46:00 -0800 | [diff] [blame] | 61 | // An input device composed of sub devices can be individually enabled or disabled. |
| 62 | // If any of the sub device is enabled then the input device is considered as enabled. |
| 63 | bool enabled = false; |
| 64 | for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); }); |
| 65 | return enabled; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 68 | std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) { |
| 69 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 70 | if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) { |
| 71 | ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", " |
| 72 | "but the corresponding viewport is not found", |
| 73 | getName().c_str(), *mAssociatedDisplayPort); |
| 74 | enabled = false; |
| 75 | } |
| 76 | |
| 77 | if (isEnabled() == enabled) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 78 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 81 | // When resetting some devices, the driver needs to be queried to ensure that a proper reset is |
| 82 | // performed. The querying must happen when the device is enabled, so we reset after enabling |
| 83 | // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 84 | if (enabled) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 85 | for_each_subdevice([](auto& context) { context.enableDevice(); }); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 86 | out += reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 87 | } else { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 88 | out += reset(when); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 89 | for_each_subdevice([](auto& context) { context.disableDevice(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 90 | } |
| 91 | // Must change generation to flag this device as changed |
| 92 | bumpGeneration(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 93 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 96 | void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 97 | InputDeviceInfo deviceInfo = getDeviceInfo(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 98 | |
| 99 | dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(), |
| 100 | deviceInfo.getDisplayName().c_str()); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 101 | dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 102 | dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration); |
| 103 | dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
| 104 | dump += StringPrintf(INDENT2 "AssociatedDisplayPort: "); |
| 105 | if (mAssociatedDisplayPort) { |
| 106 | dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort); |
| 107 | } else { |
| 108 | dump += "<none>\n"; |
| 109 | } |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 110 | dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: "); |
| 111 | if (mAssociatedDisplayUniqueId) { |
| 112 | dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str()); |
| 113 | } else { |
| 114 | dump += "<none>\n"; |
| 115 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 116 | dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic)); |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 117 | dump += StringPrintf(INDENT2 "Sources: %s\n", |
| 118 | inputEventSourceToString(deviceInfo.getSources()).c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 119 | dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 120 | dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 121 | |
| 122 | const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
| 123 | if (!ranges.empty()) { |
| 124 | dump += INDENT2 "Motion Ranges:\n"; |
| 125 | for (size_t i = 0; i < ranges.size(); i++) { |
| 126 | const InputDeviceInfo::MotionRange& range = ranges[i]; |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 127 | const char* label = InputEventLookup::getAxisLabel(range.axis); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 128 | char name[32]; |
| 129 | if (label) { |
| 130 | strncpy(name, label, sizeof(name)); |
| 131 | name[sizeof(name) - 1] = '\0'; |
| 132 | } else { |
| 133 | snprintf(name, sizeof(name), "%d", range.axis); |
| 134 | } |
| 135 | dump += StringPrintf(INDENT3 |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 136 | "%s: source=%s, " |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 137 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n", |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 138 | name, inputEventSourceToString(range.source).c_str(), range.min, |
| 139 | range.max, range.flat, range.fuzz, range.resolution); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 143 | for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); }); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 144 | if (mController) { |
| 145 | mController->dump(dump); |
| 146 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame^] | 149 | void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 150 | if (mDevices.find(eventHubId) != mDevices.end()) { |
| 151 | return; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 152 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 153 | std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId)); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 154 | std::vector<std::unique_ptr<InputMapper>> mappers; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 155 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame^] | 156 | mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))}); |
| 157 | } |
| 158 | |
| 159 | void InputDevice::addEventHubDevice(int32_t eventHubId, |
| 160 | const InputReaderConfiguration& readerConfig) { |
| 161 | if (mDevices.find(eventHubId) != mDevices.end()) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 162 | return; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 163 | } |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame^] | 164 | std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId)); |
| 165 | std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 166 | |
| 167 | // insert the context into the devices set |
| 168 | mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))}); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 169 | // Must change generation to flag this device as changed |
| 170 | bumpGeneration(); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void InputDevice::removeEventHubDevice(int32_t eventHubId) { |
Siarhei Vishniakou | 30feb8c | 2022-09-28 10:48:29 -0700 | [diff] [blame] | 174 | if (mController != nullptr && mController->getEventHubId() == eventHubId) { |
| 175 | // Delete mController, since the corresponding eventhub device is going away |
| 176 | mController = nullptr; |
| 177 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 178 | mDevices.erase(eventHubId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 181 | std::list<NotifyArgs> InputDevice::configure(nsecs_t when, |
| 182 | const InputReaderConfiguration& readerConfig, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 183 | uint32_t changes) { |
| 184 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 185 | mSources = 0; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 186 | mClasses = ftl::Flags<InputDeviceClass>(0); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 187 | mControllerNumber = 0; |
| 188 | |
| 189 | for_each_subdevice([this](InputDeviceContext& context) { |
| 190 | mClasses |= context.getDeviceClasses(); |
| 191 | int32_t controllerNumber = context.getDeviceControllerNumber(); |
| 192 | if (controllerNumber > 0) { |
| 193 | if (mControllerNumber && mControllerNumber != controllerNumber) { |
| 194 | ALOGW("InputDevice::configure(): composite device contains multiple unique " |
| 195 | "controller numbers"); |
| 196 | } |
| 197 | mControllerNumber = controllerNumber; |
| 198 | } |
| 199 | }); |
| 200 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 201 | mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL); |
| 202 | mHasMic = mClasses.test(InputDeviceClass::MIC); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 203 | |
| 204 | if (!isIgnored()) { |
Ambrus Weisz | 7b6e16b | 2022-12-16 17:54:57 +0000 | [diff] [blame] | 205 | // Full configuration should happen the first time configure is called |
| 206 | // and when the device type is changed. Changing a device type can |
| 207 | // affect various other parameters so should result in a |
| 208 | // reconfiguration. |
| 209 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_TYPE)) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 210 | mConfiguration.clear(); |
| 211 | for_each_subdevice([this](InputDeviceContext& context) { |
Harry Cutts | c34f758 | 2023-03-07 16:23:30 +0000 | [diff] [blame] | 212 | std::optional<PropertyMap> configuration = |
| 213 | getEventHub()->getConfiguration(context.getEventHubId()); |
| 214 | if (configuration) { |
| 215 | mConfiguration.addAll(&(*configuration)); |
| 216 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 217 | }); |
Ambrus Weisz | 7bc23bf | 2022-10-04 13:13:07 +0000 | [diff] [blame] | 218 | |
| 219 | mAssociatedDeviceType = |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 220 | getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 224 | if (!mClasses.test(InputDeviceClass::VIRTUAL)) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 225 | std::shared_ptr<KeyCharacterMap> keyboardLayout = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 226 | mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 227 | bool shouldBumpGeneration = false; |
| 228 | for_each_subdevice( |
| 229 | [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) { |
| 230 | if (context.setKeyboardLayoutOverlay(keyboardLayout)) { |
| 231 | shouldBumpGeneration = true; |
| 232 | } |
| 233 | }); |
| 234 | if (shouldBumpGeneration) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 235 | bumpGeneration(); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 241 | if (!(mClasses.test(InputDeviceClass::VIRTUAL))) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 242 | std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); |
| 243 | if (mAlias != alias) { |
| 244 | mAlias = alias; |
| 245 | bumpGeneration(); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Siarhei Vishniakou | 21e96e6 | 2022-10-27 10:23:37 -0700 | [diff] [blame] | 250 | if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) { |
| 251 | // Do not execute this code on the first configure, because 'setEnabled' would call |
| 252 | // InputMapper::reset, and you can't reset a mapper before it has been configured. |
| 253 | // The mappers are configured for the first time at the bottom of this function. |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 254 | auto it = readerConfig.disabledDevices.find(mId); |
| 255 | bool enabled = it == readerConfig.disabledDevices.end(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 256 | out += setEnabled(enabled, when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 260 | // In most situations, no port or name will be specified. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 261 | mAssociatedDisplayPort = std::nullopt; |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 262 | mAssociatedDisplayUniqueId = std::nullopt; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 263 | mAssociatedViewport = std::nullopt; |
| 264 | // Find the display port that corresponds to the current input port. |
| 265 | const std::string& inputPort = mIdentifier.location; |
| 266 | if (!inputPort.empty()) { |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 267 | const std::unordered_map<std::string, uint8_t>& ports = |
| 268 | readerConfig.portAssociations; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 269 | const auto& displayPort = ports.find(inputPort); |
| 270 | if (displayPort != ports.end()) { |
| 271 | mAssociatedDisplayPort = std::make_optional(displayPort->second); |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 272 | } else { |
| 273 | const std::unordered_map<std::string, std::string>& displayUniqueIds = |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 274 | readerConfig.uniqueIdAssociations; |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 275 | const auto& displayUniqueId = displayUniqueIds.find(inputPort); |
| 276 | if (displayUniqueId != displayUniqueIds.end()) { |
| 277 | mAssociatedDisplayUniqueId = displayUniqueId->second; |
| 278 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | // If the device was explicitly disabled by the user, it would be present in the |
| 283 | // "disabledDevices" list. If it is associated with a specific display, and it was not |
| 284 | // explicitly disabled, then enable/disable the device based on whether we can find the |
| 285 | // corresponding viewport. |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 286 | bool enabled = |
| 287 | (readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 288 | if (mAssociatedDisplayPort) { |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 289 | mAssociatedViewport = |
| 290 | readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 291 | if (!mAssociatedViewport) { |
| 292 | ALOGW("Input device %s should be associated with display on port %" PRIu8 ", " |
| 293 | "but the corresponding viewport is not found.", |
| 294 | getName().c_str(), *mAssociatedDisplayPort); |
| 295 | enabled = false; |
| 296 | } |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 297 | } else if (mAssociatedDisplayUniqueId != std::nullopt) { |
| 298 | mAssociatedViewport = |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 299 | readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId); |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 300 | if (!mAssociatedViewport) { |
| 301 | ALOGW("Input device %s should be associated with display %s but the " |
| 302 | "corresponding viewport cannot be found", |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 303 | getName().c_str(), mAssociatedDisplayUniqueId->c_str()); |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 304 | enabled = false; |
| 305 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (changes) { |
| 309 | // For first-time configuration, only allow device to be disabled after mappers have |
| 310 | // finished configuring. This is because we need to read some of the properties from |
| 311 | // the device's open fd. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 312 | out += setEnabled(enabled, when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 316 | for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) { |
| 317 | out += mapper.reconfigure(when, readerConfig, changes); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 318 | mSources |= mapper.getSources(); |
| 319 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 320 | |
| 321 | // If a device is just plugged but it might be disabled, we need to update some info like |
| 322 | // axis range of touch from each InputMapper first, then disable it. |
| 323 | if (!changes) { |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 324 | out += setEnabled(readerConfig.disabledDevices.find(mId) == |
| 325 | readerConfig.disabledDevices.end(), |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 326 | when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 329 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 332 | std::list<NotifyArgs> InputDevice::reset(nsecs_t when) { |
| 333 | std::list<NotifyArgs> out; |
| 334 | for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 335 | |
| 336 | mContext->updateGlobalMetaState(); |
| 337 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 338 | out.push_back(notifyReset(when)); |
| 339 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 342 | std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 343 | // Process all of the events in order for each mapper. |
| 344 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 345 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 346 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 347 | // in the order received. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 348 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 349 | for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) { |
Prabir Pradhan | 011ca3d | 2023-02-22 21:31:39 +0000 | [diff] [blame] | 350 | if (debugRawEvents()) { |
Prabir Pradhan | 1e63fc2 | 2023-02-23 19:03:03 +0000 | [diff] [blame] | 351 | const auto [type, code, value] = |
| 352 | InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code, |
| 353 | rawEvent->value); |
| 354 | ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64, |
| 355 | rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when); |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 356 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 357 | |
| 358 | if (mDropUntilNextSync) { |
| 359 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
| 360 | mDropUntilNextSync = false; |
Prabir Pradhan | 1e63fc2 | 2023-02-23 19:03:03 +0000 | [diff] [blame] | 361 | ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun."); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 362 | } else { |
Prabir Pradhan | 1e63fc2 | 2023-02-23 19:03:03 +0000 | [diff] [blame] | 363 | ALOGD_IF(debugRawEvents(), |
| 364 | "Dropped input event while waiting for next input sync."); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 365 | } |
| 366 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
| 367 | ALOGI("Detected input event buffer overrun for device %s.", getName().c_str()); |
| 368 | mDropUntilNextSync = true; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 369 | out += reset(rawEvent->when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 370 | } else { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 371 | for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) { |
| 372 | out += mapper.process(rawEvent); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 373 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 374 | } |
| 375 | --count; |
| 376 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 377 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 380 | std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) { |
| 381 | std::list<NotifyArgs> out; |
| 382 | for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); }); |
| 383 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 386 | std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) { |
| 387 | std::list<NotifyArgs> out; |
| 388 | for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); }); |
| 389 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 392 | InputDeviceInfo InputDevice::getDeviceInfo() { |
| 393 | InputDeviceInfo outDeviceInfo; |
| 394 | outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal, |
Prabir Pradhan | e04ffaa | 2022-12-13 23:04:04 +0000 | [diff] [blame] | 395 | mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE)); |
| 396 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 397 | for_each_mapper( |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 398 | [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); }); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 399 | |
| 400 | if (mController) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 401 | mController->populateDeviceInfo(&outDeviceInfo); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 402 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 403 | return outDeviceInfo; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 407 | return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState); |
| 408 | } |
| 409 | |
| 410 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 411 | return getState(sourceMask, scanCode, &InputMapper::getScanCodeState); |
| 412 | } |
| 413 | |
| 414 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 415 | return getState(sourceMask, switchCode, &InputMapper::getSwitchState); |
| 416 | } |
| 417 | |
| 418 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 419 | int32_t result = AKEY_STATE_UNKNOWN; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 420 | for (auto& deviceEntry : mDevices) { |
| 421 | auto& devicePair = deviceEntry.second; |
| 422 | auto& mappers = devicePair.second; |
| 423 | for (auto& mapperPtr : mappers) { |
| 424 | InputMapper& mapper = *mapperPtr; |
| 425 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
| 426 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 427 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 428 | int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code); |
| 429 | if (currentResult >= AKEY_STATE_DOWN) { |
| 430 | return currentResult; |
| 431 | } else if (currentResult == AKEY_STATE_UP) { |
| 432 | result = currentResult; |
| 433 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | return result; |
| 438 | } |
| 439 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame^] | 440 | std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers( |
| 441 | InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) { |
| 442 | ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses(); |
| 443 | std::vector<std::unique_ptr<InputMapper>> mappers; |
| 444 | |
| 445 | // Switch-like devices. |
| 446 | if (classes.test(InputDeviceClass::SWITCH)) { |
| 447 | mappers.push_back(std::make_unique<SwitchInputMapper>(contextPtr, readerConfig)); |
| 448 | } |
| 449 | |
| 450 | // Scroll wheel-like devices. |
| 451 | if (classes.test(InputDeviceClass::ROTARY_ENCODER)) { |
| 452 | mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(contextPtr, readerConfig)); |
| 453 | } |
| 454 | |
| 455 | // Vibrator-like devices. |
| 456 | if (classes.test(InputDeviceClass::VIBRATOR)) { |
| 457 | mappers.push_back(std::make_unique<VibratorInputMapper>(contextPtr, readerConfig)); |
| 458 | } |
| 459 | |
| 460 | // Battery-like devices or light-containing devices. |
| 461 | // PeripheralController will be created with associated EventHub device. |
| 462 | if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) { |
| 463 | mController = std::make_unique<PeripheralController>(contextPtr); |
| 464 | } |
| 465 | |
| 466 | // Keyboard-like devices. |
| 467 | uint32_t keyboardSource = 0; |
| 468 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 469 | if (classes.test(InputDeviceClass::KEYBOARD)) { |
| 470 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
| 471 | } |
| 472 | if (classes.test(InputDeviceClass::ALPHAKEY)) { |
| 473 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 474 | } |
| 475 | if (classes.test(InputDeviceClass::DPAD)) { |
| 476 | keyboardSource |= AINPUT_SOURCE_DPAD; |
| 477 | } |
| 478 | if (classes.test(InputDeviceClass::GAMEPAD)) { |
| 479 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
| 480 | } |
| 481 | |
| 482 | if (keyboardSource != 0) { |
| 483 | mappers.push_back(std::make_unique<KeyboardInputMapper>(contextPtr, readerConfig, |
| 484 | keyboardSource, keyboardType)); |
| 485 | } |
| 486 | |
| 487 | // Cursor-like devices. |
| 488 | if (classes.test(InputDeviceClass::CURSOR)) { |
| 489 | mappers.push_back(std::make_unique<CursorInputMapper>(contextPtr, readerConfig)); |
| 490 | } |
| 491 | |
| 492 | // Touchscreens and touchpad devices. |
| 493 | static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY = |
| 494 | sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true); |
| 495 | // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or |
| 496 | // at least load this setting from the IDC file. |
| 497 | const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier(); |
| 498 | const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c && |
| 499 | (identifier.product == 0x05c4 || identifier.product == 0x09cc); |
| 500 | if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) && |
| 501 | classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) { |
| 502 | mappers.push_back(std::make_unique<TouchpadInputMapper>(contextPtr, readerConfig)); |
| 503 | } else if (classes.test(InputDeviceClass::TOUCH_MT)) { |
| 504 | mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig)); |
| 505 | } else if (classes.test(InputDeviceClass::TOUCH)) { |
| 506 | mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig)); |
| 507 | } |
| 508 | |
| 509 | // Joystick-like devices. |
| 510 | if (classes.test(InputDeviceClass::JOYSTICK)) { |
| 511 | mappers.push_back(std::make_unique<JoystickInputMapper>(contextPtr, readerConfig)); |
| 512 | } |
| 513 | |
| 514 | // Motion sensor enabled devices. |
| 515 | if (classes.test(InputDeviceClass::SENSOR)) { |
| 516 | mappers.push_back(std::make_unique<SensorInputMapper>(contextPtr, readerConfig)); |
| 517 | } |
| 518 | |
| 519 | // External stylus-like devices. |
| 520 | if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) { |
| 521 | mappers.push_back(std::make_unique<ExternalStylusInputMapper>(contextPtr, readerConfig)); |
| 522 | } |
| 523 | return mappers; |
| 524 | } |
| 525 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 526 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
| 527 | uint8_t* outFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 528 | bool result = false; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 529 | for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 530 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 531 | result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 532 | } |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 533 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 534 | return result; |
| 535 | } |
| 536 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 537 | int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const { |
| 538 | std::optional<int32_t> result = first_in_mappers<int32_t>( |
| 539 | [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const { |
| 540 | if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) { |
| 541 | return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode)); |
| 542 | } |
| 543 | return std::nullopt; |
| 544 | }); |
| 545 | if (!result) { |
| 546 | ALOGE("Failed to get key code for key location: No matching InputMapper with source mask " |
| 547 | "KEYBOARD found. The provided input device with id %d has sources %s.", |
| 548 | getId(), inputEventSourceToString(getSources()).c_str()); |
| 549 | return AKEYCODE_UNKNOWN; |
| 550 | } |
| 551 | return *result; |
| 552 | } |
| 553 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 554 | std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, |
| 555 | int32_t token) { |
| 556 | std::list<NotifyArgs> out; |
| 557 | for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); }); |
| 558 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 561 | std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) { |
| 562 | std::list<NotifyArgs> out; |
| 563 | for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); }); |
| 564 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 567 | bool InputDevice::isVibrating() { |
| 568 | bool vibrating = false; |
| 569 | for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); }); |
| 570 | return vibrating; |
| 571 | } |
| 572 | |
| 573 | /* There's no guarantee the IDs provided by the different mappers are unique, so if we have two |
| 574 | * different vibration mappers then we could have duplicate IDs. |
| 575 | * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities, |
| 576 | * we would definitely have duplicate IDs. |
| 577 | */ |
| 578 | std::vector<int32_t> InputDevice::getVibratorIds() { |
| 579 | std::vector<int32_t> vibrators; |
| 580 | for_each_mapper([&vibrators](InputMapper& mapper) { |
| 581 | std::vector<int32_t> devVibs = mapper.getVibratorIds(); |
| 582 | vibrators.reserve(vibrators.size() + devVibs.size()); |
| 583 | vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end()); |
| 584 | }); |
| 585 | return vibrators; |
| 586 | } |
| 587 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 588 | bool InputDevice::enableSensor(InputDeviceSensorType sensorType, |
| 589 | std::chrono::microseconds samplingPeriod, |
| 590 | std::chrono::microseconds maxBatchReportLatency) { |
| 591 | bool success = true; |
| 592 | for_each_mapper( |
| 593 | [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) { |
| 594 | success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency); |
| 595 | }); |
| 596 | return success; |
| 597 | } |
| 598 | |
| 599 | void InputDevice::disableSensor(InputDeviceSensorType sensorType) { |
| 600 | for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); }); |
| 601 | } |
| 602 | |
| 603 | void InputDevice::flushSensor(InputDeviceSensorType sensorType) { |
| 604 | for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); }); |
| 605 | } |
| 606 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 607 | std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) { |
| 608 | std::list<NotifyArgs> out; |
| 609 | for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); }); |
| 610 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 613 | bool InputDevice::setLightColor(int32_t lightId, int32_t color) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 614 | return mController ? mController->setLightColor(lightId, color) : false; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 618 | return mController ? mController->setLightPlayerId(lightId, playerId) : false; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 622 | return mController ? mController->getLightColor(lightId) : std::nullopt; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 626 | return mController ? mController->getLightPlayerId(lightId) : std::nullopt; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 629 | int32_t InputDevice::getMetaState() { |
| 630 | int32_t result = 0; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 631 | for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 632 | return result; |
| 633 | } |
| 634 | |
| 635 | void InputDevice::updateMetaState(int32_t keyCode) { |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 636 | first_in_mappers<bool>([keyCode](InputMapper& mapper) { |
| 637 | if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) && |
| 638 | mapper.updateMetaState(keyCode)) { |
| 639 | return std::make_optional(true); |
| 640 | } |
| 641 | return std::optional<bool>(); |
| 642 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 645 | void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) { |
| 646 | for_each_subdevice([fromKeyCode, toKeyCode](auto& context) { |
| 647 | context.addKeyRemapping(fromKeyCode, toKeyCode); |
| 648 | }); |
| 649 | } |
| 650 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 651 | void InputDevice::bumpGeneration() { |
| 652 | mGeneration = mContext->bumpGeneration(); |
| 653 | } |
| 654 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 655 | NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) { |
| 656 | return NotifyDeviceResetArgs(mContext->getNextId(), when, mId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | std::optional<int32_t> InputDevice::getAssociatedDisplayId() { |
| 660 | // Check if we had associated to the specific display. |
| 661 | if (mAssociatedViewport) { |
| 662 | return mAssociatedViewport->displayId; |
| 663 | } |
| 664 | |
| 665 | // No associated display port, check if some InputMapper is associated. |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 666 | return first_in_mappers<int32_t>( |
| 667 | [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 670 | // returns the number of mappers associated with the device |
| 671 | size_t InputDevice::getMapperCount() { |
| 672 | size_t count = 0; |
| 673 | for (auto& deviceEntry : mDevices) { |
| 674 | auto& devicePair = deviceEntry.second; |
| 675 | auto& mappers = devicePair.second; |
| 676 | count += mappers.size(); |
| 677 | } |
| 678 | return count; |
| 679 | } |
| 680 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 681 | void InputDevice::updateLedState(bool reset) { |
| 682 | for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); }); |
| 683 | } |
| 684 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 685 | std::optional<int32_t> InputDevice::getBatteryEventHubId() const { |
| 686 | return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt; |
| 687 | } |
| 688 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 689 | InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId) |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 690 | : mDevice(device), |
| 691 | mContext(device.getContext()), |
| 692 | mEventHub(device.getContext()->getEventHub()), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 693 | mId(eventHubId), |
| 694 | mDeviceId(device.getId()) {} |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 695 | |
| 696 | InputDeviceContext::~InputDeviceContext() {} |
| 697 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 698 | } // namespace android |