blob: 8f75d22d876261cf320eafdc7106cc30708670e2 [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 }
106 dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
107 dump += StringPrintf(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
108 dump += StringPrintf(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Chris Yee7310032020-09-22 15:36:28 -0700109 dump += StringPrintf(INDENT2 "ControllerNum: %d\n", deviceInfo.getControllerNumber());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
112 if (!ranges.empty()) {
113 dump += INDENT2 "Motion Ranges:\n";
114 for (size_t i = 0; i < ranges.size(); i++) {
115 const InputDeviceInfo::MotionRange& range = ranges[i];
Chris Ye4958d062020-08-20 13:21:10 -0700116 const char* label = InputEventLookup::getAxisLabel(range.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 char name[32];
118 if (label) {
119 strncpy(name, label, sizeof(name));
120 name[sizeof(name) - 1] = '\0';
121 } else {
122 snprintf(name, sizeof(name), "%d", range.axis);
123 }
124 dump += StringPrintf(INDENT3
125 "%s: source=0x%08x, "
126 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
127 name, range.source, range.min, range.max, range.flat, range.fuzz,
128 range.resolution);
129 }
130 }
131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800132 for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800133 if (mController) {
134 mController->dump(dump);
135 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136}
137
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800138void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
139 if (mDevices.find(eventHubId) != mDevices.end()) {
140 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800141 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800142 std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
Chris Ye1b0c7342020-07-28 21:57:03 -0700143 Flags<InputDeviceClass> classes = contextPtr->getDeviceClasses();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800144 std::vector<std::unique_ptr<InputMapper>> mappers;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800146 // Check if we should skip population
147 if (!populateMappers) {
148 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
149 return;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800150 }
151
152 // Switch-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700153 if (classes.test(InputDeviceClass::SWITCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800154 mappers.push_back(std::make_unique<SwitchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800155 }
156
157 // Scroll wheel-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700158 if (classes.test(InputDeviceClass::ROTARY_ENCODER)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800159 mappers.push_back(std::make_unique<RotaryEncoderInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800160 }
161
162 // Vibrator-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700163 if (classes.test(InputDeviceClass::VIBRATOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800164 mappers.push_back(std::make_unique<VibratorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800165 }
166
Chris Yee2b1e5c2021-03-10 22:45:12 -0800167 // Battery-like devices or light-containing devices.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700168 // PeripheralController will be created with associated EventHub device.
Chris Yee2b1e5c2021-03-10 22:45:12 -0800169 if (classes.test(InputDeviceClass::BATTERY) || classes.test(InputDeviceClass::LIGHT)) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700170 mController = std::make_unique<PeripheralController>(*contextPtr);
Kim Low03ea0352020-11-06 12:45:07 -0800171 }
172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800173 // Keyboard-like devices.
174 uint32_t keyboardSource = 0;
175 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
Chris Ye1b0c7342020-07-28 21:57:03 -0700176 if (classes.test(InputDeviceClass::KEYBOARD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800177 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
178 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700179 if (classes.test(InputDeviceClass::ALPHAKEY)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800180 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
181 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700182 if (classes.test(InputDeviceClass::DPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800183 keyboardSource |= AINPUT_SOURCE_DPAD;
184 }
Chris Ye1b0c7342020-07-28 21:57:03 -0700185 if (classes.test(InputDeviceClass::GAMEPAD)) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
187 }
188
189 if (keyboardSource != 0) {
190 mappers.push_back(
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800191 std::make_unique<KeyboardInputMapper>(*contextPtr, keyboardSource, keyboardType));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800192 }
193
194 // Cursor-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700195 if (classes.test(InputDeviceClass::CURSOR)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800196 mappers.push_back(std::make_unique<CursorInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800197 }
198
199 // Touchscreens and touchpad devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700200 if (classes.test(InputDeviceClass::TOUCH_MT)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800201 mappers.push_back(std::make_unique<MultiTouchInputMapper>(*contextPtr));
Chris Ye1b0c7342020-07-28 21:57:03 -0700202 } else if (classes.test(InputDeviceClass::TOUCH)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800203 mappers.push_back(std::make_unique<SingleTouchInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800204 }
205
206 // Joystick-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700207 if (classes.test(InputDeviceClass::JOYSTICK)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208 mappers.push_back(std::make_unique<JoystickInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800209 }
210
Chris Yef59a2f42020-10-16 12:55:26 -0700211 // Motion sensor enabled devices.
212 if (classes.test(InputDeviceClass::SENSOR)) {
213 mappers.push_back(std::make_unique<SensorInputMapper>(*contextPtr));
214 }
215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800216 // External stylus-like devices.
Chris Ye1b0c7342020-07-28 21:57:03 -0700217 if (classes.test(InputDeviceClass::EXTERNAL_STYLUS)) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800218 mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800219 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800220
221 // insert the context into the devices set
222 mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
Chris Yee7310032020-09-22 15:36:28 -0700223 // Must change generation to flag this device as changed
224 bumpGeneration();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800225}
226
227void InputDevice::removeEventHubDevice(int32_t eventHubId) {
228 mDevices.erase(eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229}
230
231void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
232 uint32_t changes) {
233 mSources = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -0700234 mClasses = Flags<InputDeviceClass>(0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800235 mControllerNumber = 0;
236
237 for_each_subdevice([this](InputDeviceContext& context) {
238 mClasses |= context.getDeviceClasses();
239 int32_t controllerNumber = context.getDeviceControllerNumber();
240 if (controllerNumber > 0) {
241 if (mControllerNumber && mControllerNumber != controllerNumber) {
242 ALOGW("InputDevice::configure(): composite device contains multiple unique "
243 "controller numbers");
244 }
245 mControllerNumber = controllerNumber;
246 }
247 });
248
Chris Ye1b0c7342020-07-28 21:57:03 -0700249 mIsExternal = mClasses.test(InputDeviceClass::EXTERNAL);
250 mHasMic = mClasses.test(InputDeviceClass::MIC);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700251
252 if (!isIgnored()) {
253 if (!changes) { // first time only
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800254 mConfiguration.clear();
255 for_each_subdevice([this](InputDeviceContext& context) {
256 PropertyMap configuration;
257 context.getConfiguration(&configuration);
258 mConfiguration.addAll(&configuration);
259 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700260 }
261
262 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700263 if (!mClasses.test(InputDeviceClass::VIRTUAL)) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700264 std::shared_ptr<KeyCharacterMap> keyboardLayout =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700265 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266 bool shouldBumpGeneration = false;
267 for_each_subdevice(
268 [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
269 if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
270 shouldBumpGeneration = true;
271 }
272 });
273 if (shouldBumpGeneration) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700274 bumpGeneration();
275 }
276 }
277 }
278
279 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700280 if (!(mClasses.test(InputDeviceClass::VIRTUAL))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700281 std::string alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
282 if (mAlias != alias) {
283 mAlias = alias;
284 bumpGeneration();
285 }
286 }
287 }
288
289 if (!changes || (changes & InputReaderConfiguration::CHANGE_ENABLED_STATE)) {
290 auto it = config->disabledDevices.find(mId);
291 bool enabled = it == config->disabledDevices.end();
292 setEnabled(enabled, when);
293 }
294
295 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
296 // In most situations, no port will be specified.
297 mAssociatedDisplayPort = std::nullopt;
298 mAssociatedViewport = std::nullopt;
299 // Find the display port that corresponds to the current input port.
300 const std::string& inputPort = mIdentifier.location;
301 if (!inputPort.empty()) {
302 const std::unordered_map<std::string, uint8_t>& ports = config->portAssociations;
303 const auto& displayPort = ports.find(inputPort);
304 if (displayPort != ports.end()) {
305 mAssociatedDisplayPort = std::make_optional(displayPort->second);
306 }
307 }
308
309 // If the device was explicitly disabled by the user, it would be present in the
310 // "disabledDevices" list. If it is associated with a specific display, and it was not
311 // explicitly disabled, then enable/disable the device based on whether we can find the
312 // corresponding viewport.
313 bool enabled = (config->disabledDevices.find(mId) == config->disabledDevices.end());
314 if (mAssociatedDisplayPort) {
315 mAssociatedViewport = config->getDisplayViewportByPort(*mAssociatedDisplayPort);
316 if (!mAssociatedViewport) {
317 ALOGW("Input device %s should be associated with display on port %" PRIu8 ", "
318 "but the corresponding viewport is not found.",
319 getName().c_str(), *mAssociatedDisplayPort);
320 enabled = false;
321 }
322 }
323
324 if (changes) {
325 // For first-time configuration, only allow device to be disabled after mappers have
326 // finished configuring. This is because we need to read some of the properties from
327 // the device's open fd.
328 setEnabled(enabled, when);
329 }
330 }
331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800332 for_each_mapper([this, when, config, changes](InputMapper& mapper) {
333 mapper.configure(when, config, changes);
334 mSources |= mapper.getSources();
335 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700336
337 // If a device is just plugged but it might be disabled, we need to update some info like
338 // axis range of touch from each InputMapper first, then disable it.
339 if (!changes) {
340 setEnabled(config->disabledDevices.find(mId) == config->disabledDevices.end(), when);
341 }
342 }
343}
344
345void InputDevice::reset(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800346 for_each_mapper([when](InputMapper& mapper) { mapper.reset(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347
348 mContext->updateGlobalMetaState();
349
350 notifyReset(when);
351}
352
353void InputDevice::process(const RawEvent* rawEvents, size_t count) {
354 // Process all of the events in order for each mapper.
355 // We cannot simply ask each mapper to process them in bulk because mappers may
356 // have side-effects that must be interleaved. For example, joystick movement events and
357 // gamepad button presses are handled by different mappers but they should be dispatched
358 // in the order received.
359 for (const RawEvent* rawEvent = rawEvents; count != 0; rawEvent++) {
360#if DEBUG_RAW_EVENTS
361 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%" PRId64,
362 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, rawEvent->when);
363#endif
364
365 if (mDropUntilNextSync) {
366 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
367 mDropUntilNextSync = false;
368#if DEBUG_RAW_EVENTS
369 ALOGD("Recovered from input event buffer overrun.");
370#endif
371 } else {
372#if DEBUG_RAW_EVENTS
373 ALOGD("Dropped input event while waiting for next input sync.");
374#endif
375 }
376 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
377 ALOGI("Detected input event buffer overrun for device %s.", getName().c_str());
378 mDropUntilNextSync = true;
379 reset(rawEvent->when);
380 } else {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800381 for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
382 mapper.process(rawEvent);
383 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700384 }
385 --count;
386 }
387}
388
389void InputDevice::timeoutExpired(nsecs_t when) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800390 for_each_mapper([when](InputMapper& mapper) { mapper.timeoutExpired(when); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700391}
392
393void InputDevice::updateExternalStylusState(const StylusState& state) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800394 for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395}
396
397void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
398 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
399 mHasMic);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800400 for_each_mapper(
401 [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
Chris Yee2b1e5c2021-03-10 22:45:12 -0800402
403 if (mController) {
404 mController->populateDeviceInfo(outDeviceInfo);
405 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700406}
407
408int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
409 return getState(sourceMask, keyCode, &InputMapper::getKeyCodeState);
410}
411
412int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
413 return getState(sourceMask, scanCode, &InputMapper::getScanCodeState);
414}
415
416int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
417 return getState(sourceMask, switchCode, &InputMapper::getSwitchState);
418}
419
420int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
421 int32_t result = AKEY_STATE_UNKNOWN;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800422 for (auto& deviceEntry : mDevices) {
423 auto& devicePair = deviceEntry.second;
424 auto& mappers = devicePair.second;
425 for (auto& mapperPtr : mappers) {
426 InputMapper& mapper = *mapperPtr;
427 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
428 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
429 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
430 int32_t currentResult = (mapper.*getStateFunc)(sourceMask, code);
431 if (currentResult >= AKEY_STATE_DOWN) {
432 return currentResult;
433 } else if (currentResult == AKEY_STATE_UP) {
434 result = currentResult;
435 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700436 }
437 }
438 }
439 return result;
440}
441
442bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
443 const int32_t* keyCodes, uint8_t* outFlags) {
444 bool result = false;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800445 for_each_mapper([&result, sourceMask, numCodes, keyCodes, outFlags](InputMapper& mapper) {
446 if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
447 result |= mapper.markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700448 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800449 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700450 return result;
451}
452
Chris Ye87143712020-11-10 05:05:58 +0000453void InputDevice::vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token) {
454 for_each_mapper([sequence, repeat, token](InputMapper& mapper) {
455 mapper.vibrate(sequence, repeat, token);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800456 });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700457}
458
459void InputDevice::cancelVibrate(int32_t token) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800460 for_each_mapper([token](InputMapper& mapper) { mapper.cancelVibrate(token); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700461}
462
Chris Ye87143712020-11-10 05:05:58 +0000463bool InputDevice::isVibrating() {
464 bool vibrating = false;
465 for_each_mapper([&vibrating](InputMapper& mapper) { vibrating |= mapper.isVibrating(); });
466 return vibrating;
467}
468
469/* There's no guarantee the IDs provided by the different mappers are unique, so if we have two
470 * different vibration mappers then we could have duplicate IDs.
471 * Alternatively, if we have a merged device that has multiple evdev nodes with FF_* capabilities,
472 * we would definitely have duplicate IDs.
473 */
474std::vector<int32_t> InputDevice::getVibratorIds() {
475 std::vector<int32_t> vibrators;
476 for_each_mapper([&vibrators](InputMapper& mapper) {
477 std::vector<int32_t> devVibs = mapper.getVibratorIds();
478 vibrators.reserve(vibrators.size() + devVibs.size());
479 vibrators.insert(vibrators.end(), devVibs.begin(), devVibs.end());
480 });
481 return vibrators;
482}
483
Chris Yef59a2f42020-10-16 12:55:26 -0700484bool InputDevice::enableSensor(InputDeviceSensorType sensorType,
485 std::chrono::microseconds samplingPeriod,
486 std::chrono::microseconds maxBatchReportLatency) {
487 bool success = true;
488 for_each_mapper(
489 [&success, sensorType, samplingPeriod, maxBatchReportLatency](InputMapper& mapper) {
490 success &= mapper.enableSensor(sensorType, samplingPeriod, maxBatchReportLatency);
491 });
492 return success;
493}
494
495void InputDevice::disableSensor(InputDeviceSensorType sensorType) {
496 for_each_mapper([sensorType](InputMapper& mapper) { mapper.disableSensor(sensorType); });
497}
498
499void InputDevice::flushSensor(InputDeviceSensorType sensorType) {
500 for_each_mapper([sensorType](InputMapper& mapper) { mapper.flushSensor(sensorType); });
501}
502
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000503void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) {
504 for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700505}
506
Kim Low03ea0352020-11-06 12:45:07 -0800507std::optional<int32_t> InputDevice::getBatteryCapacity() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800508 return mController ? mController->getBatteryCapacity(DEFAULT_BATTERY_ID) : std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800509}
510
511std::optional<int32_t> InputDevice::getBatteryStatus() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800512 return mController ? mController->getBatteryStatus(DEFAULT_BATTERY_ID) : std::nullopt;
Kim Low03ea0352020-11-06 12:45:07 -0800513}
514
Chris Ye3fdbfef2021-01-06 18:45:18 -0800515bool InputDevice::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800516 return mController ? mController->setLightColor(lightId, color) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800517}
518
519bool InputDevice::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800520 return mController ? mController->setLightPlayerId(lightId, playerId) : false;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800521}
522
523std::optional<int32_t> InputDevice::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800524 return mController ? mController->getLightColor(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800525}
526
527std::optional<int32_t> InputDevice::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800528 return mController ? mController->getLightPlayerId(lightId) : std::nullopt;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800529}
530
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700531int32_t InputDevice::getMetaState() {
532 int32_t result = 0;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800533 for_each_mapper([&result](InputMapper& mapper) { result |= mapper.getMetaState(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700534 return result;
535}
536
537void InputDevice::updateMetaState(int32_t keyCode) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800538 for_each_mapper([keyCode](InputMapper& mapper) { mapper.updateMetaState(keyCode); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700539}
540
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700541void InputDevice::bumpGeneration() {
542 mGeneration = mContext->bumpGeneration();
543}
544
545void InputDevice::notifyReset(nsecs_t when) {
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000546 NotifyDeviceResetArgs args(mContext->getNextId(), when, mId);
547 mContext->getListener()->notifyDeviceReset(&args);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700548}
549
550std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
551 // Check if we had associated to the specific display.
552 if (mAssociatedViewport) {
553 return mAssociatedViewport->displayId;
554 }
555
556 // No associated display port, check if some InputMapper is associated.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800557 return first_in_mappers<int32_t>(
558 [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700559}
560
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800561// returns the number of mappers associated with the device
562size_t InputDevice::getMapperCount() {
563 size_t count = 0;
564 for (auto& deviceEntry : mDevices) {
565 auto& devicePair = deviceEntry.second;
566 auto& mappers = devicePair.second;
567 count += mappers.size();
568 }
569 return count;
570}
571
arthurhungc903df12020-08-11 15:08:42 +0800572void InputDevice::updateLedState(bool reset) {
573 for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); });
574}
575
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800576InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800577 : mDevice(device),
578 mContext(device.getContext()),
579 mEventHub(device.getContext()->getEventHub()),
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800580 mId(eventHubId),
581 mDeviceId(device.getId()) {}
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800582
583InputDeviceContext::~InputDeviceContext() {}
584
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700585} // namespace android