blob: 52917766380857357e6338d76a54578276e28d7c [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"
36#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000038using android::hardware::input::InputDeviceCountryCode;
39
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070040namespace android {
41
42InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080043 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 : mContext(context),
45 mId(id),
46 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080049 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 mSources(0),
51 mIsExternal(false),
52 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080055InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
57bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080058 if (!hasEventHubDevices()) {
59 return false;
60 }
Chris Yee14523a2020-12-19 13:46:00 -080061 // An input device composed of sub devices can be individually enabled or disabled.
62 // If any of the sub device is enabled then the input device is considered as enabled.
63 bool enabled = false;
64 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
65 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066}
67
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070068std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
69 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
71 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
72 "but the corresponding viewport is not found",
73 getName().c_str(), *mAssociatedDisplayPort);
74 enabled = false;
75 }
76
77 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 }
80
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080081 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
82 // performed. The querying must happen when the device is enabled, so we reset after enabling
83 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91 // Must change generation to flag this device as changed
92 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094}
95
Chris Yee7310032020-09-22 15:36:28 -070096void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000097 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
99 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
100 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700101 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
103 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
104 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
105 if (mAssociatedDisplayPort) {
106 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
107 } else {
108 dump += "<none>\n";
109 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700110 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
111 if (mAssociatedDisplayUniqueId) {
112 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
113 } else {
114 dump += "<none>\n";
115 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000117 dump += StringPrintf(INDENT2 "Sources: %s\n",
118 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700120 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
122 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
123 if (!ranges.empty()) {
124 dump += INDENT2 "Motion Ranges:\n";
125 for (size_t i = 0; i < ranges.size(); i++) {
126 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700127 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 char name[32];
129 if (label) {
130 strncpy(name, label, sizeof(name));
131 name[sizeof(name) - 1] = '\0';
132 } else {
133 snprintf(name, sizeof(name), "%d", range.axis);
134 }
135 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000136 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 name, inputEventSourceToString(range.source).c_str(), range.min,
139 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 }
141 }
142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800143 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800144 if (mController) {
145 mController->dump(dump);
146 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147}
148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800149void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
150 if (mDevices.find(eventHubId) != mDevices.end()) {
151 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800152 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700154 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800156
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800157 // Check if we should skip population
158 if (!populateMappers) {
159 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
160 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800161 }
162
163 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700164 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800165 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800166 }
167
168 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700169 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800170 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800171 }
172
173 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700174 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800175 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800176 }
177
Chris Yee2b1e5c2021-03-10 22:45:12 -0800178 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700179 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800180 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700181 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800182 }
183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800184 // Keyboard-like devices.
185 uint32_t keyboardSource = 0;
186 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700187 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800188 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
189 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700190 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800191 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
192 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700193 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800194 keyboardSource |= AINPUT_SOURCE_DPAD;
195 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700196 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800197 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
198 }
199
200 if (keyboardSource != 0) {
201 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800202 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800203 }
204
205 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700206 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800208 }
209
210 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700211 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800212 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700213 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800214 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800215 }
216
217 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700218 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800220 }
221
Chris Yef59a2f42020-10-16 12:55:26 -0700222 // Motion sensor enabled devices.
223 if (classes.test(InputDeviceClass::SENSOR)) {
224 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
225 }
226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800227 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700228 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800229 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800230 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800231
232 // insert the context into the devices set
233 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700234 // Must change generation to flag this device as changed
235 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800236}
237
238void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700239 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
240 // Delete mController, since the corresponding eventhub device is going away
241 mController = nullptr;
242 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800243 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700244}
245
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700246std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
247 uint32_t changes) {
248 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700250 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800251 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000252 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800253
254 for_each_subdevice([this](InputDeviceContext& context) {
255 mClasses |= context.getDeviceClasses();
256 int32_t controllerNumber = context.getDeviceControllerNumber();
257 if (controllerNumber > 0) {
258 if (mControllerNumber && mControllerNumber != controllerNumber) {
259 ALOGW("InputDevice::configure(): composite device contains multiple unique "
260 "controller numbers");
261 }
262 mControllerNumber = controllerNumber;
263 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000264
265 InputDeviceCountryCode countryCode = context.getCountryCode();
266 if (countryCode != InputDeviceCountryCode::INVALID) {
267 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
268 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
269 "codes",
270 getName().c_str());
271 }
272 mCountryCode = countryCode;
273 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800274 });
275
Chris Ye1b0c7342020-07-28 21:57:03 -0700276 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
277 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700278
279 if (!isIgnored()) {
280 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800281 mConfiguration.clear();
282 for_each_subdevice([this](InputDeviceContext& context) {
283 PropertyMap configuration;
284 context.getConfiguration(&configuration);
285 mConfiguration.addAll(&configuration);
286 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 }
288
289 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700290 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700291 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700292 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800293 bool shouldBumpGeneration = false;
294 for_each_subdevice(
295 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
296 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
297 shouldBumpGeneration = true;
298 }
299 });
300 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700301 bumpGeneration();
302 }
303 }
304 }
305
306 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700307 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
309 if (mAlias != alias) {
310 mAlias = alias;
311 bumpGeneration();
312 }
313 }
314 }
315
316 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
317 auto it = config->disabledDevices.find(mId);
318 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700319 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700320 }
321
322 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700323 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700324 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700325 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326 mAssociatedViewport = std::nullopt;
327 // Find the display port that corresponds to the current input port.
328 const std::string& inputPort = mIdentifier.location;
329 if (!inputPort.empty()) {
330 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
331 const auto& displayPort = ports.find(inputPort);
332 if (displayPort != ports.end()) {
333 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800334 } else {
335 const std::unordered_map<std::string, std::string>& displayUniqueIds =
336 config->uniqueIdAssociations;
337 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
338 if (displayUniqueId != displayUniqueIds.end()) {
339 mAssociatedDisplayUniqueId = displayUniqueId->second;
340 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700341 }
342 }
343
344 // If the device was explicitly disabled by the user, it would be present in the
345 // "disabledDevices" list. If it is associated with a specific display, and it was not
346 // explicitly disabled, then enable/disable the device based on whether we can find the
347 // corresponding viewport.
348 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
349 if (mAssociatedDisplayPort) {
350 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
351 if (!mAssociatedViewport) {
352 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
353 "but the corresponding viewport is not found.",
354 getName().c_str(), *mAssociatedDisplayPort);
355 enabled = false;
356 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700357 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
358 mAssociatedViewport =
359 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
360 if (!mAssociatedViewport) {
361 ALOGW("Input device %s should be associated with display %s but the "
362 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800363 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700364 enabled = false;
365 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700366 }
367
368 if (changes) {
369 // For first-time configuration, only allow device to be disabled after mappers have
370 // finished configuring. This is because we need to read some of the properties from
371 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700372 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373 }
374 }
375
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700376 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
377 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800378 mSources |= mapper.getSources();
379 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380
381 // If a device is just plugged but it might be disabled, we need to update some info like
382 // axis range of touch from each InputMapper first, then disable it.
383 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700384 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
385 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700386 }
387 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389}
390
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700391std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
392 std::list<NotifyArgs> out;
393 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700394
395 mContext->updateGlobalMetaState();
396
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700397 out.push_back(notifyReset(when));
398 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700399}
400
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700401std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 // Process all of the events in order for each mapper.
403 // We cannot simply ask each mapper to process them in bulk because mappers may
404 // have side-effects that must be interleaved. For example, joystick movement events and
405 // gamepad button presses are handled by different mappers but they should be dispatched
406 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700407 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700408 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800409 if (DEBUG_RAW_EVENTS) {
410 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
411 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
412 rawEvent->when);
413 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700414
415 if (mDropUntilNextSync) {
416 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
417 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800418 if (DEBUG_RAW_EVENTS) {
419 ALOGD("Recovered from input event buffer overrun.");
420 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800422 if (DEBUG_RAW_EVENTS) {
423 ALOGD("Dropped input event while waiting for next input sync.");
424 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700425 }
426 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
427 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
428 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700429 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700431 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
432 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800433 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
435 --count;
436 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700437 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700438}
439
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700440std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
441 std::list<NotifyArgs> out;
442 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
443 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700444}
445
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700446std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
447 std::list<NotifyArgs> out;
448 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
449 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450}
451
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000452InputDeviceInfo InputDevice::getDeviceInfo() {
453 InputDeviceInfo outDeviceInfo;
454 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000455 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800456 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000457 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800458
459 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000460 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800461 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000462 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463}
464
465int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
466 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
467}
468
469int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
470 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
471}
472
473int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
474 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
475}
476
477int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
478 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800479 for (auto& deviceEntry : mDevices) {
480 auto& devicePair = deviceEntry.second;
481 auto& mappers = devicePair.second;
482 for (auto& mapperPtr : mappers) {
483 InputMapper& mapper = *mapperPtr;
484 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
485 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
486 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
487 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
488 if (currentResult >= AKEY_STATE_DOWN) {
489 return currentResult;
490 } else if (currentResult == AKEY_STATE_UP) {
491 result = currentResult;
492 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700493 }
494 }
495 }
496 return result;
497}
498
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700499bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
500 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700502 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800503 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700504 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700505 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800506 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700507 return result;
508}
509
Philip Junker4af3b3d2021-12-14 10:36:55 +0100510int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
511 std::optional<int32_t> result = first_in_mappers<int32_t>(
512 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
513 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
514 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
515 }
516 return std::nullopt;
517 });
518 if (!result) {
519 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
520 "KEYBOARD found. The provided input device with id %d has sources %s.",
521 getId(), inputEventSourceToString(getSources()).c_str());
522 return AKEYCODE_UNKNOWN;
523 }
524 return *result;
525}
526
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700527std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
528 int32_t token) {
529 std::list<NotifyArgs> out;
530 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
531 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700532}
533
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700534std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
535 std::list<NotifyArgs> out;
536 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
537 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700538}
539
Chris Ye87143712020-11-10 05:05:58 +0000540bool InputDevice::isVibrating() {
541 bool vibrating = false;
542 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
543 return vibrating;
544}
545
546/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
547 * different vibration mappers then we could have duplicate IDs.
548 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
549 * we would definitely have duplicate IDs.
550 */
551std::vector<int32_t> InputDevice::getVibratorIds() {
552 std::vector<int32_t> vibrators;
553 for_each_mapper([&vibrators](InputMapper& mapper) {
554 std::vector<int32_t> devVibs = mapper.getVibratorIds();
555 vibrators.reserve(vibrators.size() + devVibs.size());
556 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
557 });
558 return vibrators;
559}
560
Chris Yef59a2f42020-10-16 12:55:26 -0700561bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
562 std::chrono::microseconds samplingPeriod,
563 std::chrono::microseconds maxBatchReportLatency) {
564 bool success = true;
565 for_each_mapper(
566 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
567 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
568 });
569 return success;
570}
571
572void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
573 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
574}
575
576void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
577 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
578}
579
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700580std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
581 std::list<NotifyArgs> out;
582 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
583 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700584}
585
Chris Ye3fdbfef2021-01-06 18:45:18 -0800586bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800587 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800588}
589
590bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800591 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800592}
593
594std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800595 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800596}
597
598std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800599 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800600}
601
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700602int32_t InputDevice::getMetaState() {
603 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800604 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700605 return result;
606}
607
608void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000609 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
610 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
611 mapper.updateMetaState(keyCode)) {
612 return std::make_optional(true);
613 }
614 return std::optional<bool>();
615 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700616}
617
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700618void InputDevice::bumpGeneration() {
619 mGeneration = mContext->bumpGeneration();
620}
621
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700622NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
623 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700624}
625
626std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
627 // Check if we had associated to the specific display.
628 if (mAssociatedViewport) {
629 return mAssociatedViewport->displayId;
630 }
631
632 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800633 return first_in_mappers<int32_t>(
634 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700635}
636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800637// returns the number of mappers associated with the device
638size_t InputDevice::getMapperCount() {
639 size_t count = 0;
640 for (auto& deviceEntry : mDevices) {
641 auto& devicePair = deviceEntry.second;
642 auto& mappers = devicePair.second;
643 count += mappers.size();
644 }
645 return count;
646}
647
arthurhungc903df12020-08-11 15:08:42 +0800648void InputDevice::updateLedState(bool reset) {
649 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
650}
651
Andy Chenf9f1a022022-08-29 20:07:10 -0400652std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
653 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
654}
655
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800656InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800657 : mDevice(device),
658 mContext(device.getContext()),
659 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800660 mId(eventHubId),
661 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800662
663InputDeviceContext::~InputDeviceContext() {}
664
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700665} // namespace android