blob: 2bf634282e6da24dea3f79b2103bb470c275745a [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 Singhf4cdbef2023-05-30 14:12:49 +000068std::list<NotifyArgs> InputDevice::updateEnableState(nsecs_t when,
69 const InputReaderConfiguration& readerConfig,
70 bool forceEnable) {
71 bool enable = true;
72 if (!forceEnable) {
73 // If the device was explicitly disabled by the user, it would be present in the
74 // "disabledDevices" list. This device should be disabled.
75 enable = readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end();
76
77 // If a device is associated with a specific display but there is no
78 // associated DisplayViewport, don't enable the device.
79 if (enable && (mAssociatedDisplayPort || mAssociatedDisplayUniqueId) &&
80 !mAssociatedViewport) {
81 const std::string desc = mAssociatedDisplayPort
82 ? "port " + std::to_string(*mAssociatedDisplayPort)
83 : "uniqueId " + *mAssociatedDisplayUniqueId;
84 ALOGW("Cannot enable input device %s because it is associated "
85 "with %s, but the corresponding viewport is not found",
86 getName().c_str(), desc.c_str());
87 enable = false;
88 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 }
90
Arpit Singhf4cdbef2023-05-30 14:12:49 +000091 std::list<NotifyArgs> out;
92 if (isEnabled() == enable) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 }
95
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080096 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
97 // performed. The querying must happen when the device is enabled, so we reset after enabling
98 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Arpit Singhf4cdbef2023-05-30 14:12:49 +000099 if (enable) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800100 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700101 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700103 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800104 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105 }
106 // Must change generation to flag this device as changed
107 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700108 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700109}
110
Chris Yee7310032020-09-22 15:36:28 -0700111void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000112 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700113
114 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
115 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700116 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
118 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
119 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
120 if (mAssociatedDisplayPort) {
121 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
122 } else {
123 dump += "<none>\n";
124 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700125 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
126 if (mAssociatedDisplayUniqueId) {
127 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
128 } else {
129 dump += "<none>\n";
130 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000132 dump += StringPrintf(INDENT2 "Sources: %s\n",
133 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700135 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136
137 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
138 if (!ranges.empty()) {
139 dump += INDENT2 "Motion Ranges:\n";
140 for (size_t i = 0; i < ranges.size(); i++) {
141 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700142 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700143 char name[32];
144 if (label) {
145 strncpy(name, label, sizeof(name));
146 name[sizeof(name) - 1] = '\0';
147 } else {
148 snprintf(name, sizeof(name), "%d", range.axis);
149 }
150 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000151 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000153 name, inputEventSourceToString(range.source).c_str(), range.min,
154 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155 }
156 }
157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800158 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800159 if (mController) {
160 mController->dump(dump);
161 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700162}
163
Arpit Singh8e6fb252023-04-06 11:49:17 +0000164void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800165 if (mDevices.find(eventHubId) != mDevices.end()) {
166 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800167 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800168 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000169 std::vector<std::unique_ptr<InputMapper>> mappers;
170
171 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000172}
173
Arpit Singh7f1765e2023-07-07 13:12:37 +0000174void InputDevice::addEventHubDevice(int32_t eventHubId,
175 const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000176 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh7f1765e2023-07-07 13:12:37 +0000177 return;
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000178 }
Arpit Singh7f1765e2023-07-07 13:12:37 +0000179 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
180 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000181
Arpit Singh7f1765e2023-07-07 13:12:37 +0000182 // insert the context into the devices set
183 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700184 // Must change generation to flag this device as changed
185 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800186}
187
188void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700189 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
190 // Delete mController, since the corresponding eventhub device is going away
191 mController = nullptr;
192 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800193 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700194}
195
Arpit Singhed6c3de2023-04-05 19:24:37 +0000196std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
197 const InputReaderConfiguration& readerConfig,
Arpit Singh7f1765e2023-07-07 13:12:37 +0000198 ConfigurationChanges changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700199 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700200 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700201 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800202 mControllerNumber = 0;
203
204 for_each_subdevice([this](InputDeviceContext& context) {
205 mClasses |= context.getDeviceClasses();
206 int32_t controllerNumber = context.getDeviceControllerNumber();
207 if (controllerNumber > 0) {
208 if (mControllerNumber && mControllerNumber != controllerNumber) {
209 ALOGW("InputDevice::configure(): composite device contains multiple unique "
210 "controller numbers");
211 }
212 mControllerNumber = controllerNumber;
213 }
214 });
215
Chris Ye1b0c7342020-07-28 21:57:03 -0700216 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
217 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000219 using Change = InputReaderConfiguration::Change;
220
Arpit Singh56adebc2023-04-25 13:56:05 +0000221 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000222 // Full configuration should happen the first time configure is called
223 // and when the device type is changed. Changing a device type can
224 // affect various other parameters so should result in a
225 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000226 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227 mConfiguration.clear();
228 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000229 std::optional<PropertyMap> configuration =
230 getEventHub()->getConfiguration(context.getEventHubId());
231 if (configuration) {
232 mConfiguration.addAll(&(*configuration));
233 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800234 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000235
236 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000237 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700238 }
239
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000240 if (!changes.any() || changes.test(Change::KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700241 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700242 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244 bool shouldBumpGeneration = false;
245 for_each_subdevice(
246 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
247 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
248 shouldBumpGeneration = true;
249 }
250 });
251 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 bumpGeneration();
253 }
254 }
255 }
256
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000257 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700258 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
260 if (mAlias != alias) {
261 mAlias = alias;
262 bumpGeneration();
263 }
264 }
265 }
266
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000267 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700268 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700269 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700270 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 mAssociatedViewport = std::nullopt;
272 // Find the display port that corresponds to the current input port.
273 const std::string& inputPort = mIdentifier.location;
274 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 const std::unordered_map<std::string, uint8_t>& ports =
276 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700277 const auto& displayPort = ports.find(inputPort);
278 if (displayPort != ports.end()) {
279 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800280 } else {
281 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000282 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800283 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
284 if (displayUniqueId != displayUniqueIds.end()) {
285 mAssociatedDisplayUniqueId = displayUniqueId->second;
286 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 }
288 }
289
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000290 // If it is associated with a specific display, then find the corresponding viewport
291 // which will be used to enable/disable the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000293 mAssociatedViewport =
294 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295 if (!mAssociatedViewport) {
296 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
297 "but the corresponding viewport is not found.",
298 getName().c_str(), *mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700300 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
301 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000302 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700303 if (!mAssociatedViewport) {
304 ALOGW("Input device %s should be associated with display %s but the "
305 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800306 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700307 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308 }
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000309 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700310
Arpit Singh7f1765e2023-07-07 13:12:37 +0000311 if (changes.test(Change::ENABLED_STATE) || changes.test(Change::DISPLAY_INFO)) {
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000312 // Whether a device is enabled can depend on the display association,
313 // so update the enabled state when there is a change in display info.
Arpit Singh7f1765e2023-07-07 13:12:37 +0000314 // NOTE: The first configuration of a mapper must happen with the device enabled.
315 // Do not execute this code on the first configure to prevent mappers
316 // from being configured with the device disabled.
317 out += updateEnableState(when, readerConfig, false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 }
319
Arpit Singhed6c3de2023-04-05 19:24:37 +0000320 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
321 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800322 mSources |= mapper.getSources();
323 });
Arpit Singh7f1765e2023-07-07 13:12:37 +0000324
325 // If a device is just plugged but it might be disabled, we need to update some info like
326 // axis range of touch from each InputMapper first, then disable it.
327 if (!changes.any()) {
328 out += updateEnableState(when, readerConfig);
329 }
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