Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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_TOUCH_INPUT_MAPPER_H |
| 18 | #define _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H |
| 19 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 22 | #include "CursorButtonAccumulator.h" |
| 23 | #include "CursorScrollAccumulator.h" |
| 24 | #include "EventHub.h" |
| 25 | #include "InputMapper.h" |
| 26 | #include "InputReaderBase.h" |
| 27 | #include "TouchButtonAccumulator.h" |
| 28 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | /* Raw axis information from the driver. */ |
| 32 | struct RawPointerAxes { |
| 33 | RawAbsoluteAxisInfo x; |
| 34 | RawAbsoluteAxisInfo y; |
| 35 | RawAbsoluteAxisInfo pressure; |
| 36 | RawAbsoluteAxisInfo touchMajor; |
| 37 | RawAbsoluteAxisInfo touchMinor; |
| 38 | RawAbsoluteAxisInfo toolMajor; |
| 39 | RawAbsoluteAxisInfo toolMinor; |
| 40 | RawAbsoluteAxisInfo orientation; |
| 41 | RawAbsoluteAxisInfo distance; |
| 42 | RawAbsoluteAxisInfo tiltX; |
| 43 | RawAbsoluteAxisInfo tiltY; |
| 44 | RawAbsoluteAxisInfo trackingId; |
| 45 | RawAbsoluteAxisInfo slot; |
| 46 | |
| 47 | RawPointerAxes(); |
| 48 | inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; } |
| 49 | inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; } |
| 50 | void clear(); |
| 51 | }; |
| 52 | |
| 53 | /* Raw data for a collection of pointers including a pointer id mapping table. */ |
| 54 | struct RawPointerData { |
| 55 | struct Pointer { |
| 56 | uint32_t id; |
| 57 | int32_t x; |
| 58 | int32_t y; |
| 59 | int32_t pressure; |
| 60 | int32_t touchMajor; |
| 61 | int32_t touchMinor; |
| 62 | int32_t toolMajor; |
| 63 | int32_t toolMinor; |
| 64 | int32_t orientation; |
| 65 | int32_t distance; |
| 66 | int32_t tiltX; |
| 67 | int32_t tiltY; |
| 68 | int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant |
| 69 | bool isHovering; |
| 70 | }; |
| 71 | |
| 72 | uint32_t pointerCount; |
| 73 | Pointer pointers[MAX_POINTERS]; |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 74 | BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 75 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 76 | |
| 77 | RawPointerData(); |
| 78 | void clear(); |
| 79 | void copyFrom(const RawPointerData& other); |
| 80 | void getCentroidOfTouchingPointers(float* outX, float* outY) const; |
| 81 | |
| 82 | inline void markIdBit(uint32_t id, bool isHovering) { |
| 83 | if (isHovering) { |
| 84 | hoveringIdBits.markBit(id); |
| 85 | } else { |
| 86 | touchingIdBits.markBit(id); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | inline void clearIdBits() { |
| 91 | hoveringIdBits.clear(); |
| 92 | touchingIdBits.clear(); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 93 | canceledIdBits.clear(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | inline const Pointer& pointerForId(uint32_t id) const { return pointers[idToIndex[id]]; } |
| 97 | |
| 98 | inline bool isHovering(uint32_t pointerIndex) { return pointers[pointerIndex].isHovering; } |
| 99 | }; |
| 100 | |
| 101 | /* Cooked data for a collection of pointers including a pointer id mapping table. */ |
| 102 | struct CookedPointerData { |
| 103 | uint32_t pointerCount; |
| 104 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 105 | PointerCoords pointerCoords[MAX_POINTERS]; |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 106 | BitSet32 hoveringIdBits, touchingIdBits, canceledIdBits, validIdBits; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 107 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 108 | |
| 109 | CookedPointerData(); |
| 110 | void clear(); |
| 111 | void copyFrom(const CookedPointerData& other); |
| 112 | |
| 113 | inline const PointerCoords& pointerCoordsForId(uint32_t id) const { |
| 114 | return pointerCoords[idToIndex[id]]; |
| 115 | } |
| 116 | |
| 117 | inline PointerCoords& editPointerCoordsWithId(uint32_t id) { |
| 118 | return pointerCoords[idToIndex[id]]; |
| 119 | } |
| 120 | |
| 121 | inline PointerProperties& editPointerPropertiesWithId(uint32_t id) { |
| 122 | return pointerProperties[idToIndex[id]]; |
| 123 | } |
| 124 | |
| 125 | inline bool isHovering(uint32_t pointerIndex) const { |
| 126 | return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id); |
| 127 | } |
| 128 | |
| 129 | inline bool isTouching(uint32_t pointerIndex) const { |
| 130 | return touchingIdBits.hasBit(pointerProperties[pointerIndex].id); |
| 131 | } |
Nathaniel R. Lewis | adb58ea | 2019-08-21 04:46:29 +0000 | [diff] [blame] | 132 | |
| 133 | inline bool hasPointerCoordsForId(uint32_t id) const { return validIdBits.hasBit(id); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | class TouchInputMapper : public InputMapper { |
| 137 | public: |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 138 | explicit TouchInputMapper(InputDeviceContext& deviceContext); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 139 | ~TouchInputMapper() override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 140 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 141 | uint32_t getSources() override; |
| 142 | void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; |
| 143 | void dump(std::string& dump) override; |
| 144 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) override; |
| 145 | void reset(nsecs_t when) override; |
| 146 | void process(const RawEvent* rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 147 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 148 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override; |
| 149 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override; |
| 150 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, |
| 151 | uint8_t* outFlags) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 152 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 153 | void cancelTouch(nsecs_t when, nsecs_t readTime) override; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 154 | void timeoutExpired(nsecs_t when) override; |
| 155 | void updateExternalStylusState(const StylusState& state) override; |
| 156 | std::optional<int32_t> getAssociatedDisplayId() override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 157 | |
| 158 | protected: |
| 159 | CursorButtonAccumulator mCursorButtonAccumulator; |
| 160 | CursorScrollAccumulator mCursorScrollAccumulator; |
| 161 | TouchButtonAccumulator mTouchButtonAccumulator; |
| 162 | |
| 163 | struct VirtualKey { |
| 164 | int32_t keyCode; |
| 165 | int32_t scanCode; |
| 166 | uint32_t flags; |
| 167 | |
| 168 | // computed hit box, specified in touch screen coords based on known display size |
| 169 | int32_t hitLeft; |
| 170 | int32_t hitTop; |
| 171 | int32_t hitRight; |
| 172 | int32_t hitBottom; |
| 173 | |
| 174 | inline bool isHit(int32_t x, int32_t y) const { |
| 175 | return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | // Input sources and device mode. |
| 180 | uint32_t mSource; |
| 181 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 182 | enum class DeviceMode { |
| 183 | DISABLED, // input is disabled |
| 184 | DIRECT, // direct mapping (touchscreen) |
| 185 | UNSCALED, // unscaled mapping (touchpad) |
| 186 | NAVIGATION, // unscaled mapping with assist gesture (touch navigation) |
| 187 | POINTER, // pointer mapping (pointer) |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 188 | |
| 189 | ftl_last = POINTER |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 190 | }; |
| 191 | DeviceMode mDeviceMode; |
| 192 | |
| 193 | // The reader's configuration. |
| 194 | InputReaderConfiguration mConfig; |
| 195 | |
| 196 | // Immutable configuration parameters. |
| 197 | struct Parameters { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 198 | enum class DeviceType { |
| 199 | TOUCH_SCREEN, |
| 200 | TOUCH_PAD, |
| 201 | TOUCH_NAVIGATION, |
| 202 | POINTER, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 203 | |
| 204 | ftl_last = POINTER |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | DeviceType deviceType; |
| 208 | bool hasAssociatedDisplay; |
| 209 | bool associatedDisplayIsExternal; |
| 210 | bool orientationAware; |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 211 | |
| 212 | enum class Orientation : int32_t { |
| 213 | ORIENTATION_0 = DISPLAY_ORIENTATION_0, |
| 214 | ORIENTATION_90 = DISPLAY_ORIENTATION_90, |
| 215 | ORIENTATION_180 = DISPLAY_ORIENTATION_180, |
| 216 | ORIENTATION_270 = DISPLAY_ORIENTATION_270, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 217 | |
| 218 | ftl_last = ORIENTATION_270 |
Prabir Pradhan | ac1c74f | 2021-08-20 16:09:32 -0700 | [diff] [blame] | 219 | }; |
| 220 | Orientation orientation; |
| 221 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 222 | bool hasButtonUnderPad; |
| 223 | std::string uniqueDisplayId; |
| 224 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 225 | enum class GestureMode { |
| 226 | SINGLE_TOUCH, |
| 227 | MULTI_TOUCH, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 228 | |
| 229 | ftl_last = MULTI_TOUCH |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 230 | }; |
| 231 | GestureMode gestureMode; |
| 232 | |
| 233 | bool wake; |
| 234 | } mParameters; |
| 235 | |
| 236 | // Immutable calibration parameters in parsed form. |
| 237 | struct Calibration { |
| 238 | // Size |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 239 | enum class SizeCalibration { |
| 240 | DEFAULT, |
| 241 | NONE, |
| 242 | GEOMETRIC, |
| 243 | DIAMETER, |
| 244 | BOX, |
| 245 | AREA, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | SizeCalibration sizeCalibration; |
| 249 | |
| 250 | bool haveSizeScale; |
| 251 | float sizeScale; |
| 252 | bool haveSizeBias; |
| 253 | float sizeBias; |
| 254 | bool haveSizeIsSummed; |
| 255 | bool sizeIsSummed; |
| 256 | |
| 257 | // Pressure |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 258 | enum class PressureCalibration { |
| 259 | DEFAULT, |
| 260 | NONE, |
| 261 | PHYSICAL, |
| 262 | AMPLITUDE, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | PressureCalibration pressureCalibration; |
| 266 | bool havePressureScale; |
| 267 | float pressureScale; |
| 268 | |
| 269 | // Orientation |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 270 | enum class OrientationCalibration { |
| 271 | DEFAULT, |
| 272 | NONE, |
| 273 | INTERPOLATED, |
| 274 | VECTOR, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | OrientationCalibration orientationCalibration; |
| 278 | |
| 279 | // Distance |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 280 | enum class DistanceCalibration { |
| 281 | DEFAULT, |
| 282 | NONE, |
| 283 | SCALED, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | DistanceCalibration distanceCalibration; |
| 287 | bool haveDistanceScale; |
| 288 | float distanceScale; |
| 289 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 290 | enum class CoverageCalibration { |
| 291 | DEFAULT, |
| 292 | NONE, |
| 293 | BOX, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | CoverageCalibration coverageCalibration; |
| 297 | |
| 298 | inline void applySizeScaleAndBias(float* outSize) const { |
| 299 | if (haveSizeScale) { |
| 300 | *outSize *= sizeScale; |
| 301 | } |
| 302 | if (haveSizeBias) { |
| 303 | *outSize += sizeBias; |
| 304 | } |
| 305 | if (*outSize < 0) { |
| 306 | *outSize = 0; |
| 307 | } |
| 308 | } |
| 309 | } mCalibration; |
| 310 | |
| 311 | // Affine location transformation/calibration |
| 312 | struct TouchAffineTransformation mAffineTransform; |
| 313 | |
| 314 | RawPointerAxes mRawPointerAxes; |
| 315 | |
| 316 | struct RawState { |
| 317 | nsecs_t when; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 318 | nsecs_t readTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 319 | |
| 320 | // Raw pointer sample data. |
| 321 | RawPointerData rawPointerData; |
| 322 | |
| 323 | int32_t buttonState; |
| 324 | |
| 325 | // Scroll state. |
| 326 | int32_t rawVScroll; |
| 327 | int32_t rawHScroll; |
| 328 | |
| 329 | void copyFrom(const RawState& other) { |
| 330 | when = other.when; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 331 | readTime = other.readTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 332 | rawPointerData.copyFrom(other.rawPointerData); |
| 333 | buttonState = other.buttonState; |
| 334 | rawVScroll = other.rawVScroll; |
| 335 | rawHScroll = other.rawHScroll; |
| 336 | } |
| 337 | |
| 338 | void clear() { |
| 339 | when = 0; |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 340 | readTime = 0; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 341 | rawPointerData.clear(); |
| 342 | buttonState = 0; |
| 343 | rawVScroll = 0; |
| 344 | rawHScroll = 0; |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | struct CookedState { |
| 349 | // Cooked pointer sample data. |
| 350 | CookedPointerData cookedPointerData; |
| 351 | |
| 352 | // Id bits used to differentiate fingers, stylus and mouse tools. |
| 353 | BitSet32 fingerIdBits; |
| 354 | BitSet32 stylusIdBits; |
| 355 | BitSet32 mouseIdBits; |
| 356 | |
| 357 | int32_t buttonState; |
| 358 | |
| 359 | void copyFrom(const CookedState& other) { |
| 360 | cookedPointerData.copyFrom(other.cookedPointerData); |
| 361 | fingerIdBits = other.fingerIdBits; |
| 362 | stylusIdBits = other.stylusIdBits; |
| 363 | mouseIdBits = other.mouseIdBits; |
| 364 | buttonState = other.buttonState; |
| 365 | } |
| 366 | |
| 367 | void clear() { |
| 368 | cookedPointerData.clear(); |
| 369 | fingerIdBits.clear(); |
| 370 | stylusIdBits.clear(); |
| 371 | mouseIdBits.clear(); |
| 372 | buttonState = 0; |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | std::vector<RawState> mRawStatesPending; |
| 377 | RawState mCurrentRawState; |
| 378 | CookedState mCurrentCookedState; |
| 379 | RawState mLastRawState; |
| 380 | CookedState mLastCookedState; |
| 381 | |
| 382 | // State provided by an external stylus |
| 383 | StylusState mExternalStylusState; |
| 384 | int64_t mExternalStylusId; |
| 385 | nsecs_t mExternalStylusFusionTimeout; |
| 386 | bool mExternalStylusDataPending; |
| 387 | |
| 388 | // True if we sent a HOVER_ENTER event. |
| 389 | bool mSentHoverEnter; |
| 390 | |
| 391 | // Have we assigned pointer IDs for this stream |
| 392 | bool mHavePointerIds; |
| 393 | |
| 394 | // Is the current stream of direct touch events aborted |
| 395 | bool mCurrentMotionAborted; |
| 396 | |
| 397 | // The time the primary pointer last went down. |
| 398 | nsecs_t mDownTime; |
| 399 | |
| 400 | // The pointer controller, or null if the device is not a pointer. |
Michael Wright | 17db18e | 2020-06-26 20:51:44 +0100 | [diff] [blame] | 401 | std::shared_ptr<PointerControllerInterface> mPointerController; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 402 | |
| 403 | std::vector<VirtualKey> mVirtualKeys; |
| 404 | |
| 405 | virtual void configureParameters(); |
| 406 | virtual void dumpParameters(std::string& dump); |
| 407 | virtual void configureRawPointerAxes(); |
| 408 | virtual void dumpRawPointerAxes(std::string& dump); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 409 | virtual void configureInputDevice(nsecs_t when, bool* outResetNeeded); |
| 410 | virtual void dumpDisplay(std::string& dump); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 411 | virtual void configureVirtualKeys(); |
| 412 | virtual void dumpVirtualKeys(std::string& dump); |
| 413 | virtual void parseCalibration(); |
| 414 | virtual void resolveCalibration(); |
| 415 | virtual void dumpCalibration(std::string& dump); |
| 416 | virtual void updateAffineTransformation(); |
| 417 | virtual void dumpAffineTransformation(std::string& dump); |
| 418 | virtual void resolveExternalStylusPresence(); |
| 419 | virtual bool hasStylus() const = 0; |
| 420 | virtual bool hasExternalStylus() const; |
| 421 | |
| 422 | virtual void syncTouch(nsecs_t when, RawState* outState) = 0; |
| 423 | |
| 424 | private: |
| 425 | // The current viewport. |
| 426 | // The components of the viewport are specified in the display's rotated orientation. |
| 427 | DisplayViewport mViewport; |
| 428 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 429 | // The width and height are obtained from the viewport and are specified |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 430 | // in the natural orientation. |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 431 | int32_t mDisplayWidth; |
| 432 | int32_t mDisplayHeight; |
Arthur Hung | 4197f6b | 2020-03-16 15:39:59 +0800 | [diff] [blame] | 433 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 434 | // The physical frame is the rectangle in the display's coordinate space that maps to the |
| 435 | // the logical display frame. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 436 | int32_t mPhysicalWidth; |
| 437 | int32_t mPhysicalHeight; |
| 438 | int32_t mPhysicalLeft; |
| 439 | int32_t mPhysicalTop; |
| 440 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 441 | // The orientation of the input device relative to that of the display panel. It specifies |
| 442 | // the rotation of the input device coordinates required to produce the display panel |
| 443 | // orientation, so it will depend on whether the device is orientation aware. |
| 444 | int32_t mInputDeviceOrientation; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 445 | |
| 446 | // Translation and scaling factors, orientation-independent. |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 447 | float mXScale; |
| 448 | float mXPrecision; |
| 449 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 450 | float mYScale; |
| 451 | float mYPrecision; |
| 452 | |
| 453 | float mGeometricScale; |
| 454 | |
| 455 | float mPressureScale; |
| 456 | |
| 457 | float mSizeScale; |
| 458 | |
| 459 | float mOrientationScale; |
| 460 | |
| 461 | float mDistanceScale; |
| 462 | |
| 463 | bool mHaveTilt; |
| 464 | float mTiltXCenter; |
| 465 | float mTiltXScale; |
| 466 | float mTiltYCenter; |
| 467 | float mTiltYScale; |
| 468 | |
| 469 | bool mExternalStylusConnected; |
| 470 | |
| 471 | // Oriented motion ranges for input device info. |
| 472 | struct OrientedRanges { |
| 473 | InputDeviceInfo::MotionRange x; |
| 474 | InputDeviceInfo::MotionRange y; |
| 475 | InputDeviceInfo::MotionRange pressure; |
| 476 | |
| 477 | bool haveSize; |
| 478 | InputDeviceInfo::MotionRange size; |
| 479 | |
| 480 | bool haveTouchSize; |
| 481 | InputDeviceInfo::MotionRange touchMajor; |
| 482 | InputDeviceInfo::MotionRange touchMinor; |
| 483 | |
| 484 | bool haveToolSize; |
| 485 | InputDeviceInfo::MotionRange toolMajor; |
| 486 | InputDeviceInfo::MotionRange toolMinor; |
| 487 | |
| 488 | bool haveOrientation; |
| 489 | InputDeviceInfo::MotionRange orientation; |
| 490 | |
| 491 | bool haveDistance; |
| 492 | InputDeviceInfo::MotionRange distance; |
| 493 | |
| 494 | bool haveTilt; |
| 495 | InputDeviceInfo::MotionRange tilt; |
| 496 | |
| 497 | OrientedRanges() { clear(); } |
| 498 | |
| 499 | void clear() { |
| 500 | haveSize = false; |
| 501 | haveTouchSize = false; |
| 502 | haveToolSize = false; |
| 503 | haveOrientation = false; |
| 504 | haveDistance = false; |
| 505 | haveTilt = false; |
| 506 | } |
| 507 | } mOrientedRanges; |
| 508 | |
| 509 | // Oriented dimensions and precision. |
| 510 | float mOrientedXPrecision; |
| 511 | float mOrientedYPrecision; |
| 512 | |
| 513 | struct CurrentVirtualKeyState { |
| 514 | bool down; |
| 515 | bool ignored; |
| 516 | nsecs_t downTime; |
| 517 | int32_t keyCode; |
| 518 | int32_t scanCode; |
| 519 | } mCurrentVirtualKey; |
| 520 | |
| 521 | // Scale factor for gesture or mouse based pointer movements. |
| 522 | float mPointerXMovementScale; |
| 523 | float mPointerYMovementScale; |
| 524 | |
| 525 | // Scale factor for gesture based zooming and other freeform motions. |
| 526 | float mPointerXZoomScale; |
| 527 | float mPointerYZoomScale; |
| 528 | |
| 529 | // The maximum swipe width. |
| 530 | float mPointerGestureMaxSwipeWidth; |
| 531 | |
| 532 | struct PointerDistanceHeapElement { |
| 533 | uint32_t currentPointerIndex : 8; |
| 534 | uint32_t lastPointerIndex : 8; |
| 535 | uint64_t distance : 48; // squared distance |
| 536 | }; |
| 537 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 538 | enum class PointerUsage { |
| 539 | NONE, |
| 540 | GESTURES, |
| 541 | STYLUS, |
| 542 | MOUSE, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 543 | }; |
| 544 | PointerUsage mPointerUsage; |
| 545 | |
| 546 | struct PointerGesture { |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 547 | enum class Mode { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 548 | // No fingers, button is not pressed. |
| 549 | // Nothing happening. |
| 550 | NEUTRAL, |
| 551 | |
| 552 | // No fingers, button is not pressed. |
| 553 | // Tap detected. |
| 554 | // Emits DOWN and UP events at the pointer location. |
| 555 | TAP, |
| 556 | |
| 557 | // Exactly one finger dragging following a tap. |
| 558 | // Pointer follows the active finger. |
| 559 | // Emits DOWN, MOVE and UP events at the pointer location. |
| 560 | // |
| 561 | // Detect double-taps when the finger goes up while in TAP_DRAG mode. |
| 562 | TAP_DRAG, |
| 563 | |
| 564 | // Button is pressed. |
| 565 | // Pointer follows the active finger if there is one. Other fingers are ignored. |
| 566 | // Emits DOWN, MOVE and UP events at the pointer location. |
| 567 | BUTTON_CLICK_OR_DRAG, |
| 568 | |
| 569 | // Exactly one finger, button is not pressed. |
| 570 | // Pointer follows the active finger. |
| 571 | // Emits HOVER_MOVE events at the pointer location. |
| 572 | // |
| 573 | // Detect taps when the finger goes up while in HOVER mode. |
| 574 | HOVER, |
| 575 | |
| 576 | // Exactly two fingers but neither have moved enough to clearly indicate |
| 577 | // whether a swipe or freeform gesture was intended. We consider the |
| 578 | // pointer to be pressed so this enables clicking or long-pressing on buttons. |
| 579 | // Pointer does not move. |
| 580 | // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate. |
| 581 | PRESS, |
| 582 | |
| 583 | // Exactly two fingers moving in the same direction, button is not pressed. |
| 584 | // Pointer does not move. |
| 585 | // Emits DOWN, MOVE and UP events with a single pointer coordinate that |
| 586 | // follows the midpoint between both fingers. |
| 587 | SWIPE, |
| 588 | |
| 589 | // Two or more fingers moving in arbitrary directions, button is not pressed. |
| 590 | // Pointer does not move. |
| 591 | // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow |
| 592 | // each finger individually relative to the initial centroid of the finger. |
| 593 | FREEFORM, |
| 594 | |
| 595 | // Waiting for quiet time to end before starting the next gesture. |
| 596 | QUIET, |
| 597 | }; |
| 598 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 599 | // When a gesture is sent to an unfocused window, return true if it can bring that window |
| 600 | // into focus, false otherwise. |
| 601 | static bool canGestureAffectWindowFocus(Mode mode) { |
| 602 | switch (mode) { |
| 603 | case Mode::TAP: |
| 604 | case Mode::TAP_DRAG: |
| 605 | case Mode::BUTTON_CLICK_OR_DRAG: |
| 606 | // Taps can affect window focus. |
| 607 | return true; |
| 608 | case Mode::FREEFORM: |
| 609 | case Mode::HOVER: |
| 610 | case Mode::NEUTRAL: |
| 611 | case Mode::PRESS: |
| 612 | case Mode::QUIET: |
| 613 | case Mode::SWIPE: |
| 614 | // Most gestures can be performed on an unfocused window, so they should not |
| 615 | // not affect window focus. |
| 616 | return false; |
| 617 | } |
| 618 | } |
| 619 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 620 | // Time the first finger went down. |
| 621 | nsecs_t firstTouchTime; |
| 622 | |
| 623 | // The active pointer id from the raw touch data. |
| 624 | int32_t activeTouchId; // -1 if none |
| 625 | |
| 626 | // The active pointer id from the gesture last delivered to the application. |
| 627 | int32_t activeGestureId; // -1 if none |
| 628 | |
| 629 | // Pointer coords and ids for the current and previous pointer gesture. |
| 630 | Mode currentGestureMode; |
| 631 | BitSet32 currentGestureIdBits; |
| 632 | uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 633 | PointerProperties currentGestureProperties[MAX_POINTERS]; |
| 634 | PointerCoords currentGestureCoords[MAX_POINTERS]; |
| 635 | |
| 636 | Mode lastGestureMode; |
| 637 | BitSet32 lastGestureIdBits; |
| 638 | uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 639 | PointerProperties lastGestureProperties[MAX_POINTERS]; |
| 640 | PointerCoords lastGestureCoords[MAX_POINTERS]; |
| 641 | |
| 642 | // Time the pointer gesture last went down. |
| 643 | nsecs_t downTime; |
| 644 | |
| 645 | // Time when the pointer went down for a TAP. |
| 646 | nsecs_t tapDownTime; |
| 647 | |
| 648 | // Time when the pointer went up for a TAP. |
| 649 | nsecs_t tapUpTime; |
| 650 | |
| 651 | // Location of initial tap. |
| 652 | float tapX, tapY; |
| 653 | |
| 654 | // Time we started waiting for quiescence. |
| 655 | nsecs_t quietTime; |
| 656 | |
| 657 | // Reference points for multitouch gestures. |
| 658 | float referenceTouchX; // reference touch X/Y coordinates in surface units |
| 659 | float referenceTouchY; |
| 660 | float referenceGestureX; // reference gesture X/Y coordinates in pixels |
| 661 | float referenceGestureY; |
| 662 | |
| 663 | // Distance that each pointer has traveled which has not yet been |
| 664 | // subsumed into the reference gesture position. |
| 665 | BitSet32 referenceIdBits; |
| 666 | struct Delta { |
| 667 | float dx, dy; |
| 668 | }; |
| 669 | Delta referenceDeltas[MAX_POINTER_ID + 1]; |
| 670 | |
| 671 | // Describes how touch ids are mapped to gesture ids for freeform gestures. |
| 672 | uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1]; |
| 673 | |
| 674 | // A velocity tracker for determining whether to switch active pointers during drags. |
| 675 | VelocityTracker velocityTracker; |
| 676 | |
| 677 | void reset() { |
| 678 | firstTouchTime = LLONG_MIN; |
| 679 | activeTouchId = -1; |
| 680 | activeGestureId = -1; |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 681 | currentGestureMode = Mode::NEUTRAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 682 | currentGestureIdBits.clear(); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 683 | lastGestureMode = Mode::NEUTRAL; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 684 | lastGestureIdBits.clear(); |
| 685 | downTime = 0; |
| 686 | velocityTracker.clear(); |
| 687 | resetTap(); |
| 688 | resetQuietTime(); |
| 689 | } |
| 690 | |
| 691 | void resetTap() { |
| 692 | tapDownTime = LLONG_MIN; |
| 693 | tapUpTime = LLONG_MIN; |
| 694 | } |
| 695 | |
| 696 | void resetQuietTime() { quietTime = LLONG_MIN; } |
| 697 | } mPointerGesture; |
| 698 | |
| 699 | struct PointerSimple { |
| 700 | PointerCoords currentCoords; |
| 701 | PointerProperties currentProperties; |
| 702 | PointerCoords lastCoords; |
| 703 | PointerProperties lastProperties; |
| 704 | |
| 705 | // True if the pointer is down. |
| 706 | bool down; |
| 707 | |
| 708 | // True if the pointer is hovering. |
| 709 | bool hovering; |
| 710 | |
| 711 | // Time the pointer last went down. |
| 712 | nsecs_t downTime; |
| 713 | |
| 714 | void reset() { |
| 715 | currentCoords.clear(); |
| 716 | currentProperties.clear(); |
| 717 | lastCoords.clear(); |
| 718 | lastProperties.clear(); |
| 719 | down = false; |
| 720 | hovering = false; |
| 721 | downTime = 0; |
| 722 | } |
| 723 | } mPointerSimple; |
| 724 | |
| 725 | // The pointer and scroll velocity controls. |
| 726 | VelocityControl mPointerVelocityControl; |
| 727 | VelocityControl mWheelXVelocityControl; |
| 728 | VelocityControl mWheelYVelocityControl; |
| 729 | |
| 730 | std::optional<DisplayViewport> findViewport(); |
| 731 | |
| 732 | void resetExternalStylus(); |
| 733 | void clearStylusDataPendingFlags(); |
| 734 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 735 | void sync(nsecs_t when, nsecs_t readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 736 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 737 | bool consumeRawTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 738 | void processRawTouches(bool timeout); |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 739 | void cookAndDispatch(nsecs_t when, nsecs_t readTime); |
| 740 | void dispatchVirtualKey(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, |
| 741 | int32_t keyEventAction, int32_t keyEventFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 742 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 743 | void dispatchTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 744 | void dispatchHoverExit(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 745 | void dispatchHoverEnterAndMove(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 746 | void dispatchButtonRelease(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 747 | void dispatchButtonPress(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 748 | const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData); |
| 749 | void cookPointerData(); |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 750 | void abortTouches(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 751 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 752 | void dispatchPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, |
| 753 | PointerUsage pointerUsage); |
| 754 | void abortPointerUsage(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 755 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 756 | void dispatchPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, |
| 757 | bool isTimeout); |
| 758 | void abortPointerGestures(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 759 | bool preparePointerGestures(nsecs_t when, bool* outCancelPreviousGesture, |
| 760 | bool* outFinishPreviousGesture, bool isTimeout); |
| 761 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 762 | void dispatchPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 763 | void abortPointerStylus(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 764 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 765 | void dispatchPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
| 766 | void abortPointerMouse(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 767 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 768 | void dispatchPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, bool down, |
| 769 | bool hovering); |
| 770 | void abortPointerSimple(nsecs_t when, nsecs_t readTime, uint32_t policyFlags); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 771 | |
| 772 | bool assignExternalStylusId(const RawState& state, bool timeout); |
| 773 | void applyExternalStylusButtonState(nsecs_t when); |
| 774 | void applyExternalStylusTouchState(nsecs_t when); |
| 775 | |
| 776 | // Dispatches a motion event. |
| 777 | // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the |
| 778 | // method will take care of setting the index and transmuting the action to DOWN or UP |
| 779 | // it is the first / last pointer to go down / up. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 780 | void dispatchMotion(nsecs_t when, nsecs_t readTime, uint32_t policyFlags, uint32_t source, |
| 781 | int32_t action, int32_t actionButton, int32_t flags, int32_t metaState, |
| 782 | int32_t buttonState, int32_t edgeFlags, const PointerProperties* properties, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 783 | const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits, |
| 784 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime); |
| 785 | |
| 786 | // Updates pointer coords and properties for pointers with specified ids that have moved. |
| 787 | // Returns true if any of them changed. |
| 788 | bool updateMovedPointers(const PointerProperties* inProperties, const PointerCoords* inCoords, |
| 789 | const uint32_t* inIdToIndex, PointerProperties* outProperties, |
| 790 | PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 791 | BitSet32 idBits) const; |
| 792 | |
Garfield Tan | c734e4f | 2021-01-15 20:01:39 -0800 | [diff] [blame] | 793 | // Returns if this touch device is a touch screen with an associated display. |
| 794 | bool isTouchScreen(); |
| 795 | // Updates touch spots if they are enabled. Should only be used when this device is a |
| 796 | // touchscreen. |
| 797 | void updateTouchSpots(); |
| 798 | |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 799 | bool isPointInsidePhysicalFrame(int32_t x, int32_t y) const; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 800 | const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y); |
| 801 | |
Siarhei Vishniakou | 5747998 | 2021-03-03 01:32:21 +0000 | [diff] [blame] | 802 | static void assignPointerIds(const RawState& last, RawState& current); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 803 | |
| 804 | const char* modeToString(DeviceMode deviceMode); |
Prabir Pradhan | 1728b21 | 2021-10-19 16:00:03 -0700 | [diff] [blame^] | 805 | void rotateAndScale(float& x, float& y) const; |
Prabir Pradhan | d7482e7 | 2021-03-09 13:54:55 -0800 | [diff] [blame] | 806 | |
| 807 | // Wrapper methods for interfacing with PointerController. These are used to convert points |
| 808 | // between the coordinate spaces used by InputReader and PointerController, if they differ. |
| 809 | void moveMouseCursor(float dx, float dy) const; |
| 810 | std::pair<float, float> getMouseCursorPosition() const; |
| 811 | void setMouseCursorPosition(float x, float y) const; |
| 812 | void setTouchSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 813 | BitSet32 spotIdBits, int32_t displayId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 814 | }; |
| 815 | |
| 816 | } // namespace android |
| 817 | |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 818 | #endif // _UI_INPUTREADER_TOUCH_INPUT_MAPPER_H |