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