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() const 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 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 VelocityControl mPointerVelocityControl; 111 VelocityControl mWheelXVelocityControl; 112 VelocityControl mWheelYVelocityControl; 113 114 // The display that events generated by this mapper should target. This can be set to 115 // ADISPLAY_ID_NONE to target the focused display. If there is no display target (i.e. 116 // std::nullopt), all events will be ignored. 117 std::optional<int32_t> mDisplayId; 118 int32_t mOrientation; 119 120 std::shared_ptr<PointerControllerInterface> mPointerController; 121 122 int32_t mButtonState; 123 nsecs_t mDownTime; 124 125 void configureParameters(); 126 void dumpParameters(std::string& dump); 127 128 void sync(nsecs_t when, nsecs_t readTime); 129 }; 130 131 } // namespace android 132 133 #endif // _UI_INPUTREADER_CURSOR_INPUT_MAPPER_H 134