summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/mapper/gestures/GestureConverter.h
blob: 732a4b2ffb7096c4bf48a82d6e6676f2046535f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <array>
#include <list>
#include <memory>

#include <PointerControllerInterface.h>
#include <android/input.h>
#include <utils/Timers.h>

#include "EventHub.h"
#include "InputDevice.h"
#include "InputReaderContext.h"
#include "NotifyArgs.h"
#include "ui/Rotation.h"

#include "include/gestures.h"

namespace android {

// Converts Gesture structs from the gestures library into NotifyArgs and the appropriate
// PointerController calls.
class GestureConverter {
public:
    GestureConverter(InputReaderContext& readerContext, const InputDeviceContext& deviceContext,
                     int32_t deviceId);

    std::string dump() const;

    void setOrientation(ui::Rotation orientation) { mOrientation = orientation; }
    [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when);

    void setDisplayId(std::optional<int32_t> displayId) { mDisplayId = displayId; }

    void setBoundsInLogicalDisplay(FloatRect bounds) { mBoundsInLogicalDisplay = bounds; }

    void populateMotionRanges(InputDeviceInfo& info) const;

    [[nodiscard]] std::list<NotifyArgs> handleGesture(nsecs_t when, nsecs_t readTime,
                                                      const Gesture& gesture);

private:
    [[nodiscard]] NotifyMotionArgs handleMove(nsecs_t when, nsecs_t readTime,
                                              const Gesture& gesture);
    [[nodiscard]] std::list<NotifyArgs> handleButtonsChange(nsecs_t when, nsecs_t readTime,
                                                            const Gesture& gesture);
    [[nodiscard]] std::list<NotifyArgs> releaseAllButtons(nsecs_t when, nsecs_t readTime);
    [[nodiscard]] std::list<NotifyArgs> handleScroll(nsecs_t when, nsecs_t readTime,
                                                     const Gesture& gesture);
    [[nodiscard]] std::list<NotifyArgs> handleFling(nsecs_t when, nsecs_t readTime,
                                                    const Gesture& gesture);
    [[nodiscard]] NotifyMotionArgs endScroll(nsecs_t when, nsecs_t readTime);

    [[nodiscard]] std::list<NotifyArgs> handleMultiFingerSwipe(nsecs_t when, nsecs_t readTime,
                                                               uint32_t fingerCount, float dx,
                                                               float dy);
    [[nodiscard]] std::list<NotifyArgs> handleMultiFingerSwipeLift(nsecs_t when, nsecs_t readTime);
    [[nodiscard]] std::list<NotifyArgs> handlePinch(nsecs_t when, nsecs_t readTime,
                                                    const Gesture& gesture);
    [[nodiscard]] std::list<NotifyArgs> endPinch(nsecs_t when, nsecs_t readTime);

    NotifyMotionArgs makeMotionArgs(nsecs_t when, nsecs_t readTime, int32_t action,
                                    int32_t actionButton, int32_t buttonState,
                                    uint32_t pointerCount,
                                    const PointerProperties* pointerProperties,
                                    const PointerCoords* pointerCoords, float xCursorPosition,
                                    float yCursorPosition);

    void enableTapToClick();

    const int32_t mDeviceId;
    InputReaderContext& mReaderContext;
    std::shared_ptr<PointerControllerInterface> mPointerController;
    const bool mEnablePointerChoreographer;

    std::optional<int32_t> mDisplayId;
    FloatRect mBoundsInLogicalDisplay{};
    ui::Rotation mOrientation = ui::ROTATION_0;
    RawAbsoluteAxisInfo mXAxisInfo;
    RawAbsoluteAxisInfo mYAxisInfo;

    // The current button state according to the gestures library, but converted into MotionEvent
    // button values (AMOTION_EVENT_BUTTON_...).
    uint32_t mButtonState = 0;
    nsecs_t mDownTime = 0;

    MotionClassification mCurrentClassification = MotionClassification::NONE;
    // Only used when mCurrentClassification is MULTI_FINGER_SWIPE.
    uint32_t mSwipeFingerCount = 0;
    static constexpr float INITIAL_PINCH_SEPARATION_PX = 200.0;
    // Only used when mCurrentClassification is PINCH.
    float mPinchFingerSeparation;
    static constexpr size_t MAX_FAKE_FINGERS = 4;
    // We never need any PointerProperties other than the finger tool type, so we can just keep a
    // const array of them.
    const std::array<PointerProperties, MAX_FAKE_FINGERS> mFingerProps = {{
            {.id = 0, .toolType = ToolType::FINGER},
            {.id = 1, .toolType = ToolType::FINGER},
            {.id = 2, .toolType = ToolType::FINGER},
            {.id = 3, .toolType = ToolType::FINGER},
    }};
    std::array<PointerCoords, MAX_FAKE_FINGERS> mFakeFingerCoords = {};

    // TODO(b/260226362): consider what the appropriate source for these events is.
    static constexpr uint32_t SOURCE = AINPUT_SOURCE_MOUSE;
};

} // namespace android