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 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 21 | #include <input/Flags.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 24 | #include "CursorInputMapper.h" |
| 25 | #include "ExternalStylusInputMapper.h" |
| 26 | #include "InputReaderContext.h" |
| 27 | #include "JoystickInputMapper.h" |
| 28 | #include "KeyboardInputMapper.h" |
| 29 | #include "MultiTouchInputMapper.h" |
| 30 | #include "RotaryEncoderInputMapper.h" |
| 31 | #include "SingleTouchInputMapper.h" |
| 32 | #include "SwitchInputMapper.h" |
| 33 | #include "VibratorInputMapper.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 38 | const InputDeviceIdentifier& identifier) |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 39 | : mContext(context), |
| 40 | mId(id), |
| 41 | mGeneration(generation), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 42 | mControllerNumber(0), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 43 | mIdentifier(identifier), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 44 | mClasses(0), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 45 | mSources(0), |
| 46 | mIsExternal(false), |
| 47 | mHasMic(false), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 48 | mDropUntilNextSync(false) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 49 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 50 | InputDevice::~InputDevice() {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | |
| 52 | bool InputDevice::isEnabled() { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 53 | if (!hasEventHubDevices()) { |
| 54 | return false; |
| 55 | } |
| 56 | // devices are either all enabled or all disabled, so we only need to check the first |
| 57 | auto& devicePair = mDevices.begin()->second; |
| 58 | auto& contextPtr = devicePair.first; |
| 59 | return contextPtr->isDeviceEnabled(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void InputDevice::setEnabled(bool enabled, nsecs_t when) { |
| 63 | if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) { |
| 64 | ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", " |
| 65 | "but the corresponding viewport is not found", |
| 66 | getName().c_str(), *mAssociatedDisplayPort); |
| 67 | enabled = false; |
| 68 | } |
| 69 | |
| 70 | if (isEnabled() == enabled) { |
| 71 | return; |
| 72 | } |
| 73 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 74 | // When resetting some devices, the driver needs to be queried to ensure that a proper reset is |
| 75 | // performed. The querying must happen when the device is enabled, so we reset after enabling |
| 76 | // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 77 | if (enabled) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 78 | for_each_subdevice([](auto& context) { context.enableDevice(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 79 | reset(when); |
| 80 | } else { |
| 81 | reset(when); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 82 | for_each_subdevice([](auto& context) { context.disableDevice(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 83 | } |
| 84 | // Must change generation to flag this device as changed |
| 85 | bumpGeneration(); |
| 86 | } |
| 87 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame^] | 88 | void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 89 | InputDeviceInfo deviceInfo; |
| 90 | getDeviceInfo(&deviceInfo); |
| 91 | |
| 92 | dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(), |
| 93 | deviceInfo.getDisplayName().c_str()); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame^] | 94 | dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 95 | dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration); |
| 96 | dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
| 97 | dump += StringPrintf(INDENT2 "AssociatedDisplayPort: "); |
| 98 | if (mAssociatedDisplayPort) { |
| 99 | dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort); |
| 100 | } else { |
| 101 | dump += "<none>\n"; |
| 102 | } |
| 103 | dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic)); |
| 104 | dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 105 | dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame^] | 106 | dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 107 | |
| 108 | const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
| 109 | if (!ranges.empty()) { |
| 110 | dump += INDENT2 "Motion Ranges:\n"; |
| 111 | for (size_t i = 0; i < ranges.size(); i++) { |
| 112 | const InputDeviceInfo::MotionRange& range = ranges[i]; |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 113 | const char* label = InputEventLookup::getAxisLabel(range.axis); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 114 | char name[32]; |
| 115 | if (label) { |
| 116 | strncpy(name, label, sizeof(name)); |
| 117 | name[sizeof(name) - 1] = '\0'; |
| 118 | } else { |
| 119 | snprintf(name, sizeof(name), "%d", range.axis); |
| 120 | } |
| 121 | dump += StringPrintf(INDENT3 |
| 122 | "%s: source=0x%08x, " |
| 123 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n", |
| 124 | name, range.source, range.min, range.max, range.flat, range.fuzz, |
| 125 | range.resolution); |
| 126 | } |
| 127 | } |
| 128 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 129 | for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 132 | void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) { |
| 133 | if (mDevices.find(eventHubId) != mDevices.end()) { |
| 134 | return; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 135 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 136 | std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId)); |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 137 | Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses(); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 138 | std::vector<std::unique_ptr<InputMapper>> mappers; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 139 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 140 | // Check if we should skip population |
| 141 | if (!populateMappers) { |
| 142 | mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))}); |
| 143 | return; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Switch-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 147 | if (classes.test(InputDeviceClass::SWITCH)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 148 | mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Scroll wheel-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 152 | if (classes.test(InputDeviceClass::ROTARY_ENCODER)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 153 | mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Vibrator-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 157 | if (classes.test(InputDeviceClass::VIBRATOR)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 158 | mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Keyboard-like devices. |
| 162 | uint32_t keyboardSource = 0; |
| 163 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 164 | if (classes.test(InputDeviceClass::KEYBOARD)) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 165 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
| 166 | } |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 167 | if (classes.test(InputDeviceClass::ALPHAKEY)) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 168 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 169 | } |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 170 | if (classes.test(InputDeviceClass::DPAD)) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 171 | keyboardSource |= AINPUT_SOURCE_DPAD; |
| 172 | } |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 173 | if (classes.test(InputDeviceClass::GAMEPAD)) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 174 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
| 175 | } |
| 176 | |
| 177 | if (keyboardSource != 0) { |
| 178 | mappers.push_back( |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 179 | std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Cursor-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 183 | if (classes.test(InputDeviceClass::CURSOR)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 184 | mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Touchscreens and touchpad devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 188 | if (classes.test(InputDeviceClass::TOUCH_MT)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 189 | mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr)); |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 190 | } else if (classes.test(InputDeviceClass::TOUCH)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 191 | mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Joystick-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 195 | if (classes.test(InputDeviceClass::JOYSTICK)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 196 | mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // External stylus-like devices. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 200 | if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 201 | mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr)); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 202 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 203 | |
| 204 | // insert the context into the devices set |
| 205 | mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))}); |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame^] | 206 | // Must change generation to flag this device as changed |
| 207 | bumpGeneration(); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void InputDevice::removeEventHubDevice(int32_t eventHubId) { |
| 211 | mDevices.erase(eventHubId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, |
| 215 | uint32_t changes) { |
| 216 | mSources = 0; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 217 | mClasses = Flags<InputDeviceClass>(0); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 218 | mControllerNumber = 0; |
| 219 | |
| 220 | for_each_subdevice([this](InputDeviceContext& context) { |
| 221 | mClasses |= context.getDeviceClasses(); |
| 222 | int32_t controllerNumber = context.getDeviceControllerNumber(); |
| 223 | if (controllerNumber > 0) { |
| 224 | if (mControllerNumber && mControllerNumber != controllerNumber) { |
| 225 | ALOGW("InputDevice::configure(): composite device contains multiple unique " |
| 226 | "controller numbers"); |
| 227 | } |
| 228 | mControllerNumber = controllerNumber; |
| 229 | } |
| 230 | }); |
| 231 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 232 | mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL); |
| 233 | mHasMic = mClasses.test(InputDeviceClass::MIC); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 234 | |
| 235 | if (!isIgnored()) { |
| 236 | if (!changes) { // first time only |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 237 | mConfiguration.clear(); |
| 238 | for_each_subdevice([this](InputDeviceContext& context) { |
| 239 | PropertyMap configuration; |
| 240 | context.getConfiguration(&configuration); |
| 241 | mConfiguration.addAll(&configuration); |
| 242 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (!changes || (changes & InputReaderConfiguration::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 | |
| 262 | if (!changes || (changes & InputReaderConfiguration::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 | |
| 272 | if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) { |
| 273 | auto it = config->disabledDevices.find(mId); |
| 274 | bool enabled = it == config->disabledDevices.end(); |
| 275 | setEnabled(enabled, when); |
| 276 | } |
| 277 | |
| 278 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 279 | // In most situations, no port will be specified. |
| 280 | mAssociatedDisplayPort = std::nullopt; |
| 281 | mAssociatedViewport = std::nullopt; |
| 282 | // Find the display port that corresponds to the current input port. |
| 283 | const std::string& inputPort = mIdentifier.location; |
| 284 | if (!inputPort.empty()) { |
| 285 | const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations; |
| 286 | const auto& displayPort = ports.find(inputPort); |
| 287 | if (displayPort != ports.end()) { |
| 288 | mAssociatedDisplayPort = std::make_optional(displayPort->second); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // If the device was explicitly disabled by the user, it would be present in the |
| 293 | // "disabledDevices" list. If it is associated with a specific display, and it was not |
| 294 | // explicitly disabled, then enable/disable the device based on whether we can find the |
| 295 | // corresponding viewport. |
| 296 | bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end()); |
| 297 | if (mAssociatedDisplayPort) { |
| 298 | mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort); |
| 299 | if (!mAssociatedViewport) { |
| 300 | ALOGW("Input device %s should be associated with display on port %" PRIu8 ", " |
| 301 | "but the corresponding viewport is not found.", |
| 302 | getName().c_str(), *mAssociatedDisplayPort); |
| 303 | enabled = false; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (changes) { |
| 308 | // For first-time configuration, only allow device to be disabled after mappers have |
| 309 | // finished configuring. This is because we need to read some of the properties from |
| 310 | // the device's open fd. |
| 311 | setEnabled(enabled, when); |
| 312 | } |
| 313 | } |
| 314 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 315 | for_each_mapper([this, when, config, changes](InputMapper& mapper) { |
| 316 | mapper.configure(when, config, changes); |
| 317 | mSources |= mapper.getSources(); |
| 318 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 319 | |
| 320 | // If a device is just plugged but it might be disabled, we need to update some info like |
| 321 | // axis range of touch from each InputMapper first, then disable it. |
| 322 | if (!changes) { |
| 323 | setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void InputDevice::reset(nsecs_t when) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 329 | for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 330 | |
| 331 | mContext->updateGlobalMetaState(); |
| 332 | |
| 333 | notifyReset(when); |
| 334 | } |
| 335 | |
| 336 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 337 | // Process all of the events in order for each mapper. |
| 338 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 339 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 340 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 341 | // in the order received. |
| 342 | for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) { |
| 343 | #if DEBUG_RAW_EVENTS |
| 344 | ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64, |
| 345 | rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when); |
| 346 | #endif |
| 347 | |
| 348 | if (mDropUntilNextSync) { |
| 349 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
| 350 | mDropUntilNextSync = false; |
| 351 | #if DEBUG_RAW_EVENTS |
| 352 | ALOGD("Recovered from input event buffer overrun."); |
| 353 | #endif |
| 354 | } else { |
| 355 | #if DEBUG_RAW_EVENTS |
| 356 | ALOGD("Dropped input event while waiting for next input sync."); |
| 357 | #endif |
| 358 | } |
| 359 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
| 360 | ALOGI("Detected input event buffer overrun for device %s.", getName().c_str()); |
| 361 | mDropUntilNextSync = true; |
| 362 | reset(rawEvent->when); |
| 363 | } else { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 364 | for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) { |
| 365 | mapper.process(rawEvent); |
| 366 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 367 | } |
| 368 | --count; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void InputDevice::timeoutExpired(nsecs_t when) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 373 | for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void InputDevice::updateExternalStylusState(const StylusState& state) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 377 | for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 381 | outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal, |
| 382 | mHasMic); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 383 | for_each_mapper( |
| 384 | [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 388 | return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState); |
| 389 | } |
| 390 | |
| 391 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 392 | return getState(sourceMask, scanCode, &InputMapper::getScanCodeState); |
| 393 | } |
| 394 | |
| 395 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 396 | return getState(sourceMask, switchCode, &InputMapper::getSwitchState); |
| 397 | } |
| 398 | |
| 399 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 400 | int32_t result = AKEY_STATE_UNKNOWN; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 401 | for (auto& deviceEntry : mDevices) { |
| 402 | auto& devicePair = deviceEntry.second; |
| 403 | auto& mappers = devicePair.second; |
| 404 | for (auto& mapperPtr : mappers) { |
| 405 | InputMapper& mapper = *mapperPtr; |
| 406 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
| 407 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 408 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 409 | int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code); |
| 410 | if (currentResult >= AKEY_STATE_DOWN) { |
| 411 | return currentResult; |
| 412 | } else if (currentResult == AKEY_STATE_UP) { |
| 413 | result = currentResult; |
| 414 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | return result; |
| 419 | } |
| 420 | |
| 421 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 422 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 423 | bool result = false; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 424 | for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) { |
| 425 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
| 426 | result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 427 | } |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 428 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 429 | return result; |
| 430 | } |
| 431 | |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 432 | void InputDevice::vibrate(const std::vector<VibrationElement>& pattern, ssize_t repeat, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 433 | int32_t token) { |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 434 | for_each_mapper([pattern, repeat, token](InputMapper& mapper) { |
| 435 | mapper.vibrate(pattern, repeat, token); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 436 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void InputDevice::cancelVibrate(int32_t token) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 440 | for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void InputDevice::cancelTouch(nsecs_t when) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 444 | for_each_mapper([when](InputMapper& mapper) { mapper.cancelTouch(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | int32_t InputDevice::getMetaState() { |
| 448 | int32_t result = 0; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 449 | for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 450 | return result; |
| 451 | } |
| 452 | |
| 453 | void InputDevice::updateMetaState(int32_t keyCode) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 454 | for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 457 | void InputDevice::bumpGeneration() { |
| 458 | mGeneration = mContext->bumpGeneration(); |
| 459 | } |
| 460 | |
| 461 | void InputDevice::notifyReset(nsecs_t when) { |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 462 | NotifyDeviceResetArgs args(mContext->getNextId(), when, mId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 463 | mContext->getListener()->notifyDeviceReset(&args); |
| 464 | } |
| 465 | |
| 466 | std::optional<int32_t> InputDevice::getAssociatedDisplayId() { |
| 467 | // Check if we had associated to the specific display. |
| 468 | if (mAssociatedViewport) { |
| 469 | return mAssociatedViewport->displayId; |
| 470 | } |
| 471 | |
| 472 | // No associated display port, check if some InputMapper is associated. |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 473 | return first_in_mappers<int32_t>( |
| 474 | [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 477 | // returns the number of mappers associated with the device |
| 478 | size_t InputDevice::getMapperCount() { |
| 479 | size_t count = 0; |
| 480 | for (auto& deviceEntry : mDevices) { |
| 481 | auto& devicePair = deviceEntry.second; |
| 482 | auto& mappers = devicePair.second; |
| 483 | count += mappers.size(); |
| 484 | } |
| 485 | return count; |
| 486 | } |
| 487 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 488 | void InputDevice::updateLedState(bool reset) { |
| 489 | for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); }); |
| 490 | } |
| 491 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 492 | InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId) |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 493 | : mDevice(device), |
| 494 | mContext(device.getContext()), |
| 495 | mEventHub(device.getContext()->getEventHub()), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 496 | mId(eventHubId), |
| 497 | mDeviceId(device.getId()) {} |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 498 | |
| 499 | InputDeviceContext::~InputDeviceContext() {} |
| 500 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 501 | } // namespace android |