blob: 271bc2f943fbba33ae5b6e67e79e385f35dacbc3 [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"
30#include "RotaryEncoderInputMapper.h"
31#include "SingleTouchInputMapper.h"
32#include "SwitchInputMapper.h"
33#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070034
35namespace android {
36
37InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080038 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039 : mContext(context),
40 mId(id),
41 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080042 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080044 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045 mSources(0),
46 mIsExternal(false),
47 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080048 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080050InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051
52bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 if (!hasEventHubDevices()) {
54 return false;
55 }
56 // devices are either all enabled or all disabled, so we only need to check the first
57 auto& devicePair = mDevices.begin()->second;
58 auto& contextPtr = devicePair.first;
59 return contextPtr->isDeviceEnabled();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060}
61
62void InputDevice::setEnabled(bool enabled, nsecs_t when) {
63 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
64 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
65 "but the corresponding viewport is not found",
66 getName().c_str(), *mAssociatedDisplayPort);
67 enabled = false;
68 }
69
70 if (isEnabled() == enabled) {
71 return;
72 }
73
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080074 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
75 // performed. The querying must happen when the device is enabled, so we reset after enabling
76 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080078 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 reset(when);
80 } else {
81 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080082 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 }
84 // Must change generation to flag this device as changed
85 bumpGeneration();
86}
87
Chris Yee7310032020-09-22 15:36:28 -070088void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 InputDeviceInfo deviceInfo;
90 getDeviceInfo(&deviceInfo);
91
92 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
93 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -070094 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
96 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
97 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
98 if (mAssociatedDisplayPort) {
99 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
100 } else {
101 dump += "<none>\n";
102 }
103 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
104 dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
105 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700106 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700107
108 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
109 if (!ranges.empty()) {
110 dump += INDENT2 "Motion Ranges:\n";
111 for (size_t i = 0; i < ranges.size(); i++) {
112 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700113 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700114 char name[32];
115 if (label) {
116 strncpy(name, label, sizeof(name));
117 name[sizeof(name) - 1] = '\0';
118 } else {
119 snprintf(name, sizeof(name), "%d", range.axis);
120 }
121 dump += StringPrintf(INDENT3
122 "%s: source=0x%08x, "
123 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
124 name, range.source, range.min, range.max, range.flat, range.fuzz,
125 range.resolution);
126 }
127 }
128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800129 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130}
131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800132void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
133 if (mDevices.find(eventHubId) != mDevices.end()) {
134 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800135 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800136 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Chris Ye1b0c7342020-07-28 21:57:03 -0700137 Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800138 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800139
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800140 // Check if we should skip population
141 if (!populateMappers) {
142 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
143 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800144 }
145
146 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700147 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800148 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800149 }
150
151 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700152 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800153 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800154 }
155
156 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700157 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800158 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800159 }
160
161 // Keyboard-like devices.
162 uint32_t keyboardSource = 0;
163 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700164 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800165 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
166 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700167 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800168 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
169 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700170 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800171 keyboardSource |= AINPUT_SOURCE_DPAD;
172 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700173 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800174 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
175 }
176
177 if (keyboardSource != 0) {
178 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800179 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800180 }
181
182 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700183 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800184 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800185 }
186
187 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700188 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800189 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700190 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800191 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 }
193
194 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700195 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800196 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800197 }
198
199 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700200 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800201 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800202 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800203
204 // insert the context into the devices set
205 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700206 // Must change generation to flag this device as changed
207 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800208}
209
210void InputDevice::removeEventHubDevice(int32_t eventHubId) {
211 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700212}
213
214void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
215 uint32_t changes) {
216 mSources = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -0700217 mClasses = Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800218 mControllerNumber = 0;
219
220 for_each_subdevice([this](InputDeviceContext& context) {
221 mClasses |= context.getDeviceClasses();
222 int32_t controllerNumber = context.getDeviceControllerNumber();
223 if (controllerNumber > 0) {
224 if (mControllerNumber && mControllerNumber != controllerNumber) {
225 ALOGW("InputDevice::configure(): composite device contains multiple unique "
226 "controller numbers");
227 }
228 mControllerNumber = controllerNumber;
229 }
230 });
231
Chris Ye1b0c7342020-07-28 21:57:03 -0700232 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
233 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700234
235 if (!isIgnored()) {
236 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800237 mConfiguration.clear();
238 for_each_subdevice([this](InputDeviceContext& context) {
239 PropertyMap configuration;
240 context.getConfiguration(&configuration);
241 mConfiguration.addAll(&configuration);
242 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700243 }
244
245 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700246 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700247 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700248 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800249 bool shouldBumpGeneration = false;
250 for_each_subdevice(
251 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
252 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
253 shouldBumpGeneration = true;
254 }
255 });
256 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700257 bumpGeneration();
258 }
259 }
260 }
261
262 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700263 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
265 if (mAlias != alias) {
266 mAlias = alias;
267 bumpGeneration();
268 }
269 }
270 }
271
272 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
273 auto it = config->disabledDevices.find(mId);
274 bool enabled = it == config->disabledDevices.end();
275 setEnabled(enabled, when);
276 }
277
278 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
279 // In most situations, no port will be specified.
280 mAssociatedDisplayPort = std::nullopt;
281 mAssociatedViewport = std::nullopt;
282 // Find the display port that corresponds to the current input port.
283 const std::string& inputPort = mIdentifier.location;
284 if (!inputPort.empty()) {
285 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
286 const auto& displayPort = ports.find(inputPort);
287 if (displayPort != ports.end()) {
288 mAssociatedDisplayPort = std::make_optional(displayPort->second);
289 }
290 }
291
292 // If the device was explicitly disabled by the user, it would be present in the
293 // "disabledDevices" list. If it is associated with a specific display, and it was not
294 // explicitly disabled, then enable/disable the device based on whether we can find the
295 // corresponding viewport.
296 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
297 if (mAssociatedDisplayPort) {
298 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
299 if (!mAssociatedViewport) {
300 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
301 "but the corresponding viewport is not found.",
302 getName().c_str(), *mAssociatedDisplayPort);
303 enabled = false;
304 }
305 }
306
307 if (changes) {
308 // For first-time configuration, only allow device to be disabled after mappers have
309 // finished configuring. This is because we need to read some of the properties from
310 // the device's open fd.
311 setEnabled(enabled, when);
312 }
313 }
314
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800315 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
316 mapper.configure(when, config, changes);
317 mSources |= mapper.getSources();
318 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700319
320 // If a device is just plugged but it might be disabled, we need to update some info like
321 // axis range of touch from each InputMapper first, then disable it.
322 if (!changes) {
323 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
324 }
325 }
326}
327
328void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800329 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700330
331 mContext->updateGlobalMetaState();
332
333 notifyReset(when);
334}
335
336void InputDevice::process(const RawEvent* rawEvents, size_t count) {
337 // Process all of the events in order for each mapper.
338 // We cannot simply ask each mapper to process them in bulk because mappers may
339 // have side-effects that must be interleaved. For example, joystick movement events and
340 // gamepad button presses are handled by different mappers but they should be dispatched
341 // in the order received.
342 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
343#if DEBUG_RAW_EVENTS
344 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
345 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when);
346#endif
347
348 if (mDropUntilNextSync) {
349 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
350 mDropUntilNextSync = false;
351#if DEBUG_RAW_EVENTS
352 ALOGD("Recovered from input event buffer overrun.");
353#endif
354 } else {
355#if DEBUG_RAW_EVENTS
356 ALOGD("Dropped input event while waiting for next input sync.");
357#endif
358 }
359 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
360 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
361 mDropUntilNextSync = true;
362 reset(rawEvent->when);
363 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800364 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
365 mapper.process(rawEvent);
366 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700367 }
368 --count;
369 }
370}
371
372void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800373 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374}
375
376void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800377 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700378}
379
380void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
381 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
382 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800383 for_each_mapper(
384 [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700385}
386
387int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
388 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
389}
390
391int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
392 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
393}
394
395int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
396 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
397}
398
399int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
400 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800401 for (auto& deviceEntry : mDevices) {
402 auto& devicePair = deviceEntry.second;
403 auto& mappers = devicePair.second;
404 for (auto& mapperPtr : mappers) {
405 InputMapper& mapper = *mapperPtr;
406 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
407 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
408 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
409 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
410 if (currentResult >= AKEY_STATE_DOWN) {
411 return currentResult;
412 } else if (currentResult == AKEY_STATE_UP) {
413 result = currentResult;
414 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700415 }
416 }
417 }
418 return result;
419}
420
421bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
422 const int32_t* keyCodes, uint8_t* outFlags) {
423 bool result = false;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800424 for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) {
425 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
426 result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700427 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800428 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700429 return result;
430}
431
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000432void InputDevice::vibrate(const std::vector<VibrationElement>& pattern, ssize_t repeat,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 int32_t token) {
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000434 for_each_mapper([pattern, repeat, token](InputMapper& mapper) {
435 mapper.vibrate(pattern, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800436 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700437}
438
439void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800440 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700441}
442
443void InputDevice::cancelTouch(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800444 for_each_mapper([when](InputMapper& mapper) { mapper.cancelTouch(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700445}
446
447int32_t InputDevice::getMetaState() {
448 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800449 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450 return result;
451}
452
453void InputDevice::updateMetaState(int32_t keyCode) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800454 for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700455}
456
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457void InputDevice::bumpGeneration() {
458 mGeneration = mContext->bumpGeneration();
459}
460
461void InputDevice::notifyReset(nsecs_t when) {
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800462 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700463 mContext->getListener()->notifyDeviceReset(&args);
464}
465
466std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
467 // Check if we had associated to the specific display.
468 if (mAssociatedViewport) {
469 return mAssociatedViewport->displayId;
470 }
471
472 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800473 return first_in_mappers<int32_t>(
474 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700475}
476
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800477// returns the number of mappers associated with the device
478size_t InputDevice::getMapperCount() {
479 size_t count = 0;
480 for (auto& deviceEntry : mDevices) {
481 auto& devicePair = deviceEntry.second;
482 auto& mappers = devicePair.second;
483 count += mappers.size();
484 }
485 return count;
486}
487
arthurhungc903df12020-08-11 15:08:42 +0800488void InputDevice::updateLedState(bool reset) {
489 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
490}
491
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800492InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800493 : mDevice(device),
494 mContext(device.getContext()),
495 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800496 mId(eventHubId),
497 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800498
499InputDeviceContext::~InputDeviceContext() {}
500
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501} // namespace android