blob: 2dd05f5c66f503c70066f92deaea9afa2dc68ee4 [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) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +000038 // The evdev multi-touch protocol does not allow userspace applications to query the initial or
39 // current state of the pointers at any time. This means if we clear our accumulated state when
40 // resetting the input mapper, there's no way to rebuild the full initial state of the pointers.
41 // We can only wait for updates to all the pointers and axes. Rather than clearing the state and
42 // rebuilding the state from scratch, we work around this kernel API limitation by never
43 // fully clearing any state specific to the multi-touch protocol.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070044 return TouchInputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070045}
46
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent* rawEvent) {
48 std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070049
50 mMultiTouchMotionAccumulator.process(rawEvent);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070051 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052}
53
arthurhungcc7f9802020-04-30 17:55:40 +080054std::optional<int32_t> MultiTouchInputMapper::getActiveBitId(
55 const MultiTouchMotionAccumulator::Slot& inSlot) {
56 if (mHavePointerIds) {
57 int32_t trackingId = inSlot.getTrackingId();
58 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) {
59 int32_t n = idBits.clearFirstMarkedBit();
60 if (mPointerTrackingIdMap[n] == trackingId) {
61 return std::make_optional(n);
62 }
63 }
64 }
65 return std::nullopt;
66}
67
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) {
69 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
70 size_t outCount = 0;
71 BitSet32 newPointerIdBits;
72 mHavePointerIds = true;
73
74 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080075 const MultiTouchMotionAccumulator::Slot& inSlot =
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 mMultiTouchMotionAccumulator.getSlot(inIndex);
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080077 if (!inSlot.isInUse()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 continue;
79 }
80
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070081 if (inSlot.getToolType() == ToolType::PALM) {
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080082 std::optional<int32_t> id = getActiveBitId(inSlot);
arthurhungcc7f9802020-04-30 17:55:40 +080083 if (id) {
84 outState->rawPointerData.canceledIdBits.markBit(id.value());
Arthur Hung421eb1c2020-01-16 00:09:42 +080085 }
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050086 if (DEBUG_POINTERS) {
87 ALOGI("Stop processing slot %zu for it received a palm event from device %s",
88 inIndex, getDeviceName().c_str());
89 }
arthurhungbf89a482020-04-17 17:37:55 +080090 continue;
Arthur Hung421eb1c2020-01-16 00:09:42 +080091 }
92
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 if (outCount >= MAX_POINTERS) {
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050094 if (DEBUG_POINTERS) {
Siarhei Vishniakou01747382022-01-20 13:23:27 -080095 ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; "
Siarhei Vishniakoue3ce49d2020-09-29 19:08:13 -050096 "ignoring the rest.",
97 getDeviceName().c_str(), MAX_POINTERS);
98 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099 break; // too many fingers!
100 }
101
102 RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount];
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800103 outPointer.x = inSlot.getX();
104 outPointer.y = inSlot.getY();
105 outPointer.pressure = inSlot.getPressure();
106 outPointer.touchMajor = inSlot.getTouchMajor();
107 outPointer.touchMinor = inSlot.getTouchMinor();
108 outPointer.toolMajor = inSlot.getToolMajor();
109 outPointer.toolMinor = inSlot.getToolMinor();
110 outPointer.orientation = inSlot.getOrientation();
111 outPointer.distance = inSlot.getDistance();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112 outPointer.tiltX = 0;
113 outPointer.tiltY = 0;
114
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -0800115 outPointer.toolType = inSlot.getToolType();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700116 if (outPointer.toolType == ToolType::UNKNOWN) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 outPointer.toolType = mTouchButtonAccumulator.getToolType();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700118 if (outPointer.toolType == ToolType::UNKNOWN) {
119 outPointer.toolType = ToolType::FINGER;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700121 } else if (outPointer.toolType == ToolType::STYLUS && !mStylusMtToolSeen) {
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000122 mStylusMtToolSeen = true;
123 // The multi-touch device produced a stylus event with MT_TOOL_PEN. Dynamically
124 // re-configure this input device so that we add SOURCE_STYLUS if we haven't already.
125 // This is to cover the case where we cannot reliably detect whether a multi-touch
126 // device will ever produce stylus events when it is initially being configured.
127 if (!isFromSource(mSource, AINPUT_SOURCE_STYLUS)) {
128 // Add the stylus source immediately so that it is included in any events generated
129 // before we have a chance to re-configure the device.
130 mSource |= AINPUT_SOURCE_STYLUS;
131 bumpGeneration();
132 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133 }
Prabir Pradhan522758a2023-11-14 23:55:51 +0000134 if (mShouldSimulateStylusWithTouch && outPointer.toolType == ToolType::FINGER) {
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700135 outPointer.toolType = ToolType::STYLUS;
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800136 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700138 bool isHovering = mTouchButtonAccumulator.getToolType() != ToolType::MOUSE &&
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139 (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
Prabir Pradhan522758a2023-11-14 23:55:51 +0000180std::list<NotifyArgs> MultiTouchInputMapper::reconfigure(nsecs_t when,
181 const InputReaderConfiguration& config,
182 ConfigurationChanges changes) {
183 const bool simulateStylusWithTouch =
184 sysprop::InputProperties::simulate_stylus_with_touch().value_or(false);
185 if (simulateStylusWithTouch != mShouldSimulateStylusWithTouch) {
186 mShouldSimulateStylusWithTouch = simulateStylusWithTouch;
187 bumpGeneration();
188 }
189 return TouchInputMapper::reconfigure(when, config, changes);
190}
191
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700192void MultiTouchInputMapper::configureRawPointerAxes() {
193 TouchInputMapper::configureRawPointerAxes();
194
195 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
196 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
197 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
198 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
199 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
200 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
201 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
202 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
203 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
204 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
205 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
206
207 if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid &&
208 mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
209 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
210 if (slotCount > MAX_SLOTS) {
211 ALOGW("MultiTouch Device %s reported %zu slots but the framework "
212 "only supports a maximum of %zu slots at this time.",
213 getDeviceName().c_str(), slotCount, MAX_SLOTS);
214 slotCount = MAX_SLOTS;
215 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800216 mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount,
Harry Cutts33476232023-01-30 19:57:29 +0000217 /*usingSlotsProtocol=*/true);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700218 } else {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800219 mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS,
Harry Cutts33476232023-01-30 19:57:29 +0000220 /*usingSlotsProtocol=*/false);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700221 }
222}
223
224bool MultiTouchInputMapper::hasStylus() const {
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +0000225 return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() ||
Prabir Pradhan522758a2023-11-14 23:55:51 +0000226 mShouldSimulateStylusWithTouch;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700227}
228
229} // namespace android