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

Commit 18a332d2 authored by nadlabak's avatar nadlabak Committed by Bruno Martins
Browse files

Forward port 'Swap volume buttons' (2/3)

[mikeioannina]: Adjust for 5.0 changes
[LuK1337]: Adjust for 8.1 changes
[mikeioannina]: Adjust for 10.0 changes
[LuK1337]: Adjust for 11.0 changes

Change-Id: I5ed4ae2b7e69e2ada067ed1d3524b3d3fad30e2e
parent e6fad482
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -152,6 +152,9 @@ struct InputReaderConfiguration {
        // The set of disabled input devices (disabledDevices) has changed.
        CHANGE_ENABLED_STATE = 1 << 9,

        // Volume keys rotation option changed.
        CHANGE_VOLUME_KEYS_ROTATION = 1 << 10,

        // All devices must be reopened.
        CHANGE_MUST_REOPEN = 1 << 31,
    };
@@ -252,6 +255,10 @@ struct InputReaderConfiguration {
    // The set of currently disabled input devices.
    std::set<int32_t> disabledDevices;

    // Remap volume keys according to display rotation
    // 0 - disabled, 1 - phone or hybrid rotation mode, 2 - tablet rotation mode
    int volumeKeysRotationMode;

    InputReaderConfiguration() :
            virtualKeyQuietTime(0),
            pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
@@ -268,7 +275,8 @@ struct InputReaderConfiguration {
            pointerGestureSwipeMaxWidthRatio(0.25f),
            pointerGestureMovementSpeedRatio(0.8f),
            pointerGestureZoomSpeedRatio(0.3f),
            showTouches(false), pointerCapture(false) { }
            showTouches(false), pointerCapture(false),
            volumeKeysRotationMode(0) { }

    static std::string changesToString(uint32_t changes);

+24 −6
Original line number Diff line number Diff line
@@ -23,9 +23,10 @@ namespace android {
// --- Static Definitions ---

static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
                                           const int32_t map[][4], size_t mapSize) {
                                           const int32_t map[][4], size_t mapSize,
                                           int32_t rotationMapOffset) {
    if (orientation != DISPLAY_ORIENTATION_0) {
        for (size_t i = 0; i < mapSize; i++) {
        for (size_t i = rotationMapOffset; i < mapSize; i++) {
            if (value == map[i][0]) {
                return map[i][orientation];
            }
@@ -37,6 +38,16 @@ static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
static const int32_t keyCodeRotationMap[][4] = {
        // key codes enumerated counter-clockwise with the original (unrotated) key first
        // no rotation,        90 degree rotation,  180 degree rotation, 270 degree rotation

        // volume keys - tablet
        {AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN},
        {AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP},

        // volume keys - phone or hybrid
        {AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP},
        {AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN},

        // dpad keys - common
        {AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT},
        {AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN},
        {AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT},
@@ -79,10 +90,10 @@ static int32_t stemKeyRotationMap[][2] = {
static const size_t stemKeyRotationMapSize =
        sizeof(stemKeyRotationMap) / sizeof(stemKeyRotationMap[0]);

static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation, int32_t rotationMapOffset) {
    keyCode = rotateStemKey(keyCode, orientation, stemKeyRotationMap, stemKeyRotationMapSize);
    return rotateValueUsingRotationMap(keyCode, orientation, keyCodeRotationMap,
                                       keyCodeRotationMapSize);
                                       keyCodeRotationMapSize, rotationMapOffset);
}

// --- KeyboardInputMapper ---
@@ -156,6 +167,13 @@ void KeyboardInputMapper::configure(nsecs_t when, const InputReaderConfiguration
    if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
        mViewport = findViewport(when, config);
    }

    if (!changes || (changes & InputReaderConfiguration::CHANGE_VOLUME_KEYS_ROTATION)) {
        // mode 0 (disabled) ~ offset 4
        // mode 1 (phone) ~ offset 2
        // mode 2 (tablet) ~ offset 0
        mRotationMapOffset = 4 - 2 * config->volumeKeysRotationMode;
    }
}

static void mapStemKey(int32_t keyCode, const PropertyMap& config, char const* property) {
@@ -171,7 +189,7 @@ static void mapStemKey(int32_t keyCode, const PropertyMap& config, char const* p
}

void KeyboardInputMapper::configureParameters() {
    mParameters.orientationAware = false;
    mParameters.orientationAware = !getDeviceContext().isExternal();
    const PropertyMap& config = getDeviceContext().getConfiguration();
    config.tryGetProperty(String8("keyboard.orientationAware"), mParameters.orientationAware);

@@ -282,7 +300,7 @@ void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t scanCode,
    if (down) {
        // Rotate key codes according to orientation if needed.
        if (mParameters.orientationAware) {
            keyCode = rotateKeyCode(keyCode, getOrientation());
            keyCode = rotateKeyCode(keyCode, getOrientation(), mRotationMapOffset);
        }

        // Add key down.
+2 −0
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ private:
    uint32_t mSource;
    int32_t mKeyboardType;

    int32_t mRotationMapOffset; // determines if and how volume keys rotate

    std::vector<KeyDown> mKeyDowns; // keys that are down
    int32_t mMetaState;
    nsecs_t mDownTime; // time of most recent key down