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

Commit a7b82e1d authored by Nathaniel R. Lewis's avatar Nathaniel R. Lewis Committed by Prabir Pradhan
Browse files

Support multiple EventHub devices per InputDevice

Some physical devices contain more functions than can be addressed by
a single Linux evdev device. Examples of such devices are the Sony
DualShock 4 and Wacom Tablets, which appear as multiple evdev devices
sharing the same value for the EVIOCGUNIQ ioctl. As more instances of
such devices hit the market, apps need a way of figuring out which
InputDevices are part of the same physical device.

Per conversation with the android input team, a solution proposed to
this problem is to merge multiple EventHub devices (which have a 1:1
relation with evdev) into a single android InputDevice.

Changes:

Decouple the EventHub device id ("eventHubId") from the InputDevice
device id ("deviceId"). This requires InputDeviceContext to track the
the EventHub devices it represents. Most logic changes are inside
InputDeviceContext, so there are minimal changes to InputMappers.

Added enum value END_RESERVED_ID to represent the first available
id that can be assigned to a normal InputDevice. The android
framework assumes specific values for the virtual keyboard and
built-in hardware keyboard, so for these two cases, the "deviceId"
must match the "eventHubId."

Added "EVENTHUB_ID" constants to tests and changed where applicable.

Cherry-picked from pa/1475694.

Bug: 38511270
Test: atest inputflinger_tests libinput_tests
Change-Id: I89df085fadc1c09bc999599ac5db35c9277c4a2a
parent 0cab12d4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -180,6 +180,8 @@ enum ReservedInputDeviceId : int32_t {
    VIRTUAL_KEYBOARD_ID = -1,
    // Device id of the "built-in" keyboard if there is one.
    BUILT_IN_KEYBOARD_ID = 0,
    // First device id available for dynamic devices
    END_RESERVED_ID = 1,
};

} // namespace android
+97 −36
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#include "InputDevice.h"

#include <algorithm>

#include "CursorInputMapper.h"
#include "ExternalStylusInputMapper.h"
#include "InputReaderContext.h"
@@ -32,25 +34,28 @@
namespace android {

InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
                         int32_t controllerNumber, const InputDeviceIdentifier& identifier,
                         uint32_t classes)
                         const InputDeviceIdentifier& identifier)
      : mContext(context),
        mId(id),
        mGeneration(generation),
        mControllerNumber(controllerNumber),
        mControllerNumber(0),
        mIdentifier(identifier),
        mClasses(classes),
        mClasses(0),
        mSources(0),
        mIsExternal(false),
        mHasMic(false),
        mDropUntilNextSync(false) {
    mDeviceContext = std::make_unique<InputDeviceContext>(*this);
}
        mDropUntilNextSync(false) {}

InputDevice::~InputDevice() {}

bool InputDevice::isEnabled() {
    return mDeviceContext->isDeviceEnabled();
    if (!hasEventHubDevices()) {
        return false;
    }
    // devices are either all enabled or all disabled, so we only need to check the first
    auto& devicePair = mDevices.begin()->second;
    auto& contextPtr = devicePair.first;
    return contextPtr->isDeviceEnabled();
}

void InputDevice::setEnabled(bool enabled, nsecs_t when) {
@@ -65,12 +70,15 @@ void InputDevice::setEnabled(bool enabled, nsecs_t when) {
        return;
    }

    // When resetting some devices, the driver needs to be queried to ensure that a proper reset is
    // performed. The querying must happen when the device is enabled, so we reset after enabling
    // but before disabling the device. See MultiTouchMotionAccumulator::reset for more information.
    if (enabled) {
        mDeviceContext->enableDevice();
        for_each_subdevice([](auto& context) { context.enableDevice(); });
        reset(when);
    } else {
        reset(when);
        mDeviceContext->disableDevice();
        for_each_subdevice([](auto& context) { context.disableDevice(); });
    }
    // Must change generation to flag this device as changed
    bumpGeneration();
@@ -118,19 +126,18 @@ void InputDevice::dump(std::string& dump) {
    for_each_mapper([&dump](InputMapper& mapper) { mapper.dump(dump); });
}

void InputDevice::populateMappers() {
    uint32_t classes = mClasses;
    std::vector<std::unique_ptr<InputMapper>>& mappers = mMappers;
    std::unique_ptr<InputDeviceContext>& contextPtr = mDeviceContext;

    // External devices.
    if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
        setExternal(true);
void InputDevice::addEventHubDevice(int32_t eventHubId, bool populateMappers) {
    if (mDevices.find(eventHubId) != mDevices.end()) {
        return;
    }
    std::unique_ptr<InputDeviceContext> contextPtr(new InputDeviceContext(*this, eventHubId));
    uint32_t classes = contextPtr->getDeviceClasses();
    std::vector<std::unique_ptr<InputMapper>> mappers;

    // Devices with mics.
    if (classes & INPUT_DEVICE_CLASS_MIC) {
        setMic(true);
    // Check if we should skip population
    if (!populateMappers) {
        mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
        return;
    }

    // Switch-like devices.
@@ -190,22 +197,58 @@ void InputDevice::populateMappers() {
    if (classes & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
        mappers.push_back(std::make_unique<ExternalStylusInputMapper>(*contextPtr));
    }

    // insert the context into the devices set
    mDevices.insert({eventHubId, std::make_pair(std::move(contextPtr), std::move(mappers))});
}

void InputDevice::removeEventHubDevice(int32_t eventHubId) {
    mDevices.erase(eventHubId);
}

void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
                            uint32_t changes) {
    mSources = 0;
    mClasses = 0;
    mControllerNumber = 0;

    for_each_subdevice([this](InputDeviceContext& context) {
        mClasses |= context.getDeviceClasses();
        int32_t controllerNumber = context.getDeviceControllerNumber();
        if (controllerNumber > 0) {
            if (mControllerNumber && mControllerNumber != controllerNumber) {
                ALOGW("InputDevice::configure(): composite device contains multiple unique "
                      "controller numbers");
            }
            mControllerNumber = controllerNumber;
        }
    });

    mIsExternal = !!(mClasses & INPUT_DEVICE_CLASS_EXTERNAL);
    mHasMic = !!(mClasses & INPUT_DEVICE_CLASS_MIC);

    if (!isIgnored()) {
        if (!changes) { // first time only
            mDeviceContext->getConfiguration(&mConfiguration);
            mConfiguration.clear();
            for_each_subdevice([this](InputDeviceContext& context) {
                PropertyMap configuration;
                context.getConfiguration(&configuration);
                mConfiguration.addAll(&configuration);
            });
        }

        if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
            if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
                sp<KeyCharacterMap> keyboardLayout =
                        mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
                if (mDeviceContext->setKeyboardLayoutOverlay(keyboardLayout)) {
                bool shouldBumpGeneration = false;
                for_each_subdevice(
                        [&keyboardLayout, &shouldBumpGeneration](InputDeviceContext& context) {
                            if (context.setKeyboardLayoutOverlay(keyboardLayout)) {
                                shouldBumpGeneration = true;
                            }
                        });
                if (shouldBumpGeneration) {
                    bumpGeneration();
                }
            }
@@ -313,7 +356,9 @@ void InputDevice::process(const RawEvent* rawEvents, size_t count) {
            mDropUntilNextSync = true;
            reset(rawEvent->when);
        } else {
            for_each_mapper([rawEvent](InputMapper& mapper) { mapper.process(rawEvent); });
            for_each_mapper_in_subdevice(rawEvent->deviceId, [rawEvent](InputMapper& mapper) {
                mapper.process(rawEvent);
            });
        }
        --count;
    }
@@ -348,7 +393,10 @@ int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {

int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
    int32_t result = AKEY_STATE_UNKNOWN;
    for (auto& mapperPtr : mMappers) {
    for (auto& deviceEntry : mDevices) {
        auto& devicePair = deviceEntry.second;
        auto& mappers = devicePair.second;
        for (auto& mapperPtr : mappers) {
            InputMapper& mapper = *mapperPtr;
            if (sourcesMatchMask(mapper.getSources(), sourceMask)) {
                // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
@@ -361,6 +409,7 @@ int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc ge
                }
            }
        }
    }
    return result;
}

@@ -424,11 +473,23 @@ std::optional<int32_t> InputDevice::getAssociatedDisplayId() {
            [](InputMapper& mapper) { return mapper.getAssociatedDisplayId(); });
}

InputDeviceContext::InputDeviceContext(InputDevice& device)
// returns the number of mappers associated with the device
size_t InputDevice::getMapperCount() {
    size_t count = 0;
    for (auto& deviceEntry : mDevices) {
        auto& devicePair = deviceEntry.second;
        auto& mappers = devicePair.second;
        count += mappers.size();
    }
    return count;
}

InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId)
      : mDevice(device),
        mContext(device.getContext()),
        mEventHub(device.getContext()->getEventHub()),
        mId(device.getId()) {}
        mId(eventHubId),
        mDeviceId(device.getId()) {}

InputDeviceContext::~InputDeviceContext() {}

+76 −56
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
        mNextSequenceNum(1),
        mGlobalMetaState(0),
        mGeneration(1),
        mNextInputDeviceId(END_RESERVED_ID),
        mDisableVirtualKeysTimeout(LLONG_MIN),
        mNextTimeout(LLONG_MAX),
        mConfigurationChangesToRefresh(0) {
@@ -184,30 +185,28 @@ void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
    }
}

void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
    if (mDevices.find(deviceId) != mDevices.end()) {
        ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
void InputReader::addDeviceLocked(nsecs_t when, int32_t eventHubId) {
    if (mDevices.find(eventHubId) != mDevices.end()) {
        ALOGW("Ignoring spurious device added event for eventHubId %d.", eventHubId);
        return;
    }

    InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
    uint32_t classes = mEventHub->getDeviceClasses(deviceId);
    int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);

    std::shared_ptr<InputDevice> device =
            createDeviceLocked(deviceId, controllerNumber, identifier, classes);
    InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(eventHubId);
    std::shared_ptr<InputDevice> device = createDeviceLocked(eventHubId, identifier);
    device->configure(when, &mConfig, 0);
    device->reset(when);

    if (device->isIgnored()) {
        ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
              identifier.name.c_str());
        ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
              "(ignored non-input device)",
              device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str());
    } else {
        ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, identifier.name.c_str(),
        ALOGI("Device added: id=%d, eventHubId=%d, name='%s', descriptor='%s',sources=0x%08x",
              device->getId(), eventHubId, identifier.name.c_str(), identifier.descriptor.c_str(),
              device->getSources());
    }

    mDevices.emplace(deviceId, device);
    mDevices.emplace(eventHubId, device);
    bumpGenerationLocked();

    if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
@@ -215,10 +214,10 @@ void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
    }
}

void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
    auto deviceIt = mDevices.find(deviceId);
void InputReader::removeDeviceLocked(nsecs_t when, int32_t eventHubId) {
    auto deviceIt = mDevices.find(eventHubId);
    if (deviceIt == mDevices.end()) {
        ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
        ALOGW("Ignoring spurious device removed event for eventHubId %d.", eventHubId);
        return;
    }

@@ -227,35 +226,52 @@ void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
    bumpGenerationLocked();

    if (device->isIgnored()) {
        ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", device->getId(),
              device->getName().c_str());
        ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s' "
              "(ignored non-input device)",
              device->getId(), eventHubId, device->getName().c_str(),
              device->getDescriptor().c_str());
    } else {
        ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", device->getId(),
              device->getName().c_str(), device->getSources());
        ALOGI("Device removed: id=%d, eventHubId=%d, name='%s', descriptor='%s', sources=0x%08x",
              device->getId(), eventHubId, device->getName().c_str(),
              device->getDescriptor().c_str(), device->getSources());
    }

    device->removeEventHubDevice(eventHubId);

    if (device->getClasses() & INPUT_DEVICE_CLASS_EXTERNAL_STYLUS) {
        notifyExternalStylusPresenceChanged();
    }

    if (device->hasEventHubDevices()) {
        device->configure(when, &mConfig, 0);
    }
    device->reset(when);
}

std::shared_ptr<InputDevice> InputReader::createDeviceLocked(
        int32_t deviceId, int32_t controllerNumber, const InputDeviceIdentifier& identifier,
        uint32_t classes) {
    std::shared_ptr<InputDevice> device =
            std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
                                          controllerNumber, identifier, classes);
    device->populateMappers();
        int32_t eventHubId, const InputDeviceIdentifier& identifier) {
    auto deviceIt = std::find_if(mDevices.begin(), mDevices.end(), [identifier](auto& devicePair) {
        return devicePair.second->getDescriptor().size() && identifier.descriptor.size() &&
                devicePair.second->getDescriptor() == identifier.descriptor;
    });

    std::shared_ptr<InputDevice> device;
    if (deviceIt != mDevices.end()) {
        device = deviceIt->second;
    } else {
        int32_t deviceId = (eventHubId < END_RESERVED_ID) ? eventHubId : nextInputDeviceIdLocked();
        device = std::make_shared<InputDevice>(&mContext, deviceId, bumpGenerationLocked(),
                                               identifier);
    }
    device->addEventHubDevice(eventHubId);
    return device;
}

void InputReader::processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents,
void InputReader::processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents,
                                               size_t count) {
    auto deviceIt = mDevices.find(deviceId);
    auto deviceIt = mDevices.find(eventHubId);
    if (deviceIt == mDevices.end()) {
        ALOGW("Discarding event for unknown deviceId %d.", deviceId);
        ALOGW("Discarding event for unknown eventHubId %d.", eventHubId);
        return;
    }

@@ -268,6 +284,17 @@ void InputReader::processEventsForDeviceLocked(int32_t deviceId, const RawEvent*
    device->process(rawEvents, count);
}

InputDevice* InputReader::findInputDevice(int32_t deviceId) {
    auto deviceIt =
            std::find_if(mDevices.begin(), mDevices.end(), [deviceId](const auto& devicePair) {
                return devicePair.second->getId() == deviceId;
            });
    if (deviceIt != mDevices.end()) {
        return deviceIt->second.get();
    }
    return nullptr;
}

void InputReader::timeoutExpiredLocked(nsecs_t when) {
    for (auto& devicePair : mDevices) {
        std::shared_ptr<InputDevice>& device = devicePair.second;
@@ -277,6 +304,10 @@ void InputReader::timeoutExpiredLocked(nsecs_t when) {
    }
}

int32_t InputReader::nextInputDeviceIdLocked() {
    return ++mNextInputDeviceId;
}

void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
    // Reset global meta state because it depends on the list of all configured devices.
    updateGlobalMetaStateLocked();
@@ -414,12 +445,9 @@ int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32
                                    GetStateFunc getStateFunc) {
    int32_t result = AKEY_STATE_UNKNOWN;
    if (deviceId >= 0) {
        auto deviceIt = mDevices.find(deviceId);
        if (deviceIt != mDevices.end()) {
            std::shared_ptr<InputDevice>& device = deviceIt->second;
            if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
                result = (device.get()->*getStateFunc)(sourceMask, code);
            }
        InputDevice* device = findInputDevice(deviceId);
        if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
            result = (device->*getStateFunc)(sourceMask, code);
        }
    } else {
        for (auto& devicePair : mDevices) {
@@ -440,13 +468,12 @@ int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32
}

void InputReader::toggleCapsLockState(int32_t deviceId) {
    auto deviceIt = mDevices.find(deviceId);
    if (deviceIt == mDevices.end()) {
    InputDevice* device = findInputDevice(deviceId);
    if (!device) {
        ALOGW("Ignoring toggleCapsLock for unknown deviceId %" PRId32 ".", deviceId);
        return;
    }

    std::shared_ptr<InputDevice>& device = deviceIt->second;
    if (device->isIgnored()) {
        return;
    }
@@ -467,13 +494,10 @@ bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceM
                                              uint8_t* outFlags) {
    bool result = false;
    if (deviceId >= 0) {
        auto deviceIt = mDevices.find(deviceId);
        if (deviceIt != mDevices.end()) {
            std::shared_ptr<InputDevice>& device = deviceIt->second;
            if (!device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
        InputDevice* device = findInputDevice(deviceId);
        if (device && !device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
            result = device->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
        }
        }
    } else {
        for (auto& devicePair : mDevices) {
            std::shared_ptr<InputDevice>& device = devicePair.second;
@@ -501,9 +525,8 @@ void InputReader::requestRefreshConfiguration(uint32_t changes) {
void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
                          ssize_t repeat, int32_t token) {
    AutoMutex _l(mLock);
    auto deviceIt = mDevices.find(deviceId);
    if (deviceIt != mDevices.end()) {
        std::shared_ptr<InputDevice>& device = deviceIt->second;
    InputDevice* device = findInputDevice(deviceId);
    if (device) {
        device->vibrate(pattern, patternSize, repeat, token);
    }
}
@@ -511,9 +534,8 @@ void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patte
void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
    AutoMutex _l(mLock);

    auto deviceIt = mDevices.find(deviceId);
    if (deviceIt != mDevices.end()) {
        std::shared_ptr<InputDevice>& device = deviceIt->second;
    InputDevice* device = findInputDevice(deviceId);
    if (device) {
        device->cancelVibrate(token);
    }
}
@@ -521,9 +543,8 @@ void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
    AutoMutex _l(mLock);

    auto deviceIt = mDevices.find(deviceId);
    if (deviceIt != mDevices.end()) {
        std::shared_ptr<InputDevice>& device = deviceIt->second;
    InputDevice* device = findInputDevice(deviceId);
    if (device) {
        return device->isEnabled();
    }
    ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
@@ -533,13 +554,12 @@ bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
bool InputReader::canDispatchToDisplay(int32_t deviceId, int32_t displayId) {
    AutoMutex _l(mLock);

    auto deviceIt = mDevices.find(deviceId);
    if (deviceIt == mDevices.end()) {
    InputDevice* device = findInputDevice(deviceId);
    if (!device) {
        ALOGW("Ignoring invalid device id %" PRId32 ".", deviceId);
        return false;
    }

    std::shared_ptr<InputDevice>& device = deviceIt->second;
    if (!device->isEnabled()) {
        ALOGW("Ignoring disabled device %s", device->getName().c_str());
        return false;
+69 −25
Original line number Diff line number Diff line
@@ -17,18 +17,19 @@
#ifndef _UI_INPUTREADER_INPUT_DEVICE_H
#define _UI_INPUTREADER_INPUT_DEVICE_H

#include "EventHub.h"
#include "InputReaderBase.h"
#include "InputReaderContext.h"

#include <input/DisplayViewport.h>
#include <input/InputDevice.h>
#include <stdint.h>
#include <utils/PropertyMap.h>

#include <stdint.h>
#include <optional>
#include <unordered_map>
#include <vector>

#include "EventHub.h"
#include "InputReaderBase.h"
#include "InputReaderContext.h"

namespace android {

class InputDeviceContext;
@@ -38,8 +39,7 @@ class InputMapper;
class InputDevice {
public:
    InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
                int32_t controllerNumber, const InputDeviceIdentifier& identifier,
                uint32_t classes);
                const InputDeviceIdentifier& identifier);
    ~InputDevice();

    inline InputReaderContext* getContext() { return mContext; }
@@ -50,25 +50,25 @@ public:
    inline const std::string getDescriptor() { return mIdentifier.descriptor; }
    inline uint32_t getClasses() const { return mClasses; }
    inline uint32_t getSources() const { return mSources; }
    inline bool hasEventHubDevices() const { return !mDevices.empty(); }

    inline bool isExternal() { return mIsExternal; }
    inline void setExternal(bool external) { mIsExternal = external; }
    inline std::optional<uint8_t> getAssociatedDisplayPort() const {
        return mAssociatedDisplayPort;
    }
    inline std::optional<DisplayViewport> getAssociatedViewport() const {
        return mAssociatedViewport;
    }
    inline void setMic(bool hasMic) { mHasMic = hasMic; }
    inline bool hasMic() const { return mHasMic; }

    inline bool isIgnored() { return mMappers.empty(); }
    inline bool isIgnored() { return !getMapperCount(); }

    bool isEnabled();
    void setEnabled(bool enabled, nsecs_t when);

    void dump(std::string& dump);
    void populateMappers();
    void addEventHubDevice(int32_t eventHubId, bool populateMappers = true);
    void removeEventHubDevice(int32_t eventHubId);
    void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
    void reset(nsecs_t when);
    void process(const RawEvent* rawEvents, size_t count);
@@ -99,11 +99,20 @@ public:

    std::optional<int32_t> getAssociatedDisplayId();

    size_t getMapperCount();

    // construct and add a mapper to the input device
    template <class T, typename... Args>
    T& addMapper(Args... args) {
        T* mapper = new T(*mDeviceContext, args...);
        mMappers.emplace_back(mapper);
    T& addMapper(int32_t eventHubId, Args... args) {
        // ensure a device entry exists for this eventHubId
        addEventHubDevice(eventHubId, false);

        // create mapper
        auto& devicePair = mDevices[eventHubId];
        auto& deviceContext = devicePair.first;
        auto& mappers = devicePair.second;
        T* mapper = new T(*deviceContext, args...);
        mappers.emplace_back(mapper);
        return *mapper;
    }

@@ -116,8 +125,10 @@ private:
    std::string mAlias;
    uint32_t mClasses;

    std::unique_ptr<InputDeviceContext> mDeviceContext;
    std::vector<std::unique_ptr<InputMapper>> mMappers;
    // map from eventHubId to device context and mappers
    using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
    using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
    std::unordered_map<int32_t, DevicePair> mDevices;

    uint32_t mSources;
    bool mIsExternal;
@@ -131,23 +142,54 @@ private:

    PropertyMap mConfiguration;

    // run a function against every mapper
    // helpers to interate over the devices collection
    // run a function against every mapper on every subdevice
    inline void for_each_mapper(std::function<void(InputMapper&)> f) {
        for (auto& mapperPtr : mMappers) {
        for (auto& deviceEntry : mDevices) {
            auto& devicePair = deviceEntry.second;
            auto& mappers = devicePair.second;
            for (auto& mapperPtr : mappers) {
                f(*mapperPtr);
            }
        }
    }

    // run a function against every mapper on a specific subdevice
    inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
                                             std::function<void(InputMapper&)> f) {
        auto deviceIt = mDevices.find(eventHubDevice);
        if (deviceIt != mDevices.end()) {
            auto& devicePair = deviceIt->second;
            auto& mappers = devicePair.second;
            for (auto& mapperPtr : mappers) {
                f(*mapperPtr);
            }
        }
    }

    // run a function against every subdevice
    inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
        for (auto& deviceEntry : mDevices) {
            auto& devicePair = deviceEntry.second;
            auto& contextPtr = devicePair.first;
            f(*contextPtr);
        }
    }

    // return the first value returned by a function over every mapper.
    // if all mappers return nullopt, return nullopt.
    template <typename T>
    inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
        for (auto& mapperPtr : mMappers) {
        for (auto& deviceEntry : mDevices) {
            auto& devicePair = deviceEntry.second;
            auto& mappers = devicePair.second;
            for (auto& mapperPtr : mappers) {
                std::optional<T> ret = f(*mapperPtr);
                if (ret) {
                    return ret;
                }
            }
        }
        return std::nullopt;
    }
};
@@ -159,11 +201,12 @@ private:
 */
class InputDeviceContext {
public:
    InputDeviceContext(InputDevice& device);
    InputDeviceContext(InputDevice& device, int32_t eventHubId);
    ~InputDeviceContext();

    inline InputReaderContext* getContext() { return mContext; }
    inline int32_t getId() { return mId; }
    inline int32_t getId() { return mDeviceId; }
    inline int32_t getEventHubId() { return mId; }

    inline uint32_t getDeviceClasses() const { return mEventHub->getDeviceClasses(mId); }
    inline InputDeviceIdentifier getDeviceIdentifier() const {
@@ -259,6 +302,7 @@ private:
    InputReaderContext* mContext;
    EventHubInterface* mEventHub;
    int32_t mId;
    int32_t mDeviceId;
};

} // namespace android
+14 −8

File changed.

Preview size limit exceeded, changes collapsed.

Loading