blob: 8d5377fc737634eb0830363d44a42bbac54a785a [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Macros.h"
18
19#include "InputDevice.h"
20
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080021#include <algorithm>
22
Harry Cutts89844622022-12-02 15:02:26 +000023#if defined(__ANDROID__)
24#include <android/sysprop/InputProperties.sysprop.h>
25#endif
Dominik Laskowski2f01d772022-03-23 16:01:29 -070026#include <ftl/flags.h>
27
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080028#include "CursorInputMapper.h"
29#include "ExternalStylusInputMapper.h"
30#include "InputReaderContext.h"
31#include "JoystickInputMapper.h"
32#include "KeyboardInputMapper.h"
33#include "MultiTouchInputMapper.h"
Chris Ye1dd2e5c2021-04-04 23:12:41 -070034#include "PeripheralController.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "RotaryEncoderInputMapper.h"
Chris Yef59a2f42020-10-16 12:55:26 -070036#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080037#include "SingleTouchInputMapper.h"
38#include "SwitchInputMapper.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000039#include "TouchpadInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080040#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000042using android::hardware::input::InputDeviceCountryCode;
43
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044namespace android {
45
46InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 : mContext(context),
49 mId(id),
50 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080051 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054 mSources(0),
55 mIsExternal(false),
56 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080057 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080059InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060
61bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080062 if (!hasEventHubDevices()) {
63 return false;
64 }
Chris Yee14523a2020-12-19 13:46:00 -080065 // An input device composed of sub devices can be individually enabled or disabled.
66 // If any of the sub device is enabled then the input device is considered as enabled.
67 bool enabled = false;
68 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
69 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070}
71
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070072std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
73 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
75 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
76 "but the corresponding viewport is not found",
77 getName().c_str(), *mAssociatedDisplayPort);
78 enabled = false;
79 }
80
81 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070082 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 }
84
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
86 // performed. The querying must happen when the device is enabled, so we reset after enabling
87 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070088 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070090 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070092 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080093 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 }
95 // Must change generation to flag this device as changed
96 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070097 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098}
99
Chris Yee7310032020-09-22 15:36:28 -0700100void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000101 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102
103 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
104 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700105 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
107 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
108 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
109 if (mAssociatedDisplayPort) {
110 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
111 } else {
112 dump += "<none>\n";
113 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700114 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
115 if (mAssociatedDisplayUniqueId) {
116 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
117 } else {
118 dump += "<none>\n";
119 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000121 dump += StringPrintf(INDENT2 "Sources: %s\n",
122 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700124 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700125
126 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
127 if (!ranges.empty()) {
128 dump += INDENT2 "Motion Ranges:\n";
129 for (size_t i = 0; i < ranges.size(); i++) {
130 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700131 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132 char name[32];
133 if (label) {
134 strncpy(name, label, sizeof(name));
135 name[sizeof(name) - 1] = '\0';
136 } else {
137 snprintf(name, sizeof(name), "%d", range.axis);
138 }
139 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000140 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000142 name, inputEventSourceToString(range.source).c_str(), range.min,
143 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700144 }
145 }
146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800147 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800148 if (mController) {
149 mController->dump(dump);
150 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700151}
152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
154 if (mDevices.find(eventHubId) != mDevices.end()) {
155 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800156 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800157 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700158 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800159 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800161 // Check if we should skip population
162 if (!populateMappers) {
163 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
164 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800165 }
166
167 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700168 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800169 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800170 }
171
172 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700173 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800174 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800175 }
176
177 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700178 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800179 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800180 }
181
Chris Yee2b1e5c2021-03-10 22:45:12 -0800182 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700183 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800184 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700185 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800186 }
187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800188 // Keyboard-like devices.
189 uint32_t keyboardSource = 0;
190 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700191 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
193 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700194 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800195 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
196 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700197 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800198 keyboardSource |= AINPUT_SOURCE_DPAD;
199 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700200 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800201 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
202 }
203
204 if (keyboardSource != 0) {
205 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800206 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800207 }
208
209 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700210 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800211 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800212 }
213
214 // Touchscreens and touchpad devices.
Harry Cutts89844622022-12-02 15:02:26 +0000215 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
216#if defined(__ANDROID__)
217 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(false);
218#else
219 false;
220#endif
221 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
Harry Cutts1f48a442022-11-15 17:38:36 +0000222 classes.test(InputDeviceClass::TOUCH_MT)) {
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000223 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
224 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800225 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700226 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800227 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800228 }
229
230 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700231 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800232 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800233 }
234
Chris Yef59a2f42020-10-16 12:55:26 -0700235 // Motion sensor enabled devices.
236 if (classes.test(InputDeviceClass::SENSOR)) {
237 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
238 }
239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800240 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700241 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800242 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800243 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244
245 // insert the context into the devices set
246 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700247 // Must change generation to flag this device as changed
248 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800249}
250
251void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700252 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
253 // Delete mController, since the corresponding eventhub device is going away
254 mController = nullptr;
255 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800256 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257}
258
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700259std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
260 uint32_t changes) {
261 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700262 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700263 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800264 mControllerNumber = 0;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000265 mCountryCode = InputDeviceCountryCode::INVALID;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266
267 for_each_subdevice([this](InputDeviceContext& context) {
268 mClasses |= context.getDeviceClasses();
269 int32_t controllerNumber = context.getDeviceControllerNumber();
270 if (controllerNumber > 0) {
271 if (mControllerNumber && mControllerNumber != controllerNumber) {
272 ALOGW("InputDevice::configure(): composite device contains multiple unique "
273 "controller numbers");
274 }
275 mControllerNumber = controllerNumber;
276 }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000277
278 InputDeviceCountryCode countryCode = context.getCountryCode();
279 if (countryCode != InputDeviceCountryCode::INVALID) {
280 if (mCountryCode != InputDeviceCountryCode::INVALID && mCountryCode != countryCode) {
281 ALOGW("InputDevice::configure(): %s device contains multiple unique country "
282 "codes",
283 getName().c_str());
284 }
285 mCountryCode = countryCode;
286 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800287 });
288
Chris Ye1b0c7342020-07-28 21:57:03 -0700289 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
290 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291
292 if (!isIgnored()) {
293 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800294 mConfiguration.clear();
295 for_each_subdevice([this](InputDeviceContext& context) {
296 PropertyMap configuration;
297 context.getConfiguration(&configuration);
298 mConfiguration.addAll(&configuration);
299 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700300 }
301
302 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700303 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700304 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800306 bool shouldBumpGeneration = false;
307 for_each_subdevice(
308 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
309 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
310 shouldBumpGeneration = true;
311 }
312 });
313 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 bumpGeneration();
315 }
316 }
317 }
318
319 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700320 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
322 if (mAlias != alias) {
323 mAlias = alias;
324 bumpGeneration();
325 }
326 }
327 }
328
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700329 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
330 // Do not execute this code on the first configure, because 'setEnabled' would call
331 // InputMapper::reset, and you can't reset a mapper before it has been configured.
332 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700333 auto it = config->disabledDevices.find(mId);
334 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700335 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 }
337
338 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700339 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700340 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700341 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700342 mAssociatedViewport = std::nullopt;
343 // Find the display port that corresponds to the current input port.
344 const std::string& inputPort = mIdentifier.location;
345 if (!inputPort.empty()) {
346 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
347 const auto& displayPort = ports.find(inputPort);
348 if (displayPort != ports.end()) {
349 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800350 } else {
351 const std::unordered_map<std::string, std::string>& displayUniqueIds =
352 config->uniqueIdAssociations;
353 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
354 if (displayUniqueId != displayUniqueIds.end()) {
355 mAssociatedDisplayUniqueId = displayUniqueId->second;
356 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700357 }
358 }
359
360 // If the device was explicitly disabled by the user, it would be present in the
361 // "disabledDevices" list. If it is associated with a specific display, and it was not
362 // explicitly disabled, then enable/disable the device based on whether we can find the
363 // corresponding viewport.
364 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
365 if (mAssociatedDisplayPort) {
366 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
367 if (!mAssociatedViewport) {
368 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
369 "but the corresponding viewport is not found.",
370 getName().c_str(), *mAssociatedDisplayPort);
371 enabled = false;
372 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700373 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
374 mAssociatedViewport =
375 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
376 if (!mAssociatedViewport) {
377 ALOGW("Input device %s should be associated with display %s but the "
378 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800379 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700380 enabled = false;
381 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700382 }
383
384 if (changes) {
385 // For first-time configuration, only allow device to be disabled after mappers have
386 // finished configuring. This is because we need to read some of the properties from
387 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700388 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389 }
390 }
391
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700392 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
393 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800394 mSources |= mapper.getSources();
395 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700396
397 // If a device is just plugged but it might be disabled, we need to update some info like
398 // axis range of touch from each InputMapper first, then disable it.
399 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700400 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
401 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700402 }
403 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700404 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700405}
406
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700407std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
408 std::list<NotifyArgs> out;
409 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700410
411 mContext->updateGlobalMetaState();
412
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700413 out.push_back(notifyReset(when));
414 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415}
416
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700417std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700418 // Process all of the events in order for each mapper.
419 // We cannot simply ask each mapper to process them in bulk because mappers may
420 // have side-effects that must be interleaved. For example, joystick movement events and
421 // gamepad button presses are handled by different mappers but they should be dispatched
422 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700423 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700424 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800425 if (DEBUG_RAW_EVENTS) {
426 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
427 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
428 rawEvent->when);
429 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700430
431 if (mDropUntilNextSync) {
432 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
433 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800434 if (DEBUG_RAW_EVENTS) {
435 ALOGD("Recovered from input event buffer overrun.");
436 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800438 if (DEBUG_RAW_EVENTS) {
439 ALOGD("Dropped input event while waiting for next input sync.");
440 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441 }
442 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
443 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
444 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700445 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700447 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
448 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800449 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450 }
451 --count;
452 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700453 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700454}
455
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700456std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
457 std::list<NotifyArgs> out;
458 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
459 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700460}
461
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700462std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
463 std::list<NotifyArgs> out;
464 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
465 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700466}
467
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000468InputDeviceInfo InputDevice::getDeviceInfo() {
469 InputDeviceInfo outDeviceInfo;
470 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000471 mHasMic, mCountryCode);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800472 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000473 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800474
475 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000476 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800477 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000478 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700479}
480
481int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
482 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
483}
484
485int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
486 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
487}
488
489int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
490 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
491}
492
493int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
494 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800495 for (auto& deviceEntry : mDevices) {
496 auto& devicePair = deviceEntry.second;
497 auto& mappers = devicePair.second;
498 for (auto& mapperPtr : mappers) {
499 InputMapper& mapper = *mapperPtr;
500 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
501 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
502 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
503 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
504 if (currentResult >= AKEY_STATE_DOWN) {
505 return currentResult;
506 } else if (currentResult == AKEY_STATE_UP) {
507 result = currentResult;
508 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700509 }
510 }
511 }
512 return result;
513}
514
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700515bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
516 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700517 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700518 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800519 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700520 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700521 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800522 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700523 return result;
524}
525
Philip Junker4af3b3d2021-12-14 10:36:55 +0100526int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
527 std::optional<int32_t> result = first_in_mappers<int32_t>(
528 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
529 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
530 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
531 }
532 return std::nullopt;
533 });
534 if (!result) {
535 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
536 "KEYBOARD found. The provided input device with id %d has sources %s.",
537 getId(), inputEventSourceToString(getSources()).c_str());
538 return AKEYCODE_UNKNOWN;
539 }
540 return *result;
541}
542
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700543std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
544 int32_t token) {
545 std::list<NotifyArgs> out;
546 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
547 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700548}
549
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700550std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
551 std::list<NotifyArgs> out;
552 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
553 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700554}
555
Chris Ye87143712020-11-10 05:05:58 +0000556bool InputDevice::isVibrating() {
557 bool vibrating = false;
558 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
559 return vibrating;
560}
561
562/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
563 * different vibration mappers then we could have duplicate IDs.
564 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
565 * we would definitely have duplicate IDs.
566 */
567std::vector<int32_t> InputDevice::getVibratorIds() {
568 std::vector<int32_t> vibrators;
569 for_each_mapper([&vibrators](InputMapper& mapper) {
570 std::vector<int32_t> devVibs = mapper.getVibratorIds();
571 vibrators.reserve(vibrators.size() + devVibs.size());
572 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
573 });
574 return vibrators;
575}
576
Chris Yef59a2f42020-10-16 12:55:26 -0700577bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
578 std::chrono::microseconds samplingPeriod,
579 std::chrono::microseconds maxBatchReportLatency) {
580 bool success = true;
581 for_each_mapper(
582 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
583 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
584 });
585 return success;
586}
587
588void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
589 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
590}
591
592void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
593 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
594}
595
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700596std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
597 std::list<NotifyArgs> out;
598 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
599 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700600}
601
Chris Ye3fdbfef2021-01-06 18:45:18 -0800602bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800603 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800604}
605
606bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800607 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800608}
609
610std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800611 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800612}
613
614std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800615 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800616}
617
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700618int32_t InputDevice::getMetaState() {
619 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800620 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700621 return result;
622}
623
624void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000625 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
626 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
627 mapper.updateMetaState(keyCode)) {
628 return std::make_optional(true);
629 }
630 return std::optional<bool>();
631 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700632}
633
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700634void InputDevice::bumpGeneration() {
635 mGeneration = mContext->bumpGeneration();
636}
637
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700638NotifyDeviceResetArgs InputDevice::notifyReset(nsecs_t when) {
639 return NotifyDeviceResetArgs(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700640}
641
642std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
643 // Check if we had associated to the specific display.
644 if (mAssociatedViewport) {
645 return mAssociatedViewport->displayId;
646 }
647
648 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800649 return first_in_mappers<int32_t>(
650 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700651}
652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800653// returns the number of mappers associated with the device
654size_t InputDevice::getMapperCount() {
655 size_t count = 0;
656 for (auto& deviceEntry : mDevices) {
657 auto& devicePair = deviceEntry.second;
658 auto& mappers = devicePair.second;
659 count += mappers.size();
660 }
661 return count;
662}
663
arthurhungc903df12020-08-11 15:08:42 +0800664void InputDevice::updateLedState(bool reset) {
665 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
666}
667
Andy Chenf9f1a022022-08-29 20:07:10 -0400668std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
669 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
670}
671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800672InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800673 : mDevice(device),
674 mContext(device.getContext()),
675 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800676 mId(eventHubId),
677 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800678
679InputDeviceContext::~InputDeviceContext() {}
680
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700681} // namespace android