• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <array>
20 #include <list>
21 #include <memory>
22 
23 #include <PointerControllerInterface.h>
24 #include <android/input.h>
25 #include <utils/Timers.h>
26 
27 #include "EventHub.h"
28 #include "InputDevice.h"
29 #include "InputReaderContext.h"
30 #include "NotifyArgs.h"
31 #include "ui/Rotation.h"
32 
33 #include "include/gestures.h"
34 
35 namespace android {
36 
37 // Converts Gesture structs from the gestures library into NotifyArgs and the appropriate
38 // PointerController calls.
39 class GestureConverter {
40 public:
41     GestureConverter(InputReaderContext& readerContext, const InputDeviceContext& deviceContext,
42                      int32_t deviceId);
43 
44     std::string dump() const;
45 
setOrientation(ui::Rotation orientation)46     void setOrientation(ui::Rotation orientation) { mOrientation = orientation; }
47     [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when);
48 
49     void populateMotionRanges(InputDeviceInfo& info) const;
50 
51     [[nodiscard]] std::list<NotifyArgs> handleGesture(nsecs_t when, nsecs_t readTime,
52                                                       const Gesture& gesture);
53 
54 private:
55     [[nodiscard]] NotifyMotionArgs handleMove(nsecs_t when, nsecs_t readTime,
56                                               const Gesture& gesture);
57     [[nodiscard]] std::list<NotifyArgs> handleButtonsChange(nsecs_t when, nsecs_t readTime,
58                                                             const Gesture& gesture);
59     [[nodiscard]] std::list<NotifyArgs> releaseAllButtons(nsecs_t when, nsecs_t readTime);
60     [[nodiscard]] std::list<NotifyArgs> handleScroll(nsecs_t when, nsecs_t readTime,
61                                                      const Gesture& gesture);
62     [[nodiscard]] std::list<NotifyArgs> handleFling(nsecs_t when, nsecs_t readTime,
63                                                     const Gesture& gesture);
64     [[nodiscard]] NotifyMotionArgs endScroll(nsecs_t when, nsecs_t readTime);
65 
66     [[nodiscard]] std::list<NotifyArgs> handleMultiFingerSwipe(nsecs_t when, nsecs_t readTime,
67                                                                uint32_t fingerCount, float dx,
68                                                                float dy);
69     [[nodiscard]] std::list<NotifyArgs> handleMultiFingerSwipeLift(nsecs_t when, nsecs_t readTime);
70     [[nodiscard]] std::list<NotifyArgs> handlePinch(nsecs_t when, nsecs_t readTime,
71                                                     const Gesture& gesture);
72     [[nodiscard]] std::list<NotifyArgs> endPinch(nsecs_t when, nsecs_t readTime);
73 
74     NotifyMotionArgs makeMotionArgs(nsecs_t when, nsecs_t readTime, int32_t action,
75                                     int32_t actionButton, int32_t buttonState,
76                                     uint32_t pointerCount,
77                                     const PointerProperties* pointerProperties,
78                                     const PointerCoords* pointerCoords, float xCursorPosition,
79                                     float yCursorPosition);
80 
81     void enableTapToClick();
82 
83     const int32_t mDeviceId;
84     InputReaderContext& mReaderContext;
85     std::shared_ptr<PointerControllerInterface> mPointerController;
86 
87     ui::Rotation mOrientation = ui::ROTATION_0;
88     RawAbsoluteAxisInfo mXAxisInfo;
89     RawAbsoluteAxisInfo mYAxisInfo;
90 
91     // The current button state according to the gestures library, but converted into MotionEvent
92     // button values (AMOTION_EVENT_BUTTON_...).
93     uint32_t mButtonState = 0;
94     nsecs_t mDownTime = 0;
95 
96     MotionClassification mCurrentClassification = MotionClassification::NONE;
97     // Only used when mCurrentClassification is MULTI_FINGER_SWIPE.
98     uint32_t mSwipeFingerCount = 0;
99     static constexpr float INITIAL_PINCH_SEPARATION_PX = 200.0;
100     // Only used when mCurrentClassification is PINCH.
101     float mPinchFingerSeparation;
102     static constexpr size_t MAX_FAKE_FINGERS = 4;
103     // We never need any PointerProperties other than the finger tool type, so we can just keep a
104     // const array of them.
105     const std::array<PointerProperties, MAX_FAKE_FINGERS> mFingerProps = {{
106             {.id = 0, .toolType = ToolType::FINGER},
107             {.id = 1, .toolType = ToolType::FINGER},
108             {.id = 2, .toolType = ToolType::FINGER},
109             {.id = 3, .toolType = ToolType::FINGER},
110     }};
111     std::array<PointerCoords, MAX_FAKE_FINGERS> mFakeFingerCoords = {};
112 
113     // TODO(b/260226362): consider what the appropriate source for these events is.
114     static constexpr uint32_t SOURCE = AINPUT_SOURCE_MOUSE;
115 };
116 
117 } // namespace android
118