blob: fd8224a6080defe43e24b118064b8410c54d57b0 [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
Prabir Pradhan9244aea2020-02-05 20:31:40 -080017#include "../Macros.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
Prabir Pradhanf4d65b12022-02-10 07:15:38 -080019#include <android/sysprop/InputProperties.sysprop.h>
Siarhei Vishniakou9faa4462022-12-06 11:45:04 -080020#include "MultiTouchInputMapper.h"
Prabir Pradhanf4d65b12022-02-10 07:15:38 -080021
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070022namespace android {
23
24// --- Constants ---
25
26// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
27static constexpr size_t MAX_SLOTS = 32;
28
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029// --- MultiTouchInputMapper ---
30
Arpit Singh8e6fb252023-04-06 11:49:17 +000031MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext,
32 const InputReaderConfiguration& readerConfig)
33 : TouchInputMapper(deviceContext, readerConfig) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070034
35MultiTouchInputMapper::~MultiTouchInputMapper() {}
36
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070037std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) {
Arpit Singh4b4a4572023-11-24 18:19:56 +000038 mPointerIdBits.clear();
39 mMultiTouchMotionAccumulator.reset(mDeviceContext);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070040 return TouchInputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041}
42
Harry Cuttsa32a1192024-06-04 15:10:31 +000043std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent& rawEvent) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070044 std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045
Harry Cuttsa32a1192024-06-04 15:10:31 +000046 mMultiTouchMotionAccumulator.process(rawEvent);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048}
49
arthurhungcc7f9802020-04-30 17:55:40 +080050std::optional<int32_t> MultiTouchInputMapper::getActiveBitId(
51 const MultiTouchMotionAccumulator::Slot& inSlot) {
52 if (mHavePointerIds) {
53 int32_t trackingId = inSlot.getTrackingId();
54 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
55 int32_t n = idBits.clearFirstMarkedBit();
56 if (mPointerTrackingIdMap[n] == trackingId) {
57 return std::make_optional(n);
58 }
59 }
60 }
61 return std::nullopt;
62}
63
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070064void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
65 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
66 size_t outCount = 0;
67 BitSet32 newPointerIdBits;
68 mHavePointerIds = true;
69
70 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080071 const MultiTouchMotionAccumulator::Slot& inSlot =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 mMultiTouchMotionAccumulator.getSlot(inIndex);
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080073 if (!inSlot.isInUse()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 continue;
75 }
76
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070077 if (inSlot.getToolType() == ToolType::PALM) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080078 std::optional<int32_t> id = getActiveBitId(inSlot);
arthurhungcc7f9802020-04-30 17:55:40 +080079 if (id) {
80 outState->rawPointerData.canceledIdBits.markBit(id.value());
Arthur Hung421eb1c2020-01-16 00:09:42 +080081 }
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050082 if (DEBUG_POINTERS) {
83 ALOGI("Stop processing slot %zu for it received a palm event from device %s",
84 inIndex, getDeviceName().c_str());
85 }
arthurhungbf89a482020-04-17 17:37:55 +080086 continue;
Arthur Hung421eb1c2020-01-16 00:09:42 +080087 }
88
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 if (outCount >= MAX_POINTERS) {
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050090 if (DEBUG_POINTERS) {
Siarhei Vishniakou01747382022-01-20 13:23:27 -080091 ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; "
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050092 "ignoring the rest.",
93 getDeviceName().c_str(), MAX_POINTERS);
94 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095 break; // too many fingers!
96 }
97
98 RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080099 outPointer.x = inSlot.getX();
100 outPointer.y = inSlot.getY();
101 outPointer.pressure = inSlot.getPressure();
102 outPointer.touchMajor = inSlot.getTouchMajor();
103 outPointer.touchMinor = inSlot.getTouchMinor();
104 outPointer.toolMajor = inSlot.getToolMajor();
105 outPointer.toolMinor = inSlot.getToolMinor();
106 outPointer.orientation = inSlot.getOrientation();
107 outPointer.distance = inSlot.getDistance();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 outPointer.tiltX = 0;
109 outPointer.tiltY = 0;
110
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800111 outPointer.toolType = inSlot.getToolType();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700112 if (outPointer.toolType == ToolType::UNKNOWN) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700113 outPointer.toolType = mTouchButtonAccumulator.getToolType();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700114 if (outPointer.toolType == ToolType::UNKNOWN) {
115 outPointer.toolType = ToolType::FINGER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700117 } else if (outPointer.toolType == ToolType::STYLUS && !mStylusMtToolSeen) {
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000118 mStylusMtToolSeen = true;
119 // The multi-touch device produced a stylus event with MT_TOOL_PEN. Dynamically
120 // re-configure this input device so that we add SOURCE_STYLUS if we haven't already.
121 // This is to cover the case where we cannot reliably detect whether a multi-touch
122 // device will ever produce stylus events when it is initially being configured.
123 if (!isFromSource(mSource, AINPUT_SOURCE_STYLUS)) {
124 // Add the stylus source immediately so that it is included in any events generated
125 // before we have a chance to re-configure the device.
126 mSource |= AINPUT_SOURCE_STYLUS;
127 bumpGeneration();
128 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700129 }
Prabir Pradhan522758a2023-11-14 23:55:51 +0000130 if (mShouldSimulateStylusWithTouch && outPointer.toolType == ToolType::FINGER) {
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700131 outPointer.toolType = ToolType::STYLUS;
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800132 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700134 bool isHovering = mTouchButtonAccumulator.getToolType() != ToolType::MOUSE &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700135 (mTouchButtonAccumulator.isHovering() ||
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000136 (mRawPointerAxes.pressure && inSlot.getPressure() <= 0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137 outPointer.isHovering = isHovering;
138
139 // Assign pointer id using tracking id if available.
140 if (mHavePointerIds) {
Linnan Li3db547c2024-04-26 11:13:00 +0000141 const int32_t trackingId = inSlot.getTrackingId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142 int32_t id = -1;
143 if (trackingId >= 0) {
144 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
145 uint32_t n = idBits.clearFirstMarkedBit();
146 if (mPointerTrackingIdMap[n] == trackingId) {
147 id = n;
Linnan Li3db547c2024-04-26 11:13:00 +0000148 break;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700149 }
150 }
151
152 if (id < 0 && !mPointerIdBits.isFull()) {
153 id = mPointerIdBits.markFirstUnmarkedBit();
154 mPointerTrackingIdMap[id] = trackingId;
155 }
156 }
157 if (id < 0) {
158 mHavePointerIds = false;
159 outState->rawPointerData.clearIdBits();
160 newPointerIdBits.clear();
161 } else {
162 outPointer.id = id;
163 outState->rawPointerData.idToIndex[id] = outCount;
164 outState->rawPointerData.markIdBit(id, isHovering);
165 newPointerIdBits.markBit(id);
166 }
167 }
168 outCount += 1;
169 }
170
171 outState->rawPointerData.pointerCount = outCount;
172 mPointerIdBits = newPointerIdBits;
173
174 mMultiTouchMotionAccumulator.finishSync();
175}
176
Prabir Pradhan522758a2023-11-14 23:55:51 +0000177std::list<NotifyArgs> MultiTouchInputMapper::reconfigure(nsecs_t when,
178 const InputReaderConfiguration& config,
179 ConfigurationChanges changes) {
180 const bool simulateStylusWithTouch =
181 sysprop::InputProperties::simulate_stylus_with_touch().value_or(false);
182 if (simulateStylusWithTouch != mShouldSimulateStylusWithTouch) {
183 mShouldSimulateStylusWithTouch = simulateStylusWithTouch;
184 bumpGeneration();
185 }
186 return TouchInputMapper::reconfigure(when, config, changes);
187}
188
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700189void MultiTouchInputMapper::configureRawPointerAxes() {
190 TouchInputMapper::configureRawPointerAxes();
191
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000192 // TODO(b/351870641): Investigate why we are sometime not getting valid axis infos for the x/y
193 // axes, even though those axes are required to be supported.
194 if (const auto xInfo = getAbsoluteAxisInfo(ABS_MT_POSITION_X); xInfo.has_value()) {
195 mRawPointerAxes.x = *xInfo;
196 }
197 if (const auto yInfo = getAbsoluteAxisInfo(ABS_MT_POSITION_Y); yInfo.has_value()) {
198 mRawPointerAxes.y = *yInfo;
199 }
200 mRawPointerAxes.touchMajor = getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR);
201 mRawPointerAxes.touchMinor = getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR);
202 mRawPointerAxes.toolMajor = getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR);
203 mRawPointerAxes.toolMinor = getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR);
204 mRawPointerAxes.orientation = getAbsoluteAxisInfo(ABS_MT_ORIENTATION);
205 mRawPointerAxes.pressure = getAbsoluteAxisInfo(ABS_MT_PRESSURE);
206 mRawPointerAxes.distance = getAbsoluteAxisInfo(ABS_MT_DISTANCE);
207 mRawPointerAxes.trackingId = getAbsoluteAxisInfo(ABS_MT_TRACKING_ID);
208 mRawPointerAxes.slot = getAbsoluteAxisInfo(ABS_MT_SLOT);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000210 if (mRawPointerAxes.trackingId && mRawPointerAxes.slot && mRawPointerAxes.slot->minValue == 0 &&
211 mRawPointerAxes.slot->maxValue > 0) {
212 size_t slotCount = mRawPointerAxes.slot->maxValue + 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700213 if (slotCount > MAX_SLOTS) {
214 ALOGW("MultiTouch Device %s reported %zu slots but the framework "
215 "only supports a maximum of %zu slots at this time.",
216 getDeviceName().c_str(), slotCount, MAX_SLOTS);
217 slotCount = MAX_SLOTS;
218 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount,
Harry Cutts33476232023-01-30 19:57:29 +0000220 /*usingSlotsProtocol=*/true);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 } else {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800222 mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS,
Harry Cutts33476232023-01-30 19:57:29 +0000223 /*usingSlotsProtocol=*/false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 }
225}
226
227bool MultiTouchInputMapper::hasStylus() const {
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000228 return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() ||
Prabir Pradhan522758a2023-11-14 23:55:51 +0000229 mShouldSimulateStylusWithTouch;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700230}
231
232} // namespace android