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 #ifndef _UI_INPUTREADER_CURSOR_INPUT_MAPPER_H
18 #define _UI_INPUTREADER_CURSOR_INPUT_MAPPER_H
19 
20 #include "CursorButtonAccumulator.h"
21 #include "CursorScrollAccumulator.h"
22 #include "InputMapper.h"
23 
24 #include <PointerControllerInterface.h>
25 #include <input/VelocityControl.h>
26 
27 namespace android {
28 
29 class VelocityControl;
30 class PointerControllerInterface;
31 
32 class CursorButtonAccumulator;
33 class CursorScrollAccumulator;
34 
35 /* Keeps track of cursor movements. */
36 class CursorMotionAccumulator {
37 public:
38     CursorMotionAccumulator();
39     void reset(InputDeviceContext& deviceContext);
40 
41     void process(const RawEvent* rawEvent);
42     void finishSync();
43 
getRelativeX()44     inline int32_t getRelativeX() const { return mRelX; }
getRelativeY()45     inline int32_t getRelativeY() const { return mRelY; }
46 
47 private:
48     int32_t mRelX;
49     int32_t mRelY;
50 
51     void clearRelativeAxes();
52 };
53 
54 class CursorInputMapper : public InputMapper {
55 public:
56     explicit CursorInputMapper(InputDeviceContext& deviceContext);
57     virtual ~CursorInputMapper();
58 
59     virtual uint32_t getSources() override;
60     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
61     virtual void dump(std::string& dump) override;
62     virtual void configure(nsecs_t when, const InputReaderConfiguration* config,
63                            uint32_t changes) override;
64     virtual void reset(nsecs_t when) override;
65     virtual void process(const RawEvent* rawEvent) override;
66 
67     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
68 
69     virtual std::optional<int32_t> 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 Mode {
78             MODE_POINTER,
79             MODE_POINTER_RELATIVE,
80             MODE_NAVIGATION,
81         };
82 
83         Mode mode;
84         bool hasAssociatedDisplay;
85         bool orientationAware;
86     } mParameters;
87 
88     CursorButtonAccumulator mCursorButtonAccumulator;
89     CursorMotionAccumulator mCursorMotionAccumulator;
90     CursorScrollAccumulator mCursorScrollAccumulator;
91 
92     int32_t mSource;
93     float mXScale;
94     float mYScale;
95     float mXPrecision;
96     float mYPrecision;
97 
98     float mVWheelScale;
99     float mHWheelScale;
100 
101     // Velocity controls for mouse pointer and wheel movements.
102     // The controls for X and Y wheel movements are separate to keep them decoupled.
103     VelocityControl mPointerVelocityControl;
104     VelocityControl mWheelXVelocityControl;
105     VelocityControl mWheelYVelocityControl;
106 
107     int32_t mOrientation;
108 
109     sp<PointerControllerInterface> mPointerController;
110 
111     int32_t mButtonState;
112     nsecs_t mDownTime;
113 
114     void configureParameters();
115     void dumpParameters(std::string& dump);
116 
117     void sync(nsecs_t when);
118 };
119 
120 } // namespace android
121 
122 #endif // _UI_INPUTREADER_CURSOR_INPUT_MAPPER_H
123