blob: ac72ac4189034c13790517810bade61b775152bf [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"
Chris Yef59a2f42020-10-16 12:55:26 -070031#include "SensorInputMapper.h"
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080032#include "SingleTouchInputMapper.h"
33#include "SwitchInputMapper.h"
34#include "VibratorInputMapper.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070035
36namespace android {
37
38InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080039 const InputDeviceIdentifier& identifier)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070040 : mContext(context),
41 mId(id),
42 mGeneration(generation),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080043 mControllerNumber(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 mIdentifier(identifier),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080045 mClasses(0),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 mSources(0),
47 mIsExternal(false),
48 mHasMic(false),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080049 mDropUntilNextSync(false) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080051InputDevice::~InputDevice() {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
53bool InputDevice::isEnabled() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080054 if (!hasEventHubDevices()) {
55 return false;
56 }
57 // devices are either all enabled or all disabled, so we only need to check the first
58 auto& devicePair = mDevices.begin()->second;
59 auto& contextPtr = devicePair.first;
60 return contextPtr->isDeviceEnabled();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070061}
62
63void InputDevice::setEnabled(bool enabled, nsecs_t when) {
64 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
65 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
66 "but the corresponding viewport is not found",
67 getName().c_str(), *mAssociatedDisplayPort);
68 enabled = false;
69 }
70
71 if (isEnabled() == enabled) {
72 return;
73 }
74
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080075 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
76 // performed. The querying must happen when the device is enabled, so we reset after enabling
77 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080079 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 reset(when);
81 } else {
82 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080083 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 }
85 // Must change generation to flag this device as changed
86 bumpGeneration();
87}
88
Chris Yee7310032020-09-22 15:36:28 -070089void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 InputDeviceInfo deviceInfo;
91 getDeviceInfo(&deviceInfo);
92
93 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
94 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -070095 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
97 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
98 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
99 if (mAssociatedDisplayPort) {
100 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
101 } else {
102 dump += "<none>\n";
103 }
104 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
105 dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
106 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700107 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108
109 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
110 if (!ranges.empty()) {
111 dump += INDENT2 "Motion Ranges:\n";
112 for (size_t i = 0; i < ranges.size(); i++) {
113 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700114 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 char name[32];
116 if (label) {
117 strncpy(name, label, sizeof(name));
118 name[sizeof(name) - 1] = '\0';
119 } else {
120 snprintf(name, sizeof(name), "%d", range.axis);
121 }
122 dump += StringPrintf(INDENT3
123 "%s: source=0x%08x, "
124 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
125 name, range.source, range.min, range.max, range.flat, range.fuzz,
126 range.resolution);
127 }
128 }
129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800130 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131}
132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800133void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
134 if (mDevices.find(eventHubId) != mDevices.end()) {
135 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800136 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800137 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Chris Ye1b0c7342020-07-28 21:57:03 -0700138 Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800139 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800140
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800141 // Check if we should skip population
142 if (!populateMappers) {
143 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
144 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800145 }
146
147 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700148 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800149 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800150 }
151
152 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700153 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800154 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800155 }
156
157 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700158 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800159 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800160 }
161
162 // Keyboard-like devices.
163 uint32_t keyboardSource = 0;
164 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700165 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800166 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
167 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700168 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800169 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
170 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700171 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800172 keyboardSource |= AINPUT_SOURCE_DPAD;
173 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700174 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800175 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
176 }
177
178 if (keyboardSource != 0) {
179 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800181 }
182
183 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700184 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 }
187
188 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700189 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800190 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700191 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800192 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800193 }
194
195 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700196 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800197 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800198 }
199
Chris Yef59a2f42020-10-16 12:55:26 -0700200 // Motion sensor enabled devices.
201 if (classes.test(InputDeviceClass::SENSOR)) {
202 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
203 }
204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800205 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700206 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800208 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800209
210 // insert the context into the devices set
211 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700212 // Must change generation to flag this device as changed
213 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800214}
215
216void InputDevice::removeEventHubDevice(int32_t eventHubId) {
217 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218}
219
220void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
221 uint32_t changes) {
222 mSources = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -0700223 mClasses = Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800224 mControllerNumber = 0;
225
226 for_each_subdevice([this](InputDeviceContext& context) {
227 mClasses |= context.getDeviceClasses();
228 int32_t controllerNumber = context.getDeviceControllerNumber();
229 if (controllerNumber > 0) {
230 if (mControllerNumber && mControllerNumber != controllerNumber) {
231 ALOGW("InputDevice::configure(): composite device contains multiple unique "
232 "controller numbers");
233 }
234 mControllerNumber = controllerNumber;
235 }
236 });
237
Chris Ye1b0c7342020-07-28 21:57:03 -0700238 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
239 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700240
241 if (!isIgnored()) {
242 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800243 mConfiguration.clear();
244 for_each_subdevice([this](InputDeviceContext& context) {
245 PropertyMap configuration;
246 context.getConfiguration(&configuration);
247 mConfiguration.addAll(&configuration);
248 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 }
250
251 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700252 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700253 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700254 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800255 bool shouldBumpGeneration = false;
256 for_each_subdevice(
257 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
258 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
259 shouldBumpGeneration = true;
260 }
261 });
262 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 bumpGeneration();
264 }
265 }
266 }
267
268 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700269 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700270 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
271 if (mAlias != alias) {
272 mAlias = alias;
273 bumpGeneration();
274 }
275 }
276 }
277
278 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
279 auto it = config->disabledDevices.find(mId);
280 bool enabled = it == config->disabledDevices.end();
281 setEnabled(enabled, when);
282 }
283
284 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
285 // In most situations, no port will be specified.
286 mAssociatedDisplayPort = std::nullopt;
287 mAssociatedViewport = std::nullopt;
288 // Find the display port that corresponds to the current input port.
289 const std::string& inputPort = mIdentifier.location;
290 if (!inputPort.empty()) {
291 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
292 const auto& displayPort = ports.find(inputPort);
293 if (displayPort != ports.end()) {
294 mAssociatedDisplayPort = std::make_optional(displayPort->second);
295 }
296 }
297
298 // If the device was explicitly disabled by the user, it would be present in the
299 // "disabledDevices" list. If it is associated with a specific display, and it was not
300 // explicitly disabled, then enable/disable the device based on whether we can find the
301 // corresponding viewport.
302 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
303 if (mAssociatedDisplayPort) {
304 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
305 if (!mAssociatedViewport) {
306 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
307 "but the corresponding viewport is not found.",
308 getName().c_str(), *mAssociatedDisplayPort);
309 enabled = false;
310 }
311 }
312
313 if (changes) {
314 // For first-time configuration, only allow device to be disabled after mappers have
315 // finished configuring. This is because we need to read some of the properties from
316 // the device's open fd.
317 setEnabled(enabled, when);
318 }
319 }
320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800321 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
322 mapper.configure(when, config, changes);
323 mSources |= mapper.getSources();
324 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700325
326 // If a device is just plugged but it might be disabled, we need to update some info like
327 // axis range of touch from each InputMapper first, then disable it.
328 if (!changes) {
329 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
330 }
331 }
332}
333
334void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800335 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336
337 mContext->updateGlobalMetaState();
338
339 notifyReset(when);
340}
341
342void InputDevice::process(const RawEvent* rawEvents, size_t count) {
343 // Process all of the events in order for each mapper.
344 // We cannot simply ask each mapper to process them in bulk because mappers may
345 // have side-effects that must be interleaved. For example, joystick movement events and
346 // gamepad button presses are handled by different mappers but they should be dispatched
347 // in the order received.
348 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
349#if DEBUG_RAW_EVENTS
350 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
351 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when);
352#endif
353
354 if (mDropUntilNextSync) {
355 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
356 mDropUntilNextSync = false;
357#if DEBUG_RAW_EVENTS
358 ALOGD("Recovered from input event buffer overrun.");
359#endif
360 } else {
361#if DEBUG_RAW_EVENTS
362 ALOGD("Dropped input event while waiting for next input sync.");
363#endif
364 }
365 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
366 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
367 mDropUntilNextSync = true;
368 reset(rawEvent->when);
369 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800370 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
371 mapper.process(rawEvent);
372 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700373 }
374 --count;
375 }
376}
377
378void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800379 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700380}
381
382void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800383 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384}
385
386void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
387 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
388 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800389 for_each_mapper(
390 [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700391}
392
393int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
394 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
395}
396
397int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
398 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
399}
400
401int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
402 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
403}
404
405int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
406 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800407 for (auto& deviceEntry : mDevices) {
408 auto& devicePair = deviceEntry.second;
409 auto& mappers = devicePair.second;
410 for (auto& mapperPtr : mappers) {
411 InputMapper& mapper = *mapperPtr;
412 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
413 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
414 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
415 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
416 if (currentResult >= AKEY_STATE_DOWN) {
417 return currentResult;
418 } else if (currentResult == AKEY_STATE_UP) {
419 result = currentResult;
420 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700421 }
422 }
423 }
424 return result;
425}
426
427bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
428 const int32_t* keyCodes, uint8_t* outFlags) {
429 bool result = false;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800430 for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) {
431 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
432 result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700433 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800434 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700435 return result;
436}
437
Chris Ye87143712020-11-10 05:05:58 +0000438void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
439 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
440 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800441 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700442}
443
444void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800445 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700446}
447
Chris Ye87143712020-11-10 05:05:58 +0000448bool InputDevice::isVibrating() {
449 bool vibrating = false;
450 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
451 return vibrating;
452}
453
454/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
455 * different vibration mappers then we could have duplicate IDs.
456 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
457 * we would definitely have duplicate IDs.
458 */
459std::vector<int32_t> InputDevice::getVibratorIds() {
460 std::vector<int32_t> vibrators;
461 for_each_mapper([&vibrators](InputMapper& mapper) {
462 std::vector<int32_t> devVibs = mapper.getVibratorIds();
463 vibrators.reserve(vibrators.size() + devVibs.size());
464 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
465 });
466 return vibrators;
467}
468
Chris Yef59a2f42020-10-16 12:55:26 -0700469bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
470 std::chrono::microseconds samplingPeriod,
471 std::chrono::microseconds maxBatchReportLatency) {
472 bool success = true;
473 for_each_mapper(
474 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
475 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
476 });
477 return success;
478}
479
480void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
481 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
482}
483
484void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
485 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
486}
487
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700488void InputDevice::cancelTouch(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800489 for_each_mapper([when](InputMapper& mapper) { mapper.cancelTouch(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700490}
491
492int32_t InputDevice::getMetaState() {
493 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800494 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700495 return result;
496}
497
498void InputDevice::updateMetaState(int32_t keyCode) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800499 for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700500}
501
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700502void InputDevice::bumpGeneration() {
503 mGeneration = mContext->bumpGeneration();
504}
505
506void InputDevice::notifyReset(nsecs_t when) {
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800507 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700508 mContext->getListener()->notifyDeviceReset(&args);
509}
510
511std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
512 // Check if we had associated to the specific display.
513 if (mAssociatedViewport) {
514 return mAssociatedViewport->displayId;
515 }
516
517 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800518 return first_in_mappers<int32_t>(
519 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700520}
521
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800522// returns the number of mappers associated with the device
523size_t InputDevice::getMapperCount() {
524 size_t count = 0;
525 for (auto& deviceEntry : mDevices) {
526 auto& devicePair = deviceEntry.second;
527 auto& mappers = devicePair.second;
528 count += mappers.size();
529 }
530 return count;
531}
532
arthurhungc903df12020-08-11 15:08:42 +0800533void InputDevice::updateLedState(bool reset) {
534 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
535}
536
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800537InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800538 : mDevice(device),
539 mContext(device.getContext()),
540 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800541 mId(eventHubId),
542 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800543
544InputDeviceContext::~InputDeviceContext() {}
545
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700546} // namespace android