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 #pragma once 18 19 #include <android/os/IInputConstants.h> 20 #include <input/DisplayViewport.h> 21 #include <input/Input.h> 22 #include <input/InputDevice.h> 23 #include <input/VelocityControl.h> 24 #include <input/VelocityTracker.h> 25 #include <stddef.h> 26 #include <ui/Rotation.h> 27 #include <unistd.h> 28 #include <utils/Errors.h> 29 #include <utils/RefBase.h> 30 31 #include <optional> 32 #include <set> 33 #include <unordered_map> 34 #include <vector> 35 36 #include "PointerControllerInterface.h" 37 #include "VibrationElement.h" 38 39 // Maximum supported size of a vibration pattern. 40 // Must be at least 2. 41 #define MAX_VIBRATE_PATTERN_SIZE 100 42 43 namespace android { 44 45 // --- InputReaderConfiguration --- 46 47 /* 48 * Input reader configuration. 49 * 50 * Specifies various options that modify the behavior of the input reader. 51 */ 52 struct InputReaderConfiguration { 53 // Describes changes that have occurred. 54 enum class Change : uint32_t { 55 // The mouse pointer speed changed. 56 POINTER_SPEED = 1u << 0, 57 58 // The pointer gesture control changed. 59 POINTER_GESTURE_ENABLEMENT = 1u << 1, 60 61 // The display size or orientation changed. 62 DISPLAY_INFO = 1u << 2, 63 64 // The keyboard layouts must be reloaded. 65 KEYBOARD_LAYOUTS = 1u << 4, 66 67 // The device name alias supplied by the may have changed for some devices. 68 DEVICE_ALIAS = 1u << 5, 69 70 // The location calibration matrix changed. 71 TOUCH_AFFINE_TRANSFORMATION = 1u << 6, 72 73 // The presence of an external stylus has changed. 74 EXTERNAL_STYLUS_PRESENCE = 1u << 7, 75 76 // The pointer capture mode has changed. 77 POINTER_CAPTURE = 1u << 8, 78 79 // The set of disabled input devices (disabledDevices) has changed. 80 ENABLED_STATE = 1u << 9, 81 82 // The device type has been updated. 83 DEVICE_TYPE = 1u << 10, 84 85 // The keyboard layout association has changed. 86 KEYBOARD_LAYOUT_ASSOCIATION = 1u << 11, 87 88 // The stylus button reporting configurations has changed. 89 STYLUS_BUTTON_REPORTING = 1u << 12, 90 91 // The touchpad settings changed. 92 TOUCHPAD_SETTINGS = 1u << 13, 93 94 // All devices must be reopened. 95 MUST_REOPEN = 1u << 31, 96 }; 97 98 // Gets the amount of time to disable virtual keys after the screen is touched 99 // in order to filter out accidental virtual key presses due to swiping gestures 100 // or taps near the edge of the display. May be 0 to disable the feature. 101 nsecs_t virtualKeyQuietTime; 102 103 // The excluded device names for the platform. 104 // Devices with these names will be ignored. 105 std::vector<std::string> excludedDeviceNames; 106 107 // The associations between input ports and display ports. 108 // Used to determine which DisplayViewport should be tied to which InputDevice. 109 std::unordered_map<std::string, uint8_t> inputPortToDisplayPortAssociations; 110 111 // The associations between input device ports and display unique ids. 112 // Used to determine which DisplayViewport should be tied to which InputDevice. 113 std::unordered_map<std::string, std::string> inputPortToDisplayUniqueIdAssociations; 114 115 // The associations between input device descriptor and display unique ids. 116 // Used to determine which DisplayViewport should be tied to which InputDevice. 117 std::unordered_map<std::string, std::string> inputDeviceDescriptorToDisplayUniqueIdAssociations; 118 119 // The associations between input device ports device types. 120 // This is used to determine which device type and source should be tied to which InputDevice. 121 std::unordered_map<std::string, std::string> deviceTypeAssociations; 122 123 // The map from the input device physical port location to the input device layout info. 124 // Can be used to determine the layout of the keyboard device. 125 std::unordered_map<std::string, KeyboardLayoutInfo> keyboardLayoutAssociations; 126 127 // The suggested display ID to show the cursor. 128 ui::LogicalDisplayId defaultPointerDisplayId; 129 130 // The mouse pointer speed, as a number from -7 (slowest) to 7 (fastest). 131 // 132 // Currently only used when the enable_new_mouse_pointer_ballistics flag is enabled. 133 int32_t mousePointerSpeed; 134 135 // Displays on which an acceleration curve shouldn't be applied for pointer movements from mice. 136 // 137 // Currently only used when the enable_new_mouse_pointer_ballistics flag is enabled. 138 std::set<ui::LogicalDisplayId> displaysWithMousePointerAccelerationDisabled; 139 140 // Velocity control parameters for mouse pointer movements. 141 // 142 // If the enable_new_mouse_pointer_ballistics flag is enabled, these are ignored and the values 143 // of mousePointerSpeed and mousePointerAccelerationEnabled used instead. 144 VelocityControlParameters pointerVelocityControlParameters; 145 146 // Velocity control parameters for mouse wheel movements. 147 VelocityControlParameters wheelVelocityControlParameters; 148 149 // True if pointer gestures are enabled. 150 bool pointerGesturesEnabled; 151 152 // Quiet time between certain pointer gesture transitions. 153 // Time to allow for all fingers or buttons to settle into a stable state before 154 // starting a new gesture. 155 nsecs_t pointerGestureQuietInterval; 156 157 // The minimum speed that a pointer must travel for us to consider switching the active 158 // touch pointer to it during a drag. This threshold is set to avoid switching due 159 // to noise from a finger resting on the touch pad (perhaps just pressing it down). 160 float pointerGestureDragMinSwitchSpeed; // in pixels per second 161 162 // Tap gesture delay time. 163 // The time between down and up must be less than this to be considered a tap. 164 nsecs_t pointerGestureTapInterval; 165 166 // Tap drag gesture delay time. 167 // The time between the previous tap's up and the next down must be less than 168 // this to be considered a drag. Otherwise, the previous tap is finished and a 169 // new tap begins. 170 // 171 // Note that the previous tap will be held down for this entire duration so this 172 // interval must be shorter than the long press timeout. 173 nsecs_t pointerGestureTapDragInterval; 174 175 // The distance in pixels that the pointer is allowed to move from initial down 176 // to up and still be called a tap. 177 float pointerGestureTapSlop; // in pixels 178 179 // Time after the first touch points go down to settle on an initial centroid. 180 // This is intended to be enough time to handle cases where the user puts down two 181 // fingers at almost but not quite exactly the same time. 182 nsecs_t pointerGestureMultitouchSettleInterval; 183 184 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when 185 // at least two pointers have moved at least this far from their starting place. 186 float pointerGestureMultitouchMinDistance; // in pixels 187 188 // The transition from PRESS to SWIPE gesture mode can only occur when the 189 // cosine of the angle between the two vectors is greater than or equal to than this value 190 // which indicates that the vectors are oriented in the same direction. 191 // When the vectors are oriented in the exactly same direction, the cosine is 1.0. 192 // (In exactly opposite directions, the cosine is -1.0.) 193 float pointerGestureSwipeTransitionAngleCosine; 194 195 // The transition from PRESS to SWIPE gesture mode can only occur when the 196 // fingers are no more than this far apart relative to the diagonal size of 197 // the touch pad. For example, a ratio of 0.5 means that the fingers must be 198 // no more than half the diagonal size of the touch pad apart. 199 float pointerGestureSwipeMaxWidthRatio; 200 201 // The gesture movement speed factor relative to the size of the display. 202 // Movement speed applies when the fingers are moving in the same direction. 203 // Without acceleration, a full swipe of the touch pad diagonal in movement mode 204 // will cover this portion of the display diagonal. 205 float pointerGestureMovementSpeedRatio; 206 207 // The gesture zoom speed factor relative to the size of the display. 208 // Zoom speed applies when the fingers are mostly moving relative to each other 209 // to execute a scale gesture or similar. 210 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode 211 // will cover this portion of the display diagonal. 212 float pointerGestureZoomSpeedRatio; 213 214 // The latest request to enable or disable Pointer Capture. 215 PointerCaptureRequest pointerCaptureRequest; 216 217 // The touchpad pointer speed, as a number from -7 (slowest) to 7 (fastest). 218 int32_t touchpadPointerSpeed; 219 220 // True to invert the touchpad scrolling direction, so that moving two fingers downwards on the 221 // touchpad scrolls the content upwards. 222 bool touchpadNaturalScrollingEnabled; 223 224 // True to enable tap-to-click on touchpads. 225 bool touchpadTapToClickEnabled; 226 227 // True to enable tap dragging on touchpads. 228 bool touchpadTapDraggingEnabled; 229 230 // True to enable a zone on the right-hand side of touchpads where clicks will be turned into 231 // context (a.k.a. "right") clicks. 232 bool touchpadRightClickZoneEnabled; 233 234 // The set of currently disabled input devices. 235 std::set<int32_t> disabledDevices; 236 237 // True if stylus button reporting through motion events should be enabled, in which case 238 // stylus button state changes are reported through motion events. 239 bool stylusButtonMotionEventsEnabled; 240 241 // True if a pointer icon should be shown for direct stylus pointers. 242 bool stylusPointerIconEnabled; 243 InputReaderConfigurationInputReaderConfiguration244 InputReaderConfiguration() 245 : virtualKeyQuietTime(0), 246 defaultPointerDisplayId(ui::LogicalDisplayId::DEFAULT), 247 mousePointerSpeed(0), 248 displaysWithMousePointerAccelerationDisabled(), 249 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 250 static_cast<float>( 251 android::os::IInputConstants:: 252 DEFAULT_POINTER_ACCELERATION)), 253 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f), 254 pointerGesturesEnabled(true), 255 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms 256 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second 257 pointerGestureTapInterval(150 * 1000000LL), // 150 ms 258 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms 259 pointerGestureTapSlop(10.0f), // 10 pixels 260 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms 261 pointerGestureMultitouchMinDistance(15), // 15 pixels 262 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees 263 pointerGestureSwipeMaxWidthRatio(0.25f), 264 pointerGestureMovementSpeedRatio(0.8f), 265 pointerGestureZoomSpeedRatio(0.3f), 266 pointerCaptureRequest(), 267 touchpadPointerSpeed(0), 268 touchpadNaturalScrollingEnabled(true), 269 touchpadTapToClickEnabled(true), 270 touchpadTapDraggingEnabled(false), 271 touchpadRightClickZoneEnabled(false), 272 stylusButtonMotionEventsEnabled(true), 273 stylusPointerIconEnabled(false) {} 274 275 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const; 276 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId) 277 const; 278 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const; 279 std::optional<DisplayViewport> getDisplayViewportById(ui::LogicalDisplayId displayId) const; 280 void setDisplayViewports(const std::vector<DisplayViewport>& viewports); 281 282 void dump(std::string& dump) const; 283 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const; 284 285 private: 286 std::vector<DisplayViewport> mDisplays; 287 }; 288 289 using ConfigurationChanges = ftl::Flags<InputReaderConfiguration::Change>; 290 291 // --- InputReaderInterface --- 292 293 /* The interface for the InputReader shared library. 294 * 295 * Manages one or more threads that process raw input events and sends cooked event data to an 296 * input listener. 297 * 298 * The implementation must guarantee thread safety for this interface. However, since the input 299 * listener is NOT thread safe, all calls to the listener must happen from the same thread. 300 */ 301 class InputReaderInterface { 302 public: InputReaderInterface()303 InputReaderInterface() {} ~InputReaderInterface()304 virtual ~InputReaderInterface() {} 305 /* Dumps the state of the input reader. 306 * 307 * This method may be called on any thread (usually by the input manager). */ 308 virtual void dump(std::string& dump) = 0; 309 310 /* Called by the heartbeat to ensures that the reader has not deadlocked. */ 311 virtual void monitor() = 0; 312 313 /* Makes the reader start processing events from the kernel. */ 314 virtual status_t start() = 0; 315 316 /* Makes the reader stop processing any more events. */ 317 virtual status_t stop() = 0; 318 319 /* Gets information about all input devices. 320 * 321 * This method may be called on any thread (usually by the input manager). 322 */ 323 virtual std::vector<InputDeviceInfo> getInputDevices() const = 0; 324 325 /* Query current input state. */ 326 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) = 0; 327 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) = 0; 328 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) = 0; 329 330 virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, 331 int32_t toKeyCode) const = 0; 332 333 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0; 334 335 /* Toggle Caps Lock */ 336 virtual void toggleCapsLockState(int32_t deviceId) = 0; 337 338 /* Determine whether physical keys exist for the given framework-domain key codes. */ 339 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, 340 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) = 0; 341 342 /* Requests that a reconfiguration of all input devices. 343 * The changes flag is a bitfield that indicates what has changed and whether 344 * the input devices must all be reopened. */ 345 virtual void requestRefreshConfiguration(ConfigurationChanges changes) = 0; 346 347 /* Controls the vibrator of a particular input device. */ 348 virtual void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat, 349 int32_t token) = 0; 350 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0; 351 352 virtual bool isVibrating(int32_t deviceId) = 0; 353 354 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0; 355 /* Get battery level of a particular input device. */ 356 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0; 357 /* Get battery status of a particular input device. */ 358 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0; 359 /* Get the device path for the battery of an input device. */ 360 virtual std::optional<std::string> getBatteryDevicePath(int32_t deviceId) = 0; 361 362 virtual std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) = 0; 363 364 virtual std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) = 0; 365 366 /* Return true if the device can send input events to the specified display. */ 367 virtual bool canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) = 0; 368 369 /* Enable sensor in input reader mapper. */ 370 virtual bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType, 371 std::chrono::microseconds samplingPeriod, 372 std::chrono::microseconds maxBatchReportLatency) = 0; 373 374 /* Disable sensor in input reader mapper. */ 375 virtual void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0; 376 377 /* Flush sensor data in input reader mapper. */ 378 virtual void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0; 379 380 /* Set color for the light */ 381 virtual bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) = 0; 382 /* Set player ID for the light */ 383 virtual bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) = 0; 384 /* Get light color */ 385 virtual std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) = 0; 386 /* Get light player ID */ 387 virtual std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) = 0; 388 389 /* Get the Bluetooth address of an input device, if known. */ 390 virtual std::optional<std::string> getBluetoothAddress(int32_t deviceId) const = 0; 391 392 /* Sysfs node change reported. Recreate device if required to incorporate the new sysfs nodes */ 393 virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0; 394 395 /* Get the ID of the InputDevice that was used most recently. 396 * 397 * Returns ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID if no device has been used since boot. 398 */ 399 virtual DeviceId getLastUsedInputDeviceId() = 0; 400 }; 401 402 // --- TouchAffineTransformation --- 403 404 struct TouchAffineTransformation { 405 float x_scale; 406 float x_ymix; 407 float x_offset; 408 float y_xmix; 409 float y_scale; 410 float y_offset; 411 TouchAffineTransformationTouchAffineTransformation412 TouchAffineTransformation() : 413 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f), 414 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) { 415 } 416 TouchAffineTransformationTouchAffineTransformation417 TouchAffineTransformation(float xscale, float xymix, float xoffset, 418 float yxmix, float yscale, float yoffset) : 419 x_scale(xscale), x_ymix(xymix), x_offset(xoffset), 420 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) { 421 } 422 423 void applyTo(float& x, float& y) const; 424 }; 425 426 // --- InputReaderPolicyInterface --- 427 428 /* 429 * Input reader policy interface. 430 * 431 * The input reader policy is used by the input reader to interact with the Window Manager 432 * and other system components. 433 * 434 * The actual implementation is partially supported by callbacks into the DVM 435 * via JNI. This interface is also mocked in the unit tests. 436 * 437 * These methods will NOT re-enter the input reader interface, so they may be called from 438 * any method in the input reader interface. 439 */ 440 class InputReaderPolicyInterface : public virtual RefBase { 441 protected: InputReaderPolicyInterface()442 InputReaderPolicyInterface() { } ~InputReaderPolicyInterface()443 virtual ~InputReaderPolicyInterface() { } 444 445 public: 446 /* Gets the input reader configuration. */ 447 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0; 448 449 /* Notifies the input reader policy that some input devices have changed 450 * and provides information about all current input devices. 451 */ 452 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0; 453 454 /* Gets the keyboard layout for a particular input device. */ 455 virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay( 456 const InputDeviceIdentifier& identifier, 457 const std::optional<KeyboardLayoutInfo> keyboardLayoutInfo) = 0; 458 459 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */ 460 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0; 461 462 /* Gets the affine calibration associated with the specified device. */ 463 virtual TouchAffineTransformation getTouchAffineTransformation( 464 const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) = 0; 465 /* Notifies the input reader policy that a stylus gesture has started. */ 466 virtual void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) = 0; 467 468 /* Returns true if any InputConnection is currently active. */ 469 virtual bool isInputMethodConnectionActive() = 0; 470 471 /* Gets the viewport of a particular display that the pointer device is associated with. If 472 * the pointer device is not associated with any display, it should ADISPLAY_IS_NONE to get 473 * the viewport that should be used. The device should get a new viewport using this method 474 * every time there is a display configuration change. The logical bounds of the viewport should 475 * be used as the range of possible values for pointing devices, like mice and touchpads. 476 */ 477 virtual std::optional<DisplayViewport> getPointerViewportForAssociatedDisplay( 478 ui::LogicalDisplayId associatedDisplayId = ui::LogicalDisplayId::INVALID) = 0; 479 }; 480 481 } // namespace android 482