TouchInputMapper: s/mExternalStylusId/mFusedStylusPointerId

Use std::optional to hold the fused external stylus pointer id.

Bug: 246394583
Test: atest inputflinger_tests
Change-Id: Iebd9027639cb994a5e2916334e1c458aa45b9019
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
index 5e81ff8..d447368 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
@@ -21,6 +21,7 @@
 #include "TouchInputMapper.h"
 
 #include <ftl/enum.h>
+#include <input/PrintTools.h>
 
 #include "CursorButtonAccumulator.h"
 #include "CursorScrollAccumulator.h"
@@ -251,7 +252,8 @@
     dump += INDENT3 "Stylus Fusion:\n";
     dump += StringPrintf(INDENT4 "ExternalStylusConnected: %s\n",
                          toString(mExternalStylusConnected));
-    dump += StringPrintf(INDENT4 "External Stylus ID: %" PRId64 "\n", mExternalStylusId);
+    dump += StringPrintf(INDENT4 "Fused External Stylus Pointer ID: %s\n",
+                         toString(mFusedStylusPointerId).c_str());
     dump += StringPrintf(INDENT4 "External Stylus Data Timeout: %" PRId64 "\n",
                          mExternalStylusFusionTimeout);
     dump += StringPrintf(INDENT4 " External Stylus Buttons Applied: 0x%08x",
@@ -1412,7 +1414,7 @@
 
 void TouchInputMapper::resetExternalStylus() {
     mExternalStylusState.clear();
-    mExternalStylusId = -1;
+    mFusedStylusPointerId.reset();
     mExternalStylusFusionTimeout = LLONG_MAX;
     mExternalStylusDataPending = false;
     mExternalStylusButtonsApplied = 0;
@@ -1704,17 +1706,18 @@
     CookedPointerData& currentPointerData = mCurrentCookedState.cookedPointerData;
     const CookedPointerData& lastPointerData = mLastCookedState.cookedPointerData;
 
-    if (mExternalStylusId != -1 && currentPointerData.isTouching(mExternalStylusId)) {
+    if (mFusedStylusPointerId && currentPointerData.isTouching(*mFusedStylusPointerId)) {
         float pressure = mExternalStylusState.pressure;
-        if (pressure == 0.0f && lastPointerData.isTouching(mExternalStylusId)) {
-            const PointerCoords& coords = lastPointerData.pointerCoordsForId(mExternalStylusId);
+        if (pressure == 0.0f && lastPointerData.isTouching(*mFusedStylusPointerId)) {
+            const PointerCoords& coords =
+                    lastPointerData.pointerCoordsForId(*mFusedStylusPointerId);
             pressure = coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
         }
-        PointerCoords& coords = currentPointerData.editPointerCoordsWithId(mExternalStylusId);
+        PointerCoords& coords = currentPointerData.editPointerCoordsWithId(*mFusedStylusPointerId);
         coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
 
         PointerProperties& properties =
-                currentPointerData.editPointerPropertiesWithId(mExternalStylusId);
+                currentPointerData.editPointerPropertiesWithId(*mFusedStylusPointerId);
         if (mExternalStylusState.toolType != AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
             properties.toolType = mExternalStylusState.toolType;
         }
@@ -1731,10 +1734,10 @@
     if (initialDown) {
         if (mExternalStylusState.pressure != 0.0f) {
             ALOGD_IF(DEBUG_STYLUS_FUSION, "Have both stylus and touch data, beginning fusion");
-            mExternalStylusId = state.rawPointerData.touchingIdBits.firstMarkedBit();
+            mFusedStylusPointerId = state.rawPointerData.touchingIdBits.firstMarkedBit();
         } else if (timeout) {
             ALOGD_IF(DEBUG_STYLUS_FUSION, "Timeout expired, assuming touch is not a stylus.");
-            mExternalStylusId = -1;
+            mFusedStylusPointerId.reset();
             mExternalStylusFusionTimeout = LLONG_MAX;
         } else {
             if (mExternalStylusFusionTimeout == LLONG_MAX) {
@@ -1749,9 +1752,10 @@
     }
 
     // Check if the stylus pointer has gone up.
-    if (mExternalStylusId != -1 && !state.rawPointerData.touchingIdBits.hasBit(mExternalStylusId)) {
+    if (mFusedStylusPointerId &&
+        !state.rawPointerData.touchingIdBits.hasBit(*mFusedStylusPointerId)) {
         ALOGD_IF(DEBUG_STYLUS_FUSION, "Stylus pointer is going up");
-        mExternalStylusId = -1;
+        mFusedStylusPointerId.reset();
     }
 
     return false;
@@ -1779,7 +1783,7 @@
     std::list<NotifyArgs> out;
     const bool buttonsChanged = mExternalStylusState.buttons != state.buttons;
     mExternalStylusState.copyFrom(state);
-    if (mExternalStylusId != -1 || mExternalStylusFusionTimeout != LLONG_MAX || buttonsChanged) {
+    if (mFusedStylusPointerId || mExternalStylusFusionTimeout != LLONG_MAX || buttonsChanged) {
         // The following three cases are handled here:
         // - We're in the middle of a fused stream of data;
         // - We're waiting on external stylus data before dispatching the initial down; or
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.h b/services/inputflinger/reader/mapper/TouchInputMapper.h
index efbe885..d2faf3f 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.h
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.h
@@ -362,7 +362,10 @@
 
     // State provided by an external stylus
     StylusState mExternalStylusState;
-    int64_t mExternalStylusId;
+    // If an external stylus is capable of reporting pointer-specific data like pressure, we will
+    // attempt to fuse the pointer data reported by the stylus to the first touch pointer. This is
+    // the id of the pointer to which the external stylus data is fused.
+    std::optional<uint32_t> mFusedStylusPointerId;
     nsecs_t mExternalStylusFusionTimeout;
     bool mExternalStylusDataPending;
     // A subset of the buttons in mCurrentRawState that came from an external stylus.