blob: 6b9b9f1255c00b3e7462d84317113982f77cfe28 [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
68void InputDevice::setEnabled(bool enabled, nsecs_t when) {
69 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
70 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
71 "but the corresponding viewport is not found",
72 getName().c_str(), *mAssociatedDisplayPort);
73 enabled = false;
74 }
75
76 if (isEnabled() == enabled) {
77 return;
78 }
79
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080080 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
81 // performed. The querying must happen when the device is enabled, so we reset after enabling
82 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080084 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 reset(when);
86 } else {
87 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080088 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 }
90 // Must change generation to flag this device as changed
91 bumpGeneration();
92}
93
Chris Yee7310032020-09-22 15:36:28 -070094void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000095 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096
97 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
98 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -070099 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
101 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
102 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
103 if (mAssociatedDisplayPort) {
104 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
105 } else {
106 dump += "<none>\n";
107 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700108 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
109 if (mAssociatedDisplayUniqueId) {
110 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
111 } else {
112 dump += "<none>\n";
113 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000115 dump += StringPrintf(INDENT2 "Sources: %s\n",
116 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700118 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119
120 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
121 if (!ranges.empty()) {
122 dump += INDENT2 "Motion Ranges:\n";
123 for (size_t i = 0; i < ranges.size(); i++) {
124 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700125 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700126 char name[32];
127 if (label) {
128 strncpy(name, label, sizeof(name));
129 name[sizeof(name) - 1] = '\0';
130 } else {
131 snprintf(name, sizeof(name), "%d", range.axis);
132 }
133 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000134 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700135 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000136 name, inputEventSourceToString(range.source).c_str(), range.min,
137 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 }
139 }
140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800141 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800142 if (mController) {
143 mController->dump(dump);
144 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700145}
146
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800147void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
148 if (mDevices.find(eventHubId) != mDevices.end()) {
149 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800150 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800151 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700152 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 // Check if we should skip population
156 if (!populateMappers) {
157 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
158 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800159 }
160
161 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700162 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800163 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800164 }
165
166 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700167 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800168 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800169 }
170
171 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700172 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800173 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800174 }
175
Chris Yee2b1e5c2021-03-10 22:45:12 -0800176 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700177 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800178 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700179 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800180 }
181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800182 // Keyboard-like devices.
183 uint32_t keyboardSource = 0;
184 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700185 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
187 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700188 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800189 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
190 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700191 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 keyboardSource |= AINPUT_SOURCE_DPAD;
193 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700194 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800195 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
196 }
197
198 if (keyboardSource != 0) {
199 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800200 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800201 }
202
203 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700204 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800205 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800206 }
207
208 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700209 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800210 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700211 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800212 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800213 }
214
215 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700216 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800217 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800218 }
219
Chris Yef59a2f42020-10-16 12:55:26 -0700220 // Motion sensor enabled devices.
221 if (classes.test(InputDeviceClass::SENSOR)) {
222 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
223 }
224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800225 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700226 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800227 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800228 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800229
230 // insert the context into the devices set
231 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700232 // Must change generation to flag this device as changed
233 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800234}
235
236void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700237 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
238 // Delete mController, since the corresponding eventhub device is going away
239 mController = nullptr;
240 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800241 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700242}
243
244void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
245 uint32_t changes) {
246 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700247 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800248 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000249 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800250
251 for_each_subdevice([this](InputDeviceContext& context) {
252 mClasses |= context.getDeviceClasses();
253 int32_t controllerNumber = context.getDeviceControllerNumber();
254 if (controllerNumber > 0) {
255 if (mControllerNumber && mControllerNumber != controllerNumber) {
256 ALOGW("InputDevice::configure(): composite device contains multiple unique "
257 "controller numbers");
258 }
259 mControllerNumber = controllerNumber;
260 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000261
262 InputDeviceCountryCode countryCode = context.getCountryCode();
263 if (countryCode != InputDeviceCountryCode::INVALID) {
264 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
265 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
266 "codes",
267 getName().c_str());
268 }
269 mCountryCode = countryCode;
270 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800271 });
272
Chris Ye1b0c7342020-07-28 21:57:03 -0700273 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
274 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700275
276 if (!isIgnored()) {
277 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800278 mConfiguration.clear();
279 for_each_subdevice([this](InputDeviceContext& context) {
280 PropertyMap configuration;
281 context.getConfiguration(&configuration);
282 mConfiguration.addAll(&configuration);
283 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700284 }
285
286 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700287 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700288 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700289 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800290 bool shouldBumpGeneration = false;
291 for_each_subdevice(
292 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
293 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
294 shouldBumpGeneration = true;
295 }
296 });
297 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700298 bumpGeneration();
299 }
300 }
301 }
302
303 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700304 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
306 if (mAlias != alias) {
307 mAlias = alias;
308 bumpGeneration();
309 }
310 }
311 }
312
313 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
314 auto it = config->disabledDevices.find(mId);
315 bool enabled = it == config->disabledDevices.end();
316 setEnabled(enabled, when);
317 }
318
319 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700320 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700322 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700323 mAssociatedViewport = std::nullopt;
324 // Find the display port that corresponds to the current input port.
325 const std::string& inputPort = mIdentifier.location;
326 if (!inputPort.empty()) {
327 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
328 const auto& displayPort = ports.find(inputPort);
329 if (displayPort != ports.end()) {
330 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800331 } else {
332 const std::unordered_map<std::string, std::string>& displayUniqueIds =
333 config->uniqueIdAssociations;
334 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
335 if (displayUniqueId != displayUniqueIds.end()) {
336 mAssociatedDisplayUniqueId = displayUniqueId->second;
337 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700338 }
339 }
340
341 // If the device was explicitly disabled by the user, it would be present in the
342 // "disabledDevices" list. If it is associated with a specific display, and it was not
343 // explicitly disabled, then enable/disable the device based on whether we can find the
344 // corresponding viewport.
345 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
346 if (mAssociatedDisplayPort) {
347 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
348 if (!mAssociatedViewport) {
349 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
350 "but the corresponding viewport is not found.",
351 getName().c_str(), *mAssociatedDisplayPort);
352 enabled = false;
353 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700354 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
355 mAssociatedViewport =
356 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
357 if (!mAssociatedViewport) {
358 ALOGW("Input device %s should be associated with display %s but the "
359 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800360 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700361 enabled = false;
362 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700363 }
364
365 if (changes) {
366 // For first-time configuration, only allow device to be disabled after mappers have
367 // finished configuring. This is because we need to read some of the properties from
368 // the device's open fd.
369 setEnabled(enabled, when);
370 }
371 }
372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800373 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
374 mapper.configure(when, config, changes);
375 mSources |= mapper.getSources();
376 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700377
378 // If a device is just plugged but it might be disabled, we need to update some info like
379 // axis range of touch from each InputMapper first, then disable it.
380 if (!changes) {
381 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
382 }
383 }
384}
385
386void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800387 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700388
389 mContext->updateGlobalMetaState();
390
391 notifyReset(when);
392}
393
394void InputDevice::process(const RawEvent* rawEvents, size_t count) {
395 // Process all of the events in order for each mapper.
396 // We cannot simply ask each mapper to process them in bulk because mappers may
397 // have side-effects that must be interleaved. For example, joystick movement events and
398 // gamepad button presses are handled by different mappers but they should be dispatched
399 // in the order received.
400 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800401 if (DEBUG_RAW_EVENTS) {
402 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
403 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
404 rawEvent->when);
405 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700406
407 if (mDropUntilNextSync) {
408 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
409 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800410 if (DEBUG_RAW_EVENTS) {
411 ALOGD("Recovered from input event buffer overrun.");
412 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700413 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800414 if (DEBUG_RAW_EVENTS) {
415 ALOGD("Dropped input event while waiting for next input sync.");
416 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700417 }
418 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
419 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
420 mDropUntilNextSync = true;
421 reset(rawEvent->when);
422 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800423 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
424 mapper.process(rawEvent);
425 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700426 }
427 --count;
428 }
429}
430
431void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800432 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433}
434
435void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800436 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437}
438
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000439InputDeviceInfo InputDevice::getDeviceInfo() {
440 InputDeviceInfo outDeviceInfo;
441 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000442 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800443 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000444 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800445
446 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000447 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800448 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000449 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450}
451
452int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
453 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
454}
455
456int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
457 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
458}
459
460int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
461 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
462}
463
464int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
465 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800466 for (auto& deviceEntry : mDevices) {
467 auto& devicePair = deviceEntry.second;
468 auto& mappers = devicePair.second;
469 for (auto& mapperPtr : mappers) {
470 InputMapper& mapper = *mapperPtr;
471 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
472 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
473 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
474 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
475 if (currentResult >= AKEY_STATE_DOWN) {
476 return currentResult;
477 } else if (currentResult == AKEY_STATE_UP) {
478 result = currentResult;
479 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700480 }
481 }
482 }
483 return result;
484}
485
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700486bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
487 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700488 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700489 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800490 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700491 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700492 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800493 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700494 return result;
495}
496
Philip Junker4af3b3d2021-12-14 10:36:55 +0100497int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
498 std::optional<int32_t> result = first_in_mappers<int32_t>(
499 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
500 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
501 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
502 }
503 return std::nullopt;
504 });
505 if (!result) {
506 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
507 "KEYBOARD found. The provided input device with id %d has sources %s.",
508 getId(), inputEventSourceToString(getSources()).c_str());
509 return AKEYCODE_UNKNOWN;
510 }
511 return *result;
512}
513
Chris Ye87143712020-11-10 05:05:58 +0000514void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
515 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
516 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800517 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518}
519
520void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800521 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700522}
523
Chris Ye87143712020-11-10 05:05:58 +0000524bool InputDevice::isVibrating() {
525 bool vibrating = false;
526 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
527 return vibrating;
528}
529
530/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
531 * different vibration mappers then we could have duplicate IDs.
532 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
533 * we would definitely have duplicate IDs.
534 */
535std::vector<int32_t> InputDevice::getVibratorIds() {
536 std::vector<int32_t> vibrators;
537 for_each_mapper([&vibrators](InputMapper& mapper) {
538 std::vector<int32_t> devVibs = mapper.getVibratorIds();
539 vibrators.reserve(vibrators.size() + devVibs.size());
540 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
541 });
542 return vibrators;
543}
544
Chris Yef59a2f42020-10-16 12:55:26 -0700545bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
546 std::chrono::microseconds samplingPeriod,
547 std::chrono::microseconds maxBatchReportLatency) {
548 bool success = true;
549 for_each_mapper(
550 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
551 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
552 });
553 return success;
554}
555
556void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
557 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
558}
559
560void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
561 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
562}
563
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000564void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
565 for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700566}
567
Chris Ye3fdbfef2021-01-06 18:45:18 -0800568bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800569 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800570}
571
572bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800573 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800574}
575
576std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800577 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800578}
579
580std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800581 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800582}
583
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700584int32_t InputDevice::getMetaState() {
585 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800586 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700587 return result;
588}
589
590void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000591 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
592 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
593 mapper.updateMetaState(keyCode)) {
594 return std::make_optional(true);
595 }
596 return std::optional<bool>();
597 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700598}
599
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700600void InputDevice::bumpGeneration() {
601 mGeneration = mContext->bumpGeneration();
602}
603
604void InputDevice::notifyReset(nsecs_t when) {
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000605 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700606 mContext->getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700607}
608
609std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
610 // Check if we had associated to the specific display.
611 if (mAssociatedViewport) {
612 return mAssociatedViewport->displayId;
613 }
614
615 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 return first_in_mappers<int32_t>(
617 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700618}
619
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800620// returns the number of mappers associated with the device
621size_t InputDevice::getMapperCount() {
622 size_t count = 0;
623 for (auto& deviceEntry : mDevices) {
624 auto& devicePair = deviceEntry.second;
625 auto& mappers = devicePair.second;
626 count += mappers.size();
627 }
628 return count;
629}
630
arthurhungc903df12020-08-11 15:08:42 +0800631void InputDevice::updateLedState(bool reset) {
632 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
633}
634
Andy Chenf9f1a022022-08-29 20:07:10 -0400635std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
636 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
637}
638
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800639InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800640 : mDevice(device),
641 mContext(device.getContext()),
642 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800643 mId(eventHubId),
644 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800645
646InputDeviceContext::~InputDeviceContext() {}
647
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700648} // namespace android