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

Commit bb26732f authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge changes from topics "input-device-bluetooth-address", "notify-stylus-presence"

* changes:
  InputReader: Get Bluetooth address from InputDeviceIdentifier
  Determine the bluetooth address of an input device from its uniqueId
  Notify the policy when a stylus gesture starts
parents e9af9d01 b54ffb2a
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -58,6 +58,9 @@ struct InputDeviceIdentifier {
    // reuse values that are not associated with an input anymore.
    uint16_t nonce;

    // The bluetooth address of the device, if known.
    std::optional<std::string> bluetoothAddress;

    /**
     * Return InputDeviceIdentifier.name that has been adjusted as follows:
     *     - all characters besides alphanumerics, dash,
+7 −3
Original line number Diff line number Diff line
@@ -24,15 +24,19 @@
namespace android {

template <typename T>
std::string constToString(const T& v) {
inline std::string constToString(const T& v) {
    return std::to_string(v);
}

inline std::string constToString(const std::string& s) {
    return s;
}

/**
 * Convert an optional type to string.
 */
template <typename T>
std::string toString(const std::optional<T>& optional,
inline std::string toString(const std::optional<T>& optional,
                            std::string (*toString)(const T&) = constToString) {
    return optional ? toString(*optional) : "<not set>";
}
+5 −0
Original line number Diff line number Diff line
@@ -142,6 +142,9 @@ public:
    virtual std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) = 0;
    /* Get light player ID */
    virtual std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) = 0;

    /* Get the Bluetooth address of an input device, if known. */
    virtual std::optional<std::string> getBluetoothAddress(int32_t deviceId) const = 0;
};

// --- InputReaderConfiguration ---
@@ -393,6 +396,8 @@ public:
    /* Gets the affine calibration associated with the specified device. */
    virtual TouchAffineTransformation getTouchAffineTransformation(
            const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
    /* Notifies the input reader policy that a stylus gesture has started. */
    virtual void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) = 0;
};

} // namespace android
+15 −6
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include <ftl/enum.h>
#include <input/KeyCharacterMap.h>
#include <input/KeyLayoutMap.h>
#include <input/PrintTools.h>
#include <input/VirtualKeyMap.h>
#include <openssl/sha.h>
#include <statslog.h>
@@ -134,10 +135,6 @@ const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightC
                                                                  {"green", LightColor::GREEN},
                                                                  {"blue", LightColor::BLUE}};

static inline const char* toString(bool value) {
    return value ? "true" : "false";
}

static std::string sha1(const std::string& in) {
    SHA_CTX ctx;
    SHA1_Init(&ctx);
@@ -2128,6 +2125,17 @@ void EventHub::openDeviceLocked(const std::string& devicePath) {
        identifier.uniqueId = buffer;
    }

    // Attempt to get the bluetooth address of an input device from the uniqueId.
    if (identifier.bus == BUS_BLUETOOTH &&
        std::regex_match(identifier.uniqueId,
                         std::regex("^[A-Fa-f0-9]{2}(?::[A-Fa-f0-9]{2}){5}$"))) {
        identifier.bluetoothAddress = identifier.uniqueId;
        // The Bluetooth stack requires alphabetic characters to be uppercase in a valid address.
        for (auto& c : *identifier.bluetoothAddress) {
            c = ::toupper(c);
        }
    }

    // Fill in the descriptor.
    assignDescriptorLocked(identifier);

@@ -2625,9 +2633,10 @@ void EventHub::dump(std::string& dump) const {
            dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
            dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str());
            dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
                                         "product=0x%04x, version=0x%04x\n",
                                         "product=0x%04x, version=0x%04x, bluetoothAddress=%s\n",
                                 device->identifier.bus, device->identifier.vendor,
                                 device->identifier.product, device->identifier.version);
                                 device->identifier.product, device->identifier.version,
                                 toString(device->identifier.bluetoothAddress).c_str());
            dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n",
                                 device->keyMap.keyLayoutFile.c_str());
            dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n",
+36 −2
Original line number Diff line number Diff line
@@ -58,6 +58,18 @@ static bool isSubDevice(const InputDeviceIdentifier& identifier1,
            identifier1.location == identifier2.location);
}

static bool isStylusPointerGestureStart(const NotifyMotionArgs& motionArgs) {
    const auto actionMasked = MotionEvent::getActionMasked(motionArgs.action);
    if (actionMasked != AMOTION_EVENT_ACTION_HOVER_ENTER &&
        actionMasked != AMOTION_EVENT_ACTION_DOWN &&
        actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN) {
        return false;
    }
    const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
    return motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
            motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
}

// --- InputReader ---

InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
@@ -101,8 +113,10 @@ status_t InputReader::stop() {
void InputReader::loopOnce() {
    int32_t oldGeneration;
    int32_t timeoutMillis;
    // Copy some state so that we can access it outside the lock later.
    bool inputDevicesChanged = false;
    std::vector<InputDeviceInfo> inputDevices;
    std::list<NotifyArgs> notifyArgs;
    { // acquire lock
        std::scoped_lock _l(mLock);

@@ -127,7 +141,7 @@ void InputReader::loopOnce() {
        mReaderIsAliveCondition.notify_all();

        if (!events.empty()) {
            notifyAll(processEventsLocked(events.data(), events.size()));
            notifyArgs += processEventsLocked(events.data(), events.size());
        }

        if (mNextTimeout != LLONG_MAX) {
@@ -137,7 +151,7 @@ void InputReader::loopOnce() {
                    ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
                }
                mNextTimeout = LLONG_MAX;
                notifyAll(timeoutExpiredLocked(now));
                notifyArgs += timeoutExpiredLocked(now);
            }
        }

@@ -152,6 +166,16 @@ void InputReader::loopOnce() {
        mPolicy->notifyInputDevicesChanged(inputDevices);
    }

    // Notify the policy of the start of every new stylus gesture outside the lock.
    for (const auto& args : notifyArgs) {
        const auto* motionArgs = std::get_if<NotifyMotionArgs>(&args);
        if (motionArgs != nullptr && isStylusPointerGestureStart(*motionArgs)) {
            mPolicy->notifyStylusGestureStarted(motionArgs->deviceId, motionArgs->eventTime);
        }
    }

    notifyAll(std::move(notifyArgs));

    // Flush queued events out to the listener.
    // This must happen outside of the lock because the listener could potentially call
    // back into the InputReader's methods, such as getScanCodeState, or become blocked
@@ -851,6 +875,16 @@ std::optional<int32_t> InputReader::getLightPlayerId(int32_t deviceId, int32_t l
    return std::nullopt;
}

std::optional<std::string> InputReader::getBluetoothAddress(int32_t deviceId) const {
    std::scoped_lock _l(mLock);

    InputDevice* device = findInputDeviceLocked(deviceId);
    if (device) {
        return device->getBluetoothAddress();
    }
    return std::nullopt;
}

bool InputReader::isInputDeviceEnabled(int32_t deviceId) {
    std::scoped_lock _l(mLock);

Loading