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