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

Commit 4f05b5fb authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

TouchButtonAccumulator: Recognize mapped stylus buttons

Input devices can be configured to remap some keys using kl files.
Ensure that we use mapped key codes to determine the state of stylus
buttons.

Bug: 246394583
Test: atest inputflinger_tests

Change-Id: Iedf67063f3bf34eefe922342456f500d88580ed9
parent 84395e6e
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -378,8 +378,11 @@ public:
        mEventHub->getAbsoluteAxisInfo(mId, code, &info);
        return info.valid;
    }
    inline bool isKeyPressed(int32_t code) const {
        return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
    inline bool isKeyPressed(int32_t scanCode) const {
        return mEventHub->getScanCodeState(mId, scanCode) == AKEY_STATE_DOWN;
    }
    inline bool isKeyCodePressed(int32_t keyCode) const {
        return mEventHub->getKeyCodeState(mId, keyCode) == AKEY_STATE_DOWN;
    }
    inline int32_t getAbsoluteAxisValue(int32_t code) const {
        int32_t value;
+3 −3
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@
namespace android {

ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext)
      : InputMapper(deviceContext) {}
      : InputMapper(deviceContext), mTouchButtonAccumulator(deviceContext) {}

uint32_t ExternalStylusInputMapper::getSources() const {
    return AINPUT_SOURCE_STYLUS;
@@ -48,13 +48,13 @@ std::list<NotifyArgs> ExternalStylusInputMapper::configure(nsecs_t when,
                                                           const InputReaderConfiguration* config,
                                                           uint32_t changes) {
    getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
    mTouchButtonAccumulator.configure(getDeviceContext());
    mTouchButtonAccumulator.configure();
    return {};
}

std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) {
    mSingleTouchMotionAccumulator.reset(getDeviceContext());
    mTouchButtonAccumulator.reset(getDeviceContext());
    mTouchButtonAccumulator.reset();
    return InputMapper::reset(when);
}

+3 −2
Original line number Diff line number Diff line
@@ -170,6 +170,7 @@ void CookedPointerData::copyFrom(const CookedPointerData& other) {

TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
      : InputMapper(deviceContext),
        mTouchButtonAccumulator(deviceContext),
        mSource(0),
        mDeviceMode(DeviceMode::DISABLED),
        mDisplayWidth(-1),
@@ -360,7 +361,7 @@ std::list<NotifyArgs> TouchInputMapper::configure(nsecs_t when,

        // Configure common accumulators.
        mCursorScrollAccumulator.configure(getDeviceContext());
        mTouchButtonAccumulator.configure(getDeviceContext());
        mTouchButtonAccumulator.configure();

        // Configure absolute axis information.
        configureRawPointerAxes();
@@ -1449,7 +1450,7 @@ std::list<NotifyArgs> TouchInputMapper::reset(nsecs_t when) {

    mCursorButtonAccumulator.reset(getDeviceContext());
    mCursorScrollAccumulator.reset(getDeviceContext());
    mTouchButtonAccumulator.reset(getDeviceContext());
    mTouchButtonAccumulator.reset();

    mPointerVelocityControl.reset();
    mWheelXVelocityControl.reset();
+49 −22
Original line number Diff line number Diff line
@@ -21,34 +21,39 @@

namespace android {

void TouchButtonAccumulator::configure(InputDeviceContext& deviceContext) {
    mHaveBtnTouch = deviceContext.hasScanCode(BTN_TOUCH);
    mHaveStylus = deviceContext.hasScanCode(BTN_TOOL_PEN) ||
            deviceContext.hasScanCode(BTN_TOOL_RUBBER) ||
            deviceContext.hasScanCode(BTN_TOOL_BRUSH) ||
            deviceContext.hasScanCode(BTN_TOOL_PENCIL) ||
            deviceContext.hasScanCode(BTN_TOOL_AIRBRUSH);
void TouchButtonAccumulator::configure() {
    mHaveBtnTouch = mDeviceContext.hasScanCode(BTN_TOUCH);
    mHaveStylus = mDeviceContext.hasScanCode(BTN_TOOL_PEN) ||
            mDeviceContext.hasScanCode(BTN_TOOL_RUBBER) ||
            mDeviceContext.hasScanCode(BTN_TOOL_BRUSH) ||
            mDeviceContext.hasScanCode(BTN_TOOL_PENCIL) ||
            mDeviceContext.hasScanCode(BTN_TOOL_AIRBRUSH);
}

void TouchButtonAccumulator::reset(InputDeviceContext& deviceContext) {
    mBtnTouch = deviceContext.isKeyPressed(BTN_TOUCH);
    mBtnStylus = deviceContext.isKeyPressed(BTN_STYLUS);
void TouchButtonAccumulator::reset() {
    mBtnTouch = mDeviceContext.isKeyPressed(BTN_TOUCH);
    mBtnStylus = mDeviceContext.isKeyPressed(BTN_STYLUS) ||
            mDeviceContext.isKeyCodePressed(AKEYCODE_STYLUS_BUTTON_PRIMARY);
    // BTN_0 is what gets mapped for the HID usage Digitizers.SecondaryBarrelSwitch
    mBtnStylus2 = deviceContext.isKeyPressed(BTN_STYLUS2) || deviceContext.isKeyPressed(BTN_0);
    mBtnToolFinger = deviceContext.isKeyPressed(BTN_TOOL_FINGER);
    mBtnToolPen = deviceContext.isKeyPressed(BTN_TOOL_PEN);
    mBtnToolRubber = deviceContext.isKeyPressed(BTN_TOOL_RUBBER);
    mBtnToolBrush = deviceContext.isKeyPressed(BTN_TOOL_BRUSH);
    mBtnToolPencil = deviceContext.isKeyPressed(BTN_TOOL_PENCIL);
    mBtnToolAirbrush = deviceContext.isKeyPressed(BTN_TOOL_AIRBRUSH);
    mBtnToolMouse = deviceContext.isKeyPressed(BTN_TOOL_MOUSE);
    mBtnToolLens = deviceContext.isKeyPressed(BTN_TOOL_LENS);
    mBtnToolDoubleTap = deviceContext.isKeyPressed(BTN_TOOL_DOUBLETAP);
    mBtnToolTripleTap = deviceContext.isKeyPressed(BTN_TOOL_TRIPLETAP);
    mBtnToolQuadTap = deviceContext.isKeyPressed(BTN_TOOL_QUADTAP);
    mBtnStylus2 = mDeviceContext.isKeyPressed(BTN_STYLUS2) || mDeviceContext.isKeyPressed(BTN_0) ||
            mDeviceContext.isKeyCodePressed(AKEYCODE_STYLUS_BUTTON_SECONDARY);
    mBtnToolFinger = mDeviceContext.isKeyPressed(BTN_TOOL_FINGER);
    mBtnToolPen = mDeviceContext.isKeyPressed(BTN_TOOL_PEN);
    mBtnToolRubber = mDeviceContext.isKeyPressed(BTN_TOOL_RUBBER);
    mBtnToolBrush = mDeviceContext.isKeyPressed(BTN_TOOL_BRUSH);
    mBtnToolPencil = mDeviceContext.isKeyPressed(BTN_TOOL_PENCIL);
    mBtnToolAirbrush = mDeviceContext.isKeyPressed(BTN_TOOL_AIRBRUSH);
    mBtnToolMouse = mDeviceContext.isKeyPressed(BTN_TOOL_MOUSE);
    mBtnToolLens = mDeviceContext.isKeyPressed(BTN_TOOL_LENS);
    mBtnToolDoubleTap = mDeviceContext.isKeyPressed(BTN_TOOL_DOUBLETAP);
    mBtnToolTripleTap = mDeviceContext.isKeyPressed(BTN_TOOL_TRIPLETAP);
    mBtnToolQuadTap = mDeviceContext.isKeyPressed(BTN_TOOL_QUADTAP);
    mHidUsageAccumulator.reset();
}

void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
    mHidUsageAccumulator.process(*rawEvent);

    if (rawEvent->type == EV_KEY) {
        switch (rawEvent->code) {
            case BTN_TOUCH:
@@ -95,8 +100,30 @@ void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
            case BTN_TOOL_QUADTAP:
                mBtnToolQuadTap = rawEvent->value;
                break;
            default:
                processMappedKey(rawEvent->code, rawEvent->value);
        }
        return;
    }
}

void TouchButtonAccumulator::processMappedKey(int32_t scanCode, bool down) {
    int32_t outKeyCode, outMetaState;
    uint32_t outFlags;
    if (mDeviceContext.mapKey(scanCode, mHidUsageAccumulator.consumeCurrentHidUsage(),
                              0 /*metaState*/, &outKeyCode, &outMetaState, &outFlags) != OK) {
        return;
    }
    switch (outKeyCode) {
        case AKEYCODE_STYLUS_BUTTON_PRIMARY:
            mBtnStylus = down;
            break;
        case AKEYCODE_STYLUS_BUTTON_SECONDARY:
            mBtnStylus2 = down;
            break;
        default:
            break;
    }
}

uint32_t TouchButtonAccumulator::getButtonState() const {
+12 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#pragma once

#include <cstdint>
#include "HidUsageAccumulator.h"

namespace android {

@@ -26,9 +27,11 @@ struct RawEvent;
/* Keeps track of the state of touch, stylus and tool buttons. */
class TouchButtonAccumulator {
public:
    TouchButtonAccumulator() = default;
    void configure(InputDeviceContext& deviceContext);
    void reset(InputDeviceContext& deviceContext);
    explicit TouchButtonAccumulator(InputDeviceContext& deviceContext)
          : mDeviceContext(deviceContext){};

    void configure();
    void reset();

    void process(const RawEvent* rawEvent);

@@ -56,6 +59,12 @@ private:
    bool mBtnToolDoubleTap{};
    bool mBtnToolTripleTap{};
    bool mBtnToolQuadTap{};

    HidUsageAccumulator mHidUsageAccumulator{};

    InputDeviceContext& mDeviceContext;

    void processMappedKey(int32_t scanCode, bool down);
};

} // namespace android
Loading