Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a6a660fc authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

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
parent fe72fc43
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <map>
#include <set>
#include <string>

namespace android {

template <typename T>
std::string constToString(const T& v) {
    return std::to_string(v);
}

/**
 * Convert a set of integral types to string.
 */
template <typename T>
std::string dumpSet(const std::set<T>& v, std::string (*toString)(const T&) = constToString) {
    std::string out;
    for (const T& entry : v) {
        out += out.empty() ? "{" : ", ";
        out += toString(entry);
    }
    return out.empty() ? "{}" : (out + "}");
}

/**
 * Convert a map to string. Both keys and values of the map should be integral type.
 */
template <typename K, typename V>
std::string dumpMap(const std::map<K, V>& map, std::string (*keyToString)(const K&) = constToString,
                    std::string (*valueToString)(const V&) = constToString) {
    std::string out;
    for (const auto& [k, v] : map) {
        if (!out.empty()) {
            out += "\n";
        }
        out += keyToString(k) + ":" + valueToString(v);
    }
    return out;
}

const char* toString(bool value);

} // namespace android
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ cc_library {
        "Keyboard.cpp",
        "KeyCharacterMap.cpp",
        "KeyLayoutMap.cpp",
        "PrintTools.cpp",
        "PropertyMap.cpp",
        "TouchVideoFrame.cpp",
        "VelocityControl.cpp",
@@ -102,6 +103,9 @@ cc_library {

            sanitize: {
                misc_undefined: ["integer"],
                diag: {
                    misc_undefined: ["integer"],
                },
            },
        },
        host: {
+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "PrintTools"

#include <input/PrintTools.h>

namespace android {

const char* toString(bool value) {
    return value ? "true" : "false";
}

} // namespace android
+4 −2
Original line number Diff line number Diff line
@@ -202,9 +202,11 @@ std::string NotifyMotionArgs::dump() const {
        coords += "}";
    }
    return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32
                        ", source=%s, action=%s, pointerCount=%" PRIu32 " pointers=%s)",
                        ", source=%s, action=%s, pointerCount=%" PRIu32
                        " pointers=%s, flags=0x%08x)",
                        id, eventTime, deviceId, inputEventSourceToString(source).c_str(),
                        MotionEvent::actionToString(action).c_str(), pointerCount, coords.c_str());
                        MotionEvent::actionToString(action).c_str(), pointerCount, coords.c_str(),
                        flags);
}

void NotifyMotionArgs::notify(InputListenerInterface& listener) const {
+158 −49
Original line number Diff line number Diff line
@@ -15,78 +15,163 @@
 */

#include "PreferStylusOverTouchBlocker.h"

#include <android-base/stringprintf.h>

using android::base::StringPrintf;

static const char* toString(bool value) {
    return value ? "true" : "false";
}
#include <input/PrintTools.h>

namespace android {

ftl::StaticVector<NotifyMotionArgs, 2> PreferStylusOverTouchBlocker::processMotion(
        const NotifyMotionArgs& args) {
    const bool isStylusEvent = isFromSource(args.source, AINPUT_SOURCE_STYLUS);
    if (isStylusEvent) {
static std::pair<bool, bool> 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;
            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);
        if (toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
            toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
            hasStylus = true;
        }
        if (toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) {
            hasTouch = true;
        }
    const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN;
    }
    return std::make_pair(hasTouch, hasStylus);
}

/**
 * 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 <typename T>
static void intersectInPlace(std::set<T>& set1, const std::set<T>& set2) {
    typename std::set<T>::iterator it1 = set1.begin();
    typename std::set<T>::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());
}

/**
 * Same as above, but prune a map
 */
template <typename K, class V>
static void intersectInPlace(std::map<K, V>& map, const std::set<K>& set2) {
    typename std::map<K, V>::iterator it1 = map.begin();
    typename std::set<K>::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 -----------------------------------

std::vector<NotifyMotionArgs> PreferStylusOverTouchBlocker::processMotion(
        const NotifyMotionArgs& args) {
    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;

    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<NotifyMotionArgs> 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);
        }
        // If we already canceled the current gesture, then continue to drop events from it, even if
        // the stylus has been lifted.
        if (mCurrentTouchIsCanceled) {

        const bool shouldDrop = mCanceledDevices.find(args.deviceId) != mCanceledDevices.end();
        if (isUpOrCancel) {
                mCurrentTouchIsCanceled = false;
            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 (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<NotifyMotionArgs, 2> PreferStylusOverTouchBlocker::processMoti
    return {args};
}

std::string PreferStylusOverTouchBlocker::dump() {
void PreferStylusOverTouchBlocker::notifyInputDevicesChanged(
        const std::vector<InputDeviceInfo>& inputDevices) {
    std::set<int32_t> 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;
}

Loading