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