Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 17 | #include "../Macros.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 19 | #include <android/sysprop/InputProperties.sysprop.h> |
Siarhei Vishniakou | 9faa446 | 2022-12-06 11:45:04 -0800 | [diff] [blame] | 20 | #include "MultiTouchInputMapper.h" |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 21 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 29 | // --- MultiTouchInputMapper --- |
| 30 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 31 | MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext, |
| 32 | const InputReaderConfiguration& readerConfig) |
| 33 | : TouchInputMapper(deviceContext, readerConfig) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 34 | |
| 35 | MultiTouchInputMapper::~MultiTouchInputMapper() {} |
| 36 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 37 | std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) { |
Arpit Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 38 | mPointerIdBits.clear(); |
| 39 | mMultiTouchMotionAccumulator.reset(mDeviceContext); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 40 | return TouchInputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 43 | std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent& rawEvent) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 44 | std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 45 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 46 | mMultiTouchMotionAccumulator.process(rawEvent); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 47 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 48 | } |
| 49 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 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 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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++) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 71 | const MultiTouchMotionAccumulator::Slot& inSlot = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 72 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 73 | if (!inSlot.isInUse()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 74 | continue; |
| 75 | } |
| 76 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 77 | if (inSlot.getToolType() == ToolType::PALM) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 78 | std::optional<int32_t> id = getActiveBitId(inSlot); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 79 | if (id) { |
| 80 | outState->rawPointerData.canceledIdBits.markBit(id.value()); |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 81 | } |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 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 | } |
arthurhung | bf89a48 | 2020-04-17 17:37:55 +0800 | [diff] [blame] | 86 | continue; |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 89 | if (outCount >= MAX_POINTERS) { |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 90 | if (DEBUG_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 91 | ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; " |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 92 | "ignoring the rest.", |
| 93 | getDeviceName().c_str(), MAX_POINTERS); |
| 94 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 95 | break; // too many fingers! |
| 96 | } |
| 97 | |
| 98 | RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount]; |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 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(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 108 | outPointer.tiltX = 0; |
| 109 | outPointer.tiltY = 0; |
| 110 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 111 | outPointer.toolType = inSlot.getToolType(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 112 | if (outPointer.toolType == ToolType::UNKNOWN) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 113 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 114 | if (outPointer.toolType == ToolType::UNKNOWN) { |
| 115 | outPointer.toolType = ToolType::FINGER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 116 | } |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 117 | } else if (outPointer.toolType == ToolType::STYLUS && !mStylusMtToolSeen) { |
Prabir Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 129 | } |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 130 | if (mShouldSimulateStylusWithTouch && outPointer.toolType == ToolType::FINGER) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 131 | outPointer.toolType = ToolType::STYLUS; |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 132 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 133 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 134 | bool isHovering = mTouchButtonAccumulator.getToolType() != ToolType::MOUSE && |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 135 | (mTouchButtonAccumulator.isHovering() || |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 136 | (mRawPointerAxes.pressure && inSlot.getPressure() <= 0)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 137 | outPointer.isHovering = isHovering; |
| 138 | |
| 139 | // Assign pointer id using tracking id if available. |
| 140 | if (mHavePointerIds) { |
Linnan Li | 3db547c | 2024-04-26 11:13:00 +0000 | [diff] [blame] | 141 | const int32_t trackingId = inSlot.getTrackingId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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; |
Linnan Li | 3db547c | 2024-04-26 11:13:00 +0000 | [diff] [blame] | 148 | break; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 189 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 190 | TouchInputMapper::configureRawPointerAxes(); |
| 191 | |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 192 | // 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 209 | |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 210 | if (mRawPointerAxes.trackingId && mRawPointerAxes.slot && mRawPointerAxes.slot->minValue == 0 && |
| 211 | mRawPointerAxes.slot->maxValue > 0) { |
| 212 | size_t slotCount = mRawPointerAxes.slot->maxValue + 1; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 213 | 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. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 219 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 220 | /*usingSlotsProtocol=*/true); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 221 | } else { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 222 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 223 | /*usingSlotsProtocol=*/false); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | bool MultiTouchInputMapper::hasStylus() const { |
Prabir Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 228 | return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() || |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 229 | mShouldSimulateStylusWithTouch; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } // namespace android |