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