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