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