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

Commit 77140f4c authored by Arpit Singh's avatar Arpit Singh
Browse files

InputMapper refactor: Cache RawAbsoluteAxisInfo

RawAbsoluteAxisInfo remains unchanged during lifetime of a device we can
store this value in cache. This will minimise dependency on
enable/disable and allow us to make read failures fatal.

Test: m checkinput && atest inputflinger_tests
Bug: 285478143

Change-Id: Ied062566f58938601a65d9e643e7acbcabd3c665
parent 5c02a719
Loading
Loading
Loading
Loading
+40 −24
Original line number Diff line number Diff line
@@ -864,6 +864,30 @@ void EventHub::addDeviceInotify() {
                        strerror(errno));
}

void EventHub::populateDeviceAbsoluteAxisInfo(Device& device) {
    for (int axis = 0; axis <= ABS_MAX; axis++) {
        if (!device.absBitmask.test(axis)) {
            continue;
        }
        struct input_absinfo info {};
        if (ioctl(device.fd, EVIOCGABS(axis), &info)) {
            ALOGE("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
                  device.identifier.name.c_str(), device.fd, errno);
            continue;
        }
        if (info.minimum == info.maximum) {
            continue;
        }
        RawAbsoluteAxisInfo& outAxisInfo = device.rawAbsoluteAxisInfoCache[axis];
        outAxisInfo.valid = true;
        outAxisInfo.minValue = info.minimum;
        outAxisInfo.maxValue = info.maximum;
        outAxisInfo.flat = info.flat;
        outAxisInfo.fuzz = info.fuzz;
        outAxisInfo.resolution = info.resolution;
    }
}

InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
    std::scoped_lock _l(mLock);
    Device* device = getDeviceLocked(deviceId);
@@ -894,32 +918,21 @@ std::optional<PropertyMap> EventHub::getConfiguration(int32_t deviceId) const {
status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
                                       RawAbsoluteAxisInfo* outAxisInfo) const {
    outAxisInfo->clear();

    if (axis >= 0 && axis <= ABS_MAX) {
    if (axis < 0 || axis > ABS_MAX) {
        return -1;
    }
    std::scoped_lock _l(mLock);

    Device* device = getDeviceLocked(deviceId);
        if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
            struct input_absinfo info;
            if (ioctl(device->fd, EVIOCGABS(axis), &info)) {
                ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis,
                      device->identifier.name.c_str(), device->fd, errno);
                return -errno;
    if (device == nullptr) {
        return -1;
    }

            if (info.minimum != info.maximum) {
                outAxisInfo->valid = true;
                outAxisInfo->minValue = info.minimum;
                outAxisInfo->maxValue = info.maximum;
                outAxisInfo->flat = info.flat;
                outAxisInfo->fuzz = info.fuzz;
                outAxisInfo->resolution = info.resolution;
    auto it = device->rawAbsoluteAxisInfoCache.find(axis);
    if (it == device->rawAbsoluteAxisInfoCache.end()) {
        return -1;
    }
    *outAxisInfo = it->second;
    return OK;
}
    }
    return -1;
}

bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
    if (axis >= 0 && axis <= REL_MAX) {
@@ -2435,6 +2448,9 @@ void EventHub::openDeviceLocked(const std::string& devicePath) {

    device->configureFd();

    // read absolute axis info for all available axes for the device
    populateDeviceAbsoluteAxisInfo(*device);

    ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, "
          "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ",
          deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(),
+8 −0
Original line number Diff line number Diff line
@@ -615,6 +615,7 @@ private:
        std::unique_ptr<PropertyMap> configuration;
        std::unique_ptr<VirtualKeyMap> virtualKeyMap;
        KeyMap keyMap;
        std::unordered_map<int /*axis*/, RawAbsoluteAxisInfo> rawAbsoluteAxisInfoCache;

        bool ffEffectPlaying;
        int16_t ffEffectId; // initially -1
@@ -717,6 +718,13 @@ private:
    void addDeviceInputInotify();
    void addDeviceInotify();

    /**
     * AbsoluteAxisInfo remains unchanged for the lifetime of the device, hence
     * we can read and store it with device
     * @param device target device
     */
    static void populateDeviceAbsoluteAxisInfo(Device& device);

    // Protect all internal state.
    mutable std::mutex mLock;