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