blob: 44a005e5a935048f12b8c144bd84786b4de3b6fe [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) {
237 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700238}
239
240void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
241 uint32_t changes) {
242 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700243 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000245 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800246
247 for_each_subdevice([this](InputDeviceContext& context) {
248 mClasses |= context.getDeviceClasses();
249 int32_t controllerNumber = context.getDeviceControllerNumber();
250 if (controllerNumber > 0) {
251 if (mControllerNumber && mControllerNumber != controllerNumber) {
252 ALOGW("InputDevice::configure(): composite device contains multiple unique "
253 "controller numbers");
254 }
255 mControllerNumber = controllerNumber;
256 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000257
258 InputDeviceCountryCode countryCode = context.getCountryCode();
259 if (countryCode != InputDeviceCountryCode::INVALID) {
260 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
261 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
262 "codes",
263 getName().c_str());
264 }
265 mCountryCode = countryCode;
266 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800267 });
268
Chris Ye1b0c7342020-07-28 21:57:03 -0700269 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
270 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271
272 if (!isIgnored()) {
273 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800274 mConfiguration.clear();
275 for_each_subdevice([this](InputDeviceContext& context) {
276 PropertyMap configuration;
277 context.getConfiguration(&configuration);
278 mConfiguration.addAll(&configuration);
279 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 }
281
282 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700283 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700284 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700285 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800286 bool shouldBumpGeneration = false;
287 for_each_subdevice(
288 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
289 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
290 shouldBumpGeneration = true;
291 }
292 });
293 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 bumpGeneration();
295 }
296 }
297 }
298
299 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700300 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700301 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
302 if (mAlias != alias) {
303 mAlias = alias;
304 bumpGeneration();
305 }
306 }
307 }
308
309 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
310 auto it = config->disabledDevices.find(mId);
311 bool enabled = it == config->disabledDevices.end();
312 setEnabled(enabled, when);
313 }
314
315 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700316 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700317 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700318 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700319 mAssociatedViewport = std::nullopt;
320 // Find the display port that corresponds to the current input port.
321 const std::string& inputPort = mIdentifier.location;
322 if (!inputPort.empty()) {
323 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
324 const auto& displayPort = ports.find(inputPort);
325 if (displayPort != ports.end()) {
326 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800327 } else {
328 const std::unordered_map<std::string, std::string>& displayUniqueIds =
329 config->uniqueIdAssociations;
330 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
331 if (displayUniqueId != displayUniqueIds.end()) {
332 mAssociatedDisplayUniqueId = displayUniqueId->second;
333 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700334 }
335 }
336
337 // If the device was explicitly disabled by the user, it would be present in the
338 // "disabledDevices" list. If it is associated with a specific display, and it was not
339 // explicitly disabled, then enable/disable the device based on whether we can find the
340 // corresponding viewport.
341 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
342 if (mAssociatedDisplayPort) {
343 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
344 if (!mAssociatedViewport) {
345 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
346 "but the corresponding viewport is not found.",
347 getName().c_str(), *mAssociatedDisplayPort);
348 enabled = false;
349 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700350 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
351 mAssociatedViewport =
352 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
353 if (!mAssociatedViewport) {
354 ALOGW("Input device %s should be associated with display %s but the "
355 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800356 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700357 enabled = false;
358 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700359 }
360
361 if (changes) {
362 // For first-time configuration, only allow device to be disabled after mappers have
363 // finished configuring. This is because we need to read some of the properties from
364 // the device's open fd.
365 setEnabled(enabled, when);
366 }
367 }
368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800369 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
370 mapper.configure(when, config, changes);
371 mSources |= mapper.getSources();
372 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373
374 // If a device is just plugged but it might be disabled, we need to update some info like
375 // axis range of touch from each InputMapper first, then disable it.
376 if (!changes) {
377 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
378 }
379 }
380}
381
382void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800383 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384
385 mContext->updateGlobalMetaState();
386
387 notifyReset(when);
388}
389
390void InputDevice::process(const RawEvent* rawEvents, size_t count) {
391 // Process all of the events in order for each mapper.
392 // We cannot simply ask each mapper to process them in bulk because mappers may
393 // have side-effects that must be interleaved. For example, joystick movement events and
394 // gamepad button presses are handled by different mappers but they should be dispatched
395 // in the order received.
396 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800397 if (DEBUG_RAW_EVENTS) {
398 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
399 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
400 rawEvent->when);
401 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402
403 if (mDropUntilNextSync) {
404 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
405 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800406 if (DEBUG_RAW_EVENTS) {
407 ALOGD("Recovered from input event buffer overrun.");
408 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800410 if (DEBUG_RAW_EVENTS) {
411 ALOGD("Dropped input event while waiting for next input sync.");
412 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700413 }
414 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
415 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
416 mDropUntilNextSync = true;
417 reset(rawEvent->when);
418 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800419 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
420 mapper.process(rawEvent);
421 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422 }
423 --count;
424 }
425}
426
427void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800428 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429}
430
431void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800432 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433}
434
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000435InputDeviceInfo InputDevice::getDeviceInfo() {
436 InputDeviceInfo outDeviceInfo;
437 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000438 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800439 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000440 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800441
442 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000443 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800444 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000445 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446}
447
448int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
449 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
450}
451
452int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
453 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
454}
455
456int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
457 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
458}
459
460int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
461 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800462 for (auto& deviceEntry : mDevices) {
463 auto& devicePair = deviceEntry.second;
464 auto& mappers = devicePair.second;
465 for (auto& mapperPtr : mappers) {
466 InputMapper& mapper = *mapperPtr;
467 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
468 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
469 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
470 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
471 if (currentResult >= AKEY_STATE_DOWN) {
472 return currentResult;
473 } else if (currentResult == AKEY_STATE_UP) {
474 result = currentResult;
475 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700476 }
477 }
478 }
479 return result;
480}
481
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700482bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
483 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700484 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700485 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800486 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700487 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700488 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800489 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700490 return result;
491}
492
Philip Junker4af3b3d2021-12-14 10:36:55 +0100493int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
494 std::optional<int32_t> result = first_in_mappers<int32_t>(
495 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
496 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
497 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
498 }
499 return std::nullopt;
500 });
501 if (!result) {
502 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
503 "KEYBOARD found. The provided input device with id %d has sources %s.",
504 getId(), inputEventSourceToString(getSources()).c_str());
505 return AKEYCODE_UNKNOWN;
506 }
507 return *result;
508}
509
Chris Ye87143712020-11-10 05:05:58 +0000510void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
511 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
512 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800513 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700514}
515
516void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800517 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700518}
519
Chris Ye87143712020-11-10 05:05:58 +0000520bool InputDevice::isVibrating() {
521 bool vibrating = false;
522 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
523 return vibrating;
524}
525
526/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
527 * different vibration mappers then we could have duplicate IDs.
528 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
529 * we would definitely have duplicate IDs.
530 */
531std::vector<int32_t> InputDevice::getVibratorIds() {
532 std::vector<int32_t> vibrators;
533 for_each_mapper([&vibrators](InputMapper& mapper) {
534 std::vector<int32_t> devVibs = mapper.getVibratorIds();
535 vibrators.reserve(vibrators.size() + devVibs.size());
536 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
537 });
538 return vibrators;
539}
540
Chris Yef59a2f42020-10-16 12:55:26 -0700541bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
542 std::chrono::microseconds samplingPeriod,
543 std::chrono::microseconds maxBatchReportLatency) {
544 bool success = true;
545 for_each_mapper(
546 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
547 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
548 });
549 return success;
550}
551
552void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
553 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
554}
555
556void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
557 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
558}
559
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000560void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
561 for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700562}
563
Chris Ye3fdbfef2021-01-06 18:45:18 -0800564bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800565 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800566}
567
568bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800569 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800570}
571
572std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800573 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800574}
575
576std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800577 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800578}
579
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700580int32_t InputDevice::getMetaState() {
581 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800582 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700583 return result;
584}
585
586void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000587 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
588 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
589 mapper.updateMetaState(keyCode)) {
590 return std::make_optional(true);
591 }
592 return std::optional<bool>();
593 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700594}
595
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700596void InputDevice::bumpGeneration() {
597 mGeneration = mContext->bumpGeneration();
598}
599
600void InputDevice::notifyReset(nsecs_t when) {
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000601 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700602 mContext->getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700603}
604
605std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
606 // Check if we had associated to the specific display.
607 if (mAssociatedViewport) {
608 return mAssociatedViewport->displayId;
609 }
610
611 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800612 return first_in_mappers<int32_t>(
613 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700614}
615
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800616// returns the number of mappers associated with the device
617size_t InputDevice::getMapperCount() {
618 size_t count = 0;
619 for (auto& deviceEntry : mDevices) {
620 auto& devicePair = deviceEntry.second;
621 auto& mappers = devicePair.second;
622 count += mappers.size();
623 }
624 return count;
625}
626
arthurhungc903df12020-08-11 15:08:42 +0800627void InputDevice::updateLedState(bool reset) {
628 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
629}
630
Andy Chenf9f1a022022-08-29 20:07:10 -0400631std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
632 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
633}
634
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800635InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800636 : mDevice(device),
637 mContext(device.getContext()),
638 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800639 mId(eventHubId),
640 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800641
642InputDeviceContext::~InputDeviceContext() {}
643
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700644} // namespace android