1 /* 2 * Copyright (C) 2018 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_INPUT_READER_BASE_H 18 #define _UI_INPUT_READER_BASE_H 19 20 #include "PointerControllerInterface.h" 21 22 #include <input/Input.h> 23 #include <input/InputDevice.h> 24 #include <input/DisplayViewport.h> 25 #include <input/VelocityControl.h> 26 #include <input/VelocityTracker.h> 27 #include <utils/KeyedVector.h> 28 #include <utils/Thread.h> 29 #include <utils/RefBase.h> 30 #include <utils/SortedVector.h> 31 32 #include <optional> 33 #include <stddef.h> 34 #include <unistd.h> 35 #include <unordered_map> 36 #include <vector> 37 38 // Maximum supported size of a vibration pattern. 39 // Must be at least 2. 40 #define MAX_VIBRATE_PATTERN_SIZE 100 41 42 // Maximum allowable delay value in a vibration pattern before 43 // which the delay will be truncated. 44 #define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL) 45 46 namespace android { 47 48 /* Processes raw input events and sends cooked event data to an input listener. */ 49 class InputReaderInterface : public virtual RefBase { 50 protected: InputReaderInterface()51 InputReaderInterface() { } ~InputReaderInterface()52 virtual ~InputReaderInterface() { } 53 54 public: 55 /* Dumps the state of the input reader. 56 * 57 * This method may be called on any thread (usually by the input manager). */ 58 virtual void dump(std::string& dump) = 0; 59 60 /* Called by the heatbeat to ensures that the reader has not deadlocked. */ 61 virtual void monitor() = 0; 62 63 /* Returns true if the input device is enabled. */ 64 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0; 65 66 /* Runs a single iteration of the processing loop. 67 * Nominally reads and processes one incoming message from the EventHub. 68 * 69 * This method should be called on the input reader thread. 70 */ 71 virtual void loopOnce() = 0; 72 73 /* Gets information about all input devices. 74 * 75 * This method may be called on any thread (usually by the input manager). 76 */ 77 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) = 0; 78 79 /* Query current input state. */ 80 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, 81 int32_t scanCode) = 0; 82 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, 83 int32_t keyCode) = 0; 84 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, 85 int32_t sw) = 0; 86 87 /* Toggle Caps Lock */ 88 virtual void toggleCapsLockState(int32_t deviceId) = 0; 89 90 /* Determine whether physical keys exist for the given framework-domain key codes. */ 91 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 92 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0; 93 94 /* Requests that a reconfiguration of all input devices. 95 * The changes flag is a bitfield that indicates what has changed and whether 96 * the input devices must all be reopened. */ 97 virtual void requestRefreshConfiguration(uint32_t changes) = 0; 98 99 /* Controls the vibrator of a particular input device. */ 100 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, 101 ssize_t repeat, int32_t token) = 0; 102 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0; 103 104 /* Return true if the device can send input events to the specified display. */ 105 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) = 0; 106 }; 107 108 /* Reads raw events from the event hub and processes them, endlessly. */ 109 class InputReaderThread : public Thread { 110 public: 111 explicit InputReaderThread(const sp<InputReaderInterface>& reader); 112 virtual ~InputReaderThread(); 113 114 private: 115 sp<InputReaderInterface> mReader; 116 117 virtual bool threadLoop(); 118 }; 119 120 /* 121 * Input reader configuration. 122 * 123 * Specifies various options that modify the behavior of the input reader. 124 */ 125 struct InputReaderConfiguration { 126 // Describes changes that have occurred. 127 enum { 128 // The pointer speed changed. 129 CHANGE_POINTER_SPEED = 1 << 0, 130 131 // The pointer gesture control changed. 132 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1, 133 134 // The display size or orientation changed. 135 CHANGE_DISPLAY_INFO = 1 << 2, 136 137 // The visible touches option changed. 138 CHANGE_SHOW_TOUCHES = 1 << 3, 139 140 // The keyboard layouts must be reloaded. 141 CHANGE_KEYBOARD_LAYOUTS = 1 << 4, 142 143 // The device name alias supplied by the may have changed for some devices. 144 CHANGE_DEVICE_ALIAS = 1 << 5, 145 146 // The location calibration matrix changed. 147 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6, 148 149 // The presence of an external stylus has changed. 150 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7, 151 152 // The pointer capture mode has changed. 153 CHANGE_POINTER_CAPTURE = 1 << 8, 154 155 // The set of disabled input devices (disabledDevices) has changed. 156 CHANGE_ENABLED_STATE = 1 << 9, 157 158 // All devices must be reopened. 159 CHANGE_MUST_REOPEN = 1 << 31, 160 }; 161 162 // Gets the amount of time to disable virtual keys after the screen is touched 163 // in order to filter out accidental virtual key presses due to swiping gestures 164 // or taps near the edge of the display. May be 0 to disable the feature. 165 nsecs_t virtualKeyQuietTime; 166 167 // The excluded device names for the platform. 168 // Devices with these names will be ignored. 169 std::vector<std::string> excludedDeviceNames; 170 171 // The associations between input ports and display ports. 172 // Used to determine which DisplayViewport should be tied to which InputDevice. 173 std::unordered_map<std::string, uint8_t> portAssociations; 174 175 // The suggested display ID to show the cursor. 176 int32_t defaultPointerDisplayId; 177 178 // Velocity control parameters for mouse pointer movements. 179 VelocityControlParameters pointerVelocityControlParameters; 180 181 // Velocity control parameters for mouse wheel movements. 182 VelocityControlParameters wheelVelocityControlParameters; 183 184 // True if pointer gestures are enabled. 185 bool pointerGesturesEnabled; 186 187 // Quiet time between certain pointer gesture transitions. 188 // Time to allow for all fingers or buttons to settle into a stable state before 189 // starting a new gesture. 190 nsecs_t pointerGestureQuietInterval; 191 192 // The minimum speed that a pointer must travel for us to consider switching the active 193 // touch pointer to it during a drag. This threshold is set to avoid switching due 194 // to noise from a finger resting on the touch pad (perhaps just pressing it down). 195 float pointerGestureDragMinSwitchSpeed; // in pixels per second 196 197 // Tap gesture delay time. 198 // The time between down and up must be less than this to be considered a tap. 199 nsecs_t pointerGestureTapInterval; 200 201 // Tap drag gesture delay time. 202 // The time between the previous tap's up and the next down must be less than 203 // this to be considered a drag. Otherwise, the previous tap is finished and a 204 // new tap begins. 205 // 206 // Note that the previous tap will be held down for this entire duration so this 207 // interval must be shorter than the long press timeout. 208 nsecs_t pointerGestureTapDragInterval; 209 210 // The distance in pixels that the pointer is allowed to move from initial down 211 // to up and still be called a tap. 212 float pointerGestureTapSlop; // in pixels 213 214 // Time after the first touch points go down to settle on an initial centroid. 215 // This is intended to be enough time to handle cases where the user puts down two 216 // fingers at almost but not quite exactly the same time. 217 nsecs_t pointerGestureMultitouchSettleInterval; 218 219 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when 220 // at least two pointers have moved at least this far from their starting place. 221 float pointerGestureMultitouchMinDistance; // in pixels 222 223 // The transition from PRESS to SWIPE gesture mode can only occur when the 224 // cosine of the angle between the two vectors is greater than or equal to than this value 225 // which indicates that the vectors are oriented in the same direction. 226 // When the vectors are oriented in the exactly same direction, the cosine is 1.0. 227 // (In exactly opposite directions, the cosine is -1.0.) 228 float pointerGestureSwipeTransitionAngleCosine; 229 230 // The transition from PRESS to SWIPE gesture mode can only occur when the 231 // fingers are no more than this far apart relative to the diagonal size of 232 // the touch pad. For example, a ratio of 0.5 means that the fingers must be 233 // no more than half the diagonal size of the touch pad apart. 234 float pointerGestureSwipeMaxWidthRatio; 235 236 // The gesture movement speed factor relative to the size of the display. 237 // Movement speed applies when the fingers are moving in the same direction. 238 // Without acceleration, a full swipe of the touch pad diagonal in movement mode 239 // will cover this portion of the display diagonal. 240 float pointerGestureMovementSpeedRatio; 241 242 // The gesture zoom speed factor relative to the size of the display. 243 // Zoom speed applies when the fingers are mostly moving relative to each other 244 // to execute a scale gesture or similar. 245 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode 246 // will cover this portion of the display diagonal. 247 float pointerGestureZoomSpeedRatio; 248 249 // True to show the location of touches on the touch screen as spots. 250 bool showTouches; 251 252 // True if pointer capture is enabled. 253 bool pointerCapture; 254 255 // The set of currently disabled input devices. 256 SortedVector<int32_t> disabledDevices; 257 InputReaderConfigurationInputReaderConfiguration258 InputReaderConfiguration() : 259 virtualKeyQuietTime(0), 260 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f), 261 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f), 262 pointerGesturesEnabled(true), 263 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms 264 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second 265 pointerGestureTapInterval(150 * 1000000LL), // 150 ms 266 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms 267 pointerGestureTapSlop(10.0f), // 10 pixels 268 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms 269 pointerGestureMultitouchMinDistance(15), // 15 pixels 270 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees 271 pointerGestureSwipeMaxWidthRatio(0.25f), 272 pointerGestureMovementSpeedRatio(0.8f), 273 pointerGestureZoomSpeedRatio(0.3f), 274 showTouches(false), pointerCapture(false) { } 275 276 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const; 277 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId) 278 const; 279 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const; 280 std::optional<DisplayViewport> getDisplayViewportById(int32_t displayId) const; 281 void setDisplayViewports(const std::vector<DisplayViewport>& viewports); 282 283 284 void dump(std::string& dump) const; 285 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const; 286 287 private: 288 std::vector<DisplayViewport> mDisplays; 289 }; 290 291 struct TouchAffineTransformation { 292 float x_scale; 293 float x_ymix; 294 float x_offset; 295 float y_xmix; 296 float y_scale; 297 float y_offset; 298 TouchAffineTransformationTouchAffineTransformation299 TouchAffineTransformation() : 300 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f), 301 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) { 302 } 303 TouchAffineTransformationTouchAffineTransformation304 TouchAffineTransformation(float xscale, float xymix, float xoffset, 305 float yxmix, float yscale, float yoffset) : 306 x_scale(xscale), x_ymix(xymix), x_offset(xoffset), 307 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) { 308 } 309 310 void applyTo(float& x, float& y) const; 311 }; 312 313 /* 314 * Input reader policy interface. 315 * 316 * The input reader policy is used by the input reader to interact with the Window Manager 317 * and other system components. 318 * 319 * The actual implementation is partially supported by callbacks into the DVM 320 * via JNI. This interface is also mocked in the unit tests. 321 * 322 * These methods must NOT re-enter the input reader since they may be called while 323 * holding the input reader lock. 324 */ 325 class InputReaderPolicyInterface : public virtual RefBase { 326 protected: InputReaderPolicyInterface()327 InputReaderPolicyInterface() { } ~InputReaderPolicyInterface()328 virtual ~InputReaderPolicyInterface() { } 329 330 public: 331 /* Gets the input reader configuration. */ 332 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0; 333 334 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */ 335 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController( 336 int32_t deviceId) = 0; 337 338 /* Notifies the input reader policy that some input devices have changed 339 * and provides information about all current input devices. 340 */ 341 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0; 342 343 /* Gets the keyboard layout for a particular input device. */ 344 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay( 345 const InputDeviceIdentifier& identifier) = 0; 346 347 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */ 348 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0; 349 350 /* Gets the affine calibration associated with the specified device. */ 351 virtual TouchAffineTransformation getTouchAffineTransformation( 352 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0; 353 }; 354 355 } // namespace android 356 357 #endif // _UI_INPUT_READER_COMMON_H 358