Track hovering pointers explicitly
Before this CL, hovering window was tracked separately inside
InputDispatcher. This window was getting updated in various places.
Inconsistent motion streams, like HOVER_ENTER->DOWN->UP->HOVER_EXIT were
possible.
In this CL, we track hovering pointers inside TouchedWindow. At all
times, the currently tracked pointer must always be in the touch state.
The hovering pointer is removed when HOVER_EXIT is received.
This CL also establishes the foundation for multi-device, multi-pointer
streams, by storing hovering pointers inside TouchedWindow per-device.
Eventually, we can look into separately creating touched targets from
updating the touch state. This approach is partially used in this CL.
TouchState is used to keep track of where the hovering pointer is
currently. The 'addHoveringWindowsLocked' function returns the
equivalent of InputTargets. Eventually, we can change this to return
InputTargets.
Bug: 211379801
Test: m inputflinger_tests && adb sync data && adb shell -t /data/nativetest64/inputflinger_tests/inputflinger_tests
Change-Id: I047926e53b846c96807aed45fb585e031e5b88b9
diff --git a/services/inputflinger/dispatcher/TouchState.cpp b/services/inputflinger/dispatcher/TouchState.cpp
index c21af9e..f120fc9 100644
--- a/services/inputflinger/dispatcher/TouchState.cpp
+++ b/services/inputflinger/dispatcher/TouchState.cpp
@@ -31,10 +31,30 @@
*this = TouchState();
}
+void TouchState::removeTouchedPointer(int32_t pointerId) {
+ for (TouchedWindow& touchedWindow : windows) {
+ touchedWindow.pointerIds.clearBit(pointerId);
+ }
+}
+
+void TouchState::clearHoveringPointers() {
+ for (TouchedWindow& touchedWindow : windows) {
+ touchedWindow.clearHoveringPointers();
+ }
+}
+
+void TouchState::clearWindowsWithoutPointers() {
+ std::erase_if(windows, [](const TouchedWindow& w) {
+ return w.pointerIds.isEmpty() && !w.hasHoveringPointers();
+ });
+}
+
void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle,
ftl::Flags<InputTarget::Flags> targetFlags, BitSet32 pointerIds,
std::optional<nsecs_t> eventTime) {
for (TouchedWindow& touchedWindow : windows) {
+ // We do not compare windows by token here because two windows that share the same token
+ // may have a different transform
if (touchedWindow.windowHandle == windowHandle) {
touchedWindow.targetFlags |= targetFlags;
if (targetFlags.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) {
@@ -59,6 +79,21 @@
windows.push_back(touchedWindow);
}
+void TouchState::addHoveringPointerToWindow(const sp<WindowInfoHandle>& windowHandle,
+ int32_t hoveringDeviceId, int32_t hoveringPointerId) {
+ for (TouchedWindow& touchedWindow : windows) {
+ if (touchedWindow.windowHandle == windowHandle) {
+ touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
+ return;
+ }
+ }
+
+ TouchedWindow touchedWindow;
+ touchedWindow.windowHandle = windowHandle;
+ touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
+ windows.push_back(touchedWindow);
+}
+
void TouchState::removeWindowByToken(const sp<IBinder>& token) {
for (size_t i = 0; i < windows.size(); i++) {
if (windows[i].windowHandle->getToken() == token) {
@@ -145,6 +180,26 @@
[](const TouchedWindow& window) { return !window.pointerIds.isEmpty(); });
}
+std::set<sp<WindowInfoHandle>> TouchState::getWindowsWithHoveringPointer(int32_t hoveringDeviceId,
+ int32_t pointerId) const {
+ std::set<sp<WindowInfoHandle>> out;
+ for (const TouchedWindow& window : windows) {
+ if (window.hasHoveringPointer(hoveringDeviceId, pointerId)) {
+ out.insert(window.windowHandle);
+ }
+ }
+ return out;
+}
+
+void TouchState::removeHoveringPointer(int32_t hoveringDeviceId, int32_t hoveringPointerId) {
+ for (TouchedWindow& window : windows) {
+ window.removeHoveringPointer(hoveringDeviceId, hoveringPointerId);
+ }
+ std::erase_if(windows, [](const TouchedWindow& w) {
+ return w.pointerIds.isEmpty() && !w.hasHoveringPointers();
+ });
+}
+
std::string TouchState::dump() const {
std::string out;
out += StringPrintf("deviceId=%d, source=%s\n", deviceId,