Return a std::optional from EventHub::getAbsoluteAxisInfo
InputDevice::getAbsoluteAxisInfo will be refactored similarly in a
follow-up.
Test: atest inputflinger_tests
Test: m checkinput
Test: build and run inputflinger_multitouch_input_fuzzer for a while
Bug: 245989146
Flag: EXEMPT refactor
Change-Id: I57de645c889de53c8be0f9f8744a62e92fafdadb
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index fe70a51..7052c61 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -998,26 +998,23 @@
return *device->configuration;
}
-status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const {
- outAxisInfo->clear();
+std::optional<RawAbsoluteAxisInfo> EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis) const {
if (axis < 0 || axis > ABS_MAX) {
- return NAME_NOT_FOUND;
+ return std::nullopt;
}
std::scoped_lock _l(mLock);
const Device* device = getDeviceLocked(deviceId);
if (device == nullptr) {
- return NAME_NOT_FOUND;
+ return std::nullopt;
}
// We can read the RawAbsoluteAxisInfo even if the device is disabled and doesn't have a valid
// fd, because the info is populated once when the device is first opened, and it doesn't change
// throughout the device lifecycle.
auto it = device->absState.find(axis);
if (it == device->absState.end()) {
- return NAME_NOT_FOUND;
+ return std::nullopt;
}
- *outAxisInfo = it->second.info;
- return OK;
+ return it->second.info;
}
bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
diff --git a/services/inputflinger/reader/include/EventHub.h b/services/inputflinger/reader/include/EventHub.h
index 7cf584d..0ae57e3 100644
--- a/services/inputflinger/reader/include/EventHub.h
+++ b/services/inputflinger/reader/include/EventHub.h
@@ -21,6 +21,7 @@
#include <filesystem>
#include <functional>
#include <map>
+#include <optional>
#include <ostream>
#include <string>
#include <unordered_map>
@@ -278,8 +279,8 @@
*/
virtual std::optional<PropertyMap> getConfiguration(int32_t deviceId) const = 0;
- virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const = 0;
+ virtual std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
+ int axis) const = 0;
virtual bool hasRelativeAxis(int32_t deviceId, int axis) const = 0;
@@ -511,8 +512,8 @@
std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override final;
- status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const override final;
+ std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
+ int axis) const override final;
bool hasRelativeAxis(int32_t deviceId, int axis) const override final;
diff --git a/services/inputflinger/reader/include/InputDevice.h b/services/inputflinger/reader/include/InputDevice.h
index 2a7e262..37c7e5c 100644
--- a/services/inputflinger/reader/include/InputDevice.h
+++ b/services/inputflinger/reader/include/InputDevice.h
@@ -306,9 +306,11 @@
return mEventHub->getDeviceControllerNumber(mId);
}
inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
- if (const auto status = mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); status != OK) {
- return status;
+ std::optional<RawAbsoluteAxisInfo> info = mEventHub->getAbsoluteAxisInfo(mId, code);
+ if (!info.has_value()) {
+ return NAME_NOT_FOUND;
}
+ *axisInfo = *info;
// Validate axis info for InputDevice.
if (axisInfo->valid && axisInfo->minValue == axisInfo->maxValue) {
@@ -432,9 +434,8 @@
}
inline bool hasAbsoluteAxis(int32_t code) const {
- RawAbsoluteAxisInfo info;
- mEventHub->getAbsoluteAxisInfo(mId, code, &info);
- return info.valid;
+ std::optional<RawAbsoluteAxisInfo> info = mEventHub->getAbsoluteAxisInfo(mId, code);
+ return info.has_value() && info->valid;
}
inline bool isKeyPressed(int32_t scanCode) const {
return mEventHub->getScanCodeState(mId, scanCode) == AKEY_STATE_DOWN;
diff --git a/services/inputflinger/tests/FakeEventHub.cpp b/services/inputflinger/tests/FakeEventHub.cpp
index daa000f..1e9be0e 100644
--- a/services/inputflinger/tests/FakeEventHub.cpp
+++ b/services/inputflinger/tests/FakeEventHub.cpp
@@ -16,6 +16,8 @@
#include "FakeEventHub.h"
+#include <optional>
+
#include <android-base/thread_annotations.h>
#include <gtest/gtest.h>
#include <linux/input-event-codes.h>
@@ -263,18 +265,16 @@
return device->configuration;
}
-status_t FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const {
+std::optional<RawAbsoluteAxisInfo> FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId,
+ int axis) const {
Device* device = getDevice(deviceId);
if (device) {
ssize_t index = device->absoluteAxes.indexOfKey(axis);
if (index >= 0) {
- *outAxisInfo = device->absoluteAxes.valueAt(index);
- return OK;
+ return device->absoluteAxes.valueAt(index);
}
}
- outAxisInfo->clear();
- return -1;
+ return std::nullopt;
}
bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
diff --git a/services/inputflinger/tests/FakeEventHub.h b/services/inputflinger/tests/FakeEventHub.h
index f07b344..37bcc40 100644
--- a/services/inputflinger/tests/FakeEventHub.h
+++ b/services/inputflinger/tests/FakeEventHub.h
@@ -168,8 +168,8 @@
InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override;
int32_t getDeviceControllerNumber(int32_t) const override;
std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override;
- status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const override;
+ std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
+ int axis) const override;
bool hasRelativeAxis(int32_t deviceId, int axis) const override;
bool hasInputProperty(int32_t, int) const override;
bool hasMscEvent(int32_t deviceId, int mscEvent) const override final;
diff --git a/services/inputflinger/tests/HardwareProperties_test.cpp b/services/inputflinger/tests/HardwareProperties_test.cpp
index 8dfa8c8..643fab6 100644
--- a/services/inputflinger/tests/HardwareProperties_test.cpp
+++ b/services/inputflinger/tests/HardwareProperties_test.cpp
@@ -48,24 +48,20 @@
static constexpr int32_t EVENTHUB_ID = 1;
void setupValidAxis(int axis, int32_t min, int32_t max, int32_t resolution) {
- EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis, testing::_))
- .WillRepeatedly([=](int32_t, int32_t, RawAbsoluteAxisInfo* outAxisInfo) {
- outAxisInfo->valid = true;
- outAxisInfo->minValue = min;
- outAxisInfo->maxValue = max;
- outAxisInfo->flat = 0;
- outAxisInfo->fuzz = 0;
- outAxisInfo->resolution = resolution;
- return OK;
- });
+ EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
+ .WillRepeatedly(Return(std::optional<RawAbsoluteAxisInfo>{{
+ .valid = true,
+ .minValue = min,
+ .maxValue = max,
+ .flat = 0,
+ .fuzz = 0,
+ .resolution = resolution,
+ }}));
}
void setupInvalidAxis(int axis) {
- EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis, testing::_))
- .WillRepeatedly([=](int32_t, int32_t, RawAbsoluteAxisInfo* outAxisInfo) {
- outAxisInfo->valid = false;
- return -1;
- });
+ EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
+ .WillRepeatedly(Return(std::nullopt));
}
void setProperty(int property, bool value) {
diff --git a/services/inputflinger/tests/InputMapperTest.cpp b/services/inputflinger/tests/InputMapperTest.cpp
index b5c9232..19bc5be 100644
--- a/services/inputflinger/tests/InputMapperTest.cpp
+++ b/services/inputflinger/tests/InputMapperTest.cpp
@@ -57,16 +57,16 @@
void InputMapperUnitTest::setupAxis(int axis, bool valid, int32_t min, int32_t max,
int32_t resolution) {
- EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis, _))
- .WillRepeatedly([=](int32_t, int32_t, RawAbsoluteAxisInfo* outAxisInfo) {
- outAxisInfo->valid = valid;
- outAxisInfo->minValue = min;
- outAxisInfo->maxValue = max;
- outAxisInfo->flat = 0;
- outAxisInfo->fuzz = 0;
- outAxisInfo->resolution = resolution;
- return valid ? OK : -1;
- });
+ EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
+ .WillRepeatedly(Return(valid ? std::optional<RawAbsoluteAxisInfo>{{
+ .valid = true,
+ .minValue = min,
+ .maxValue = max,
+ .flat = 0,
+ .fuzz = 0,
+ .resolution = resolution,
+ }}
+ : std::nullopt));
}
void InputMapperUnitTest::expectScanCodes(bool present, std::set<int> scanCodes) {
diff --git a/services/inputflinger/tests/InterfaceMocks.h b/services/inputflinger/tests/InterfaceMocks.h
index 16d3193..a7a0833 100644
--- a/services/inputflinger/tests/InterfaceMocks.h
+++ b/services/inputflinger/tests/InterfaceMocks.h
@@ -91,8 +91,8 @@
MOCK_METHOD(InputDeviceIdentifier, getDeviceIdentifier, (int32_t deviceId), (const));
MOCK_METHOD(int32_t, getDeviceControllerNumber, (int32_t deviceId), (const));
MOCK_METHOD(std::optional<PropertyMap>, getConfiguration, (int32_t deviceId), (const));
- MOCK_METHOD(status_t, getAbsoluteAxisInfo,
- (int32_t deviceId, int axis, RawAbsoluteAxisInfo* outAxisInfo), (const));
+ MOCK_METHOD(std::optional<RawAbsoluteAxisInfo>, getAbsoluteAxisInfo,
+ (int32_t deviceId, int axis), (const));
MOCK_METHOD(bool, hasRelativeAxis, (int32_t deviceId, int axis), (const));
MOCK_METHOD(bool, hasInputProperty, (int32_t deviceId, int property), (const));
MOCK_METHOD(bool, hasMscEvent, (int32_t deviceId, int mscEvent), (const));
diff --git a/services/inputflinger/tests/fuzzers/MapperHelpers.h b/services/inputflinger/tests/fuzzers/MapperHelpers.h
index ff425dd..50fbf4b 100644
--- a/services/inputflinger/tests/fuzzers/MapperHelpers.h
+++ b/services/inputflinger/tests/fuzzers/MapperHelpers.h
@@ -17,6 +17,7 @@
#include <map>
#include <memory>
+#include <optional>
#include <EventHub.h>
#include <InputDevice.h>
@@ -119,16 +120,26 @@
void setAbsoluteAxisInfo(int32_t deviceId, int axis, const RawAbsoluteAxisInfo& axisInfo) {
mAxes[deviceId][axis] = axisInfo;
}
- status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
- RawAbsoluteAxisInfo* outAxisInfo) const override {
+ std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t deviceId,
+ int axis) const override {
if (auto deviceAxesIt = mAxes.find(deviceId); deviceAxesIt != mAxes.end()) {
const std::map<int, RawAbsoluteAxisInfo>& deviceAxes = deviceAxesIt->second;
if (auto axisInfoIt = deviceAxes.find(axis); axisInfoIt != deviceAxes.end()) {
- *outAxisInfo = axisInfoIt->second;
- return OK;
+ return axisInfoIt->second;
}
}
- return mFdp->ConsumeIntegral<status_t>();
+ if (mFdp->ConsumeBool()) {
+ return std::optional<RawAbsoluteAxisInfo>({
+ .valid = mFdp->ConsumeBool(),
+ .minValue = mFdp->ConsumeIntegral<int32_t>(),
+ .maxValue = mFdp->ConsumeIntegral<int32_t>(),
+ .flat = mFdp->ConsumeIntegral<int32_t>(),
+ .fuzz = mFdp->ConsumeIntegral<int32_t>(),
+ .resolution = mFdp->ConsumeIntegral<int32_t>(),
+ });
+ } else {
+ return std::nullopt;
+ }
}
bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
bool hasInputProperty(int32_t deviceId, int property) const override {