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

Commit edbc56ed authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add PreferStylusOverTouchBlocker and handle multiple devices"

parents 4b974701 a6a660fc
Loading
Loading
Loading
Loading
+61 −0
Original line number Original line 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 Original line Diff line number Diff line
@@ -50,6 +50,7 @@ cc_library {
        "Keyboard.cpp",
        "Keyboard.cpp",
        "KeyCharacterMap.cpp",
        "KeyCharacterMap.cpp",
        "KeyLayoutMap.cpp",
        "KeyLayoutMap.cpp",
        "PrintTools.cpp",
        "PropertyMap.cpp",
        "PropertyMap.cpp",
        "TouchVideoFrame.cpp",
        "TouchVideoFrame.cpp",
        "VelocityControl.cpp",
        "VelocityControl.cpp",
@@ -102,6 +103,9 @@ cc_library {


            sanitize: {
            sanitize: {
                misc_undefined: ["integer"],
                misc_undefined: ["integer"],
                diag: {
                    misc_undefined: ["integer"],
                },
            },
            },
        },
        },
        host: {
        host: {
+27 −0
Original line number Original line 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 Original line Diff line number Diff line
@@ -202,9 +202,11 @@ std::string NotifyMotionArgs::dump() const {
        coords += "}";
        coords += "}";
    }
    }
    return StringPrintf("NotifyMotionArgs(id=%" PRId32 ", eventTime=%" PRId64 ", deviceId=%" PRId32
    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(),
                        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 {
void NotifyMotionArgs::notify(InputListenerInterface& listener) const {
+158 −49
Original line number Original line Diff line number Diff line
@@ -15,78 +15,163 @@
 */
 */


#include "PreferStylusOverTouchBlocker.h"
#include "PreferStylusOverTouchBlocker.h"

#include <input/PrintTools.h>
#include <android-base/stringprintf.h>

using android::base::StringPrintf;

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


namespace android {
namespace android {


ftl::StaticVector<NotifyMotionArgs, 2> PreferStylusOverTouchBlocker::processMotion(
static std::pair<bool, bool> checkToolType(const NotifyMotionArgs& args) {
        const NotifyMotionArgs& args) {
    bool hasStylus = false;
    const bool isStylusEvent = isFromSource(args.source, AINPUT_SOURCE_STYLUS);
    bool hasTouch = false;
    if (isStylusEvent) {
    for (size_t i = 0; i < args.pointerCount; i++) {
    for (size_t i = 0; i < args.pointerCount; i++) {
        // Make sure we are canceling stylus pointers
        // Make sure we are canceling stylus pointers
        const int32_t toolType = args.pointerProperties[i].toolType;
        const int32_t toolType = args.pointerProperties[i].toolType;
            LOG_ALWAYS_FATAL_IF(toolType != AMOTION_EVENT_TOOL_TYPE_STYLUS &&
        if (toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
                                        toolType != AMOTION_EVENT_TOOL_TYPE_ERASER,
            toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
                                "The pointer %zu has toolType=%i, but the source is STYLUS. If "
            hasStylus = true;
                                "simultaneous touch and stylus is supported, "
                                "'PreferStylusOverTouchBlocker' should be disabled.",
                                i, toolType);
        }
        }
        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 =
    const bool isUpOrCancel =
            args.action == AMOTION_EVENT_ACTION_UP || args.action == AMOTION_EVENT_ACTION_CANCEL;
            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 (isStylusEvent) {
        if (isDown) {
        if (isDown) {
            // Reject all touch while stylus is down
            // Reject all touch while stylus is down
            mIsStylusDown = true;
            mActiveStyli.insert(args.deviceId);
            if (mIsTouchDown && !mCurrentTouchIsCanceled) {

                // Cancel touch!
            // Cancel all current touch!
                mCurrentTouchIsCanceled = true;
            std::vector<NotifyMotionArgs> result;
                mLastTouchEvent.action = AMOTION_EVENT_ACTION_CANCEL;
            for (auto& [deviceId, lastTouchEvent] : mLastTouchEvents) {
                mLastTouchEvent.flags |= AMOTION_EVENT_FLAG_CANCELED;
                if (mCanceledDevices.find(deviceId) != mCanceledDevices.end()) {
                mLastTouchEvent.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
                    // Already canceled, go to next one.
                return {mLastTouchEvent, args};
                    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) {
        if (isUpOrCancel) {
            mIsStylusDown = false;
            mActiveStyli.erase(args.deviceId);
        }
        }
        // Never drop stylus events
        // Never drop stylus events
        return {args};
        return {args};
    }
    }


    const bool isTouchEvent =
    const bool isTouchEvent = hasTouch;
            isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN) && !isStylusEvent;
    if (isTouchEvent) {
    if (isTouchEvent) {
        if (mIsStylusDown) {
        // Suppress the current gesture if any stylus is still down
            mCurrentTouchIsCanceled = true;
        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.
        const bool shouldDrop = mCanceledDevices.find(args.deviceId) != mCanceledDevices.end();
        if (mCurrentTouchIsCanceled) {
        if (isUpOrCancel) {
        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 {};
            return {};
        }
        }


        // Update state
        if (!isUpOrCancel) {
        mLastTouchEvent = args;
            mLastTouchEvents[args.deviceId] = args;
        if (isDown) {
            mIsTouchDown = true;
        }
        if (isUpOrCancel) {
            mIsTouchDown = false;
            mCurrentTouchIsCanceled = false;
        }
        }
        return {args};
        return {args};
    }
    }
@@ -95,12 +180,36 @@ ftl::StaticVector<NotifyMotionArgs, 2> PreferStylusOverTouchBlocker::processMoti
    return {args};
    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;
    std::string out;
    out += StringPrintf("mIsTouchDown: %s\n", toString(mIsTouchDown));
    out += "mActiveStyli: " + dumpSet(mActiveStyli) + "\n";
    out += StringPrintf("mIsStylusDown: %s\n", toString(mIsStylusDown));
    out += "mLastTouchEvents: " + dumpMap(mLastTouchEvents, constToString, dumpArgs) + "\n";
    out += StringPrintf("mLastTouchEvent: %s\n", mLastTouchEvent.dump().c_str());
    out += "mDevicesWithMixedToolType: " + dumpSet(mDevicesWithMixedToolType) + "\n";
    out += StringPrintf("mCurrentTouchIsCanceled: %s\n", toString(mCurrentTouchIsCanceled));
    out += "mCanceledDevices: " + dumpSet(mCanceledDevices) + "\n";
    return out;
    return out;
}
}


Loading