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