diff options
| author | 2024-05-14 21:50:57 +0000 | |
|---|---|---|
| committer | 2024-05-14 21:50:57 +0000 | |
| commit | 58558d185968f62dfb7543b4d132dc00a1eae9de (patch) | |
| tree | 2d530522a0a568f8f634b8e2e414642b8b346916 /services/inputflinger/reader | |
| parent | 08f13b72a0eb164fe87a8d6dc2f706e3dfa0768e (diff) | |
| parent | 018faea11a94d96ebc9e7294fc68f7df165c4875 (diff) | |
Merge "InputReader: Add API to get the last used input device" into main
Diffstat (limited to 'services/inputflinger/reader')
| -rw-r--r-- | services/inputflinger/reader/InputReader.cpp | 40 | ||||
| -rw-r--r-- | services/inputflinger/reader/include/InputReader.h | 5 |
2 files changed, 42 insertions, 3 deletions
diff --git a/services/inputflinger/reader/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp index 12f52b899c..69555f8961 100644 --- a/services/inputflinger/reader/InputReader.cpp +++ b/services/inputflinger/reader/InputReader.cpp @@ -38,6 +38,8 @@ using android::base::StringPrintf; namespace android { +namespace { + /** * Determines if the identifiers passed are a sub-devices. Sub-devices are physical devices * that expose multiple input device paths such a keyboard that also has a touchpad input. @@ -49,8 +51,8 @@ namespace android { * inputs versus the same device plugged into multiple ports. */ -static bool isSubDevice(const InputDeviceIdentifier& identifier1, - const InputDeviceIdentifier& identifier2) { +bool isSubDevice(const InputDeviceIdentifier& identifier1, + const InputDeviceIdentifier& identifier2) { return (identifier1.vendor == identifier2.vendor && identifier1.product == identifier2.product && identifier1.bus == identifier2.bus && identifier1.version == identifier2.version && @@ -58,7 +60,7 @@ static bool isSubDevice(const InputDeviceIdentifier& identifier1, identifier1.location == identifier2.location); } -static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) { +bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) { const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action); if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER && actionMasked != AMOTION_EVENT_ACTION_DOWN && @@ -69,6 +71,28 @@ static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) { return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType); } +bool isNewGestureStart(const NotifyMotionArgs& motion) { + return motion.action == AMOTION_EVENT_ACTION_DOWN || + motion.action == AMOTION_EVENT_ACTION_HOVER_ENTER; +} + +bool isNewGestureStart(const NotifyKeyArgs& key) { + return key.action == AKEY_EVENT_ACTION_DOWN; +} + +// Return the event's device ID if it marks the start of a new gesture. +std::optional<DeviceId> getDeviceIdOfNewGesture(const NotifyArgs& args) { + if (const auto* motion = std::get_if<NotifyMotionArgs>(&args); motion != nullptr) { + return isNewGestureStart(*motion) ? std::make_optional(motion->deviceId) : std::nullopt; + } + if (const auto* key = std::get_if<NotifyKeyArgs>(&args); key != nullptr) { + return isNewGestureStart(*key) ? std::make_optional(key->deviceId) : std::nullopt; + } + return std::nullopt; +} + +} // namespace + // --- InputReader --- InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub, @@ -162,6 +186,11 @@ void InputReader::loopOnce() { } std::swap(notifyArgs, mPendingArgs); + + // Keep track of the last used device + for (const NotifyArgs& args : notifyArgs) { + mLastUsedDeviceId = getDeviceIdOfNewGesture(args).value_or(mLastUsedDeviceId); + } } // release lock // Flush queued events out to the listener. @@ -883,6 +912,11 @@ void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) { mEventHub->sysfsNodeChanged(sysfsNodePath); } +DeviceId InputReader::getLastUsedInputDeviceId() { + std::scoped_lock _l(mLock); + return mLastUsedDeviceId; +} + void InputReader::dump(std::string& dump) { std::scoped_lock _l(mLock); diff --git a/services/inputflinger/reader/include/InputReader.h b/services/inputflinger/reader/include/InputReader.h index d9ac917031..92a778a5bb 100644 --- a/services/inputflinger/reader/include/InputReader.h +++ b/services/inputflinger/reader/include/InputReader.h @@ -118,6 +118,8 @@ public: void sysfsNodeChanged(const std::string& sysfsNodePath) override; + DeviceId getLastUsedInputDeviceId() override; + protected: // These members are protected so they can be instrumented by test cases. virtual std::shared_ptr<InputDevice> createDeviceLocked(nsecs_t when, int32_t deviceId, @@ -200,6 +202,9 @@ private: // records timestamp of the last key press on the physical keyboard nsecs_t mLastKeyDownTimestamp GUARDED_BY(mLock){0}; + // The input device that produced a new gesture most recently. + DeviceId mLastUsedDeviceId GUARDED_BY(mLock){ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID}; + // low-level input event decoding and device management [[nodiscard]] std::list<NotifyArgs> processEventsLocked(const RawEvent* rawEvents, size_t count) REQUIRES(mLock); |