blob: ad503fde99d849cf60fca76784b9e5e95b395725 [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
Chris Ye1b0c7342020-07-28 21:57:03 -070021#include <input/Flags.h>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080022#include <algorithm>
23
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080024#include "CursorInputMapper.h"
25#include "ExternalStylusInputMapper.h"
26#include "InputReaderContext.h"
27#include "JoystickInputMapper.h"
28#include "KeyboardInputMapper.h"
29#include "MultiTouchInputMapper.h"
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include "PeripheralController.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080031#include "RotaryEncoderInputMapper.h"
Chris Yef59a2f42020-10-16 12:55:26 -070032#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080033#include "SingleTouchInputMapper.h"
34#include "SwitchInputMapper.h"
35#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036
37namespace android {
38
39InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080040 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041 : mContext(context),
42 mId(id),
43 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080044 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080046 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070047 mSources(0),
48 mIsExternal(false),
49 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080050 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080052InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053
54bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080055 if (!hasEventHubDevices()) {
56 return false;
57 }
Chris Yee14523a2020-12-19 13:46:00 -080058 // An input device composed of sub devices can be individually enabled or disabled.
59 // If any of the sub device is enabled then the input device is considered as enabled.
60 bool enabled = false;
61 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
62 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063}
64
65void InputDevice::setEnabled(bool enabled, nsecs_t when) {
66 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
67 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
68 "but the corresponding viewport is not found",
69 getName().c_str(), *mAssociatedDisplayPort);
70 enabled = false;
71 }
72
73 if (isEnabled() == enabled) {
74 return;
75 }
76
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080077 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
78 // performed. The querying must happen when the device is enabled, so we reset after enabling
79 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080081 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082 reset(when);
83 } else {
84 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080085 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086 }
87 // Must change generation to flag this device as changed
88 bumpGeneration();
89}
90
Chris Yee7310032020-09-22 15:36:28 -070091void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092 InputDeviceInfo deviceInfo;
93 getDeviceInfo(&deviceInfo);
94
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));
113 dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
114 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700115 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116
117 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
118 if (!ranges.empty()) {
119 dump += INDENT2 "Motion Ranges:\n";
120 for (size_t i = 0; i < ranges.size(); i++) {
121 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700122 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700123 char name[32];
124 if (label) {
125 strncpy(name, label, sizeof(name));
126 name[sizeof(name) - 1] = '\0';
127 } else {
128 snprintf(name, sizeof(name), "%d", range.axis);
129 }
130 dump += StringPrintf(INDENT3
131 "%s: source=0x%08x, "
132 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
133 name, range.source, range.min, range.max, range.flat, range.fuzz,
134 range.resolution);
135 }
136 }
137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800138 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800139 if (mController) {
140 mController->dump(dump);
141 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142}
143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800144void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
145 if (mDevices.find(eventHubId) != mDevices.end()) {
146 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800147 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800148 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Chris Ye1b0c7342020-07-28 21:57:03 -0700149 Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800150 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800151
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800152 // Check if we should skip population
153 if (!populateMappers) {
154 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
155 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800156 }
157
158 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700159 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800160 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800161 }
162
163 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700164 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800165 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800166 }
167
168 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700169 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800170 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800171 }
172
Chris Yee2b1e5c2021-03-10 22:45:12 -0800173 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700174 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800175 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700176 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800177 }
178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800179 // Keyboard-like devices.
180 uint32_t keyboardSource = 0;
181 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700182 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800183 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
184 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700185 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
187 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700188 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800189 keyboardSource |= AINPUT_SOURCE_DPAD;
190 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700191 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
193 }
194
195 if (keyboardSource != 0) {
196 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800197 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800198 }
199
200 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700201 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800202 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800203 }
204
205 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700206 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700208 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800209 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800210 }
211
212 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700213 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800214 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800215 }
216
Chris Yef59a2f42020-10-16 12:55:26 -0700217 // Motion sensor enabled devices.
218 if (classes.test(InputDeviceClass::SENSOR)) {
219 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
220 }
221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800222 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700223 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800224 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800225 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800226
227 // insert the context into the devices set
228 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700229 // Must change generation to flag this device as changed
230 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800231}
232
233void InputDevice::removeEventHubDevice(int32_t eventHubId) {
234 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700235}
236
237void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
238 uint32_t changes) {
239 mSources = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -0700240 mClasses = Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800241 mControllerNumber = 0;
242
243 for_each_subdevice([this](InputDeviceContext& context) {
244 mClasses |= context.getDeviceClasses();
245 int32_t controllerNumber = context.getDeviceControllerNumber();
246 if (controllerNumber > 0) {
247 if (mControllerNumber && mControllerNumber != controllerNumber) {
248 ALOGW("InputDevice::configure(): composite device contains multiple unique "
249 "controller numbers");
250 }
251 mControllerNumber = controllerNumber;
252 }
253 });
254
Chris Ye1b0c7342020-07-28 21:57:03 -0700255 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
256 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257
258 if (!isIgnored()) {
259 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800260 mConfiguration.clear();
261 for_each_subdevice([this](InputDeviceContext& context) {
262 PropertyMap configuration;
263 context.getConfiguration(&configuration);
264 mConfiguration.addAll(&configuration);
265 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700266 }
267
268 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700269 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700270 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800272 bool shouldBumpGeneration = false;
273 for_each_subdevice(
274 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
275 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
276 shouldBumpGeneration = true;
277 }
278 });
279 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700280 bumpGeneration();
281 }
282 }
283 }
284
285 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700286 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700287 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
288 if (mAlias != alias) {
289 mAlias = alias;
290 bumpGeneration();
291 }
292 }
293 }
294
295 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
296 auto it = config->disabledDevices.find(mId);
297 bool enabled = it == config->disabledDevices.end();
298 setEnabled(enabled, when);
299 }
300
301 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Christine Franks1ba71cc2021-04-07 14:37:42 -0700302 // In most situations, no port or name will be specified.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700303 mAssociatedDisplayPort = std::nullopt;
Christine Franks1ba71cc2021-04-07 14:37:42 -0700304 mAssociatedDisplayUniqueId = std::nullopt;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 mAssociatedViewport = std::nullopt;
306 // Find the display port that corresponds to the current input port.
307 const std::string& inputPort = mIdentifier.location;
308 if (!inputPort.empty()) {
309 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
310 const auto& displayPort = ports.find(inputPort);
311 if (displayPort != ports.end()) {
312 mAssociatedDisplayPort = std::make_optional(displayPort->second);
313 }
314 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700315 const std::string& inputDeviceName = mIdentifier.name;
316 const std::unordered_map<std::string, std::string>& names =
317 config->uniqueIdAssociations;
318 const auto& displayUniqueId = names.find(inputDeviceName);
319 if (displayUniqueId != names.end()) {
320 mAssociatedDisplayUniqueId = displayUniqueId->second;
321 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700322
323 // If the device was explicitly disabled by the user, it would be present in the
324 // "disabledDevices" list. If it is associated with a specific display, and it was not
325 // explicitly disabled, then enable/disable the device based on whether we can find the
326 // corresponding viewport.
327 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
328 if (mAssociatedDisplayPort) {
329 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
330 if (!mAssociatedViewport) {
331 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
332 "but the corresponding viewport is not found.",
333 getName().c_str(), *mAssociatedDisplayPort);
334 enabled = false;
335 }
Christine Franks1ba71cc2021-04-07 14:37:42 -0700336 } else if (mAssociatedDisplayUniqueId != std::nullopt) {
337 mAssociatedViewport =
338 config->getDisplayViewportByUniqueId(*mAssociatedDisplayUniqueId);
339 if (!mAssociatedViewport) {
340 ALOGW("Input device %s should be associated with display %s but the "
341 "corresponding viewport cannot be found",
342 inputDeviceName.c_str(), mAssociatedDisplayUniqueId->c_str());
343 enabled = false;
344 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700345 }
346
347 if (changes) {
348 // For first-time configuration, only allow device to be disabled after mappers have
349 // finished configuring. This is because we need to read some of the properties from
350 // the device's open fd.
351 setEnabled(enabled, when);
352 }
353 }
354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800355 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
356 mapper.configure(when, config, changes);
357 mSources |= mapper.getSources();
358 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700359
360 // If a device is just plugged but it might be disabled, we need to update some info like
361 // axis range of touch from each InputMapper first, then disable it.
362 if (!changes) {
363 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
364 }
365 }
366}
367
368void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800369 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700370
371 mContext->updateGlobalMetaState();
372
373 notifyReset(when);
374}
375
376void InputDevice::process(const RawEvent* rawEvents, size_t count) {
377 // Process all of the events in order for each mapper.
378 // We cannot simply ask each mapper to process them in bulk because mappers may
379 // have side-effects that must be interleaved. For example, joystick movement events and
380 // gamepad button presses are handled by different mappers but they should be dispatched
381 // in the order received.
382 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
383#if DEBUG_RAW_EVENTS
384 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
385 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when);
386#endif
387
388 if (mDropUntilNextSync) {
389 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
390 mDropUntilNextSync = false;
391#if DEBUG_RAW_EVENTS
392 ALOGD("Recovered from input event buffer overrun.");
393#endif
394 } else {
395#if DEBUG_RAW_EVENTS
396 ALOGD("Dropped input event while waiting for next input sync.");
397#endif
398 }
399 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
400 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
401 mDropUntilNextSync = true;
402 reset(rawEvent->when);
403 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800404 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
405 mapper.process(rawEvent);
406 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700407 }
408 --count;
409 }
410}
411
412void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800413 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700414}
415
416void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800417 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700418}
419
420void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
421 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
422 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800423 for_each_mapper(
424 [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800425
426 if (mController) {
427 mController->populateDeviceInfo(outDeviceInfo);
428 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429}
430
431int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
432 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
433}
434
435int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
436 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
437}
438
439int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
440 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
441}
442
443int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
444 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800445 for (auto& deviceEntry : mDevices) {
446 auto& devicePair = deviceEntry.second;
447 auto& mappers = devicePair.second;
448 for (auto& mapperPtr : mappers) {
449 InputMapper& mapper = *mapperPtr;
450 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
451 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
452 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
453 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
454 if (currentResult >= AKEY_STATE_DOWN) {
455 return currentResult;
456 } else if (currentResult == AKEY_STATE_UP) {
457 result = currentResult;
458 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700459 }
460 }
461 }
462 return result;
463}
464
465bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
466 const int32_t* keyCodes, uint8_t* outFlags) {
467 bool result = false;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800468 for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) {
469 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
470 result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700471 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800472 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700473 return result;
474}
475
Chris Ye87143712020-11-10 05:05:58 +0000476void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
477 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
478 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800479 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700480}
481
482void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800483 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700484}
485
Chris Ye87143712020-11-10 05:05:58 +0000486bool InputDevice::isVibrating() {
487 bool vibrating = false;
488 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
489 return vibrating;
490}
491
492/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
493 * different vibration mappers then we could have duplicate IDs.
494 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
495 * we would definitely have duplicate IDs.
496 */
497std::vector<int32_t> InputDevice::getVibratorIds() {
498 std::vector<int32_t> vibrators;
499 for_each_mapper([&vibrators](InputMapper& mapper) {
500 std::vector<int32_t> devVibs = mapper.getVibratorIds();
501 vibrators.reserve(vibrators.size() + devVibs.size());
502 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
503 });
504 return vibrators;
505}
506
Chris Yef59a2f42020-10-16 12:55:26 -0700507bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
508 std::chrono::microseconds samplingPeriod,
509 std::chrono::microseconds maxBatchReportLatency) {
510 bool success = true;
511 for_each_mapper(
512 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
513 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
514 });
515 return success;
516}
517
518void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
519 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
520}
521
522void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
523 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
524}
525
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000526void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
527 for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700528}
529
Kim Low03ea0352020-11-06 12:45:07 -0800530std::optional<int32_t> InputDevice::getBatteryCapacity() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800531 return mController ? mController->getBatteryCapacity(DEFAULT_BATTERY_ID) : std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800532}
533
534std::optional<int32_t> InputDevice::getBatteryStatus() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800535 return mController ? mController->getBatteryStatus(DEFAULT_BATTERY_ID) : std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800536}
537
Chris Ye3fdbfef2021-01-06 18:45:18 -0800538bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800539 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800540}
541
542bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800543 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800544}
545
546std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800547 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800548}
549
550std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800551 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800552}
553
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700554int32_t InputDevice::getMetaState() {
555 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800556 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700557 return result;
558}
559
560void InputDevice::updateMetaState(int32_t keyCode) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800561 for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700562}
563
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700564void InputDevice::bumpGeneration() {
565 mGeneration = mContext->bumpGeneration();
566}
567
568void InputDevice::notifyReset(nsecs_t when) {
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000569 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
570 mContext->getListener()->notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700571}
572
573std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
574 // Check if we had associated to the specific display.
575 if (mAssociatedViewport) {
576 return mAssociatedViewport->displayId;
577 }
578
579 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800580 return first_in_mappers<int32_t>(
581 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700582}
583
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800584// returns the number of mappers associated with the device
585size_t InputDevice::getMapperCount() {
586 size_t count = 0;
587 for (auto& deviceEntry : mDevices) {
588 auto& devicePair = deviceEntry.second;
589 auto& mappers = devicePair.second;
590 count += mappers.size();
591 }
592 return count;
593}
594
arthurhungc903df12020-08-11 15:08:42 +0800595void InputDevice::updateLedState(bool reset) {
596 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
597}
598
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800600 : mDevice(device),
601 mContext(device.getContext()),
602 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800603 mId(eventHubId),
604 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800605
606InputDeviceContext::~InputDeviceContext() {}
607
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700608} // namespace android