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