blob: 6185f1ab9e3ff82b552b58cd8664bd4521dfffbc [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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. Lewisa7b82e12020-02-12 15:40:45 -080021#include <algorithm>
22
Harry Cutts89844622022-12-02 15:02:26 +000023#include <android/sysprop/InputProperties.sysprop.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070024#include <ftl/flags.h>
25
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080026#include "CursorInputMapper.h"
27#include "ExternalStylusInputMapper.h"
28#include "InputReaderContext.h"
29#include "JoystickInputMapper.h"
30#include "KeyboardInputMapper.h"
31#include "MultiTouchInputMapper.h"
Chris Ye1dd2e5c2021-04-04 23:12:41 -070032#include "PeripheralController.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080033#include "RotaryEncoderInputMapper.h"
Chris Yef59a2f42020-10-16 12:55:26 -070034#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "SingleTouchInputMapper.h"
36#include "SwitchInputMapper.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000037#include "TouchpadInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080038#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039
40namespace android {
41
42InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080043 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 : mContext(context),
45 mId(id),
46 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080049 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 mSources(0),
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -070051 mIsWaking(false),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 mIsExternal(false),
53 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080054 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080056InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057
58bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080059 if (!hasEventHubDevices()) {
60 return false;
61 }
Chris Yee14523a2020-12-19 13:46:00 -080062 // An input device composed of sub devices can be individually enabled or disabled.
63 // If any of the sub device is enabled then the input device is considered as enabled.
64 bool enabled = false;
65 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
66 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067}
68
Arpit Singh48189772023-05-30 14:12:49 +000069std::list<NotifyArgs> InputDevice::updateEnableState(nsecs_t when,
Arpit Singh82f29a12023-06-13 15:05:53 +000070 const InputReaderConfiguration& readerConfig,
71 bool forceEnable) {
72 bool enable = forceEnable;
73 if (!forceEnable) {
74 // If the device was explicitly disabled by the user, it would be present in the
75 // "disabledDevices" list. This device should be disabled.
76 enable = readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end();
Arpit Singh48189772023-05-30 14:12:49 +000077
Arpit Singh82f29a12023-06-13 15:05:53 +000078 // If a device is associated with a specific display but there is no
79 // associated DisplayViewport, don't enable the device.
Antonio Kantek0ac5e092024-04-22 17:10:27 +000080 if (enable && (mAssociatedDisplayPort || mAssociatedDisplayUniqueIdByPort) &&
Arpit Singh82f29a12023-06-13 15:05:53 +000081 !mAssociatedViewport) {
82 const std::string desc = mAssociatedDisplayPort
83 ? "port " + std::to_string(*mAssociatedDisplayPort)
Antonio Kantek0ac5e092024-04-22 17:10:27 +000084 : "uniqueId " + *mAssociatedDisplayUniqueIdByPort;
Arpit Singh82f29a12023-06-13 15:05:53 +000085 ALOGW("Cannot enable input device %s because it is associated "
86 "with %s, but the corresponding viewport is not found",
87 getName().c_str(), desc.c_str());
88 enable = false;
89 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91
Arpit Singh48189772023-05-30 14:12:49 +000092 std::list<NotifyArgs> out;
93 if (isEnabled() == enable) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070094 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095 }
96
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080097 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
98 // performed. The querying must happen when the device is enabled, so we reset after enabling
99 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Arpit Singh48189772023-05-30 14:12:49 +0000100 if (enable) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800101 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700102 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700104 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800105 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 }
107 // Must change generation to flag this device as changed
108 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700109 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110}
111
Chris Yee7310032020-09-22 15:36:28 -0700112void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000113 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114
115 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
116 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700117 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
119 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700120 dump += StringPrintf(INDENT2 "IsWaking: %s\n", toString(mIsWaking));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
122 if (mAssociatedDisplayPort) {
123 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
124 } else {
125 dump += "<none>\n";
126 }
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000127 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueIdByPort: ");
128 if (mAssociatedDisplayUniqueIdByPort) {
129 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueIdByPort->c_str());
130 } else {
131 dump += "<none>\n";
132 }
133 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueIdByDescriptor: ");
134 if (mAssociatedDisplayUniqueIdByDescriptor) {
135 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueIdByDescriptor->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700136 } else {
137 dump += "<none>\n";
138 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000140 dump += StringPrintf(INDENT2 "Sources: %s\n",
141 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700143 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700144
145 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
146 if (!ranges.empty()) {
147 dump += INDENT2 "Motion Ranges:\n";
148 for (size_t i = 0; i < ranges.size(); i++) {
149 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700150 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700151 char name[32];
152 if (label) {
153 strncpy(name, label, sizeof(name));
154 name[sizeof(name) - 1] = '\0';
155 } else {
156 snprintf(name, sizeof(name), "%d", range.axis);
157 }
158 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000159 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700160 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000161 name, inputEventSourceToString(range.source).c_str(), range.min,
162 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700163 }
164 }
165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800166 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800167 if (mController) {
168 mController->dump(dump);
169 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700170}
171
Arpit Singh8e6fb252023-04-06 11:49:17 +0000172void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800173 if (mDevices.find(eventHubId) != mDevices.end()) {
174 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800175 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800176 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000177 std::vector<std::unique_ptr<InputMapper>> mappers;
178
179 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000180}
181
Arpit Singh82f29a12023-06-13 15:05:53 +0000182[[nodiscard]] std::list<NotifyArgs> InputDevice::addEventHubDevice(
183 nsecs_t when, int32_t eventHubId, const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000184 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh82f29a12023-06-13 15:05:53 +0000185 return {};
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000186 }
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000187
Arpit Singh82f29a12023-06-13 15:05:53 +0000188 // Add an empty device configure and keep it enabled to allow mapper population with correct
189 // configuration/context,
190 // Note: we need to ensure device is kept enabled till mappers are configured
191 // TODO: b/281852638 refactor tests to remove this flag and reliance on the empty device
192 addEmptyEventHubDevice(eventHubId);
193 std::list<NotifyArgs> out = configureInternal(when, readerConfig, {}, /*forceEnable=*/true);
194
195 DevicePair& devicePair = mDevices[eventHubId];
196 devicePair.second = createMappers(*devicePair.first, readerConfig);
197
Chris Yee7310032020-09-22 15:36:28 -0700198 // Must change generation to flag this device as changed
199 bumpGeneration();
Arpit Singh82f29a12023-06-13 15:05:53 +0000200 return out;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800201}
202
203void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700204 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
205 // Delete mController, since the corresponding eventhub device is going away
206 mController = nullptr;
207 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800208 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209}
210
Arpit Singhed6c3de2023-04-05 19:24:37 +0000211std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
212 const InputReaderConfiguration& readerConfig,
Arpit Singh7f1765e2023-07-07 13:12:37 +0000213 ConfigurationChanges changes) {
Arpit Singh82f29a12023-06-13 15:05:53 +0000214 return configureInternal(when, readerConfig, changes);
215}
216std::list<NotifyArgs> InputDevice::configureInternal(nsecs_t when,
217 const InputReaderConfiguration& readerConfig,
218 ConfigurationChanges changes,
219 bool forceEnable) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700220 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700222 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800223 mControllerNumber = 0;
224
225 for_each_subdevice([this](InputDeviceContext& context) {
226 mClasses |= context.getDeviceClasses();
227 int32_t controllerNumber = context.getDeviceControllerNumber();
228 if (controllerNumber > 0) {
229 if (mControllerNumber && mControllerNumber != controllerNumber) {
230 ALOGW("InputDevice::configure(): composite device contains multiple unique "
231 "controller numbers");
232 }
233 mControllerNumber = controllerNumber;
234 }
235 });
236
Chris Ye1b0c7342020-07-28 21:57:03 -0700237 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
238 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700239
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000240 // Update keyboard type
241 if (mClasses.test(InputDeviceClass::KEYBOARD)) {
242 mContext->getKeyboardClassifier().notifyKeyboardChanged(mId, mIdentifier, mClasses.get());
243 mKeyboardType = mContext->getKeyboardClassifier().getKeyboardType(mId);
244 }
245
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000246 using Change = InputReaderConfiguration::Change;
247
Arpit Singh56adebc2023-04-25 13:56:05 +0000248 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000249 // Full configuration should happen the first time configure is called
250 // and when the device type is changed. Changing a device type can
251 // affect various other parameters so should result in a
252 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000253 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800254 mConfiguration.clear();
255 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000256 std::optional<PropertyMap> configuration =
257 getEventHub()->getConfiguration(context.getEventHubId());
258 if (configuration) {
259 mConfiguration.addAll(&(*configuration));
260 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800261 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000262
263 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000264 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700265 mIsWaking = mConfiguration.getBool("device.wake").value_or(false);
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800266 mShouldSmoothScroll = mConfiguration.getBool("device.viewBehavior_smoothScroll");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700267 }
268
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000269 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700270 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
272 if (mAlias != alias) {
273 mAlias = alias;
274 bumpGeneration();
275 }
276 }
277 }
278
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000279 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Prabir Pradhan55c5ee22024-02-14 06:03:02 +0000280 const auto oldAssociatedDisplayId = getAssociatedDisplayId();
281
Christine Franks1ba71cc2021-04-07 14:37:42 -0700282 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700283 mAssociatedDisplayPort = std::nullopt;
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000284 mAssociatedDisplayUniqueIdByPort = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285 mAssociatedViewport = std::nullopt;
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000286 // Find the display port that corresponds to the current input device descriptor
287 const std::string& inputDeviceDescriptor = mIdentifier.descriptor;
288 if (!inputDeviceDescriptor.empty()) {
289 const std::unordered_map<std::string, uint8_t>& ports =
Mayank Garg60b45622024-05-05 20:23:56 -0700290 readerConfig.inputPortToDisplayPortAssociations;
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000291 const auto& displayPort = ports.find(inputDeviceDescriptor);
292 if (displayPort != ports.end()) {
293 mAssociatedDisplayPort = std::make_optional(displayPort->second);
294 } else {
295 const std::unordered_map<std::string, std::string>&
296 displayUniqueIdsByDescriptor =
Mayank Garg60b45622024-05-05 20:23:56 -0700297 readerConfig.inputDeviceDescriptorToDisplayUniqueIdAssociations;
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000298 const auto& displayUniqueIdByDescriptor =
299 displayUniqueIdsByDescriptor.find(inputDeviceDescriptor);
300 if (displayUniqueIdByDescriptor != displayUniqueIdsByDescriptor.end()) {
301 mAssociatedDisplayUniqueIdByDescriptor =
302 displayUniqueIdByDescriptor->second;
303 }
304 }
305 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 // Find the display port that corresponds to the current input port.
307 const std::string& inputPort = mIdentifier.location;
308 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000309 const std::unordered_map<std::string, uint8_t>& ports =
Mayank Garg60b45622024-05-05 20:23:56 -0700310 readerConfig.inputPortToDisplayPortAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 const auto& displayPort = ports.find(inputPort);
312 if (displayPort != ports.end()) {
313 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800314 } else {
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000315 const std::unordered_map<std::string, std::string>& displayUniqueIdsByPort =
Mayank Garg60b45622024-05-05 20:23:56 -0700316 readerConfig.inputPortToDisplayUniqueIdAssociations;
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000317 const auto& displayUniqueIdByPort = displayUniqueIdsByPort.find(inputPort);
318 if (displayUniqueIdByPort != displayUniqueIdsByPort.end()) {
319 mAssociatedDisplayUniqueIdByPort = displayUniqueIdByPort->second;
Christine Franks2a2293c2022-01-18 11:51:16 -0800320 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 }
322 }
323
Arpit Singh48189772023-05-30 14:12:49 +0000324 // If it is associated with a specific display, then find the corresponding viewport
325 // which will be used to enable/disable the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000327 mAssociatedViewport =
328 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329 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);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 }
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000334 } else if (mAssociatedDisplayUniqueIdByDescriptor != std::nullopt) {
335 mAssociatedViewport = readerConfig.getDisplayViewportByUniqueId(
336 *mAssociatedDisplayUniqueIdByDescriptor);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700337 if (!mAssociatedViewport) {
338 ALOGW("Input device %s should be associated with display %s but the "
339 "corresponding viewport cannot be found",
Antonio Kantek0ac5e092024-04-22 17:10:27 +0000340 getName().c_str(), mAssociatedDisplayUniqueIdByDescriptor->c_str());
341 }
342 } else if (mAssociatedDisplayUniqueIdByPort != std::nullopt) {
343 mAssociatedViewport = readerConfig.getDisplayViewportByUniqueId(
344 *mAssociatedDisplayUniqueIdByPort);
345 if (!mAssociatedViewport) {
346 ALOGW("Input device %s should be associated with display %s but the "
347 "corresponding viewport cannot be found",
348 getName().c_str(), mAssociatedDisplayUniqueIdByPort->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700349 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700350 }
Prabir Pradhan55c5ee22024-02-14 06:03:02 +0000351
352 if (getAssociatedDisplayId() != oldAssociatedDisplayId) {
353 bumpGeneration();
354 }
Arpit Singh48189772023-05-30 14:12:49 +0000355 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700356
Arpit Singhed6c3de2023-04-05 19:24:37 +0000357 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
358 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800359 mSources |= mapper.getSources();
360 });
Arpit Singh7f1765e2023-07-07 13:12:37 +0000361
Arpit Singh82f29a12023-06-13 15:05:53 +0000362 if (!changes.any() || changes.test(Change::ENABLED_STATE) ||
363 changes.test(Change::DISPLAY_INFO)) {
364 // Whether a device is enabled can depend on the display association,
365 // so update the enabled state when there is a change in display info.
366 out += updateEnableState(when, readerConfig, forceEnable);
Arpit Singh7f1765e2023-07-07 13:12:37 +0000367 }
Linnan Lie5657f22024-09-13 21:54:37 +0800368
369 if (!changes.any() || changes.test(InputReaderConfiguration::Change::KEY_REMAPPING)) {
370 const bool isFullKeyboard =
371 (mSources & AINPUT_SOURCE_KEYBOARD) == AINPUT_SOURCE_KEYBOARD &&
372 mKeyboardType == KeyboardType::ALPHABETIC;
373 if (isFullKeyboard) {
374 for_each_subdevice([&readerConfig](auto& context) {
375 context.setKeyRemapping(readerConfig.keyRemapping);
376 });
377 bumpGeneration();
378 }
379 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700381 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700382}
383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700384std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
385 std::list<NotifyArgs> out;
386 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700387
388 mContext->updateGlobalMetaState();
389
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700390 out.push_back(notifyReset(when));
391 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392}
393
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700394std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395 // 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.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700400 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700401 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000402 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000403 const auto [type, code, value] =
404 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
405 rawEvent->value);
406 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
407 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800408 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409
410 if (mDropUntilNextSync) {
411 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Arpit Singh4b4a4572023-11-24 18:19:56 +0000412 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700413 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000414 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000416 ALOGD_IF(debugRawEvents(),
417 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700418 }
419 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
420 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
421 mDropUntilNextSync = true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700423 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
Harry Cuttsa32a1192024-06-04 15:10:31 +0000424 out += mapper.process(*rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800425 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700426 }
427 --count;
428 }
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700429 postProcess(out);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700430 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431}
432
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700433void InputDevice::postProcess(std::list<NotifyArgs>& args) const {
434 if (mIsWaking) {
435 // Update policy flags to request wake for the `NotifyArgs` that come from waking devices.
436 for (auto& arg : args) {
437 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
438 notifyMotionArgs->policyFlags |= POLICY_FLAG_WAKE;
439 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
440 notifySwitchArgs->policyFlags |= POLICY_FLAG_WAKE;
441 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
442 notifyKeyArgs->policyFlags |= POLICY_FLAG_WAKE;
443 }
444 }
445 }
446}
447
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700448std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
449 std::list<NotifyArgs> out;
450 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
451 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700452}
453
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700454std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
455 std::list<NotifyArgs> out;
456 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
457 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700458}
459
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000460InputDeviceInfo InputDevice::getDeviceInfo() {
461 InputDeviceInfo outDeviceInfo;
462 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700463 mHasMic,
464 getAssociatedDisplayId().value_or(ui::LogicalDisplayId::INVALID),
Linnan Li48f80da2024-04-22 18:38:16 +0000465 {mShouldSmoothScroll}, isEnabled());
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000466 outDeviceInfo.setKeyboardType(static_cast<int32_t>(mKeyboardType));
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800468 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000469 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800470
471 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000472 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800473 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000474 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475}
476
477int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
478 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
479}
480
481int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
482 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
483}
484
485int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
486 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
487}
488
489int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
490 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800491 for (auto& deviceEntry : mDevices) {
492 auto& devicePair = deviceEntry.second;
493 auto& mappers = devicePair.second;
494 for (auto& mapperPtr : mappers) {
495 InputMapper& mapper = *mapperPtr;
496 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
497 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
498 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
499 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
500 if (currentResult >= AKEY_STATE_DOWN) {
501 return currentResult;
502 } else if (currentResult == AKEY_STATE_UP) {
503 result = currentResult;
504 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700505 }
506 }
507 }
508 return result;
509}
510
Arpit Singh8e6fb252023-04-06 11:49:17 +0000511std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000512 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
513 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000514 std::vector<std::unique_ptr<InputMapper>> mappers;
515
516 // Switch-like devices.
517 if (classes.test(InputDeviceClass::SWITCH)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000518 mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000519 }
520
521 // Scroll wheel-like devices.
522 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000523 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000524 }
525
526 // Vibrator-like devices.
527 if (classes.test(InputDeviceClass::VIBRATOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000528 mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000529 }
530
531 // Battery-like devices or light-containing devices.
532 // PeripheralController will be created with associated EventHub device.
533 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000534 mController = std::make_unique<PeripheralController>(contextPtr);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000535 }
536
537 // Keyboard-like devices.
538 uint32_t keyboardSource = 0;
Arpit Singh8e6fb252023-04-06 11:49:17 +0000539 if (classes.test(InputDeviceClass::KEYBOARD)) {
540 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
541 }
Arpit Singh8e6fb252023-04-06 11:49:17 +0000542 if (classes.test(InputDeviceClass::DPAD)) {
543 keyboardSource |= AINPUT_SOURCE_DPAD;
544 }
545 if (classes.test(InputDeviceClass::GAMEPAD)) {
546 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
547 }
548
549 if (keyboardSource != 0) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000550 mappers.push_back(
551 createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig, keyboardSource));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000552 }
553
554 // Cursor-like devices.
555 if (classes.test(InputDeviceClass::CURSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000556 mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000557 }
558
559 // Touchscreens and touchpad devices.
Harry Cutts60b459a2024-03-22 18:21:55 +0000560 if (classes.test(InputDeviceClass::TOUCHPAD) && classes.test(InputDeviceClass::TOUCH_MT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000561 mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000562 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Arpit Singhd7053742023-05-18 16:56:41 +0000563 mappers.push_back(createInputMapper<MultiTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000564 } else if (classes.test(InputDeviceClass::TOUCH)) {
Arpit Singhd7053742023-05-18 16:56:41 +0000565 mappers.push_back(createInputMapper<SingleTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000566 }
567
568 // Joystick-like devices.
569 if (classes.test(InputDeviceClass::JOYSTICK)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000570 mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000571 }
572
573 // Motion sensor enabled devices.
574 if (classes.test(InputDeviceClass::SENSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000575 mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000576 }
577
578 // External stylus-like devices.
579 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000580 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000581 }
582 return mappers;
583}
584
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700585bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
586 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700587 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700588 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800589 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700590 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700591 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800592 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700593 return result;
594}
595
Philip Junker4af3b3d2021-12-14 10:36:55 +0100596int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
597 std::optional<int32_t> result = first_in_mappers<int32_t>(
598 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
599 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
600 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
601 }
602 return std::nullopt;
603 });
604 if (!result) {
605 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
606 "KEYBOARD found. The provided input device with id %d has sources %s.",
607 getId(), inputEventSourceToString(getSources()).c_str());
608 return AKEYCODE_UNKNOWN;
609 }
610 return *result;
611}
612
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700613std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
614 int32_t token) {
615 std::list<NotifyArgs> out;
616 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
617 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700618}
619
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700620std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
621 std::list<NotifyArgs> out;
622 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
623 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700624}
625
Chris Ye87143712020-11-10 05:05:58 +0000626bool InputDevice::isVibrating() {
627 bool vibrating = false;
628 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
629 return vibrating;
630}
631
632/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
633 * different vibration mappers then we could have duplicate IDs.
634 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
635 * we would definitely have duplicate IDs.
636 */
637std::vector<int32_t> InputDevice::getVibratorIds() {
638 std::vector<int32_t> vibrators;
639 for_each_mapper([&vibrators](InputMapper& mapper) {
640 std::vector<int32_t> devVibs = mapper.getVibratorIds();
641 vibrators.reserve(vibrators.size() + devVibs.size());
642 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
643 });
644 return vibrators;
645}
646
Chris Yef59a2f42020-10-16 12:55:26 -0700647bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
648 std::chrono::microseconds samplingPeriod,
649 std::chrono::microseconds maxBatchReportLatency) {
650 bool success = true;
651 for_each_mapper(
652 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
653 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
654 });
655 return success;
656}
657
658void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
659 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
660}
661
662void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
663 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
664}
665
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700666std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
667 std::list<NotifyArgs> out;
668 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
669 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700670}
671
Chris Ye3fdbfef2021-01-06 18:45:18 -0800672bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800673 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800674}
675
676bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800677 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800678}
679
680std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800681 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800682}
683
684std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800685 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800686}
687
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700688int32_t InputDevice::getMetaState() {
689 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800690 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700691 return result;
692}
693
694void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000695 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
696 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
697 mapper.updateMetaState(keyCode)) {
698 return std::make_optional(true);
699 }
700 return std::optional<bool>();
701 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700702}
703
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700704void InputDevice::bumpGeneration() {
705 mGeneration = mContext->bumpGeneration();
706}
707
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700708NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
709 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700710}
711
Linnan Li13bf76a2024-05-05 19:18:02 +0800712std::optional<ui::LogicalDisplayId> InputDevice::getAssociatedDisplayId() {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700713 // Check if we had associated to the specific display.
714 if (mAssociatedViewport) {
715 return mAssociatedViewport->displayId;
716 }
717
718 // No associated display port, check if some InputMapper is associated.
Linnan Li13bf76a2024-05-05 19:18:02 +0800719 return first_in_mappers<ui::LogicalDisplayId>(
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800720 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700721}
722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800723// returns the number of mappers associated with the device
724size_t InputDevice::getMapperCount() {
725 size_t count = 0;
726 for (auto& deviceEntry : mDevices) {
727 auto& devicePair = deviceEntry.second;
728 auto& mappers = devicePair.second;
729 count += mappers.size();
730 }
731 return count;
732}
733
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000734std::optional<HardwareProperties> InputDevice::getTouchpadHardwareProperties() {
735 std::optional<HardwareProperties> result = first_in_mappers<HardwareProperties>(
736 [](InputMapper& mapper) -> std::optional<HardwareProperties> {
737 return mapper.getTouchpadHardwareProperties();
738 });
739
740 return result;
741}
742
arthurhungc903df12020-08-11 15:08:42 +0800743void InputDevice::updateLedState(bool reset) {
744 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
745}
746
Andy Chenf9f1a022022-08-29 20:07:10 -0400747std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
748 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
749}
750
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000751void InputDevice::setKeyboardType(KeyboardType keyboardType) {
752 if (mKeyboardType != keyboardType) {
753 mKeyboardType = keyboardType;
754 bumpGeneration();
755 }
756}
757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800759 : mDevice(device),
760 mContext(device.getContext()),
761 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800762 mId(eventHubId),
763 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800764
765InputDeviceContext::~InputDeviceContext() {}
766
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700767} // namespace android