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