blob: 4ab55cba873bd8af4211aeba70c6aa67d6c50d48 [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
38namespace android {
39
40InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080041 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042 : mContext(context),
43 mId(id),
44 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080045 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 mSources(0),
49 mIsExternal(false),
50 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080051 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080053InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054
55bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080056 if (!hasEventHubDevices()) {
57 return false;
58 }
Chris Yee14523a2020-12-19 13:46:00 -080059 // An input device composed of sub devices can be individually enabled or disabled.
60 // If any of the sub device is enabled then the input device is considered as enabled.
61 bool enabled = false;
62 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
63 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070064}
65
66void InputDevice::setEnabled(bool enabled, nsecs_t when) {
67 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
68 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
69 "but the corresponding viewport is not found",
70 getName().c_str(), *mAssociatedDisplayPort);
71 enabled = false;
72 }
73
74 if (isEnabled() == enabled) {
75 return;
76 }
77
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080078 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
79 // performed. The querying must happen when the device is enabled, so we reset after enabling
80 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080082 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 reset(when);
84 } else {
85 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080086 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 }
88 // Must change generation to flag this device as changed
89 bumpGeneration();
90}
91
Chris Yee7310032020-09-22 15:36:28 -070092void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000093 InputDeviceInfo deviceInfo = getDeviceInfo();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094
95 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
96 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -070097 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
99 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
100 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
101 if (mAssociatedDisplayPort) {
102 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
103 } else {
104 dump += "<none>\n";
105 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700106 dump += StringPrintf(INDENT2 "AssociatedDisplayUniqueId: ");
107 if (mAssociatedDisplayUniqueId) {
108 dump += StringPrintf("%s\n", mAssociatedDisplayUniqueId->c_str());
109 } else {
110 dump += "<none>\n";
111 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000113 dump += StringPrintf(INDENT2 "Sources: %s\n",
114 inputEventSourceToString(deviceInfo.getSources()).c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700116 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117
118 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
119 if (!ranges.empty()) {
120 dump += INDENT2 "Motion Ranges:\n";
121 for (size_t i = 0; i < ranges.size(); i++) {
122 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700123 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700124 char name[32];
125 if (label) {
126 strncpy(name, label, sizeof(name));
127 name[sizeof(name) - 1] = '\0';
128 } else {
129 snprintf(name, sizeof(name), "%d", range.axis);
130 }
131 dump += StringPrintf(INDENT3
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000132 "%s: source=%s, "
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
Siarhei Vishniakou88151b82022-08-11 00:53:38 +0000134 name, inputEventSourceToString(range.source).c_str(), range.min,
135 range.max, range.flat, range.fuzz, range.resolution);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136 }
137 }
138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800139 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800140 if (mController) {
141 mController->dump(dump);
142 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700143}
144
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800145void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
146 if (mDevices.find(eventHubId) != mDevices.end()) {
147 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800148 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800149 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700150 ftl::Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800151 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 // Check if we should skip population
154 if (!populateMappers) {
155 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
156 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800157 }
158
159 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700160 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800161 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800162 }
163
164 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700165 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800166 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800167 }
168
169 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700170 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800171 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800172 }
173
Chris Yee2b1e5c2021-03-10 22:45:12 -0800174 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700175 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800176 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700177 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800178 }
179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800180 // Keyboard-like devices.
181 uint32_t keyboardSource = 0;
182 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700183 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800184 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
185 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700186 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800187 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
188 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700189 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800190 keyboardSource |= AINPUT_SOURCE_DPAD;
191 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700192 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800193 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
194 }
195
196 if (keyboardSource != 0) {
197 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800198 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800199 }
200
201 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700202 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800203 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800204 }
205
206 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700207 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700209 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800210 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800211 }
212
213 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700214 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800215 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800216 }
217
Chris Yef59a2f42020-10-16 12:55:26 -0700218 // Motion sensor enabled devices.
219 if (classes.test(InputDeviceClass::SENSOR)) {
220 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
221 }
222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800223 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700224 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800225 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800226 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227
228 // insert the context into the devices set
229 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700230 // Must change generation to flag this device as changed
231 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800232}
233
234void InputDevice::removeEventHubDevice(int32_t eventHubId) {
235 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700236}
237
238void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
239 uint32_t changes) {
240 mSources = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700241 mClasses = ftl::Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800242 mControllerNumber = 0;
243
244 for_each_subdevice([this](InputDeviceContext& context) {
245 mClasses |= context.getDeviceClasses();
246 int32_t controllerNumber = context.getDeviceControllerNumber();
247 if (controllerNumber > 0) {
248 if (mControllerNumber && mControllerNumber != controllerNumber) {
249 ALOGW("InputDevice::configure(): composite device contains multiple unique "
250 "controller numbers");
251 }
252 mControllerNumber = controllerNumber;
253 }
254 });
255
Chris Ye1b0c7342020-07-28 21:57:03 -0700256 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
257 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700258
259 if (!isIgnored()) {
260 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800261 mConfiguration.clear();
262 for_each_subdevice([this](InputDeviceContext& context) {
263 PropertyMap configuration;
264 context.getConfiguration(&configuration);
265 mConfiguration.addAll(&configuration);
266 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700267 }
268
269 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700270 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700271 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800273 bool shouldBumpGeneration = false;
274 for_each_subdevice(
275 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
276 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
277 shouldBumpGeneration = true;
278 }
279 });
280 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 bumpGeneration();
282 }
283 }
284 }
285
286 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700287 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700288 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
289 if (mAlias != alias) {
290 mAlias = alias;
291 bumpGeneration();
292 }
293 }
294 }
295
296 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
297 auto it = config->disabledDevices.find(mId);
298 bool enabled = it == config->disabledDevices.end();
299 setEnabled(enabled, when);
300 }
301
302 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700303 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700304 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700305 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700306 mAssociatedViewport = std::nullopt;
307 // Find the display port that corresponds to the current input port.
308 const std::string& inputPort = mIdentifier.location;
309 if (!inputPort.empty()) {
310 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
311 const auto& displayPort = ports.find(inputPort);
312 if (displayPort != ports.end()) {
313 mAssociatedDisplayPort = std::make_optional(displayPort->second);
Christine Franks2a2293c2022-01-18 11:51:16 -0800314 } else {
315 const std::unordered_map<std::string, std::string>& displayUniqueIds =
316 config->uniqueIdAssociations;
317 const auto& displayUniqueId = displayUniqueIds.find(inputPort);
318 if (displayUniqueId != displayUniqueIds.end()) {
319 mAssociatedDisplayUniqueId = displayUniqueId->second;
320 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700321 }
322 }
323
324 // If the device was explicitly disabled by the user, it would be present in the
325 // "disabledDevices" list. If it is associated with a specific display, and it was not
326 // explicitly disabled, then enable/disable the device based on whether we can find the
327 // corresponding viewport.
328 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
329 if (mAssociatedDisplayPort) {
330 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
331 if (!mAssociatedViewport) {
332 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
333 "but the corresponding viewport is not found.",
334 getName().c_str(), *mAssociatedDisplayPort);
335 enabled = false;
336 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700337 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
338 mAssociatedViewport =
339 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
340 if (!mAssociatedViewport) {
341 ALOGW("Input device %s should be associated with display %s but the "
342 "corresponding viewport cannot be found",
Christine Franks2a2293c2022-01-18 11:51:16 -0800343 getName().c_str(), mAssociatedDisplayUniqueId->c_str());
Christine Franks1ba71cc2021-04-07 14:37:42 -0700344 enabled = false;
345 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 }
347
348 if (changes) {
349 // For first-time configuration, only allow device to be disabled after mappers have
350 // finished configuring. This is because we need to read some of the properties from
351 // the device's open fd.
352 setEnabled(enabled, when);
353 }
354 }
355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800356 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
357 mapper.configure(when, config, changes);
358 mSources |= mapper.getSources();
359 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700360
361 // If a device is just plugged but it might be disabled, we need to update some info like
362 // axis range of touch from each InputMapper first, then disable it.
363 if (!changes) {
364 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
365 }
366 }
367}
368
369void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800370 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700371
372 mContext->updateGlobalMetaState();
373
374 notifyReset(when);
375}
376
377void InputDevice::process(const RawEvent* rawEvents, size_t count) {
378 // Process all of the events in order for each mapper.
379 // We cannot simply ask each mapper to process them in bulk because mappers may
380 // have side-effects that must be interleaved. For example, joystick movement events and
381 // gamepad button presses are handled by different mappers but they should be dispatched
382 // in the order received.
383 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800384 if (DEBUG_RAW_EVENTS) {
385 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
386 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
387 rawEvent->when);
388 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700389
390 if (mDropUntilNextSync) {
391 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
392 mDropUntilNextSync = false;
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800393 if (DEBUG_RAW_EVENTS) {
394 ALOGD("Recovered from input event buffer overrun.");
395 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700396 } else {
Siarhei Vishniakou465e1c02021-12-09 10:47:29 -0800397 if (DEBUG_RAW_EVENTS) {
398 ALOGD("Dropped input event while waiting for next input sync.");
399 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700400 }
401 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
402 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
403 mDropUntilNextSync = true;
404 reset(rawEvent->when);
405 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800406 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
407 mapper.process(rawEvent);
408 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700409 }
410 --count;
411 }
412}
413
414void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800415 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700416}
417
418void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800419 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700420}
421
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000422InputDeviceInfo InputDevice::getDeviceInfo() {
423 InputDeviceInfo outDeviceInfo;
424 outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
425 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800426 for_each_mapper(
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000427 [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800428
429 if (mController) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000430 mController->populateDeviceInfo(&outDeviceInfo);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800431 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000432 return outDeviceInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433}
434
435int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
436 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
437}
438
439int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
440 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
441}
442
443int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
444 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
445}
446
447int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
448 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800449 for (auto& deviceEntry : mDevices) {
450 auto& devicePair = deviceEntry.second;
451 auto& mappers = devicePair.second;
452 for (auto& mapperPtr : mappers) {
453 InputMapper& mapper = *mapperPtr;
454 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
455 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
456 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
457 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
458 if (currentResult >= AKEY_STATE_DOWN) {
459 return currentResult;
460 } else if (currentResult == AKEY_STATE_UP) {
461 result = currentResult;
462 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 }
464 }
465 }
466 return result;
467}
468
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700469bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
470 uint8_t* outFlags) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471 bool result = false;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700472 for_each_mapper([&result, sourceMask, keyCodes, outFlags](InputMapper& mapper) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800473 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700474 result |= mapper.markSupportedKeyCodes(sourceMask, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800476 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700477 return result;
478}
479
Philip Junker4af3b3d2021-12-14 10:36:55 +0100480int32_t InputDevice::getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
481 std::optional<int32_t> result = first_in_mappers<int32_t>(
482 [locationKeyCode](const InputMapper& mapper) -> std::optional<int32_t> const {
483 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD)) {
484 return std::make_optional(mapper.getKeyCodeForKeyLocation(locationKeyCode));
485 }
486 return std::nullopt;
487 });
488 if (!result) {
489 ALOGE("Failed to get key code for key location: No matching InputMapper with source mask "
490 "KEYBOARD found. The provided input device with id %d has sources %s.",
491 getId(), inputEventSourceToString(getSources()).c_str());
492 return AKEYCODE_UNKNOWN;
493 }
494 return *result;
495}
496
Chris Ye87143712020-11-10 05:05:58 +0000497void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
498 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
499 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800500 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501}
502
503void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800504 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700505}
506
Chris Ye87143712020-11-10 05:05:58 +0000507bool InputDevice::isVibrating() {
508 bool vibrating = false;
509 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
510 return vibrating;
511}
512
513/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
514 * different vibration mappers then we could have duplicate IDs.
515 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
516 * we would definitely have duplicate IDs.
517 */
518std::vector<int32_t> InputDevice::getVibratorIds() {
519 std::vector<int32_t> vibrators;
520 for_each_mapper([&vibrators](InputMapper& mapper) {
521 std::vector<int32_t> devVibs = mapper.getVibratorIds();
522 vibrators.reserve(vibrators.size() + devVibs.size());
523 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
524 });
525 return vibrators;
526}
527
Chris Yef59a2f42020-10-16 12:55:26 -0700528bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
529 std::chrono::microseconds samplingPeriod,
530 std::chrono::microseconds maxBatchReportLatency) {
531 bool success = true;
532 for_each_mapper(
533 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
534 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
535 });
536 return success;
537}
538
539void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
540 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
541}
542
543void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
544 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
545}
546
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000547void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
548 for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700549}
550
Chris Ye3fdbfef2021-01-06 18:45:18 -0800551bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800552 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800553}
554
555bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800556 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800557}
558
559std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800560 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800561}
562
563std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800564 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800565}
566
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700567int32_t InputDevice::getMetaState() {
568 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800569 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700570 return result;
571}
572
573void InputDevice::updateMetaState(int32_t keyCode) {
Arthur Hungcb40a002021-08-03 14:31:01 +0000574 first_in_mappers<bool>([keyCode](InputMapper& mapper) {
575 if (sourcesMatchMask(mapper.getSources(), AINPUT_SOURCE_KEYBOARD) &&
576 mapper.updateMetaState(keyCode)) {
577 return std::make_optional(true);
578 }
579 return std::optional<bool>();
580 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700581}
582
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700583void InputDevice::bumpGeneration() {
584 mGeneration = mContext->bumpGeneration();
585}
586
587void InputDevice::notifyReset(nsecs_t when) {
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000588 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700589 mContext->getListener().notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700590}
591
592std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
593 // Check if we had associated to the specific display.
594 if (mAssociatedViewport) {
595 return mAssociatedViewport->displayId;
596 }
597
598 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800599 return first_in_mappers<int32_t>(
600 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700601}
602
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800603// returns the number of mappers associated with the device
604size_t InputDevice::getMapperCount() {
605 size_t count = 0;
606 for (auto& deviceEntry : mDevices) {
607 auto& devicePair = deviceEntry.second;
608 auto& mappers = devicePair.second;
609 count += mappers.size();
610 }
611 return count;
612}
613
arthurhungc903df12020-08-11 15:08:42 +0800614void InputDevice::updateLedState(bool reset) {
615 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
616}
617
Andy Chenf9f1a022022-08-29 20:07:10 -0400618std::optional<int32_t> InputDevice::getBatteryEventHubId() const {
619 return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt;
620}
621
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800623 : mDevice(device),
624 mContext(device.getContext()),
625 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 mId(eventHubId),
627 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800628
629InputDeviceContext::~InputDeviceContext() {}
630
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700631} // namespace android