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