blob: 33e72c7c6fcfd1e35f66b7ee651b581dd271acd0 [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
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080031MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext)
32 : TouchInputMapper(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070033
34MultiTouchInputMapper::~MultiTouchInputMapper() {}
35
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070036std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +000037 // The evdev multi-touch protocol does not allow userspace applications to query the initial or
38 // current state of the pointers at any time. This means if we clear our accumulated state when
39 // resetting the input mapper, there's no way to rebuild the full initial state of the pointers.
40 // We can only wait for updates to all the pointers and axes. Rather than clearing the state and
41 // rebuilding the state from scratch, we work around this kernel API limitation by never
42 // fully clearing any state specific to the multi-touch protocol.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070043 return TouchInputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044}
45
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070046std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent* rawEvent) {
47 std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048
49 mMultiTouchMotionAccumulator.process(rawEvent);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070050 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051}
52
arthurhungcc7f9802020-04-30 17:55:40 +080053std::optional<int32_t> MultiTouchInputMapper::getActiveBitId(
54 const MultiTouchMotionAccumulator::Slot& inSlot) {
55 if (mHavePointerIds) {
56 int32_t trackingId = inSlot.getTrackingId();
57 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
58 int32_t n = idBits.clearFirstMarkedBit();
59 if (mPointerTrackingIdMap[n] == trackingId) {
60 return std::make_optional(n);
61 }
62 }
63 }
64 return std::nullopt;
65}
66
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
68 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
69 size_t outCount = 0;
70 BitSet32 newPointerIdBits;
71 mHavePointerIds = true;
72
73 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080074 const MultiTouchMotionAccumulator::Slot& inSlot =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 mMultiTouchMotionAccumulator.getSlot(inIndex);
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080076 if (!inSlot.isInUse()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077 continue;
78 }
79
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080080 if (inSlot.getToolType() == AMOTION_EVENT_TOOL_TYPE_PALM) {
81 std::optional<int32_t> id = getActiveBitId(inSlot);
arthurhungcc7f9802020-04-30 17:55:40 +080082 if (id) {
83 outState->rawPointerData.canceledIdBits.markBit(id.value());
Arthur Hung421eb1c2020-01-16 00:09:42 +080084 }
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050085 if (DEBUG_POINTERS) {
86 ALOGI("Stop processing slot %zu for it received a palm event from device %s",
87 inIndex, getDeviceName().c_str());
88 }
arthurhungbf89a482020-04-17 17:37:55 +080089 continue;
Arthur Hung421eb1c2020-01-16 00:09:42 +080090 }
91
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092 if (outCount >= MAX_POINTERS) {
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050093 if (DEBUG_POINTERS) {
Siarhei Vishniakou01747382022-01-20 13:23:27 -080094 ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; "
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050095 "ignoring the rest.",
96 getDeviceName().c_str(), MAX_POINTERS);
97 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 break; // too many fingers!
99 }
100
101 RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800102 outPointer.x = inSlot.getX();
103 outPointer.y = inSlot.getY();
104 outPointer.pressure = inSlot.getPressure();
105 outPointer.touchMajor = inSlot.getTouchMajor();
106 outPointer.touchMinor = inSlot.getTouchMinor();
107 outPointer.toolMajor = inSlot.getToolMajor();
108 outPointer.toolMinor = inSlot.getToolMinor();
109 outPointer.orientation = inSlot.getOrientation();
110 outPointer.distance = inSlot.getDistance();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700111 outPointer.tiltX = 0;
112 outPointer.tiltY = 0;
113
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800114 outPointer.toolType = inSlot.getToolType();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
116 outPointer.toolType = mTouchButtonAccumulator.getToolType();
117 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
118 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
119 }
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000120 } else if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS && !mStylusMtToolSeen) {
121 mStylusMtToolSeen = true;
122 // The multi-touch device produced a stylus event with MT_TOOL_PEN. Dynamically
123 // re-configure this input device so that we add SOURCE_STYLUS if we haven't already.
124 // This is to cover the case where we cannot reliably detect whether a multi-touch
125 // device will ever produce stylus events when it is initially being configured.
126 if (!isFromSource(mSource, AINPUT_SOURCE_STYLUS)) {
127 // Add the stylus source immediately so that it is included in any events generated
128 // before we have a chance to re-configure the device.
129 mSource |= AINPUT_SOURCE_STYLUS;
130 bumpGeneration();
131 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132 }
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800133 if (shouldSimulateStylusWithTouch() &&
134 outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) {
135 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
136 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137
138 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE &&
139 (mTouchButtonAccumulator.isHovering() ||
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800140 (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700141 outPointer.isHovering = isHovering;
142
143 // Assign pointer id using tracking id if available.
144 if (mHavePointerIds) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800145 int32_t trackingId = inSlot.getTrackingId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700146 int32_t id = -1;
147 if (trackingId >= 0) {
148 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
149 uint32_t n = idBits.clearFirstMarkedBit();
150 if (mPointerTrackingIdMap[n] == trackingId) {
151 id = n;
152 }
153 }
154
155 if (id < 0 && !mPointerIdBits.isFull()) {
156 id = mPointerIdBits.markFirstUnmarkedBit();
157 mPointerTrackingIdMap[id] = trackingId;
158 }
159 }
160 if (id < 0) {
161 mHavePointerIds = false;
162 outState->rawPointerData.clearIdBits();
163 newPointerIdBits.clear();
164 } else {
165 outPointer.id = id;
166 outState->rawPointerData.idToIndex[id] = outCount;
167 outState->rawPointerData.markIdBit(id, isHovering);
168 newPointerIdBits.markBit(id);
169 }
170 }
171 outCount += 1;
172 }
173
174 outState->rawPointerData.pointerCount = outCount;
175 mPointerIdBits = newPointerIdBits;
176
177 mMultiTouchMotionAccumulator.finishSync();
178}
179
180void MultiTouchInputMapper::configureRawPointerAxes() {
181 TouchInputMapper::configureRawPointerAxes();
182
183 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
184 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
185 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
186 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
187 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
188 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
189 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
190 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
191 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
192 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
193 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
194
195 if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid &&
196 mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
197 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
198 if (slotCount > MAX_SLOTS) {
199 ALOGW("MultiTouch Device %s reported %zu slots but the framework "
200 "only supports a maximum of %zu slots at this time.",
201 getDeviceName().c_str(), slotCount, MAX_SLOTS);
202 slotCount = MAX_SLOTS;
203 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800204 mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount,
Harry Cutts33476232023-01-30 19:57:29 +0000205 /*usingSlotsProtocol=*/true);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700206 } else {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207 mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS,
Harry Cutts33476232023-01-30 19:57:29 +0000208 /*usingSlotsProtocol=*/false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209 }
210}
211
212bool MultiTouchInputMapper::hasStylus() const {
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000213 return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() ||
214 shouldSimulateStylusWithTouch();
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800215}
216
217bool MultiTouchInputMapper::shouldSimulateStylusWithTouch() const {
218 static const bool SIMULATE_STYLUS_WITH_TOUCH =
219 sysprop::InputProperties::simulate_stylus_with_touch().value_or(false);
220 return SIMULATE_STYLUS_WITH_TOUCH &&
221 mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700222}
223
224} // namespace android