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