blob: 6ca629835df385a5867579e3192aab6d8291cb37 [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
Dominik Laskowski2f01d772022-03-23 16:01:29 -070023#include <ftl/flags.h>
24
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080025#include "CursorInputMapper.h"
26#include "ExternalStylusInputMapper.h"
27#include "InputReaderContext.h"
28#include "JoystickInputMapper.h"
29#include "KeyboardInputMapper.h"
30#include "MultiTouchInputMapper.h"
Chris Ye1dd2e5c2021-04-04 23:12:41 -070031#include "PeripheralController.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080032#include "RotaryEncoderInputMapper.h"
Chris Yef59a2f42020-10-16 12:55:26 -070033#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080034#include "SingleTouchInputMapper.h"
35#include "SwitchInputMapper.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000036#include "TouchpadInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080037#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000039using android::hardware::input::InputDeviceCountryCode;
40
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041namespace android {
42
43InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080044 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045 : mContext(context),
46 mId(id),
47 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080048 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070049 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080050 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051 mSources(0),
52 mIsExternal(false),
53 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080054 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080056InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057
58bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080059 if (!hasEventHubDevices()) {
60 return false;
61 }
Chris Yee14523a2020-12-19 13:46:00 -080062 // An input device composed of sub devices can be individually enabled or disabled.
63 // If any of the sub device is enabled then the input device is considered as enabled.
64 bool enabled = false;
65 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
66 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067}
68
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070069std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
70 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
72 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
73 "but the corresponding viewport is not found",
74 getName().c_str(), *mAssociatedDisplayPort);
75 enabled = false;
76 }
77
78 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070079 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 }
81
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080082 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
83 // performed. The querying must happen when the device is enabled, so we reset after enabling
84 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080086 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070087 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070088 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070089 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080090 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 }
92 // Must change generation to flag this device as changed
93 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070094 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095}
96
Chris Yee7310032020-09-22 15:36:28 -070097void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000098 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099
100 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
101 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700102 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
104 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
105 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
106 if (mAssociatedDisplayPort) {
107 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
108 } else {
109 dump += "<none>\n";
110 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700111 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
112 if (mAssociatedDisplayUniqueId) {
113 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
114 } else {
115 dump += "<none>\n";
116 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000118 dump += StringPrintf(INDENT2 "Sources: %s\n",
119 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700121 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122
123 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
124 if (!ranges.empty()) {
125 dump += INDENT2 "Motion Ranges:\n";
126 for (size_t i = 0; i < ranges.size(); i++) {
127 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700128 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700129 char name[32];
130 if (label) {
131 strncpy(name, label, sizeof(name));
132 name[sizeof(name) - 1] = '\0';
133 } else {
134 snprintf(name, sizeof(name), "%d", range.axis);
135 }
136 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000137 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000139 name, inputEventSourceToString(range.source).c_str(), range.min,
140 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 }
142 }
143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800144 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800145 if (mController) {
146 mController->dump(dump);
147 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148}
149
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800150void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
151 if (mDevices.find(eventHubId) != mDevices.end()) {
152 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800153 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800154 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700155 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800156 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800158 // Check if we should skip population
159 if (!populateMappers) {
160 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
161 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800162 }
163
164 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700165 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800166 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800167 }
168
169 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700170 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800171 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800172 }
173
174 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700175 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800176 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800177 }
178
Chris Yee2b1e5c2021-03-10 22:45:12 -0800179 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700180 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800181 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700182 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800183 }
184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800185 // Keyboard-like devices.
186 uint32_t keyboardSource = 0;
187 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700188 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800189 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
190 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700191 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
193 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700194 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800195 keyboardSource |= AINPUT_SOURCE_DPAD;
196 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700197 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800198 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
199 }
200
201 if (keyboardSource != 0) {
202 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800203 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800204 }
205
206 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700207 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800209 }
210
211 // Touchscreens and touchpad devices.
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000212 // TODO(b/251196347): replace this with a proper flag.
213 constexpr bool ENABLE_NEW_TOUCHPAD_STACK = false;
214 if (ENABLE_NEW_TOUCHPAD_STACK && classes.test(InputDeviceClass::TOUCHPAD)) {
215 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
216 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800217 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700218 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800220 }
221
222 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700223 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800224 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800225 }
226
Chris Yef59a2f42020-10-16 12:55:26 -0700227 // Motion sensor enabled devices.
228 if (classes.test(InputDeviceClass::SENSOR)) {
229 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
230 }
231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800232 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700233 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800234 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800235 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800236
237 // insert the context into the devices set
238 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700239 // Must change generation to flag this device as changed
240 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800241}
242
243void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700244 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
245 // Delete mController, since the corresponding eventhub device is going away
246 mController = nullptr;
247 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800248 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249}
250
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700251std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
252 uint32_t changes) {
253 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700255 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800256 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000257 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800258
259 for_each_subdevice([this](InputDeviceContext& context) {
260 mClasses |= context.getDeviceClasses();
261 int32_t controllerNumber = context.getDeviceControllerNumber();
262 if (controllerNumber > 0) {
263 if (mControllerNumber && mControllerNumber != controllerNumber) {
264 ALOGW("InputDevice::configure(): composite device contains multiple unique "
265 "controller numbers");
266 }
267 mControllerNumber = controllerNumber;
268 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000269
270 InputDeviceCountryCode countryCode = context.getCountryCode();
271 if (countryCode != InputDeviceCountryCode::INVALID) {
272 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
273 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
274 "codes",
275 getName().c_str());
276 }
277 mCountryCode = countryCode;
278 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800279 });
280
Chris Ye1b0c7342020-07-28 21:57:03 -0700281 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
282 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700283
284 if (!isIgnored()) {
285 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800286 mConfiguration.clear();
287 for_each_subdevice([this](InputDeviceContext& context) {
288 PropertyMap configuration;
289 context.getConfiguration(&configuration);
290 mConfiguration.addAll(&configuration);
291 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 }
293
294 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700295 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700296 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700297 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 bool shouldBumpGeneration = false;
299 for_each_subdevice(
300 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
301 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
302 shouldBumpGeneration = true;
303 }
304 });
305 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 bumpGeneration();
307 }
308 }
309 }
310
311 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700312 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700313 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
314 if (mAlias != alias) {
315 mAlias = alias;
316 bumpGeneration();
317 }
318 }
319 }
320
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700321 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
322 // Do not execute this code on the first configure, because 'setEnabled' would call
323 // InputMapper::reset, and you can't reset a mapper before it has been configured.
324 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700325 auto it = config->disabledDevices.find(mId);
326 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700327 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700328 }
329
330 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700331 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700332 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700334 mAssociatedViewport = std::nullopt;
335 // Find the display port that corresponds to the current input port.
336 const std::string& inputPort = mIdentifier.location;
337 if (!inputPort.empty()) {
338 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
339 const auto& displayPort = ports.find(inputPort);
340 if (displayPort != ports.end()) {
341 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800342 } else {
343 const std::unordered_map<std::string, std::string>& displayUniqueIds =
344 config->uniqueIdAssociations;
345 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
346 if (displayUniqueId != displayUniqueIds.end()) {
347 mAssociatedDisplayUniqueId = displayUniqueId->second;
348 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349 }
350 }
351
352 // If the device was explicitly disabled by the user, it would be present in the
353 // "disabledDevices" list. If it is associated with a specific display, and it was not
354 // explicitly disabled, then enable/disable the device based on whether we can find the
355 // corresponding viewport.
356 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
357 if (mAssociatedDisplayPort) {
358 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
359 if (!mAssociatedViewport) {
360 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
361 "but the corresponding viewport is not found.",
362 getName().c_str(), *mAssociatedDisplayPort);
363 enabled = false;
364 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700365 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
366 mAssociatedViewport =
367 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
368 if (!mAssociatedViewport) {
369 ALOGW("Input device %s should be associated with display %s but the "
370 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800371 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700372 enabled = false;
373 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374 }
375
376 if (changes) {
377 // For first-time configuration, only allow device to be disabled after mappers have
378 // finished configuring. This is because we need to read some of the properties from
379 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700380 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700381 }
382 }
383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700384 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
385 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800386 mSources |= mapper.getSources();
387 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388
389 // If a device is just plugged but it might be disabled, we need to update some info like
390 // axis range of touch from each InputMapper first, then disable it.
391 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700392 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
393 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394 }
395 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700396 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397}
398
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700399std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
400 std::list<NotifyArgs> out;
401 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402
403 mContext->updateGlobalMetaState();
404
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700405 out.push_back(notifyReset(when));
406 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407}
408
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700409std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700410 // Process all of the events in order for each mapper.
411 // We cannot simply ask each mapper to process them in bulk because mappers may
412 // have side-effects that must be interleaved. For example, joystick movement events and
413 // gamepad button presses are handled by different mappers but they should be dispatched
414 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700415 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700416 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800417 if (DEBUG_RAW_EVENTS) {
418 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
419 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
420 rawEvent->when);
421 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422
423 if (mDropUntilNextSync) {
424 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
425 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800426 if (DEBUG_RAW_EVENTS) {
427 ALOGD("Recovered from input event buffer overrun.");
428 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800430 if (DEBUG_RAW_EVENTS) {
431 ALOGD("Dropped input event while waiting for next input sync.");
432 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 }
434 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
435 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
436 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700437 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700439 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
440 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800441 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442 }
443 --count;
444 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700445 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446}
447
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700448std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
449 std::list<NotifyArgs> out;
450 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
451 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700452}
453
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700454std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
455 std::list<NotifyArgs> out;
456 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
457 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700458}
459
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000460InputDeviceInfo InputDevice::getDeviceInfo() {
461 InputDeviceInfo outDeviceInfo;
462 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000463 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800464 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000465 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800466
467 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000468 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800469 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000470 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471}
472
473int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
474 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
475}
476
477int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
478 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
479}
480
481int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
482 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
483}
484
485int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
486 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800487 for (auto& deviceEntry : mDevices) {
488 auto& devicePair = deviceEntry.second;
489 auto& mappers = devicePair.second;
490 for (auto& mapperPtr : mappers) {
491 InputMapper& mapper = *mapperPtr;
492 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
493 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
494 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
495 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
496 if (currentResult >= AKEY_STATE_DOWN) {
497 return currentResult;
498 } else if (currentResult == AKEY_STATE_UP) {
499 result = currentResult;
500 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501 }
502 }
503 }
504 return result;
505}
506
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700507bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
508 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700509 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700510 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800511 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700512 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700513 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800514 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700515 return result;
516}
517
Philip Junker4af3b3d2021-12-14 10:36:55 +0100518int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
519 std::optional<int32_t> result = first_in_mappers<int32_t>(
520 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
521 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
522 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
523 }
524 return std::nullopt;
525 });
526 if (!result) {
527 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
528 "KEYBOARD found. The provided input device with id %d has sources %s.",
529 getId(), inputEventSourceToString(getSources()).c_str());
530 return AKEYCODE_UNKNOWN;
531 }
532 return *result;
533}
534
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700535std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
536 int32_t token) {
537 std::list<NotifyArgs> out;
538 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
539 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700540}
541
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700542std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
543 std::list<NotifyArgs> out;
544 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
545 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700546}
547
Chris Ye87143712020-11-10 05:05:58 +0000548bool InputDevice::isVibrating() {
549 bool vibrating = false;
550 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
551 return vibrating;
552}
553
554/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
555 * different vibration mappers then we could have duplicate IDs.
556 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
557 * we would definitely have duplicate IDs.
558 */
559std::vector<int32_t> InputDevice::getVibratorIds() {
560 std::vector<int32_t> vibrators;
561 for_each_mapper([&vibrators](InputMapper& mapper) {
562 std::vector<int32_t> devVibs = mapper.getVibratorIds();
563 vibrators.reserve(vibrators.size() + devVibs.size());
564 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
565 });
566 return vibrators;
567}
568
Chris Yef59a2f42020-10-16 12:55:26 -0700569bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
570 std::chrono::microseconds samplingPeriod,
571 std::chrono::microseconds maxBatchReportLatency) {
572 bool success = true;
573 for_each_mapper(
574 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
575 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
576 });
577 return success;
578}
579
580void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
581 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
582}
583
584void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
585 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
586}
587
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700588std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
589 std::list<NotifyArgs> out;
590 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
591 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700592}
593
Chris Ye3fdbfef2021-01-06 18:45:18 -0800594bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800595 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800596}
597
598bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800599 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800600}
601
602std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800603 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800604}
605
606std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800607 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800608}
609
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700610int32_t InputDevice::getMetaState() {
611 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800612 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700613 return result;
614}
615
616void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000617 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
618 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
619 mapper.updateMetaState(keyCode)) {
620 return std::make_optional(true);
621 }
622 return std::optional<bool>();
623 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700624}
625
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700626void InputDevice::bumpGeneration() {
627 mGeneration = mContext->bumpGeneration();
628}
629
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700630NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
631 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700632}
633
634std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
635 // Check if we had associated to the specific display.
636 if (mAssociatedViewport) {
637 return mAssociatedViewport->displayId;
638 }
639
640 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800641 return first_in_mappers<int32_t>(
642 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700643}
644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800645// returns the number of mappers associated with the device
646size_t InputDevice::getMapperCount() {
647 size_t count = 0;
648 for (auto& deviceEntry : mDevices) {
649 auto& devicePair = deviceEntry.second;
650 auto& mappers = devicePair.second;
651 count += mappers.size();
652 }
653 return count;
654}
655
arthurhungc903df12020-08-11 15:08:42 +0800656void InputDevice::updateLedState(bool reset) {
657 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
658}
659
Andy Chenf9f1a022022-08-29 20:07:10 -0400660std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
661 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
662}
663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800664InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800665 : mDevice(device),
666 mContext(device.getContext()),
667 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800668 mId(eventHubId),
669 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800670
671InputDeviceContext::~InputDeviceContext() {}
672
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700673} // namespace android