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