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 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 31 | MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext) |
| 32 | : TouchInputMapper(deviceContext) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 33 | |
| 34 | MultiTouchInputMapper::~MultiTouchInputMapper() {} |
| 35 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 36 | std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) { |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 37 | // 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 Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 43 | return TouchInputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 46 | std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 47 | std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 48 | |
| 49 | mMultiTouchMotionAccumulator.process(rawEvent); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 50 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | } |
| 52 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 53 | std::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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 67 | void 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 Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 74 | const MultiTouchMotionAccumulator::Slot& inSlot = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 75 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 76 | if (!inSlot.isInUse()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 77 | continue; |
| 78 | } |
| 79 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 80 | if (inSlot.getToolType() == AMOTION_EVENT_TOOL_TYPE_PALM) { |
| 81 | std::optional<int32_t> id = getActiveBitId(inSlot); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 82 | if (id) { |
| 83 | outState->rawPointerData.canceledIdBits.markBit(id.value()); |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 84 | } |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 85 | if (DEBUG_POINTERS) { |
| 86 | ALOGI("Stop processing slot %zu for it received a palm event from device %s", |
| 87 | inIndex, getDeviceName().c_str()); |
| 88 | } |
arthurhung | bf89a48 | 2020-04-17 17:37:55 +0800 | [diff] [blame] | 89 | continue; |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 92 | if (outCount >= MAX_POINTERS) { |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 93 | if (DEBUG_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 94 | ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; " |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 95 | "ignoring the rest.", |
| 96 | getDeviceName().c_str(), MAX_POINTERS); |
| 97 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 98 | break; // too many fingers! |
| 99 | } |
| 100 | |
| 101 | RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount]; |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 102 | 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 111 | outPointer.tiltX = 0; |
| 112 | outPointer.tiltY = 0; |
| 113 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 114 | outPointer.toolType = inSlot.getToolType(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 115 | 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 Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 120 | } 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 132 | } |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 133 | if (shouldSimulateStylusWithTouch() && |
| 134 | outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) { |
| 135 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 136 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 137 | |
| 138 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE && |
| 139 | (mTouchButtonAccumulator.isHovering() || |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 140 | (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 141 | outPointer.isHovering = isHovering; |
| 142 | |
| 143 | // Assign pointer id using tracking id if available. |
| 144 | if (mHavePointerIds) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 145 | int32_t trackingId = inSlot.getTrackingId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 146 | 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 | |
| 180 | void 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. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 204 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 205 | /*usingSlotsProtocol=*/true); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 206 | } else { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 207 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 208 | /*usingSlotsProtocol=*/false); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | bool MultiTouchInputMapper::hasStylus() const { |
Prabir Pradhan | 5d0d97d | 2022-11-17 01:06:01 +0000 | [diff] [blame] | 213 | return mStylusMtToolSeen || mTouchButtonAccumulator.hasStylus() || |
| 214 | shouldSimulateStylusWithTouch(); |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } // namespace android |