blob: 6dfe5f52fa193bf4429c914a0dc3b71cb077e3d4 [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
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +000059template <typename K, typename V>
60std::optional<V> getValueByKey(const std::unordered_map<K, V>& map, K key) {
61 auto it = map.find(key);
62 std::optional<V> value = std::nullopt;
63 if (it != map.end()) {
64 value = it->second;
65 }
66 return value;
67}
68
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080070 if (!hasEventHubDevices()) {
71 return false;
72 }
Chris Yee14523a2020-12-19 13:46:00 -080073 // An input device composed of sub devices can be individually enabled or disabled.
74 // If any of the sub device is enabled then the input device is considered as enabled.
75 bool enabled = false;
76 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
77 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078}
79
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070080std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
81 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
83 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
84 "but the corresponding viewport is not found",
85 getName().c_str(), *mAssociatedDisplayPort);
86 enabled = false;
87 }
88
89 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070090 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 }
92
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080093 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
94 // performed. The querying must happen when the device is enabled, so we reset after enabling
95 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080097 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700100 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800101 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 }
103 // Must change generation to flag this device as changed
104 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700105 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106}
107
Chris Yee7310032020-09-22 15:36:28 -0700108void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000109 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
112 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700113 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
115 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
116 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
117 if (mAssociatedDisplayPort) {
118 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
119 } else {
120 dump += "<none>\n";
121 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700122 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
123 if (mAssociatedDisplayUniqueId) {
124 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
125 } else {
126 dump += "<none>\n";
127 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000129 dump += StringPrintf(INDENT2 "Sources: %s\n",
130 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700132 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133
134 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
135 if (!ranges.empty()) {
136 dump += INDENT2 "Motion Ranges:\n";
137 for (size_t i = 0; i < ranges.size(); i++) {
138 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700139 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 char name[32];
141 if (label) {
142 strncpy(name, label, sizeof(name));
143 name[sizeof(name) - 1] = '\0';
144 } else {
145 snprintf(name, sizeof(name), "%d", range.axis);
146 }
147 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000148 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700149 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000150 name, inputEventSourceToString(range.source).c_str(), range.min,
151 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152 }
153 }
154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800155 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800156 if (mController) {
157 mController->dump(dump);
158 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159}
160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800161void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
162 if (mDevices.find(eventHubId) != mDevices.end()) {
163 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800164 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800165 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700166 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800167 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800168
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800169 // Check if we should skip population
170 if (!populateMappers) {
171 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
172 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800173 }
174
175 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700176 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800178 }
179
180 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700181 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800183 }
184
185 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700186 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800187 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800188 }
189
Chris Yee2b1e5c2021-03-10 22:45:12 -0800190 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700191 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800192 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700193 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800194 }
195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800196 // Keyboard-like devices.
197 uint32_t keyboardSource = 0;
198 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700199 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800200 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
201 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700202 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800203 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
204 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700205 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800206 keyboardSource |= AINPUT_SOURCE_DPAD;
207 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700208 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800209 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
210 }
211
212 if (keyboardSource != 0) {
213 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800214 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800215 }
216
217 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700218 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800220 }
221
222 // Touchscreens and touchpad devices.
Harry Cutts89844622022-12-02 15:02:26 +0000223 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
Harry Cutts89844622022-12-02 15:02:26 +0000224 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(false);
Harry Cutts89844622022-12-02 15:02:26 +0000225 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
Harry Cutts1f48a442022-11-15 17:38:36 +0000226 classes.test(InputDeviceClass::TOUCH_MT)) {
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000227 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
228 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800229 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700230 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800231 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800232 }
233
234 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700235 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800236 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800237 }
238
Chris Yef59a2f42020-10-16 12:55:26 -0700239 // Motion sensor enabled devices.
240 if (classes.test(InputDeviceClass::SENSOR)) {
241 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
242 }
243
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800244 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700245 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800246 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800247 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800248
249 // insert the context into the devices set
250 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700251 // Must change generation to flag this device as changed
252 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800253}
254
255void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700256 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
257 // Delete mController, since the corresponding eventhub device is going away
258 mController = nullptr;
259 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800260 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700261}
262
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700263std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
264 uint32_t changes) {
265 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700267 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800268 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000269 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800270
271 for_each_subdevice([this](InputDeviceContext& context) {
272 mClasses |= context.getDeviceClasses();
273 int32_t controllerNumber = context.getDeviceControllerNumber();
274 if (controllerNumber > 0) {
275 if (mControllerNumber && mControllerNumber != controllerNumber) {
276 ALOGW("InputDevice::configure(): composite device contains multiple unique "
277 "controller numbers");
278 }
279 mControllerNumber = controllerNumber;
280 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000281
282 InputDeviceCountryCode countryCode = context.getCountryCode();
283 if (countryCode != InputDeviceCountryCode::INVALID) {
284 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
285 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
286 "codes",
287 getName().c_str());
288 }
289 mCountryCode = countryCode;
290 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800291 });
292
Chris Ye1b0c7342020-07-28 21:57:03 -0700293 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
294 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700295
296 if (!isIgnored()) {
297 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800298 mConfiguration.clear();
299 for_each_subdevice([this](InputDeviceContext& context) {
300 PropertyMap configuration;
301 context.getConfiguration(&configuration);
302 mConfiguration.addAll(&configuration);
303 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000304
305 mAssociatedDeviceType =
306 getValueByKey(config->deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700307 }
308
309 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700310 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700311 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700312 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800313 bool shouldBumpGeneration = false;
314 for_each_subdevice(
315 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
316 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
317 shouldBumpGeneration = true;
318 }
319 });
320 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 bumpGeneration();
322 }
323 }
324 }
325
326 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700327 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700328 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
329 if (mAlias != alias) {
330 mAlias = alias;
331 bumpGeneration();
332 }
333 }
334 }
335
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700336 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
337 // Do not execute this code on the first configure, because 'setEnabled' would call
338 // InputMapper::reset, and you can't reset a mapper before it has been configured.
339 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 auto it = config->disabledDevices.find(mId);
341 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700342 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700343 }
344
345 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700346 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700348 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700349 mAssociatedViewport = std::nullopt;
350 // Find the display port that corresponds to the current input port.
351 const std::string& inputPort = mIdentifier.location;
352 if (!inputPort.empty()) {
353 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
354 const auto& displayPort = ports.find(inputPort);
355 if (displayPort != ports.end()) {
356 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800357 } else {
358 const std::unordered_map<std::string, std::string>& displayUniqueIds =
359 config->uniqueIdAssociations;
360 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
361 if (displayUniqueId != displayUniqueIds.end()) {
362 mAssociatedDisplayUniqueId = displayUniqueId->second;
363 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700364 }
365 }
366
367 // If the device was explicitly disabled by the user, it would be present in the
368 // "disabledDevices" list. If it is associated with a specific display, and it was not
369 // explicitly disabled, then enable/disable the device based on whether we can find the
370 // corresponding viewport.
371 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
372 if (mAssociatedDisplayPort) {
373 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
374 if (!mAssociatedViewport) {
375 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
376 "but the corresponding viewport is not found.",
377 getName().c_str(), *mAssociatedDisplayPort);
378 enabled = false;
379 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700380 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
381 mAssociatedViewport =
382 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
383 if (!mAssociatedViewport) {
384 ALOGW("Input device %s should be associated with display %s but the "
385 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800386 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700387 enabled = false;
388 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389 }
390
391 if (changes) {
392 // For first-time configuration, only allow device to be disabled after mappers have
393 // finished configuring. This is because we need to read some of the properties from
394 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700395 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700396 }
397 }
398
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700399 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
400 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800401 mSources |= mapper.getSources();
402 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700403
404 // If a device is just plugged but it might be disabled, we need to update some info like
405 // axis range of touch from each InputMapper first, then disable it.
406 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700407 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
408 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 }
410 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700411 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700412}
413
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700414std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
415 std::list<NotifyArgs> out;
416 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700417
418 mContext->updateGlobalMetaState();
419
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700420 out.push_back(notifyReset(when));
421 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422}
423
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700424std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 // Process all of the events in order for each mapper.
426 // We cannot simply ask each mapper to process them in bulk because mappers may
427 // have side-effects that must be interleaved. For example, joystick movement events and
428 // gamepad button presses are handled by different mappers but they should be dispatched
429 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700430 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800432 if (DEBUG_RAW_EVENTS) {
433 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
434 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
435 rawEvent->when);
436 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437
438 if (mDropUntilNextSync) {
439 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
440 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800441 if (DEBUG_RAW_EVENTS) {
442 ALOGD("Recovered from input event buffer overrun.");
443 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800445 if (DEBUG_RAW_EVENTS) {
446 ALOGD("Dropped input event while waiting for next input sync.");
447 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700448 }
449 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
450 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
451 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700452 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700454 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
455 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800456 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457 }
458 --count;
459 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700460 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461}
462
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700463std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
464 std::list<NotifyArgs> out;
465 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
466 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700467}
468
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700469std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
470 std::list<NotifyArgs> out;
471 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
472 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473}
474
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000475InputDeviceInfo InputDevice::getDeviceInfo() {
476 InputDeviceInfo outDeviceInfo;
477 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000478 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800479 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000480 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800481
482 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000483 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800484 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000485 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700486}
487
488int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
489 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
490}
491
492int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
493 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
494}
495
496int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
497 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
498}
499
500int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
501 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800502 for (auto& deviceEntry : mDevices) {
503 auto& devicePair = deviceEntry.second;
504 auto& mappers = devicePair.second;
505 for (auto& mapperPtr : mappers) {
506 InputMapper& mapper = *mapperPtr;
507 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
508 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
509 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
510 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
511 if (currentResult >= AKEY_STATE_DOWN) {
512 return currentResult;
513 } else if (currentResult == AKEY_STATE_UP) {
514 result = currentResult;
515 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700516 }
517 }
518 }
519 return result;
520}
521
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700522bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
523 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700524 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700525 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800526 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700527 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700528 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800529 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700530 return result;
531}
532
Philip Junker4af3b3d2021-12-14 10:36:55 +0100533int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
534 std::optional<int32_t> result = first_in_mappers<int32_t>(
535 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
536 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
537 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
538 }
539 return std::nullopt;
540 });
541 if (!result) {
542 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
543 "KEYBOARD found. The provided input device with id %d has sources %s.",
544 getId(), inputEventSourceToString(getSources()).c_str());
545 return AKEYCODE_UNKNOWN;
546 }
547 return *result;
548}
549
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700550std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
551 int32_t token) {
552 std::list<NotifyArgs> out;
553 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
554 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700555}
556
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700557std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
558 std::list<NotifyArgs> out;
559 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
560 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700561}
562
Chris Ye87143712020-11-10 05:05:58 +0000563bool InputDevice::isVibrating() {
564 bool vibrating = false;
565 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
566 return vibrating;
567}
568
569/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
570 * different vibration mappers then we could have duplicate IDs.
571 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
572 * we would definitely have duplicate IDs.
573 */
574std::vector<int32_t> InputDevice::getVibratorIds() {
575 std::vector<int32_t> vibrators;
576 for_each_mapper([&vibrators](InputMapper& mapper) {
577 std::vector<int32_t> devVibs = mapper.getVibratorIds();
578 vibrators.reserve(vibrators.size() + devVibs.size());
579 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
580 });
581 return vibrators;
582}
583
Chris Yef59a2f42020-10-16 12:55:26 -0700584bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
585 std::chrono::microseconds samplingPeriod,
586 std::chrono::microseconds maxBatchReportLatency) {
587 bool success = true;
588 for_each_mapper(
589 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
590 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
591 });
592 return success;
593}
594
595void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
596 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
597}
598
599void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
600 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
601}
602
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700603std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
604 std::list<NotifyArgs> out;
605 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
606 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700607}
608
Chris Ye3fdbfef2021-01-06 18:45:18 -0800609bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800610 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800611}
612
613bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800614 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800615}
616
617std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800618 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800619}
620
621std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800622 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800623}
624
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700625int32_t InputDevice::getMetaState() {
626 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800627 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700628 return result;
629}
630
631void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000632 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
633 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
634 mapper.updateMetaState(keyCode)) {
635 return std::make_optional(true);
636 }
637 return std::optional<bool>();
638 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700639}
640
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000641void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
642 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
643 context.addKeyRemapping(fromKeyCode, toKeyCode);
644 });
645}
646
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700647void InputDevice::bumpGeneration() {
648 mGeneration = mContext->bumpGeneration();
649}
650
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700651NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
652 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700653}
654
655std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
656 // Check if we had associated to the specific display.
657 if (mAssociatedViewport) {
658 return mAssociatedViewport->displayId;
659 }
660
661 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800662 return first_in_mappers<int32_t>(
663 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700664}
665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800666// returns the number of mappers associated with the device
667size_t InputDevice::getMapperCount() {
668 size_t count = 0;
669 for (auto& deviceEntry : mDevices) {
670 auto& devicePair = deviceEntry.second;
671 auto& mappers = devicePair.second;
672 count += mappers.size();
673 }
674 return count;
675}
676
arthurhungc903df12020-08-11 15:08:42 +0800677void InputDevice::updateLedState(bool reset) {
678 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
679}
680
Andy Chenf9f1a022022-08-29 20:07:10 -0400681std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
682 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
683}
684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800685InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800686 : mDevice(device),
687 mContext(device.getContext()),
688 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800689 mId(eventHubId),
690 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800691
692InputDeviceContext::~InputDeviceContext() {}
693
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700694} // namespace android