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