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) { |
Prabir Pradhan | f8d9e44 | 2023-12-06 22:06:13 +0000 | [diff] [blame^] | 38 | // TODO(b/291626046): Sync the MT state with the kernel using EVIOCGMTSLOTS. |
| 39 | mMultiTouchMotionAccumulator.reset(getDeviceContext()); |
| 40 | mPointerIdBits.clear(); |
| 41 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 42 | return TouchInputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 45 | std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 46 | std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 47 | |
| 48 | mMultiTouchMotionAccumulator.process(rawEvent); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 49 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 52 | std::optional<int32_t> MultiTouchInputMapper::getActiveBitId( |
| 53 | const MultiTouchMotionAccumulator::Slot& inSlot) { |
| 54 | if (mHavePointerIds) { |
| 55 | int32_t trackingId = inSlot.getTrackingId(); |
| 56 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) { |
| 57 | int32_t n = idBits.clearFirstMarkedBit(); |
| 58 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 59 | return std::make_optional(n); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return std::nullopt; |
| 64 | } |
| 65 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) { |
| 67 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
| 68 | size_t outCount = 0; |
| 69 | BitSet32 newPointerIdBits; |
| 70 | mHavePointerIds = true; |
| 71 | |
| 72 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 73 | const MultiTouchMotionAccumulator::Slot& inSlot = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 74 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 75 | if (!inSlot.isInUse()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 76 | continue; |
| 77 | } |
| 78 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 79 | if (inSlot.getToolType() == ToolType::PALM) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 80 | std::optional<int32_t> id = getActiveBitId(inSlot); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 81 | if (id) { |
| 82 | outState->rawPointerData.canceledIdBits.markBit(id.value()); |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 83 | } |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 84 | if (DEBUG_POINTERS) { |
| 85 | ALOGI("Stop processing slot %zu for it received a palm event from device %s", |
| 86 | inIndex, getDeviceName().c_str()); |
| 87 | } |
arthurhung | bf89a48 | 2020-04-17 17:37:55 +0800 | [diff] [blame] | 88 | continue; |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 91 | if (outCount >= MAX_POINTERS) { |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 92 | if (DEBUG_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 93 | ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; " |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 94 | "ignoring the rest.", |
| 95 | getDeviceName().c_str(), MAX_POINTERS); |
| 96 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 97 | break; // too many fingers! |
| 98 | } |
| 99 | |
| 100 | RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount]; |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 101 | outPointer.x = inSlot.getX(); |
| 102 | outPointer.y = inSlot.getY(); |
| 103 | outPointer.pressure = inSlot.getPressure(); |
| 104 | outPointer.touchMajor = inSlot.getTouchMajor(); |
| 105 | outPointer.touchMinor = inSlot.getTouchMinor(); |
| 106 | outPointer.toolMajor = inSlot.getToolMajor(); |
| 107 | outPointer.toolMinor = inSlot.getToolMinor(); |
| 108 | outPointer.orientation = inSlot.getOrientation(); |
| 109 | outPointer.distance = inSlot.getDistance(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 110 | outPointer.tiltX = 0; |
| 111 | outPointer.tiltY = 0; |
| 112 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 113 | outPointer.toolType = inSlot.getToolType(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 114 | if (outPointer.toolType == ToolType::UNKNOWN) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 115 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 116 | if (outPointer.toolType == ToolType::UNKNOWN) { |
| 117 | outPointer.toolType = ToolType::FINGER; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 118 | } |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 119 | } else if (outPointer.toolType == ToolType::STYLUS && !mStylusMtToolSeen) { |
Prabir Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 120 | mStylusMtToolSeen = true; |
| 121 | // The multi-touch device produced a stylus event with MT_TOOL_PEN. Dynamically |
| 122 | // re-configure this input device so that we add SOURCE_STYLUS if we haven't already. |
| 123 | // This is to cover the case where we cannot reliably detect whether a multi-touch |
| 124 | // device will ever produce stylus events when it is initially being configured. |
| 125 | if (!isFromSource(mSource, AINPUT_SOURCE_STYLUS)) { |
| 126 | // Add the stylus source immediately so that it is included in any events generated |
| 127 | // before we have a chance to re-configure the device. |
| 128 | mSource |= AINPUT_SOURCE_STYLUS; |
| 129 | bumpGeneration(); |
| 130 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 131 | } |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 132 | if (mShouldSimulateStylusWithTouch && outPointer.toolType == ToolType::FINGER) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 133 | outPointer.toolType = ToolType::STYLUS; |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 134 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 135 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 136 | bool isHovering = mTouchButtonAccumulator.getToolType() != ToolType::MOUSE && |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 137 | (mTouchButtonAccumulator.isHovering() || |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 138 | (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 139 | outPointer.isHovering = isHovering; |
| 140 | |
| 141 | // Assign pointer id using tracking id if available. |
| 142 | if (mHavePointerIds) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 143 | int32_t trackingId = inSlot.getTrackingId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 144 | int32_t id = -1; |
| 145 | if (trackingId >= 0) { |
| 146 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) { |
| 147 | uint32_t n = idBits.clearFirstMarkedBit(); |
| 148 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 149 | id = n; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (id < 0 && !mPointerIdBits.isFull()) { |
| 154 | id = mPointerIdBits.markFirstUnmarkedBit(); |
| 155 | mPointerTrackingIdMap[id] = trackingId; |
| 156 | } |
| 157 | } |
| 158 | if (id < 0) { |
| 159 | mHavePointerIds = false; |
| 160 | outState->rawPointerData.clearIdBits(); |
| 161 | newPointerIdBits.clear(); |
| 162 | } else { |
| 163 | outPointer.id = id; |
| 164 | outState->rawPointerData.idToIndex[id] = outCount; |
| 165 | outState->rawPointerData.markIdBit(id, isHovering); |
| 166 | newPointerIdBits.markBit(id); |
| 167 | } |
| 168 | } |
| 169 | outCount += 1; |
| 170 | } |
| 171 | |
| 172 | outState->rawPointerData.pointerCount = outCount; |
| 173 | mPointerIdBits = newPointerIdBits; |
| 174 | |
| 175 | mMultiTouchMotionAccumulator.finishSync(); |
| 176 | } |
| 177 | |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 178 | std::list<NotifyArgs> MultiTouchInputMapper::reconfigure(nsecs_t when, |
| 179 | const InputReaderConfiguration& config, |
| 180 | ConfigurationChanges changes) { |
| 181 | const bool simulateStylusWithTouch = |
| 182 | sysprop::InputProperties::simulate_stylus_with_touch().value_or(false); |
| 183 | if (simulateStylusWithTouch != mShouldSimulateStylusWithTouch) { |
| 184 | mShouldSimulateStylusWithTouch = simulateStylusWithTouch; |
| 185 | bumpGeneration(); |
| 186 | } |
| 187 | return TouchInputMapper::reconfigure(when, config, changes); |
| 188 | } |
| 189 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 190 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 191 | TouchInputMapper::configureRawPointerAxes(); |
| 192 | |
| 193 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 194 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 195 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 196 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 197 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 198 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 199 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 200 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 201 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 202 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 203 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
| 204 | |
| 205 | if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid && |
| 206 | mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 207 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
| 208 | if (slotCount > MAX_SLOTS) { |
| 209 | ALOGW("MultiTouch Device %s reported %zu slots but the framework " |
| 210 | "only supports a maximum of %zu slots at this time.", |
| 211 | getDeviceName().c_str(), slotCount, MAX_SLOTS); |
| 212 | slotCount = MAX_SLOTS; |
| 213 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 214 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 215 | /*usingSlotsProtocol=*/true); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 216 | } else { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 217 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 218 | /*usingSlotsProtocol=*/false); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | bool MultiTouchInputMapper::hasStylus() const { |
Prabir Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 223 | return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() || |
Prabir Pradhan | 522758a | 2023-11-14 23:55:51 +0000 | [diff] [blame] | 224 | mShouldSimulateStylusWithTouch; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace android |