blob: 9fe652ce7136f2a39d166b841d37c87dfcd7d75c [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Macros.h"
18
19#include "InputDevice.h"
20
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080021#include <algorithm>
22
Harry Cutts89844622022-12-02 15:02:26 +000023#include <android/sysprop/InputProperties.sysprop.h>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070024#include <ftl/flags.h>
25
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080026#include "CursorInputMapper.h"
27#include "ExternalStylusInputMapper.h"
28#include "InputReaderContext.h"
29#include "JoystickInputMapper.h"
30#include "KeyboardInputMapper.h"
31#include "MultiTouchInputMapper.h"
Chris Ye1dd2e5c2021-04-04 23:12:41 -070032#include "PeripheralController.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080033#include "RotaryEncoderInputMapper.h"
Chris Yef59a2f42020-10-16 12:55:26 -070034#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080035#include "SingleTouchInputMapper.h"
36#include "SwitchInputMapper.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000037#include "TouchpadInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080038#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039
40namespace android {
41
42InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080043 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 : mContext(context),
45 mId(id),
46 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080049 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 mSources(0),
51 mIsExternal(false),
52 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080055InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
57bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080058 if (!hasEventHubDevices()) {
59 return false;
60 }
Chris Yee14523a2020-12-19 13:46:00 -080061 // An input device composed of sub devices can be individually enabled or disabled.
62 // If any of the sub device is enabled then the input device is considered as enabled.
63 bool enabled = false;
64 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
65 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066}
67
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070068std::list<NotifyArgs> InputDevice::setEnabled(bool enabled, nsecs_t when) {
69 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
71 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
72 "but the corresponding viewport is not found",
73 getName().c_str(), *mAssociatedDisplayPort);
74 enabled = false;
75 }
76
77 if (isEnabled() == enabled) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 }
80
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080081 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
82 // performed. The querying must happen when the device is enabled, so we reset after enabling
83 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 for_each_subdevice([](auto& context) { context.enableDevice(); });
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 out += reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 out += reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91 // Must change generation to flag this device as changed
92 bumpGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094}
95
Chris Yee7310032020-09-22 15:36:28 -070096void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000097 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
99 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
100 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -0700101 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
103 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
104 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
105 if (mAssociatedDisplayPort) {
106 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
107 } else {
108 dump += "<none>\n";
109 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700110 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
111 if (mAssociatedDisplayUniqueId) {
112 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
113 } else {
114 dump += "<none>\n";
115 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000117 dump += StringPrintf(INDENT2 "Sources: %s\n",
118 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700120 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
122 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
123 if (!ranges.empty()) {
124 dump += INDENT2 "Motion Ranges:\n";
125 for (size_t i = 0; i < ranges.size(); i++) {
126 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700127 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 char name[32];
129 if (label) {
130 strncpy(name, label, sizeof(name));
131 name[sizeof(name) - 1] = '\0';
132 } else {
133 snprintf(name, sizeof(name), "%d", range.axis);
134 }
135 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000136 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000138 name, inputEventSourceToString(range.source).c_str(), range.min,
139 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140 }
141 }
142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800143 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800144 if (mController) {
145 mController->dump(dump);
146 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147}
148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800149void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
150 if (mDevices.find(eventHubId) != mDevices.end()) {
151 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800152 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700154 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800156
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800157 // Check if we should skip population
158 if (!populateMappers) {
159 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
160 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800161 }
162
163 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700164 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800165 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800166 }
167
168 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700169 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800170 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800171 }
172
173 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700174 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800175 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800176 }
177
Chris Yee2b1e5c2021-03-10 22:45:12 -0800178 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700179 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800180 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700181 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800182 }
183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800184 // Keyboard-like devices.
185 uint32_t keyboardSource = 0;
186 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700187 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800188 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
189 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700190 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800191 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
192 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700193 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800194 keyboardSource |= AINPUT_SOURCE_DPAD;
195 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700196 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800197 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
198 }
199
200 if (keyboardSource != 0) {
201 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800202 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800203 }
204
205 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700206 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800208 }
209
210 // Touchscreens and touchpad devices.
Harry Cutts89844622022-12-02 15:02:26 +0000211 static const bool ENABLE_TOUCHPAD_GESTURES_LIBRARY =
Harry Cuttsa6927832023-01-25 20:06:17 +0000212 sysprop::InputProperties::enable_touchpad_gestures_library().value_or(true);
Harry Cuttse9231702023-01-19 12:52:03 +0000213 // TODO(b/246587538): Fix the new touchpad stack for Sony DualShock 4 (5c4, 9cc) and DualSense
214 // (ce6) touchpads, or at least load this setting from the IDC file.
Harry Cutts86f5df62023-01-26 11:40:03 +0000215 const InputDeviceIdentifier identifier = contextPtr->getDeviceIdentifier();
Harry Cuttse9231702023-01-19 12:52:03 +0000216 const bool isSonyGamepadTouchpad = identifier.vendor == 0x054c &&
217 (identifier.product == 0x05c4 || identifier.product == 0x09cc ||
218 identifier.product == 0x0ce6);
Harry Cutts89844622022-12-02 15:02:26 +0000219 if (ENABLE_TOUCHPAD_GESTURES_LIBRARY && classes.test(InputDeviceClass::TOUCHPAD) &&
Harry Cuttse9231702023-01-19 12:52:03 +0000220 classes.test(InputDeviceClass::TOUCH_MT) && !isSonyGamepadTouchpad) {
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000221 mappers.push_back(std::make_unique<TouchpadInputMapper>(*contextPtr));
222 } else if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800223 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700224 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800225 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800226 }
227
228 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700229 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800230 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800231 }
232
Chris Yef59a2f42020-10-16 12:55:26 -0700233 // Motion sensor enabled devices.
234 if (classes.test(InputDeviceClass::SENSOR)) {
235 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
236 }
237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800238 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700239 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800240 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800241 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800242
243 // insert the context into the devices set
244 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700245 // Must change generation to flag this device as changed
246 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800247}
248
249void InputDevice::removeEventHubDevice(int32_t eventHubId) {
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -0700250 if (mController != nullptr && mController->getEventHubId() == eventHubId) {
251 // Delete mController, since the corresponding eventhub device is going away
252 mController = nullptr;
253 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800254 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255}
256
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700257std::list<NotifyArgs> InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
258 uint32_t changes) {
259 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700261 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800262 mControllerNumber = 0;
263
264 for_each_subdevice([this](InputDeviceContext& context) {
265 mClasses |= context.getDeviceClasses();
266 int32_t controllerNumber = context.getDeviceControllerNumber();
267 if (controllerNumber > 0) {
268 if (mControllerNumber && mControllerNumber != controllerNumber) {
269 ALOGW("InputDevice::configure(): composite device contains multiple unique "
270 "controller numbers");
271 }
272 mControllerNumber = controllerNumber;
273 }
274 });
275
Chris Ye1b0c7342020-07-28 21:57:03 -0700276 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
277 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700278
279 if (!isIgnored()) {
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +0000280 // Full configuration should happen the first time configure is called
281 // and when the device type is changed. Changing a device type can
282 // affect various other parameters so should result in a
283 // reconfiguration.
284 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_TYPE)) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800285 mConfiguration.clear();
286 for_each_subdevice([this](InputDeviceContext& context) {
287 PropertyMap configuration;
288 context.getConfiguration(&configuration);
289 mConfiguration.addAll(&configuration);
290 });
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +0000291
292 mAssociatedDeviceType =
293 getValueByKey(config->deviceTypeAssociations, mIdentifier.location);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700294 }
295
296 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700297 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700298 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700299 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800300 bool shouldBumpGeneration = false;
301 for_each_subdevice(
302 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
303 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
304 shouldBumpGeneration = true;
305 }
306 });
307 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308 bumpGeneration();
309 }
310 }
311 }
312
313 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700314 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700315 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
316 if (mAlias != alias) {
317 mAlias = alias;
318 bumpGeneration();
319 }
320 }
321 }
322
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700323 if (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE) {
324 // Do not execute this code on the first configure, because 'setEnabled' would call
325 // InputMapper::reset, and you can't reset a mapper before it has been configured.
326 // The mappers are configured for the first time at the bottom of this function.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700327 auto it = config->disabledDevices.find(mId);
328 bool enabled = it == config->disabledDevices.end();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700329 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330 }
331
332 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700334 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700335 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336 mAssociatedViewport = std::nullopt;
337 // Find the display port that corresponds to the current input port.
338 const std::string& inputPort = mIdentifier.location;
339 if (!inputPort.empty()) {
340 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
341 const auto& displayPort = ports.find(inputPort);
342 if (displayPort != ports.end()) {
343 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800344 } else {
345 const std::unordered_map<std::string, std::string>& displayUniqueIds =
346 config->uniqueIdAssociations;
347 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
348 if (displayUniqueId != displayUniqueIds.end()) {
349 mAssociatedDisplayUniqueId = displayUniqueId->second;
350 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700351 }
352 }
353
354 // If the device was explicitly disabled by the user, it would be present in the
355 // "disabledDevices" list. If it is associated with a specific display, and it was not
356 // explicitly disabled, then enable/disable the device based on whether we can find the
357 // corresponding viewport.
358 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
359 if (mAssociatedDisplayPort) {
360 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
361 if (!mAssociatedViewport) {
362 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
363 "but the corresponding viewport is not found.",
364 getName().c_str(), *mAssociatedDisplayPort);
365 enabled = false;
366 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700367 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
368 mAssociatedViewport =
369 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
370 if (!mAssociatedViewport) {
371 ALOGW("Input device %s should be associated with display %s but the "
372 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800373 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700374 enabled = false;
375 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700376 }
377
378 if (changes) {
379 // For first-time configuration, only allow device to be disabled after mappers have
380 // finished configuring. This is because we need to read some of the properties from
381 // the device's open fd.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700382 out += setEnabled(enabled, when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700383 }
384 }
385
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700386 for_each_mapper([this, when, &config, changes, &out](InputMapper& mapper) {
387 out += mapper.configure(when, config, changes);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800388 mSources |= mapper.getSources();
389 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700390
391 // If a device is just plugged but it might be disabled, we need to update some info like
392 // axis range of touch from each InputMapper first, then disable it.
393 if (!changes) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700394 out += setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(),
395 when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700396 }
397 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700398 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700399}
400
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700401std::list<NotifyArgs> InputDevice::reset(nsecs_t when) {
402 std::list<NotifyArgs> out;
403 for_each_mapper([&](InputMapper& mapper) { out += mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700404
405 mContext->updateGlobalMetaState();
406
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700407 out.push_back(notifyReset(when));
408 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409}
410
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700411std::list<NotifyArgs> InputDevice::process(const RawEvent* rawEvents, size_t count) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700412 // Process all of the events in order for each mapper.
413 // We cannot simply ask each mapper to process them in bulk because mappers may
414 // have side-effects that must be interleaved. For example, joystick movement events and
415 // gamepad button presses are handled by different mappers but they should be dispatched
416 // in the order received.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700417 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700418 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Prabir Pradhan011ca3d2023-02-22 21:31:39 +0000419 if (debugRawEvents()) {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000420 const auto [type, code, value] =
421 InputEventLookup::getLinuxEvdevLabel(rawEvent->type, rawEvent->code,
422 rawEvent->value);
423 ALOGD("Input event: eventHubDevice=%d type=%s code=%s value=%s when=%" PRId64,
424 rawEvent->deviceId, type.c_str(), code.c_str(), value.c_str(), rawEvent->when);
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800425 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700426
427 if (mDropUntilNextSync) {
428 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
429 mDropUntilNextSync = false;
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000430 ALOGD_IF(debugRawEvents(), "Recovered from input event buffer overrun.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700431 } else {
Prabir Pradhan1e63fc22023-02-23 19:03:03 +0000432 ALOGD_IF(debugRawEvents(),
433 "Dropped input event while waiting for next input sync.");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
435 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
436 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
437 mDropUntilNextSync = true;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700438 out += reset(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700439 } else {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700440 for_each_mapper_in_subdevice(rawEvent->deviceId, [&](InputMapper& mapper) {
441 out += mapper.process(rawEvent);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800442 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443 }
444 --count;
445 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700446 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447}
448
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700449std::list<NotifyArgs> InputDevice::timeoutExpired(nsecs_t when) {
450 std::list<NotifyArgs> out;
451 for_each_mapper([&](InputMapper& mapper) { out += mapper.timeoutExpired(when); });
452 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700453}
454
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700455std::list<NotifyArgs> InputDevice::updateExternalStylusState(const StylusState& state) {
456 std::list<NotifyArgs> out;
457 for_each_mapper([&](InputMapper& mapper) { out += mapper.updateExternalStylusState(state); });
458 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459}
460
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000461InputDeviceInfo InputDevice::getDeviceInfo() {
462 InputDeviceInfo outDeviceInfo;
463 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000464 mHasMic, getAssociatedDisplayId().value_or(ADISPLAY_ID_NONE));
465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800466 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000467 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800468
469 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000470 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800471 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000472 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473}
474
475int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
476 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
477}
478
479int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
480 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
481}
482
483int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
484 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
485}
486
487int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
488 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800489 for (auto& deviceEntry : mDevices) {
490 auto& devicePair = deviceEntry.second;
491 auto& mappers = devicePair.second;
492 for (auto& mapperPtr : mappers) {
493 InputMapper& mapper = *mapperPtr;
494 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
495 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
496 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
497 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
498 if (currentResult >= AKEY_STATE_DOWN) {
499 return currentResult;
500 } else if (currentResult == AKEY_STATE_UP) {
501 result = currentResult;
502 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700503 }
504 }
505 }
506 return result;
507}
508
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700509bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
510 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700511 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700512 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800513 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700514 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700515 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800516 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700517 return result;
518}
519
Philip Junker4af3b3d2021-12-14 10:36:55 +0100520int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
521 std::optional<int32_t> result = first_in_mappers<int32_t>(
522 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
523 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
524 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
525 }
526 return std::nullopt;
527 });
528 if (!result) {
529 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
530 "KEYBOARD found. The provided input device with id %d has sources %s.",
531 getId(), inputEventSourceToString(getSources()).c_str());
532 return AKEYCODE_UNKNOWN;
533 }
534 return *result;
535}
536
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700537std::list<NotifyArgs> InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat,
538 int32_t token) {
539 std::list<NotifyArgs> out;
540 for_each_mapper([&](InputMapper& mapper) { out += mapper.vibrate(sequence, repeat, token); });
541 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700542}
543
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700544std::list<NotifyArgs> InputDevice::cancelVibrate(int32_t token) {
545 std::list<NotifyArgs> out;
546 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelVibrate(token); });
547 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700548}
549
Chris Ye87143712020-11-10 05:05:58 +0000550bool InputDevice::isVibrating() {
551 bool vibrating = false;
552 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
553 return vibrating;
554}
555
556/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
557 * different vibration mappers then we could have duplicate IDs.
558 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
559 * we would definitely have duplicate IDs.
560 */
561std::vector<int32_t> InputDevice::getVibratorIds() {
562 std::vector<int32_t> vibrators;
563 for_each_mapper([&vibrators](InputMapper& mapper) {
564 std::vector<int32_t> devVibs = mapper.getVibratorIds();
565 vibrators.reserve(vibrators.size() + devVibs.size());
566 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
567 });
568 return vibrators;
569}
570
Chris Yef59a2f42020-10-16 12:55:26 -0700571bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
572 std::chrono::microseconds samplingPeriod,
573 std::chrono::microseconds maxBatchReportLatency) {
574 bool success = true;
575 for_each_mapper(
576 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
577 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
578 });
579 return success;
580}
581
582void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
583 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
584}
585
586void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
587 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
588}
589
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700590std::list<NotifyArgs> InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
591 std::list<NotifyArgs> out;
592 for_each_mapper([&](InputMapper& mapper) { out += mapper.cancelTouch(when, readTime); });
593 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700594}
595
Chris Ye3fdbfef2021-01-06 18:45:18 -0800596bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800597 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800598}
599
600bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800601 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800602}
603
604std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800605 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800606}
607
608std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800609 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800610}
611
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700612int32_t InputDevice::getMetaState() {
613 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800614 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700615 return result;
616}
617
618void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000619 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
620 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
621 mapper.updateMetaState(keyCode)) {
622 return std::make_optional(true);
623 }
624 return std::optional<bool>();
625 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700626}
627
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000628void InputDevice::addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) {
629 for_each_subdevice([fromKeyCode, toKeyCode](auto& context) {
630 context.addKeyRemapping(fromKeyCode, toKeyCode);
631 });
632}
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