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(InputDevice* device);
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(InputDevice* device);
57     virtual ~CursorInputMapper();
58 
59     virtual uint32_t getSources();
60     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
61     virtual void dump(std::string& dump);
62     virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
63     virtual void reset(nsecs_t when);
64     virtual void process(const RawEvent* rawEvent);
65 
66     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
67 
68     virtual void fadePointer();
69 
70     virtual std::optional<int32_t> getAssociatedDisplay();
71 
72 private:
73     // Amount that trackball needs to move in order to generate a key event.
74     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
75 
76     // Immutable configuration parameters.
77     struct Parameters {
78         enum Mode {
79             MODE_POINTER,
80             MODE_POINTER_RELATIVE,
81             MODE_NAVIGATION,
82         };
83 
84         Mode mode;
85         bool hasAssociatedDisplay;
86         bool orientationAware;
87     } mParameters;
88 
89     CursorButtonAccumulator mCursorButtonAccumulator;
90     CursorMotionAccumulator mCursorMotionAccumulator;
91     CursorScrollAccumulator mCursorScrollAccumulator;
92 
93     int32_t mSource;
94     float mXScale;
95     float mYScale;
96     float mXPrecision;
97     float mYPrecision;
98 
99     float mVWheelScale;
100     float mHWheelScale;
101 
102     // Velocity controls for mouse pointer and wheel movements.
103     // The controls for X and Y wheel movements are separate to keep them decoupled.
104     VelocityControl mPointerVelocityControl;
105     VelocityControl mWheelXVelocityControl;
106     VelocityControl mWheelYVelocityControl;
107 
108     int32_t mOrientation;
109 
110     std::shared_ptr<PointerControllerInterface> mPointerController;
111 
112     int32_t mButtonState;
113     nsecs_t mDownTime;
114 
115     void configureParameters();
116     void dumpParameters(std::string& dump);
117 
118     void sync(nsecs_t when);
119     void updatePointerControllerDisplayViewport(const InputReaderConfiguration& config);
120 };
121 
122 } // namespace android
123 
124 #endif // _UI_INPUTREADER_CURSOR_INPUT_MAPPER_H
125