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