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

Commit 0659550f authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge changes If07a2d1d,I30fd72a7,I7d959e76 into main

* changes:
  Add test cases for fallback key generation from InputDispatcher
  Do not re-use the same EventEntry and DispatchEntry for fallback keys
  Use shared_ptr for InjectionState
parents f4d5f265 b0dad3ad
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
@@ -160,4 +160,90 @@ private:
    std::vector<PointerBuilder> mPointers;
};

class KeyEventBuilder {
public:
    KeyEventBuilder(int32_t action, int32_t source) {
        mAction = action;
        mSource = source;
        mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
        mDownTime = mEventTime;
    }

    KeyEventBuilder(const KeyEvent& event) {
        mAction = event.getAction();
        mDeviceId = event.getDeviceId();
        mSource = event.getSource();
        mDownTime = event.getDownTime();
        mEventTime = event.getEventTime();
        mDisplayId = event.getDisplayId();
        mFlags = event.getFlags();
        mKeyCode = event.getKeyCode();
        mScanCode = event.getScanCode();
        mMetaState = event.getMetaState();
        mRepeatCount = event.getRepeatCount();
    }

    KeyEventBuilder& deviceId(int32_t deviceId) {
        mDeviceId = deviceId;
        return *this;
    }

    KeyEventBuilder& downTime(nsecs_t downTime) {
        mDownTime = downTime;
        return *this;
    }

    KeyEventBuilder& eventTime(nsecs_t eventTime) {
        mEventTime = eventTime;
        return *this;
    }

    KeyEventBuilder& displayId(int32_t displayId) {
        mDisplayId = displayId;
        return *this;
    }

    KeyEventBuilder& policyFlags(int32_t policyFlags) {
        mPolicyFlags = policyFlags;
        return *this;
    }

    KeyEventBuilder& addFlag(uint32_t flags) {
        mFlags |= flags;
        return *this;
    }

    KeyEventBuilder& keyCode(int32_t keyCode) {
        mKeyCode = keyCode;
        return *this;
    }

    KeyEventBuilder& repeatCount(int32_t repeatCount) {
        mRepeatCount = repeatCount;
        return *this;
    }

    KeyEvent build() const {
        KeyEvent event{};
        event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
                         mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime,
                         mEventTime);
        return event;
    }

private:
    int32_t mAction;
    int32_t mDeviceId = DEFAULT_DEVICE_ID;
    uint32_t mSource;
    nsecs_t mDownTime;
    nsecs_t mEventTime;
    int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
    uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
    int32_t mFlags{0};
    int32_t mKeyCode{AKEYCODE_UNKNOWN};
    int32_t mScanCode{0};
    int32_t mMetaState{AMETA_NONE};
    int32_t mRepeatCount{0};
};

} // namespace android
+17 −51
Original line number Diff line number Diff line
@@ -67,24 +67,11 @@ EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policy
        injectionState(nullptr),
        dispatchInProgress(false) {}

EventEntry::~EventEntry() {
    releaseInjectionState();
}

void EventEntry::releaseInjectionState() {
    if (injectionState) {
        injectionState->release();
        injectionState = nullptr;
    }
}

// --- ConfigurationChangedEntry ---

ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
      : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}

ConfigurationChangedEntry::~ConfigurationChangedEntry() {}

std::string ConfigurationChangedEntry::getDescription() const {
    return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
}
@@ -94,8 +81,6 @@ std::string ConfigurationChangedEntry::getDescription() const {
DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
      : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}

DeviceResetEntry::~DeviceResetEntry() {}

std::string DeviceResetEntry::getDescription() const {
    return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
}
@@ -110,8 +95,6 @@ FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToke
        hasFocus(hasFocus),
        reason(reason) {}

FocusEntry::~FocusEntry() {}

std::string FocusEntry::getDescription() const {
    return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
}
@@ -125,8 +108,6 @@ PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t event
      : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
        pointerCaptureRequest(request) {}

PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}

std::string PointerCaptureChangedEntry::getDescription() const {
    return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
                        pointerCaptureRequest.enable ? "true" : "false");
@@ -143,18 +124,16 @@ DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken,
        x(x),
        y(y) {}

DragEntry::~DragEntry() {}

std::string DragEntry::getDescription() const {
    return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
}

// --- KeyEntry ---

KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
                   int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
                   int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
                   nsecs_t downTime)
KeyEntry::KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
                   int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
                   int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
                   int32_t metaState, int32_t repeatCount, nsecs_t downTime)
      : EventEntry(id, Type::KEY, eventTime, policyFlags),
        deviceId(deviceId),
        source(source),
@@ -168,9 +147,9 @@ KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t sou
        downTime(downTime),
        syntheticRepeat(false),
        interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
        interceptKeyWakeupTime(0) {}

KeyEntry::~KeyEntry() {}
        interceptKeyWakeupTime(0) {
    EventEntry::injectionState = std::move(injectionState);
}

std::string KeyEntry::getDescription() const {
    if (!IS_DEBUGGABLE_BUILD) {
@@ -185,15 +164,6 @@ std::string KeyEntry::getDescription() const {
                        keyCode, scanCode, metaState, repeatCount, policyFlags);
}

void KeyEntry::recycle() {
    releaseInjectionState();

    dispatchInProgress = false;
    syntheticRepeat = false;
    interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
    interceptKeyWakeupTime = 0;
}

// --- TouchModeEntry ---

TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int displayId)
@@ -201,21 +171,19 @@ TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode,
        inTouchMode(inTouchMode),
        displayId(displayId) {}

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,
                         int32_t displayId, uint32_t policyFlags, int32_t action,
                         int32_t actionButton, int32_t flags, int32_t metaState,
                         int32_t buttonState, MotionClassification classification,
                         int32_t edgeFlags, float xPrecision, float yPrecision,
                         float xCursorPosition, float yCursorPosition, nsecs_t downTime,
                         const std::vector<PointerProperties>& pointerProperties,
MotionEntry::MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState,
                         nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
                         uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
                         int32_t metaState, int32_t buttonState,
                         MotionClassification classification, int32_t edgeFlags, float xPrecision,
                         float yPrecision, float xCursorPosition, float yCursorPosition,
                         nsecs_t downTime, const std::vector<PointerProperties>& pointerProperties,
                         const std::vector<PointerCoords>& pointerCoords)
      : EventEntry(id, Type::MOTION, eventTime, policyFlags),
        deviceId(deviceId),
@@ -234,9 +202,9 @@ MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32
        yCursorPosition(yCursorPosition),
        downTime(downTime),
        pointerProperties(pointerProperties),
        pointerCoords(pointerCoords) {}

MotionEntry::~MotionEntry() {}
        pointerCoords(pointerCoords) {
    EventEntry::injectionState = std::move(injectionState);
}

std::string MotionEntry::getDescription() const {
    if (!IS_DEBUGGABLE_BUILD) {
@@ -285,8 +253,6 @@ SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32
        hwTimestamp(hwTimestamp),
        values(std::move(values)) {}

SensorEntry::~SensorEntry() {}

std::string SensorEntry::getDescription() const {
    std::string msg;
    msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
+12 −33
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ struct EventEntry {
    Type type;
    nsecs_t eventTime;
    uint32_t policyFlags;
    InjectionState* injectionState;
    std::shared_ptr<InjectionState> injectionState;

    bool dispatchInProgress; // initially false, set to true while dispatching

@@ -72,17 +72,12 @@ struct EventEntry {
    virtual std::string getDescription() const = 0;

    EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
    virtual ~EventEntry();

protected:
    void releaseInjectionState();
    virtual ~EventEntry() = default;
};

struct ConfigurationChangedEntry : EventEntry {
    explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
    std::string getDescription() const override;

    ~ConfigurationChangedEntry() override;
};

struct DeviceResetEntry : EventEntry {
@@ -90,8 +85,6 @@ struct DeviceResetEntry : EventEntry {

    DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
    std::string getDescription() const override;

    ~DeviceResetEntry() override;
};

struct FocusEntry : EventEntry {
@@ -102,8 +95,6 @@ struct FocusEntry : EventEntry {
    FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
               const std::string& reason);
    std::string getDescription() const override;

    ~FocusEntry() override;
};

struct PointerCaptureChangedEntry : EventEntry {
@@ -111,8 +102,6 @@ struct PointerCaptureChangedEntry : EventEntry {

    PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
    std::string getDescription() const override;

    ~PointerCaptureChangedEntry() override;
};

struct DragEntry : EventEntry {
@@ -123,8 +112,6 @@ struct DragEntry : EventEntry {
    DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
              float y);
    std::string getDescription() const override;

    ~DragEntry() override;
};

struct KeyEntry : EventEntry {
@@ -150,13 +137,11 @@ struct KeyEntry : EventEntry {
    InterceptKeyResult interceptKeyResult; // set based on the interception result
    nsecs_t interceptKeyWakeupTime;        // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER

    KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
             uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
             int32_t metaState, int32_t repeatCount, nsecs_t downTime);
    KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
             int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
             int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
             int32_t repeatCount, nsecs_t downTime);
    std::string getDescription() const override;
    void recycle();

    ~KeyEntry() override;
};

struct MotionEntry : EventEntry {
@@ -180,16 +165,14 @@ struct MotionEntry : EventEntry {

    size_t getPointerCount() const { return pointerProperties.size(); }

    MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
                uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
                int32_t metaState, int32_t buttonState, MotionClassification classification,
                int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
                float yCursorPosition, nsecs_t downTime,
                const std::vector<PointerProperties>& pointerProperties,
    MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
                int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
                int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
                int32_t buttonState, MotionClassification classification, int32_t edgeFlags,
                float xPrecision, float yPrecision, float xCursorPosition, float yCursorPosition,
                nsecs_t downTime, const std::vector<PointerProperties>& pointerProperties,
                const std::vector<PointerCoords>& pointerCoords);
    std::string getDescription() const override;

    ~MotionEntry() override;
};

std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry);
@@ -209,8 +192,6 @@ struct SensorEntry : EventEntry {
                InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
                std::vector<float> values);
    std::string getDescription() const override;

    ~SensorEntry() override;
};

struct TouchModeEntry : EventEntry {
@@ -219,8 +200,6 @@ struct TouchModeEntry : EventEntry {

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

    ~TouchModeEntry() override;
};

// Tracks the progress of dispatching a particular event to a particular connection.
+3 −15
Original line number Diff line number Diff line
@@ -20,22 +20,10 @@

namespace android::inputdispatcher {

InjectionState::InjectionState(const std::optional<gui::Uid>& targetUid)
      : refCount(1),
        targetUid(targetUid),
InjectionState::InjectionState(const std::optional<gui::Uid>& targetUid, bool isAsync)
      : targetUid(targetUid),
        injectionIsAsync(isAsync),
        injectionResult(android::os::InputEventInjectionResult::PENDING),
        injectionIsAsync(false),
        pendingForegroundDispatches(0) {}

InjectionState::~InjectionState() {}

void InjectionState::release() {
    refCount -= 1;
    if (refCount == 0) {
        delete this;
    } else {
        ALOG_ASSERT(refCount > 0);
    }
}

} // namespace android::inputdispatcher
+3 −9
Original line number Diff line number Diff line
@@ -24,18 +24,12 @@ namespace android {
namespace inputdispatcher {

struct InjectionState {
    mutable int32_t refCount;

    std::optional<gui::Uid> targetUid;
    const std::optional<gui::Uid> targetUid;
    const bool injectionIsAsync; // set to true if injection is not waiting for the result
    android::os::InputEventInjectionResult injectionResult; // initially PENDING
    bool injectionIsAsync;               // set to true if injection is not waiting for the result
    int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress

    explicit InjectionState(const std::optional<gui::Uid>& targetUid);
    void release();

private:
    ~InjectionState();
    explicit InjectionState(const std::optional<gui::Uid>& targetUid, bool isAsync);
};

} // namespace inputdispatcher
Loading