blob: 90ba003846b8b1e5b03a60a72fdf15a7878d3ed1 [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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070068std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
69 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
71 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
72 "but the corresponding viewport is not found",
73 getName().c_str(), *mAssociatedDisplayPort);
74 enabled = false;
75 }
76
77 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 }
80
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080081 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
82 // performed. The querying must happen when the device is enabled, so we reset after enabling
83 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91 // Must change generation to flag this device as changed
92 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094}
95
Chris Yee7310032020-09-22 15:36:28 -070096void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000097 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
99 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
100 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700101 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
103 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
104 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
105 if (mAssociatedDisplayPort) {
106 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
107 } else {
108 dump += "<none>\n";
109 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700110 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
111 if (mAssociatedDisplayUniqueId) {
112 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
113 } else {
114 dump += "<none>\n";
115 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000117 dump += StringPrintf(INDENT2 "Sources: %s\n",
118 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700120 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
122 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
123 if (!ranges.empty()) {
124 dump += INDENT2 "Motion Ranges:\n";
125 for (size_t i = 0; i < ranges.size(); i++) {
126 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700127 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 char name[32];
129 if (label) {
130 strncpy(name, label, sizeof(name));
131 name[sizeof(name) - 1] = '\0';
132 } else {
133 snprintf(name, sizeof(name), "%d", range.axis);
134 }
135 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000136 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 name, inputEventSourceToString(range.source).c_str(), range.min,
139 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 }
141 }
142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800143 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800144 if (mController) {
145 mController->dump(dump);
146 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147}
148
Arpit Singh8e6fb252023-04-06 11:49:17 +0000149void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800150 if (mDevices.find(eventHubId) != mDevices.end()) {
151 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800152 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Arpit Singh7f4dd512023-05-18 13:22:12 +0000154 mDevices.insert(
155 {eventHubId,
156 std::make_pair<std::unique_ptr<InputDeviceContext>,
157 std::vector<std::unique_ptr<InputMapper>>>(std::move(contextPtr), {})});
Arpit Singh8e6fb252023-04-06 11:49:17 +0000158}
159
Arpit Singh7f4dd512023-05-18 13:22:12 +0000160void InputDevice::populateMappers(int32_t eventHubId,
161 const InputReaderConfiguration& readerConfig) {
162 auto targetDevice = mDevices.find(eventHubId);
163 LOG_ALWAYS_FATAL_IF(targetDevice == mDevices.end(),
164 "InputDevice::populateMappers(): missing device with eventHubId %d ",
165 eventHubId);
166 // create and add mappers to device
167 InputDeviceContext& context = *targetDevice->second.first;
168 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(context, readerConfig);
169 targetDevice->second.second = std::move(mappers);
Chris Yee7310032020-09-22 15:36:28 -0700170 // Must change generation to flag this device as changed
171 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800172}
173
174void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700175 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
176 // Delete mController, since the corresponding eventhub device is going away
177 mController = nullptr;
178 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800179 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700180}
181
Arpit Singhed6c3de2023-04-05 19:24:37 +0000182std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
183 const InputReaderConfiguration& readerConfig,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000184 ConfigurationChanges changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700185 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700186 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700187 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800188 mControllerNumber = 0;
189
190 for_each_subdevice([this](InputDeviceContext& context) {
191 mClasses |= context.getDeviceClasses();
192 int32_t controllerNumber = context.getDeviceControllerNumber();
193 if (controllerNumber > 0) {
194 if (mControllerNumber && mControllerNumber != controllerNumber) {
195 ALOGW("InputDevice::configure(): composite device contains multiple unique "
196 "controller numbers");
197 }
198 mControllerNumber = controllerNumber;
199 }
200 });
201
Chris Ye1b0c7342020-07-28 21:57:03 -0700202 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
203 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700204
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000205 using Change = InputReaderConfiguration::Change;
206
Arpit Singh56adebc2023-04-25 13:56:05 +0000207 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000208 // Full configuration should happen the first time configure is called
209 // and when the device type is changed. Changing a device type can
210 // affect various other parameters so should result in a
211 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000212 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800213 mConfiguration.clear();
214 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000215 std::optional<PropertyMap> configuration =
216 getEventHub()->getConfiguration(context.getEventHubId());
217 if (configuration) {
218 mConfiguration.addAll(&(*configuration));
219 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800220 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000221
222 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000223 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 }
225
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000226 if (!changes.any() || changes.test(Change::KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700227 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700228 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800230 bool shouldBumpGeneration = false;
231 for_each_subdevice(
232 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
233 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
234 shouldBumpGeneration = true;
235 }
236 });
237 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700238 bumpGeneration();
239 }
240 }
241 }
242
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000243 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700244 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700245 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
246 if (mAlias != alias) {
247 mAlias = alias;
248 bumpGeneration();
249 }
250 }
251 }
252
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000253 if (changes.test(Change::ENABLED_STATE)) {
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700254 // Do not execute this code on the first configure, because 'setEnabled' would call
255 // InputMapper::reset, and you can't reset a mapper before it has been configured.
256 // The mappers are configured for the first time at the bottom of this function.
Arpit Singhed6c3de2023-04-05 19:24:37 +0000257 auto it = readerConfig.disabledDevices.find(mId);
258 bool enabled = it == readerConfig.disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700259 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 }
261
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000262 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700263 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700265 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 mAssociatedViewport = std::nullopt;
267 // Find the display port that corresponds to the current input port.
268 const std::string& inputPort = mIdentifier.location;
269 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000270 const std::unordered_map<std::string, uint8_t>& ports =
271 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 const auto& displayPort = ports.find(inputPort);
273 if (displayPort != ports.end()) {
274 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800275 } else {
276 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000277 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800278 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
279 if (displayUniqueId != displayUniqueIds.end()) {
280 mAssociatedDisplayUniqueId = displayUniqueId->second;
281 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700282 }
283 }
284
285 // If the device was explicitly disabled by the user, it would be present in the
286 // "disabledDevices" list. If it is associated with a specific display, and it was not
287 // explicitly disabled, then enable/disable the device based on whether we can find the
288 // corresponding viewport.
Arpit Singhed6c3de2023-04-05 19:24:37 +0000289 bool enabled =
290 (readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000292 mAssociatedViewport =
293 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 if (!mAssociatedViewport) {
295 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
296 "but the corresponding viewport is not found.",
297 getName().c_str(), *mAssociatedDisplayPort);
298 enabled = false;
299 }
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 enabled = false;
308 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700309 }
310
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000311 if (changes.any()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 // For first-time configuration, only allow device to be disabled after mappers have
313 // finished configuring. This is because we need to read some of the properties from
314 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700315 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 }
317 }
318
Arpit Singhed6c3de2023-04-05 19:24:37 +0000319 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
320 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800321 mSources |= mapper.getSources();
322 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323
324 // If a device is just plugged but it might be disabled, we need to update some info like
325 // axis range of touch from each InputMapper first, then disable it.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000326 if (!changes.any()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000327 out += setEnabled(readerConfig.disabledDevices.find(mId) ==
328 readerConfig.disabledDevices.end(),
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700329 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 }
331 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700332 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333}
334
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700335std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
336 std::list<NotifyArgs> out;
337 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700338
339 mContext->updateGlobalMetaState();
340
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700341 out.push_back(notifyReset(when));
342 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343}
344
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700345std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 // Process all of the events in order for each mapper.
347 // We cannot simply ask each mapper to process them in bulk because mappers may
348 // have side-effects that must be interleaved. For example, joystick movement events and
349 // gamepad button presses are handled by different mappers but they should be dispatched
350 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700351 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700352 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000353 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000354 const auto [type, code, value] =
355 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
356 rawEvent->value);
357 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
358 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800359 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360
361 if (mDropUntilNextSync) {
362 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
363 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000364 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700365 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000366 ALOGD_IF(debugRawEvents(),
367 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700368 }
369 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
370 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
371 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700372 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700374 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
375 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800376 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700377 }
378 --count;
379 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700380 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700381}
382
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700383std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
384 std::list<NotifyArgs> out;
385 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
386 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700387}
388
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700389std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
390 std::list<NotifyArgs> out;
391 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
392 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700393}
394
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000395InputDeviceInfo InputDevice::getDeviceInfo() {
396 InputDeviceInfo outDeviceInfo;
397 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000398 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800400 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000401 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800402
403 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000404 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800405 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000406 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407}
408
409int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
410 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
411}
412
413int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
414 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
415}
416
417int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
418 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
419}
420
421int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
422 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800423 for (auto& deviceEntry : mDevices) {
424 auto& devicePair = deviceEntry.second;
425 auto& mappers = devicePair.second;
426 for (auto& mapperPtr : mappers) {
427 InputMapper& mapper = *mapperPtr;
428 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
429 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
430 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
431 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
432 if (currentResult >= AKEY_STATE_DOWN) {
433 return currentResult;
434 } else if (currentResult == AKEY_STATE_UP) {
435 result = currentResult;
436 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437 }
438 }
439 }
440 return result;
441}
442
Arpit Singh8e6fb252023-04-06 11:49:17 +0000443std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Arpit Singh7f4dd512023-05-18 13:22:12 +0000444 InputDeviceContext& context, const InputReaderConfiguration& readerConfig) {
445 ftl::Flags<InputDeviceClass> classes = context.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000446 std::vector<std::unique_ptr<InputMapper>> mappers;
447
448 // Switch-like devices.
449 if (classes.test(InputDeviceClass::SWITCH)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000450 mappers.push_back(createInputMapper<SwitchInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000451 }
452
453 // Scroll wheel-like devices.
454 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000455 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000456 }
457
458 // Vibrator-like devices.
459 if (classes.test(InputDeviceClass::VIBRATOR)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000460 mappers.push_back(createInputMapper<VibratorInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000461 }
462
463 // Battery-like devices or light-containing devices.
464 // PeripheralController will be created with associated EventHub device.
465 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000466 mController = std::make_unique<PeripheralController>(context);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000467 }
468
469 // Keyboard-like devices.
470 uint32_t keyboardSource = 0;
471 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
472 if (classes.test(InputDeviceClass::KEYBOARD)) {
473 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
474 }
475 if (classes.test(InputDeviceClass::ALPHAKEY)) {
476 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
477 }
478 if (classes.test(InputDeviceClass::DPAD)) {
479 keyboardSource |= AINPUT_SOURCE_DPAD;
480 }
481 if (classes.test(InputDeviceClass::GAMEPAD)) {
482 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
483 }
484
485 if (keyboardSource != 0) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000486 mappers.push_back(createInputMapper<KeyboardInputMapper>(context, readerConfig,
Arpit Singh033e3ec2023-04-26 14:43:16 +0000487 keyboardSource, keyboardType));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000488 }
489
490 // Cursor-like devices.
491 if (classes.test(InputDeviceClass::CURSOR)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000492 mappers.push_back(createInputMapper<CursorInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000493 }
494
495 // Touchscreens and touchpad devices.
496 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
497 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
498 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
499 // at least load this setting from the IDC file.
Arpit Singh7f4dd512023-05-18 13:22:12 +0000500 const InputDeviceIdentifier identifier = context.getDeviceIdentifier();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000501 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
502 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
503 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
504 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000505 mappers.push_back(createInputMapper<TouchpadInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000506 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Siarhei Vishniakou342e1dd2023-05-25 00:12:00 +0000507 mappers.push_back(std::make_unique<MultiTouchInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000508 } else if (classes.test(InputDeviceClass::TOUCH)) {
Siarhei Vishniakou342e1dd2023-05-25 00:12:00 +0000509 mappers.push_back(std::make_unique<SingleTouchInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000510 }
511
512 // Joystick-like devices.
513 if (classes.test(InputDeviceClass::JOYSTICK)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000514 mappers.push_back(createInputMapper<JoystickInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000515 }
516
517 // Motion sensor enabled devices.
518 if (classes.test(InputDeviceClass::SENSOR)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000519 mappers.push_back(createInputMapper<SensorInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000520 }
521
522 // External stylus-like devices.
523 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Arpit Singh7f4dd512023-05-18 13:22:12 +0000524 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(context, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000525 }
526 return mappers;
527}
528
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700529bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
530 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700531 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700532 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800533 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700534 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700535 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800536 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700537 return result;
538}
539
Philip Junker4af3b3d2021-12-14 10:36:55 +0100540int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
541 std::optional<int32_t> result = first_in_mappers<int32_t>(
542 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
543 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
544 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
545 }
546 return std::nullopt;
547 });
548 if (!result) {
549 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
550 "KEYBOARD found. The provided input device with id %d has sources %s.",
551 getId(), inputEventSourceToString(getSources()).c_str());
552 return AKEYCODE_UNKNOWN;
553 }
554 return *result;
555}
556
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700557std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
558 int32_t token) {
559 std::list<NotifyArgs> out;
560 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
561 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700562}
563
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700564std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
565 std::list<NotifyArgs> out;
566 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
567 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700568}
569
Chris Ye87143712020-11-10 05:05:58 +0000570bool InputDevice::isVibrating() {
571 bool vibrating = false;
572 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
573 return vibrating;
574}
575
576/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
577 * different vibration mappers then we could have duplicate IDs.
578 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
579 * we would definitely have duplicate IDs.
580 */
581std::vector<int32_t> InputDevice::getVibratorIds() {
582 std::vector<int32_t> vibrators;
583 for_each_mapper([&vibrators](InputMapper& mapper) {
584 std::vector<int32_t> devVibs = mapper.getVibratorIds();
585 vibrators.reserve(vibrators.size() + devVibs.size());
586 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
587 });
588 return vibrators;
589}
590
Chris Yef59a2f42020-10-16 12:55:26 -0700591bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
592 std::chrono::microseconds samplingPeriod,
593 std::chrono::microseconds maxBatchReportLatency) {
594 bool success = true;
595 for_each_mapper(
596 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
597 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
598 });
599 return success;
600}
601
602void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
603 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
604}
605
606void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
607 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
608}
609
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700610std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
611 std::list<NotifyArgs> out;
612 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
613 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700614}
615
Chris Ye3fdbfef2021-01-06 18:45:18 -0800616bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800617 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800618}
619
620bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800621 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800622}
623
624std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800625 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800626}
627
628std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800629 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800630}
631
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700632int32_t InputDevice::getMetaState() {
633 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700635 return result;
636}
637
638void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000639 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
640 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
641 mapper.updateMetaState(keyCode)) {
642 return std::make_optional(true);
643 }
644 return std::optional<bool>();
645 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700646}
647
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000648void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
649 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
650 context.addKeyRemapping(fromKeyCode, toKeyCode);
651 });
652}
653
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700654void InputDevice::bumpGeneration() {
655 mGeneration = mContext->bumpGeneration();
656}
657
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700658NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
659 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700660}
661
662std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
663 // Check if we had associated to the specific display.
664 if (mAssociatedViewport) {
665 return mAssociatedViewport->displayId;
666 }
667
668 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800669 return first_in_mappers<int32_t>(
670 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700671}
672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800673// returns the number of mappers associated with the device
674size_t InputDevice::getMapperCount() {
675 size_t count = 0;
676 for (auto& deviceEntry : mDevices) {
677 auto& devicePair = deviceEntry.second;
678 auto& mappers = devicePair.second;
679 count += mappers.size();
680 }
681 return count;
682}
683
arthurhungc903df12020-08-11 15:08:42 +0800684void InputDevice::updateLedState(bool reset) {
685 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
686}
687
Andy Chenf9f1a022022-08-29 20:07:10 -0400688std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
689 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
690}
691
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800692InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800693 : mDevice(device),
694 mContext(device.getContext()),
695 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800696 mId(eventHubId),
697 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800698
699InputDeviceContext::~InputDeviceContext() {}
700
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700701} // namespace android