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) { |
| 237 | mDevices.erase(eventHubId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, |
| 241 | uint32_t changes) { |
| 242 | mSources = 0; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 243 | mClasses = ftl::Flags<InputDeviceClass>(0); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 244 | mControllerNumber = 0; |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 245 | mCountryCode = InputDeviceCountryCode::INVALID; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 246 | |
| 247 | for_each_subdevice([this](InputDeviceContext& context) { |
| 248 | mClasses |= context.getDeviceClasses(); |
| 249 | int32_t controllerNumber = context.getDeviceControllerNumber(); |
| 250 | if (controllerNumber > 0) { |
| 251 | if (mControllerNumber && mControllerNumber != controllerNumber) { |
| 252 | ALOGW("InputDevice::configure(): composite device contains multiple unique " |
| 253 | "controller numbers"); |
| 254 | } |
| 255 | mControllerNumber = controllerNumber; |
| 256 | } |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 257 | |
| 258 | InputDeviceCountryCode countryCode = context.getCountryCode(); |
| 259 | if (countryCode != InputDeviceCountryCode::INVALID) { |
| 260 | if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) { |
| 261 | ALOGW("InputDevice::configure(): %s device contains multiple unique country " |
| 262 | "codes", |
| 263 | getName().c_str()); |
| 264 | } |
| 265 | mCountryCode = countryCode; |
| 266 | } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 267 | }); |
| 268 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 269 | mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL); |
| 270 | mHasMic = mClasses.test(InputDeviceClass::MIC); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 271 | |
| 272 | if (!isIgnored()) { |
| 273 | if (!changes) { // first time only |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 274 | mConfiguration.clear(); |
| 275 | for_each_subdevice([this](InputDeviceContext& context) { |
| 276 | PropertyMap configuration; |
| 277 | context.getConfiguration(&configuration); |
| 278 | mConfiguration.addAll(&configuration); |
| 279 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 283 | if (!mClasses.test(InputDeviceClass::VIRTUAL)) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 284 | std::shared_ptr<KeyCharacterMap> keyboardLayout = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 285 | mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 286 | bool shouldBumpGeneration = false; |
| 287 | for_each_subdevice( |
| 288 | [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) { |
| 289 | if (context.setKeyboardLayoutOverlay(keyboardLayout)) { |
| 290 | shouldBumpGeneration = true; |
| 291 | } |
| 292 | }); |
| 293 | if (shouldBumpGeneration) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 294 | bumpGeneration(); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 300 | if (!(mClasses.test(InputDeviceClass::VIRTUAL))) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 301 | std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); |
| 302 | if (mAlias != alias) { |
| 303 | mAlias = alias; |
| 304 | bumpGeneration(); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) { |
| 310 | auto it = config->disabledDevices.find(mId); |
| 311 | bool enabled = it == config->disabledDevices.end(); |
| 312 | setEnabled(enabled, when); |
| 313 | } |
| 314 | |
| 315 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 316 | // In most situations, no port or name will be specified. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 317 | mAssociatedDisplayPort = std::nullopt; |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 318 | mAssociatedDisplayUniqueId = std::nullopt; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 319 | mAssociatedViewport = std::nullopt; |
| 320 | // Find the display port that corresponds to the current input port. |
| 321 | const std::string& inputPort = mIdentifier.location; |
| 322 | if (!inputPort.empty()) { |
| 323 | const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations; |
| 324 | const auto& displayPort = ports.find(inputPort); |
| 325 | if (displayPort != ports.end()) { |
| 326 | mAssociatedDisplayPort = std::make_optional(displayPort->second); |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 327 | } else { |
| 328 | const std::unordered_map<std::string, std::string>& displayUniqueIds = |
| 329 | config->uniqueIdAssociations; |
| 330 | const auto& displayUniqueId = displayUniqueIds.find(inputPort); |
| 331 | if (displayUniqueId != displayUniqueIds.end()) { |
| 332 | mAssociatedDisplayUniqueId = displayUniqueId->second; |
| 333 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | // If the device was explicitly disabled by the user, it would be present in the |
| 338 | // "disabledDevices" list. If it is associated with a specific display, and it was not |
| 339 | // explicitly disabled, then enable/disable the device based on whether we can find the |
| 340 | // corresponding viewport. |
| 341 | bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end()); |
| 342 | if (mAssociatedDisplayPort) { |
| 343 | mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort); |
| 344 | if (!mAssociatedViewport) { |
| 345 | ALOGW("Input device %s should be associated with display on port %" PRIu8 ", " |
| 346 | "but the corresponding viewport is not found.", |
| 347 | getName().c_str(), *mAssociatedDisplayPort); |
| 348 | enabled = false; |
| 349 | } |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 350 | } else if (mAssociatedDisplayUniqueId != std::nullopt) { |
| 351 | mAssociatedViewport = |
| 352 | config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId); |
| 353 | if (!mAssociatedViewport) { |
| 354 | ALOGW("Input device %s should be associated with display %s but the " |
| 355 | "corresponding viewport cannot be found", |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 356 | getName().c_str(), mAssociatedDisplayUniqueId->c_str()); |
Christine Franks | 1ba71cc | 2021-04-07 14:37:42 -0700 | [diff] [blame] | 357 | enabled = false; |
| 358 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (changes) { |
| 362 | // For first-time configuration, only allow device to be disabled after mappers have |
| 363 | // finished configuring. This is because we need to read some of the properties from |
| 364 | // the device's open fd. |
| 365 | setEnabled(enabled, when); |
| 366 | } |
| 367 | } |
| 368 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 369 | for_each_mapper([this, when, config, changes](InputMapper& mapper) { |
| 370 | mapper.configure(when, config, changes); |
| 371 | mSources |= mapper.getSources(); |
| 372 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 373 | |
| 374 | // If a device is just plugged but it might be disabled, we need to update some info like |
| 375 | // axis range of touch from each InputMapper first, then disable it. |
| 376 | if (!changes) { |
| 377 | setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void InputDevice::reset(nsecs_t when) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 383 | for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 384 | |
| 385 | mContext->updateGlobalMetaState(); |
| 386 | |
| 387 | notifyReset(when); |
| 388 | } |
| 389 | |
| 390 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 391 | // Process all of the events in order for each mapper. |
| 392 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 393 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 394 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 395 | // in the order received. |
| 396 | for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) { |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 397 | if (DEBUG_RAW_EVENTS) { |
| 398 | ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64, |
| 399 | rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, |
| 400 | rawEvent->when); |
| 401 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 402 | |
| 403 | if (mDropUntilNextSync) { |
| 404 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
| 405 | mDropUntilNextSync = false; |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 406 | if (DEBUG_RAW_EVENTS) { |
| 407 | ALOGD("Recovered from input event buffer overrun."); |
| 408 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 409 | } else { |
Siarhei Vishniakou | 465e1c0 | 2021-12-09 10:47:29 -0800 | [diff] [blame] | 410 | if (DEBUG_RAW_EVENTS) { |
| 411 | ALOGD("Dropped input event while waiting for next input sync."); |
| 412 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 413 | } |
| 414 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
| 415 | ALOGI("Detected input event buffer overrun for device %s.", getName().c_str()); |
| 416 | mDropUntilNextSync = true; |
| 417 | reset(rawEvent->when); |
| 418 | } else { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 419 | for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) { |
| 420 | mapper.process(rawEvent); |
| 421 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 422 | } |
| 423 | --count; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void InputDevice::timeoutExpired(nsecs_t when) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 428 | for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void InputDevice::updateExternalStylusState(const StylusState& state) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 432 | for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 435 | InputDeviceInfo InputDevice::getDeviceInfo() { |
| 436 | InputDeviceInfo outDeviceInfo; |
| 437 | outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal, |
Vaibhav Devmurari | dd82b8e | 2022-08-16 15:34:01 +0000 | [diff] [blame] | 438 | mHasMic, mCountryCode); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 439 | for_each_mapper( |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 440 | [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); }); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 441 | |
| 442 | if (mController) { |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 443 | mController->populateDeviceInfo(&outDeviceInfo); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 444 | } |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 445 | return outDeviceInfo; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 449 | return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState); |
| 450 | } |
| 451 | |
| 452 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 453 | return getState(sourceMask, scanCode, &InputMapper::getScanCodeState); |
| 454 | } |
| 455 | |
| 456 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 457 | return getState(sourceMask, switchCode, &InputMapper::getSwitchState); |
| 458 | } |
| 459 | |
| 460 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 461 | int32_t result = AKEY_STATE_UNKNOWN; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 462 | for (auto& deviceEntry : mDevices) { |
| 463 | auto& devicePair = deviceEntry.second; |
| 464 | auto& mappers = devicePair.second; |
| 465 | for (auto& mapperPtr : mappers) { |
| 466 | InputMapper& mapper = *mapperPtr; |
| 467 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
| 468 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 469 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 470 | int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code); |
| 471 | if (currentResult >= AKEY_STATE_DOWN) { |
| 472 | return currentResult; |
| 473 | } else if (currentResult == AKEY_STATE_UP) { |
| 474 | result = currentResult; |
| 475 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | } |
| 479 | return result; |
| 480 | } |
| 481 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 482 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
| 483 | uint8_t* outFlags) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 484 | bool result = false; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 485 | for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 486 | if (sourcesMatchMask(mapper.getSources(), sourceMask)) { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 487 | result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 488 | } |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 489 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 490 | return result; |
| 491 | } |
| 492 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 493 | int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const { |
| 494 | std::optional<int32_t> result = first_in_mappers<int32_t>( |
| 495 | [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const { |
| 496 | if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) { |
| 497 | return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode)); |
| 498 | } |
| 499 | return std::nullopt; |
| 500 | }); |
| 501 | if (!result) { |
| 502 | ALOGE("Failed to get key code for key location: No matching InputMapper with source mask " |
| 503 | "KEYBOARD found. The provided input device with id %d has sources %s.", |
| 504 | getId(), inputEventSourceToString(getSources()).c_str()); |
| 505 | return AKEYCODE_UNKNOWN; |
| 506 | } |
| 507 | return *result; |
| 508 | } |
| 509 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 510 | void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) { |
| 511 | for_each_mapper([sequence, repeat, token](InputMapper& mapper) { |
| 512 | mapper.vibrate(sequence, repeat, token); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 513 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void InputDevice::cancelVibrate(int32_t token) { |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 517 | for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 520 | bool InputDevice::isVibrating() { |
| 521 | bool vibrating = false; |
| 522 | for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); }); |
| 523 | return vibrating; |
| 524 | } |
| 525 | |
| 526 | /* There's no guarantee the IDs provided by the different mappers are unique, so if we have two |
| 527 | * different vibration mappers then we could have duplicate IDs. |
| 528 | * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities, |
| 529 | * we would definitely have duplicate IDs. |
| 530 | */ |
| 531 | std::vector<int32_t> InputDevice::getVibratorIds() { |
| 532 | std::vector<int32_t> vibrators; |
| 533 | for_each_mapper([&vibrators](InputMapper& mapper) { |
| 534 | std::vector<int32_t> devVibs = mapper.getVibratorIds(); |
| 535 | vibrators.reserve(vibrators.size() + devVibs.size()); |
| 536 | vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end()); |
| 537 | }); |
| 538 | return vibrators; |
| 539 | } |
| 540 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 541 | bool InputDevice::enableSensor(InputDeviceSensorType sensorType, |
| 542 | std::chrono::microseconds samplingPeriod, |
| 543 | std::chrono::microseconds maxBatchReportLatency) { |
| 544 | bool success = true; |
| 545 | for_each_mapper( |
| 546 | [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) { |
| 547 | success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency); |
| 548 | }); |
| 549 | return success; |
| 550 | } |
| 551 | |
| 552 | void InputDevice::disableSensor(InputDeviceSensorType sensorType) { |
| 553 | for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); }); |
| 554 | } |
| 555 | |
| 556 | void InputDevice::flushSensor(InputDeviceSensorType sensorType) { |
| 557 | for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); }); |
| 558 | } |
| 559 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 560 | void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) { |
| 561 | for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 564 | bool InputDevice::setLightColor(int32_t lightId, int32_t color) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 565 | return mController ? mController->setLightColor(lightId, color) : false; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 569 | return mController ? mController->setLightPlayerId(lightId, playerId) : false; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 573 | return mController ? mController->getLightColor(lightId) : std::nullopt; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) { |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 577 | return mController ? mController->getLightPlayerId(lightId) : std::nullopt; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 580 | int32_t InputDevice::getMetaState() { |
| 581 | int32_t result = 0; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 582 | for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 583 | return result; |
| 584 | } |
| 585 | |
| 586 | void InputDevice::updateMetaState(int32_t keyCode) { |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 587 | first_in_mappers<bool>([keyCode](InputMapper& mapper) { |
| 588 | if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) && |
| 589 | mapper.updateMetaState(keyCode)) { |
| 590 | return std::make_optional(true); |
| 591 | } |
| 592 | return std::optional<bool>(); |
| 593 | }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 596 | void InputDevice::bumpGeneration() { |
| 597 | mGeneration = mContext->bumpGeneration(); |
| 598 | } |
| 599 | |
| 600 | void InputDevice::notifyReset(nsecs_t when) { |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 601 | NotifyDeviceResetArgs args(mContext->getNextId(), when, mId); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 602 | mContext->getListener().notifyDeviceReset(&args); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | std::optional<int32_t> InputDevice::getAssociatedDisplayId() { |
| 606 | // Check if we had associated to the specific display. |
| 607 | if (mAssociatedViewport) { |
| 608 | return mAssociatedViewport->displayId; |
| 609 | } |
| 610 | |
| 611 | // No associated display port, check if some InputMapper is associated. |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 612 | return first_in_mappers<int32_t>( |
| 613 | [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); }); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 616 | // returns the number of mappers associated with the device |
| 617 | size_t InputDevice::getMapperCount() { |
| 618 | size_t count = 0; |
| 619 | for (auto& deviceEntry : mDevices) { |
| 620 | auto& devicePair = deviceEntry.second; |
| 621 | auto& mappers = devicePair.second; |
| 622 | count += mappers.size(); |
| 623 | } |
| 624 | return count; |
| 625 | } |
| 626 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 627 | void InputDevice::updateLedState(bool reset) { |
| 628 | for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); }); |
| 629 | } |
| 630 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 631 | std::optional<int32_t> InputDevice::getBatteryEventHubId() const { |
| 632 | return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt; |
| 633 | } |
| 634 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 635 | InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId) |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 636 | : mDevice(device), |
| 637 | mContext(device.getContext()), |
| 638 | mEventHub(device.getContext()->getEventHub()), |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 639 | mId(eventHubId), |
| 640 | mDeviceId(device.getId()) {} |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 641 | |
| 642 | InputDeviceContext::~InputDeviceContext() {} |
| 643 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 644 | } // namespace android |