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