blob: 047f068751977f3c8f8d5d53dcb3e48ef171a515 [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
19#include "MultiTouchInputMapper.h"
20
Prabir Pradhanf4d65b12022-02-10 07:15:38 -080021#include <android/sysprop/InputProperties.sysprop.h>
22
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070023namespace android {
24
25// --- Constants ---
26
27// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
28static constexpr size_t MAX_SLOTS = 32;
29
30// --- MultiTouchMotionAccumulator ---
31
32MultiTouchMotionAccumulator::MultiTouchMotionAccumulator()
33 : mCurrentSlot(-1),
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070034 mUsingSlotsProtocol(false),
35 mHaveStylus(false) {}
36
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080037void MultiTouchMotionAccumulator::configure(InputDeviceContext& deviceContext, size_t slotCount,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038 bool usingSlotsProtocol) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039 mUsingSlotsProtocol = usingSlotsProtocol;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080040 mHaveStylus = deviceContext.hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080041 mSlots = std::vector<Slot>(slotCount);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042
Prabir Pradhanafabcde2022-09-27 19:32:43 +000043 mCurrentSlot = -1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 if (mUsingSlotsProtocol) {
45 // Query the driver for the current slot index and use it as the initial slot
46 // before we start reading events from the device. It is possible that the
47 // current slot index will not be the same as it was when the first event was
48 // written into the evdev buffer, which means the input mapper could start
49 // out of sync with the initial state of the events in the evdev buffer.
50 // In the extremely unlikely case that this happens, the data from
51 // two slots will be confused until the next ABS_MT_SLOT event is received.
52 // This can cause the touch point to "jump", but at least there will be
53 // no stuck touches.
54 int32_t initialSlot;
Prabir Pradhanafabcde2022-09-27 19:32:43 +000055 if (const auto status = deviceContext.getAbsoluteAxisValue(ABS_MT_SLOT, &initialSlot);
56 status == OK) {
57 mCurrentSlot = initialSlot;
58 } else {
59 ALOGD("Could not retrieve current multi-touch slot index. status=%d", status);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070061 }
62}
63
Prabir Pradhanafabcde2022-09-27 19:32:43 +000064void MultiTouchMotionAccumulator::resetSlots() {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080065 for (Slot& slot : mSlots) {
66 slot.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067 }
Prabir Pradhanafabcde2022-09-27 19:32:43 +000068 mCurrentSlot = -1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069}
70
71void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
72 if (rawEvent->type == EV_ABS) {
73 bool newSlot = false;
74 if (mUsingSlotsProtocol) {
75 if (rawEvent->code == ABS_MT_SLOT) {
76 mCurrentSlot = rawEvent->value;
77 newSlot = true;
78 }
79 } else if (mCurrentSlot < 0) {
80 mCurrentSlot = 0;
81 }
82
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080083 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlots.size()) {
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050084 if (DEBUG_POINTERS) {
85 if (newSlot) {
86 ALOGW("MultiTouch device emitted invalid slot index %d but it "
87 "should be between 0 and %zd; ignoring this slot.",
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080088 mCurrentSlot, mSlots.size() - 1);
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050089 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 } else {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080092 Slot& slot = mSlots[mCurrentSlot];
Arthur Hung9ad18942021-06-19 02:04:46 +000093 // If mUsingSlotsProtocol is true, it means the raw pointer has axis info of
94 // ABS_MT_TRACKING_ID and ABS_MT_SLOT, so driver should send a valid trackingId while
95 // updating the slot.
96 if (!mUsingSlotsProtocol) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080097 slot.mInUse = true;
Arthur Hung9ad18942021-06-19 02:04:46 +000098 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099
100 switch (rawEvent->code) {
101 case ABS_MT_POSITION_X:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800102 slot.mAbsMTPositionX = rawEvent->value;
103 warnIfNotInUse(*rawEvent, slot);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104 break;
105 case ABS_MT_POSITION_Y:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800106 slot.mAbsMTPositionY = rawEvent->value;
107 warnIfNotInUse(*rawEvent, slot);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 break;
109 case ABS_MT_TOUCH_MAJOR:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800110 slot.mAbsMTTouchMajor = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700111 break;
112 case ABS_MT_TOUCH_MINOR:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800113 slot.mAbsMTTouchMinor = rawEvent->value;
114 slot.mHaveAbsMTTouchMinor = true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 break;
116 case ABS_MT_WIDTH_MAJOR:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800117 slot.mAbsMTWidthMajor = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 break;
119 case ABS_MT_WIDTH_MINOR:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800120 slot.mAbsMTWidthMinor = rawEvent->value;
121 slot.mHaveAbsMTWidthMinor = true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122 break;
123 case ABS_MT_ORIENTATION:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800124 slot.mAbsMTOrientation = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700125 break;
126 case ABS_MT_TRACKING_ID:
127 if (mUsingSlotsProtocol && rawEvent->value < 0) {
128 // The slot is no longer in use but it retains its previous contents,
129 // which may be reused for subsequent touches.
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800130 slot.mInUse = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131 } else {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800132 slot.mInUse = true;
133 slot.mAbsMTTrackingId = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134 }
135 break;
136 case ABS_MT_PRESSURE:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800137 slot.mAbsMTPressure = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 break;
139 case ABS_MT_DISTANCE:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800140 slot.mAbsMTDistance = rawEvent->value;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 break;
142 case ABS_MT_TOOL_TYPE:
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800143 slot.mAbsMTToolType = rawEvent->value;
144 slot.mHaveAbsMTToolType = true;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700145 break;
146 }
147 }
148 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
149 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
150 mCurrentSlot += 1;
151 }
152}
153
154void MultiTouchMotionAccumulator::finishSync() {
155 if (!mUsingSlotsProtocol) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +0000156 resetSlots();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700157 }
158}
159
160bool MultiTouchMotionAccumulator::hasStylus() const {
161 return mHaveStylus;
162}
163
Arthur Hung9ad18942021-06-19 02:04:46 +0000164void MultiTouchMotionAccumulator::warnIfNotInUse(const RawEvent& event, const Slot& slot) {
165 if (!slot.mInUse) {
166 ALOGW("Received unexpected event (0x%0x, 0x%0x) for slot %i with tracking id %i",
167 event.code, event.value, mCurrentSlot, slot.mAbsMTTrackingId);
168 }
169}
170
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700171// --- MultiTouchMotionAccumulator::Slot ---
172
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
174 if (mHaveAbsMTToolType) {
175 switch (mAbsMTToolType) {
176 case MT_TOOL_FINGER:
177 return AMOTION_EVENT_TOOL_TYPE_FINGER;
178 case MT_TOOL_PEN:
179 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
Arthur Hung421eb1c2020-01-16 00:09:42 +0800180 case MT_TOOL_PALM:
181 return AMOTION_EVENT_TOOL_TYPE_PALM;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182 }
183 }
184 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
185}
186
187// --- MultiTouchInputMapper ---
188
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800189MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext)
190 : TouchInputMapper(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700191
192MultiTouchInputMapper::~MultiTouchInputMapper() {}
193
194void MultiTouchInputMapper::reset(nsecs_t when) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +0000195 // The evdev multi-touch protocol does not allow userspace applications to query the initial or
196 // current state of the pointers at any time. This means if we clear our accumulated state when
197 // resetting the input mapper, there's no way to rebuild the full initial state of the pointers.
198 // We can only wait for updates to all the pointers and axes. Rather than clearing the state and
199 // rebuilding the state from scratch, we work around this kernel API limitation by never
200 // fully clearing any state specific to the multi-touch protocol.
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201 TouchInputMapper::reset(when);
202}
203
204void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
205 TouchInputMapper::process(rawEvent);
206
207 mMultiTouchMotionAccumulator.process(rawEvent);
208}
209
arthurhungcc7f9802020-04-30 17:55:40 +0800210std::optional<int32_t> MultiTouchInputMapper::getActiveBitId(
211 const MultiTouchMotionAccumulator::Slot& inSlot) {
212 if (mHavePointerIds) {
213 int32_t trackingId = inSlot.getTrackingId();
214 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
215 int32_t n = idBits.clearFirstMarkedBit();
216 if (mPointerTrackingIdMap[n] == trackingId) {
217 return std::make_optional(n);
218 }
219 }
220 }
221 return std::nullopt;
222}
223
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
225 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
226 size_t outCount = 0;
227 BitSet32 newPointerIdBits;
228 mHavePointerIds = true;
229
230 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800231 const MultiTouchMotionAccumulator::Slot& inSlot =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700232 mMultiTouchMotionAccumulator.getSlot(inIndex);
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800233 if (!inSlot.isInUse()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700234 continue;
235 }
236
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800237 if (inSlot.getToolType() == AMOTION_EVENT_TOOL_TYPE_PALM) {
238 std::optional<int32_t> id = getActiveBitId(inSlot);
arthurhungcc7f9802020-04-30 17:55:40 +0800239 if (id) {
240 outState->rawPointerData.canceledIdBits.markBit(id.value());
Arthur Hung421eb1c2020-01-16 00:09:42 +0800241 }
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -0500242 if (DEBUG_POINTERS) {
243 ALOGI("Stop processing slot %zu for it received a palm event from device %s",
244 inIndex, getDeviceName().c_str());
245 }
arthurhungbf89a482020-04-17 17:37:55 +0800246 continue;
Arthur Hung421eb1c2020-01-16 00:09:42 +0800247 }
248
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700249 if (outCount >= MAX_POINTERS) {
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -0500250 if (DEBUG_POINTERS) {
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800251 ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; "
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -0500252 "ignoring the rest.",
253 getDeviceName().c_str(), MAX_POINTERS);
254 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 break; // too many fingers!
256 }
257
258 RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800259 outPointer.x = inSlot.getX();
260 outPointer.y = inSlot.getY();
261 outPointer.pressure = inSlot.getPressure();
262 outPointer.touchMajor = inSlot.getTouchMajor();
263 outPointer.touchMinor = inSlot.getTouchMinor();
264 outPointer.toolMajor = inSlot.getToolMajor();
265 outPointer.toolMinor = inSlot.getToolMinor();
266 outPointer.orientation = inSlot.getOrientation();
267 outPointer.distance = inSlot.getDistance();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 outPointer.tiltX = 0;
269 outPointer.tiltY = 0;
270
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800271 outPointer.toolType = inSlot.getToolType();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700272 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
273 outPointer.toolType = mTouchButtonAccumulator.getToolType();
274 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
275 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
276 }
277 }
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800278 if (shouldSimulateStylusWithTouch() &&
279 outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) {
280 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
281 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700282
283 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE &&
284 (mTouchButtonAccumulator.isHovering() ||
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800285 (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700286 outPointer.isHovering = isHovering;
287
288 // Assign pointer id using tracking id if available.
289 if (mHavePointerIds) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800290 int32_t trackingId = inSlot.getTrackingId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700291 int32_t id = -1;
292 if (trackingId >= 0) {
293 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
294 uint32_t n = idBits.clearFirstMarkedBit();
295 if (mPointerTrackingIdMap[n] == trackingId) {
296 id = n;
297 }
298 }
299
300 if (id < 0 && !mPointerIdBits.isFull()) {
301 id = mPointerIdBits.markFirstUnmarkedBit();
302 mPointerTrackingIdMap[id] = trackingId;
303 }
304 }
305 if (id < 0) {
306 mHavePointerIds = false;
307 outState->rawPointerData.clearIdBits();
308 newPointerIdBits.clear();
309 } else {
310 outPointer.id = id;
311 outState->rawPointerData.idToIndex[id] = outCount;
312 outState->rawPointerData.markIdBit(id, isHovering);
313 newPointerIdBits.markBit(id);
314 }
315 }
316 outCount += 1;
317 }
318
319 outState->rawPointerData.pointerCount = outCount;
320 mPointerIdBits = newPointerIdBits;
321
322 mMultiTouchMotionAccumulator.finishSync();
323}
324
325void MultiTouchInputMapper::configureRawPointerAxes() {
326 TouchInputMapper::configureRawPointerAxes();
327
328 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
329 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
330 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
331 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
332 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
333 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
334 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
335 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
336 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
337 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
338 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
339
340 if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid &&
341 mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
342 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
343 if (slotCount > MAX_SLOTS) {
344 ALOGW("MultiTouch Device %s reported %zu slots but the framework "
345 "only supports a maximum of %zu slots at this time.",
346 getDeviceName().c_str(), slotCount, MAX_SLOTS);
347 slotCount = MAX_SLOTS;
348 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800349 mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount,
350 true /*usingSlotsProtocol*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700351 } else {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800352 mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700353 false /*usingSlotsProtocol*/);
354 }
355}
356
357bool MultiTouchInputMapper::hasStylus() const {
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800358 return mMultiTouchMotionAccumulator.hasStylus() || mTouchButtonAccumulator.hasStylus() ||
359 shouldSimulateStylusWithTouch();
360}
361
362bool MultiTouchInputMapper::shouldSimulateStylusWithTouch() const {
363 static const bool SIMULATE_STYLUS_WITH_TOUCH =
364 sysprop::InputProperties::simulate_stylus_with_touch().value_or(false);
365 return SIMULATE_STYLUS_WITH_TOUCH &&
366 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700367}
368
369} // namespace android