blob: 13f40ee8e4a0e6507464a474c3e9bde1ee136c7e [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
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000040using android::hardware::input::InputDeviceCountryCode;
41
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042namespace android {
43
44InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080045 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 : mContext(context),
47 mId(id),
48 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080049 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080051 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 mSources(0),
53 mIsExternal(false),
54 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080055 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080057InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070058
59bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080060 if (!hasEventHubDevices()) {
61 return false;
62 }
Chris Yee14523a2020-12-19 13:46:00 -080063 // An input device composed of sub devices can be individually enabled or disabled.
64 // If any of the sub device is enabled then the input device is considered as enabled.
65 bool enabled = false;
66 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
67 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068}
69
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070070std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
71 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
73 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
74 "but the corresponding viewport is not found",
75 getName().c_str(), *mAssociatedDisplayPort);
76 enabled = false;
77 }
78
79 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070080 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 }
82
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080083 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
84 // performed. The querying must happen when the device is enabled, so we reset after enabling
85 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080087 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070090 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080091 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092 }
93 // Must change generation to flag this device as changed
94 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070095 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096}
97
Chris Yee7310032020-09-22 15:36:28 -070098void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000099 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100
101 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
102 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700103 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
105 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
106 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
107 if (mAssociatedDisplayPort) {
108 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
109 } else {
110 dump += "<none>\n";
111 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700112 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
113 if (mAssociatedDisplayUniqueId) {
114 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
115 } else {
116 dump += "<none>\n";
117 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000119 dump += StringPrintf(INDENT2 "Sources: %s\n",
120 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700122 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123
124 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
125 if (!ranges.empty()) {
126 dump += INDENT2 "Motion Ranges:\n";
127 for (size_t i = 0; i < ranges.size(); i++) {
128 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700129 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130 char name[32];
131 if (label) {
132 strncpy(name, label, sizeof(name));
133 name[sizeof(name) - 1] = '\0';
134 } else {
135 snprintf(name, sizeof(name), "%d", range.axis);
136 }
137 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000140 name, inputEventSourceToString(range.source).c_str(), range.min,
141 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 }
143 }
144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800145 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800146 if (mController) {
147 mController->dump(dump);
148 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700149}
150
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800151void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
152 if (mDevices.find(eventHubId) != mDevices.end()) {
153 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800154 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700156 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800157 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800158
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800159 // Check if we should skip population
160 if (!populateMappers) {
161 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
162 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800163 }
164
165 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700166 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800167 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800168 }
169
170 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700171 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800172 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800173 }
174
175 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700176 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800178 }
179
Chris Yee2b1e5c2021-03-10 22:45:12 -0800180 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700181 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800182 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700183 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800184 }
185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 // Keyboard-like devices.
187 uint32_t keyboardSource = 0;
188 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700189 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800190 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
191 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700192 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800193 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
194 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700195 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800196 keyboardSource |= AINPUT_SOURCE_DPAD;
197 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700198 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800199 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
200 }
201
202 if (keyboardSource != 0) {
203 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800204 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800205 }
206
207 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700208 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800209 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800210 }
211
212 // Touchscreens and touchpad devices.
Harry Cutts89844622022-12-02 15:02:26 +0000213 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
Harry Cutts89844622022-12-02 15:02:26 +0000214 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(false);
Harry Cutts89844622022-12-02 15:02:26 +0000215 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
Harry Cutts1f48a442022-11-15 17:38:36 +0000216 classes.test(InputDeviceClass::TOUCH_MT)) {
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000217 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
218 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700220 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800221 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800222 }
223
224 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700225 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800226 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800227 }
228
Chris Yef59a2f42020-10-16 12:55:26 -0700229 // Motion sensor enabled devices.
230 if (classes.test(InputDeviceClass::SENSOR)) {
231 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
232 }
233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800234 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700235 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800236 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800237 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800238
239 // insert the context into the devices set
240 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700241 // Must change generation to flag this device as changed
242 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800243}
244
245void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700246 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
247 // Delete mController, since the corresponding eventhub device is going away
248 mController = nullptr;
249 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251}
252
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700253std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
254 uint32_t changes) {
255 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700256 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700257 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800258 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000259 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800260
261 for_each_subdevice([this](InputDeviceContext& context) {
262 mClasses |= context.getDeviceClasses();
263 int32_t controllerNumber = context.getDeviceControllerNumber();
264 if (controllerNumber > 0) {
265 if (mControllerNumber && mControllerNumber != controllerNumber) {
266 ALOGW("InputDevice::configure(): composite device contains multiple unique "
267 "controller numbers");
268 }
269 mControllerNumber = controllerNumber;
270 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000271
272 InputDeviceCountryCode countryCode = context.getCountryCode();
273 if (countryCode != InputDeviceCountryCode::INVALID) {
274 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
275 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
276 "codes",
277 getName().c_str());
278 }
279 mCountryCode = countryCode;
280 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800281 });
282
Chris Ye1b0c7342020-07-28 21:57:03 -0700283 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
284 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285
286 if (!isIgnored()) {
287 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800288 mConfiguration.clear();
289 for_each_subdevice([this](InputDeviceContext& context) {
290 PropertyMap configuration;
291 context.getConfiguration(&configuration);
292 mConfiguration.addAll(&configuration);
293 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000294
295 mAssociatedDeviceType =
296 getValueByKey(config->deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700297 }
298
299 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700300 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700301 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700302 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800303 bool shouldBumpGeneration = false;
304 for_each_subdevice(
305 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
306 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
307 shouldBumpGeneration = true;
308 }
309 });
310 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311 bumpGeneration();
312 }
313 }
314 }
315
316 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700317 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700318 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
319 if (mAlias != alias) {
320 mAlias = alias;
321 bumpGeneration();
322 }
323 }
324 }
325
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700326 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
327 // Do not execute this code on the first configure, because 'setEnabled' would call
328 // InputMapper::reset, and you can't reset a mapper before it has been configured.
329 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 auto it = config->disabledDevices.find(mId);
331 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700332 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 }
334
335 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700336 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700338 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700339 mAssociatedViewport = std::nullopt;
340 // Find the display port that corresponds to the current input port.
341 const std::string& inputPort = mIdentifier.location;
342 if (!inputPort.empty()) {
343 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
344 const auto& displayPort = ports.find(inputPort);
345 if (displayPort != ports.end()) {
346 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800347 } else {
348 const std::unordered_map<std::string, std::string>& displayUniqueIds =
349 config->uniqueIdAssociations;
350 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
351 if (displayUniqueId != displayUniqueIds.end()) {
352 mAssociatedDisplayUniqueId = displayUniqueId->second;
353 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700354 }
355 }
356
357 // If the device was explicitly disabled by the user, it would be present in the
358 // "disabledDevices" list. If it is associated with a specific display, and it was not
359 // explicitly disabled, then enable/disable the device based on whether we can find the
360 // corresponding viewport.
361 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
362 if (mAssociatedDisplayPort) {
363 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
364 if (!mAssociatedViewport) {
365 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
366 "but the corresponding viewport is not found.",
367 getName().c_str(), *mAssociatedDisplayPort);
368 enabled = false;
369 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700370 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
371 mAssociatedViewport =
372 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
373 if (!mAssociatedViewport) {
374 ALOGW("Input device %s should be associated with display %s but the "
375 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800376 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700377 enabled = false;
378 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700379 }
380
381 if (changes) {
382 // For first-time configuration, only allow device to be disabled after mappers have
383 // finished configuring. This is because we need to read some of the properties from
384 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700385 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386 }
387 }
388
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700389 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
390 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800391 mSources |= mapper.getSources();
392 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700393
394 // If a device is just plugged but it might be disabled, we need to update some info like
395 // axis range of touch from each InputMapper first, then disable it.
396 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700397 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
398 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700399 }
400 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700401 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402}
403
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
405 std::list<NotifyArgs> out;
406 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407
408 mContext->updateGlobalMetaState();
409
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700410 out.push_back(notifyReset(when));
411 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700412}
413
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700414std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415 // Process all of the events in order for each mapper.
416 // We cannot simply ask each mapper to process them in bulk because mappers may
417 // have side-effects that must be interleaved. For example, joystick movement events and
418 // gamepad button presses are handled by different mappers but they should be dispatched
419 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700420 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800422 if (DEBUG_RAW_EVENTS) {
423 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
424 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
425 rawEvent->when);
426 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700427
428 if (mDropUntilNextSync) {
429 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
430 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800431 if (DEBUG_RAW_EVENTS) {
432 ALOGD("Recovered from input event buffer overrun.");
433 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800435 if (DEBUG_RAW_EVENTS) {
436 ALOGD("Dropped input event while waiting for next input sync.");
437 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438 }
439 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
440 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
441 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700442 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700444 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
445 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800446 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447 }
448 --count;
449 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700450 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700451}
452
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700453std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
454 std::list<NotifyArgs> out;
455 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
456 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457}
458
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700459std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
460 std::list<NotifyArgs> out;
461 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
462 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463}
464
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000465InputDeviceInfo InputDevice::getDeviceInfo() {
466 InputDeviceInfo outDeviceInfo;
467 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000468 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800469 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000470 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800471
472 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000473 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800474 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000475 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700476}
477
478int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
479 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
480}
481
482int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
483 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
484}
485
486int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
487 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
488}
489
490int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
491 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800492 for (auto& deviceEntry : mDevices) {
493 auto& devicePair = deviceEntry.second;
494 auto& mappers = devicePair.second;
495 for (auto& mapperPtr : mappers) {
496 InputMapper& mapper = *mapperPtr;
497 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
498 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
499 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
500 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
501 if (currentResult >= AKEY_STATE_DOWN) {
502 return currentResult;
503 } else if (currentResult == AKEY_STATE_UP) {
504 result = currentResult;
505 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700506 }
507 }
508 }
509 return result;
510}
511
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700512bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
513 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700515 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800516 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700517 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800519 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520 return result;
521}
522
Philip Junker4af3b3d2021-12-14 10:36:55 +0100523int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
524 std::optional<int32_t> result = first_in_mappers<int32_t>(
525 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
526 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
527 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
528 }
529 return std::nullopt;
530 });
531 if (!result) {
532 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
533 "KEYBOARD found. The provided input device with id %d has sources %s.",
534 getId(), inputEventSourceToString(getSources()).c_str());
535 return AKEYCODE_UNKNOWN;
536 }
537 return *result;
538}
539
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700540std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
541 int32_t token) {
542 std::list<NotifyArgs> out;
543 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
544 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700545}
546
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700547std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
548 std::list<NotifyArgs> out;
549 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
550 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700551}
552
Chris Ye87143712020-11-10 05:05:58 +0000553bool InputDevice::isVibrating() {
554 bool vibrating = false;
555 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
556 return vibrating;
557}
558
559/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
560 * different vibration mappers then we could have duplicate IDs.
561 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
562 * we would definitely have duplicate IDs.
563 */
564std::vector<int32_t> InputDevice::getVibratorIds() {
565 std::vector<int32_t> vibrators;
566 for_each_mapper([&vibrators](InputMapper& mapper) {
567 std::vector<int32_t> devVibs = mapper.getVibratorIds();
568 vibrators.reserve(vibrators.size() + devVibs.size());
569 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
570 });
571 return vibrators;
572}
573
Chris Yef59a2f42020-10-16 12:55:26 -0700574bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
575 std::chrono::microseconds samplingPeriod,
576 std::chrono::microseconds maxBatchReportLatency) {
577 bool success = true;
578 for_each_mapper(
579 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
580 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
581 });
582 return success;
583}
584
585void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
586 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
587}
588
589void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
590 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
591}
592
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700593std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
594 std::list<NotifyArgs> out;
595 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
596 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700597}
598
Chris Ye3fdbfef2021-01-06 18:45:18 -0800599bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800600 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800601}
602
603bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800604 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800605}
606
607std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800608 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800609}
610
611std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800612 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800613}
614
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700615int32_t InputDevice::getMetaState() {
616 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800617 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700618 return result;
619}
620
621void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000622 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
623 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
624 mapper.updateMetaState(keyCode)) {
625 return std::make_optional(true);
626 }
627 return std::optional<bool>();
628 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700629}
630
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000631void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
632 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
633 context.addKeyRemapping(fromKeyCode, toKeyCode);
634 });
635}
636
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700637void InputDevice::bumpGeneration() {
638 mGeneration = mContext->bumpGeneration();
639}
640
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700641NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
642 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700643}
644
645std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
646 // Check if we had associated to the specific display.
647 if (mAssociatedViewport) {
648 return mAssociatedViewport->displayId;
649 }
650
651 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800652 return first_in_mappers<int32_t>(
653 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700654}
655
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800656// returns the number of mappers associated with the device
657size_t InputDevice::getMapperCount() {
658 size_t count = 0;
659 for (auto& deviceEntry : mDevices) {
660 auto& devicePair = deviceEntry.second;
661 auto& mappers = devicePair.second;
662 count += mappers.size();
663 }
664 return count;
665}
666
arthurhungc903df12020-08-11 15:08:42 +0800667void InputDevice::updateLedState(bool reset) {
668 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
669}
670
Andy Chenf9f1a022022-08-29 20:07:10 -0400671std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
672 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
673}
674
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800675InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800676 : mDevice(device),
677 mContext(device.getContext()),
678 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800679 mId(eventHubId),
680 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800681
682InputDeviceContext::~InputDeviceContext() {}
683
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700684} // namespace android