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 | |
| 19 | #include "MultiTouchInputMapper.h" |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame^] | 20 | #if defined(__ANDROID__) |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 21 | #include <android/sysprop/InputProperties.sysprop.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame^] | 22 | #endif |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 23 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | |
| 26 | // --- Constants --- |
| 27 | |
| 28 | // Maximum number of slots supported when using the slot-based Multitouch Protocol B. |
| 29 | static constexpr size_t MAX_SLOTS = 32; |
| 30 | |
| 31 | // --- MultiTouchMotionAccumulator --- |
| 32 | |
| 33 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() |
| 34 | : mCurrentSlot(-1), |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 35 | mUsingSlotsProtocol(false), |
| 36 | mHaveStylus(false) {} |
| 37 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 38 | void MultiTouchMotionAccumulator::configure(InputDeviceContext& deviceContext, size_t slotCount, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 39 | bool usingSlotsProtocol) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 40 | mUsingSlotsProtocol = usingSlotsProtocol; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 41 | mHaveStylus = deviceContext.hasAbsoluteAxis(ABS_MT_TOOL_TYPE); |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 42 | mSlots = std::vector<Slot>(slotCount); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 43 | |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 44 | mCurrentSlot = -1; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 45 | if (mUsingSlotsProtocol) { |
| 46 | // Query the driver for the current slot index and use it as the initial slot |
| 47 | // before we start reading events from the device. It is possible that the |
| 48 | // current slot index will not be the same as it was when the first event was |
| 49 | // written into the evdev buffer, which means the input mapper could start |
| 50 | // out of sync with the initial state of the events in the evdev buffer. |
| 51 | // In the extremely unlikely case that this happens, the data from |
| 52 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 53 | // This can cause the touch point to "jump", but at least there will be |
| 54 | // no stuck touches. |
| 55 | int32_t initialSlot; |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 56 | if (const auto status = deviceContext.getAbsoluteAxisValue(ABS_MT_SLOT, &initialSlot); |
| 57 | status == OK) { |
| 58 | mCurrentSlot = initialSlot; |
| 59 | } else { |
| 60 | ALOGD("Could not retrieve current multi-touch slot index. status=%d", status); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 61 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 65 | void MultiTouchMotionAccumulator::resetSlots() { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 66 | for (Slot& slot : mSlots) { |
| 67 | slot.clear(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 68 | } |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 69 | mCurrentSlot = -1; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 73 | if (rawEvent->type == EV_ABS) { |
| 74 | bool newSlot = false; |
| 75 | if (mUsingSlotsProtocol) { |
| 76 | if (rawEvent->code == ABS_MT_SLOT) { |
| 77 | mCurrentSlot = rawEvent->value; |
| 78 | newSlot = true; |
| 79 | } |
| 80 | } else if (mCurrentSlot < 0) { |
| 81 | mCurrentSlot = 0; |
| 82 | } |
| 83 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 84 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlots.size()) { |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 85 | if (DEBUG_POINTERS) { |
| 86 | if (newSlot) { |
| 87 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
| 88 | "should be between 0 and %zd; ignoring this slot.", |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 89 | mCurrentSlot, mSlots.size() - 1); |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 90 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 91 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 92 | } else { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 93 | Slot& slot = mSlots[mCurrentSlot]; |
Arthur Hung | 9ad1894 | 2021-06-19 02:04:46 +0000 | [diff] [blame] | 94 | // If mUsingSlotsProtocol is true, it means the raw pointer has axis info of |
| 95 | // ABS_MT_TRACKING_ID and ABS_MT_SLOT, so driver should send a valid trackingId while |
| 96 | // updating the slot. |
| 97 | if (!mUsingSlotsProtocol) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 98 | slot.mInUse = true; |
Arthur Hung | 9ad1894 | 2021-06-19 02:04:46 +0000 | [diff] [blame] | 99 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | |
| 101 | switch (rawEvent->code) { |
| 102 | case ABS_MT_POSITION_X: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 103 | slot.mAbsMTPositionX = rawEvent->value; |
| 104 | warnIfNotInUse(*rawEvent, slot); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 105 | break; |
| 106 | case ABS_MT_POSITION_Y: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 107 | slot.mAbsMTPositionY = rawEvent->value; |
| 108 | warnIfNotInUse(*rawEvent, slot); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 109 | break; |
| 110 | case ABS_MT_TOUCH_MAJOR: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 111 | slot.mAbsMTTouchMajor = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 112 | break; |
| 113 | case ABS_MT_TOUCH_MINOR: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 114 | slot.mAbsMTTouchMinor = rawEvent->value; |
| 115 | slot.mHaveAbsMTTouchMinor = true; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 116 | break; |
| 117 | case ABS_MT_WIDTH_MAJOR: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 118 | slot.mAbsMTWidthMajor = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 119 | break; |
| 120 | case ABS_MT_WIDTH_MINOR: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 121 | slot.mAbsMTWidthMinor = rawEvent->value; |
| 122 | slot.mHaveAbsMTWidthMinor = true; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 123 | break; |
| 124 | case ABS_MT_ORIENTATION: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 125 | slot.mAbsMTOrientation = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 126 | break; |
| 127 | case ABS_MT_TRACKING_ID: |
| 128 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
| 129 | // The slot is no longer in use but it retains its previous contents, |
| 130 | // which may be reused for subsequent touches. |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 131 | slot.mInUse = false; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 132 | } else { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 133 | slot.mInUse = true; |
| 134 | slot.mAbsMTTrackingId = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 135 | } |
| 136 | break; |
| 137 | case ABS_MT_PRESSURE: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 138 | slot.mAbsMTPressure = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 139 | break; |
| 140 | case ABS_MT_DISTANCE: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 141 | slot.mAbsMTDistance = rawEvent->value; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 142 | break; |
| 143 | case ABS_MT_TOOL_TYPE: |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 144 | slot.mAbsMTToolType = rawEvent->value; |
| 145 | slot.mHaveAbsMTToolType = true; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) { |
| 150 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 151 | mCurrentSlot += 1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void MultiTouchMotionAccumulator::finishSync() { |
| 156 | if (!mUsingSlotsProtocol) { |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 157 | resetSlots(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | bool MultiTouchMotionAccumulator::hasStylus() const { |
| 162 | return mHaveStylus; |
| 163 | } |
| 164 | |
Arthur Hung | 9ad1894 | 2021-06-19 02:04:46 +0000 | [diff] [blame] | 165 | void MultiTouchMotionAccumulator::warnIfNotInUse(const RawEvent& event, const Slot& slot) { |
| 166 | if (!slot.mInUse) { |
| 167 | ALOGW("Received unexpected event (0x%0x, 0x%0x) for slot %i with tracking id %i", |
| 168 | event.code, event.value, mCurrentSlot, slot.mAbsMTTrackingId); |
| 169 | } |
| 170 | } |
| 171 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 172 | // --- MultiTouchMotionAccumulator::Slot --- |
| 173 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 174 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 175 | if (mHaveAbsMTToolType) { |
| 176 | switch (mAbsMTToolType) { |
| 177 | case MT_TOOL_FINGER: |
| 178 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 179 | case MT_TOOL_PEN: |
| 180 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 181 | case MT_TOOL_PALM: |
| 182 | return AMOTION_EVENT_TOOL_TYPE_PALM; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 186 | } |
| 187 | |
| 188 | // --- MultiTouchInputMapper --- |
| 189 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 190 | MultiTouchInputMapper::MultiTouchInputMapper(InputDeviceContext& deviceContext) |
| 191 | : TouchInputMapper(deviceContext) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 192 | |
| 193 | MultiTouchInputMapper::~MultiTouchInputMapper() {} |
| 194 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 195 | std::list<NotifyArgs> MultiTouchInputMapper::reset(nsecs_t when) { |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 196 | // The evdev multi-touch protocol does not allow userspace applications to query the initial or |
| 197 | // current state of the pointers at any time. This means if we clear our accumulated state when |
| 198 | // resetting the input mapper, there's no way to rebuild the full initial state of the pointers. |
| 199 | // We can only wait for updates to all the pointers and axes. Rather than clearing the state and |
| 200 | // rebuilding the state from scratch, we work around this kernel API limitation by never |
| 201 | // fully clearing any state specific to the multi-touch protocol. |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 202 | return TouchInputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 205 | std::list<NotifyArgs> MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 206 | std::list<NotifyArgs> out = TouchInputMapper::process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 207 | |
| 208 | mMultiTouchMotionAccumulator.process(rawEvent); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 209 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 210 | } |
| 211 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 212 | std::optional<int32_t> MultiTouchInputMapper::getActiveBitId( |
| 213 | const MultiTouchMotionAccumulator::Slot& inSlot) { |
| 214 | if (mHavePointerIds) { |
| 215 | int32_t trackingId = inSlot.getTrackingId(); |
| 216 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) { |
| 217 | int32_t n = idBits.clearFirstMarkedBit(); |
| 218 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 219 | return std::make_optional(n); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | return std::nullopt; |
| 224 | } |
| 225 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 226 | void MultiTouchInputMapper::syncTouch(nsecs_t when, RawState* outState) { |
| 227 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
| 228 | size_t outCount = 0; |
| 229 | BitSet32 newPointerIdBits; |
| 230 | mHavePointerIds = true; |
| 231 | |
| 232 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 233 | const MultiTouchMotionAccumulator::Slot& inSlot = |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 234 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 235 | if (!inSlot.isInUse()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 236 | continue; |
| 237 | } |
| 238 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 239 | if (inSlot.getToolType() == AMOTION_EVENT_TOOL_TYPE_PALM) { |
| 240 | std::optional<int32_t> id = getActiveBitId(inSlot); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 241 | if (id) { |
| 242 | outState->rawPointerData.canceledIdBits.markBit(id.value()); |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 243 | } |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 244 | if (DEBUG_POINTERS) { |
| 245 | ALOGI("Stop processing slot %zu for it received a palm event from device %s", |
| 246 | inIndex, getDeviceName().c_str()); |
| 247 | } |
arthurhung | bf89a48 | 2020-04-17 17:37:55 +0800 | [diff] [blame] | 248 | continue; |
Arthur Hung | 421eb1c | 2020-01-16 00:09:42 +0800 | [diff] [blame] | 249 | } |
| 250 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 251 | if (outCount >= MAX_POINTERS) { |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 252 | if (DEBUG_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 253 | ALOGD("MultiTouch device %s emitted more than maximum of %zu pointers; " |
Siarhei Vishniakou | e3ce49d | 2020-09-29 19:08:13 -0500 | [diff] [blame] | 254 | "ignoring the rest.", |
| 255 | getDeviceName().c_str(), MAX_POINTERS); |
| 256 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 257 | break; // too many fingers! |
| 258 | } |
| 259 | |
| 260 | RawPointerData::Pointer& outPointer = outState->rawPointerData.pointers[outCount]; |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 261 | outPointer.x = inSlot.getX(); |
| 262 | outPointer.y = inSlot.getY(); |
| 263 | outPointer.pressure = inSlot.getPressure(); |
| 264 | outPointer.touchMajor = inSlot.getTouchMajor(); |
| 265 | outPointer.touchMinor = inSlot.getTouchMinor(); |
| 266 | outPointer.toolMajor = inSlot.getToolMajor(); |
| 267 | outPointer.toolMinor = inSlot.getToolMinor(); |
| 268 | outPointer.orientation = inSlot.getOrientation(); |
| 269 | outPointer.distance = inSlot.getDistance(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 270 | outPointer.tiltX = 0; |
| 271 | outPointer.tiltY = 0; |
| 272 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 273 | outPointer.toolType = inSlot.getToolType(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 274 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 275 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 276 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 277 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 278 | } |
| 279 | } |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 280 | if (shouldSimulateStylusWithTouch() && |
| 281 | outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) { |
| 282 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 283 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 284 | |
| 285 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE && |
| 286 | (mTouchButtonAccumulator.isHovering() || |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 287 | (mRawPointerAxes.pressure.valid && inSlot.getPressure() <= 0)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 288 | outPointer.isHovering = isHovering; |
| 289 | |
| 290 | // Assign pointer id using tracking id if available. |
| 291 | if (mHavePointerIds) { |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 292 | int32_t trackingId = inSlot.getTrackingId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 293 | int32_t id = -1; |
| 294 | if (trackingId >= 0) { |
| 295 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty();) { |
| 296 | uint32_t n = idBits.clearFirstMarkedBit(); |
| 297 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 298 | id = n; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (id < 0 && !mPointerIdBits.isFull()) { |
| 303 | id = mPointerIdBits.markFirstUnmarkedBit(); |
| 304 | mPointerTrackingIdMap[id] = trackingId; |
| 305 | } |
| 306 | } |
| 307 | if (id < 0) { |
| 308 | mHavePointerIds = false; |
| 309 | outState->rawPointerData.clearIdBits(); |
| 310 | newPointerIdBits.clear(); |
| 311 | } else { |
| 312 | outPointer.id = id; |
| 313 | outState->rawPointerData.idToIndex[id] = outCount; |
| 314 | outState->rawPointerData.markIdBit(id, isHovering); |
| 315 | newPointerIdBits.markBit(id); |
| 316 | } |
| 317 | } |
| 318 | outCount += 1; |
| 319 | } |
| 320 | |
| 321 | outState->rawPointerData.pointerCount = outCount; |
| 322 | mPointerIdBits = newPointerIdBits; |
| 323 | |
| 324 | mMultiTouchMotionAccumulator.finishSync(); |
| 325 | } |
| 326 | |
| 327 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 328 | TouchInputMapper::configureRawPointerAxes(); |
| 329 | |
| 330 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 331 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 332 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 333 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 334 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 335 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 336 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 337 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 338 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 339 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 340 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
| 341 | |
| 342 | if (mRawPointerAxes.trackingId.valid && mRawPointerAxes.slot.valid && |
| 343 | mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 344 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
| 345 | if (slotCount > MAX_SLOTS) { |
| 346 | ALOGW("MultiTouch Device %s reported %zu slots but the framework " |
| 347 | "only supports a maximum of %zu slots at this time.", |
| 348 | getDeviceName().c_str(), slotCount, MAX_SLOTS); |
| 349 | slotCount = MAX_SLOTS; |
| 350 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 351 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), slotCount, |
| 352 | true /*usingSlotsProtocol*/); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 353 | } else { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 354 | mMultiTouchMotionAccumulator.configure(getDeviceContext(), MAX_POINTERS, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 355 | false /*usingSlotsProtocol*/); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool MultiTouchInputMapper::hasStylus() const { |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 360 | return mMultiTouchMotionAccumulator.hasStylus() || mTouchButtonAccumulator.hasStylus() || |
| 361 | shouldSimulateStylusWithTouch(); |
| 362 | } |
| 363 | |
| 364 | bool MultiTouchInputMapper::shouldSimulateStylusWithTouch() const { |
| 365 | static const bool SIMULATE_STYLUS_WITH_TOUCH = |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame^] | 366 | #if defined(__ANDROID__) |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 367 | sysprop::InputProperties::simulate_stylus_with_touch().value_or(false); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame^] | 368 | #else |
| 369 | // Disable this developer feature where sysproperties are not available |
| 370 | false; |
| 371 | #endif |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 372 | return SIMULATE_STYLUS_WITH_TOUCH && |
| 373 | mParameters.deviceType == Parameters::DeviceType::TOUCH_SCREEN; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | } // namespace android |