blob: 0a64a1c4a8fcb4c9e6b1c731dcc13632a344ffb9 [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),
51 mIsExternal(false),
52 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080055InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
57bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080058 if (!hasEventHubDevices()) {
59 return false;
60 }
Chris Yee14523a2020-12-19 13:46:00 -080061 // 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 Pradhanbaa5c822019-08-30 15:27:05 -070066}
67
Arpit Singh3e56f7e2023-07-07 13:12:37 +000068std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
69 std::list<NotifyArgs> out;
70 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
71 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
72 "but the corresponding viewport is not found",
73 getName().c_str(), *mAssociatedDisplayPort);
74 enabled = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 }
76
Arpit Singh3e56f7e2023-07-07 13:12:37 +000077 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 }
80
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080081 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
82 // performed. The querying must happen when the device is enabled, so we reset after enabling
83 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Arpit Singh3e56f7e2023-07-07 13:12:37 +000084 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91 // Must change generation to flag this device as changed
92 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094}
95
Chris Yee7310032020-09-22 15:36:28 -070096void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000097 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
99 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
100 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700101 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
103 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
104 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
105 if (mAssociatedDisplayPort) {
106 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
107 } else {
108 dump += "<none>\n";
109 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700110 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
111 if (mAssociatedDisplayUniqueId) {
112 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
113 } else {
114 dump += "<none>\n";
115 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000117 dump += StringPrintf(INDENT2 "Sources: %s\n",
118 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700120 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
122 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
123 if (!ranges.empty()) {
124 dump += INDENT2 "Motion Ranges:\n";
125 for (size_t i = 0; i < ranges.size(); i++) {
126 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700127 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 char name[32];
129 if (label) {
130 strncpy(name, label, sizeof(name));
131 name[sizeof(name) - 1] = '\0';
132 } else {
133 snprintf(name, sizeof(name), "%d", range.axis);
134 }
135 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000136 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 name, inputEventSourceToString(range.source).c_str(), range.min,
139 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 }
141 }
142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800143 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800144 if (mController) {
145 mController->dump(dump);
146 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147}
148
Arpit Singh8e6fb252023-04-06 11:49:17 +0000149void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800150 if (mDevices.find(eventHubId) != mDevices.end()) {
151 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800152 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000154 std::vector<std::unique_ptr<InputMapper>> mappers;
155
156 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000157}
158
Arpit Singh7f1765e2023-07-07 13:12:37 +0000159void InputDevice::addEventHubDevice(int32_t eventHubId,
160 const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000161 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh7f1765e2023-07-07 13:12:37 +0000162 return;
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000163 }
Arpit Singh7f1765e2023-07-07 13:12:37 +0000164 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
165 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000166
Arpit Singh7f1765e2023-07-07 13:12:37 +0000167 // insert the context into the devices set
168 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700169 // Must change generation to flag this device as changed
170 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800171}
172
173void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700174 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
175 // Delete mController, since the corresponding eventhub device is going away
176 mController = nullptr;
177 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800178 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700179}
180
Arpit Singhed6c3de2023-04-05 19:24:37 +0000181std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
182 const InputReaderConfiguration& readerConfig,
Arpit Singh7f1765e2023-07-07 13:12:37 +0000183 ConfigurationChanges changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700184 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700185 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700186 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800187 mControllerNumber = 0;
188
189 for_each_subdevice([this](InputDeviceContext& context) {
190 mClasses |= context.getDeviceClasses();
191 int32_t controllerNumber = context.getDeviceControllerNumber();
192 if (controllerNumber > 0) {
193 if (mControllerNumber && mControllerNumber != controllerNumber) {
194 ALOGW("InputDevice::configure(): composite device contains multiple unique "
195 "controller numbers");
196 }
197 mControllerNumber = controllerNumber;
198 }
199 });
200
Chris Ye1b0c7342020-07-28 21:57:03 -0700201 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
202 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700203
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000204 using Change = InputReaderConfiguration::Change;
205
Arpit Singh56adebc2023-04-25 13:56:05 +0000206 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000207 // Full configuration should happen the first time configure is called
208 // and when the device type is changed. Changing a device type can
209 // affect various other parameters so should result in a
210 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000211 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800212 mConfiguration.clear();
213 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000214 std::optional<PropertyMap> configuration =
215 getEventHub()->getConfiguration(context.getEventHubId());
216 if (configuration) {
217 mConfiguration.addAll(&(*configuration));
218 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800219 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000220
221 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000222 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223 }
224
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000225 if (!changes.any() || changes.test(Change::KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700226 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700227 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700228 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800229 bool shouldBumpGeneration = false;
230 for_each_subdevice(
231 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
232 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
233 shouldBumpGeneration = true;
234 }
235 });
236 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700237 bumpGeneration();
238 }
239 }
240 }
241
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000242 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700243 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700244 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
245 if (mAlias != alias) {
246 mAlias = alias;
247 bumpGeneration();
248 }
249 }
250 }
251
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000252 if (changes.test(Change::ENABLED_STATE)) {
253 // Do not execute this code on the first configure, because 'setEnabled' would call
254 // InputMapper::reset, and you can't reset a mapper before it has been configured.
255 // The mappers are configured for the first time at the bottom of this function.
256 auto it = readerConfig.disabledDevices.find(mId);
257 bool enabled = it == readerConfig.disabledDevices.end();
258 out += setEnabled(enabled, when);
259 }
260
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000261 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700262 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700264 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700265 mAssociatedViewport = std::nullopt;
266 // Find the display port that corresponds to the current input port.
267 const std::string& inputPort = mIdentifier.location;
268 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000269 const std::unordered_map<std::string, uint8_t>& ports =
270 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 const auto& displayPort = ports.find(inputPort);
272 if (displayPort != ports.end()) {
273 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800274 } else {
275 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000276 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800277 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
278 if (displayUniqueId != displayUniqueIds.end()) {
279 mAssociatedDisplayUniqueId = displayUniqueId->second;
280 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 }
282 }
283
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000284 // If the device was explicitly disabled by the user, it would be present in the
285 // "disabledDevices" list. If it is associated with a specific display, and it was not
286 // explicitly disabled, then enable/disable the device based on whether we can find the
287 // corresponding viewport.
288 bool enabled =
289 (readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700290 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000291 mAssociatedViewport =
292 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700293 if (!mAssociatedViewport) {
294 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
295 "but the corresponding viewport is not found.",
296 getName().c_str(), *mAssociatedDisplayPort);
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000297 enabled = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700299 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
300 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000301 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700302 if (!mAssociatedViewport) {
303 ALOGW("Input device %s should be associated with display %s but the "
304 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800305 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000306 enabled = false;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700307 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308 }
309
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000310 if (changes.any()) {
311 // For first-time configuration, only allow device to be disabled after mappers have
312 // finished configuring. This is because we need to read some of the properties from
313 // the device's open fd.
314 out += setEnabled(enabled, when);
315 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 }
317
Arpit Singhed6c3de2023-04-05 19:24:37 +0000318 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
319 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800320 mSources |= mapper.getSources();
321 });
Arpit Singh7f1765e2023-07-07 13:12:37 +0000322
323 // If a device is just plugged but it might be disabled, we need to update some info like
324 // axis range of touch from each InputMapper first, then disable it.
325 if (!changes.any()) {
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000326 out += setEnabled(readerConfig.disabledDevices.find(mId) ==
327 readerConfig.disabledDevices.end(),
328 when);
Arpit Singh7f1765e2023-07-07 13:12:37 +0000329 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700331 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700332}
333
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700334std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
335 std::list<NotifyArgs> out;
336 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337
338 mContext->updateGlobalMetaState();
339
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700340 out.push_back(notifyReset(when));
341 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700342}
343
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700344std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345 // Process all of the events in order for each mapper.
346 // We cannot simply ask each mapper to process them in bulk because mappers may
347 // have side-effects that must be interleaved. For example, joystick movement events and
348 // gamepad button presses are handled by different mappers but they should be dispatched
349 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700350 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700351 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000352 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000353 const auto [type, code, value] =
354 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
355 rawEvent->value);
356 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
357 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800358 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700359
360 if (mDropUntilNextSync) {
361 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
362 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000363 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000365 ALOGD_IF(debugRawEvents(),
366 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700367 }
368 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
369 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
370 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700371 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700372 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700373 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
374 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800375 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376 }
377 --count;
378 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700379 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380}
381
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700382std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
383 std::list<NotifyArgs> out;
384 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
385 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386}
387
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
389 std::list<NotifyArgs> out;
390 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
391 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392}
393
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000394InputDeviceInfo InputDevice::getDeviceInfo() {
395 InputDeviceInfo outDeviceInfo;
396 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000397 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800399 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000400 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800401
402 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000403 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800404 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000405 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700406}
407
408int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
409 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
410}
411
412int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
413 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
414}
415
416int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
417 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
418}
419
420int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
421 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800422 for (auto& deviceEntry : mDevices) {
423 auto& devicePair = deviceEntry.second;
424 auto& mappers = devicePair.second;
425 for (auto& mapperPtr : mappers) {
426 InputMapper& mapper = *mapperPtr;
427 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
428 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
429 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
430 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
431 if (currentResult >= AKEY_STATE_DOWN) {
432 return currentResult;
433 } else if (currentResult == AKEY_STATE_UP) {
434 result = currentResult;
435 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 }
437 }
438 }
439 return result;
440}
441
Arpit Singh8e6fb252023-04-06 11:49:17 +0000442std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000443 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
444 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000445 std::vector<std::unique_ptr<InputMapper>> mappers;
446
447 // Switch-like devices.
448 if (classes.test(InputDeviceClass::SWITCH)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000449 mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000450 }
451
452 // Scroll wheel-like devices.
453 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000454 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000455 }
456
457 // Vibrator-like devices.
458 if (classes.test(InputDeviceClass::VIBRATOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000459 mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000460 }
461
462 // Battery-like devices or light-containing devices.
463 // PeripheralController will be created with associated EventHub device.
464 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000465 mController = std::make_unique<PeripheralController>(contextPtr);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000466 }
467
468 // Keyboard-like devices.
469 uint32_t keyboardSource = 0;
470 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
471 if (classes.test(InputDeviceClass::KEYBOARD)) {
472 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
473 }
474 if (classes.test(InputDeviceClass::ALPHAKEY)) {
475 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
476 }
477 if (classes.test(InputDeviceClass::DPAD)) {
478 keyboardSource |= AINPUT_SOURCE_DPAD;
479 }
480 if (classes.test(InputDeviceClass::GAMEPAD)) {
481 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
482 }
483
484 if (keyboardSource != 0) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000485 mappers.push_back(createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig,
Arpit Singh033e3ec2023-04-26 14:43:16 +0000486 keyboardSource, keyboardType));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000487 }
488
489 // Cursor-like devices.
490 if (classes.test(InputDeviceClass::CURSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000491 mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000492 }
493
494 // Touchscreens and touchpad devices.
495 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
496 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
497 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
498 // at least load this setting from the IDC file.
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000499 const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000500 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
501 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
502 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
503 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000504 mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000505 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Arpit Singh51399572023-07-07 13:12:37 +0000506 mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000507 } else if (classes.test(InputDeviceClass::TOUCH)) {
Arpit Singh51399572023-07-07 13:12:37 +0000508 mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000509 }
510
511 // Joystick-like devices.
512 if (classes.test(InputDeviceClass::JOYSTICK)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000513 mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000514 }
515
516 // Motion sensor enabled devices.
517 if (classes.test(InputDeviceClass::SENSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000518 mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000519 }
520
521 // External stylus-like devices.
522 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000523 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000524 }
525 return mappers;
526}
527
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700528bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
529 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700530 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700531 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800532 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700533 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700534 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800535 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700536 return result;
537}
538
Philip Junker4af3b3d2021-12-14 10:36:55 +0100539int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
540 std::optional<int32_t> result = first_in_mappers<int32_t>(
541 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
542 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
543 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
544 }
545 return std::nullopt;
546 });
547 if (!result) {
548 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
549 "KEYBOARD found. The provided input device with id %d has sources %s.",
550 getId(), inputEventSourceToString(getSources()).c_str());
551 return AKEYCODE_UNKNOWN;
552 }
553 return *result;
554}
555
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700556std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
557 int32_t token) {
558 std::list<NotifyArgs> out;
559 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
560 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700561}
562
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700563std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
564 std::list<NotifyArgs> out;
565 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
566 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700567}
568
Chris Ye87143712020-11-10 05:05:58 +0000569bool InputDevice::isVibrating() {
570 bool vibrating = false;
571 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
572 return vibrating;
573}
574
575/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
576 * different vibration mappers then we could have duplicate IDs.
577 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
578 * we would definitely have duplicate IDs.
579 */
580std::vector<int32_t> InputDevice::getVibratorIds() {
581 std::vector<int32_t> vibrators;
582 for_each_mapper([&vibrators](InputMapper& mapper) {
583 std::vector<int32_t> devVibs = mapper.getVibratorIds();
584 vibrators.reserve(vibrators.size() + devVibs.size());
585 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
586 });
587 return vibrators;
588}
589
Chris Yef59a2f42020-10-16 12:55:26 -0700590bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
591 std::chrono::microseconds samplingPeriod,
592 std::chrono::microseconds maxBatchReportLatency) {
593 bool success = true;
594 for_each_mapper(
595 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
596 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
597 });
598 return success;
599}
600
601void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
602 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
603}
604
605void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
606 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
607}
608
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700609std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
610 std::list<NotifyArgs> out;
611 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
612 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700613}
614
Chris Ye3fdbfef2021-01-06 18:45:18 -0800615bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800616 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800617}
618
619bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800620 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800621}
622
623std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800624 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800625}
626
627std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800628 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800629}
630
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631int32_t InputDevice::getMetaState() {
632 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800633 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700634 return result;
635}
636
637void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000638 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
639 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
640 mapper.updateMetaState(keyCode)) {
641 return std::make_optional(true);
642 }
643 return std::optional<bool>();
644 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700645}
646
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000647void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
648 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
649 context.addKeyRemapping(fromKeyCode, toKeyCode);
650 });
651}
652
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700653void InputDevice::bumpGeneration() {
654 mGeneration = mContext->bumpGeneration();
655}
656
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700657NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
658 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700659}
660
661std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
662 // Check if we had associated to the specific display.
663 if (mAssociatedViewport) {
664 return mAssociatedViewport->displayId;
665 }
666
667 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800668 return first_in_mappers<int32_t>(
669 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700670}
671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800672// returns the number of mappers associated with the device
673size_t InputDevice::getMapperCount() {
674 size_t count = 0;
675 for (auto& deviceEntry : mDevices) {
676 auto& devicePair = deviceEntry.second;
677 auto& mappers = devicePair.second;
678 count += mappers.size();
679 }
680 return count;
681}
682
arthurhungc903df12020-08-11 15:08:42 +0800683void InputDevice::updateLedState(bool reset) {
684 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
685}
686
Andy Chenf9f1a022022-08-29 20:07:10 -0400687std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
688 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
689}
690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800691InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800692 : mDevice(device),
693 mContext(device.getContext()),
694 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800695 mId(eventHubId),
696 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800697
698InputDeviceContext::~InputDeviceContext() {}
699
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700700} // namespace android