blob: 49998a0789980f6ca0c154e0cca2117fe06be096 [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,
70 const InputReaderConfiguration& readerConfig) {
71 // If the device was explicitly disabled by the user, it would be present in the
72 // "disabledDevices" list. This device should be disabled.
73 bool enable = readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end();
74
75 // If a device is associated with a specific display but there is no
76 // associated DisplayViewport, don't enable the device.
77 if (enable && (mAssociatedDisplayPort || mAssociatedDisplayUniqueId) && !mAssociatedViewport) {
78 const std::string desc = mAssociatedDisplayPort
79 ? "port " + std::to_string(*mAssociatedDisplayPort)
80 : "uniqueId " + *mAssociatedDisplayUniqueId;
81 ALOGW("Cannot enable input device %s because it is associated "
82 "with %s, but the corresponding viewport is not found",
83 getName().c_str(), desc.c_str());
84 enable = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 }
86
Arpit Singh48189772023-05-30 14:12:49 +000087 std::list<NotifyArgs> out;
88 if (isEnabled() == enable) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070089 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080092 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
93 // performed. The querying must happen when the device is enabled, so we reset after enabling
94 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Arpit Singh48189772023-05-30 14:12:49 +000095 if (enable) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080096 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070097 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070099 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800100 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101 }
102 // Must change generation to flag this device as changed
103 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700104 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105}
106
Chris Yee7310032020-09-22 15:36:28 -0700107void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000108 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700109
110 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
111 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700112 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700113 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
114 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700115 dump += StringPrintf(INDENT2 "IsWaking: %s\n", toString(mIsWaking));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
117 if (mAssociatedDisplayPort) {
118 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
119 } else {
120 dump += "<none>\n";
121 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700122 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
123 if (mAssociatedDisplayUniqueId) {
124 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
125 } else {
126 dump += "<none>\n";
127 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000129 dump += StringPrintf(INDENT2 "Sources: %s\n",
130 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700132 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133
134 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
135 if (!ranges.empty()) {
136 dump += INDENT2 "Motion Ranges:\n";
137 for (size_t i = 0; i < ranges.size(); i++) {
138 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700139 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 char name[32];
141 if (label) {
142 strncpy(name, label, sizeof(name));
143 name[sizeof(name) - 1] = '\0';
144 } else {
145 snprintf(name, sizeof(name), "%d", range.axis);
146 }
147 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000148 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700149 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000150 name, inputEventSourceToString(range.source).c_str(), range.min,
151 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152 }
153 }
154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800155 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800156 if (mController) {
157 mController->dump(dump);
158 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159}
160
Arpit Singh8e6fb252023-04-06 11:49:17 +0000161void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800162 if (mDevices.find(eventHubId) != mDevices.end()) {
163 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800164 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800165 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000166 std::vector<std::unique_ptr<InputMapper>> mappers;
167
168 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000169}
170
Arpit Singh7f1765e2023-07-07 13:12:37 +0000171void InputDevice::addEventHubDevice(int32_t eventHubId,
172 const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000173 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh7f1765e2023-07-07 13:12:37 +0000174 return;
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000175 }
Arpit Singh7f1765e2023-07-07 13:12:37 +0000176 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
177 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000178
Arpit Singh7f1765e2023-07-07 13:12:37 +0000179 // insert the context into the devices set
180 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700181 // Must change generation to flag this device as changed
182 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800183}
184
185void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700186 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
187 // Delete mController, since the corresponding eventhub device is going away
188 mController = nullptr;
189 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800190 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191}
192
Arpit Singhed6c3de2023-04-05 19:24:37 +0000193std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
194 const InputReaderConfiguration& readerConfig,
Arpit Singh7f1765e2023-07-07 13:12:37 +0000195 ConfigurationChanges changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700196 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700197 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700198 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800199 mControllerNumber = 0;
200
201 for_each_subdevice([this](InputDeviceContext& context) {
202 mClasses |= context.getDeviceClasses();
203 int32_t controllerNumber = context.getDeviceControllerNumber();
204 if (controllerNumber > 0) {
205 if (mControllerNumber && mControllerNumber != controllerNumber) {
206 ALOGW("InputDevice::configure(): composite device contains multiple unique "
207 "controller numbers");
208 }
209 mControllerNumber = controllerNumber;
210 }
211 });
212
Chris Ye1b0c7342020-07-28 21:57:03 -0700213 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
214 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700215
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000216 using Change = InputReaderConfiguration::Change;
217
Arpit Singh56adebc2023-04-25 13:56:05 +0000218 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000219 // Full configuration should happen the first time configure is called
220 // and when the device type is changed. Changing a device type can
221 // affect various other parameters so should result in a
222 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000223 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800224 mConfiguration.clear();
225 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000226 std::optional<PropertyMap> configuration =
227 getEventHub()->getConfiguration(context.getEventHubId());
228 if (configuration) {
229 mConfiguration.addAll(&(*configuration));
230 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800231 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000232
233 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000234 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700235 mIsWaking = mConfiguration.getBool("device.wake").value_or(false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700236 }
237
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000238 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700239 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
241 if (mAlias != alias) {
242 mAlias = alias;
243 bumpGeneration();
244 }
245 }
246 }
247
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000248 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700249 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700250 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700251 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 mAssociatedViewport = std::nullopt;
253 // Find the display port that corresponds to the current input port.
254 const std::string& inputPort = mIdentifier.location;
255 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000256 const std::unordered_map<std::string, uint8_t>& ports =
257 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258 const auto& displayPort = ports.find(inputPort);
259 if (displayPort != ports.end()) {
260 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800261 } else {
262 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000263 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800264 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
265 if (displayUniqueId != displayUniqueIds.end()) {
266 mAssociatedDisplayUniqueId = displayUniqueId->second;
267 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 }
269 }
270
Arpit Singh48189772023-05-30 14:12:49 +0000271 // If it is associated with a specific display, then find the corresponding viewport
272 // which will be used to enable/disable the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700273 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000274 mAssociatedViewport =
275 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 if (!mAssociatedViewport) {
277 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
278 "but the corresponding viewport is not found.",
279 getName().c_str(), *mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700281 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
282 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000283 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700284 if (!mAssociatedViewport) {
285 ALOGW("Input device %s should be associated with display %s but the "
286 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800287 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700288 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700289 }
Arpit Singh48189772023-05-30 14:12:49 +0000290 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291
Arpit Singh48189772023-05-30 14:12:49 +0000292 if (changes.test(Change::ENABLED_STATE) || changes.test(Change::DISPLAY_INFO)) {
293 // Whether a device is enabled can depend on the display association,
294 // so update the enabled state when there is a change in display info.
295 // NOTE: The first configuration of a mapper must happen with the device enabled.
296 // Do not execute this code on the first configure to prevent mappers
297 // from being configured with the device disabled.
298 out += updateEnableState(when, readerConfig);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 }
300
Arpit Singhed6c3de2023-04-05 19:24:37 +0000301 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
302 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800303 mSources |= mapper.getSources();
304 });
Arpit Singh7f1765e2023-07-07 13:12:37 +0000305
306 // If a device is just plugged but it might be disabled, we need to update some info like
307 // axis range of touch from each InputMapper first, then disable it.
308 if (!changes.any()) {
Arpit Singh48189772023-05-30 14:12:49 +0000309 out += updateEnableState(when, readerConfig);
Arpit Singh7f1765e2023-07-07 13:12:37 +0000310 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313}
314
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700315std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
316 std::list<NotifyArgs> out;
317 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318
319 mContext->updateGlobalMetaState();
320
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700321 out.push_back(notifyReset(when));
322 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323}
324
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700325std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 // Process all of the events in order for each mapper.
327 // We cannot simply ask each mapper to process them in bulk because mappers may
328 // have side-effects that must be interleaved. For example, joystick movement events and
329 // gamepad button presses are handled by different mappers but they should be dispatched
330 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700331 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700332 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000333 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000334 const auto [type, code, value] =
335 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
336 rawEvent->value);
337 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
338 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800339 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340
341 if (mDropUntilNextSync) {
342 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
343 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000344 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000346 ALOGD_IF(debugRawEvents(),
347 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700348 }
349 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
350 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
351 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700352 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700353 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700354 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
355 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800356 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700357 }
358 --count;
359 }
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700360 postProcess(out);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700361 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362}
363
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700364void InputDevice::postProcess(std::list<NotifyArgs>& args) const {
365 if (mIsWaking) {
366 // Update policy flags to request wake for the `NotifyArgs` that come from waking devices.
367 for (auto& arg : args) {
368 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
369 notifyMotionArgs->policyFlags |= POLICY_FLAG_WAKE;
370 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
371 notifySwitchArgs->policyFlags |= POLICY_FLAG_WAKE;
372 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
373 notifyKeyArgs->policyFlags |= POLICY_FLAG_WAKE;
374 }
375 }
376 }
377}
378
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700379std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
380 std::list<NotifyArgs> out;
381 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
382 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700383}
384
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700385std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
386 std::list<NotifyArgs> out;
387 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
388 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389}
390
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000391InputDeviceInfo InputDevice::getDeviceInfo() {
392 InputDeviceInfo outDeviceInfo;
393 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000394 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800396 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000397 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800398
399 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000400 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800401 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000402 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403}
404
405int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
406 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
407}
408
409int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
410 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
411}
412
413int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
414 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
415}
416
417int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
418 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800419 for (auto& deviceEntry : mDevices) {
420 auto& devicePair = deviceEntry.second;
421 auto& mappers = devicePair.second;
422 for (auto& mapperPtr : mappers) {
423 InputMapper& mapper = *mapperPtr;
424 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
425 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
426 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
427 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
428 if (currentResult >= AKEY_STATE_DOWN) {
429 return currentResult;
430 } else if (currentResult == AKEY_STATE_UP) {
431 result = currentResult;
432 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 }
434 }
435 }
436 return result;
437}
438
Arpit Singh8e6fb252023-04-06 11:49:17 +0000439std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000440 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
441 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000442 std::vector<std::unique_ptr<InputMapper>> mappers;
443
444 // Switch-like devices.
445 if (classes.test(InputDeviceClass::SWITCH)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000446 mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000447 }
448
449 // Scroll wheel-like devices.
450 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000451 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000452 }
453
454 // Vibrator-like devices.
455 if (classes.test(InputDeviceClass::VIBRATOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000456 mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000457 }
458
459 // Battery-like devices or light-containing devices.
460 // PeripheralController will be created with associated EventHub device.
461 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000462 mController = std::make_unique<PeripheralController>(contextPtr);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000463 }
464
465 // Keyboard-like devices.
466 uint32_t keyboardSource = 0;
467 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
468 if (classes.test(InputDeviceClass::KEYBOARD)) {
469 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
470 }
471 if (classes.test(InputDeviceClass::ALPHAKEY)) {
472 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
473 }
474 if (classes.test(InputDeviceClass::DPAD)) {
475 keyboardSource |= AINPUT_SOURCE_DPAD;
476 }
477 if (classes.test(InputDeviceClass::GAMEPAD)) {
478 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
479 }
480
481 if (keyboardSource != 0) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000482 mappers.push_back(createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig,
Arpit Singh033e3ec2023-04-26 14:43:16 +0000483 keyboardSource, keyboardType));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000484 }
485
486 // Cursor-like devices.
487 if (classes.test(InputDeviceClass::CURSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000488 mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000489 }
490
491 // Touchscreens and touchpad devices.
492 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
493 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
494 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
495 // at least load this setting from the IDC file.
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000496 const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000497 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
498 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
499 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
500 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000501 mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000502 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Arpit Singh51399572023-07-07 13:12:37 +0000503 mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000504 } else if (classes.test(InputDeviceClass::TOUCH)) {
Arpit Singh51399572023-07-07 13:12:37 +0000505 mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000506 }
507
508 // Joystick-like devices.
509 if (classes.test(InputDeviceClass::JOYSTICK)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000510 mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000511 }
512
513 // Motion sensor enabled devices.
514 if (classes.test(InputDeviceClass::SENSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000515 mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000516 }
517
518 // External stylus-like devices.
519 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000520 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000521 }
522 return mappers;
523}
524
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700525bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
526 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700527 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700528 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800529 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700530 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700531 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800532 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700533 return result;
534}
535
Philip Junker4af3b3d2021-12-14 10:36:55 +0100536int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
537 std::optional<int32_t> result = first_in_mappers<int32_t>(
538 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
539 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
540 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
541 }
542 return std::nullopt;
543 });
544 if (!result) {
545 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
546 "KEYBOARD found. The provided input device with id %d has sources %s.",
547 getId(), inputEventSourceToString(getSources()).c_str());
548 return AKEYCODE_UNKNOWN;
549 }
550 return *result;
551}
552
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700553std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
554 int32_t token) {
555 std::list<NotifyArgs> out;
556 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
557 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700558}
559
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700560std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
561 std::list<NotifyArgs> out;
562 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
563 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564}
565
Chris Ye87143712020-11-10 05:05:58 +0000566bool InputDevice::isVibrating() {
567 bool vibrating = false;
568 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
569 return vibrating;
570}
571
572/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
573 * different vibration mappers then we could have duplicate IDs.
574 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
575 * we would definitely have duplicate IDs.
576 */
577std::vector<int32_t> InputDevice::getVibratorIds() {
578 std::vector<int32_t> vibrators;
579 for_each_mapper([&vibrators](InputMapper& mapper) {
580 std::vector<int32_t> devVibs = mapper.getVibratorIds();
581 vibrators.reserve(vibrators.size() + devVibs.size());
582 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
583 });
584 return vibrators;
585}
586
Chris Yef59a2f42020-10-16 12:55:26 -0700587bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
588 std::chrono::microseconds samplingPeriod,
589 std::chrono::microseconds maxBatchReportLatency) {
590 bool success = true;
591 for_each_mapper(
592 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
593 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
594 });
595 return success;
596}
597
598void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
599 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
600}
601
602void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
603 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
604}
605
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700606std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
607 std::list<NotifyArgs> out;
608 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
609 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700610}
611
Chris Ye3fdbfef2021-01-06 18:45:18 -0800612bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800613 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800614}
615
616bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800617 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800618}
619
620std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800621 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800622}
623
624std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800625 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800626}
627
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700628int32_t InputDevice::getMetaState() {
629 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800630 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631 return result;
632}
633
634void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000635 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
636 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
637 mapper.updateMetaState(keyCode)) {
638 return std::make_optional(true);
639 }
640 return std::optional<bool>();
641 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700642}
643
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000644void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
645 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
646 context.addKeyRemapping(fromKeyCode, toKeyCode);
647 });
648}
649
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700650void InputDevice::bumpGeneration() {
651 mGeneration = mContext->bumpGeneration();
652}
653
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700654NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
655 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700656}
657
658std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
659 // Check if we had associated to the specific display.
660 if (mAssociatedViewport) {
661 return mAssociatedViewport->displayId;
662 }
663
664 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800665 return first_in_mappers<int32_t>(
666 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700667}
668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800669// returns the number of mappers associated with the device
670size_t InputDevice::getMapperCount() {
671 size_t count = 0;
672 for (auto& deviceEntry : mDevices) {
673 auto& devicePair = deviceEntry.second;
674 auto& mappers = devicePair.second;
675 count += mappers.size();
676 }
677 return count;
678}
679
arthurhungc903df12020-08-11 15:08:42 +0800680void InputDevice::updateLedState(bool reset) {
681 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
682}
683
Andy Chenf9f1a022022-08-29 20:07:10 -0400684std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
685 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
686}
687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800688InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800689 : mDevice(device),
690 mContext(device.getContext()),
691 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800692 mId(eventHubId),
693 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800694
695InputDeviceContext::~InputDeviceContext() {}
696
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700697} // namespace android