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

Commit 2421088b authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use optional values in TouchInputMapper

Minor refactor to store bool + value pairs as std::optional.

Bug: 239182977
Test: atest inputflinger_tests
Change-Id: Ia4d6e9789f4e2764ad307bcb7825efc591ab74cb
parent 5d50d914
Loading
Loading
Loading
Loading
+140 −124
Original line number Diff line number Diff line
@@ -206,30 +206,30 @@ void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
                                 y.fuzz, y.resolution);
        }

        if (mOrientedRanges.haveSize) {
            info->addMotionRange(mOrientedRanges.size);
        if (mOrientedRanges.size) {
            info->addMotionRange(*mOrientedRanges.size);
        }

        if (mOrientedRanges.haveTouchSize) {
            info->addMotionRange(mOrientedRanges.touchMajor);
            info->addMotionRange(mOrientedRanges.touchMinor);
        if (mOrientedRanges.touchMajor) {
            info->addMotionRange(*mOrientedRanges.touchMajor);
            info->addMotionRange(*mOrientedRanges.touchMinor);
        }

        if (mOrientedRanges.haveToolSize) {
            info->addMotionRange(mOrientedRanges.toolMajor);
            info->addMotionRange(mOrientedRanges.toolMinor);
        if (mOrientedRanges.toolMajor) {
            info->addMotionRange(*mOrientedRanges.toolMajor);
            info->addMotionRange(*mOrientedRanges.toolMinor);
        }

        if (mOrientedRanges.haveOrientation) {
            info->addMotionRange(mOrientedRanges.orientation);
        if (mOrientedRanges.orientation) {
            info->addMotionRange(*mOrientedRanges.orientation);
        }

        if (mOrientedRanges.haveDistance) {
            info->addMotionRange(mOrientedRanges.distance);
        if (mOrientedRanges.distance) {
            info->addMotionRange(*mOrientedRanges.distance);
        }

        if (mOrientedRanges.haveTilt) {
            info->addMotionRange(mOrientedRanges.tilt);
        if (mOrientedRanges.tilt) {
            info->addMotionRange(*mOrientedRanges.tilt);
        }

        if (mCursorScrollAccumulator.haveRelativeVWheel()) {
@@ -641,57 +641,58 @@ void TouchInputMapper::initializeSizeRanges() {
        mSizeScale = 0.0f;
    }

    mOrientedRanges.haveTouchSize = true;
    mOrientedRanges.haveToolSize = true;
    mOrientedRanges.haveSize = true;
    mOrientedRanges.touchMajor = InputDeviceInfo::MotionRange{
            .axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR,
            .source = mSource,
            .min = 0,
            .max = diagonalSize,
            .flat = 0,
            .fuzz = 0,
            .resolution = 0,
    };

    mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
    mOrientedRanges.touchMajor.source = mSource;
    mOrientedRanges.touchMajor.min = 0;
    mOrientedRanges.touchMajor.max = diagonalSize;
    mOrientedRanges.touchMajor.flat = 0;
    mOrientedRanges.touchMajor.fuzz = 0;
    mOrientedRanges.touchMajor.resolution = 0;
    if (mRawPointerAxes.touchMajor.valid) {
        mRawPointerAxes.touchMajor.resolution =
                clampResolution("touchMajor", mRawPointerAxes.touchMajor.resolution);
        mOrientedRanges.touchMajor.resolution = mRawPointerAxes.touchMajor.resolution;
        mOrientedRanges.touchMajor->resolution = mRawPointerAxes.touchMajor.resolution;
    }

    mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
    mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
    mOrientedRanges.touchMinor->axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
    if (mRawPointerAxes.touchMinor.valid) {
        mRawPointerAxes.touchMinor.resolution =
                clampResolution("touchMinor", mRawPointerAxes.touchMinor.resolution);
        mOrientedRanges.touchMinor.resolution = mRawPointerAxes.touchMinor.resolution;
    }

    mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
    mOrientedRanges.toolMajor.source = mSource;
    mOrientedRanges.toolMajor.min = 0;
    mOrientedRanges.toolMajor.max = diagonalSize;
    mOrientedRanges.toolMajor.flat = 0;
    mOrientedRanges.toolMajor.fuzz = 0;
    mOrientedRanges.toolMajor.resolution = 0;
        mOrientedRanges.touchMinor->resolution = mRawPointerAxes.touchMinor.resolution;
    }

    mOrientedRanges.toolMajor = InputDeviceInfo::MotionRange{
            .axis = AMOTION_EVENT_AXIS_TOOL_MAJOR,
            .source = mSource,
            .min = 0,
            .max = diagonalSize,
            .flat = 0,
            .fuzz = 0,
            .resolution = 0,
    };
    if (mRawPointerAxes.toolMajor.valid) {
        mRawPointerAxes.toolMajor.resolution =
                clampResolution("toolMajor", mRawPointerAxes.toolMajor.resolution);
        mOrientedRanges.toolMajor.resolution = mRawPointerAxes.toolMajor.resolution;
        mOrientedRanges.toolMajor->resolution = mRawPointerAxes.toolMajor.resolution;
    }

    mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
    mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
    mOrientedRanges.toolMinor->axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
    if (mRawPointerAxes.toolMinor.valid) {
        mRawPointerAxes.toolMinor.resolution =
                clampResolution("toolMinor", mRawPointerAxes.toolMinor.resolution);
        mOrientedRanges.toolMinor.resolution = mRawPointerAxes.toolMinor.resolution;
        mOrientedRanges.toolMinor->resolution = mRawPointerAxes.toolMinor.resolution;
    }

    if (mCalibration.sizeCalibration == Calibration::SizeCalibration::GEOMETRIC) {
        mOrientedRanges.touchMajor.resolution *= mGeometricScale;
        mOrientedRanges.touchMinor.resolution *= mGeometricScale;
        mOrientedRanges.toolMajor.resolution *= mGeometricScale;
        mOrientedRanges.toolMinor.resolution *= mGeometricScale;
        mOrientedRanges.touchMajor->resolution *= mGeometricScale;
        mOrientedRanges.touchMinor->resolution *= mGeometricScale;
        mOrientedRanges.toolMajor->resolution *= mGeometricScale;
        mOrientedRanges.toolMinor->resolution *= mGeometricScale;
    } else {
        // Support for other calibrations can be added here.
        ALOGW("%s calibration is not supported for size ranges at the moment. "
@@ -699,13 +700,15 @@ void TouchInputMapper::initializeSizeRanges() {
              ftl::enum_string(mCalibration.sizeCalibration).c_str());
    }

    mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
    mOrientedRanges.size.source = mSource;
    mOrientedRanges.size.min = 0;
    mOrientedRanges.size.max = 1.0;
    mOrientedRanges.size.flat = 0;
    mOrientedRanges.size.fuzz = 0;
    mOrientedRanges.size.resolution = 0;
    mOrientedRanges.size = InputDeviceInfo::MotionRange{
            .axis = AMOTION_EVENT_AXIS_SIZE,
            .source = mSource,
            .min = 0,
            .max = 1.0,
            .flat = 0,
            .fuzz = 0,
            .resolution = 0,
    };
}

void TouchInputMapper::initializeOrientedRanges() {
@@ -732,21 +735,23 @@ void TouchInputMapper::initializeOrientedRanges() {
    float pressureMax = 1.0;
    if (mCalibration.pressureCalibration == Calibration::PressureCalibration::PHYSICAL ||
        mCalibration.pressureCalibration == Calibration::PressureCalibration::AMPLITUDE) {
        if (mCalibration.havePressureScale) {
            mPressureScale = mCalibration.pressureScale;
        if (mCalibration.pressureScale) {
            mPressureScale = *mCalibration.pressureScale;
            pressureMax = mPressureScale * mRawPointerAxes.pressure.maxValue;
        } else if (mRawPointerAxes.pressure.valid && mRawPointerAxes.pressure.maxValue != 0) {
            mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
        }
    }

    mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
    mOrientedRanges.pressure.source = mSource;
    mOrientedRanges.pressure.min = 0;
    mOrientedRanges.pressure.max = pressureMax;
    mOrientedRanges.pressure.flat = 0;
    mOrientedRanges.pressure.fuzz = 0;
    mOrientedRanges.pressure.resolution = 0;
    mOrientedRanges.pressure = InputDeviceInfo::MotionRange{
            .axis = AMOTION_EVENT_AXIS_PRESSURE,
            .source = mSource,
            .min = 0,
            .max = pressureMax,
            .flat = 0,
            .fuzz = 0,
            .resolution = 0,
    };

    // Tilt
    mTiltXCenter = 0;
@@ -767,29 +772,30 @@ void TouchInputMapper::initializeOrientedRanges() {
            mTiltYScale = 1.0 / mRawPointerAxes.tiltY.resolution;
        }

        mOrientedRanges.haveTilt = true;

        mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
        mOrientedRanges.tilt.source = mSource;
        mOrientedRanges.tilt.min = 0;
        mOrientedRanges.tilt.max = M_PI_2;
        mOrientedRanges.tilt.flat = 0;
        mOrientedRanges.tilt.fuzz = 0;
        mOrientedRanges.tilt.resolution = 0;
        mOrientedRanges.tilt = InputDeviceInfo::MotionRange{
                .axis = AMOTION_EVENT_AXIS_TILT,
                .source = mSource,
                .min = 0,
                .max = M_PI_2,
                .flat = 0,
                .fuzz = 0,
                .resolution = 0,
        };
    }

    // Orientation
    mOrientationScale = 0;
    if (mHaveTilt) {
        mOrientedRanges.haveOrientation = true;

        mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
        mOrientedRanges.orientation.source = mSource;
        mOrientedRanges.orientation.min = -M_PI;
        mOrientedRanges.orientation.max = M_PI;
        mOrientedRanges.orientation.flat = 0;
        mOrientedRanges.orientation.fuzz = 0;
        mOrientedRanges.orientation.resolution = 0;
        mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
                .axis = AMOTION_EVENT_AXIS_ORIENTATION,
                .source = mSource,
                .min = -M_PI,
                .max = M_PI,
                .flat = 0,
                .fuzz = 0,
                .resolution = 0,
        };

    } else if (mCalibration.orientationCalibration != Calibration::OrientationCalibration::NONE) {
        if (mCalibration.orientationCalibration ==
            Calibration::OrientationCalibration::INTERPOLATED) {
@@ -804,37 +810,34 @@ void TouchInputMapper::initializeOrientedRanges() {
            }
        }

        mOrientedRanges.haveOrientation = true;

        mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
        mOrientedRanges.orientation.source = mSource;
        mOrientedRanges.orientation.min = -M_PI_2;
        mOrientedRanges.orientation.max = M_PI_2;
        mOrientedRanges.orientation.flat = 0;
        mOrientedRanges.orientation.fuzz = 0;
        mOrientedRanges.orientation.resolution = 0;
        mOrientedRanges.orientation = InputDeviceInfo::MotionRange{
                .axis = AMOTION_EVENT_AXIS_ORIENTATION,
                .source = mSource,
                .min = -M_PI_2,
                .max = M_PI_2,
                .flat = 0,
                .fuzz = 0,
                .resolution = 0,
        };
    }

    // Distance
    mDistanceScale = 0;
    if (mCalibration.distanceCalibration != Calibration::DistanceCalibration::NONE) {
        if (mCalibration.distanceCalibration == Calibration::DistanceCalibration::SCALED) {
            if (mCalibration.haveDistanceScale) {
                mDistanceScale = mCalibration.distanceScale;
            } else {
                mDistanceScale = 1.0f;
            }
            mDistanceScale = mCalibration.distanceScale.value_or(1.0f);
        }

        mOrientedRanges.haveDistance = true;
        mOrientedRanges.distance = InputDeviceInfo::MotionRange{

        mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
        mOrientedRanges.distance.source = mSource;
        mOrientedRanges.distance.min = mRawPointerAxes.distance.minValue * mDistanceScale;
        mOrientedRanges.distance.max = mRawPointerAxes.distance.maxValue * mDistanceScale;
        mOrientedRanges.distance.flat = 0;
        mOrientedRanges.distance.fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale;
        mOrientedRanges.distance.resolution = 0;
                .axis = AMOTION_EVENT_AXIS_DISTANCE,
                .source = mSource,
                .min = mRawPointerAxes.distance.minValue * mDistanceScale,
                .max = mRawPointerAxes.distance.maxValue * mDistanceScale,
                .flat = 0,
                .fuzz = mRawPointerAxes.distance.fuzz * mDistanceScale,
                .resolution = 0,
        };
    }

    // Compute oriented precision, scales and ranges.
@@ -1220,9 +1223,19 @@ void TouchInputMapper::parseCalibration() {
        }
    }

    out.haveSizeScale = in.tryGetProperty("touch.size.scale", out.sizeScale);
    out.haveSizeBias = in.tryGetProperty("touch.size.bias", out.sizeBias);
    out.haveSizeIsSummed = in.tryGetProperty("touch.size.isSummed", out.sizeIsSummed);
    float sizeScale;

    if (in.tryGetProperty("touch.size.scale", sizeScale)) {
        out.sizeScale = sizeScale;
    }
    float sizeBias;
    if (in.tryGetProperty("touch.size.bias", sizeBias)) {
        out.sizeBias = sizeBias;
    }
    bool sizeIsSummed;
    if (in.tryGetProperty("touch.size.isSummed", sizeIsSummed)) {
        out.sizeIsSummed = sizeIsSummed;
    }

    // Pressure
    out.pressureCalibration = Calibration::PressureCalibration::DEFAULT;
@@ -1240,7 +1253,10 @@ void TouchInputMapper::parseCalibration() {
        }
    }

    out.havePressureScale = in.tryGetProperty("touch.pressure.scale", out.pressureScale);
    float pressureScale;
    if (in.tryGetProperty("touch.pressure.scale", pressureScale)) {
        out.pressureScale = pressureScale;
    }

    // Orientation
    out.orientationCalibration = Calibration::OrientationCalibration::DEFAULT;
@@ -1272,7 +1288,10 @@ void TouchInputMapper::parseCalibration() {
        }
    }

    out.haveDistanceScale = in.tryGetProperty("touch.distance.scale", out.distanceScale);
    float distanceScale;
    if (in.tryGetProperty("touch.distance.scale", distanceScale)) {
        out.distanceScale = distanceScale;
    }

    out.coverageCalibration = Calibration::CoverageCalibration::DEFAULT;
    std::string coverageCalibrationString;
@@ -1337,17 +1356,17 @@ void TouchInputMapper::dumpCalibration(std::string& dump) {
    dump += INDENT4 "touch.size.calibration: ";
    dump += ftl::enum_string(mCalibration.sizeCalibration) + "\n";

    if (mCalibration.haveSizeScale) {
        dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", mCalibration.sizeScale);
    if (mCalibration.sizeScale) {
        dump += StringPrintf(INDENT4 "touch.size.scale: %0.3f\n", *mCalibration.sizeScale);
    }

    if (mCalibration.haveSizeBias) {
        dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", mCalibration.sizeBias);
    if (mCalibration.sizeBias) {
        dump += StringPrintf(INDENT4 "touch.size.bias: %0.3f\n", *mCalibration.sizeBias);
    }

    if (mCalibration.haveSizeIsSummed) {
    if (mCalibration.sizeIsSummed) {
        dump += StringPrintf(INDENT4 "touch.size.isSummed: %s\n",
                             toString(mCalibration.sizeIsSummed));
                             toString(*mCalibration.sizeIsSummed));
    }

    // Pressure
@@ -1365,8 +1384,8 @@ void TouchInputMapper::dumpCalibration(std::string& dump) {
            ALOG_ASSERT(false);
    }

    if (mCalibration.havePressureScale) {
        dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", mCalibration.pressureScale);
    if (mCalibration.pressureScale) {
        dump += StringPrintf(INDENT4 "touch.pressure.scale: %0.3f\n", *mCalibration.pressureScale);
    }

    // Orientation
@@ -1396,8 +1415,8 @@ void TouchInputMapper::dumpCalibration(std::string& dump) {
            ALOG_ASSERT(false);
    }

    if (mCalibration.haveDistanceScale) {
        dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", mCalibration.distanceScale);
    if (mCalibration.distanceScale) {
        dump += StringPrintf(INDENT4 "touch.distance.scale: %0.3f\n", *mCalibration.distanceScale);
    }

    switch (mCalibration.coverageCalibration) {
@@ -2186,7 +2205,7 @@ void TouchInputMapper::cookPointerData() {
                    size = 0;
                }

                if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
                if (mCalibration.sizeIsSummed && *mCalibration.sizeIsSummed) {
                    uint32_t touchingCount = mCurrentRawState.rawPointerData.touchingIdBits.count();
                    if (touchingCount > 1) {
                        touchMajor /= touchingCount;
@@ -2315,10 +2334,9 @@ void TouchInputMapper::cookPointerData() {
                bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
                top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
                orientation -= M_PI_2;
                if (mOrientedRanges.haveOrientation &&
                    orientation < mOrientedRanges.orientation.min) {
                if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
                    orientation +=
                            (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
                            (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
                }
                break;
            case DISPLAY_ORIENTATION_180:
@@ -2327,10 +2345,9 @@ void TouchInputMapper::cookPointerData() {
                bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
                top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
                orientation -= M_PI;
                if (mOrientedRanges.haveOrientation &&
                    orientation < mOrientedRanges.orientation.min) {
                if (mOrientedRanges.orientation && orientation < mOrientedRanges.orientation->min) {
                    orientation +=
                            (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
                            (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
                }
                break;
            case DISPLAY_ORIENTATION_270:
@@ -2339,10 +2356,9 @@ void TouchInputMapper::cookPointerData() {
                bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
                top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale;
                orientation += M_PI_2;
                if (mOrientedRanges.haveOrientation &&
                    orientation > mOrientedRanges.orientation.max) {
                if (mOrientedRanges.orientation && orientation > mOrientedRanges.orientation->max) {
                    orientation -=
                            (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
                            (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
                }
                break;
            default:
+25 −36
Original line number Diff line number Diff line
@@ -248,12 +248,9 @@ protected:

        SizeCalibration sizeCalibration;

        bool haveSizeScale;
        float sizeScale;
        bool haveSizeBias;
        float sizeBias;
        bool haveSizeIsSummed;
        bool sizeIsSummed;
        std::optional<float> sizeScale;
        std::optional<float> sizeBias;
        std::optional<bool> sizeIsSummed;

        // Pressure
        enum class PressureCalibration {
@@ -264,8 +261,7 @@ protected:
        };

        PressureCalibration pressureCalibration;
        bool havePressureScale;
        float pressureScale;
        std::optional<float> pressureScale;

        // Orientation
        enum class OrientationCalibration {
@@ -285,8 +281,7 @@ protected:
        };

        DistanceCalibration distanceCalibration;
        bool haveDistanceScale;
        float distanceScale;
        std::optional<float> distanceScale;

        enum class CoverageCalibration {
            DEFAULT,
@@ -297,11 +292,11 @@ protected:
        CoverageCalibration coverageCalibration;

        inline void applySizeScaleAndBias(float* outSize) const {
            if (haveSizeScale) {
                *outSize *= sizeScale;
            if (sizeScale) {
                *outSize *= *sizeScale;
            }
            if (haveSizeBias) {
                *outSize += sizeBias;
            if (sizeBias) {
                *outSize += *sizeBias;
            }
            if (*outSize < 0) {
                *outSize = 0;
@@ -475,35 +470,29 @@ private:
        InputDeviceInfo::MotionRange y;
        InputDeviceInfo::MotionRange pressure;

        bool haveSize;
        InputDeviceInfo::MotionRange size;
        std::optional<InputDeviceInfo::MotionRange> size;

        bool haveTouchSize;
        InputDeviceInfo::MotionRange touchMajor;
        InputDeviceInfo::MotionRange touchMinor;
        std::optional<InputDeviceInfo::MotionRange> touchMajor;
        std::optional<InputDeviceInfo::MotionRange> touchMinor;

        bool haveToolSize;
        InputDeviceInfo::MotionRange toolMajor;
        InputDeviceInfo::MotionRange toolMinor;
        std::optional<InputDeviceInfo::MotionRange> toolMajor;
        std::optional<InputDeviceInfo::MotionRange> toolMinor;

        bool haveOrientation;
        InputDeviceInfo::MotionRange orientation;
        std::optional<InputDeviceInfo::MotionRange> orientation;

        bool haveDistance;
        InputDeviceInfo::MotionRange distance;
        std::optional<InputDeviceInfo::MotionRange> distance;

        bool haveTilt;
        InputDeviceInfo::MotionRange tilt;

        OrientedRanges() { clear(); }
        std::optional<InputDeviceInfo::MotionRange> tilt;

        void clear() {
            haveSize = false;
            haveTouchSize = false;
            haveToolSize = false;
            haveOrientation = false;
            haveDistance = false;
            haveTilt = false;
            size = std::nullopt;
            touchMajor = std::nullopt;
            touchMinor = std::nullopt;
            toolMajor = std::nullopt;
            toolMinor = std::nullopt;
            orientation = std::nullopt;
            distance = std::nullopt;
            tilt = std::nullopt;
        }
    } mOrientedRanges;