1 /*
2  * Copyright (C) 2019 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 "CursorButtonAccumulator.h"
20 #include "CursorScrollAccumulator.h"
21 #include "InputMapper.h"
22 
23 #include <input/VelocityControl.h>
24 #include <ui/Rotation.h>
25 
26 namespace android {
27 
28 class CursorButtonAccumulator;
29 class CursorScrollAccumulator;
30 
31 /* Keeps track of cursor movements. */
32 class CursorMotionAccumulator {
33 public:
34     CursorMotionAccumulator();
35     void reset(InputDeviceContext& deviceContext);
36 
37     void process(const RawEvent& rawEvent);
38     void finishSync();
39 
getRelativeX()40     inline int32_t getRelativeX() const { return mRelX; }
getRelativeY()41     inline int32_t getRelativeY() const { return mRelY; }
42 
43 private:
44     int32_t mRelX;
45     int32_t mRelY;
46 
47     void clearRelativeAxes();
48 };
49 
50 class CursorInputMapper : public InputMapper {
51 public:
52     template <class T, class... Args>
53     friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
54                                                 const InputReaderConfiguration& readerConfig,
55                                                 Args... args);
56     virtual ~CursorInputMapper() = default;
57 
58     virtual uint32_t getSources() const override;
59     virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
60     virtual void dump(std::string& dump) override;
61     [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
62                                                     const InputReaderConfiguration& readerConfig,
63                                                     ConfigurationChanges changes) override;
64     [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
65     [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override;
66 
67     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
68 
69     virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override;
70 
71 private:
72     // Amount that trackball needs to move in order to generate a key event.
73     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
74 
75     // Immutable configuration parameters.
76     struct Parameters {
77         enum class Mode {
78             // In POINTER mode, the device is a mouse that controls the mouse cursor on the screen,
79             // reporting absolute screen locations using SOURCE_MOUSE.
80             POINTER,
81             // A mouse device in POINTER mode switches to the POINTER_RELATIVE mode when Pointer
82             // Capture is enabled, and reports relative values only using SOURCE_MOUSE_RELATIVE.
83             POINTER_RELATIVE,
84             // A device in NAVIGATION mode emits relative values using SOURCE_TRACKBALL.
85             NAVIGATION,
86 
87             ftl_last = NAVIGATION,
88         };
89 
90         Mode mode;
91         bool hasAssociatedDisplay;
92         bool orientationAware;
93     } mParameters;
94 
95     CursorButtonAccumulator mCursorButtonAccumulator;
96     CursorMotionAccumulator mCursorMotionAccumulator;
97     CursorScrollAccumulator mCursorScrollAccumulator;
98 
99     int32_t mSource;
100     float mXScale;
101     float mYScale;
102     float mXPrecision;
103     float mYPrecision;
104 
105     float mVWheelScale;
106     float mHWheelScale;
107 
108     // Velocity controls for mouse pointer and wheel movements.
109     // The controls for X and Y wheel movements are separate to keep them decoupled.
110     SimpleVelocityControl mOldPointerVelocityControl;
111     CurvedVelocityControl mNewPointerVelocityControl;
112     SimpleVelocityControl mWheelXVelocityControl;
113     SimpleVelocityControl mWheelYVelocityControl;
114 
115     // The display that events generated by this mapper should target. This can be set to
116     // LogicalDisplayId::INVALID to target the focused display. If there is no display target (i.e.
117     // std::nullopt), all events will be ignored.
118     std::optional<ui::LogicalDisplayId> mDisplayId;
119     ui::Rotation mOrientation{ui::ROTATION_0};
120     FloatRect mBoundsInLogicalDisplay{};
121 
122     int32_t mButtonState;
123     nsecs_t mDownTime;
124     nsecs_t mLastEventTime;
125 
126     const bool mEnableNewMousePointerBallistics;
127 
128     explicit CursorInputMapper(InputDeviceContext& deviceContext,
129                                const InputReaderConfiguration& readerConfig);
130     void dumpParameters(std::string& dump);
131     void configureBasicParams();
132     void configureOnPointerCapture(const InputReaderConfiguration& config);
133     void configureOnChangePointerSpeed(const InputReaderConfiguration& config);
134     void configureOnChangeDisplayInfo(const InputReaderConfiguration& config);
135 
136     [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
137 
138     static Parameters computeParameters(const InputDeviceContext& deviceContext);
139 };
140 
141 } // namespace android
142