From a6a660fc0aa74ea4f5930b74523cf1893b2f9282 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Fri, 4 Mar 2022 15:12:16 -0800 Subject: Add PreferStylusOverTouchBlocker and handle multiple devices We removed PreferStylusOverTouchBlocker previously in order to avoid a crash. In this CL, we are adding it back in, and handling the case of input device having "SOURCE_STYLUS", but reporting "finger" tool type. If there's a stylus event with one of the pointers labeled as 'finger', let's assume that the device supports simultaneous touch and stylus. For this situation, simply disable PreferStylusOverTouchBlocker going forward for these devices, and pass through any events coming from there. Currently, this happens on emulator. In their touch driver, they configure stylus properties as well as touch properties, but most of the events that they send are TOOL_TYPE_FINGER. Previously, this triggered a crash in PreferStylusOverTouchBlocker. Bug: 222531989 Test: atest inputflinger_tests Change-Id: Ifbb08858a4dfebc95c30ca19d6e68533855db7e4 --- .../inputflinger/PreferStylusOverTouchBlocker.cpp | 207 ++++++++++++++++----- 1 file changed, 158 insertions(+), 49 deletions(-) (limited to 'services/inputflinger/PreferStylusOverTouchBlocker.cpp') diff --git a/services/inputflinger/PreferStylusOverTouchBlocker.cpp b/services/inputflinger/PreferStylusOverTouchBlocker.cpp index ad639b4ef8..beec2e162e 100644 --- a/services/inputflinger/PreferStylusOverTouchBlocker.cpp +++ b/services/inputflinger/PreferStylusOverTouchBlocker.cpp @@ -15,78 +15,163 @@ */ #include "PreferStylusOverTouchBlocker.h" +#include -#include +namespace android { -using android::base::StringPrintf; +static std::pair checkToolType(const NotifyMotionArgs& args) { + bool hasStylus = false; + bool hasTouch = false; + for (size_t i = 0; i < args.pointerCount; i++) { + // Make sure we are canceling stylus pointers + const int32_t toolType = args.pointerProperties[i].toolType; + if (toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || + toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { + hasStylus = true; + } + if (toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) { + hasTouch = true; + } + } + return std::make_pair(hasTouch, hasStylus); +} -static const char* toString(bool value) { - return value ? "true" : "false"; +/** + * Intersect two sets in-place, storing the result in 'set1'. + * Find elements in set1 that are not present in set2 and delete them, + * relying on the fact that the two sets are ordered. + */ +template +static void intersectInPlace(std::set& set1, const std::set& set2) { + typename std::set::iterator it1 = set1.begin(); + typename std::set::const_iterator it2 = set2.begin(); + while (it1 != set1.end() && it2 != set2.end()) { + const T& element1 = *it1; + const T& element2 = *it2; + if (element1 < element2) { + // This element is not present in set2. Remove it from set1. + it1 = set1.erase(it1); + continue; + } + if (element2 < element1) { + it2++; + } + if (element1 == element2) { + it1++; + it2++; + } + } + // Remove the rest of the elements in set1 because set2 is already exhausted. + set1.erase(it1, set1.end()); } -namespace android { +/** + * Same as above, but prune a map + */ +template +static void intersectInPlace(std::map& map, const std::set& set2) { + typename std::map::iterator it1 = map.begin(); + typename std::set::const_iterator it2 = set2.begin(); + while (it1 != map.end() && it2 != set2.end()) { + const auto& [key, _] = *it1; + const K& element2 = *it2; + if (key < element2) { + // This element is not present in set2. Remove it from map. + it1 = map.erase(it1); + continue; + } + if (element2 < key) { + it2++; + } + if (key == element2) { + it1++; + it2++; + } + } + // Remove the rest of the elements in map because set2 is already exhausted. + map.erase(it1, map.end()); +} + +// -------------------------------- PreferStylusOverTouchBlocker ----------------------------------- -ftl::StaticVector PreferStylusOverTouchBlocker::processMotion( +std::vector PreferStylusOverTouchBlocker::processMotion( const NotifyMotionArgs& args) { - const bool isStylusEvent = isFromSource(args.source, AINPUT_SOURCE_STYLUS); - if (isStylusEvent) { - for (size_t i = 0; i < args.pointerCount; i++) { - // Make sure we are canceling stylus pointers - const int32_t toolType = args.pointerProperties[i].toolType; - LOG_ALWAYS_FATAL_IF(toolType != AMOTION_EVENT_TOOL_TYPE_STYLUS && - toolType != AMOTION_EVENT_TOOL_TYPE_ERASER, - "The pointer %zu has toolType=%i, but the source is STYLUS. If " - "simultaneous touch and stylus is supported, " - "'PreferStylusOverTouchBlocker' should be disabled.", - i, toolType); + const auto [hasTouch, hasStylus] = checkToolType(args); + const bool isUpOrCancel = + args.action == AMOTION_EVENT_ACTION_UP || args.action == AMOTION_EVENT_ACTION_CANCEL; + + if (hasTouch && hasStylus) { + mDevicesWithMixedToolType.insert(args.deviceId); + } + // Handle the case where mixed touch and stylus pointers are reported. Add this device to the + // ignore list, since it clearly supports simultaneous touch and stylus. + if (mDevicesWithMixedToolType.find(args.deviceId) != mDevicesWithMixedToolType.end()) { + // This event comes from device with mixed stylus and touch event. Ignore this device. + if (mCanceledDevices.find(args.deviceId) != mCanceledDevices.end()) { + // If we started to cancel events from this device, continue to do so to keep + // the stream consistent. It should happen at most once per "mixed" device. + if (isUpOrCancel) { + mCanceledDevices.erase(args.deviceId); + mLastTouchEvents.erase(args.deviceId); + } + return {}; } + return {args}; } + + const bool isStylusEvent = hasStylus; const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; - const bool isUpOrCancel = - args.action == AMOTION_EVENT_ACTION_UP || args.action == AMOTION_EVENT_ACTION_CANCEL; + if (isStylusEvent) { if (isDown) { // Reject all touch while stylus is down - mIsStylusDown = true; - if (mIsTouchDown && !mCurrentTouchIsCanceled) { - // Cancel touch! - mCurrentTouchIsCanceled = true; - mLastTouchEvent.action = AMOTION_EVENT_ACTION_CANCEL; - mLastTouchEvent.flags |= AMOTION_EVENT_FLAG_CANCELED; - mLastTouchEvent.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); - return {mLastTouchEvent, args}; + mActiveStyli.insert(args.deviceId); + + // Cancel all current touch! + std::vector result; + for (auto& [deviceId, lastTouchEvent] : mLastTouchEvents) { + if (mCanceledDevices.find(deviceId) != mCanceledDevices.end()) { + // Already canceled, go to next one. + continue; + } + // Not yet canceled. Cancel it. + lastTouchEvent.action = AMOTION_EVENT_ACTION_CANCEL; + lastTouchEvent.flags |= AMOTION_EVENT_FLAG_CANCELED; + lastTouchEvent.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); + result.push_back(lastTouchEvent); + mCanceledDevices.insert(deviceId); } + result.push_back(args); + return result; } if (isUpOrCancel) { - mIsStylusDown = false; + mActiveStyli.erase(args.deviceId); } // Never drop stylus events return {args}; } - const bool isTouchEvent = - isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN) && !isStylusEvent; + const bool isTouchEvent = hasTouch; if (isTouchEvent) { - if (mIsStylusDown) { - mCurrentTouchIsCanceled = true; + // Suppress the current gesture if any stylus is still down + if (!mActiveStyli.empty()) { + mCanceledDevices.insert(args.deviceId); + } + + const bool shouldDrop = mCanceledDevices.find(args.deviceId) != mCanceledDevices.end(); + if (isUpOrCancel) { + mCanceledDevices.erase(args.deviceId); + mLastTouchEvents.erase(args.deviceId); } + // If we already canceled the current gesture, then continue to drop events from it, even if // the stylus has been lifted. - if (mCurrentTouchIsCanceled) { - if (isUpOrCancel) { - mCurrentTouchIsCanceled = false; - } + if (shouldDrop) { return {}; } - // Update state - mLastTouchEvent = args; - if (isDown) { - mIsTouchDown = true; - } - if (isUpOrCancel) { - mIsTouchDown = false; - mCurrentTouchIsCanceled = false; + if (!isUpOrCancel) { + mLastTouchEvents[args.deviceId] = args; } return {args}; } @@ -95,12 +180,36 @@ ftl::StaticVector PreferStylusOverTouchBlocker::processMoti return {args}; } -std::string PreferStylusOverTouchBlocker::dump() { +void PreferStylusOverTouchBlocker::notifyInputDevicesChanged( + const std::vector& inputDevices) { + std::set presentDevices; + for (const InputDeviceInfo& device : inputDevices) { + presentDevices.insert(device.getId()); + } + // Only keep the devices that are still present. + intersectInPlace(mDevicesWithMixedToolType, presentDevices); + intersectInPlace(mLastTouchEvents, presentDevices); + intersectInPlace(mCanceledDevices, presentDevices); + intersectInPlace(mActiveStyli, presentDevices); +} + +void PreferStylusOverTouchBlocker::notifyDeviceReset(const NotifyDeviceResetArgs& args) { + mDevicesWithMixedToolType.erase(args.deviceId); + mLastTouchEvents.erase(args.deviceId); + mCanceledDevices.erase(args.deviceId); + mActiveStyli.erase(args.deviceId); +} + +static std::string dumpArgs(const NotifyMotionArgs& args) { + return args.dump(); +} + +std::string PreferStylusOverTouchBlocker::dump() const { std::string out; - out += StringPrintf("mIsTouchDown: %s\n", toString(mIsTouchDown)); - out += StringPrintf("mIsStylusDown: %s\n", toString(mIsStylusDown)); - out += StringPrintf("mLastTouchEvent: %s\n", mLastTouchEvent.dump().c_str()); - out += StringPrintf("mCurrentTouchIsCanceled: %s\n", toString(mCurrentTouchIsCanceled)); + out += "mActiveStyli: " + dumpSet(mActiveStyli) + "\n"; + out += "mLastTouchEvents: " + dumpMap(mLastTouchEvents, constToString, dumpArgs) + "\n"; + out += "mDevicesWithMixedToolType: " + dumpSet(mDevicesWithMixedToolType) + "\n"; + out += "mCanceledDevices: " + dumpSet(mCanceledDevices) + "\n"; return out; } -- cgit v1.2.3-59-g8ed1b