1 /*
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 <android/sysprop/InputProperties.sysprop.h>
20 #include "MultiTouchInputMapper.h"
21
22 namespace android {
23
24 // --- Constants ---
25
26 // Maximum number of slots supported when using the slot-based Multitouch Protocol B.
27 static constexpr size_t MAX_SLOTS = 32;
28
29 // --- MultiTouchInputMapper ---
30
MultiTouchInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig)31 MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext,
32 const InputReaderConfiguration& readerConfig)
33 : TouchInputMapper(deviceContext, readerConfig) {}
34
~MultiTouchInputMapper()35 MultiTouchInputMapper::~MultiTouchInputMapper() {}
36
reset(nsecs_t when)37 std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) {
38 mPointerIdBits.clear();
39 mMultiTouchMotionAccumulator.reset(mDeviceContext);
40 return TouchInputMapper::reset(when);
41 }
42
process(const RawEvent & rawEvent)43 std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent& rawEvent) {
44 std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent);
45
46 mMultiTouchMotionAccumulator.process(rawEvent);
47 return out;
48 }
49
getActiveBitId(const MultiTouchMotionAccumulator::Slot & inSlot)50 std::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
syncTouch(nsecs_t when,RawState * outState)64 void 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++) {
71 const MultiTouchMotionAccumulator::Slot& inSlot =
72 mMultiTouchMotionAccumulator.getSlot(inIndex);
73 if (!inSlot.isInUse()) {
74 continue;
75 }
76
77 if (inSlot.getToolType() == ToolType::PALM) {
78 std::optional<int32_t> id = getActiveBitId(inSlot);
79 if (id) {
80 outState->rawPointerData.canceledIdBits.markBit(id.value());
81 }
82 if (DEBUG_POINTERS) {
83 ALOGI("Stop processing slot %zu for it received a palm event from device %s",
84 inIndex, getDeviceName().c_str());
85 }
86 continue;
87 }
88
89 if (outCount >= MAX_POINTERS) {
90 if (DEBUG_POINTERS) {
91 ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; "
92 "ignoring the rest.",
93 getDeviceName().c_str(), MAX_POINTERS);
94 }
95 break; // too many fingers!
96 }
97
98 RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
99 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();
108 outPointer.tiltX = 0;
109 outPointer.tiltY = 0;
110
111 outPointer.toolType = inSlot.getToolType();
112 if (outPointer.toolType == ToolType::UNKNOWN) {
113 outPointer.toolType = mTouchButtonAccumulator.getToolType();
114 if (outPointer.toolType == ToolType::UNKNOWN) {
115 outPointer.toolType = ToolType::FINGER;
116 }
117 } else if (outPointer.toolType == ToolType::STYLUS && !mStylusMtToolSeen) {
118 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 }
129 }
130 if (mShouldSimulateStylusWithTouch && outPointer.toolType == ToolType::FINGER) {
131 outPointer.toolType = ToolType::STYLUS;
132 }
133
134 bool isHovering = mTouchButtonAccumulator.getToolType() != ToolType::MOUSE &&
135 (mTouchButtonAccumulator.isHovering() ||
136 (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0));
137 outPointer.isHovering = isHovering;
138
139 // Assign pointer id using tracking id if available.
140 if (mHavePointerIds) {
141 const int32_t trackingId = inSlot.getTrackingId();
142 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;
148 break;
149 }
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
reconfigure(nsecs_t when,const InputReaderConfiguration & config,ConfigurationChanges changes)177 std::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
configureRawPointerAxes()189 void MultiTouchInputMapper::configureRawPointerAxes() {
190 TouchInputMapper::configureRawPointerAxes();
191
192 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
193 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
194 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
195 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
196 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
197 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
198 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
199 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
200 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
201 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
202 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
203
204 if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid &&
205 mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
206 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
207 if (slotCount > MAX_SLOTS) {
208 ALOGW("MultiTouch Device %s reported %zu slots but the framework "
209 "only supports a maximum of %zu slots at this time.",
210 getDeviceName().c_str(), slotCount, MAX_SLOTS);
211 slotCount = MAX_SLOTS;
212 }
213 mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount,
214 /*usingSlotsProtocol=*/true);
215 } else {
216 mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS,
217 /*usingSlotsProtocol=*/false);
218 }
219 }
220
hasStylus() const221 bool MultiTouchInputMapper::hasStylus() const {
222 return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() ||
223 mShouldSimulateStylusWithTouch;
224 }
225
226 } // namespace android
227