blob: e9a94b3a422dfc720b68069c968a4d910469d260 [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;
Harry Cutts1f48a442022-11-15 17:38:36 +0000214 if (ENABLE_NEW_TOUCHPAD_STACK && classes.test(InputDeviceClass::TOUCHPAD) &&
215 classes.test(InputDeviceClass::TOUCH_MT)) {
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000216 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
217 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800218 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700219 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800220 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800221 }
222
223 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700224 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800225 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800226 }
227
Chris Yef59a2f42020-10-16 12:55:26 -0700228 // Motion sensor enabled devices.
229 if (classes.test(InputDeviceClass::SENSOR)) {
230 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
231 }
232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800233 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700234 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800235 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800236 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800237
238 // insert the context into the devices set
239 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700240 // Must change generation to flag this device as changed
241 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800242}
243
244void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700245 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
246 // Delete mController, since the corresponding eventhub device is going away
247 mController = nullptr;
248 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800249 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700250}
251
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700252std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
253 uint32_t changes) {
254 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700256 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800257 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000258 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800259
260 for_each_subdevice([this](InputDeviceContext& context) {
261 mClasses |= context.getDeviceClasses();
262 int32_t controllerNumber = context.getDeviceControllerNumber();
263 if (controllerNumber > 0) {
264 if (mControllerNumber && mControllerNumber != controllerNumber) {
265 ALOGW("InputDevice::configure(): composite device contains multiple unique "
266 "controller numbers");
267 }
268 mControllerNumber = controllerNumber;
269 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000270
271 InputDeviceCountryCode countryCode = context.getCountryCode();
272 if (countryCode != InputDeviceCountryCode::INVALID) {
273 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
274 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
275 "codes",
276 getName().c_str());
277 }
278 mCountryCode = countryCode;
279 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800280 });
281
Chris Ye1b0c7342020-07-28 21:57:03 -0700282 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
283 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700284
285 if (!isIgnored()) {
286 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800287 mConfiguration.clear();
288 for_each_subdevice([this](InputDeviceContext& context) {
289 PropertyMap configuration;
290 context.getConfiguration(&configuration);
291 mConfiguration.addAll(&configuration);
292 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700293 }
294
295 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700296 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700297 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800299 bool shouldBumpGeneration = false;
300 for_each_subdevice(
301 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
302 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
303 shouldBumpGeneration = true;
304 }
305 });
306 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700307 bumpGeneration();
308 }
309 }
310 }
311
312 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700313 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
315 if (mAlias != alias) {
316 mAlias = alias;
317 bumpGeneration();
318 }
319 }
320 }
321
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700322 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
323 // Do not execute this code on the first configure, because 'setEnabled' would call
324 // InputMapper::reset, and you can't reset a mapper before it has been configured.
325 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 auto it = config->disabledDevices.find(mId);
327 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700328 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700329 }
330
331 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700332 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700334 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700335 mAssociatedViewport = std::nullopt;
336 // Find the display port that corresponds to the current input port.
337 const std::string& inputPort = mIdentifier.location;
338 if (!inputPort.empty()) {
339 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
340 const auto& displayPort = ports.find(inputPort);
341 if (displayPort != ports.end()) {
342 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800343 } else {
344 const std::unordered_map<std::string, std::string>& displayUniqueIds =
345 config->uniqueIdAssociations;
346 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
347 if (displayUniqueId != displayUniqueIds.end()) {
348 mAssociatedDisplayUniqueId = displayUniqueId->second;
349 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700350 }
351 }
352
353 // If the device was explicitly disabled by the user, it would be present in the
354 // "disabledDevices" list. If it is associated with a specific display, and it was not
355 // explicitly disabled, then enable/disable the device based on whether we can find the
356 // corresponding viewport.
357 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
358 if (mAssociatedDisplayPort) {
359 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
360 if (!mAssociatedViewport) {
361 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
362 "but the corresponding viewport is not found.",
363 getName().c_str(), *mAssociatedDisplayPort);
364 enabled = false;
365 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700366 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
367 mAssociatedViewport =
368 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
369 if (!mAssociatedViewport) {
370 ALOGW("Input device %s should be associated with display %s but the "
371 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800372 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700373 enabled = false;
374 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700375 }
376
377 if (changes) {
378 // For first-time configuration, only allow device to be disabled after mappers have
379 // finished configuring. This is because we need to read some of the properties from
380 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700381 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700382 }
383 }
384
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700385 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
386 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800387 mSources |= mapper.getSources();
388 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389
390 // If a device is just plugged but it might be disabled, we need to update some info like
391 // axis range of touch from each InputMapper first, then disable it.
392 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700393 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
394 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395 }
396 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700397 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700398}
399
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700400std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
401 std::list<NotifyArgs> out;
402 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403
404 mContext->updateGlobalMetaState();
405
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700406 out.push_back(notifyReset(when));
407 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700408}
409
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700410std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700411 // Process all of the events in order for each mapper.
412 // We cannot simply ask each mapper to process them in bulk because mappers may
413 // have side-effects that must be interleaved. For example, joystick movement events and
414 // gamepad button presses are handled by different mappers but they should be dispatched
415 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700416 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700417 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800418 if (DEBUG_RAW_EVENTS) {
419 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
420 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
421 rawEvent->when);
422 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700423
424 if (mDropUntilNextSync) {
425 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
426 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800427 if (DEBUG_RAW_EVENTS) {
428 ALOGD("Recovered from input event buffer overrun.");
429 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800431 if (DEBUG_RAW_EVENTS) {
432 ALOGD("Dropped input event while waiting for next input sync.");
433 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
435 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
436 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
437 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700438 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700440 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
441 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800442 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 }
444 --count;
445 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700446 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447}
448
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700449std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
450 std::list<NotifyArgs> out;
451 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
452 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453}
454
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700455std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
456 std::list<NotifyArgs> out;
457 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
458 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459}
460
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000461InputDeviceInfo InputDevice::getDeviceInfo() {
462 InputDeviceInfo outDeviceInfo;
463 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000464 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800465 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000466 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800467
468 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000469 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800470 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000471 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700472}
473
474int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
475 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
476}
477
478int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
479 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
480}
481
482int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
483 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
484}
485
486int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
487 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800488 for (auto& deviceEntry : mDevices) {
489 auto& devicePair = deviceEntry.second;
490 auto& mappers = devicePair.second;
491 for (auto& mapperPtr : mappers) {
492 InputMapper& mapper = *mapperPtr;
493 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
494 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
495 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
496 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
497 if (currentResult >= AKEY_STATE_DOWN) {
498 return currentResult;
499 } else if (currentResult == AKEY_STATE_UP) {
500 result = currentResult;
501 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700502 }
503 }
504 }
505 return result;
506}
507
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700508bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
509 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700510 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700511 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800512 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700513 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800515 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516 return result;
517}
518
Philip Junker4af3b3d2021-12-14 10:36:55 +0100519int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
520 std::optional<int32_t> result = first_in_mappers<int32_t>(
521 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
522 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
523 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
524 }
525 return std::nullopt;
526 });
527 if (!result) {
528 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
529 "KEYBOARD found. The provided input device with id %d has sources %s.",
530 getId(), inputEventSourceToString(getSources()).c_str());
531 return AKEYCODE_UNKNOWN;
532 }
533 return *result;
534}
535
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700536std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
537 int32_t token) {
538 std::list<NotifyArgs> out;
539 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
540 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700541}
542
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700543std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
544 std::list<NotifyArgs> out;
545 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
546 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700547}
548
Chris Ye87143712020-11-10 05:05:58 +0000549bool InputDevice::isVibrating() {
550 bool vibrating = false;
551 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
552 return vibrating;
553}
554
555/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
556 * different vibration mappers then we could have duplicate IDs.
557 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
558 * we would definitely have duplicate IDs.
559 */
560std::vector<int32_t> InputDevice::getVibratorIds() {
561 std::vector<int32_t> vibrators;
562 for_each_mapper([&vibrators](InputMapper& mapper) {
563 std::vector<int32_t> devVibs = mapper.getVibratorIds();
564 vibrators.reserve(vibrators.size() + devVibs.size());
565 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
566 });
567 return vibrators;
568}
569
Chris Yef59a2f42020-10-16 12:55:26 -0700570bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
571 std::chrono::microseconds samplingPeriod,
572 std::chrono::microseconds maxBatchReportLatency) {
573 bool success = true;
574 for_each_mapper(
575 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
576 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
577 });
578 return success;
579}
580
581void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
582 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
583}
584
585void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
586 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
587}
588
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700589std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
590 std::list<NotifyArgs> out;
591 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
592 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700593}
594
Chris Ye3fdbfef2021-01-06 18:45:18 -0800595bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800596 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800597}
598
599bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800600 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800601}
602
603std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800604 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800605}
606
607std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800608 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800609}
610
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700611int32_t InputDevice::getMetaState() {
612 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800613 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700614 return result;
615}
616
617void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000618 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
619 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
620 mapper.updateMetaState(keyCode)) {
621 return std::make_optional(true);
622 }
623 return std::optional<bool>();
624 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700625}
626
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000627void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
628 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
629 context.addKeyRemapping(fromKeyCode, toKeyCode);
630 });
631}
632
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700633void InputDevice::bumpGeneration() {
634 mGeneration = mContext->bumpGeneration();
635}
636
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700637NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
638 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700639}
640
641std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
642 // Check if we had associated to the specific display.
643 if (mAssociatedViewport) {
644 return mAssociatedViewport->displayId;
645 }
646
647 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 return first_in_mappers<int32_t>(
649 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700650}
651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800652// returns the number of mappers associated with the device
653size_t InputDevice::getMapperCount() {
654 size_t count = 0;
655 for (auto& deviceEntry : mDevices) {
656 auto& devicePair = deviceEntry.second;
657 auto& mappers = devicePair.second;
658 count += mappers.size();
659 }
660 return count;
661}
662
arthurhungc903df12020-08-11 15:08:42 +0800663void InputDevice::updateLedState(bool reset) {
664 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
665}
666
Andy Chenf9f1a022022-08-29 20:07:10 -0400667std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
668 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
669}
670
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800671InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800672 : mDevice(device),
673 mContext(device.getContext()),
674 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800675 mId(eventHubId),
676 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800677
678InputDeviceContext::~InputDeviceContext() {}
679
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700680} // namespace android