blob: b5ee0445c200fed3618b662d9483821b83054353 [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));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800154 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800155
Arpit Singh8e6fb252023-04-06 11:49:17 +0000156 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
157}
158
159void InputDevice::addEventHubDevice(int32_t eventHubId,
160 const InputReaderConfiguration& readerConfig) {
161 if (mDevices.find(eventHubId) != mDevices.end()) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800162 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800163 }
Arpit Singh8e6fb252023-04-06 11:49:17 +0000164 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
165 std::vector<std::unique_ptr<InputMapper>> mappers = createMappers(*contextPtr, readerConfig);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800166
167 // insert the context into the devices set
168 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700169 // Must change generation to flag this device as changed
170 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800171}
172
173void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700174 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
175 // Delete mController, since the corresponding eventhub device is going away
176 mController = nullptr;
177 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800178 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700179}
180
Arpit Singhed6c3de2023-04-05 19:24:37 +0000181std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
182 const InputReaderConfiguration& readerConfig,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700183 uint32_t changes) {
184 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700185 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700186 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800187 mControllerNumber = 0;
188
189 for_each_subdevice([this](InputDeviceContext& context) {
190 mClasses |= context.getDeviceClasses();
191 int32_t controllerNumber = context.getDeviceControllerNumber();
192 if (controllerNumber > 0) {
193 if (mControllerNumber && mControllerNumber != controllerNumber) {
194 ALOGW("InputDevice::configure(): composite device contains multiple unique "
195 "controller numbers");
196 }
197 mControllerNumber = controllerNumber;
198 }
199 });
200
Chris Ye1b0c7342020-07-28 21:57:03 -0700201 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
202 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700203
204 if (!isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000205 // Full configuration should happen the first time configure is called
206 // and when the device type is changed. Changing a device type can
207 // affect various other parameters so should result in a
208 // reconfiguration.
209 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800210 mConfiguration.clear();
211 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000212 std::optional<PropertyMap> configuration =
213 getEventHub()->getConfiguration(context.getEventHubId());
214 if (configuration) {
215 mConfiguration.addAll(&(*configuration));
216 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800217 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000218
219 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000220 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 }
222
223 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700224 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700225 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700226 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227 bool shouldBumpGeneration = false;
228 for_each_subdevice(
229 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
230 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
231 shouldBumpGeneration = true;
232 }
233 });
234 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235 bumpGeneration();
236 }
237 }
238 }
239
240 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700241 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700242 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
243 if (mAlias != alias) {
244 mAlias = alias;
245 bumpGeneration();
246 }
247 }
248 }
249
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700250 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
251 // Do not execute this code on the first configure, because 'setEnabled' would call
252 // InputMapper::reset, and you can't reset a mapper before it has been configured.
253 // The mappers are configured for the first time at the bottom of this function.
Arpit Singhed6c3de2023-04-05 19:24:37 +0000254 auto it = readerConfig.disabledDevices.find(mId);
255 bool enabled = it == readerConfig.disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700256 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 }
258
259 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700260 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700261 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700262 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 mAssociatedViewport = std::nullopt;
264 // Find the display port that corresponds to the current input port.
265 const std::string& inputPort = mIdentifier.location;
266 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 const std::unordered_map<std::string, uint8_t>& ports =
268 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700269 const auto& displayPort = ports.find(inputPort);
270 if (displayPort != ports.end()) {
271 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800272 } else {
273 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000274 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800275 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
276 if (displayUniqueId != displayUniqueIds.end()) {
277 mAssociatedDisplayUniqueId = displayUniqueId->second;
278 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700279 }
280 }
281
282 // If the device was explicitly disabled by the user, it would be present in the
283 // "disabledDevices" list. If it is associated with a specific display, and it was not
284 // explicitly disabled, then enable/disable the device based on whether we can find the
285 // corresponding viewport.
Arpit Singhed6c3de2023-04-05 19:24:37 +0000286 bool enabled =
287 (readerConfig.disabledDevices.find(mId) == readerConfig.disabledDevices.end());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700288 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000289 mAssociatedViewport =
290 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291 if (!mAssociatedViewport) {
292 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
293 "but the corresponding viewport is not found.",
294 getName().c_str(), *mAssociatedDisplayPort);
295 enabled = false;
296 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700297 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
298 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000299 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700300 if (!mAssociatedViewport) {
301 ALOGW("Input device %s should be associated with display %s but the "
302 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800303 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700304 enabled = false;
305 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 }
307
308 if (changes) {
309 // For first-time configuration, only allow device to be disabled after mappers have
310 // finished configuring. This is because we need to read some of the properties from
311 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313 }
314 }
315
Arpit Singhed6c3de2023-04-05 19:24:37 +0000316 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
317 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800318 mSources |= mapper.getSources();
319 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700320
321 // If a device is just plugged but it might be disabled, we need to update some info like
322 // axis range of touch from each InputMapper first, then disable it.
323 if (!changes) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000324 out += setEnabled(readerConfig.disabledDevices.find(mId) ==
325 readerConfig.disabledDevices.end(),
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700326 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327 }
328 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700329 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330}
331
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700332std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
333 std::list<NotifyArgs> out;
334 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335
336 mContext->updateGlobalMetaState();
337
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700338 out.push_back(notifyReset(when));
339 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340}
341
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700342std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343 // Process all of the events in order for each mapper.
344 // We cannot simply ask each mapper to process them in bulk because mappers may
345 // have side-effects that must be interleaved. For example, joystick movement events and
346 // gamepad button presses are handled by different mappers but they should be dispatched
347 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700348 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000350 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000351 const auto [type, code, value] =
352 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
353 rawEvent->value);
354 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
355 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800356 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700357
358 if (mDropUntilNextSync) {
359 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
360 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000361 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700362 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000363 ALOGD_IF(debugRawEvents(),
364 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700365 }
366 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
367 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
368 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700369 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700370 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700371 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
372 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800373 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374 }
375 --count;
376 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700377 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700378}
379
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700380std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
381 std::list<NotifyArgs> out;
382 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
383 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384}
385
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700386std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
387 std::list<NotifyArgs> out;
388 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
389 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390}
391
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000392InputDeviceInfo InputDevice::getDeviceInfo() {
393 InputDeviceInfo outDeviceInfo;
394 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000395 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800397 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000398 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800399
400 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000401 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800402 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000403 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700404}
405
406int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
407 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
408}
409
410int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
411 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
412}
413
414int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
415 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
416}
417
418int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
419 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800420 for (auto& deviceEntry : mDevices) {
421 auto& devicePair = deviceEntry.second;
422 auto& mappers = devicePair.second;
423 for (auto& mapperPtr : mappers) {
424 InputMapper& mapper = *mapperPtr;
425 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
426 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
427 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
428 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
429 if (currentResult >= AKEY_STATE_DOWN) {
430 return currentResult;
431 } else if (currentResult == AKEY_STATE_UP) {
432 result = currentResult;
433 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
435 }
436 }
437 return result;
438}
439
Arpit Singh8e6fb252023-04-06 11:49:17 +0000440std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
441 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
442 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
443 std::vector<std::unique_ptr<InputMapper>> mappers;
444
445 // Switch-like devices.
446 if (classes.test(InputDeviceClass::SWITCH)) {
447 mappers.push_back(std::make_unique<SwitchInputMapper>(contextPtr, readerConfig));
448 }
449
450 // Scroll wheel-like devices.
451 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
452 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(contextPtr, readerConfig));
453 }
454
455 // Vibrator-like devices.
456 if (classes.test(InputDeviceClass::VIBRATOR)) {
457 mappers.push_back(std::make_unique<VibratorInputMapper>(contextPtr, readerConfig));
458 }
459
460 // Battery-like devices or light-containing devices.
461 // PeripheralController will be created with associated EventHub device.
462 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
463 mController = std::make_unique<PeripheralController>(contextPtr);
464 }
465
466 // Keyboard-like devices.
467 uint32_t keyboardSource = 0;
468 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
469 if (classes.test(InputDeviceClass::KEYBOARD)) {
470 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
471 }
472 if (classes.test(InputDeviceClass::ALPHAKEY)) {
473 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
474 }
475 if (classes.test(InputDeviceClass::DPAD)) {
476 keyboardSource |= AINPUT_SOURCE_DPAD;
477 }
478 if (classes.test(InputDeviceClass::GAMEPAD)) {
479 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
480 }
481
482 if (keyboardSource != 0) {
483 mappers.push_back(std::make_unique<KeyboardInputMapper>(contextPtr, readerConfig,
484 keyboardSource, keyboardType));
485 }
486
487 // Cursor-like devices.
488 if (classes.test(InputDeviceClass::CURSOR)) {
489 mappers.push_back(std::make_unique<CursorInputMapper>(contextPtr, readerConfig));
490 }
491
492 // Touchscreens and touchpad devices.
493 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
494 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
495 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
496 // at least load this setting from the IDC file.
497 const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
498 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
499 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
500 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
501 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
502 mappers.push_back(std::make_unique<TouchpadInputMapper>(contextPtr, readerConfig));
503 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
504 mappers.push_back(std::make_unique<MultiTouchInputMapper>(contextPtr, readerConfig));
505 } else if (classes.test(InputDeviceClass::TOUCH)) {
506 mappers.push_back(std::make_unique<SingleTouchInputMapper>(contextPtr, readerConfig));
507 }
508
509 // Joystick-like devices.
510 if (classes.test(InputDeviceClass::JOYSTICK)) {
511 mappers.push_back(std::make_unique<JoystickInputMapper>(contextPtr, readerConfig));
512 }
513
514 // Motion sensor enabled devices.
515 if (classes.test(InputDeviceClass::SENSOR)) {
516 mappers.push_back(std::make_unique<SensorInputMapper>(contextPtr, readerConfig));
517 }
518
519 // External stylus-like devices.
520 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
521 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(contextPtr, readerConfig));
522 }
523 return mappers;
524}
525
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700526bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
527 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700528 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700529 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800530 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700531 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700532 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800533 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700534 return result;
535}
536
Philip Junker4af3b3d2021-12-14 10:36:55 +0100537int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
538 std::optional<int32_t> result = first_in_mappers<int32_t>(
539 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
540 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
541 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
542 }
543 return std::nullopt;
544 });
545 if (!result) {
546 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
547 "KEYBOARD found. The provided input device with id %d has sources %s.",
548 getId(), inputEventSourceToString(getSources()).c_str());
549 return AKEYCODE_UNKNOWN;
550 }
551 return *result;
552}
553
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700554std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
555 int32_t token) {
556 std::list<NotifyArgs> out;
557 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
558 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559}
560
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700561std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
562 std::list<NotifyArgs> out;
563 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
564 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700565}
566
Chris Ye87143712020-11-10 05:05:58 +0000567bool InputDevice::isVibrating() {
568 bool vibrating = false;
569 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
570 return vibrating;
571}
572
573/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
574 * different vibration mappers then we could have duplicate IDs.
575 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
576 * we would definitely have duplicate IDs.
577 */
578std::vector<int32_t> InputDevice::getVibratorIds() {
579 std::vector<int32_t> vibrators;
580 for_each_mapper([&vibrators](InputMapper& mapper) {
581 std::vector<int32_t> devVibs = mapper.getVibratorIds();
582 vibrators.reserve(vibrators.size() + devVibs.size());
583 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
584 });
585 return vibrators;
586}
587
Chris Yef59a2f42020-10-16 12:55:26 -0700588bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
589 std::chrono::microseconds samplingPeriod,
590 std::chrono::microseconds maxBatchReportLatency) {
591 bool success = true;
592 for_each_mapper(
593 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
594 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
595 });
596 return success;
597}
598
599void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
600 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
601}
602
603void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
604 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
605}
606
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700607std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
608 std::list<NotifyArgs> out;
609 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
610 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700611}
612
Chris Ye3fdbfef2021-01-06 18:45:18 -0800613bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800614 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800615}
616
617bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800618 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800619}
620
621std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800622 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800623}
624
625std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800626 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800627}
628
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700629int32_t InputDevice::getMetaState() {
630 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800631 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700632 return result;
633}
634
635void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000636 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
637 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
638 mapper.updateMetaState(keyCode)) {
639 return std::make_optional(true);
640 }
641 return std::optional<bool>();
642 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700643}
644
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000645void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
646 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
647 context.addKeyRemapping(fromKeyCode, toKeyCode);
648 });
649}
650
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700651void InputDevice::bumpGeneration() {
652 mGeneration = mContext->bumpGeneration();
653}
654
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700655NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
656 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700657}
658
659std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
660 // Check if we had associated to the specific display.
661 if (mAssociatedViewport) {
662 return mAssociatedViewport->displayId;
663 }
664
665 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800666 return first_in_mappers<int32_t>(
667 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700668}
669
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800670// returns the number of mappers associated with the device
671size_t InputDevice::getMapperCount() {
672 size_t count = 0;
673 for (auto& deviceEntry : mDevices) {
674 auto& devicePair = deviceEntry.second;
675 auto& mappers = devicePair.second;
676 count += mappers.size();
677 }
678 return count;
679}
680
arthurhungc903df12020-08-11 15:08:42 +0800681void InputDevice::updateLedState(bool reset) {
682 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
683}
684
Andy Chenf9f1a022022-08-29 20:07:10 -0400685std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
686 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
687}
688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800689InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800690 : mDevice(device),
691 mContext(device.getContext()),
692 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800693 mId(eventHubId),
694 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800695
696InputDeviceContext::~InputDeviceContext() {}
697
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700698} // namespace android