blob: c7c8e7c9d333321bca3a4e075538ba235e2aa30e [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 }
Chris Yee14523a2020-12-19 13:46:00 -080057 // An input device composed of sub devices can be individually enabled or disabled.
58 // If any of the sub device is enabled then the input device is considered as enabled.
59 bool enabled = false;
60 for_each_subdevice([&enabled](auto& context) { enabled |= context.isDeviceEnabled(); });
61 return enabled;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062}
63
64void InputDevice::setEnabled(bool enabled, nsecs_t when) {
65 if (enabled && mAssociatedDisplayPort && !mAssociatedViewport) {
66 ALOGW("Cannot enable input device %s because it is associated with port %" PRIu8 ", "
67 "but the corresponding viewport is not found",
68 getName().c_str(), *mAssociatedDisplayPort);
69 enabled = false;
70 }
71
72 if (isEnabled() == enabled) {
73 return;
74 }
75
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080076 // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
77 // performed. The querying must happen when the device is enabled, so we reset after enabling
78 // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 if (enabled) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080080 for_each_subdevice([](auto& context) { context.enableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 reset(when);
82 } else {
83 reset(when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080084 for_each_subdevice([](auto& context) { context.disableDevice(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 }
86 // Must change generation to flag this device as changed
87 bumpGeneration();
88}
89
Chris Yee7310032020-09-22 15:36:28 -070090void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 InputDeviceInfo deviceInfo;
92 getDeviceInfo(&deviceInfo);
93
94 dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
95 deviceInfo.getDisplayName().c_str());
Chris Yee7310032020-09-22 15:36:28 -070096 dump += StringPrintf(INDENT "%s", eventHubDevStr.c_str());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070097 dump += StringPrintf(INDENT2 "Generation: %d\n", mGeneration);
98 dump += StringPrintf(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
99 dump += StringPrintf(INDENT2 "AssociatedDisplayPort: ");
100 if (mAssociatedDisplayPort) {
101 dump += StringPrintf("%" PRIu8 "\n", *mAssociatedDisplayPort);
102 } else {
103 dump += "<none>\n";
104 }
105 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
106 dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
107 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700108 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700109
110 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
111 if (!ranges.empty()) {
112 dump += INDENT2 "Motion Ranges:\n";
113 for (size_t i = 0; i < ranges.size(); i++) {
114 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700115 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 char name[32];
117 if (label) {
118 strncpy(name, label, sizeof(name));
119 name[sizeof(name) - 1] = '\0';
120 } else {
121 snprintf(name, sizeof(name), "%d", range.axis);
122 }
123 dump += StringPrintf(INDENT3
124 "%s: source=0x%08x, "
125 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
126 name, range.source, range.min, range.max, range.flat, range.fuzz,
127 range.resolution);
128 }
129 }
130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800131 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132}
133
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800134void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
135 if (mDevices.find(eventHubId) != mDevices.end()) {
136 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800137 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800138 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Chris Ye1b0c7342020-07-28 21:57:03 -0700139 Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800140 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800142 // Check if we should skip population
143 if (!populateMappers) {
144 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
145 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800146 }
147
148 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700149 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800150 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800151 }
152
153 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700154 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800155 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800156 }
157
158 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700159 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800160 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800161 }
162
163 // Keyboard-like devices.
164 uint32_t keyboardSource = 0;
165 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700166 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800167 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
168 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700169 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800170 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
171 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700172 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800173 keyboardSource |= AINPUT_SOURCE_DPAD;
174 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700175 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800176 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
177 }
178
179 if (keyboardSource != 0) {
180 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800182 }
183
184 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700185 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800186 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800187 }
188
189 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700190 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800191 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700192 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800193 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800194 }
195
196 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700197 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800198 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800199 }
200
Chris Yef59a2f42020-10-16 12:55:26 -0700201 // Motion sensor enabled devices.
202 if (classes.test(InputDeviceClass::SENSOR)) {
203 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
204 }
205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800206 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700207 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800209 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800210
211 // insert the context into the devices set
212 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700213 // Must change generation to flag this device as changed
214 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800215}
216
217void InputDevice::removeEventHubDevice(int32_t eventHubId) {
218 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700219}
220
221void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
222 uint32_t changes) {
223 mSources = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -0700224 mClasses = Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800225 mControllerNumber = 0;
226
227 for_each_subdevice([this](InputDeviceContext& context) {
228 mClasses |= context.getDeviceClasses();
229 int32_t controllerNumber = context.getDeviceControllerNumber();
230 if (controllerNumber > 0) {
231 if (mControllerNumber && mControllerNumber != controllerNumber) {
232 ALOGW("InputDevice::configure(): composite device contains multiple unique "
233 "controller numbers");
234 }
235 mControllerNumber = controllerNumber;
236 }
237 });
238
Chris Ye1b0c7342020-07-28 21:57:03 -0700239 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
240 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700241
242 if (!isIgnored()) {
243 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244 mConfiguration.clear();
245 for_each_subdevice([this](InputDeviceContext& context) {
246 PropertyMap configuration;
247 context.getConfiguration(&configuration);
248 mConfiguration.addAll(&configuration);
249 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700250 }
251
252 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700253 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700254 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800256 bool shouldBumpGeneration = false;
257 for_each_subdevice(
258 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
259 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
260 shouldBumpGeneration = true;
261 }
262 });
263 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700264 bumpGeneration();
265 }
266 }
267 }
268
269 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700270 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700271 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
272 if (mAlias != alias) {
273 mAlias = alias;
274 bumpGeneration();
275 }
276 }
277 }
278
279 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
280 auto it = config->disabledDevices.find(mId);
281 bool enabled = it == config->disabledDevices.end();
282 setEnabled(enabled, when);
283 }
284
285 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
286 // In most situations, no port will be specified.
287 mAssociatedDisplayPort = std::nullopt;
288 mAssociatedViewport = std::nullopt;
289 // Find the display port that corresponds to the current input port.
290 const std::string& inputPort = mIdentifier.location;
291 if (!inputPort.empty()) {
292 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
293 const auto& displayPort = ports.find(inputPort);
294 if (displayPort != ports.end()) {
295 mAssociatedDisplayPort = std::make_optional(displayPort->second);
296 }
297 }
298
299 // If the device was explicitly disabled by the user, it would be present in the
300 // "disabledDevices" list. If it is associated with a specific display, and it was not
301 // explicitly disabled, then enable/disable the device based on whether we can find the
302 // corresponding viewport.
303 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
304 if (mAssociatedDisplayPort) {
305 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
306 if (!mAssociatedViewport) {
307 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
308 "but the corresponding viewport is not found.",
309 getName().c_str(), *mAssociatedDisplayPort);
310 enabled = false;
311 }
312 }
313
314 if (changes) {
315 // For first-time configuration, only allow device to be disabled after mappers have
316 // finished configuring. This is because we need to read some of the properties from
317 // the device's open fd.
318 setEnabled(enabled, when);
319 }
320 }
321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800322 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
323 mapper.configure(when, config, changes);
324 mSources |= mapper.getSources();
325 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700326
327 // If a device is just plugged but it might be disabled, we need to update some info like
328 // axis range of touch from each InputMapper first, then disable it.
329 if (!changes) {
330 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
331 }
332 }
333}
334
335void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800336 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337
338 mContext->updateGlobalMetaState();
339
340 notifyReset(when);
341}
342
343void InputDevice::process(const RawEvent* rawEvents, size_t count) {
344 // Process all of the events in order for each mapper.
345 // We cannot simply ask each mapper to process them in bulk because mappers may
346 // have side-effects that must be interleaved. For example, joystick movement events and
347 // gamepad button presses are handled by different mappers but they should be dispatched
348 // in the order received.
349 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
350#if DEBUG_RAW_EVENTS
351 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
352 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when);
353#endif
354
355 if (mDropUntilNextSync) {
356 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
357 mDropUntilNextSync = false;
358#if DEBUG_RAW_EVENTS
359 ALOGD("Recovered from input event buffer overrun.");
360#endif
361 } else {
362#if DEBUG_RAW_EVENTS
363 ALOGD("Dropped input event while waiting for next input sync.");
364#endif
365 }
366 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
367 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
368 mDropUntilNextSync = true;
369 reset(rawEvent->when);
370 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800371 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
372 mapper.process(rawEvent);
373 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700374 }
375 --count;
376 }
377}
378
379void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800380 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700381}
382
383void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800384 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700385}
386
387void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
388 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
389 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800390 for_each_mapper(
391 [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700392}
393
394int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
395 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
396}
397
398int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
399 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
400}
401
402int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
403 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
404}
405
406int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
407 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800408 for (auto& deviceEntry : mDevices) {
409 auto& devicePair = deviceEntry.second;
410 auto& mappers = devicePair.second;
411 for (auto& mapperPtr : mappers) {
412 InputMapper& mapper = *mapperPtr;
413 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
414 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
415 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
416 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
417 if (currentResult >= AKEY_STATE_DOWN) {
418 return currentResult;
419 } else if (currentResult == AKEY_STATE_UP) {
420 result = currentResult;
421 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700422 }
423 }
424 }
425 return result;
426}
427
428bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
429 const int32_t* keyCodes, uint8_t* outFlags) {
430 bool result = false;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800431 for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) {
432 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
433 result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700434 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800435 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 return result;
437}
438
Chris Ye87143712020-11-10 05:05:58 +0000439void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
440 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
441 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800442 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700443}
444
445void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800446 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700447}
448
Chris Ye87143712020-11-10 05:05:58 +0000449bool InputDevice::isVibrating() {
450 bool vibrating = false;
451 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
452 return vibrating;
453}
454
455/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
456 * different vibration mappers then we could have duplicate IDs.
457 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
458 * we would definitely have duplicate IDs.
459 */
460std::vector<int32_t> InputDevice::getVibratorIds() {
461 std::vector<int32_t> vibrators;
462 for_each_mapper([&vibrators](InputMapper& mapper) {
463 std::vector<int32_t> devVibs = mapper.getVibratorIds();
464 vibrators.reserve(vibrators.size() + devVibs.size());
465 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
466 });
467 return vibrators;
468}
469
Chris Yef59a2f42020-10-16 12:55:26 -0700470bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
471 std::chrono::microseconds samplingPeriod,
472 std::chrono::microseconds maxBatchReportLatency) {
473 bool success = true;
474 for_each_mapper(
475 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
476 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
477 });
478 return success;
479}
480
481void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
482 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
483}
484
485void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
486 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
487}
488
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700489void InputDevice::cancelTouch(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800490 for_each_mapper([when](InputMapper& mapper) { mapper.cancelTouch(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700491}
492
493int32_t InputDevice::getMetaState() {
494 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800495 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700496 return result;
497}
498
499void InputDevice::updateMetaState(int32_t keyCode) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800500 for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700501}
502
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700503void InputDevice::bumpGeneration() {
504 mGeneration = mContext->bumpGeneration();
505}
506
507void InputDevice::notifyReset(nsecs_t when) {
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800508 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700509 mContext->getListener()->notifyDeviceReset(&args);
510}
511
512std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
513 // Check if we had associated to the specific display.
514 if (mAssociatedViewport) {
515 return mAssociatedViewport->displayId;
516 }
517
518 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800519 return first_in_mappers<int32_t>(
520 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700521}
522
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800523// returns the number of mappers associated with the device
524size_t InputDevice::getMapperCount() {
525 size_t count = 0;
526 for (auto& deviceEntry : mDevices) {
527 auto& devicePair = deviceEntry.second;
528 auto& mappers = devicePair.second;
529 count += mappers.size();
530 }
531 return count;
532}
533
arthurhungc903df12020-08-11 15:08:42 +0800534void InputDevice::updateLedState(bool reset) {
535 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
536}
537
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800538InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800539 : mDevice(device),
540 mContext(device.getContext()),
541 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800542 mId(eventHubId),
543 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800544
545InputDeviceContext::~InputDeviceContext() {}
546
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700547} // namespace android