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

Commit 7242d8bd authored by Antonio Kantek's avatar Antonio Kantek
Browse files

TouchEvent (3/n) Enhancing InputDispatcher to support TouchModeEvent

This CL provides the infrastructure to dispatch TouchModeEvent via
InputDispatcher. Remaining logic like permission check, Java integration, etc will be added in next incoming CLs.

Note:
InputDispatcher::requestTouchModeChange instantiate and enqueue TouchModeEvent.
InputDispatcher::dispatchTouchModeChangeLocked notifies all windows
about the touch mode update.

Test: atest inputflinger_tests
Bug: 193718270
Change-Id: I3cfeec0e9bc33737f15d6822242f7eb5e381a119
parent 9cf612f1
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -192,6 +192,18 @@ void KeyEntry::recycle() {
    interceptKeyWakeupTime = 0;
}

// --- TouchModeEntry ---

TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode)
      : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
        inTouchMode(inTouchMode) {}

TouchModeEntry::~TouchModeEntry() {}

std::string TouchModeEntry::getDescription() const {
    return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
}

// --- MotionEntry ---

MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
+11 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ struct EventEntry {
        SENSOR,
        POINTER_CAPTURE_CHANGED,
        DRAG,
        TOUCH_MODE_CHANGED,
    };

    int32_t id;
@@ -185,7 +186,7 @@ struct MotionEntry : EventEntry {
                float xOffset, float yOffset);
    std::string getDescription() const override;

    virtual ~MotionEntry();
    ~MotionEntry() override;
};

struct SensorEntry : EventEntry {
@@ -207,6 +208,15 @@ struct SensorEntry : EventEntry {
    ~SensorEntry() override;
};

struct TouchModeEntry : EventEntry {
    bool inTouchMode;

    TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode);
    std::string getDescription() const override;

    ~TouchModeEntry() override;
};

// Tracks the progress of dispatching a particular event to a particular connection.
struct DispatchEntry {
    const uint32_t seq; // unique sequence number, never 0
+66 −0
Original line number Diff line number Diff line
@@ -826,6 +826,14 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
            break;
        }

        case EventEntry::Type::TOUCH_MODE_CHANGED: {
            const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent);
            dispatchTouchModeChangeLocked(currentTime, typedEntry);
            done = true;
            dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped
            break;
        }

        case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
            const auto typedEntry =
                    std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent);
@@ -1006,6 +1014,7 @@ bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newE
            LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked");
            break;
        }
        case EventEntry::Type::TOUCH_MODE_CHANGED:
        case EventEntry::Type::CONFIGURATION_CHANGED:
        case EventEntry::Type::DEVICE_RESET:
        case EventEntry::Type::SENSOR:
@@ -1140,6 +1149,7 @@ void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason
            break;
        }
        case EventEntry::Type::FOCUS:
        case EventEntry::Type::TOUCH_MODE_CHANGED:
        case EventEntry::Type::CONFIGURATION_CHANGED:
        case EventEntry::Type::DEVICE_RESET: {
            LOG_ALWAYS_FATAL("Should not drop %s events", NamedEnum::string(entry.type).c_str());
@@ -1384,6 +1394,43 @@ void InputDispatcher::dispatchPointerCaptureChangedLocked(
    dropReason = DropReason::NOT_DROPPED;
}

void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime,
                                                    const std::shared_ptr<TouchModeEntry>& entry) {
    const std::vector<sp<WindowInfoHandle>>& windowHandles =
            getWindowHandlesLocked(mFocusedDisplayId);
    if (windowHandles.empty()) {
        return;
    }
    const std::vector<InputTarget> inputTargets =
            getInputTargetsFromWindowHandlesLocked(windowHandles);
    if (inputTargets.empty()) {
        return;
    }
    entry->dispatchInProgress = true;
    dispatchEventLocked(currentTime, entry, inputTargets);
}

std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked(
        const std::vector<sp<WindowInfoHandle>>& windowHandles) const {
    std::vector<InputTarget> inputTargets;
    for (const sp<WindowInfoHandle>& handle : windowHandles) {
        // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only.
        const sp<IBinder>& token = handle->getToken();
        if (token == nullptr) {
            continue;
        }
        std::shared_ptr<InputChannel> channel = getInputChannelLocked(token);
        if (channel == nullptr) {
            continue; // Window has gone away
        }
        InputTarget target;
        target.inputChannel = channel;
        target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
        inputTargets.push_back(target);
    }
    return inputTargets;
}

bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry,
                                        DropReason* dropReason, nsecs_t* nextWakeupTime) {
    // Preprocessing.
@@ -1746,6 +1793,7 @@ int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) {
        }
        case EventEntry::Type::POINTER_CAPTURE_CHANGED:
        case EventEntry::Type::FOCUS:
        case EventEntry::Type::TOUCH_MODE_CHANGED:
        case EventEntry::Type::CONFIGURATION_CHANGED:
        case EventEntry::Type::DEVICE_RESET:
        case EventEntry::Type::SENSOR:
@@ -2732,6 +2780,10 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
            eventType = USER_ACTIVITY_EVENT_BUTTON;
            break;
        }
        case EventEntry::Type::TOUCH_MODE_CHANGED: {
            break;
        }

        case EventEntry::Type::FOCUS:
        case EventEntry::Type::CONFIGURATION_CHANGED:
        case EventEntry::Type::DEVICE_RESET:
@@ -2960,6 +3012,7 @@ void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connectio
            break;
        }
        case EventEntry::Type::FOCUS:
        case EventEntry::Type::TOUCH_MODE_CHANGED:
        case EventEntry::Type::POINTER_CAPTURE_CHANGED:
        case EventEntry::Type::DRAG: {
            break;
@@ -3178,6 +3231,16 @@ void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
                break;
            }

            case EventEntry::Type::TOUCH_MODE_CHANGED: {
                const TouchModeEntry& touchModeEntry =
                        static_cast<const TouchModeEntry&>(eventEntry);
                status = connection->inputPublisher
                                 .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id,
                                                        touchModeEntry.inTouchMode);

                break;
            }

            case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
                const auto& captureEntry =
                        static_cast<const PointerCaptureChangedEntry&>(eventEntry);
@@ -3512,6 +3575,7 @@ void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
                break;
            }
            case EventEntry::Type::FOCUS:
            case EventEntry::Type::TOUCH_MODE_CHANGED:
            case EventEntry::Type::POINTER_CAPTURE_CHANGED:
            case EventEntry::Type::DRAG: {
                LOG_ALWAYS_FATAL("Canceling %s events is not supported",
@@ -3575,6 +3639,7 @@ void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(

            case EventEntry::Type::KEY:
            case EventEntry::Type::FOCUS:
            case EventEntry::Type::TOUCH_MODE_CHANGED:
            case EventEntry::Type::CONFIGURATION_CHANGED:
            case EventEntry::Type::DEVICE_RESET:
            case EventEntry::Type::POINTER_CAPTURE_CHANGED:
@@ -4774,6 +4839,7 @@ void InputDispatcher::setInputFilterEnabled(bool enabled) {
void InputDispatcher::setInTouchMode(bool inTouchMode) {
    std::scoped_lock lock(mLock);
    mInTouchMode = inTouchMode;
    // TODO(b/193718270): Fire TouchModeEvent here.
}

void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
+9 −0
Original line number Diff line number Diff line
@@ -361,6 +361,12 @@ private:
    bool hasResponsiveConnectionLocked(android::gui::WindowInfoHandle& windowHandle) const
            REQUIRES(mLock);

    // Gets all the input targets (with their respective input channels) from the window handles
    // passed as argument.
    std::vector<InputTarget> getInputTargetsFromWindowHandlesLocked(
            const std::vector<sp<android::gui::WindowInfoHandle>>& windowHandles) const
            REQUIRES(mLock);

    /*
     * Validate and update InputWindowHandles for a given display.
     */
@@ -421,6 +427,9 @@ private:
    void dispatchPointerCaptureChangedLocked(
            nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry,
            DropReason& dropReason) REQUIRES(mLock);
    void dispatchTouchModeChangeLocked(nsecs_t currentTime,
                                       const std::shared_ptr<TouchModeEntry>& entry)
            REQUIRES(mLock);
    void dispatchEventLocked(nsecs_t currentTime, std::shared_ptr<EventEntry> entry,
                             const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
    void dispatchSensorLocked(nsecs_t currentTime, const std::shared_ptr<SensorEntry>& entry,
+1 −0
Original line number Diff line number Diff line
@@ -202,6 +202,7 @@ public:
     * InputDispatcher is the source of truth of Pointer Capture.
     */
    virtual void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) = 0;

    /* Flush input device motion sensor.
     *
     * Returns true on success.