blob: bacc7203b61b7e964a9f9c9af632226ee97c2da5 [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 Singh3e56f7e2023-07-07 13:12:37 +000069std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
70 std::list<NotifyArgs> out;
71 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
72 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
73 "but the corresponding viewport is not found",
74 getName().c_str(), *mAssociatedDisplayPort);
75 enabled = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 }
77
Arpit Singh3e56f7e2023-07-07 13:12:37 +000078 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070079 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 }
81
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080082 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
83 // performed. The querying must happen when the device is enabled, so we reset after enabling
84 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Arpit Singh3e56f7e2023-07-07 13:12:37 +000085 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080086 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070087 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070088 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070089 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080090 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 }
92 // Must change generation to flag this device as changed
93 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070094 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095}
96
Chris Yee7310032020-09-22 15:36:28 -070097void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000098 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099
100 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
101 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700102 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
104 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700105 dump += StringPrintf(INDENT2 "IsWaking: %s\n", toString(mIsWaking));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
107 if (mAssociatedDisplayPort) {
108 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
109 } else {
110 dump += "<none>\n";
111 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700112 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
113 if (mAssociatedDisplayUniqueId) {
114 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
115 } else {
116 dump += "<none>\n";
117 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000119 dump += StringPrintf(INDENT2 "Sources: %s\n",
120 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700122 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123
124 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
125 if (!ranges.empty()) {
126 dump += INDENT2 "Motion Ranges:\n";
127 for (size_t i = 0; i < ranges.size(); i++) {
128 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700129 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130 char name[32];
131 if (label) {
132 strncpy(name, label, sizeof(name));
133 name[sizeof(name) - 1] = '\0';
134 } else {
135 snprintf(name, sizeof(name), "%d", range.axis);
136 }
137 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000140 name, inputEventSourceToString(range.source).c_str(), range.min,
141 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 }
143 }
144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800145 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800146 if (mController) {
147 mController->dump(dump);
148 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700149}
150
Arpit Singh8e6fb252023-04-06 11:49:17 +0000151void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800152 if (mDevices.find(eventHubId) != mDevices.end()) {
153 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800154 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000156 std::vector<std::unique_ptr<InputMapper>> mappers;
157
158 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000159}
160
Arpit Singh7f1765e2023-07-07 13:12:37 +0000161void InputDevice::addEventHubDevice(int32_t eventHubId,
162 const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000163 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh7f1765e2023-07-07 13:12:37 +0000164 return;
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000165 }
Arpit Singh7f1765e2023-07-07 13:12:37 +0000166 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
167 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000168
Arpit Singh7f1765e2023-07-07 13:12:37 +0000169 // insert the context into the devices set
170 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700171 // Must change generation to flag this device as changed
172 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800173}
174
175void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700176 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
177 // Delete mController, since the corresponding eventhub device is going away
178 mController = nullptr;
179 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800180 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700181}
182
Arpit Singhed6c3de2023-04-05 19:24:37 +0000183std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
184 const InputReaderConfiguration& readerConfig,
Arpit Singh7f1765e2023-07-07 13:12:37 +0000185 ConfigurationChanges changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700186 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700187 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700188 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800189 mControllerNumber = 0;
190
191 for_each_subdevice([this](InputDeviceContext& context) {
192 mClasses |= context.getDeviceClasses();
193 int32_t controllerNumber = context.getDeviceControllerNumber();
194 if (controllerNumber > 0) {
195 if (mControllerNumber && mControllerNumber != controllerNumber) {
196 ALOGW("InputDevice::configure(): composite device contains multiple unique "
197 "controller numbers");
198 }
199 mControllerNumber = controllerNumber;
200 }
201 });
202
Chris Ye1b0c7342020-07-28 21:57:03 -0700203 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
204 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700205
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000206 using Change = InputReaderConfiguration::Change;
207
Arpit Singh56adebc2023-04-25 13:56:05 +0000208 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000209 // Full configuration should happen the first time configure is called
210 // and when the device type is changed. Changing a device type can
211 // affect various other parameters so should result in a
212 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000213 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800214 mConfiguration.clear();
215 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000216 std::optional<PropertyMap> configuration =
217 getEventHub()->getConfiguration(context.getEventHubId());
218 if (configuration) {
219 mConfiguration.addAll(&(*configuration));
220 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800221 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000222
223 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000224 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700225 mIsWaking = mConfiguration.getBool("device.wake").value_or(false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700226 }
227
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000228 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700229 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700230 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
231 if (mAlias != alias) {
232 mAlias = alias;
233 bumpGeneration();
234 }
235 }
236 }
237
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000238 if (changes.test(Change::ENABLED_STATE)) {
239 // Do not execute this code on the first configure, because 'setEnabled' would call
240 // InputMapper::reset, and you can't reset a mapper before it has been configured.
241 // The mappers are configured for the first time at the bottom of this function.
242 auto it = readerConfig.disabledDevices.find(mId);
243 bool enabled = it == readerConfig.disabledDevices.end();
244 out += setEnabled(enabled, when);
245 }
246
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000247 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700248 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700250 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251 mAssociatedViewport = std::nullopt;
252 // Find the display port that corresponds to the current input port.
253 const std::string& inputPort = mIdentifier.location;
254 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000255 const std::unordered_map<std::string, uint8_t>& ports =
256 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 const auto& displayPort = ports.find(inputPort);
258 if (displayPort != ports.end()) {
259 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800260 } else {
261 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000262 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800263 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
264 if (displayUniqueId != displayUniqueIds.end()) {
265 mAssociatedDisplayUniqueId = displayUniqueId->second;
266 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700267 }
268 }
269
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000270 // If the device was explicitly disabled by the user, it would be present in the
271 // "disabledDevices" list. If it is associated with a specific display, and it was not
272 // explicitly disabled, then enable/disable the device based on whether we can find the
273 // corresponding viewport.
274 bool enabled =
275 (readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000277 mAssociatedViewport =
278 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700279 if (!mAssociatedViewport) {
280 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
281 "but the corresponding viewport is not found.",
282 getName().c_str(), *mAssociatedDisplayPort);
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000283 enabled = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700284 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700285 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
286 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000287 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700288 if (!mAssociatedViewport) {
289 ALOGW("Input device %s should be associated with display %s but the "
290 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800291 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000292 enabled = false;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700293 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 }
295
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000296 if (changes.any()) {
297 // For first-time configuration, only allow device to be disabled after mappers have
298 // finished configuring. This is because we need to read some of the properties from
299 // the device's open fd.
300 out += setEnabled(enabled, when);
301 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700302 }
303
Arpit Singhed6c3de2023-04-05 19:24:37 +0000304 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
305 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800306 mSources |= mapper.getSources();
307 });
Arpit Singh7f1765e2023-07-07 13:12:37 +0000308
309 // If a device is just plugged but it might be disabled, we need to update some info like
310 // axis range of touch from each InputMapper first, then disable it.
311 if (!changes.any()) {
Arpit Singh3e56f7e2023-07-07 13:12:37 +0000312 out += setEnabled(readerConfig.disabledDevices.find(mId) ==
313 readerConfig.disabledDevices.end(),
314 when);
Arpit Singh7f1765e2023-07-07 13:12:37 +0000315 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700317 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318}
319
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700320std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
321 std::list<NotifyArgs> out;
322 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323
324 mContext->updateGlobalMetaState();
325
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700326 out.push_back(notifyReset(when));
327 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700328}
329
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700330std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700331 // Process all of the events in order for each mapper.
332 // We cannot simply ask each mapper to process them in bulk because mappers may
333 // have side-effects that must be interleaved. For example, joystick movement events and
334 // gamepad button presses are handled by different mappers but they should be dispatched
335 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700336 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000338 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000339 const auto [type, code, value] =
340 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
341 rawEvent->value);
342 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
343 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800344 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345
346 if (mDropUntilNextSync) {
347 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
348 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000349 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700350 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000351 ALOGD_IF(debugRawEvents(),
352 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700353 }
354 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
355 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
356 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700357 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700358 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700359 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
360 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800361 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362 }
363 --count;
364 }
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700365 postProcess(out);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700366 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700367}
368
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700369void InputDevice::postProcess(std::list<NotifyArgs>& args) const {
370 if (mIsWaking) {
371 // Update policy flags to request wake for the `NotifyArgs` that come from waking devices.
372 for (auto& arg : args) {
373 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
374 notifyMotionArgs->policyFlags |= POLICY_FLAG_WAKE;
375 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
376 notifySwitchArgs->policyFlags |= POLICY_FLAG_WAKE;
377 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
378 notifyKeyArgs->policyFlags |= POLICY_FLAG_WAKE;
379 }
380 }
381 }
382}
383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700384std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
385 std::list<NotifyArgs> out;
386 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
387 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388}
389
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700390std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
391 std::list<NotifyArgs> out;
392 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
393 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394}
395
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000396InputDeviceInfo InputDevice::getDeviceInfo() {
397 InputDeviceInfo outDeviceInfo;
398 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000399 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800401 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000402 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800403
404 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000405 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800406 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000407 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700408}
409
410int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
411 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
412}
413
414int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
415 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
416}
417
418int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
419 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
420}
421
422int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
423 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800424 for (auto& deviceEntry : mDevices) {
425 auto& devicePair = deviceEntry.second;
426 auto& mappers = devicePair.second;
427 for (auto& mapperPtr : mappers) {
428 InputMapper& mapper = *mapperPtr;
429 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
430 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
431 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
432 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
433 if (currentResult >= AKEY_STATE_DOWN) {
434 return currentResult;
435 } else if (currentResult == AKEY_STATE_UP) {
436 result = currentResult;
437 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 }
439 }
440 }
441 return result;
442}
443
Arpit Singh8e6fb252023-04-06 11:49:17 +0000444std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000445 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
446 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000447 std::vector<std::unique_ptr<InputMapper>> mappers;
448
449 // Switch-like devices.
450 if (classes.test(InputDeviceClass::SWITCH)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000451 mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000452 }
453
454 // Scroll wheel-like devices.
455 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000456 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000457 }
458
459 // Vibrator-like devices.
460 if (classes.test(InputDeviceClass::VIBRATOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000461 mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000462 }
463
464 // Battery-like devices or light-containing devices.
465 // PeripheralController will be created with associated EventHub device.
466 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000467 mController = std::make_unique<PeripheralController>(contextPtr);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000468 }
469
470 // Keyboard-like devices.
471 uint32_t keyboardSource = 0;
472 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
473 if (classes.test(InputDeviceClass::KEYBOARD)) {
474 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
475 }
476 if (classes.test(InputDeviceClass::ALPHAKEY)) {
477 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
478 }
479 if (classes.test(InputDeviceClass::DPAD)) {
480 keyboardSource |= AINPUT_SOURCE_DPAD;
481 }
482 if (classes.test(InputDeviceClass::GAMEPAD)) {
483 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
484 }
485
486 if (keyboardSource != 0) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000487 mappers.push_back(createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig,
Arpit Singh033e3ec2023-04-26 14:43:16 +0000488 keyboardSource, keyboardType));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000489 }
490
491 // Cursor-like devices.
492 if (classes.test(InputDeviceClass::CURSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000493 mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000494 }
495
496 // Touchscreens and touchpad devices.
497 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
498 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
499 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
500 // at least load this setting from the IDC file.
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000501 const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000502 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
503 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
504 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
505 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000506 mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000507 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Arpit Singh51399572023-07-07 13:12:37 +0000508 mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000509 } else if (classes.test(InputDeviceClass::TOUCH)) {
Arpit Singh51399572023-07-07 13:12:37 +0000510 mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000511 }
512
513 // Joystick-like devices.
514 if (classes.test(InputDeviceClass::JOYSTICK)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000515 mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000516 }
517
518 // Motion sensor enabled devices.
519 if (classes.test(InputDeviceClass::SENSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000520 mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000521 }
522
523 // External stylus-like devices.
524 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000525 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000526 }
527 return mappers;
528}
529
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700530bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
531 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700532 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700533 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800534 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700535 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700536 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800537 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700538 return result;
539}
540
Philip Junker4af3b3d2021-12-14 10:36:55 +0100541int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
542 std::optional<int32_t> result = first_in_mappers<int32_t>(
543 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
544 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
545 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
546 }
547 return std::nullopt;
548 });
549 if (!result) {
550 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
551 "KEYBOARD found. The provided input device with id %d has sources %s.",
552 getId(), inputEventSourceToString(getSources()).c_str());
553 return AKEYCODE_UNKNOWN;
554 }
555 return *result;
556}
557
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700558std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
559 int32_t token) {
560 std::list<NotifyArgs> out;
561 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
562 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700563}
564
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700565std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
566 std::list<NotifyArgs> out;
567 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
568 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700569}
570
Chris Ye87143712020-11-10 05:05:58 +0000571bool InputDevice::isVibrating() {
572 bool vibrating = false;
573 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
574 return vibrating;
575}
576
577/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
578 * different vibration mappers then we could have duplicate IDs.
579 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
580 * we would definitely have duplicate IDs.
581 */
582std::vector<int32_t> InputDevice::getVibratorIds() {
583 std::vector<int32_t> vibrators;
584 for_each_mapper([&vibrators](InputMapper& mapper) {
585 std::vector<int32_t> devVibs = mapper.getVibratorIds();
586 vibrators.reserve(vibrators.size() + devVibs.size());
587 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
588 });
589 return vibrators;
590}
591
Chris Yef59a2f42020-10-16 12:55:26 -0700592bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
593 std::chrono::microseconds samplingPeriod,
594 std::chrono::microseconds maxBatchReportLatency) {
595 bool success = true;
596 for_each_mapper(
597 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
598 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
599 });
600 return success;
601}
602
603void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
604 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
605}
606
607void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
608 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
609}
610
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700611std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
612 std::list<NotifyArgs> out;
613 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
614 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700615}
616
Chris Ye3fdbfef2021-01-06 18:45:18 -0800617bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800618 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800619}
620
621bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800622 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800623}
624
625std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800626 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800627}
628
629std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800630 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800631}
632
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700633int32_t InputDevice::getMetaState() {
634 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800635 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700636 return result;
637}
638
639void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000640 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
641 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
642 mapper.updateMetaState(keyCode)) {
643 return std::make_optional(true);
644 }
645 return std::optional<bool>();
646 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700647}
648
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000649void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
650 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
651 context.addKeyRemapping(fromKeyCode, toKeyCode);
652 });
653}
654
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700655void InputDevice::bumpGeneration() {
656 mGeneration = mContext->bumpGeneration();
657}
658
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700659NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
660 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700661}
662
663std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
664 // Check if we had associated to the specific display.
665 if (mAssociatedViewport) {
666 return mAssociatedViewport->displayId;
667 }
668
669 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800670 return first_in_mappers<int32_t>(
671 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700672}
673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800674// returns the number of mappers associated with the device
675size_t InputDevice::getMapperCount() {
676 size_t count = 0;
677 for (auto& deviceEntry : mDevices) {
678 auto& devicePair = deviceEntry.second;
679 auto& mappers = devicePair.second;
680 count += mappers.size();
681 }
682 return count;
683}
684
arthurhungc903df12020-08-11 15:08:42 +0800685void InputDevice::updateLedState(bool reset) {
686 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
687}
688
Andy Chenf9f1a022022-08-29 20:07:10 -0400689std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
690 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
691}
692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800693InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800694 : mDevice(device),
695 mContext(device.getContext()),
696 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 mId(eventHubId),
698 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800699
700InputDeviceContext::~InputDeviceContext() {}
701
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700702} // namespace android