blob: bd5a9644c0240f2873e2b6368f19570411358efd [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 Singh4f61f3c2023-06-13 15:05:53 +0000174[[nodiscard]] std::list<NotifyArgs> InputDevice::addEventHubDevice(
175 nsecs_t when, int32_t eventHubId, const InputReaderConfiguration& readerConfig) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000176 if (mDevices.find(eventHubId) != mDevices.end()) {
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000177 return {};
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000178 }
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000179
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000180 // Add an empty device configure and keep it enabled to allow mapper population
181 // with correct configuration/context
182 addEmptyEventHubDevice(eventHubId);
183 std::list<NotifyArgs> out = configure(when, readerConfig, {}, /*forceEnable=*/true);
184
185 DevicePair& devicePair = mDevices[eventHubId];
186 devicePair.second = createMappers(*devicePair.first, readerConfig);
187
Chris Yee7310032020-09-22 15:36:28 -0700188 // Must change generation to flag this device as changed
189 bumpGeneration();
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000190 return out;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800191}
192
193void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700194 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
195 // Delete mController, since the corresponding eventhub device is going away
196 mController = nullptr;
197 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800198 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700199}
200
Arpit Singhed6c3de2023-04-05 19:24:37 +0000201std::list<NotifyArgs> InputDevice::configure(nsecs_t when,
202 const InputReaderConfiguration& readerConfig,
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000203 ConfigurationChanges changes, bool forceEnable) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700204 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700205 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700206 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800207 mControllerNumber = 0;
208
209 for_each_subdevice([this](InputDeviceContext& context) {
210 mClasses |= context.getDeviceClasses();
211 int32_t controllerNumber = context.getDeviceControllerNumber();
212 if (controllerNumber > 0) {
213 if (mControllerNumber && mControllerNumber != controllerNumber) {
214 ALOGW("InputDevice::configure(): composite device contains multiple unique "
215 "controller numbers");
216 }
217 mControllerNumber = controllerNumber;
218 }
219 });
220
Chris Ye1b0c7342020-07-28 21:57:03 -0700221 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
222 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700223
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000224 using Change = InputReaderConfiguration::Change;
225
Arpit Singh56adebc2023-04-25 13:56:05 +0000226 if (!changes.any() || !isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000227 // Full configuration should happen the first time configure is called
228 // and when the device type is changed. Changing a device type can
229 // affect various other parameters so should result in a
230 // reconfiguration.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000231 if (!changes.any() || changes.test(Change::DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800232 mConfiguration.clear();
233 for_each_subdevice([this](InputDeviceContext& context) {
Harry Cuttsc34f7582023-03-07 16:23:30 +0000234 std::optional<PropertyMap> configuration =
235 getEventHub()->getConfiguration(context.getEventHubId());
236 if (configuration) {
237 mConfiguration.addAll(&(*configuration));
238 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800239 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000240
241 mAssociatedDeviceType =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000242 getValueByKey(readerConfig.deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 }
244
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000245 if (!changes.any() || changes.test(Change::KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700246 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700247 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700248 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800249 bool shouldBumpGeneration = false;
250 for_each_subdevice(
251 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
252 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
253 shouldBumpGeneration = true;
254 }
255 });
256 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 bumpGeneration();
258 }
259 }
260 }
261
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000262 if (!changes.any() || changes.test(Change::DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700263 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
265 if (mAlias != alias) {
266 mAlias = alias;
267 bumpGeneration();
268 }
269 }
270 }
271
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000272 if (!changes.any() || changes.test(Change::DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700273 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700274 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700275 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700276 mAssociatedViewport = std::nullopt;
277 // Find the display port that corresponds to the current input port.
278 const std::string& inputPort = mIdentifier.location;
279 if (!inputPort.empty()) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000280 const std::unordered_map<std::string, uint8_t>& ports =
281 readerConfig.portAssociations;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700282 const auto& displayPort = ports.find(inputPort);
283 if (displayPort != ports.end()) {
284 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800285 } else {
286 const std::unordered_map<std::string, std::string>& displayUniqueIds =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000287 readerConfig.uniqueIdAssociations;
Christine Franks2a2293c2022-01-18 11:51:16 -0800288 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
289 if (displayUniqueId != displayUniqueIds.end()) {
290 mAssociatedDisplayUniqueId = displayUniqueId->second;
291 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 }
293 }
294
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000295 // If it is associated with a specific display, then find the corresponding viewport
296 // which will be used to enable/disable the device.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700297 if (mAssociatedDisplayPort) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mAssociatedViewport =
299 readerConfig.getDisplayViewportByPort(*mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300 if (!mAssociatedViewport) {
301 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
302 "but the corresponding viewport is not found.",
303 getName().c_str(), *mAssociatedDisplayPort);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700304 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700305 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
306 mAssociatedViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +0000307 readerConfig.getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
Christine Franks1ba71cc2021-04-07 14:37:42 -0700308 if (!mAssociatedViewport) {
309 ALOGW("Input device %s should be associated with display %s but the "
310 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800311 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700312 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313 }
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000314 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700315
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000316 if (!changes.any() || changes.test(Change::ENABLED_STATE) ||
317 changes.test(Change::DISPLAY_INFO)) {
Arpit Singhf4cdbef2023-05-30 14:12:49 +0000318 // Whether a device is enabled can depend on the display association,
319 // so update the enabled state when there is a change in display info.
Arpit Singh4f61f3c2023-06-13 15:05:53 +0000320 out += updateEnableState(when, readerConfig, forceEnable);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 }
322
Arpit Singhed6c3de2023-04-05 19:24:37 +0000323 for_each_mapper([this, when, &readerConfig, changes, &out](InputMapper& mapper) {
324 out += mapper.reconfigure(when, readerConfig, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800325 mSources |= mapper.getSources();
326 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700328 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329}
330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700331std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
332 std::list<NotifyArgs> out;
333 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700334
335 mContext->updateGlobalMetaState();
336
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700337 out.push_back(notifyReset(when));
338 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339}
340
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700341std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700342 // Process all of the events in order for each mapper.
343 // We cannot simply ask each mapper to process them in bulk because mappers may
344 // have side-effects that must be interleaved. For example, joystick movement events and
345 // gamepad button presses are handled by different mappers but they should be dispatched
346 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700347 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700348 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000349 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000350 const auto [type, code, value] =
351 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
352 rawEvent->value);
353 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
354 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800355 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700356
357 if (mDropUntilNextSync) {
358 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
359 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000360 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700361 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000362 ALOGD_IF(debugRawEvents(),
363 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364 }
365 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
366 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
367 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700368 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700369 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700370 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
371 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800372 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373 }
374 --count;
375 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700376 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700377}
378
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700379std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
380 std::list<NotifyArgs> out;
381 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
382 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700383}
384
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700385std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
386 std::list<NotifyArgs> out;
387 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
388 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389}
390
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000391InputDeviceInfo InputDevice::getDeviceInfo() {
392 InputDeviceInfo outDeviceInfo;
393 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000394 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800396 for_each_mapper(
Harry Cuttsd02ea102023-03-17 18:21:30 +0000397 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800398
399 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000400 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800401 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000402 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403}
404
405int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
406 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
407}
408
409int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
410 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
411}
412
413int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
414 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
415}
416
417int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
418 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800419 for (auto& deviceEntry : mDevices) {
420 auto& devicePair = deviceEntry.second;
421 auto& mappers = devicePair.second;
422 for (auto& mapperPtr : mappers) {
423 InputMapper& mapper = *mapperPtr;
424 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
425 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
426 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
427 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
428 if (currentResult >= AKEY_STATE_DOWN) {
429 return currentResult;
430 } else if (currentResult == AKEY_STATE_UP) {
431 result = currentResult;
432 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 }
434 }
435 }
436 return result;
437}
438
Arpit Singh8e6fb252023-04-06 11:49:17 +0000439std::vector<std::unique_ptr<InputMapper>> InputDevice::createMappers(
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000440 InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig) {
441 ftl::Flags<InputDeviceClass> classes = contextPtr.getDeviceClasses();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000442 std::vector<std::unique_ptr<InputMapper>> mappers;
443
444 // Switch-like devices.
445 if (classes.test(InputDeviceClass::SWITCH)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000446 mappers.push_back(createInputMapper<SwitchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000447 }
448
449 // Scroll wheel-like devices.
450 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000451 mappers.push_back(createInputMapper<RotaryEncoderInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000452 }
453
454 // Vibrator-like devices.
455 if (classes.test(InputDeviceClass::VIBRATOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000456 mappers.push_back(createInputMapper<VibratorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000457 }
458
459 // Battery-like devices or light-containing devices.
460 // PeripheralController will be created with associated EventHub device.
461 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000462 mController = std::make_unique<PeripheralController>(contextPtr);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000463 }
464
465 // Keyboard-like devices.
466 uint32_t keyboardSource = 0;
467 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
468 if (classes.test(InputDeviceClass::KEYBOARD)) {
469 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
470 }
471 if (classes.test(InputDeviceClass::ALPHAKEY)) {
472 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
473 }
474 if (classes.test(InputDeviceClass::DPAD)) {
475 keyboardSource |= AINPUT_SOURCE_DPAD;
476 }
477 if (classes.test(InputDeviceClass::GAMEPAD)) {
478 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
479 }
480
481 if (keyboardSource != 0) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000482 mappers.push_back(createInputMapper<KeyboardInputMapper>(contextPtr, readerConfig,
Arpit Singh033e3ec2023-04-26 14:43:16 +0000483 keyboardSource, keyboardType));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000484 }
485
486 // Cursor-like devices.
487 if (classes.test(InputDeviceClass::CURSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000488 mappers.push_back(createInputMapper<CursorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000489 }
490
491 // Touchscreens and touchpad devices.
492 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
493 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
494 // TODO(b/272518665): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) touchpads, or
495 // at least load this setting from the IDC file.
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000496 const InputDeviceIdentifier identifier = contextPtr.getDeviceIdentifier();
Arpit Singh8e6fb252023-04-06 11:49:17 +0000497 const bool isSonyDualShock4Touchpad = identifier.vendor == 0x054c &&
498 (identifier.product == 0x05c4 || identifier.product == 0x09cc);
499 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
500 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyDualShock4Touchpad) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000501 mappers.push_back(createInputMapper<TouchpadInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000502 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Arpit Singh285bdc52023-05-18 16:56:41 +0000503 mappers.push_back(createInputMapper<MultiTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000504 } else if (classes.test(InputDeviceClass::TOUCH)) {
Arpit Singh285bdc52023-05-18 16:56:41 +0000505 mappers.push_back(createInputMapper<SingleTouchInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000506 }
507
508 // Joystick-like devices.
509 if (classes.test(InputDeviceClass::JOYSTICK)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000510 mappers.push_back(createInputMapper<JoystickInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000511 }
512
513 // Motion sensor enabled devices.
514 if (classes.test(InputDeviceClass::SENSOR)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000515 mappers.push_back(createInputMapper<SensorInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000516 }
517
518 // External stylus-like devices.
519 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +0000520 mappers.push_back(createInputMapper<ExternalStylusInputMapper>(contextPtr, readerConfig));
Arpit Singh8e6fb252023-04-06 11:49:17 +0000521 }
522 return mappers;
523}
524
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700525bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
526 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700527 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700528 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800529 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700530 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700531 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800532 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700533 return result;
534}
535
Philip Junker4af3b3d2021-12-14 10:36:55 +0100536int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
537 std::optional<int32_t> result = first_in_mappers<int32_t>(
538 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
539 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
540 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
541 }
542 return std::nullopt;
543 });
544 if (!result) {
545 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
546 "KEYBOARD found. The provided input device with id %d has sources %s.",
547 getId(), inputEventSourceToString(getSources()).c_str());
548 return AKEYCODE_UNKNOWN;
549 }
550 return *result;
551}
552
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700553std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
554 int32_t token) {
555 std::list<NotifyArgs> out;
556 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
557 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700558}
559
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700560std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
561 std::list<NotifyArgs> out;
562 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
563 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564}
565
Chris Ye87143712020-11-10 05:05:58 +0000566bool InputDevice::isVibrating() {
567 bool vibrating = false;
568 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
569 return vibrating;
570}
571
572/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
573 * different vibration mappers then we could have duplicate IDs.
574 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
575 * we would definitely have duplicate IDs.
576 */
577std::vector<int32_t> InputDevice::getVibratorIds() {
578 std::vector<int32_t> vibrators;
579 for_each_mapper([&vibrators](InputMapper& mapper) {
580 std::vector<int32_t> devVibs = mapper.getVibratorIds();
581 vibrators.reserve(vibrators.size() + devVibs.size());
582 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
583 });
584 return vibrators;
585}
586
Chris Yef59a2f42020-10-16 12:55:26 -0700587bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
588 std::chrono::microseconds samplingPeriod,
589 std::chrono::microseconds maxBatchReportLatency) {
590 bool success = true;
591 for_each_mapper(
592 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
593 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
594 });
595 return success;
596}
597
598void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
599 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
600}
601
602void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
603 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
604}
605
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700606std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
607 std::list<NotifyArgs> out;
608 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
609 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700610}
611
Chris Ye3fdbfef2021-01-06 18:45:18 -0800612bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800613 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800614}
615
616bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800617 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800618}
619
620std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800621 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800622}
623
624std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800625 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800626}
627
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700628int32_t InputDevice::getMetaState() {
629 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800630 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631 return result;
632}
633
634void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000635 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
636 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
637 mapper.updateMetaState(keyCode)) {
638 return std::make_optional(true);
639 }
640 return std::optional<bool>();
641 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700642}
643
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000644void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
645 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
646 context.addKeyRemapping(fromKeyCode, toKeyCode);
647 });
648}
649
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700650void InputDevice::bumpGeneration() {
651 mGeneration = mContext->bumpGeneration();
652}
653
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700654NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
655 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700656}
657
658std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
659 // Check if we had associated to the specific display.
660 if (mAssociatedViewport) {
661 return mAssociatedViewport->displayId;
662 }
663
664 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800665 return first_in_mappers<int32_t>(
666 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700667}
668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800669// returns the number of mappers associated with the device
670size_t InputDevice::getMapperCount() {
671 size_t count = 0;
672 for (auto& deviceEntry : mDevices) {
673 auto& devicePair = deviceEntry.second;
674 auto& mappers = devicePair.second;
675 count += mappers.size();
676 }
677 return count;
678}
679
arthurhungc903df12020-08-11 15:08:42 +0800680void InputDevice::updateLedState(bool reset) {
681 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
682}
683
Andy Chenf9f1a022022-08-29 20:07:10 -0400684std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
685 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
686}
687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800688InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800689 : mDevice(device),
690 mContext(device.getContext()),
691 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800692 mId(eventHubId),
693 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800694
695InputDeviceContext::~InputDeviceContext() {}
696
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700697} // namespace android