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