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

Commit 2197cb4c authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

KeyboardInputMapper: Miscelaneous code cleanup

Initialize all members at construction, make some functions static,
remove some unused parameters, and use an optional return type for
clarity.

There should be no behavior changes in this CL.

Bug: 245989146
Test: atest inputflinger_tests
Change-Id: Ia57d1c88f0d60938a5e803f51d99c335e7fcf459
parent c4e0c65f
Loading
Loading
Loading
Loading
+80 −90
Original line number Diff line number Diff line
@@ -24,19 +24,8 @@ namespace android {

// --- Static Definitions ---

static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
                                           const int32_t map[][4], size_t mapSize) {
    if (orientation != DISPLAY_ORIENTATION_0) {
        for (size_t i = 0; i < mapSize; i++) {
            if (value == map[i][0]) {
                return map[i][orientation];
            }
        }
    }
    return value;
}

static const int32_t keyCodeRotationMap[][4] = {
static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
    static constexpr int32_t KEYCODE_ROTATION_MAP[][4] = {
            // key codes enumerated counter-clockwise with the original (unrotated) key first
            // no rotation,        90 degree rotation,  180 degree rotation, 270 degree rotation
            {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
@@ -53,12 +42,51 @@ static const int32_t keyCodeRotationMap[][4] = {
             AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
    };

static const size_t keyCodeRotationMapSize =
        sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
    LOG_ALWAYS_FATAL_IF(orientation < 0 || orientation > 3, "Invalid orientation: %d", orientation);
    if (orientation != DISPLAY_ORIENTATION_0) {
        for (const auto& rotation : KEYCODE_ROTATION_MAP) {
            if (rotation[DISPLAY_ORIENTATION_0] == keyCode) {
                return rotation[orientation];
            }
        }
    }
    return keyCode;
}

static bool isKeyboardOrGamepadKey(int32_t scanCode) {
    return scanCode < BTN_MOUSE || scanCode >= BTN_WHEEL ||
            (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) ||
            (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
}

static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
    return rotateValueUsingRotationMap(keyCode, orientation, keyCodeRotationMap,
                                       keyCodeRotationMapSize);
static bool isMediaKey(int32_t keyCode) {
    switch (keyCode) {
        case AKEYCODE_MEDIA_PLAY:
        case AKEYCODE_MEDIA_PAUSE:
        case AKEYCODE_MEDIA_PLAY_PAUSE:
        case AKEYCODE_MUTE:
        case AKEYCODE_HEADSETHOOK:
        case AKEYCODE_MEDIA_STOP:
        case AKEYCODE_MEDIA_NEXT:
        case AKEYCODE_MEDIA_PREVIOUS:
        case AKEYCODE_MEDIA_REWIND:
        case AKEYCODE_MEDIA_RECORD:
        case AKEYCODE_MEDIA_FAST_FORWARD:
        case AKEYCODE_MEDIA_SKIP_FORWARD:
        case AKEYCODE_MEDIA_SKIP_BACKWARD:
        case AKEYCODE_MEDIA_STEP_FORWARD:
        case AKEYCODE_MEDIA_STEP_BACKWARD:
        case AKEYCODE_MEDIA_AUDIO_TRACK:
        case AKEYCODE_VOLUME_UP:
        case AKEYCODE_VOLUME_DOWN:
        case AKEYCODE_VOLUME_MUTE:
        case AKEYCODE_TV_AUDIO_DESCRIPTION:
        case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
        case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
            return true;
        default:
            return false;
    }
}

// --- KeyboardInputMapper ---
@@ -67,8 +95,6 @@ KeyboardInputMapper::KeyboardInputMapper(InputDeviceContext& deviceContext, uint
                                         int32_t keyboardType)
      : InputMapper(deviceContext), mSource(source), mKeyboardType(keyboardType) {}

KeyboardInputMapper::~KeyboardInputMapper() {}

uint32_t KeyboardInputMapper::getSources() const {
    return mSource;
}
@@ -104,7 +130,7 @@ void KeyboardInputMapper::dump(std::string& dump) {
}

std::optional<DisplayViewport> KeyboardInputMapper::findViewport(
        nsecs_t when, const InputReaderConfiguration* config) {
        const InputReaderConfiguration* config) {
    if (getDeviceContext().getAssociatedViewport()) {
        return getDeviceContext().getAssociatedViewport();
    }
@@ -128,7 +154,7 @@ std::list<NotifyArgs> KeyboardInputMapper::configure(nsecs_t when,
    }

    if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
        mViewport = findViewport(when, config);
        mViewport = findViewport(config);
    }
    return out;
}
@@ -145,7 +171,7 @@ void KeyboardInputMapper::configureParameters() {
    config.tryGetProperty("keyboard.doNotWakeByDefault", mParameters.doNotWakeByDefault);
}

void KeyboardInputMapper::dumpParameters(std::string& dump) {
void KeyboardInputMapper::dumpParameters(std::string& dump) const {
    dump += INDENT3 "Parameters:\n";
    dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware));
    dump += StringPrintf(INDENT4 "HandlesKeyRepeat: %s\n", toString(mParameters.handlesKeyRepeat));
@@ -190,41 +216,6 @@ std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent* rawEvent) {
    return out;
}

bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
    return scanCode < BTN_MOUSE || scanCode >= BTN_WHEEL ||
            (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) ||
            (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
}

bool KeyboardInputMapper::isMediaKey(int32_t keyCode) {
    switch (keyCode) {
        case AKEYCODE_MEDIA_PLAY:
        case AKEYCODE_MEDIA_PAUSE:
        case AKEYCODE_MEDIA_PLAY_PAUSE:
        case AKEYCODE_MUTE:
        case AKEYCODE_HEADSETHOOK:
        case AKEYCODE_MEDIA_STOP:
        case AKEYCODE_MEDIA_NEXT:
        case AKEYCODE_MEDIA_PREVIOUS:
        case AKEYCODE_MEDIA_REWIND:
        case AKEYCODE_MEDIA_RECORD:
        case AKEYCODE_MEDIA_FAST_FORWARD:
        case AKEYCODE_MEDIA_SKIP_FORWARD:
        case AKEYCODE_MEDIA_SKIP_BACKWARD:
        case AKEYCODE_MEDIA_STEP_FORWARD:
        case AKEYCODE_MEDIA_STEP_BACKWARD:
        case AKEYCODE_MEDIA_AUDIO_TRACK:
        case AKEYCODE_VOLUME_UP:
        case AKEYCODE_VOLUME_DOWN:
        case AKEYCODE_VOLUME_MUTE:
        case AKEYCODE_TV_AUDIO_DESCRIPTION:
        case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP:
        case AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN:
            return true;
    }
    return false;
}

std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t readTime, bool down,
                                                      int32_t scanCode, int32_t usageCode) {
    std::list<NotifyArgs> out;
@@ -240,6 +231,7 @@ std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t read
    }

    nsecs_t downTime = when;
    std::optional<size_t> keyDownIndex = findKeyDownIndex(scanCode);
    if (down) {
        // Rotate key codes according to orientation if needed.
        if (mParameters.orientationAware) {
@@ -247,11 +239,10 @@ std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t read
        }

        // Add key down.
        ssize_t keyDownIndex = findKeyDown(scanCode);
        if (keyDownIndex >= 0) {
        if (keyDownIndex) {
            // key repeat, be sure to use same keycode as before in case of rotation
            keyCode = mKeyDowns[keyDownIndex].keyCode;
            downTime = mKeyDowns[keyDownIndex].downTime;
            keyCode = mKeyDowns[*keyDownIndex].keyCode;
            downTime = mKeyDowns[*keyDownIndex].downTime;
        } else {
            // key down
            if ((policyFlags & POLICY_FLAG_VIRTUAL) &&
@@ -270,12 +261,11 @@ std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t read
        }
    } else {
        // Remove key down.
        ssize_t keyDownIndex = findKeyDown(scanCode);
        if (keyDownIndex >= 0) {
        if (keyDownIndex) {
            // key up, be sure to use same keycode as before in case of rotation
            keyCode = mKeyDowns[keyDownIndex].keyCode;
            downTime = mKeyDowns[keyDownIndex].downTime;
            mKeyDowns.erase(mKeyDowns.begin() + (size_t)keyDownIndex);
            keyCode = mKeyDowns[*keyDownIndex].keyCode;
            downTime = mKeyDowns[*keyDownIndex].downTime;
            mKeyDowns.erase(mKeyDowns.begin() + *keyDownIndex);
        } else {
            // key was not actually down
            ALOGI("Dropping key up from device %s because the key was not down.  "
@@ -308,22 +298,22 @@ std::list<NotifyArgs> KeyboardInputMapper::processKey(nsecs_t when, nsecs_t read
        policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
    }

    out.push_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
                                getDisplayId(), policyFlags,
    out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
                                   mSource, getDisplayId(), policyFlags,
                                   down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
                                   AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, keyMetaState,
                                   downTime));
    return out;
}

ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
std::optional<size_t> KeyboardInputMapper::findKeyDownIndex(int32_t scanCode) {
    size_t n = mKeyDowns.size();
    for (size_t i = 0; i < n; i++) {
        if (mKeyDowns[i].scanCode == scanCode) {
            return i;
        }
    }
    return -1;
    return {};
}

int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
@@ -436,7 +426,7 @@ std::list<NotifyArgs> KeyboardInputMapper::cancelAllDownKeys(nsecs_t when) {
    std::list<NotifyArgs> out;
    size_t n = mKeyDowns.size();
    for (size_t i = 0; i < n; i++) {
        out.push_back(NotifyKeyArgs(getContext()->getNextId(), when,
        out.emplace_back(NotifyKeyArgs(getContext()->getNextId(), when,
                                       systemTime(SYSTEM_TIME_MONOTONIC), getDeviceId(), mSource,
                                       getDisplayId(), 0 /*policyFlags*/, AKEY_EVENT_ACTION_UP,
                                       AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED,
+22 −26
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ namespace android {
class KeyboardInputMapper : public InputMapper {
public:
    KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, int32_t keyboardType);
    virtual ~KeyboardInputMapper();
    ~KeyboardInputMapper() override = default;

    uint32_t getSources() const override;
    void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
@@ -47,58 +47,54 @@ public:

private:
    // The current viewport.
    std::optional<DisplayViewport> mViewport;
    std::optional<DisplayViewport> mViewport{};

    struct KeyDown {
        nsecs_t downTime;
        int32_t keyCode;
        int32_t scanCode;
        nsecs_t downTime{};
        int32_t keyCode{};
        int32_t scanCode{};
    };

    uint32_t mSource;
    int32_t mKeyboardType;
    uint32_t mSource{};
    int32_t mKeyboardType{};

    std::vector<KeyDown> mKeyDowns; // keys that are down
    int32_t mMetaState;
    std::vector<KeyDown> mKeyDowns{}; // keys that are down
    int32_t mMetaState{};

    int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
    int32_t mCurrentHidUsage{}; // most recent HID usage seen this packet, or 0 if none

    struct LedState {
        bool avail; // led is available
        bool on;    // we think the led is currently on
        bool avail{}; // led is available
        bool on{};    // we think the led is currently on
    };
    LedState mCapsLockLedState;
    LedState mNumLockLedState;
    LedState mScrollLockLedState;
    LedState mCapsLockLedState{};
    LedState mNumLockLedState{};
    LedState mScrollLockLedState{};

    // Immutable configuration parameters.
    struct Parameters {
        bool orientationAware;
        bool handlesKeyRepeat;
        bool doNotWakeByDefault;
    } mParameters;
        bool orientationAware{};
        bool handlesKeyRepeat{};
        bool doNotWakeByDefault{};
    } mParameters{};

    void configureParameters();
    void dumpParameters(std::string& dump);
    void dumpParameters(std::string& dump) const;

    int32_t getOrientation();
    int32_t getDisplayId();

    bool isKeyboardOrGamepadKey(int32_t scanCode);
    bool isMediaKey(int32_t keyCode);

    [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
                                                   int32_t scanCode, int32_t usageCode);

    bool updateMetaStateIfNeeded(int32_t keyCode, bool down);

    ssize_t findKeyDown(int32_t scanCode);
    std::optional<size_t> findKeyDownIndex(int32_t scanCode);

    void resetLedState();
    void initializeLedState(LedState& ledState, int32_t led);
    void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
    std::optional<DisplayViewport> findViewport(nsecs_t when,
                                                const InputReaderConfiguration* config);
    std::optional<DisplayViewport> findViewport(const InputReaderConfiguration* config);
    [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
};